prefix . 'aieo_survey_choice_register'; } public static function ensure_table() { if ( null !== self::$table_ok ) { return self::$table_ok; } global $wpdb; $t = self::table(); if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) === $t ) { self::$table_ok = true; return true; } $charset = aieo_get_charset_collate(); $wpdb->query( "CREATE TABLE IF NOT EXISTS {$t} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, value_hash char(32) NOT NULL, observed_value varchar(255) NOT NULL DEFAULT '', observed_norm varchar(191) NOT NULL DEFAULT '', question_code varchar(64) NOT NULL DEFAULT '', choice_code varchar(64) NOT NULL DEFAULT '', choice_label varchar(191) NOT NULL DEFAULT '', status varchar(16) NOT NULL DEFAULT 'suggested', hits int(10) unsigned NOT NULL DEFAULT 1, updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), UNIQUE KEY uk_value (value_hash), KEY idx_norm (observed_norm), KEY idx_code (choice_code) ) {$charset}" ); self::$table_ok = (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) === $t; return self::$table_ok; } public static function normalise( $value ) { $value = trim( (string) $value ); if ( '' === $value ) { return ''; } if ( function_exists( 'mb_strtolower' ) ) { $value = mb_strtolower( $value, 'UTF-8' ); } else { $value = strtolower( $value ); } $value = remove_accents( $value ); $greek = array( 'ά' => 'α', 'έ' => 'ε', 'ή' => 'η', 'ί' => 'ι', 'ϊ' => 'ι', 'ΐ' => 'ι', 'ό' => 'ο', 'ύ' => 'υ', 'ϋ' => 'υ', 'ΰ' => 'υ', 'ώ' => 'ω', 'ς' => 'σ', ); $value = strtr( $value, $greek ); $value = preg_replace( '/\s+/u', ' ', $value ); return (string) $value; } private static function hash( $value ) { return md5( self::normalise( $value ) ); } public static function learn( $observed, $code, $label = '', $status = 'confirmed', $question = '' ) { global $wpdb; $observed = trim( (string) $observed ); $code = sanitize_key( (string) $code ); if ( '' === $observed || '' === $code || ! self::ensure_table() ) { return false; } $status = in_array( $status, self::STATUSES, true ) ? $status : 'suggested'; $existing = self::lookup( $observed ); if ( $existing && in_array( $existing['status'], array( 'confirmed', 'manual' ), true ) && in_array( $status, array( 'auto', 'suggested' ), true ) ) { return false; } $ok = false !== $wpdb->query( $wpdb->prepare( 'INSERT INTO ' . self::table() . ' (value_hash, observed_value, observed_norm, question_code, choice_code, choice_label, status, hits) VALUES (%s, %s, %s, %s, %s, %s, %s, 1) ON DUPLICATE KEY UPDATE observed_value = VALUES(observed_value), question_code = VALUES(question_code), choice_code = VALUES(choice_code), choice_label = VALUES(choice_label), status = VALUES(status), hits = hits + 1, updated_at = UTC_TIMESTAMP()', self::hash( $observed ), mb_substr( $observed, 0, 255 ), mb_substr( self::normalise( $observed ), 0, 191 ), sanitize_key( (string) $question ), $code, mb_substr( (string) $label, 0, 191 ), $status ) ); if ( $ok ) { self::changed(); } return $ok; } private static function changed() { do_action( 'aieo_survey_register_changed' ); } public static function forget( $observed ) { global $wpdb; if ( ! self::ensure_table() ) { return false; } $ok = false !== $wpdb->delete( self::table(), array( 'value_hash' => self::hash( $observed ) ), array( '%s' ) ); if ( $ok ) { self::changed(); } return $ok; } public static function lookup( $observed ) { global $wpdb; $observed = trim( (string) $observed ); if ( '' === $observed || ! self::ensure_table() ) { return null; } $row = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . self::table() . ' WHERE value_hash = %s', self::hash( $observed ) ), ARRAY_A ); return $row ? $row : null; } public static function code_for( $observed ) { $row = self::lookup( $observed ); return $row ? (string) $row['choice_code'] : ''; } public static function seed_from_question( array $question ) { $n = 0; foreach ( (array) $question['choices'] as $code => $label ) { if ( '' === trim( (string) $label ) ) { continue; } if ( self::learn( $label, $code, $label, 'auto', $question['code'] ) ) { $n++; } } return $n; } public static function all( $limit = 500 ) { global $wpdb; if ( ! self::ensure_table() ) { return array(); } return (array) $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . self::table() . ' ORDER BY updated_at DESC LIMIT %d', max( 1, (int) $limit ) ), ARRAY_A ); } public static function counts() { global $wpdb; if ( ! self::ensure_table() ) { return array(); } $out = array(); foreach ( (array) $wpdb->get_results( 'SELECT status, COUNT(*) AS n FROM ' . self::table() . ' GROUP BY status', ARRAY_A ) as $r ) { $out[ (string) $r['status'] ] = (int) $r['n']; } return $out; } }