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

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