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
← All changes | cache/class-wp-optimize-cache-commands.php +108 -11 3.2.5 → 4.7.0 View file →
@@ -5,11 +5,17 @@
5 5 /**
6 6 * All cache commands that are intended to be available for calling from any sort of control interface (e.g. wp-admin, UpdraftCentral) go in here. All public methods should either return the data to be returned, or a WP_Error with associated error code, message and error data.
7 7 */
8 8 class WP_Optimize_Cache_Commands {
9 -
9 +
10 + /**
11 + * @var WP_Optimizer
12 + */
10 13 private $optimizer;
11 -
14 +
15 + /**
16 + * @var WP_Optimize_Options
17 + */
12 18 private $options;
13 19
14 20 /**
15 21 * WP_Optimize_Cache_Commands constructor.
@@ -41,13 +47,14 @@
41 47 }
42 48
43 49 $enabled = false;
44 50 $disabled = false;
45 - $return = !empty($validation) ? $validation : array();
51 + $return = empty($validation) ? array() : $validation;
46 52 $previous_settings = WPO_Cache_Config::instance()->get();
53 + $settings_changed = false;
47 54
48 55 // Attempt to change current status if required
49 - if (isset($previous_settings['enable_page_caching']) && $previous_settings['enable_page_caching'] != $data['cache-settings']['enable_page_caching']) {
56 + if (isset($previous_settings['enable_page_caching']) && (bool) $previous_settings['enable_page_caching'] !== (bool) $data['cache-settings']['enable_page_caching']) {
50 57 // Disable cache.
51 58 if (empty($data['cache-settings']['enable_page_caching'])) {
52 59 $disabled = WPO_Page_Cache::instance()->disable();
53 60 // Disabling failed
@@ -52,12 +59,23 @@
52 59 $disabled = WPO_Page_Cache::instance()->disable();
53 60 // Disabling failed
54 61 if ($disabled && is_wp_error($disabled)) {
55 62 // If disabling failed, we re-enable whatever was disabled, to make sure nothing breaks.
56 - if ($previous_settings['enable_page_caching']) WPO_Page_Cache::instance()->enable(true);
63 + $cache_enable_result = null;
64 + if ($previous_settings['enable_page_caching']) {
65 + $cache_enable_result = WPO_Page_Cache::instance()->enable(true);
66 + }
67 +
68 + $error_message = $disabled->get_error_message();
69 +
70 + if (is_wp_error($cache_enable_result)) {
71 + // translators: %s: The error message.
72 + $error_message .= ' '. sprintf(__('Additionally, WP-Optimize failed to re-enable page caching after the failed disable attempt: %s', 'wp-optimize'), $cache_enable_result->get_error_message());
73 + }
74 +
57 75 $return['error'] = array(
58 76 'code' => $disabled->get_error_code(),
59 - 'message' => $disabled->get_error_message()
77 + 'message' => $error_message,
60 78 );
61 79 } elseif (WPO_Page_Cache::instance()->has_warnings()) {
62 80 $return['warnings_label'] = __('Page caching was disabled, but with some warnings:', 'wp-optimize');
63 81 $return['warnings'] = WPO_Page_Cache::instance()->get_errors('warning');
@@ -85,17 +103,23 @@
85 103 }
86 104 // Override enabled setting value
87 105 $data['cache-settings']['enable_page_caching'] = ($enabled && !is_wp_error($enabled)) || ($previous_settings['enable_page_caching'] && is_wp_error($disabled));
88 106 } else {
89 - $data['cache-settings']['enable_page_caching'] = $previous_settings['enable_page_caching'];
90 - $enabled = $previous_settings['enable_page_caching'];
107 + $enabled = isset($data['cache-settings']['enable_page_caching']) ? (bool) $data['cache-settings']['enable_page_caching'] : false;
91 108 }
92 109
110 + $data['cache-settings']['use_webp_images'] = (bool) WP_Optimize()->get_options()->get_option('webp_conversion');
111 +
93 112 $skip_if_no_file_yet = !$enabled || is_wp_error($enabled);
94 113 $save_settings_result = WPO_Cache_Config::instance()->update($data['cache-settings'], $skip_if_no_file_yet);
95 114
96 115 if ($save_settings_result && !is_wp_error($save_settings_result)) {
97 - WP_Optimize_Page_Cache_Preloader::instance()->cache_settings_updated($data['cache-settings'], $previous_settings);
116 + $settings_changed = !WP_Optimize_Utils::array_contains($data['cache-settings'], $previous_settings);
117 +
118 + if ($settings_changed) {
119 + WP_Optimize_Page_Cache_Preloader::instance()->cache_settings_updated($data['cache-settings'], $previous_settings);
120 + WPO_Page_Cache::instance()->cache_settings_updated($data['cache-settings']);
121 + }
98 122 $return['result'] = $save_settings_result;
99 123 } else {
100 124 // Saving the settings returned an error
101 125 if (is_wp_error($save_settings_result)) {
@@ -112,8 +136,39 @@
112 136 }
113 137
114 138 $return['enabled'] = ($enabled && !is_wp_error($enabled)) || ($previous_settings['enable_page_caching'] && is_wp_error($disabled));
115 139
140 + if ($enabled && !is_wp_error($enabled)) {
141 + $options_needs_purge = array_flip(array(
142 + 'enable_mobile_caching',
143 + 'enable_user_caching',
144 + 'enable_per_role_cache',
145 + 'enable_user_specific_cache',
146 + 'enable_rest_caching',
147 + 'host_gravatars_locally',
148 + 'cache_exception_urls',
149 + 'cache_ignore_query_variables',
150 + 'cache_exception_cookies',
151 + 'cache_exception_conditional_tags',
152 + 'cache_exception_browser_agents',
153 + ));
154 + $previous_settings = array_intersect_key($previous_settings, $options_needs_purge);
155 + $current_settings = wp_parse_args($data['cache-settings'], WPO_Cache_Config::instance()->defaults);
156 + $current_settings = array_intersect_key($current_settings, $options_needs_purge);
157 + ksort($previous_settings);
158 + ksort($current_settings);
159 + if ($current_settings !== $previous_settings) {
160 + $cache = WPO_Page_Cache::instance();
161 + $cache->file_log('Setting changed, purging cache');
162 + $cache->purge();
163 + }
164 + }
165 + // $disable can either be boolean or WP_Error
166 + if (!is_wp_error($disabled) && $disabled && $settings_changed) {
167 + WPO_Page_Cache::instance()->prune_cache_logs();
168 + wp_clear_scheduled_hook('wpo_prune_cache_logs');
169 + }
170 +
116 171 return $return;
117 172 }
118 173
119 174 /**
@@ -126,9 +181,11 @@
126 181
127 182 $status[] = WPO_Page_Cache::instance()->is_enabled() ? __('Caching is enabled', 'wp-optimize') : __('Caching is disabled', 'wp-optimize');
128 183
129 184 $preloader_status = WP_Optimize_Page_Cache_Preloader::instance()->get_status_info();
185 + // translators: %s: The current cache size
130 186 $status[] = sprintf(__('Current cache size: %s', 'wp-optimize'), $preloader_status['size']);
187 + // translators: %s: The number of files in the cache
131 188 $status[] = sprintf(__('Number of files: %s', 'wp-optimize'), $preloader_status['file_count']);
132 189
133 190 if (array_key_exists('message', $preloader_status)) $status[] = $preloader_status['message'];
134 191
@@ -161,16 +218,26 @@
161 218 * @return array
162 219 */
163 220 public function purge_page_cache() {
164 221
165 - if (!WP_Optimize()->can_purge_the_cache()) {
222 + $wpo_page_cache = WP_Optimize()->get_page_cache();
223 +
224 + if (!$wpo_page_cache->is_enabled()) {
166 225 return array(
167 226 'success' => false,
227 + 'error' => __('Cache is not enabled.', 'wp-optimize'),
228 + );
229 + }
230 +
231 + if (!$wpo_page_cache->can_purge_cache() && !(defined('WP_CLI') && WP_CLI)) {
232 + return array(
233 + 'success' => false,
168 234 'message' => __('You do not have permission to purge the cache', 'wp-optimize'),
169 235 );
170 236 }
171 237
172 238 $purged = WP_Optimize()->get_page_cache()->purge();
239 + if ($purged) WP_Optimize()->get_page_cache()->file_log("Full Cache Purge triggered by: " . __METHOD__);
173 240 $cache_size = WP_Optimize()->get_page_cache()->get_cache_size();
174 241 $wpo_page_cache_preloader = WP_Optimize_Page_Cache_Preloader::instance();
175 242
176 243 $response = array(
@@ -186,8 +253,11 @@
186 253 $wpo_page_cache_preloader->reschedule_preload();
187 254
188 255 // run preloader.
189 256 $wpo_page_cache_preloader->run('scheduled', $response);
257 + } elseif ($wpo_page_cache->should_auto_preload_purged_contents()) {
258 + // When auto preload is enabled but scheduled preload is disabled
259 + $wpo_page_cache_preloader->run('manual', $response);
190 260 }
191 261
192 262 if ($response['success']) {
193 263 $response['message'] = __('Page cache purged successfully', 'wp-optimize');
@@ -231,9 +301,9 @@
231 301
232 302 /**
233 303 * Run cache preload action.
234 304 *
235 - * @return void|array - Doesn't return anything if run() is successfull (Run() prints a JSON object and closed browser connection) or an array if failed.
305 + * @return void|array - Doesn't return anything if run() is successful (Run() prints a JSON object and closed browser connection) or an array if failed.
236 306 */
237 307 public function run_cache_preload() {
238 308 return WP_Optimize_Page_Cache_Preloader::instance()->run('manual');
239 309 }
@@ -284,8 +354,35 @@
284 354 }
285 355
286 356 if ($result['success']) {
287 357 $result['message'] = __('Page cache settings updated successfully.', 'wp-optimize');
358 + }
359 +
360 + return $result;
361 + }
362 +
363 + /**
364 + * Enable or disable auto preloading after cache purge
365 + *
366 + * @param array $params Option value sent via AJAX
367 + * @return array
368 + */
369 + public function save_cache_auto_preload_option($params) {
370 + $wpo_cache = WP_Optimize()->get_page_cache();
371 + $wpo_cache_options = $wpo_cache->config->get();
372 + $wpo_cache_options['auto_preload_purged_contents'] = 'true' === $params['enabled'] ? 1 : 0;
373 + $update_status = $wpo_cache->config->update($wpo_cache_options);
374 +
375 + if (is_wp_error($update_status)) {
376 + $result = array(
377 + 'success' => false,
378 + 'message' => $update_status->get_error_message(),
379 + );
380 + } else {
381 + $result = array(
382 + 'success' => true,
383 + 'message' => __('Auto preload settings have been updated successfully', 'wp-optimize'),
384 + );
288 385 }
289 386
290 387 return $result;
291 388 }