PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.6.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.6.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.6.0, at includes/class-wp-optimize-updates.php

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