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 / includes / class-commands.php

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

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