| 1 |
<?php |
| 2 |
/** |
| 3 |
* WP_Optimize_Updates class using for run updates in database from version to version. |
| 4 |
*/ |
| 5 |
|
| 6 |
if (!defined('ABSPATH')) die('Access denied.'); |
| 7 |
|
| 8 |
if (!class_exists('WP_Optimize_Updates')) : |
| 9 |
|
| 10 |
class WP_Optimize_Updates { |
| 11 |
|
| 12 |
/** |
| 13 |
* Format: key=<version>, value=array of method names to call |
| 14 |
* Example Usage: |
| 15 |
* private static $db_updates = array( |
| 16 |
* '1.0.1' => array( |
| 17 |
* 'update_101_add_new_column', |
| 18 |
* ), |
| 19 |
* ); |
| 20 |
* |
| 21 |
* @var Mixed |
| 22 |
*/ |
| 23 |
private static $updates = array( |
| 24 |
'3.0.12' => array('delete_old_locks'), |
| 25 |
'3.0.17' => array('disable_cache_directories_viewing'), |
| 26 |
'3.1.0' => array('reset_wpo_plugin_cron_tasks_schedule'), |
| 27 |
'3.1.4' => array('enable_minify_defer'), |
| 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_update_browser_cache_htaccess_config', |
| 43 |
'update_431_db_table_analysis_wipe_usage_data', |
| 44 |
), |
| 45 |
'4.4.0' => array( |
| 46 |
'update_440_change_plugin_json_permissions', |
| 47 |
), |
| 48 |
); |
| 49 |
|
| 50 |
/** |
| 51 |
* See if any database schema updates are needed, and perform them if so. |
| 52 |
* Example Usage: |
| 53 |
* private static function update_101_add_new_column() { |
| 54 |
* $wpdb = $GLOBALS['wpdb']; |
| 55 |
* $wpdb->query('ALTER TABLE tm_tasks ADD task_expiry varchar(300) AFTER id'); |
| 56 |
* } |
| 57 |
*/ |
| 58 |
public static function check_updates() { |
| 59 |
$our_version = WPO_VERSION; |
| 60 |
$db_version = get_option('wpo_update_version'); |
| 61 |
if (!$db_version || version_compare($our_version, $db_version, '>')) { |
| 62 |
foreach (self::$updates as $version => $updates) { |
| 63 |
if (version_compare($version, $db_version, '>')) { |
| 64 |
foreach ($updates as $update) { |
| 65 |
call_user_func(array(__CLASS__, $update)); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
update_option('wpo_update_version', WPO_VERSION); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Delete old semaphore locks from options database table. |
| 75 |
*/ |
| 76 |
private static function delete_old_locks() { |
| 77 |
global $wpdb; |
| 78 |
|
| 79 |
$query = "DELETE FROM `{$wpdb->options}`". |
| 80 |
" WHERE (option_name LIKE ('updraft_semaphore_%')". |
| 81 |
" OR option_name LIKE ('updraft_last_lock_time_%')". |
| 82 |
" OR option_name LIKE ('updraft_locked_%')". |
| 83 |
" OR option_name LIKE ('updraft_unlocked_%'))". |
| 84 |
" AND ". |
| 85 |
"(option_name LIKE ('%smush')". |
| 86 |
" OR option_name LIKE ('%load-url-task'));"; |
| 87 |
|
| 88 |
$wpdb->query($query); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Already prepared query, uses only hardcoded strings |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Disable cache directories viewing. |
| 94 |
*/ |
| 95 |
private static function disable_cache_directories_viewing() { |
| 96 |
if (!function_exists('wpo_disable_cache_directories_viewing')) { |
| 97 |
include_once WPO_PLUGIN_MAIN_PATH . 'cache/file-based-page-cache-functions.php'; |
| 98 |
} |
| 99 |
// Check if it's not a new installation |
| 100 |
if (self::is_new_install()) { |
| 101 |
return; |
| 102 |
} |
| 103 |
wpo_disable_cache_directories_viewing(); |
| 104 |
} |
| 105 |
|
| 106 |
private static function reset_wpo_plugin_cron_tasks_schedule() { |
| 107 |
wp_clear_scheduled_hook('wpo_plugin_cron_tasks'); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Update Minify Defer option (The option was hidden until now, but we're changing the setting) |
| 112 |
* |
| 113 |
* @return void |
| 114 |
*/ |
| 115 |
private static function enable_minify_defer() { |
| 116 |
if (!function_exists('wp_optimize_minify_config')) { |
| 117 |
include_once WPO_PLUGIN_MAIN_PATH . 'minify/class-wp-optimize-minify-config.php'; |
| 118 |
} |
| 119 |
$current_setting = wp_optimize_minify_config()->get('enable_defer_js'); |
| 120 |
if (true === $current_setting) { |
| 121 |
wp_optimize_minify_config()->update(array('enable_defer_js' => 'all')); |
| 122 |
} else { |
| 123 |
wp_optimize_minify_config()->update(array('enable_defer_js' => 'individual')); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Update Minify default exclude options |
| 129 |
* |
| 130 |
* @return void |
| 131 |
*/ |
| 132 |
private static function update_minify_excludes() { |
| 133 |
if (!function_exists('wp_optimize_minify_config')) { |
| 134 |
include_once WPO_PLUGIN_MAIN_PATH . 'minify/class-wp-optimize-minify-config.php'; |
| 135 |
} |
| 136 |
|
| 137 |
if (!class_exists('WP_Optimize_Minify_Functions')) { |
| 138 |
include_once WPO_PLUGIN_MAIN_PATH . 'minify/class-wp-optimize-minify-functions.php'; |
| 139 |
} |
| 140 |
|
| 141 |
$new_default_items = array( |
| 142 |
'elementor-admin-bar', |
| 143 |
'pdfjs-dist', |
| 144 |
'wordpress-popular-posts', |
| 145 |
); |
| 146 |
|
| 147 |
$user_excluded_blacklist_items = array(); |
| 148 |
$user_excluded_ignorelist_items = array(); |
| 149 |
|
| 150 |
// Get the lists as saved in the DB |
| 151 |
$current_blacklist = wp_optimize_minify_config()->get('blacklist'); |
| 152 |
$current_ignorelist = wp_optimize_minify_config()->get('ignore_list'); |
| 153 |
|
| 154 |
// Only proceed if the upgrade hasn't been done yet, i.e. the values aren't arrays |
| 155 |
if (is_array($current_blacklist) && is_array($current_ignorelist)) return; |
| 156 |
|
| 157 |
$current_blacklist = array_map('trim', explode("\n", $current_blacklist)); |
| 158 |
$current_ignorelist = array_map('trim', explode("\n", $current_ignorelist)); |
| 159 |
|
| 160 |
// Get the default lists |
| 161 |
$default_blacklist = WP_Optimize_Minify_Functions::get_default_ie_blacklist(); |
| 162 |
$default_ignorelist = WP_Optimize_Minify_Functions::get_default_ignore(); |
| 163 |
|
| 164 |
foreach ($default_blacklist as $bl_item) { |
| 165 |
// If a blacklist item isn't in the list, it was manually removed by the user, so we save that. |
| 166 |
if (!in_array($bl_item, $current_blacklist)) $user_excluded_blacklist_items[] = $bl_item; |
| 167 |
} |
| 168 |
|
| 169 |
foreach ($default_ignorelist as $il_item) { |
| 170 |
if (!in_array($il_item, $current_ignorelist) && !in_array($il_item, $new_default_items)) $user_excluded_ignorelist_items[] = $il_item; |
| 171 |
} |
| 172 |
|
| 173 |
// Update the options |
| 174 |
wp_optimize_minify_config()->update(array('blacklist' => $user_excluded_blacklist_items)); |
| 175 |
wp_optimize_minify_config()->update(array('ignore_list' => $user_excluded_ignorelist_items)); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Checks if it's a new installation of the plugin, as opposed to being updated. |
| 180 |
* |
| 181 |
* @return bool |
| 182 |
*/ |
| 183 |
private static function is_new_install() { |
| 184 |
$db_version = get_option('wpo_update_version'); |
| 185 |
return !$db_version; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Modifies the cache configuration for Windows OS by regenerating the 'uploads' path. |
| 190 |
*/ |
| 191 |
private static function update_3214_modify_cache_config_in_windows() { |
| 192 |
|
| 193 |
// Check if the OS is Windows and it's not a new installation. |
| 194 |
if ('WIN' !== strtoupper(substr(PHP_OS, 0, 3)) || self::is_new_install()) { |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
// Retrieve the current cache configuration |
| 199 |
$config = WPO_Cache_Config::instance()->get(); |
| 200 |
|
| 201 |
// Remove the 'uploads' path from the configuration, this will force the 'uploads' path to regenerate from the defaults |
| 202 |
if (isset($config['uploads'])) { |
| 203 |
unset($config['uploads']); |
| 204 |
} |
| 205 |
|
| 206 |
// Update the cache configuration |
| 207 |
WPO_Cache_Config::instance()->update($config); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Updates the cache configuration to incorporate the new 'use_webp_images' default setting in the cache config. |
| 212 |
*/ |
| 213 |
private static function update_3215_modify_cache_config_for_webp() { |
| 214 |
|
| 215 |
// Check if it's not a new installation |
| 216 |
if (self::is_new_install()) { |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
// Retrieve the current status of page caching and WebP conversion |
| 221 |
$cache_enabled = (bool) WPO_Cache_Config::instance()->get_option('enable_page_caching'); |
| 222 |
$webp_enabled = (bool) WP_Optimize()->get_options()->get_option('webp_conversion'); |
| 223 |
|
| 224 |
// If both page caching and WebP conversion are enabled, update the cache configuration |
| 225 |
if ($cache_enabled && $webp_enabled) { |
| 226 |
$config = WPO_Cache_Config::instance()->get(); |
| 227 |
$config['use_webp_images'] = true; |
| 228 |
WPO_Cache_Config::instance()->update($config); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Remove files in `uploads/wpo` folder created by htaccess capability tester library which is removed in 3.2.17 version |
| 234 |
*/ |
| 235 |
private static function update_3217_remove_htaccess_capability_tester_files() { |
| 236 |
if (self::is_new_install()) return; |
| 237 |
WPO_Uninstall::delete_wpo_folder(); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Resets WebP serving method |
| 242 |
* |
| 243 |
* We removed `uploads/wpo` folder in 3.2.17, When the redirection is possible |
| 244 |
* it doesn't cause issues. However, when using alter html method |
| 245 |
* WPO_WebP_Self_Test::is_webp_served requests a non-existing `uploads/wpo/images/wpo_logo_small.png` |
| 246 |
* which causes High CPU usage problem |
| 247 |
*/ |
| 248 |
private static function update_3218_reset_webp_serving_method() { |
| 249 |
if (self::is_new_install()) return; |
| 250 |
WP_Optimize()->get_webp_instance()->reset_webp_serving_method(); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Updates cache config file with date and time format |
| 255 |
*/ |
| 256 |
private static function update_3219_modify_cache_config_for_cache_time() { |
| 257 |
if (self::is_new_install()) return; |
| 258 |
$config = WPO_Cache_Config::instance()->get(); |
| 259 |
$cache_enabled = $config['enable_page_caching']; |
| 260 |
if ($cache_enabled) { |
| 261 |
WPO_Cache_Config::instance()->update($config); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Update async loading exclusion UA list |
| 267 |
*/ |
| 268 |
private static function update_330_ua_async_exclusion_list() { |
| 269 |
if (self::is_new_install()) return; |
| 270 |
|
| 271 |
$config = wp_optimize_minify_config(); |
| 272 |
$defaults = $config->get_defaults(); |
| 273 |
$new_list = $defaults['ualist']; |
| 274 |
|
| 275 |
$config->update(array( |
| 276 |
'ualist' => $new_list |
| 277 |
)); |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Move existing smush log content in old path (uploads/smush-*) to new path (uploads/wpo/logs/smush-*) |
| 282 |
*/ |
| 283 |
private static function update_350_move_content_of_existing_smush_logs_to_new_location() { |
| 284 |
if (self::is_new_install()) return; |
| 285 |
|
| 286 |
$upload_base = WP_Optimize_Utils::get_base_upload_dir(); |
| 287 |
|
| 288 |
$old_file = $upload_base . 'smush-' . substr(md5(wp_salt()), 0, 20) . '.log'; |
| 289 |
$new_file = WP_Optimize_Utils::get_log_file_path('smush'); |
| 290 |
|
| 291 |
// Check if only old file exists, if new file exists no need to overwrite its content |
| 292 |
if (is_file($old_file) && !is_file($new_file)) { |
| 293 |
$old_file_content = file_get_contents($old_file); |
| 294 |
file_put_contents($new_file, $old_file_content); |
| 295 |
} |
| 296 |
|
| 297 |
// Delete all smush log files in the old path (uploads/smush-*) |
| 298 |
if (!function_exists('glob')) return; |
| 299 |
$files = glob($upload_base . 'smush-*.log'); |
| 300 |
if (false === $files) return; |
| 301 |
foreach ($files as $file) { |
| 302 |
if (is_file($file)) { |
| 303 |
wp_delete_file($file); |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Delete wpo-plugins-tables-list.json file from old location and regenerate in new location |
| 310 |
*/ |
| 311 |
private static function update_350_delete_plugin_table_list_regenerate_in_new_location() { |
| 312 |
if (self::is_new_install()) return; |
| 313 |
|
| 314 |
// JSON file was moved to /wpo/ subfolder. Delete old files. |
| 315 |
$upload_base = WP_Optimize_Utils::get_base_upload_dir(); |
| 316 |
|
| 317 |
$old_file = $upload_base . 'wpo-plugins-tables-list.json'; |
| 318 |
if (is_file($old_file)) { |
| 319 |
wp_delete_file($old_file); |
| 320 |
} |
| 321 |
// JSON file not present in the new location, regenerate it |
| 322 |
$new_file = $upload_base . 'wpo/wpo-plugins-tables-list.json'; |
| 323 |
if (!is_file($new_file)) { |
| 324 |
WP_Optimize()->get_db_info()->update_plugin_json(); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Disable auto preloading after purge feature for existing users |
| 330 |
*/ |
| 331 |
private static function update_370_disable_auto_preload_after_purge_feature() { |
| 332 |
if (self::is_new_install()) return; |
| 333 |
$config = WPO_Cache_Config::instance()->get(); |
| 334 |
$cache_enabled = $config['enable_page_caching']; |
| 335 |
if ($cache_enabled) { |
| 336 |
$config['auto_preload_purged_contents'] = false; |
| 337 |
WPO_Cache_Config::instance()->update($config); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* Iterate over plugin utils tables creation, using WP_Optimize_Table_Management |
| 343 |
*/ |
| 344 |
private static function update_380_404_detector_table_create() { |
| 345 |
if (self::is_new_install()) return; |
| 346 |
WP_Optimize()->get_table_management()->create_plugin_tables(); |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Update the browser cache htaccess config file with max-age values |
| 351 |
*/ |
| 352 |
private static function update_431_update_browser_cache_htaccess_config() { |
| 353 |
if (self::is_new_install()) return; |
| 354 |
if (!WP_Optimize()->get_options()->get_option('enable_browser_cache')) return; |
| 355 |
|
| 356 |
WP_Optimize()->get_browser_cache()->restore(); |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Wipe WPO_DB_Table_Analysis usage data |
| 361 |
*/ |
| 362 |
private static function update_431_db_table_analysis_wipe_usage_data() { |
| 363 |
if (self::is_new_install()) return; |
| 364 |
|
| 365 |
if (class_exists('WPO_DB_Table_Analysis')) { |
| 366 |
WPO_DB_Table_Analysis::wipe_usage_data(); |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* Change permission for wpo-plugins-tables-list.json file |
| 372 |
*/ |
| 373 |
private static function update_440_change_plugin_json_permissions() { |
| 374 |
if (self::is_new_install()) return; |
| 375 |
|
| 376 |
WP_Optimize()->get_db_info()->change_plugin_json_permissions(); |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
endif; |
| 381 |
|