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 +67 -11 3.7.1 → 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,10 +103,9 @@
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
93 110 $data['cache-settings']['use_webp_images'] = (bool) WP_Optimize()->get_options()->get_option('webp_conversion');
94 111
@@ -95,9 +112,14 @@
95 112 $skip_if_no_file_yet = !$enabled || is_wp_error($enabled);
96 113 $save_settings_result = WPO_Cache_Config::instance()->update($data['cache-settings'], $skip_if_no_file_yet);
97 114
98 115 if ($save_settings_result && !is_wp_error($save_settings_result)) {
99 - 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 + }
100 122 $return['result'] = $save_settings_result;
101 123 } else {
102 124 // Saving the settings returned an error
103 125 if (is_wp_error($save_settings_result)) {
@@ -114,8 +136,39 @@
114 136 }
115 137
116 138 $return['enabled'] = ($enabled && !is_wp_error($enabled)) || ($previous_settings['enable_page_caching'] && is_wp_error($disabled));
117 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 +
118 171 return $return;
119 172 }
120 173
121 174 /**
@@ -128,9 +181,11 @@
128 181
129 182 $status[] = WPO_Page_Cache::instance()->is_enabled() ? __('Caching is enabled', 'wp-optimize') : __('Caching is disabled', 'wp-optimize');
130 183
131 184 $preloader_status = WP_Optimize_Page_Cache_Preloader::instance()->get_status_info();
185 + // translators: %s: The current cache size
132 186 $status[] = sprintf(__('Current cache size: %s', 'wp-optimize'), $preloader_status['size']);
187 + // translators: %s: The number of files in the cache
133 188 $status[] = sprintf(__('Number of files: %s', 'wp-optimize'), $preloader_status['file_count']);
134 189
135 190 if (array_key_exists('message', $preloader_status)) $status[] = $preloader_status['message'];
136 191
@@ -179,10 +234,11 @@
179 234 'message' => __('You do not have permission to purge the cache', 'wp-optimize'),
180 235 );
181 236 }
182 237
183 - $purged = $wpo_page_cache->purge();
184 - $cache_size = $wpo_page_cache->get_cache_size();
238 + $purged = WP_Optimize()->get_page_cache()->purge();
239 + if ($purged) WP_Optimize()->get_page_cache()->file_log("Full Cache Purge triggered by: " . __METHOD__);
240 + $cache_size = WP_Optimize()->get_page_cache()->get_cache_size();
185 241 $wpo_page_cache_preloader = WP_Optimize_Page_Cache_Preloader::instance();
186 242
187 243 $response = array(
188 244 'success' => $purged,