| 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 |
$data['cache-settings']['use_webp_images'] = (bool) WP_Optimize()->get_options()->get_option('webp_conversion'); |
| 94 |
|
| 95 |
$skip_if_no_file_yet = !$enabled || is_wp_error($enabled); |
| 96 |
$save_settings_result = WPO_Cache_Config::instance()->update($data['cache-settings'], $skip_if_no_file_yet); |
| 97 |
|
| 98 |
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); |
| 100 |
$return['result'] = $save_settings_result; |
| 101 |
} else { |
| 102 |
// Saving the settings returned an error |
| 103 |
if (is_wp_error($save_settings_result)) { |
| 104 |
if (isset($return['error'])) { |
| 105 |
$return['error']['message'] .= "\n\n".$save_settings_result->get_error_message(); |
| 106 |
} else { |
| 107 |
$return['error'] = array( |
| 108 |
'code' => $save_settings_result->get_error_code(), |
| 109 |
'message' => $save_settings_result->get_error_message() |
| 110 |
); |
| 111 |
} |
| 112 |
} |
| 113 |
$return['result'] = false; |
| 114 |
} |
| 115 |
|
| 116 |
$return['enabled'] = ($enabled && !is_wp_error($enabled)) || ($previous_settings['enable_page_caching'] && is_wp_error($disabled)); |
| 117 |
|
| 118 |
return $return; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Get information about current cache status. Used in cli commands. |
| 123 |
* |
| 124 |
* @return array |
| 125 |
*/ |
| 126 |
public function get_status_info() { |
| 127 |
$status = array(); |
| 128 |
|
| 129 |
$status[] = WPO_Page_Cache::instance()->is_enabled() ? __('Caching is enabled', 'wp-optimize') : __('Caching is disabled', 'wp-optimize'); |
| 130 |
|
| 131 |
$preloader_status = WP_Optimize_Page_Cache_Preloader::instance()->get_status_info(); |
| 132 |
$status[] = sprintf(__('Current cache size: %s', 'wp-optimize'), $preloader_status['size']); |
| 133 |
$status[] = sprintf(__('Number of files: %s', 'wp-optimize'), $preloader_status['file_count']); |
| 134 |
|
| 135 |
if (array_key_exists('message', $preloader_status)) $status[] = $preloader_status['message']; |
| 136 |
|
| 137 |
$status['message'] = join(PHP_EOL, $status); |
| 138 |
|
| 139 |
return $status; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Enable cache. |
| 144 |
*/ |
| 145 |
public function enable() { |
| 146 |
$settings = WPO_Cache_Config::instance()->get(); |
| 147 |
$settings['enable_page_caching'] = true; |
| 148 |
return $this->format_save_cache_settings_response($this->save_cache_settings(array('cache-settings' => $settings))); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Disable cache. |
| 153 |
*/ |
| 154 |
public function disable() { |
| 155 |
$settings = WPO_Cache_Config::instance()->get(); |
| 156 |
$settings['enable_page_caching'] = false; |
| 157 |
return $this->format_save_cache_settings_response($this->save_cache_settings(array('cache-settings' => $settings))); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Purge WP-Optimize page cache. |
| 162 |
* |
| 163 |
* @return array |
| 164 |
*/ |
| 165 |
public function purge_page_cache() { |
| 166 |
|
| 167 |
if (!WP_Optimize()->get_page_cache()->is_enabled()) { |
| 168 |
return array( |
| 169 |
'success' => false, |
| 170 |
'error' => __('Cache is not enabled.', 'wp-optimize'), |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
if (!WP_Optimize()->get_page_cache()->can_purge_cache() && !(defined('WP_CLI') && WP_CLI)) { |
| 175 |
return array( |
| 176 |
'success' => false, |
| 177 |
'message' => __('You do not have permission to purge the cache', 'wp-optimize'), |
| 178 |
); |
| 179 |
} |
| 180 |
|
| 181 |
$purged = WP_Optimize()->get_page_cache()->purge(); |
| 182 |
$cache_size = WP_Optimize()->get_page_cache()->get_cache_size(); |
| 183 |
$wpo_page_cache_preloader = WP_Optimize_Page_Cache_Preloader::instance(); |
| 184 |
|
| 185 |
$response = array( |
| 186 |
'success' => $purged, |
| 187 |
'size' => WP_Optimize()->format_size($cache_size['size']), |
| 188 |
'file_count' => $cache_size['file_count'], |
| 189 |
); |
| 190 |
|
| 191 |
// if scheduled preload enabled then reschedule and run preloader. |
| 192 |
if ($wpo_page_cache_preloader->is_scheduled_preload_enabled()) { |
| 193 |
// cancel preload and reschedule preload action. |
| 194 |
$wpo_page_cache_preloader->cancel_preload(); |
| 195 |
$wpo_page_cache_preloader->reschedule_preload(); |
| 196 |
|
| 197 |
// run preloader. |
| 198 |
$wpo_page_cache_preloader->run('scheduled', $response); |
| 199 |
} |
| 200 |
|
| 201 |
if ($response['success']) { |
| 202 |
$response['message'] = __('Page cache purged successfully', 'wp-optimize'); |
| 203 |
} |
| 204 |
|
| 205 |
return $response; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Run cache preload (for wp-cli). |
| 210 |
* |
| 211 |
* @return array|bool |
| 212 |
*/ |
| 213 |
public function run_cache_preload_cli() { |
| 214 |
if (!(defined('WP_CLI') && WP_CLI)) return false; |
| 215 |
|
| 216 |
// define WPO_ADVANCED_CACHE constant as WP-CLI doesn't load advanced-cache.php file |
| 217 |
// but we check this constant value wen detecting status of cache |
| 218 |
if (!defined('WPO_ADVANCED_CACHE')) define('WPO_ADVANCED_CACHE', true); |
| 219 |
// don't interrupt queue processing |
| 220 |
add_filter('updraft_interrupt_tasks_queue_load-url-task', '__return_false', 99); |
| 221 |
|
| 222 |
// if preloading is running then exit. |
| 223 |
if (WP_Optimize_Page_Cache_Preloader::instance()->is_busy()) { |
| 224 |
return array( |
| 225 |
'success' => false, |
| 226 |
'error' => __('Preloading is currently running in another process.', 'wp-optimize'), |
| 227 |
); |
| 228 |
} |
| 229 |
|
| 230 |
// set default response. |
| 231 |
$response = array( |
| 232 |
'success' => true, |
| 233 |
'message' => __('All URLs were preloaded into cache successfully', 'wp-optimize'), |
| 234 |
); |
| 235 |
|
| 236 |
WP_CLI::log(__('Preloading URLs into cache...', 'wp-optimize')); |
| 237 |
|
| 238 |
return WP_Optimize_Page_Cache_Preloader::instance()->run('manual', $response); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Run cache preload action. |
| 243 |
* |
| 244 |
* @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. |
| 245 |
*/ |
| 246 |
public function run_cache_preload() { |
| 247 |
return WP_Optimize_Page_Cache_Preloader::instance()->run('manual'); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Cancel cache preload action. |
| 252 |
* |
| 253 |
* @return array |
| 254 |
*/ |
| 255 |
public function cancel_cache_preload() { |
| 256 |
WP_Optimize_Page_Cache_Preloader::instance()->cancel_preload(); |
| 257 |
return WP_Optimize_Page_Cache_Preloader::instance()->get_status_info(); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Get status of cache preload. |
| 262 |
* |
| 263 |
* @return array |
| 264 |
*/ |
| 265 |
public function get_cache_preload_status() { |
| 266 |
return WP_Optimize_Page_Cache_Preloader::instance()->get_status_info(); |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Enable or disable browser cache. |
| 271 |
* |
| 272 |
* @param array $params - ['browser_cache_expire' => '1 month 15 days 2 hours' || '' - for disable cache] |
| 273 |
* @return array |
| 274 |
*/ |
| 275 |
public function enable_browser_cache($params) { |
| 276 |
return WP_Optimize()->get_browser_cache()->enable_browser_cache_command_handler($params); |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Format save_cache_settings() result for displaying in WP-CLI console |
| 281 |
* |
| 282 |
* @param array $response |
| 283 |
* @return array |
| 284 |
*/ |
| 285 |
private function format_save_cache_settings_response($response) { |
| 286 |
$result = array( |
| 287 |
'success' => $response['result'], |
| 288 |
); |
| 289 |
|
| 290 |
if (isset($response['error'])) { |
| 291 |
$result['success'] = false; |
| 292 |
$result['error'] = $response['error']['message']; |
| 293 |
} |
| 294 |
|
| 295 |
if ($result['success']) { |
| 296 |
$result['message'] = __('Page cache settings updated successfully.', 'wp-optimize'); |
| 297 |
} |
| 298 |
|
| 299 |
return $result; |
| 300 |
} |
| 301 |
} |
| 302 |
|