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