PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.21
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.21
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 / includes / class-wp-optimize-commands.php

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

630 lines 19.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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('database/status-box-contents.php', true, array('optimize_db' => false));
41 }
42
43 /**
44 * Get the database tabs information.
45 *
46 * @return string auto cleanup content.
47 */
48 public function get_settings_auto_cleanup_contents() {
49 return WP_Optimize()->include_template('database/settings-auto-cleanup.php', true, array('optimize_db' => false, 'show_innodb_option' => WP_Optimize()->template_should_include_data() && $this->optimizer->show_innodb_force_optimize()));
50 }
51
52 /**
53 * Get the settings tab information.
54 *
55 * @return string logging settings content.
56 */
57 public function get_logging_settings_contents() {
58 return WP_Optimize()->include_template('settings/settings-logging.php', true, array('optimize_db' => false));
59 }
60
61 /**
62 * Get the database tabs information
63 *
64 * @return string database table optimization rendered content
65 */
66 public function get_optimizations_table() {
67 return WP_Optimize()->include_template('database/optimizations-table.php', true, array('does_server_allows_table_optimization' => WP_Optimize()->does_server_allows_table_optimization()));
68 }
69
70 /**
71 * Pulls and return the "WP Optimize" template contents. Primarily used for UpdraftCentral
72 * content display through ajax request.
73 *
74 * @return array An array containing the WPO translations and the "WP Optimize" tab's rendered contents
75 */
76 public function get_wp_optimize_contents() {
77 $content = WP_Optimize()->include_template('database/optimize-table.php', true, array('optimize_db' => false, 'load_data' => WP_Optimize()->template_should_include_data(), 'does_server_allows_table_optimization' => WP_Optimize()->does_server_allows_table_optimization()));
78 if (WP_Optimize()->is_updraft_central_request()) {
79 $content .= $this->get_status_box_contents();
80 }
81
82 return array(
83 'content' => $content,
84 'translations' => $this->get_js_translation()
85 );
86 }
87
88 /**
89 * Pulls and return the "Table Information" template contents. Primarily used for UpdraftCentral
90 * content display through ajax request.
91 *
92 * @return array An array containing the WPO translations and the "Table Information" tab's rendered contents
93 */
94 public function get_table_information_contents() {
95 $content = WP_Optimize()->include_template('database/tables.php', true, array('optimize_db' => false, 'load_data' => WP_Optimize()->template_should_include_data()));
96
97 return array(
98 'content' => $content,
99 'translations' => $this->get_js_translation()
100 );
101 }
102
103 /**
104 * Pulls and return the "Settings" template contents. Primarily used for UpdraftCentral
105 * content display through ajax request.
106 *
107 * @return array An array containing the WPO translations and the "Settings" tab's rendered contents
108 */
109 public function get_settings_contents() {
110 $admin_settings = '<form action="#" method="post" enctype="multipart/form-data" name="settings_form" id="settings_form">';
111 $admin_settings .= WP_Optimize()->include_template('database/settings-general.php', true, array('optimize_db' => false));
112 $admin_settings .= WP_Optimize()->include_template('database/settings-auto-cleanup.php', true, array('optimize_db' => false, 'show_innodb_option' => WP_Optimize()->template_should_include_data() && $this->optimizer->show_innodb_force_optimize()));
113 $admin_settings .= WP_Optimize()->include_template('settings/settings-logging.php', true, array('optimize_db' => false));
114 $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') .'" />';
115 $admin_settings .= '</form>';
116 $admin_settings .= WP_Optimize()->include_template('settings/settings-trackback-and-comments.php', true, array('optimize_db' => false));
117 $content = $admin_settings;
118
119 return array(
120 'content' => $content,
121 'translations' => $this->get_js_translation()
122 );
123 }
124
125 /**
126 * Returns array of translations used by the WPO plugin. Primarily used for UpdraftCentral
127 * consumption.
128 *
129 * @return array The WPO translations
130 */
131 public function get_js_translation() {
132 $translations = WP_Optimize()->wpo_js_translations();
133
134 // Make sure that we include the loggers classes info whenever applicable before
135 // returning the translations to UpdraftCentral.
136 if (is_callable(array(WP_Optimize(), 'get_loggers_classes_info'))) {
137 $translations['loggers_classes_info'] = WP_Optimize()->get_loggers_classes_info();
138 }
139
140 return $translations;
141 }
142
143 /**
144 * Save settings command.
145 *
146 * @param string $data
147 * @return array
148 */
149 public function save_settings($data) {
150
151 parse_str(stripslashes($data), $posted_settings);
152
153 $saved_settings = $this->options->save_settings($posted_settings);
154 // We now have $posted_settings as an array.
155 return array(
156 'save_results' => $saved_settings,
157 'status_box_contents' => $this->get_status_box_contents(),
158 'optimizations_table' => $this->get_optimizations_table(),
159 'settings_auto_cleanup_contents' => $this->get_settings_auto_cleanup_contents(),
160 'logging_settings_contents' => $this->get_logging_settings_contents(),
161 );
162 }
163
164 /**
165 * Wipe settings command.
166 *
167 * @return bool|false|int
168 */
169 public function wipe_settings() {
170 return $this->options->wipe_settings();
171 }
172
173 /**
174 * Save lazy load settings.
175 *
176 * @param string $data
177 * @return array
178 */
179 public function save_lazy_load_settings($data) {
180 parse_str(stripslashes($data), $posted_settings);
181
182 return array(
183 'save_result' => $this->options->save_lazy_load_settings($posted_settings)
184 );
185 }
186
187 /**
188 * This sends the selected tick value over to the save function
189 * within class-wp-optimize-options.php
190 *
191 * @param array $data An array of data that includes true or false for click option.
192 * @return array
193 */
194 public function save_auto_backup_option($data) {
195 return array('save_auto_backup_option' => $this->options->save_auto_backup_option($data));
196 }
197
198 /**
199 * Save option which sites to optimize in multisite mode.
200 *
201 * @param array $data Array of settings.
202 * @return bool
203 */
204 public function save_site_settings($data) {
205 return $this->options->save_wpo_sites_option($data['wpo-sites']);
206 }
207
208 /**
209 * Perform the requested optimization
210 *
211 * @param array $params Should have keys 'optimization_id' and 'data'.
212 * @return array
213 */
214 public function do_optimization($params) {
215
216 if (!isset($params['optimization_id'])) {
217 $results = array(
218 'result' => false,
219 'messages' => array(),
220 'errors' => array(
221 __('No optimization was indicated.', 'wp-optimize')
222 )
223 );
224 } else {
225 $optimization_id = $params['optimization_id'];
226 $data = isset($params['data']) ? $params['data'] : array();
227 $include_ui_elements = isset($data['include_ui_elements']) ? $data['include_ui_elements'] : false;
228
229 $optimization = $this->optimizer->get_optimization($optimization_id, $data);
230
231 $result = is_a($optimization, 'WP_Optimization') ? $optimization->do_optimization() : null;
232
233 $results = array(
234 'result' => $result,
235 'messages' => array(),
236 'errors' => array(),
237 );
238
239 if ($include_ui_elements) {
240 $results['status_box_contents'] = $this->get_status_box_contents();
241 }
242
243 if (is_wp_error($optimization)) {
244 $results['errors'][] = $optimization->get_error_message().' ('.$optimization->get_error_code().')';
245 }
246
247 if ($include_ui_elements && $optimization->get_changes_table_data()) {
248 $table_list = $this->get_table_list();
249 $results['table_list'] = $table_list['table_list'];
250 $results['total_size'] = $table_list['total_size'];
251 }
252 }
253 return $results;
254 }
255
256 /**
257 * Preview command, used to show information about data should be optimized in popup tool.
258 *
259 * @param array $params Should have keys 'optimization_id', 'offset' and 'limit'.
260 *
261 * @return array
262 */
263 public function preview($params) {
264 if (!isset($params['optimization_id'])) {
265 $results = array(
266 'result' => false,
267 'messages' => array(),
268 'errors' => array(
269 __('No optimization was indicated.', 'wp-optimize')
270 )
271 );
272 } else {
273 $optimization_id = $params['optimization_id'];
274 $data = isset($params['data']) ? $params['data'] : array();
275 $params['offset'] = isset($params['offset']) ? (int) $params['offset'] : 0;
276 $params['limit'] = isset($params['limit']) ? (int) $params['limit'] : 50;
277
278 $optimization = $this->optimizer->get_optimization($optimization_id, $data);
279
280 if (is_a($optimization, 'WP_Optimization')) {
281 if (isset($params['site_id'])) {
282 $optimization->switch_to_blog((int) $params['site_id']);
283 }
284 $result = $optimization->preview($params);
285 } else {
286 $result = null;
287 }
288
289 $results = array(
290 'result' => $result,
291 'messages' => array(),
292 'errors' => array()
293 );
294 }
295
296 return $results;
297 }
298
299 /**
300 * Get information about requested optimization.
301 *
302 * @param array $params Should have keys 'optimization_id' and 'data'.
303 * @return array
304 */
305 public function get_optimization_info($params) {
306 if (!isset($params['optimization_id'])) {
307 $results = array(
308 'result' => false,
309 'messages' => array(),
310 'errors' => array(
311 __('No optimization was indicated.', 'wp-optimize')
312 )
313 );
314 } else {
315 $optimization_id = $params['optimization_id'];
316 $data = isset($params['data']) ? $params['data'] : array();
317 $include_ui_elements = isset($data['include_ui_elements']) ? $data['include_ui_elements'] : false;
318
319 $optimization = $this->optimizer->get_optimization($optimization_id, $data);
320 $result = is_a($optimization, 'WP_Optimization') ? $optimization->get_optimization_info() : null;
321
322 $results = array(
323 'result' => $result,
324 'messages' => array(),
325 'errors' => array(),
326 );
327
328 if ($include_ui_elements) {
329 $results['status_box_contents'] = $this->get_status_box_contents();
330 }
331 }
332
333 return $results;
334 }
335
336 /**
337 * Get the data for the tables tab
338 *
339 * @param array $data
340 * @return array
341 */
342 public function get_table_list($data = array()) {
343 if (isset($data['refresh_plugin_json']) && filter_var($data['refresh_plugin_json'], FILTER_VALIDATE_BOOLEAN)) WP_Optimize()->get_db_info()->update_plugin_json();
344
345 $size = $this->optimizer->get_current_db_size();
346
347 return apply_filters('wpo_get_tables_data', array(
348 'table_list' => WP_Optimize()->include_template('database/tables-body.php', true, array('optimize_db' => false)),
349 'total_size' => $size[0]
350 ));
351 }
352
353 /**
354 * Get the database tabs information
355 *
356 * @return array
357 */
358 public function get_database_tabs() {
359 return array_merge(array('optimizations' => $this->get_optimizations_table(), 'does_server_allows_table_optimization' => WP_Optimize()->does_server_allows_table_optimization()), $this->get_table_list());
360 }
361
362 /**
363 * Do action wp_optimize_after_optimizations
364 * used in ajax request after all optimizations completed
365 *
366 * @return boolean
367 */
368 public function optimizations_done() {
369
370 $this->options->update_option('total-cleaned', 0);
371 // Run action after all optimizations completed.
372 do_action('wp_optimize_after_optimizations');
373
374 return true;
375 }
376
377 /**
378 * Return information about all optimizations.
379 *
380 * @param array $params
381 * @return array
382 */
383 public function get_optimizations_info($params) {
384 $this->wpo_sites = isset($params['wpo-sites']) ? $params['wpo-sites'] : 0;
385
386 add_filter('get_optimization_blogs', array($this, 'get_optimization_blogs_filter'));
387
388 $results = array();
389 $optimizations = $this->optimizer->get_optimizations();
390
391 foreach ($optimizations as $optimization_id => $optimization) {
392 if (false === $optimization->display_in_optimizations_list()) continue;
393
394 $results[$optimization_id] = $optimization->get_settings_html();
395 }
396
397 return $results;
398 }
399
400 /**
401 * Filter for get_optimizations_blogs function, used in get_optimizations_info command.
402 * Not intended for direct usage as a command (is used internally as a WP filter)
403 *
404 * The class variable $wpo_sites is used for performing the filtering.
405 *
406 * @param array $sites - unfiltered list of sites
407 * @return array - after filtering
408 */
409 public function get_optimization_blogs_filter($sites) {
410 $sites = array();
411
412 if (!empty($this->wpo_sites)) {
413 foreach ($this->wpo_sites as $site) {
414 if ('all' !== $site) $sites[] = $site;
415 }
416 }
417
418 return $sites;
419 }
420
421 /**
422 * Checks overdue crons and return message
423 */
424 public function check_overdue_crons() {
425 $overdue_crons = WP_Optimize()->howmany_overdue_crons();
426
427 if ($overdue_crons >= 4) {
428 return array('m' => WP_Optimize()->show_admin_warning_overdue_crons($overdue_crons));
429 }
430 }
431
432 /**
433 * Enable or disable Gzip compression.
434 *
435 * @param array $params - ['enable' => true|false]
436 * @return array
437 */
438 public function enable_gzip_compression($params) {
439 return WP_Optimize()->get_gzip_compression()->enable_gzip_command_handler($params);
440 }
441
442 /**
443 * Get the current gzip compression status
444 *
445 * @return array
446 */
447 public function get_gzip_compression_status() {
448 $status = WP_Optimize()->get_gzip_compression()->is_gzip_compression_enabled(true);
449 return is_wp_error($status) ? array('error' => __('We could not determine if Gzip compression is enabled.', 'wp-optimize'), 'code' => $status->get_error_code(), 'message' => $status->get_error_message()) : array('status' => $status);
450 }
451
452 /**
453 * Import WP-Optimize settings.
454 *
455 * @param array $params array with 'settings' item where 'settings' json-encoded string.
456 *
457 * @return Array - the results of the import operation
458 */
459 public function import_settings($params) {
460 if (empty($params['settings'])) {
461 return array('errors' => array(__('Please upload a valid settings file.', 'wp-optimize')));
462 }
463
464 $settings = json_decode($params['settings'], true);
465
466 // check if valid json file posted (requires PHP 5.3+)
467 if ((function_exists('json_last_error') && 0 != json_last_error()) || empty($settings)) {
468 return array('errors' => array(__('Please upload a valid settings file.', 'wp-optimize')));
469 }
470
471 $cache_settings = $settings['cache_settings'];
472 $minify_settings = $settings['minify_settings'];
473 $smush_settings = $settings['smush_settings'];
474 $database_settings = $settings['database_settings'];
475
476 $cache = WP_Optimize()->get_page_cache();
477 $cache->create_folders();
478 if ($cache_settings['enable_page_caching']) {
479 $cache->enable();
480 }
481
482 $wpo_browser_cache = WP_Optimize()->get_browser_cache();
483 if (isset($cache_settings['enable_browser_cache']) && $cache_settings['enable_browser_cache']) {
484 $browser_cache = array(
485 'browser_cache_expire_days' => $cache_settings['browser_cache_expire_days'],
486 'browser_cache_expire_hours' => $cache_settings['browser_cache_expire_hours']
487 );
488 $wpo_browser_cache->enable_browser_cache_command_handler($browser_cache);
489 }
490
491 $message = '';
492 $cache_result = WP_Optimize()->get_page_cache()->config->update($cache_settings);
493 $minify_result = WP_Optimize()->get_minify()->minify_commands->save_minify_settings($minify_settings);
494 $smush_result = WP_Optimize()->get_task_manager()->commands->update_smush_options($smush_settings);
495 $this->save_settings($database_settings);
496
497 if (is_wp_error($cache_result)) {
498 $message .= $cache_result->get_error_message() . PHP_EOL;
499 }
500
501 if (!$minify_result['success']) {
502 $message .= isset($minify_result['message']) ? $minify_result['message'] . PHP_EOL : '';
503 $message .= isset($minify_result['error']) ? $minify_result['error'] . PHP_EOL : '';
504 }
505
506 if (is_wp_error($smush_result)) {
507 $message .= $smush_result->get_error_message() . PHP_EOL;
508 }
509
510 return array(
511 'success' => true,
512 'message' => empty($message) ? __('The settings were imported successfully.', 'wp-optimize') : $message
513 );
514 }
515
516 /**
517 * Dismiss install or updated notice
518 *
519 * @return mixed
520 */
521 public function dismiss_install_or_update_notice() {
522 if (!is_a(WP_Optimize()->get_install_or_update_notice(), 'WP_Optimize_Install_Or_Update_Notice') || !is_callable(array(WP_Optimize()->get_install_or_update_notice(), 'dismiss'))) {
523 return array('errors' => array('The notice could not be dismissed. The method "dismiss" on the object instance "install_or_update_notice" does not seem to exist.'));
524 }
525
526 if (!WP_Optimize()->get_install_or_update_notice()->dismiss()) {
527 return array('errors' => array('The notice could not be dismissed. The settings could not be updated'));
528 }
529
530 return true;
531 }
532
533 /**
534 * Run images trash command.
535 */
536 public function images_trash_command($params) {
537 if (!class_exists('WP_Optimize_Images_Trash_Manager_Commands')) {
538 return array(
539 'errors' => array('WP_Optimize_Images_Trash_Manager_Commands class not found'),
540 );
541 }
542
543 // get posted command.
544 $trash_command = isset($params['images_trash_command']) ? $params['images_trash_command'] : '';
545 // check if command is allowed.
546 $allowed_commands = WP_Optimize_Images_Trash_Manager_Commands::get_allowed_ajax_commands();
547
548 if (!in_array($trash_command, $allowed_commands)) {
549 return array(
550 'errors' => array('No such command found'),
551 );
552 }
553
554 $results = call_user_func(array(WP_Optimize_Images_Trash_Manager()->commands, $trash_command), $params);
555
556 if (is_wp_error($results)) {
557 $results = array(
558 'errors' => array($results->get_error_message()),
559 );
560 }
561
562 return $results;
563 }
564
565 /**
566 * Power tweak handling
567 *
568 * @param array $params
569 * @return mixed
570 */
571 public function power_tweak($params) {
572 global $wp_optimize_premium;
573 if (!is_a($wp_optimize_premium, 'WP_Optimize_Premium') || !property_exists($wp_optimize_premium, 'power_tweaks') || !isset($params['sub_action'])) return array(
574 'errors' => array(__('No such command found', 'wp-optimize')),
575 );
576
577 $action = $params['sub_action'];
578 $data = $params['data'] ? $params['data'] : array();
579 if (!isset($data['tweak'])) return array(
580 'errors' => array(__('No tweak provided', 'wp-optimize'))
581 );
582
583 $tweak = sanitize_title($data['tweak']);
584 $pt = $wp_optimize_premium->power_tweaks;
585 switch($action) {
586 case 'activate':
587 $result = $pt->activate($tweak);
588 break;
589 case 'deactivate':
590 $result = $pt->deactivate($tweak);
591 break;
592 case 'run':
593 $result = $pt->run($tweak);
594 break;
595 }
596 if ($result && !is_wp_error($result)) {
597 return is_array($result) ? array_merge(array('success' => true), $result) : array('success' => true, 'message' => $result);
598 } else {
599 $error_message = is_wp_error($result) ? $result->get_error_message() : sprintf(__('The command %s failed', 'wp-optimize'), $action);
600 return array(
601 'success' => false,
602 'errors' => array($error_message)
603 );
604 }
605 }
606
607 /**
608 * Ignores the table delete warning for the current user
609 *
610 * @return boolean
611 */
612 public function user_ignores_table_delete_warning() {
613 return array(
614 'success' => update_user_meta(get_current_user_id(), 'wpo-ignores-table-delete-warning', true)
615 );
616 }
617
618 /**
619 * Exports unused images as a CSV file to the `uploads` folder
620 *
621 * @return array
622 */
623 public function export_csv() {
624 WP_Optimization_images::instance()->output_csv();
625 return array(
626 'success' => true
627 );
628 }
629 }
630