| @@ -25,14 +25,38 @@ | ||
| 25 | 25 | '3.0.17' => array('disable_cache_directories_viewing'), |
| 26 | 26 | '3.1.0' => array('reset_wpo_plugin_cron_tasks_schedule'), |
| 27 | 27 | '3.1.4' => array('enable_minify_defer'), |
| 28 | 28 | '3.1.5' => array('update_minify_excludes'), |
| 29 | + '3.2.14' => array('update_3214_modify_cache_config_in_windows'), | |
| 30 | + '3.2.15' => array('update_3215_modify_cache_config_for_webp'), | |
| 31 | + '3.2.17' => array('update_3217_remove_htaccess_capability_tester_files'), | |
| 32 | + '3.2.18' => array('update_3218_reset_webp_serving_method'), | |
| 33 | + '3.2.19' => array('update_3219_modify_cache_config_for_cache_time'), | |
| 34 | + '3.3.0' => array('update_330_ua_async_exclusion_list'), | |
| 35 | + '3.5.0' => array( | |
| 36 | + 'update_350_move_content_of_existing_smush_logs_to_new_location', | |
| 37 | + 'update_350_delete_plugin_table_list_regenerate_in_new_location', | |
| 38 | + ), | |
| 39 | + '3.7.0' => array('update_370_disable_auto_preload_after_purge_feature'), | |
| 40 | + '3.8.0' => array('update_380_404_detector_table_create'), | |
| 41 | + '4.3.1' => array( | |
| 42 | + 'update_431_db_table_analysis_wipe_usage_data', | |
| 43 | + ), | |
| 44 | + '4.4.0' => array( | |
| 45 | + 'update_440_change_plugin_json_permissions', | |
| 46 | + ), | |
| 47 | + '4.6.1' => array( | |
| 48 | + 'update_461_update_post_meta_keys', | |
| 49 | + ), | |
| 50 | + '4.7.0' => array( | |
| 51 | + 'update_470_update_browser_cache_htaccess_config', | |
| 52 | + ), | |
| 29 | 53 | ); |
| 30 | 54 | |
| 31 | 55 | /** |
| 32 | 56 | * See if any database schema updates are needed, and perform them if so. |
| 33 | 57 | * Example Usage: |
| 34 | - * public static function update_101_add_new_column() { | |
| 58 | + * private static function update_101_add_new_column() { | |
| 35 | 59 | * $wpdb = $GLOBALS['wpdb']; |
| 36 | 60 | * $wpdb->query('ALTER TABLE tm_tasks ADD task_expiry varchar(300) AFTER id'); |
| 37 | 61 | * } |
| 38 | 62 | */ |
| @@ -48,37 +72,58 @@ | ||
| 48 | 72 | } |
| 49 | 73 | } |
| 50 | 74 | update_option('wpo_update_version', WPO_VERSION); |
| 51 | 75 | } |
| 76 | + | |
| 77 | + $wpo_version = get_option('wpo_version'); | |
| 78 | + | |
| 79 | + // If the wpo_version option is not set, but the db_version is, we assume that the wpo_version should be set to the db_version. | |
| 80 | + if (!$wpo_version) { | |
| 81 | + $wpo_version = $db_version ?: WPO_VERSION; | |
| 82 | + update_option('wpo_version', $wpo_version); | |
| 83 | + } | |
| 84 | + | |
| 85 | + if ($wpo_version && version_compare($our_version, $wpo_version, '!=')) { | |
| 86 | + update_option('wpo_version', WPO_VERSION); | |
| 87 | + // Trigger the activation actions to ensure that any necessary setup is performed when the version changes. | |
| 88 | + WPO_Activation::silence_activation_actions(); | |
| 89 | + } | |
| 52 | 90 | } |
| 53 | 91 | |
| 54 | 92 | /** |
| 55 | 93 | * Delete old semaphore locks from options database table. |
| 56 | 94 | */ |
| 57 | - public static function delete_old_locks() { | |
| 95 | + private static function delete_old_locks() { | |
| 58 | 96 | global $wpdb; |
| 97 | + | |
| 98 | + $query = "DELETE FROM `{$wpdb->options}`". | |
| 99 | + " WHERE (option_name LIKE ('updraft_semaphore_%')". | |
| 100 | + " OR option_name LIKE ('updraft_last_lock_time_%')". | |
| 101 | + " OR option_name LIKE ('updraft_locked_%')". | |
| 102 | + " OR option_name LIKE ('updraft_unlocked_%'))". | |
| 103 | + " AND ". | |
| 104 | + "(option_name LIKE ('%smush')". | |
| 105 | + " OR option_name LIKE ('%load-url-task'));"; | |
| 59 | 106 | |
| 60 | - // using this query we delete all rows related to locks. | |
| 61 | - $query = "DELETE FROM {$wpdb->options}". | |
| 62 | - " WHERE (option_name LIKE ('updraft_semaphore_%')". | |
| 63 | - " OR option_name LIKE ('updraft_last_lock_time_%')". | |
| 64 | - " OR option_name LIKE ('updraft_locked_%')". | |
| 65 | - " OR option_name LIKE ('updraft_unlocked_%'))". | |
| 66 | - " AND ". | |
| 67 | - "(option_name LIKE ('%smush')". | |
| 68 | - " OR option_name LIKE ('%load-url-task'));"; | |
| 107 | + $wpdb->query($query); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Already prepared query, uses only hardcoded strings | |
| 69 | 108 | |
| 70 | - $wpdb->query($query); | |
| 71 | 109 | } |
| 72 | 110 | |
| 73 | 111 | /** |
| 74 | 112 | * Disable cache directories viewing. |
| 75 | 113 | */ |
| 76 | - public static function disable_cache_directories_viewing() { | |
| 114 | + private static function disable_cache_directories_viewing() { | |
| 115 | + if (!function_exists('wpo_disable_cache_directories_viewing')) { | |
| 116 | + include_once WPO_PLUGIN_MAIN_PATH . 'cache/file-based-page-cache-functions.php'; | |
| 117 | + } | |
| 118 | + // Check if it's not a new installation | |
| 119 | + if (self::is_new_install()) { | |
| 120 | + return; | |
| 121 | + } | |
| 77 | 122 | wpo_disable_cache_directories_viewing(); |
| 78 | 123 | } |
| 79 | - | |
| 80 | - public static function reset_wpo_plugin_cron_tasks_schedule() { | |
| 124 | + | |
| 125 | + private static function reset_wpo_plugin_cron_tasks_schedule() { | |
| 81 | 126 | wp_clear_scheduled_hook('wpo_plugin_cron_tasks'); |
| 82 | 127 | } |
| 83 | 128 | |
| 84 | 129 | /** |
| @@ -85,11 +130,11 @@ | ||
| 85 | 130 | * Update Minify Defer option (The option was hidden until now, but we're changing the setting) |
| 86 | 131 | * |
| 87 | 132 | * @return void |
| 88 | 133 | */ |
| 89 | - public static function enable_minify_defer() { | |
| 134 | + private static function enable_minify_defer() { | |
| 90 | 135 | if (!function_exists('wp_optimize_minify_config')) { |
| 91 | - include_once WPO_PLUGIN_MAIN_PATH . '/minify/class-wp-optimize-minify-config.php'; | |
| 136 | + include_once WPO_PLUGIN_MAIN_PATH . 'minify/class-wp-optimize-minify-config.php'; | |
| 92 | 137 | } |
| 93 | 138 | $current_setting = wp_optimize_minify_config()->get('enable_defer_js'); |
| 94 | 139 | if (true === $current_setting) { |
| 95 | 140 | wp_optimize_minify_config()->update(array('enable_defer_js' => 'all')); |
| @@ -102,16 +147,15 @@ | ||
| 102 | 147 | * Update Minify default exclude options |
| 103 | 148 | * |
| 104 | 149 | * @return void |
| 105 | 150 | */ |
| 106 | - public static function update_minify_excludes() { | |
| 107 | - if (!WPO_MINIFY_PHP_VERSION_MET) return; | |
| 151 | + private static function update_minify_excludes() { | |
| 108 | 152 | if (!function_exists('wp_optimize_minify_config')) { |
| 109 | - include_once WPO_PLUGIN_MAIN_PATH . '/minify/class-wp-optimize-minify-config.php'; | |
| 153 | + include_once WPO_PLUGIN_MAIN_PATH . 'minify/class-wp-optimize-minify-config.php'; | |
| 110 | 154 | } |
| 111 | 155 | |
| 112 | 156 | if (!class_exists('WP_Optimize_Minify_Functions')) { |
| 113 | - include_once WPO_PLUGIN_MAIN_PATH . '/minify/class-wp-optimize-minify-functions.php'; | |
| 157 | + include_once WPO_PLUGIN_MAIN_PATH . 'minify/class-wp-optimize-minify-functions.php'; | |
| 114 | 158 | } |
| 115 | 159 | |
| 116 | 160 | $new_default_items = array( |
| 117 | 161 | 'elementor-admin-bar', |
| @@ -125,9 +169,9 @@ | ||
| 125 | 169 | // Get the lists as saved in the DB |
| 126 | 170 | $current_blacklist = wp_optimize_minify_config()->get('blacklist'); |
| 127 | 171 | $current_ignorelist = wp_optimize_minify_config()->get('ignore_list'); |
| 128 | 172 | |
| 129 | - // Only proceed if the the upgrade hasn't been done yet, i.e. the values aren't arrays | |
| 173 | + // Only proceed if the upgrade hasn't been done yet, i.e. the values aren't arrays | |
| 130 | 174 | if (is_array($current_blacklist) && is_array($current_ignorelist)) return; |
| 131 | 175 | |
| 132 | 176 | $current_blacklist = array_map('trim', explode("\n", $current_blacklist)); |
| 133 | 177 | $current_ignorelist = array_map('trim', explode("\n", $current_ignorelist)); |
| @@ -147,8 +191,273 @@ | ||
| 147 | 191 | |
| 148 | 192 | // Update the options |
| 149 | 193 | wp_optimize_minify_config()->update(array('blacklist' => $user_excluded_blacklist_items)); |
| 150 | 194 | wp_optimize_minify_config()->update(array('ignore_list' => $user_excluded_ignorelist_items)); |
| 195 | + } | |
| 196 | + | |
| 197 | + /** | |
| 198 | + * Checks if it's a new installation of the plugin, as opposed to being updated. | |
| 199 | + * | |
| 200 | + * @return bool | |
| 201 | + */ | |
| 202 | + private static function is_new_install() { | |
| 203 | + $db_version = get_option('wpo_update_version'); | |
| 204 | + return !$db_version; | |
| 205 | + } | |
| 206 | + | |
| 207 | + /** | |
| 208 | + * Modifies the cache configuration for Windows OS by regenerating the 'uploads' path. | |
| 209 | + */ | |
| 210 | + private static function update_3214_modify_cache_config_in_windows() { | |
| 211 | + | |
| 212 | + // Check if the OS is Windows and it's not a new installation. | |
| 213 | + if ('WIN' !== strtoupper(substr(PHP_OS, 0, 3)) || self::is_new_install()) { | |
| 214 | + return; | |
| 215 | + } | |
| 216 | + | |
| 217 | + // Retrieve the current cache configuration | |
| 218 | + $config = WPO_Cache_Config::instance()->get(); | |
| 219 | + | |
| 220 | + // Remove the 'uploads' path from the configuration, this will force the 'uploads' path to regenerate from the defaults | |
| 221 | + if (isset($config['uploads'])) { | |
| 222 | + unset($config['uploads']); | |
| 223 | + } | |
| 224 | + | |
| 225 | + // Update the cache configuration | |
| 226 | + WPO_Cache_Config::instance()->update($config); | |
| 227 | + } | |
| 228 | + | |
| 229 | + /** | |
| 230 | + * Updates the cache configuration to incorporate the new 'use_webp_images' default setting in the cache config. | |
| 231 | + */ | |
| 232 | + private static function update_3215_modify_cache_config_for_webp() { | |
| 233 | + | |
| 234 | + // Check if it's not a new installation | |
| 235 | + if (self::is_new_install()) { | |
| 236 | + return; | |
| 237 | + } | |
| 238 | + | |
| 239 | + // Retrieve the current status of page caching and WebP conversion | |
| 240 | + $cache_enabled = (bool) WPO_Cache_Config::instance()->get_option('enable_page_caching'); | |
| 241 | + $webp_enabled = (bool) WP_Optimize()->get_options()->get_option('webp_conversion'); | |
| 242 | + | |
| 243 | + // If both page caching and WebP conversion are enabled, update the cache configuration | |
| 244 | + if ($cache_enabled && $webp_enabled) { | |
| 245 | + $config = WPO_Cache_Config::instance()->get(); | |
| 246 | + $config['use_webp_images'] = true; | |
| 247 | + WPO_Cache_Config::instance()->update($config); | |
| 248 | + } | |
| 249 | + } | |
| 250 | + | |
| 251 | + /** | |
| 252 | + * Remove files in `uploads/wpo` folder created by htaccess capability tester library which is removed in 3.2.17 version | |
| 253 | + */ | |
| 254 | + private static function update_3217_remove_htaccess_capability_tester_files() { | |
| 255 | + if (self::is_new_install()) return; | |
| 256 | + WPO_Uninstall::delete_wpo_folder(); | |
| 257 | + } | |
| 258 | + | |
| 259 | + /** | |
| 260 | + * Resets WebP serving method | |
| 261 | + * | |
| 262 | + * We removed `uploads/wpo` folder in 3.2.17, When the redirection is possible | |
| 263 | + * it doesn't cause issues. However, when using alter html method | |
| 264 | + * WPO_WebP_Self_Test::is_webp_served requests a non-existing `uploads/wpo/images/wpo_logo_small.png` | |
| 265 | + * which causes High CPU usage problem | |
| 266 | + */ | |
| 267 | + private static function update_3218_reset_webp_serving_method() { | |
| 268 | + if (self::is_new_install()) return; | |
| 269 | + WP_Optimize()->get_webp_instance()->reset_webp_serving_method(); | |
| 270 | + } | |
| 271 | + | |
| 272 | + /** | |
| 273 | + * Updates cache config file with date and time format | |
| 274 | + */ | |
| 275 | + private static function update_3219_modify_cache_config_for_cache_time() { | |
| 276 | + if (self::is_new_install()) return; | |
| 277 | + $config = WPO_Cache_Config::instance()->get(); | |
| 278 | + $cache_enabled = $config['enable_page_caching']; | |
| 279 | + if ($cache_enabled) { | |
| 280 | + WPO_Cache_Config::instance()->update($config); | |
| 281 | + } | |
| 282 | + } | |
| 283 | + | |
| 284 | + /** | |
| 285 | + * Update async loading exclusion UA list | |
| 286 | + */ | |
| 287 | + private static function update_330_ua_async_exclusion_list() { | |
| 288 | + if (self::is_new_install()) return; | |
| 289 | + | |
| 290 | + $config = wp_optimize_minify_config(); | |
| 291 | + $defaults = $config->get_defaults(); | |
| 292 | + $new_list = $defaults['ualist']; | |
| 293 | + | |
| 294 | + $config->update(array( | |
| 295 | + 'ualist' => $new_list | |
| 296 | + )); | |
| 297 | + } | |
| 298 | + | |
| 299 | + /** | |
| 300 | + * Move existing smush log content in old path (uploads/smush-*) to new path (uploads/wpo/logs/smush-*) | |
| 301 | + */ | |
| 302 | + private static function update_350_move_content_of_existing_smush_logs_to_new_location() { | |
| 303 | + if (self::is_new_install()) return; | |
| 304 | + | |
| 305 | + $upload_base = WP_Optimize_Utils::get_base_upload_dir(); | |
| 306 | + | |
| 307 | + $old_file = $upload_base . 'smush-' . substr(md5(wp_salt()), 0, 20) . '.log'; | |
| 308 | + $new_file = WP_Optimize_Utils::get_log_file_path('smush'); | |
| 309 | + | |
| 310 | + // Check if only old file exists, if new file exists no need to overwrite its content | |
| 311 | + if (is_file($old_file) && !is_file($new_file)) { | |
| 312 | + $old_file_content = file_get_contents($old_file); | |
| 313 | + file_put_contents($new_file, $old_file_content); | |
| 314 | + } | |
| 315 | + | |
| 316 | + // Delete all smush log files in the old path (uploads/smush-*) | |
| 317 | + if (!function_exists('glob')) return; | |
| 318 | + $files = glob($upload_base . 'smush-*.log'); | |
| 319 | + if (false === $files) return; | |
| 320 | + foreach ($files as $file) { | |
| 321 | + if (is_file($file)) { | |
| 322 | + wp_delete_file($file); | |
| 323 | + } | |
| 324 | + } | |
| 325 | + } | |
| 326 | + | |
| 327 | + /** | |
| 328 | + * Delete wpo-plugins-tables-list.json file from old location and regenerate in new location | |
| 329 | + */ | |
| 330 | + private static function update_350_delete_plugin_table_list_regenerate_in_new_location() { | |
| 331 | + if (self::is_new_install()) return; | |
| 332 | + | |
| 333 | + // JSON file was moved to /wpo/ subfolder. Delete old files. | |
| 334 | + $upload_base = WP_Optimize_Utils::get_base_upload_dir(); | |
| 335 | + | |
| 336 | + $old_file = $upload_base . 'wpo-plugins-tables-list.json'; | |
| 337 | + if (is_file($old_file)) { | |
| 338 | + wp_delete_file($old_file); | |
| 339 | + } | |
| 340 | + // JSON file not present in the new location, regenerate it | |
| 341 | + $new_file = $upload_base . 'wpo/wpo-plugins-tables-list.json'; | |
| 342 | + if (!is_file($new_file)) { | |
| 343 | + WP_Optimize()->get_db_info()->update_plugin_json(); | |
| 344 | + } | |
| 345 | + } | |
| 346 | + | |
| 347 | + /** | |
| 348 | + * Disable auto preloading after purge feature for existing users | |
| 349 | + */ | |
| 350 | + private static function update_370_disable_auto_preload_after_purge_feature() { | |
| 351 | + if (self::is_new_install()) return; | |
| 352 | + $config = WPO_Cache_Config::instance()->get(); | |
| 353 | + $cache_enabled = $config['enable_page_caching']; | |
| 354 | + if ($cache_enabled) { | |
| 355 | + $config['auto_preload_purged_contents'] = false; | |
| 356 | + WPO_Cache_Config::instance()->update($config); | |
| 357 | + } | |
| 358 | + } | |
| 359 | + | |
| 360 | + /** | |
| 361 | + * Iterate over plugin utils tables creation, using WP_Optimize_Table_Management | |
| 362 | + */ | |
| 363 | + private static function update_380_404_detector_table_create() { | |
| 364 | + if (self::is_new_install()) return; | |
| 365 | + WP_Optimize()->get_table_management()->create_plugin_tables(); | |
| 366 | + } | |
| 367 | + | |
| 368 | + /** | |
| 369 | + * Wipe WPO_DB_Table_Analysis usage data | |
| 370 | + */ | |
| 371 | + private static function update_431_db_table_analysis_wipe_usage_data() { | |
| 372 | + if (self::is_new_install()) return; | |
| 373 | + | |
| 374 | + if (class_exists('WPO_DB_Table_Analysis')) { | |
| 375 | + WPO_DB_Table_Analysis::wipe_usage_data(); | |
| 376 | + } | |
| 377 | + } | |
| 378 | + | |
| 379 | + /** | |
| 380 | + * Change permission for wpo-plugins-tables-list.json file | |
| 381 | + */ | |
| 382 | + private static function update_440_change_plugin_json_permissions() { | |
| 383 | + if (self::is_new_install()) return; | |
| 384 | + | |
| 385 | + WP_Optimize()->get_db_info()->change_plugin_json_permissions(); | |
| 386 | + } | |
| 387 | + | |
| 388 | + /** | |
| 389 | + * Prefix WPO specific post meta keys with `_` to make it private. | |
| 390 | + */ | |
| 391 | + private static function update_461_update_post_meta_keys() { | |
| 392 | + if (self::is_new_install()) return; | |
| 393 | + | |
| 394 | + $keys_to_convert = array( | |
| 395 | + 'smush-complete', | |
| 396 | + 'smush-marked', | |
| 397 | + 'smush-info', | |
| 398 | + 'smush-stats', | |
| 399 | + 'original-file', | |
| 400 | + 'wpo-webp-conversion-complete', | |
| 401 | + ); | |
| 402 | + | |
| 403 | + if (is_multisite()) { | |
| 404 | + $sites = WP_Optimize()->get_sites(); | |
| 405 | + foreach ($sites as $site) { | |
| 406 | + $blog_id = $site->blog_id; | |
| 407 | + switch_to_blog($blog_id); | |
| 408 | + self::update_461_convert_post_meta_keys($keys_to_convert); | |
| 409 | + restore_current_blog(); | |
| 410 | + } | |
| 411 | + } else { | |
| 412 | + self::update_461_convert_post_meta_keys($keys_to_convert); | |
| 413 | + } | |
| 414 | + | |
| 415 | + } | |
| 416 | + | |
| 417 | + /** | |
| 418 | + * Perform the post meta key renaming for the current blog. | |
| 419 | + * | |
| 420 | + * @param array $keys_to_convert List of meta keys to prefix. | |
| 421 | + */ | |
| 422 | + private static function update_461_convert_post_meta_keys(array $keys_to_convert) { | |
| 423 | + global $wpdb; | |
| 424 | + | |
| 425 | + foreach ($keys_to_convert as $old_key) { | |
| 426 | + if ('wpo-webp-conversion-complete' !== $old_key) { | |
| 427 | + $new_key = '_wpo-' . $old_key; | |
| 428 | + } else { | |
| 429 | + $new_key = '_' . $old_key; | |
| 430 | + } | |
| 431 | + | |
| 432 | + $result = $wpdb->query($wpdb->prepare( | |
| 433 | + "UPDATE {$wpdb->postmeta} SET meta_key = %s WHERE meta_key = %s", | |
| 434 | + $new_key, | |
| 435 | + $old_key | |
| 436 | + )); | |
| 437 | + | |
| 438 | + if (false === $result) { | |
| 439 | + WP_Optimize()->log( | |
| 440 | + sprintf( | |
| 441 | + 'Update 4.6.1: Failed to rename post meta key "%s" to "%s" on blog %d. DB error: %s', | |
| 442 | + $old_key, | |
| 443 | + $new_key, | |
| 444 | + get_current_blog_id(), | |
| 445 | + $wpdb->last_error | |
| 446 | + ) | |
| 447 | + ); | |
| 448 | + } | |
| 449 | + } | |
| 450 | + } | |
| 451 | + | |
| 452 | + /** | |
| 453 | + * Update the browser cache htaccess config due to updated rules. | |
| 454 | + */ | |
| 455 | + private static function update_470_update_browser_cache_htaccess_config() { | |
| 456 | + if (self::is_new_install()) return; | |
| 457 | + if (!WP_Optimize()->get_options()->get_option('enable_browser_cache')) return; | |
| 458 | + | |
| 459 | + WP_Optimize()->get_browser_cache()->restore(); | |
| 151 | 460 | } |
| 152 | 461 | } |
| 153 | 462 | |
| 154 | 463 | endif; |