| 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 |
$enabled = isset($data['cache-settings']['enable_page_caching']) ? (bool) $data['cache-settings']['enable_page_caching'] : false; |
| 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 |
$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 |
} |
| 122 |
$return['result'] = $save_settings_result; |
| 123 |
} else { |
| 124 |
// Saving the settings returned an error |
| 125 |
if (is_wp_error($save_settings_result)) { |
| 126 |
if (isset($return['error'])) { |
| 127 |
$return['error']['message'] .= "\n\n".$save_settings_result->get_error_message(); |
| 128 |
} else { |
| 129 |
$return['error'] = array( |
| 130 |
'code' => $save_settings_result->get_error_code(), |
| 131 |
'message' => $save_settings_result->get_error_message() |
| 132 |
); |
| 133 |
} |
| 134 |
} |
| 135 |
$return['result'] = false; |
| 136 |
} |
| 137 |
|
| 138 |
$return['enabled'] = ($enabled && !is_wp_error($enabled)) || ($previous_settings['enable_page_caching'] && is_wp_error($disabled)); |
| 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 |
|
| 171 |
return $return; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Get information about current cache status. Used in cli commands. |
| 176 |
* |
| 177 |
* @return array |
| 178 |
*/ |
| 179 |
public function get_status_info() { |
| 180 |
$status = array(); |
| 181 |
|
| 182 |
$status[] = WPO_Page_Cache::instance()->is_enabled() ? __('Caching is enabled', 'wp-optimize') : __('Caching is disabled', 'wp-optimize'); |
| 183 |
|
| 184 |
$preloader_status = WP_Optimize_Page_Cache_Preloader::instance()->get_status_info(); |
| 185 |
// translators: %s: The current cache size |
| 186 |
$status[] = sprintf(__('Current cache size: %s', 'wp-optimize'), $preloader_status['size']); |
| 187 |
// translators: %s: The number of files in the cache |
| 188 |
$status[] = sprintf(__('Number of files: %s', 'wp-optimize'), $preloader_status['file_count']); |
| 189 |
|
| 190 |
if (array_key_exists('message', $preloader_status)) $status[] = $preloader_status['message']; |
| 191 |
|
| 192 |
$status['message'] = join(PHP_EOL, $status); |
| 193 |
|
| 194 |
return $status; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Enable cache. |
| 199 |
*/ |
| 200 |
public function enable() { |
| 201 |
$settings = WPO_Cache_Config::instance()->get(); |
| 202 |
$settings['enable_page_caching'] = true; |
| 203 |
return $this->format_save_cache_settings_response($this->save_cache_settings(array('cache-settings' => $settings))); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Disable cache. |
| 208 |
*/ |
| 209 |
public function disable() { |
| 210 |
$settings = WPO_Cache_Config::instance()->get(); |
| 211 |
$settings['enable_page_caching'] = false; |
| 212 |
return $this->format_save_cache_settings_response($this->save_cache_settings(array('cache-settings' => $settings))); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Purge WP-Optimize page cache. |
| 217 |
* |
| 218 |
* @return array |
| 219 |
*/ |
| 220 |
public function purge_page_cache() { |
| 221 |
|
| 222 |
$wpo_page_cache = WP_Optimize()->get_page_cache(); |
| 223 |
|
| 224 |
if (!$wpo_page_cache->is_enabled()) { |
| 225 |
return array( |
| 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, |
| 234 |
'message' => __('You do not have permission to purge the cache', 'wp-optimize'), |
| 235 |
); |
| 236 |
} |
| 237 |
|
| 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(); |
| 241 |
$wpo_page_cache_preloader = WP_Optimize_Page_Cache_Preloader::instance(); |
| 242 |
|
| 243 |
$response = array( |
| 244 |
'success' => $purged, |
| 245 |
'size' => WP_Optimize()->format_size($cache_size['size']), |
| 246 |
'file_count' => $cache_size['file_count'], |
| 247 |
); |
| 248 |
|
| 249 |
// if scheduled preload enabled then reschedule and run preloader. |
| 250 |
if ($wpo_page_cache_preloader->is_scheduled_preload_enabled()) { |
| 251 |
// cancel preload and reschedule preload action. |
| 252 |
$wpo_page_cache_preloader->cancel_preload(); |
| 253 |
$wpo_page_cache_preloader->reschedule_preload(); |
| 254 |
|
| 255 |
// run preloader. |
| 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); |
| 260 |
} |
| 261 |
|
| 262 |
if ($response['success']) { |
| 263 |
$response['message'] = __('Page cache purged successfully', 'wp-optimize'); |
| 264 |
} |
| 265 |
|
| 266 |
return $response; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Run cache preload (for wp-cli). |
| 271 |
* |
| 272 |
* @return array|bool |
| 273 |
*/ |
| 274 |
public function run_cache_preload_cli() { |
| 275 |
if (!(defined('WP_CLI') && WP_CLI)) return false; |
| 276 |
|
| 277 |
// define WPO_ADVANCED_CACHE constant as WP-CLI doesn't load advanced-cache.php file |
| 278 |
// but we check this constant value wen detecting status of cache |
| 279 |
if (!defined('WPO_ADVANCED_CACHE')) define('WPO_ADVANCED_CACHE', true); |
| 280 |
// don't interrupt queue processing |
| 281 |
add_filter('updraft_interrupt_tasks_queue_load-url-task', '__return_false', 99); |
| 282 |
|
| 283 |
// if preloading is running then exit. |
| 284 |
if (WP_Optimize_Page_Cache_Preloader::instance()->is_busy()) { |
| 285 |
return array( |
| 286 |
'success' => false, |
| 287 |
'error' => __('Preloading is currently running in another process.', 'wp-optimize'), |
| 288 |
); |
| 289 |
} |
| 290 |
|
| 291 |
// set default response. |
| 292 |
$response = array( |
| 293 |
'success' => true, |
| 294 |
'message' => __('All URLs were preloaded into cache successfully', 'wp-optimize'), |
| 295 |
); |
| 296 |
|
| 297 |
WP_CLI::log(__('Preloading URLs into cache...', 'wp-optimize')); |
| 298 |
|
| 299 |
return WP_Optimize_Page_Cache_Preloader::instance()->run('manual', $response); |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Run cache preload action. |
| 304 |
* |
| 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. |
| 306 |
*/ |
| 307 |
public function run_cache_preload() { |
| 308 |
return WP_Optimize_Page_Cache_Preloader::instance()->run('manual'); |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Cancel cache preload action. |
| 313 |
* |
| 314 |
* @return array |
| 315 |
*/ |
| 316 |
public function cancel_cache_preload() { |
| 317 |
WP_Optimize_Page_Cache_Preloader::instance()->cancel_preload(); |
| 318 |
return WP_Optimize_Page_Cache_Preloader::instance()->get_status_info(); |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Get status of cache preload. |
| 323 |
* |
| 324 |
* @return array |
| 325 |
*/ |
| 326 |
public function get_cache_preload_status() { |
| 327 |
return WP_Optimize_Page_Cache_Preloader::instance()->get_status_info(); |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Enable or disable browser cache. |
| 332 |
* |
| 333 |
* @param array $params - ['browser_cache_expire' => '1 month 15 days 2 hours' || '' - for disable cache] |
| 334 |
* @return array |
| 335 |
*/ |
| 336 |
public function enable_browser_cache($params) { |
| 337 |
return WP_Optimize()->get_browser_cache()->enable_browser_cache_command_handler($params); |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Format save_cache_settings() result for displaying in WP-CLI console |
| 342 |
* |
| 343 |
* @param array $response |
| 344 |
* @return array |
| 345 |
*/ |
| 346 |
private function format_save_cache_settings_response($response) { |
| 347 |
$result = array( |
| 348 |
'success' => $response['result'], |
| 349 |
); |
| 350 |
|
| 351 |
if (isset($response['error'])) { |
| 352 |
$result['success'] = false; |
| 353 |
$result['error'] = $response['error']['message']; |
| 354 |
} |
| 355 |
|
| 356 |
if ($result['success']) { |
| 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 |
); |
| 385 |
} |
| 386 |
|
| 387 |
return $result; |
| 388 |
} |
| 389 |
} |
| 390 |
|