PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.5.4
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.5.4
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 / cache / class-wp-optimize-cache-commands.php

class-wp-optimize-cache-commands.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.5.4, at cache/class-wp-optimize-cache-commands.php

392 lines 12.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) die('No direct access allowed');
4
5 /**
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 */
8 class WP_Optimize_Cache_Commands {
9
10 /**
11 * @var WP_Optimizer
12 */
13 private $optimizer;
14
15 /**
16 * @var WP_Optimize_Options
17 */
18 private $options;
19
20 /**
21 * WP_Optimize_Cache_Commands constructor.
22 */
23 public function __construct() {
24 $this->optimizer = WP_Optimize()->get_optimizer();
25 $this->options = WP_Optimize()->get_options();
26 }
27
28 /**
29 * Save cache settings
30 *
31 * @param array $data
32 *
33 * @return array
34 */
35 public function save_cache_settings($data) {
36
37 if (!class_exists('WPO_Cache_Config')) return array(
38 'result' => false,
39 'message' => "WPO_Cache_Config class doesn't exist",
40 );
41
42 // filter for validate cache settings before save it.
43 $validation = apply_filters('wpo_save_cache_settings_validation', $data['cache-settings']);
44
45 if (!empty($validation) && isset($validation['result']) && false === $validation['result']) {
46 return $validation;
47 }
48
49 $enabled = false;
50 $disabled = false;
51 $return = empty($validation) ? array() : $validation;
52 $previous_settings = WPO_Cache_Config::instance()->get();
53 $settings_changed = false;
54
55 // Attempt to change current status if required
56 if (isset($previous_settings['enable_page_caching']) && (bool) $previous_settings['enable_page_caching'] !== (bool) $data['cache-settings']['enable_page_caching']) {
57 // Disable cache.
58 if (empty($data['cache-settings']['enable_page_caching'])) {
59 $disabled = WPO_Page_Cache::instance()->disable();
60 // Disabling failed
61 if ($disabled && is_wp_error($disabled)) {
62 // If disabling failed, we re-enable whatever was disabled, to make sure nothing breaks.
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
75 $return['error'] = array(
76 'code' => $disabled->get_error_code(),
77 'message' => $error_message,
78 );
79 } elseif (WPO_Page_Cache::instance()->has_warnings()) {
80 $return['warnings_label'] = __('Page caching was disabled, but with some warnings:', 'wp-optimize');
81 $return['warnings'] = WPO_Page_Cache::instance()->get_errors('warning');
82 }
83 } else {
84 // we need to rebuild advanced-cache.php and add WP_CACHE to wp-config.
85 $enabled = WPO_Page_Cache::instance()->enable(true);
86 // Enabling failed
87 if (is_wp_error($enabled)) {
88 // disable everything, to avoid half enabled things
89 WPO_Page_Cache::instance()->disable();
90 $return['error'] = array(
91 'code' => $enabled->get_error_code(),
92 'message' => $enabled->get_error_message()
93 );
94
95 if (WPO_Page_Cache::instance()->advanced_cache_file_writing_error) {
96 $return['advanced_cache_file_writing_error'] = true;
97 $return['advanced_cache_file_content'] = WPO_Page_Cache::instance()->advanced_cache_file_content;
98 }
99 } elseif (WPO_Page_Cache::instance()->has_warnings()) {
100 $return['warnings_label'] = __('Page caching was enabled, but with some warnings:', 'wp-optimize');
101 $return['warnings'] = WPO_Page_Cache::instance()->get_errors('warning');
102 }
103 }
104 // Override enabled setting value
105 $data['cache-settings']['enable_page_caching'] = ($enabled && !is_wp_error($enabled)) || ($previous_settings['enable_page_caching'] && is_wp_error($disabled));
106 } else {
107 $data['cache-settings']['enable_page_caching'] = $previous_settings['enable_page_caching'];
108 $enabled = $previous_settings['enable_page_caching'];
109 }
110
111 $data['cache-settings']['use_webp_images'] = (bool) WP_Optimize()->get_options()->get_option('webp_conversion');
112
113 $skip_if_no_file_yet = !$enabled || is_wp_error($enabled);
114 $save_settings_result = WPO_Cache_Config::instance()->update($data['cache-settings'], $skip_if_no_file_yet);
115
116 if ($save_settings_result && !is_wp_error($save_settings_result)) {
117 $settings_changed = !WP_Optimize_Utils::array_contains($data['cache-settings'], $previous_settings);
118
119 if ($settings_changed) {
120 WP_Optimize_Page_Cache_Preloader::instance()->cache_settings_updated($data['cache-settings'], $previous_settings);
121 WPO_Page_Cache::instance()->cache_settings_updated($data['cache-settings']);
122 }
123 $return['result'] = $save_settings_result;
124 } else {
125 // Saving the settings returned an error
126 if (is_wp_error($save_settings_result)) {
127 if (isset($return['error'])) {
128 $return['error']['message'] .= "\n\n".$save_settings_result->get_error_message();
129 } else {
130 $return['error'] = array(
131 'code' => $save_settings_result->get_error_code(),
132 'message' => $save_settings_result->get_error_message()
133 );
134 }
135 }
136 $return['result'] = false;
137 }
138
139 $return['enabled'] = ($enabled && !is_wp_error($enabled)) || ($previous_settings['enable_page_caching'] && is_wp_error($disabled));
140
141 if ($enabled && !is_wp_error($enabled)) {
142 $options_needs_purge = array_flip(array(
143 'enable_mobile_caching',
144 'enable_user_caching',
145 'enable_per_role_cache',
146 'enable_user_specific_cache',
147 'enable_rest_caching',
148 'show_avatars',
149 'host_gravatars_locally',
150 'cache_exception_urls',
151 'cache_ignore_query_variables',
152 'cache_exception_cookies',
153 'cache_exception_conditional_tags',
154 'cache_exception_browser_agents',
155 ));
156 $previous_settings = array_intersect_key($previous_settings, $options_needs_purge);
157 $current_settings = wp_parse_args($data['cache-settings'], WPO_Cache_Config::instance()->defaults);
158 $current_settings = array_intersect_key($current_settings, $options_needs_purge);
159 ksort($previous_settings);
160 ksort($current_settings);
161 if ($current_settings !== $previous_settings) {
162 $cache = WPO_Page_Cache::instance();
163 $cache->file_log('Setting changed, purging cache');
164 $cache->purge();
165 }
166 }
167 // $disable can either be boolean or WP_Error
168 if (!is_wp_error($disabled) && $disabled && $settings_changed) {
169 WPO_Page_Cache::instance()->prune_cache_logs();
170 wp_clear_scheduled_hook('wpo_prune_cache_logs');
171 }
172
173 return $return;
174 }
175
176 /**
177 * Get information about current cache status. Used in cli commands.
178 *
179 * @return array
180 */
181 public function get_status_info() {
182 $status = array();
183
184 $status[] = WPO_Page_Cache::instance()->is_enabled() ? __('Caching is enabled', 'wp-optimize') : __('Caching is disabled', 'wp-optimize');
185
186 $preloader_status = WP_Optimize_Page_Cache_Preloader::instance()->get_status_info();
187 // translators: %s: The current cache size
188 $status[] = sprintf(__('Current cache size: %s', 'wp-optimize'), $preloader_status['size']);
189 // translators: %s: The number of files in the cache
190 $status[] = sprintf(__('Number of files: %s', 'wp-optimize'), $preloader_status['file_count']);
191
192 if (array_key_exists('message', $preloader_status)) $status[] = $preloader_status['message'];
193
194 $status['message'] = join(PHP_EOL, $status);
195
196 return $status;
197 }
198
199 /**
200 * Enable cache.
201 */
202 public function enable() {
203 $settings = WPO_Cache_Config::instance()->get();
204 $settings['enable_page_caching'] = true;
205 return $this->format_save_cache_settings_response($this->save_cache_settings(array('cache-settings' => $settings)));
206 }
207
208 /**
209 * Disable cache.
210 */
211 public function disable() {
212 $settings = WPO_Cache_Config::instance()->get();
213 $settings['enable_page_caching'] = false;
214 return $this->format_save_cache_settings_response($this->save_cache_settings(array('cache-settings' => $settings)));
215 }
216
217 /**
218 * Purge WP-Optimize page cache.
219 *
220 * @return array
221 */
222 public function purge_page_cache() {
223
224 $wpo_page_cache = WP_Optimize()->get_page_cache();
225
226 if (!$wpo_page_cache->is_enabled()) {
227 return array(
228 'success' => false,
229 'error' => __('Cache is not enabled.', 'wp-optimize'),
230 );
231 }
232
233 if (!$wpo_page_cache->can_purge_cache() && !(defined('WP_CLI') && WP_CLI)) {
234 return array(
235 'success' => false,
236 'message' => __('You do not have permission to purge the cache', 'wp-optimize'),
237 );
238 }
239
240 $purged = WP_Optimize()->get_page_cache()->purge();
241 if ($purged) WP_Optimize()->get_page_cache()->file_log("Full Cache Purge triggered by: " . __METHOD__);
242 $cache_size = WP_Optimize()->get_page_cache()->get_cache_size();
243 $wpo_page_cache_preloader = WP_Optimize_Page_Cache_Preloader::instance();
244
245 $response = array(
246 'success' => $purged,
247 'size' => WP_Optimize()->format_size($cache_size['size']),
248 'file_count' => $cache_size['file_count'],
249 );
250
251 // if scheduled preload enabled then reschedule and run preloader.
252 if ($wpo_page_cache_preloader->is_scheduled_preload_enabled()) {
253 // cancel preload and reschedule preload action.
254 $wpo_page_cache_preloader->cancel_preload();
255 $wpo_page_cache_preloader->reschedule_preload();
256
257 // run preloader.
258 $wpo_page_cache_preloader->run('scheduled', $response);
259 } elseif ($wpo_page_cache->should_auto_preload_purged_contents()) {
260 // When auto preload is enabled but scheduled preload is disabled
261 $wpo_page_cache_preloader->run('manual', $response);
262 }
263
264 if ($response['success']) {
265 $response['message'] = __('Page cache purged successfully', 'wp-optimize');
266 }
267
268 return $response;
269 }
270
271 /**
272 * Run cache preload (for wp-cli).
273 *
274 * @return array|bool
275 */
276 public function run_cache_preload_cli() {
277 if (!(defined('WP_CLI') && WP_CLI)) return false;
278
279 // define WPO_ADVANCED_CACHE constant as WP-CLI doesn't load advanced-cache.php file
280 // but we check this constant value wen detecting status of cache
281 if (!defined('WPO_ADVANCED_CACHE')) define('WPO_ADVANCED_CACHE', true);
282 // don't interrupt queue processing
283 add_filter('updraft_interrupt_tasks_queue_load-url-task', '__return_false', 99);
284
285 // if preloading is running then exit.
286 if (WP_Optimize_Page_Cache_Preloader::instance()->is_busy()) {
287 return array(
288 'success' => false,
289 'error' => __('Preloading is currently running in another process.', 'wp-optimize'),
290 );
291 }
292
293 // set default response.
294 $response = array(
295 'success' => true,
296 'message' => __('All URLs were preloaded into cache successfully', 'wp-optimize'),
297 );
298
299 WP_CLI::log(__('Preloading URLs into cache...', 'wp-optimize'));
300
301 return WP_Optimize_Page_Cache_Preloader::instance()->run('manual', $response);
302 }
303
304 /**
305 * Run cache preload action.
306 *
307 * @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.
308 */
309 public function run_cache_preload() {
310 return WP_Optimize_Page_Cache_Preloader::instance()->run('manual');
311 }
312
313 /**
314 * Cancel cache preload action.
315 *
316 * @return array
317 */
318 public function cancel_cache_preload() {
319 WP_Optimize_Page_Cache_Preloader::instance()->cancel_preload();
320 return WP_Optimize_Page_Cache_Preloader::instance()->get_status_info();
321 }
322
323 /**
324 * Get status of cache preload.
325 *
326 * @return array
327 */
328 public function get_cache_preload_status() {
329 return WP_Optimize_Page_Cache_Preloader::instance()->get_status_info();
330 }
331
332 /**
333 * Enable or disable browser cache.
334 *
335 * @param array $params - ['browser_cache_expire' => '1 month 15 days 2 hours' || '' - for disable cache]
336 * @return array
337 */
338 public function enable_browser_cache($params) {
339 return WP_Optimize()->get_browser_cache()->enable_browser_cache_command_handler($params);
340 }
341
342 /**
343 * Format save_cache_settings() result for displaying in WP-CLI console
344 *
345 * @param array $response
346 * @return array
347 */
348 private function format_save_cache_settings_response($response) {
349 $result = array(
350 'success' => $response['result'],
351 );
352
353 if (isset($response['error'])) {
354 $result['success'] = false;
355 $result['error'] = $response['error']['message'];
356 }
357
358 if ($result['success']) {
359 $result['message'] = __('Page cache settings updated successfully.', 'wp-optimize');
360 }
361
362 return $result;
363 }
364
365 /**
366 * Enable or disable auto preloading after cache purge
367 *
368 * @param array $params Option value sent via AJAX
369 * @return array
370 */
371 public function save_cache_auto_preload_option($params) {
372 $wpo_cache = WP_Optimize()->get_page_cache();
373 $wpo_cache_options = $wpo_cache->config->get();
374 $wpo_cache_options['auto_preload_purged_contents'] = 'true' === $params['enabled'] ? 1 : 0;
375 $update_status = $wpo_cache->config->update($wpo_cache_options);
376
377 if (is_wp_error($update_status)) {
378 $result = array(
379 'success' => false,
380 'message' => $update_status->get_error_message(),
381 );
382 } else {
383 $result = array(
384 'success' => true,
385 'message' => __('Auto preload settings have been updated successfully', 'wp-optimize'),
386 );
387 }
388
389 return $result;
390 }
391 }
392