PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.3
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.3
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-cache-commands.php

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

293 lines 9.5 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 private $optimizer;
11
12 private $options;
13
14 /**
15 * WP_Optimize_Cache_Commands constructor.
16 */
17 public function __construct() {
18 $this->optimizer = WP_Optimize()->get_optimizer();
19 $this->options = WP_Optimize()->get_options();
20 }
21
22 /**
23 * Save cache settings
24 *
25 * @param array $data
26 *
27 * @return array
28 */
29 public function save_cache_settings($data) {
30
31 if (!class_exists('WPO_Cache_Config')) return array(
32 'result' => false,
33 'message' => "WPO_Cache_Config class doesn't exist",
34 );
35
36 // filter for validate cache settings before save it.
37 $validation = apply_filters('wpo_save_cache_settings_validation', $data['cache-settings']);
38
39 if (!empty($validation) && isset($validation['result']) && false === $validation['result']) {
40 return $validation;
41 }
42
43 $enabled = false;
44 $disabled = false;
45 $return = !empty($validation) ? $validation : array();
46 $previous_settings = WPO_Cache_Config::instance()->get();
47
48 // 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']) {
50 // Disable cache.
51 if (empty($data['cache-settings']['enable_page_caching'])) {
52 $disabled = WPO_Page_Cache::instance()->disable();
53 // Disabling failed
54 if ($disabled && is_wp_error($disabled)) {
55 // 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);
57 $return['error'] = array(
58 'code' => $disabled->get_error_code(),
59 'message' => $disabled->get_error_message()
60 );
61 } elseif (WPO_Page_Cache::instance()->has_warnings()) {
62 $return['warnings_label'] = __('Page caching was disabled, but with some warnings:', 'wp-optimize');
63 $return['warnings'] = WPO_Page_Cache::instance()->get_errors('warning');
64 }
65 } else {
66 // we need to rebuild advanced-cache.php and add WP_CACHE to wp-config.
67 $enabled = WPO_Page_Cache::instance()->enable(true);
68 // Enabling failed
69 if (is_wp_error($enabled)) {
70 // disable everything, to avoid half enabled things
71 WPO_Page_Cache::instance()->disable();
72 $return['error'] = array(
73 'code' => $enabled->get_error_code(),
74 'message' => $enabled->get_error_message()
75 );
76
77 if (WPO_Page_Cache::instance()->advanced_cache_file_writing_error) {
78 $return['advanced_cache_file_writing_error'] = true;
79 $return['advanced_cache_file_content'] = WPO_Page_Cache::instance()->advanced_cache_file_content;
80 }
81 } elseif (WPO_Page_Cache::instance()->has_warnings()) {
82 $return['warnings_label'] = __('Page caching was enabled, but with some warnings:', 'wp-optimize');
83 $return['warnings'] = WPO_Page_Cache::instance()->get_errors('warning');
84 }
85 }
86 // Override enabled setting value
87 $data['cache-settings']['enable_page_caching'] = ($enabled && !is_wp_error($enabled)) || ($previous_settings['enable_page_caching'] && is_wp_error($disabled));
88 } else {
89 $data['cache-settings']['enable_page_caching'] = $previous_settings['enable_page_caching'];
90 $enabled = $previous_settings['enable_page_caching'];
91 }
92
93 $skip_if_no_file_yet = !$enabled || is_wp_error($enabled);
94 $save_settings_result = WPO_Cache_Config::instance()->update($data['cache-settings'], $skip_if_no_file_yet);
95
96 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);
98 $return['result'] = $save_settings_result;
99 } else {
100 // Saving the settings returned an error
101 if (is_wp_error($save_settings_result)) {
102 if (isset($return['error'])) {
103 $return['error']['message'] .= "\n\n".$save_settings_result->get_error_message();
104 } else {
105 $return['error'] = array(
106 'code' => $save_settings_result->get_error_code(),
107 'message' => $save_settings_result->get_error_message()
108 );
109 }
110 }
111 $return['result'] = false;
112 }
113
114 $return['enabled'] = ($enabled && !is_wp_error($enabled)) || ($previous_settings['enable_page_caching'] && is_wp_error($disabled));
115
116 return $return;
117 }
118
119 /**
120 * Get information about current cache status. Used in cli commands.
121 *
122 * @return array
123 */
124 public function get_status_info() {
125 $status = array();
126
127 $status[] = WPO_Page_Cache::instance()->is_enabled() ? __('Caching is enabled', 'wp-optimize') : __('Caching is disabled', 'wp-optimize');
128
129 $preloader_status = WP_Optimize_Page_Cache_Preloader::instance()->get_status_info();
130 $status[] = sprintf(__('Current cache size: %s', 'wp-optimize'), $preloader_status['size']);
131 $status[] = sprintf(__('Number of files: %s', 'wp-optimize'), $preloader_status['file_count']);
132
133 if (array_key_exists('message', $preloader_status)) $status[] = $preloader_status['message'];
134
135 $status['message'] = join(PHP_EOL, $status);
136
137 return $status;
138 }
139
140 /**
141 * Enable cache.
142 */
143 public function enable() {
144 $settings = WPO_Cache_Config::instance()->get();
145 $settings['enable_page_caching'] = true;
146 return $this->format_save_cache_settings_response($this->save_cache_settings(array('cache-settings' => $settings)));
147 }
148
149 /**
150 * Disable cache.
151 */
152 public function disable() {
153 $settings = WPO_Cache_Config::instance()->get();
154 $settings['enable_page_caching'] = false;
155 return $this->format_save_cache_settings_response($this->save_cache_settings(array('cache-settings' => $settings)));
156 }
157
158 /**
159 * Purge WP-Optimize page cache.
160 *
161 * @return array
162 */
163 public function purge_page_cache() {
164
165 if (!WP_Optimize()->can_purge_the_cache()) {
166 return array(
167 'success' => false,
168 'message' => __('You do not have permission to purge the cache', 'wp-optimize'),
169 );
170 }
171
172 $purged = WP_Optimize()->get_page_cache()->purge();
173 $cache_size = WP_Optimize()->get_page_cache()->get_cache_size();
174 $wpo_page_cache_preloader = WP_Optimize_Page_Cache_Preloader::instance();
175
176 $response = array(
177 'success' => $purged,
178 'size' => WP_Optimize()->format_size($cache_size['size']),
179 'file_count' => $cache_size['file_count'],
180 );
181
182 // if scheduled preload enabled then reschedule and run preloader.
183 if ($wpo_page_cache_preloader->is_scheduled_preload_enabled()) {
184 // cancel preload and reschedule preload action.
185 $wpo_page_cache_preloader->cancel_preload();
186 $wpo_page_cache_preloader->reschedule_preload();
187
188 // run preloader.
189 $wpo_page_cache_preloader->run('scheduled', $response);
190 }
191
192 if ($response['success']) {
193 $response['message'] = __('Page cache purged successfully', 'wp-optimize');
194 }
195
196 return $response;
197 }
198
199 /**
200 * Run cache preload (for wp-cli).
201 *
202 * @return array|bool
203 */
204 public function run_cache_preload_cli() {
205 if (!(defined('WP_CLI') && WP_CLI)) return false;
206
207 // define WPO_ADVANCED_CACHE constant as WP-CLI doesn't load advanced-cache.php file
208 // but we check this constant value wen detecting status of cache
209 if (!defined('WPO_ADVANCED_CACHE')) define('WPO_ADVANCED_CACHE', true);
210 // don't interrupt queue processing
211 add_filter('updraft_interrupt_tasks_queue_load-url-task', '__return_false', 99);
212
213 // if preloading is running then exit.
214 if (WP_Optimize_Page_Cache_Preloader::instance()->is_busy()) {
215 return array(
216 'success' => false,
217 'error' => __('Preloading is currently running in another process.', 'wp-optimize'),
218 );
219 }
220
221 // set default response.
222 $response = array(
223 'success' => true,
224 'message' => __('All URLs were preloaded into cache successfully', 'wp-optimize'),
225 );
226
227 WP_CLI::log(__('Preloading URLs into cache...', 'wp-optimize'));
228
229 return WP_Optimize_Page_Cache_Preloader::instance()->run('manual', $response);
230 }
231
232 /**
233 * Run cache preload action.
234 *
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.
236 */
237 public function run_cache_preload() {
238 return WP_Optimize_Page_Cache_Preloader::instance()->run('manual');
239 }
240
241 /**
242 * Cancel cache preload action.
243 *
244 * @return array
245 */
246 public function cancel_cache_preload() {
247 WP_Optimize_Page_Cache_Preloader::instance()->cancel_preload();
248 return WP_Optimize_Page_Cache_Preloader::instance()->get_status_info();
249 }
250
251 /**
252 * Get status of cache preload.
253 *
254 * @return array
255 */
256 public function get_cache_preload_status() {
257 return WP_Optimize_Page_Cache_Preloader::instance()->get_status_info();
258 }
259
260 /**
261 * Enable or disable browser cache.
262 *
263 * @param array $params - ['browser_cache_expire' => '1 month 15 days 2 hours' || '' - for disable cache]
264 * @return array
265 */
266 public function enable_browser_cache($params) {
267 return WP_Optimize()->get_browser_cache()->enable_browser_cache_command_handler($params);
268 }
269
270 /**
271 * Format save_cache_settings() result for displaying in WP-CLI console
272 *
273 * @param array $response
274 * @return array
275 */
276 private function format_save_cache_settings_response($response) {
277 $result = array(
278 'success' => $response['result'],
279 );
280
281 if (isset($response['error'])) {
282 $result['success'] = false;
283 $result['error'] = $response['error']['message'];
284 }
285
286 if ($result['success']) {
287 $result['message'] = __('Page cache settings updated successfully.', 'wp-optimize');
288 }
289
290 return $result;
291 }
292 }
293