| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('WPO_PLUGIN_MAIN_PATH')) die('No direct access allowed'); |
| 4 |
|
| 5 |
/** |
| 6 |
* All 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_Commands { |
| 9 |
|
| 10 |
private $optimizer; |
| 11 |
|
| 12 |
private $options; |
| 13 |
|
| 14 |
private $wpo_sites; // used in get_optimizations_info command. |
| 15 |
|
| 16 |
public function __construct() { |
| 17 |
$this->optimizer = WP_Optimize()->get_optimizer(); |
| 18 |
$this->options = WP_Optimize()->get_options(); |
| 19 |
} |
| 20 |
|
| 21 |
public function get_version() { |
| 22 |
return WPO_VERSION; |
| 23 |
} |
| 24 |
|
| 25 |
public function enable_or_disable_feature($data) { |
| 26 |
|
| 27 |
$type = (string) $data['type']; |
| 28 |
$enable = (boolean) $data['enable']; |
| 29 |
|
| 30 |
$options = array($type => $enable); |
| 31 |
|
| 32 |
return $this->optimizer->trackback_comment_actions($options); |
| 33 |
} |
| 34 |
|
| 35 |
public function save_manual_run_optimization_options($sent_options) { |
| 36 |
return $this->options->save_sent_manual_run_optimization_options($sent_options); |
| 37 |
} |
| 38 |
|
| 39 |
public function get_status_box_contents() { |
| 40 |
return WP_Optimize()->include_template('settings/status-box-contents.php', true, array('optimize_db' => false)); |
| 41 |
} |
| 42 |
|
| 43 |
public function get_optimizations_table() { |
| 44 |
return WP_Optimize()->include_template('database/optimizations-table.php', true); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Pulls and return the "WP Optimize" template contents. Primarily used for UpdraftCentral |
| 49 |
* content display through ajax request. |
| 50 |
* |
| 51 |
* @return array An array containing the WPO translations and the "WP Optimize" tab's rendered contents |
| 52 |
*/ |
| 53 |
public function get_wp_optimize_contents() { |
| 54 |
$content = WP_Optimize()->include_template('database/optimize-table.php', true, array('optimize_db' => false)); |
| 55 |
$content .= $this->get_status_box_contents(); |
| 56 |
|
| 57 |
return array( |
| 58 |
'content' => $content, |
| 59 |
'translations' => $this->get_js_translation() |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Pulls and return the "Table Information" template contents. Primarily used for UpdraftCentral |
| 65 |
* content display through ajax request. |
| 66 |
* |
| 67 |
* @return array An array containing the WPO translations and the "Table Information" tab's rendered contents |
| 68 |
*/ |
| 69 |
public function get_table_information_contents() { |
| 70 |
$content = WP_Optimize()->include_template('database/tables.php', true, array('optimize_db' => false)); |
| 71 |
|
| 72 |
return array( |
| 73 |
'content' => $content, |
| 74 |
'translations' => $this->get_js_translation() |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Pulls and return the "Settings" template contents. Primarily used for UpdraftCentral |
| 80 |
* content display through ajax request. |
| 81 |
* |
| 82 |
* @return array An array containing the WPO translations and the "Settings" tab's rendered contents |
| 83 |
*/ |
| 84 |
public function get_settings_contents() { |
| 85 |
$admin_settings = '<form action="#" method="post" enctype="multipart/form-data" name="settings_form" id="settings_form">'; |
| 86 |
$admin_settings .= WP_Optimize()->include_template('settings/settings-general.php', true, array('optimize_db' => false)); |
| 87 |
$admin_settings .= WP_Optimize()->include_template('settings/settings-auto-cleanup.php', true, array('optimize_db' => false)); |
| 88 |
$admin_settings .= WP_Optimize()->include_template('settings/settings-logging.php', true, array('optimize_db' => false)); |
| 89 |
$admin_settings .= '<input id="wp-optimize-settings-save" class="button button-primary" type="submit" name="wp-optimize-settings" value="' . esc_attr('Save settings', 'wp-optimize') .'" />'; |
| 90 |
$admin_settings .= '</form>'; |
| 91 |
$admin_settings .= WP_Optimize()->include_template('settings/settings-trackback-and-comments.php', true, array('optimize_db' => false)); |
| 92 |
$content = $admin_settings; |
| 93 |
|
| 94 |
return array( |
| 95 |
'content' => $content, |
| 96 |
'translations' => $this->get_js_translation() |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Returns array of translations used by the WPO plugin. Primarily used for UpdraftCentral |
| 102 |
* consumption. |
| 103 |
* |
| 104 |
* @return array The WPO translations |
| 105 |
*/ |
| 106 |
public function get_js_translation() { |
| 107 |
$translations = WP_Optimize()->wpo_js_translations(); |
| 108 |
|
| 109 |
// Make sure that we include the loggers classes info whenever applicable before |
| 110 |
// returning the translations to UpdraftCentral. |
| 111 |
if (is_callable(array(WP_Optimize(), 'get_loggers_classes_info'))) { |
| 112 |
$translations['loggers_classes_info'] = WP_Optimize()->get_loggers_classes_info(); |
| 113 |
} |
| 114 |
|
| 115 |
return $translations; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Save settings command. |
| 120 |
* |
| 121 |
* @param string $data |
| 122 |
* @return array |
| 123 |
*/ |
| 124 |
public function save_settings($data) { |
| 125 |
|
| 126 |
parse_str(stripslashes($data), $posted_settings); |
| 127 |
|
| 128 |
// We now have $posted_settings as an array. |
| 129 |
return array( |
| 130 |
'save_results' => $this->options->save_settings($posted_settings), |
| 131 |
'status_box_contents' => $this->get_status_box_contents(), |
| 132 |
'optimizations_table' => $this->get_optimizations_table(), |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Save lazy load settings. |
| 138 |
* |
| 139 |
* @param string $data |
| 140 |
* @return array |
| 141 |
*/ |
| 142 |
public function save_lazy_load_settings($data) { |
| 143 |
parse_str(stripslashes($data), $posted_settings); |
| 144 |
|
| 145 |
return array( |
| 146 |
'save_result' => $this->options->save_lazy_load_settings($posted_settings) |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* This sends the selected tick value over to the save function |
| 152 |
* within class-wp-optimize-options.php |
| 153 |
* |
| 154 |
* @param array $data An array of data that includes true or false for click option. |
| 155 |
* @return array |
| 156 |
*/ |
| 157 |
public function save_auto_backup_option($data) { |
| 158 |
return array('save_auto_backup_option' => $this->options->save_auto_backup_option($data)); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Save option which sites to optimize in multisite mode. |
| 163 |
* |
| 164 |
* @param array $data Array of settings. |
| 165 |
* @return bool |
| 166 |
*/ |
| 167 |
public function save_site_settings($data) { |
| 168 |
return $this->options->save_wpo_sites_option($data['wpo-sites']); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Perform the requested optimization |
| 173 |
* |
| 174 |
* @param array $params Should have keys 'optimization_id' and 'data'. |
| 175 |
* @return array |
| 176 |
*/ |
| 177 |
public function do_optimization($params) { |
| 178 |
|
| 179 |
if (!isset($params['optimization_id'])) { |
| 180 |
$results = array( |
| 181 |
'result' => false, |
| 182 |
'messages' => array(), |
| 183 |
'errors' => array( |
| 184 |
__('No optimization was indicated.', 'wp-optimize') |
| 185 |
) |
| 186 |
); |
| 187 |
} else { |
| 188 |
$optimization_id = $params['optimization_id']; |
| 189 |
$data = isset($params['data']) ? $params['data'] : array(); |
| 190 |
|
| 191 |
$optimization = $this->optimizer->get_optimization($optimization_id, $data); |
| 192 |
|
| 193 |
$result = is_a($optimization, 'WP_Optimization') ? $optimization->do_optimization() : null; |
| 194 |
|
| 195 |
$results = array( |
| 196 |
'result' => $result, |
| 197 |
'messages' => array(), |
| 198 |
'errors' => array(), |
| 199 |
'status_box_contents' => $this->get_status_box_contents() |
| 200 |
); |
| 201 |
|
| 202 |
if (is_wp_error($optimization)) { |
| 203 |
$results['errors'][] = $optimization->get_error_message().' ('.$optimization->get_error_code().')'; |
| 204 |
} |
| 205 |
|
| 206 |
if ($optimization->get_changes_table_data()) { |
| 207 |
$table_list = $this->get_table_list(); |
| 208 |
$results['table_list'] = $table_list['table_list']; |
| 209 |
$results['total_size'] = $table_list['total_size']; |
| 210 |
} |
| 211 |
} |
| 212 |
return $results; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Preview command, used to show information about data should be optimized in popup tool. |
| 217 |
* |
| 218 |
* @param array $params Should have keys 'optimization_id', 'offset' and 'limit'. |
| 219 |
* |
| 220 |
* @return array |
| 221 |
*/ |
| 222 |
public function preview($params) { |
| 223 |
if (!isset($params['optimization_id'])) { |
| 224 |
$results = array( |
| 225 |
'result' => false, |
| 226 |
'messages' => array(), |
| 227 |
'errors' => array( |
| 228 |
__('No optimization was indicated.', 'wp-optimize') |
| 229 |
) |
| 230 |
); |
| 231 |
} else { |
| 232 |
$optimization_id = $params['optimization_id']; |
| 233 |
$data = isset($params['data']) ? $params['data'] : array(); |
| 234 |
$params['offset'] = isset($params['offset']) ? (int) $params['offset'] : 0; |
| 235 |
$params['limit'] = isset($params['limit']) ? (int) $params['limit'] : 50; |
| 236 |
|
| 237 |
$optimization = $this->optimizer->get_optimization($optimization_id, $data); |
| 238 |
|
| 239 |
if (is_a($optimization, 'WP_Optimization')) { |
| 240 |
if (isset($params['site_id'])) { |
| 241 |
$optimization->switch_to_blog((int) $params['site_id']); |
| 242 |
} |
| 243 |
$result = $optimization->preview($params); |
| 244 |
} else { |
| 245 |
$result = null; |
| 246 |
} |
| 247 |
|
| 248 |
$results = array( |
| 249 |
'result' => $result, |
| 250 |
'messages' => array(), |
| 251 |
'errors' => array() |
| 252 |
); |
| 253 |
} |
| 254 |
|
| 255 |
return $results; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Get information about requested optimization. |
| 260 |
* |
| 261 |
* @param array $params Should have keys 'optimization_id' and 'data'. |
| 262 |
* @return array |
| 263 |
*/ |
| 264 |
public function get_optimization_info($params) { |
| 265 |
if (!isset($params['optimization_id'])) { |
| 266 |
$results = array( |
| 267 |
'result' => false, |
| 268 |
'messages' => array(), |
| 269 |
'errors' => array( |
| 270 |
__('No optimization was indicated.', 'wp-optimize') |
| 271 |
) |
| 272 |
); |
| 273 |
} else { |
| 274 |
$optimization_id = $params['optimization_id']; |
| 275 |
$data = isset($params['data']) ? $params['data'] : array(); |
| 276 |
|
| 277 |
$optimization = $this->optimizer->get_optimization($optimization_id, $data); |
| 278 |
$result = is_a($optimization, 'WP_Optimization') ? $optimization->get_optimization_info() : null; |
| 279 |
|
| 280 |
$results = array( |
| 281 |
'result' => $result, |
| 282 |
'messages' => array(), |
| 283 |
'errors' => array(), |
| 284 |
'status_box_contents' => $this->get_status_box_contents() |
| 285 |
); |
| 286 |
} |
| 287 |
|
| 288 |
return $results; |
| 289 |
} |
| 290 |
|
| 291 |
public function get_table_list() { |
| 292 |
|
| 293 |
list ($total_size, $part2) = $this->optimizer->get_current_db_size(); |
| 294 |
|
| 295 |
return array( |
| 296 |
'table_list' => WP_Optimize()->include_template('database/tables-body.php', true, array('optimize_db' => false)), |
| 297 |
'total_size' => $total_size |
| 298 |
); |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Do action wp_optimize_after_optimizations |
| 303 |
* used in ajax request after all optimizations completed |
| 304 |
* |
| 305 |
* @return boolean |
| 306 |
*/ |
| 307 |
public function optimizations_done() { |
| 308 |
|
| 309 |
$this->options->update_option('total-cleaned', 0); |
| 310 |
// Run action after all optimizations completed. |
| 311 |
do_action('wp_optimize_after_optimizations'); |
| 312 |
|
| 313 |
return true; |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Return information about all optimizations. |
| 318 |
* |
| 319 |
* @param array $params |
| 320 |
* @return array |
| 321 |
*/ |
| 322 |
public function get_optimizations_info($params) { |
| 323 |
$this->wpo_sites = isset($params['wpo-sites']) ? $params['wpo-sites'] : 0; |
| 324 |
|
| 325 |
add_filter('get_optimization_blogs', array($this, 'get_optimization_blogs_filter')); |
| 326 |
|
| 327 |
$results = array(); |
| 328 |
$optimizations = $this->optimizer->get_optimizations(); |
| 329 |
|
| 330 |
foreach ($optimizations as $optimization_id => $optimization) { |
| 331 |
if (false === $optimization->display_in_optimizations_list()) continue; |
| 332 |
|
| 333 |
$results[$optimization_id] = $optimization->get_settings_html(); |
| 334 |
} |
| 335 |
|
| 336 |
return $results; |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Filter for get_optimizations_blogs function, used in get_optimizations_info command. |
| 341 |
* Not intended for direct usage as a command (is used internally as a WP filter) |
| 342 |
* |
| 343 |
* The class variable $wpo_sites is used for performing the filtering. |
| 344 |
* |
| 345 |
* @param array $sites - unfiltered list of sites |
| 346 |
* @return array - after filtering |
| 347 |
*/ |
| 348 |
public function get_optimization_blogs_filter($sites) { |
| 349 |
$sites = array(); |
| 350 |
|
| 351 |
if (!empty($this->wpo_sites)) { |
| 352 |
foreach ($this->wpo_sites as $site) { |
| 353 |
if ('all' !== $site) $sites[] = $site; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
return $sites; |
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Checks overdue crons and return message |
| 362 |
*/ |
| 363 |
public function check_overdue_crons() { |
| 364 |
$overdue_crons = WP_Optimize()->howmany_overdue_crons(); |
| 365 |
|
| 366 |
if ($overdue_crons >= 4) { |
| 367 |
return array('m' => WP_Optimize()->show_admin_warning_overdue_crons($overdue_crons)); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* Enable or disable Gzip compression. |
| 373 |
* |
| 374 |
* @param array $params - ['enable' => true|false] |
| 375 |
* @return array |
| 376 |
*/ |
| 377 |
public function enable_gzip_compression($params) { |
| 378 |
return WP_Optimize()->get_gzip_compression()->enable_gzip_command_handler($params); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
/** |
| 383 |
* Enable or disable browser cache. |
| 384 |
* |
| 385 |
* @param array $params - ['browser_cache_expire' => '1 month 15 days 2 hours' || '' - for disable cache] |
| 386 |
* @return array |
| 387 |
*/ |
| 388 |
public function enable_browser_cache($params) { |
| 389 |
return WP_Optimize()->get_browser_cache()->enable_browser_cache_command_handler($params); |
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* Import WP-Optimize settings. |
| 394 |
* |
| 395 |
* @param array $params array with 'settings' item where 'settings' json-encoded string. |
| 396 |
* |
| 397 |
* @return Array - the results of the import operation |
| 398 |
*/ |
| 399 |
public function import_settings($params) { |
| 400 |
if (empty($params['settings'])) { |
| 401 |
return array('errors' => array(__('Please upload a valid settings file.', 'wp-optimize'))); |
| 402 |
} |
| 403 |
|
| 404 |
$params['settings'] = stripslashes($params['settings']); |
| 405 |
|
| 406 |
$settings = json_decode($params['settings'], true); |
| 407 |
|
| 408 |
// check if valid json file posted (requires PHP 5.3+) |
| 409 |
// @codingStandardsIgnoreLine |
| 410 |
if ((function_exists('json_last_error') && 0 != json_last_error()) || empty($settings)) { |
| 411 |
return array('errors' => array(__('Please upload a valid settings file.', 'wp-optimize'))); |
| 412 |
} |
| 413 |
|
| 414 |
return WP_Optimize()->get_options()->save_settings($settings); |
| 415 |
} |
| 416 |
} |
| 417 |
|