| @@ -1,7 +1,11 @@ | ||
| 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. | |
| 3 | +namespace WPDeveloper\BetterDocs\Core; | |
| 2 | 4 | |
| 3 | -namespace WPDeveloper\BetterDocs\Core; | |
| 5 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 6 | + exit; | |
| 7 | +} | |
| 4 | 8 | |
| 5 | 9 | use WP_Query; |
| 6 | 10 | use WPDeveloper\BetterDocs\Utils\Base; |
| 7 | 11 | use WPDeveloper\BetterDocs\Utils\Database; |
| @@ -32,8 +36,10 @@ | ||
| 32 | 36 | } |
| 33 | 37 | |
| 34 | 38 | $this->search_migration(); |
| 35 | 39 | $this->fix_search_table_collation(); |
| 40 | + $this->backfill_search_keyword_hash(); | |
| 41 | + $this->reset_formerly_locked_analytics_settings(); | |
| 36 | 42 | |
| 37 | 43 | /** |
| 38 | 44 | * Settings Migration |
| 39 | 45 | */ |
| @@ -50,9 +56,9 @@ | ||
| 50 | 56 | $args = [ |
| 51 | 57 | 'post_type' => 'docs', |
| 52 | 58 | 'post_status' => 'publish', |
| 53 | 59 | 'posts_per_page' => -1, |
| 54 | - 'suppress_filters' => true, | |
| 60 | + 'suppress_filters' => true, // phpcs:ignore WordPressVIPMinimum.Hooks.PreGetPosts.PreGetPosts,WordPressVIPMinimum.Performance.WPQueryParams.SuppressFilters_suppress_filters -- migration must run on raw posts without language filters. | |
| 55 | 61 | 's' => $key |
| 56 | 62 | ]; |
| 57 | 63 | |
| 58 | 64 | $loop = new WP_Query( $args ); |
| @@ -78,12 +84,13 @@ | ||
| 78 | 84 | if ( $keyword == null ) { |
| 79 | 85 | $insert = $wpdb->query( |
| 80 | 86 | $wpdb->prepare( |
| 81 | 87 | "INSERT INTO {$wpdb->prefix}betterdocs_search_keyword |
| 82 | - ( keyword ) | |
| 83 | - VALUES ( %s )", | |
| 88 | + ( keyword, keyword_hash ) | |
| 89 | + VALUES ( %s, %s )", | |
| 84 | 90 | [ |
| 85 | - $key | |
| 91 | + $key, | |
| 92 | + md5( $key ) | |
| 86 | 93 | ] |
| 87 | 94 | ) |
| 88 | 95 | ); |
| 89 | 96 | |
| @@ -96,9 +103,9 @@ | ||
| 96 | 103 | [ |
| 97 | 104 | $wpdb->insert_id, |
| 98 | 105 | $count, |
| 99 | 106 | $not_found_count, |
| 100 | - date( 'Y-m-d' ) | |
| 107 | + gmdate( 'Y-m-d' ) | |
| 101 | 108 | ] |
| 102 | 109 | ) |
| 103 | 110 | ); |
| 104 | 111 | } |
| @@ -125,29 +132,121 @@ | ||
| 125 | 132 | if ( $this->database->get( 'betterdocs_search_collation_fixed', false ) ) { |
| 126 | 133 | return; |
| 127 | 134 | } |
| 128 | 135 | |
| 129 | - // Get the WordPress default charset and collation | |
| 130 | - $charset = $wpdb->charset ? $wpdb->charset : 'utf8mb4'; | |
| 131 | - $collate = $wpdb->collate ? $wpdb->collate : 'utf8mb4_unicode_520_ci'; | |
| 136 | + // Get the WordPress default charset and collation; restrict to a safe identifier | |
| 137 | + // alphabet because they're interpolated into ALTER TABLE statements below. | |
| 138 | + $charset = preg_replace( '/[^A-Za-z0-9_]/', '', $wpdb->charset ? $wpdb->charset : 'utf8mb4' ); | |
| 139 | + $collate = preg_replace( '/[^A-Za-z0-9_]/', '', $wpdb->collate ? $wpdb->collate : 'utf8mb4_unicode_520_ci' ); | |
| 132 | 140 | |
| 133 | 141 | // Fix search_keyword table |
| 134 | 142 | $search_keyword_table = $wpdb->prefix . 'betterdocs_search_keyword'; |
| 135 | - if ( $wpdb->get_var( "SHOW TABLES LIKE '$search_keyword_table'" ) == $search_keyword_table ) { | |
| 136 | - // Convert table charset and collation | |
| 137 | - $wpdb->query( "ALTER TABLE {$search_keyword_table} CONVERT TO CHARACTER SET {$charset} COLLATE {$collate}" ); | |
| 138 | - | |
| 139 | - // Explicitly set keyword column collation | |
| 140 | - $wpdb->query( "ALTER TABLE {$search_keyword_table} MODIFY keyword TEXT CHARACTER SET {$charset} COLLATE {$collate} NOT NULL" ); | |
| 143 | + // 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. | |
| 144 | + if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $search_keyword_table ) ) === $search_keyword_table ) { | |
| 145 | + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange,PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 146 | + $wpdb->query( "ALTER TABLE `{$search_keyword_table}` CONVERT TO CHARACTER SET {$charset} COLLATE {$collate}" ); | |
| 147 | + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange,PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 148 | + $wpdb->query( "ALTER TABLE `{$search_keyword_table}` MODIFY keyword TEXT CHARACTER SET {$charset} COLLATE {$collate} NOT NULL" ); | |
| 141 | 149 | } |
| 142 | 150 | |
| 143 | 151 | // Fix search_log table |
| 144 | 152 | $search_log_table = $wpdb->prefix . 'betterdocs_search_log'; |
| 145 | - if ( $wpdb->get_var( "SHOW TABLES LIKE '$search_log_table'" ) == $search_log_table ) { | |
| 146 | - // Convert table charset and collation | |
| 147 | - $wpdb->query( "ALTER TABLE {$search_log_table} CONVERT TO CHARACTER SET {$charset} COLLATE {$collate}" ); | |
| 153 | + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange | |
| 154 | + if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $search_log_table ) ) === $search_log_table ) { | |
| 155 | + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange,PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 156 | + $wpdb->query( "ALTER TABLE `{$search_log_table}` CONVERT TO CHARACTER SET {$charset} COLLATE {$collate}" ); | |
| 148 | 157 | } |
| 149 | 158 | |
| 150 | 159 | // Mark migration as complete |
| 151 | 160 | $this->database->save( 'betterdocs_search_collation_fixed', '1.0' ); |
| 161 | + } | |
| 162 | + | |
| 163 | + /** | |
| 164 | + * Populate keyword_hash for rows that predate the column. | |
| 165 | + * | |
| 166 | + * The column is added by dbDelta with an empty default; until it holds the | |
| 167 | + * hash, insert_search_keyword() cannot find those rows by index and would | |
| 168 | + * insert duplicates. Runs in bounded batches so a large keyword table does | |
| 169 | + * not stall the request that triggers the upgrade — anything left over is | |
| 170 | + * picked up on the next admin load. | |
| 171 | + * | |
| 172 | + * @since 1.0.3 | |
| 173 | + * @return void | |
| 174 | + */ | |
| 175 | + public function backfill_search_keyword_hash() { | |
| 176 | + global $wpdb; | |
| 177 | + | |
| 178 | + if ( $this->database->get( 'betterdocs_search_keyword_hash_filled', false ) ) { | |
| 179 | + return; | |
| 180 | + } | |
| 181 | + | |
| 182 | + $table = $wpdb->prefix . 'betterdocs_search_keyword'; | |
| 183 | + | |
| 184 | + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- one-shot schema backfill; identifier comes from $wpdb. | |
| 185 | + if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) !== $table ) { | |
| 186 | + return; | |
| 187 | + } | |
| 188 | + | |
| 189 | + // Bail if the column never made it (dbDelta failed) — retry next load. | |
| 190 | + if ( ! $wpdb->get_results( "SHOW COLUMNS FROM `{$table}` LIKE 'keyword_hash'" ) ) { | |
| 191 | + return; | |
| 192 | + } | |
| 193 | + | |
| 194 | + $batches = 0; | |
| 195 | + do { | |
| 196 | + $updated = $wpdb->query( | |
| 197 | + "UPDATE `{$table}` SET keyword_hash = MD5( keyword ) WHERE keyword_hash = '' LIMIT 2000" | |
| 198 | + ); | |
| 199 | + $batches++; | |
| 200 | + } while ( $updated > 0 && $batches < 25 ); | |
| 201 | + | |
| 202 | + // Only finish once nothing is left, so a table larger than 50k keywords | |
| 203 | + // resumes instead of being marked done half-filled. | |
| 204 | + $remaining = (int) $wpdb->get_var( "SELECT COUNT(*) FROM `{$table}` WHERE keyword_hash = ''" ); | |
| 205 | + // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared | |
| 206 | + | |
| 207 | + if ( 0 === $remaining ) { | |
| 208 | + $this->database->save( 'betterdocs_search_keyword_hash_filled', '1.0.3' ); | |
| 209 | + } | |
| 210 | + } | |
| 211 | + | |
| 212 | + /** | |
| 213 | + * Clear the placeholder values the old Pro lock saved for the analytics | |
| 214 | + * collection settings. | |
| 215 | + * | |
| 216 | + * Until these settings were unlocked in Free (WP.org guideline 5), the | |
| 217 | + * settings screen merged Settings::pro_settings_default_values() into its | |
| 218 | + * form whenever Pro was inactive, so any save stored `analytics_from`, | |
| 219 | + * `unique_visitor_count`, `exclude_bot_analytics` and | |
| 220 | + * `child_category_exclude` as boolean false. Free ignored those keys then, | |
| 221 | + * so the tracker counted everyone, excluded bots and skipped unique visits. | |
| 222 | + * | |
| 223 | + * Free now reads the stored values, so a leftover `exclude_bot_analytics = | |
| 224 | + * false` would suddenly start counting bot traffic. A boolean false in | |
| 225 | + * `analytics_from` is the marker of that merge: no UI ever saves it (Free's | |
| 226 | + * select saves a string; Pro's Analytics → Settings never writes the key). | |
| 227 | + * When it is present, restore the behaviour those sites actually had. | |
| 228 | + * `unique_visitor_count` is left as stored — false there matches the old | |
| 229 | + * behaviour — and `child_category_exclude` false is already the default. | |
| 230 | + * | |
| 231 | + * @return void | |
| 232 | + */ | |
| 233 | + public function reset_formerly_locked_analytics_settings() { | |
| 234 | + if ( $this->database->get( 'betterdocs_unlocked_analytics_settings_migrated', false ) ) { | |
| 235 | + return; | |
| 236 | + } | |
| 237 | + | |
| 238 | + $settings = $this->database->get( 'betterdocs_settings', [] ); | |
| 239 | + | |
| 240 | + if ( is_array( $settings ) && array_key_exists( 'analytics_from', $settings ) && false === $settings['analytics_from'] ) { | |
| 241 | + $settings['analytics_from'] = 'everyone'; | |
| 242 | + | |
| 243 | + if ( array_key_exists( 'exclude_bot_analytics', $settings ) && ! $settings['exclude_bot_analytics'] ) { | |
| 244 | + $settings['exclude_bot_analytics'] = true; | |
| 245 | + } | |
| 246 | + | |
| 247 | + $this->database->save( 'betterdocs_settings', $settings ); | |
| 248 | + } | |
| 249 | + | |
| 250 | + $this->database->save( 'betterdocs_unlocked_analytics_settings_migrated', '1' ); | |
| 152 | 251 | } |
| 153 | 252 | } |