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

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