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

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