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

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