PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / includes / class-wp-optimize-updates.php

class-wp-optimize-updates.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.7.0, at includes/class-wp-optimize-updates.php

464 lines 14.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_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 ),
53 );
54
55 /**
56 * See if any database schema updates are needed, and perform them if so.
57 * Example Usage:
58 * private static function update_101_add_new_column() {
59 * $wpdb = $GLOBALS['wpdb'];
60 * $wpdb->query('ALTER TABLE tm_tasks ADD task_expiry varchar(300) AFTER id');
61 * }
62 */
63 public static function check_updates() {
64 $our_version = WPO_VERSION;
65 $db_version = get_option('wpo_update_version');
66 if (!$db_version || version_compare($our_version, $db_version, '>')) {
67 foreach (self::$updates as $version => $updates) {
68 if (version_compare($version, $db_version, '>')) {
69 foreach ($updates as $update) {
70 call_user_func(array(__CLASS__, $update));
71 }
72 }
73 }
74 update_option('wpo_update_version', WPO_VERSION);
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 }
90 }
91
92 /**
93 * Delete old semaphore locks from options database table.
94 */
95 private static function delete_old_locks() {
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'));";
106
107 $wpdb->query($query); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Already prepared query, uses only hardcoded strings
108
109 }
110
111 /**
112 * Disable cache directories viewing.
113 */
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 }
122 wpo_disable_cache_directories_viewing();
123 }
124
125 private static function reset_wpo_plugin_cron_tasks_schedule() {
126 wp_clear_scheduled_hook('wpo_plugin_cron_tasks');
127 }
128
129 /**
130 * Update Minify Defer option (The option was hidden until now, but we're changing the setting)
131 *
132 * @return void
133 */
134 private static function enable_minify_defer() {
135 if (!function_exists('wp_optimize_minify_config')) {
136 include_once WPO_PLUGIN_MAIN_PATH . 'minify/class-wp-optimize-minify-config.php';
137 }
138 $current_setting = wp_optimize_minify_config()->get('enable_defer_js');
139 if (true === $current_setting) {
140 wp_optimize_minify_config()->update(array('enable_defer_js' => 'all'));
141 } else {
142 wp_optimize_minify_config()->update(array('enable_defer_js' => 'individual'));
143 }
144 }
145
146 /**
147 * Update Minify default exclude options
148 *
149 * @return void
150 */
151 private static function update_minify_excludes() {
152 if (!function_exists('wp_optimize_minify_config')) {
153 include_once WPO_PLUGIN_MAIN_PATH . 'minify/class-wp-optimize-minify-config.php';
154 }
155
156 if (!class_exists('WP_Optimize_Minify_Functions')) {
157 include_once WPO_PLUGIN_MAIN_PATH . 'minify/class-wp-optimize-minify-functions.php';
158 }
159
160 $new_default_items = array(
161 'elementor-admin-bar',
162 'pdfjs-dist',
163 'wordpress-popular-posts',
164 );
165
166 $user_excluded_blacklist_items = array();
167 $user_excluded_ignorelist_items = array();
168
169 // Get the lists as saved in the DB
170 $current_blacklist = wp_optimize_minify_config()->get('blacklist');
171 $current_ignorelist = wp_optimize_minify_config()->get('ignore_list');
172
173 // Only proceed if the upgrade hasn't been done yet, i.e. the values aren't arrays
174 if (is_array($current_blacklist) && is_array($current_ignorelist)) return;
175
176 $current_blacklist = array_map('trim', explode("\n", $current_blacklist));
177 $current_ignorelist = array_map('trim', explode("\n", $current_ignorelist));
178
179 // Get the default lists
180 $default_blacklist = WP_Optimize_Minify_Functions::get_default_ie_blacklist();
181 $default_ignorelist = WP_Optimize_Minify_Functions::get_default_ignore();
182
183 foreach ($default_blacklist as $bl_item) {
184 // If a blacklist item isn't in the list, it was manually removed by the user, so we save that.
185 if (!in_array($bl_item, $current_blacklist)) $user_excluded_blacklist_items[] = $bl_item;
186 }
187
188 foreach ($default_ignorelist as $il_item) {
189 if (!in_array($il_item, $current_ignorelist) && !in_array($il_item, $new_default_items)) $user_excluded_ignorelist_items[] = $il_item;
190 }
191
192 // Update the options
193 wp_optimize_minify_config()->update(array('blacklist' => $user_excluded_blacklist_items));
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();
460 }
461 }
462
463 endif;
464