| @@ -1,12 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | -// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange -- one-shot data/schema migrations; caching unwanted. | |
| 2 | + | |
| 3 | 3 | namespace WPDeveloper\BetterDocs\Core; |
| 4 | 4 | |
| 5 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 6 | - exit; | |
| 7 | -} | |
| 8 | - | |
| 9 | 5 | use WP_Query; |
| 10 | 6 | use WPDeveloper\BetterDocs\Utils\Base; |
| 11 | 7 | use WPDeveloper\BetterDocs\Utils\Database; |
| 12 | 8 | use WPDeveloper\BetterDocs\Dependencies\DI\Container; |
| @@ -35,9 +31,8 @@ | ||
| 35 | 31 | } |
| 36 | 32 | } |
| 37 | 33 | |
| 38 | 34 | $this->search_migration(); |
| 39 | - $this->fix_search_table_collation(); | |
| 40 | 35 | |
| 41 | 36 | /** |
| 42 | 37 | * Settings Migration |
| 43 | 38 | */ |
| @@ -54,9 +49,9 @@ | ||
| 54 | 49 | $args = [ |
| 55 | 50 | 'post_type' => 'docs', |
| 56 | 51 | 'post_status' => 'publish', |
| 57 | 52 | 'posts_per_page' => -1, |
| 58 | - 'suppress_filters' => true, // phpcs:ignore WordPressVIPMinimum.Hooks.PreGetPosts.PreGetPosts,WordPressVIPMinimum.Performance.WPQueryParams.SuppressFilters_suppress_filters -- migration must run on raw posts without language filters. | |
| 53 | + 'suppress_filters' => true, | |
| 59 | 54 | 's' => $key |
| 60 | 55 | ]; |
| 61 | 56 | |
| 62 | 57 | $loop = new WP_Query( $args ); |
| @@ -67,15 +62,14 @@ | ||
| 67 | 62 | $count = 0; |
| 68 | 63 | $not_found_count = $value; |
| 69 | 64 | } |
| 70 | 65 | |
| 71 | - // Use BINARY comparison to avoid collation mismatch errors | |
| 72 | - $keyword = $wpdb->get_var( | |
| 66 | + $keyword = $wpdb->get_var( | |
| 73 | 67 | $wpdb->prepare( |
| 74 | 68 | " |
| 75 | 69 | SELECT keyword |
| 76 | 70 | FROM {$wpdb->prefix}betterdocs_search_keyword |
| 77 | - WHERE BINARY keyword = %s", | |
| 71 | + WHERE keyword = %s", | |
| 78 | 72 | $key |
| 79 | 73 | ) |
| 80 | 74 | ); |
| 81 | 75 | |
| @@ -100,9 +94,9 @@ | ||
| 100 | 94 | [ |
| 101 | 95 | $wpdb->insert_id, |
| 102 | 96 | $count, |
| 103 | 97 | $not_found_count, |
| 104 | - gmdate( 'Y-m-d' ) | |
| 98 | + date( 'Y-m-d' ) | |
| 105 | 99 | ] |
| 106 | 100 | ) |
| 107 | 101 | ); |
| 108 | 102 | } |
| @@ -111,49 +105,6 @@ | ||
| 111 | 105 | |
| 112 | 106 | $this->database->save( 'betterdocs_search_data_migration', '1.0' ); |
| 113 | 107 | } |
| 114 | 108 | } |
| 115 | - } | |
| 116 | - | |
| 117 | - /** | |
| 118 | - * Fix collation issues in search tables | |
| 119 | - * Converts tables to proper UTF-8 charset and collation to prevent | |
| 120 | - * "Illegal mix of collations" errors when searching with non-Latin characters | |
| 121 | - * | |
| 122 | - * @since 1.0.2 | |
| 123 | - * @return void | |
| 124 | - */ | |
| 125 | - public function fix_search_table_collation() { | |
| 126 | - global $wpdb; | |
| 127 | - | |
| 128 | - // Check if migration already ran | |
| 129 | - if ( $this->database->get( 'betterdocs_search_collation_fixed', false ) ) { | |
| 130 | - return; | |
| 131 | - } | |
| 132 | - | |
| 133 | - // Get the WordPress default charset and collation; restrict to a safe identifier | |
| 134 | - // alphabet because they're interpolated into ALTER TABLE statements below. | |
| 135 | - $charset = preg_replace( '/[^A-Za-z0-9_]/', '', $wpdb->charset ? $wpdb->charset : 'utf8mb4' ); | |
| 136 | - $collate = preg_replace( '/[^A-Za-z0-9_]/', '', $wpdb->collate ? $wpdb->collate : 'utf8mb4_unicode_520_ci' ); | |
| 137 | - | |
| 138 | - // Fix search_keyword table | |
| 139 | - $search_keyword_table = $wpdb->prefix . 'betterdocs_search_keyword'; | |
| 140 | - // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange -- one-shot collation migration; identifiers come from $wpdb only. | |
| 141 | - if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $search_keyword_table ) ) === $search_keyword_table ) { | |
| 142 | - // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange,PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 143 | - $wpdb->query( "ALTER TABLE `{$search_keyword_table}` CONVERT TO CHARACTER SET {$charset} COLLATE {$collate}" ); | |
| 144 | - // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange,PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 145 | - $wpdb->query( "ALTER TABLE `{$search_keyword_table}` MODIFY keyword TEXT CHARACTER SET {$charset} COLLATE {$collate} NOT NULL" ); | |
| 146 | - } | |
| 147 | - | |
| 148 | - // Fix search_log table | |
| 149 | - $search_log_table = $wpdb->prefix . 'betterdocs_search_log'; | |
| 150 | - // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange | |
| 151 | - if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $search_log_table ) ) === $search_log_table ) { | |
| 152 | - // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange,PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 153 | - $wpdb->query( "ALTER TABLE `{$search_log_table}` CONVERT TO CHARACTER SET {$charset} COLLATE {$collate}" ); | |
| 154 | - } | |
| 155 | - | |
| 156 | - // Mark migration as complete | |
| 157 | - $this->database->save( 'betterdocs_search_collation_fixed', '1.0' ); | |
| 158 | 109 | } |
| 159 | 110 | } |