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-optimization.php

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

488 lines 12.4 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_VERSION')) die('No direct access allowed');
4
5 /**
6 * Parent class for all optimizations.
7 */
8 abstract class WP_Optimization {
9
10 /**
11 * Ideally, these would all be the same. But, historically, some are not; hence, three separate IDs.
12 *
13 * @var $id
14 */
15 public $id;
16
17 protected $setting_id;
18
19 protected $dom_id;
20
21 protected $auto_id;
22
23 protected $available_for_auto;
24
25 protected $ui_sort_order;
26
27 protected $run_sort_order = 1000;
28
29 public $run_multisite = true;
30
31 public $support_preview = true; // if true then optimization support preview action for optimization data.
32
33 protected $support_ajax_get_info = false; // set to true if optimization support getting info about optimization asynchronously.
34
35 /**
36 * This property indicates whether running this optimization is likely to change the overall table optimization state. We set this to 'true' on optimizations that run SQL OPTIMIZE commands. It is only used for the UI. Strictly, of course, any optimization that deletes something can cause increased fragmentation; so; in that sense, it would be true for every optimization; but since we are just using it to keep the UI reasonably fresh, and since there is a manual "refresh" button, we set it only on some optimizations.
37 *
38 * @var [$changes_table_data
39 */
40 protected $changes_table_data;
41
42 protected $optimizer;
43
44 protected $options;
45
46 protected $logger;
47
48 protected $data;
49
50 /**
51 * Blogs ids for optimization.
52 *
53 * @var $blogs_ids
54 */
55 public $blogs_ids;
56
57 /**
58 * Count of blogs for optimization.
59 *
60 * @var $blogs_count
61 */
62 public $blogs_count;
63
64 /**
65 * Store count of optimized items.
66 *
67 * @var $processed_count
68 */
69 public $processed_count;
70
71 /**
72 * Store found items for optimization. Used in get_info() and related functions.
73 *
74 * @var $found_count;
75 */
76 public $found_count;
77
78 public $retention_enabled;
79
80 public $retention_period;
81
82 public $revisions_retention_enabled;
83
84 public $revisions_retention_count;
85
86 /**
87 * Results. These should be accessed via get_results()
88 *
89 * @var $output
90 */
91 private $output;
92
93 private $meta;
94
95 private $sql_commands;
96
97 protected $wpdb;
98
99 /**
100 * This is abstracted so as to provide future possibilities, e.g. logging.
101 *
102 * @param string $sql The quesry for SQL to be ran.
103 * @return array Return array of results
104 */
105 protected function query($sql) {
106 $this->sql_commands[] = $sql;
107 do_action('wp_optimize_optimization_query', $sql, $this);
108 $result = $this->wpdb->query($sql);
109 return apply_filters('wp_optimize_optimization_query_result', $result, $sql, $this);
110 }
111
112 /**
113 * Display or hide optimization in optimizations list.
114 *
115 * @return bool
116 */
117 public function display_in_optimizations_list() {
118 return true;
119 }
120
121 /**
122 * Returns data those should be optimized. Used to display this information in a popup tool
123 * for previewing and removing certain data for optimization.
124 *
125 * @param array $params
126 *
127 * @return array
128 */
129 public function preview($params) {
130 return array(
131 'id_key' => 'id', // key used used to identify data.
132 'offset' => $params['offset'],
133 'limit' => $params['limit'],
134 'total' => 0,
135 'data' => array(), // returned data as associative array where keys are column names and values - database cell values.
136 );
137 }
138
139 /**
140 * Convert all applicable characters to HTML entities in array. Used to prepare data for output in browser for preview.
141 *
142 * @param array $array source array
143 * @param array $exclude_keys what items shouldn't be encoded.
144 *
145 * @return array
146 */
147 public function htmlentities_array($array, $exclude_keys = array()) {
148 if (!is_array($array) || empty($array)) return $array;
149
150 foreach ($array as $key => $value) {
151 if (in_array($key, $exclude_keys)) continue;
152
153 if (!is_array($value)) {
154 $array[$key] = htmlentities($value);
155 } else {
156 $array[$key] = $this->htmlentities_array($value);
157 }
158 }
159
160 return $array;
161 }
162
163 /**
164 * Do actions before get_info() function.
165 */
166 public function before_get_info() {
167 $this->found_count = 0;
168 }
169
170 /**
171 * Do actions after get_info() function.
172 */
173 public function after_get_info() {
174
175 }
176
177 abstract public function get_info();
178
179 /**
180 * Do actions before optimize() function.
181 */
182 public function before_optimize() {
183 $this->processed_count = 0;
184 }
185
186 /**
187 * Do actions after optimize() function.
188 */
189 public function after_optimize() {
190
191 }
192
193 abstract public function optimize();
194
195 abstract public function settings_label();
196
197 /**
198 * WP_Optimization constructor.
199 *
200 * @param array $data initial data for optimization.
201 */
202 public function __construct($data = array()) {
203 $class_name = get_class($this);
204 // Remove the prefixed WP_Optimization_.
205 $this->id = substr($class_name, 16);
206 $this->data = $data;
207 $this->optimizer = WP_Optimize()->get_optimizer();
208 $this->options = WP_Optimize()->get_options();
209 $this->logger = WP_Optimize()->get_logger();
210 $wpdb = $GLOBALS['wpdb'];
211 $this->wpdb = $wpdb;
212 $this->blogs_ids = $this->get_optimization_blogs();
213 $this->init();
214 }
215
216 /**
217 * This triggers the do_optimization function
218 * within class-wp-optimizer.php to kick off the optimizations.
219 * It also passed the data array from the wpadmin.js.
220 *
221 * @return array array of results that includes sql_commands, output and meta
222 */
223 public function do_optimization() {
224 return $this->optimizer->do_optimization($this);
225 }
226
227 /**
228 * This gathers the optimization information to be displayed
229 * before triggering any optimizations
230 *
231 * @return object Returns an object of optimization information
232 */
233 public function get_optimization_info() {
234 return $this->optimizer->get_optimization_info($this);
235 }
236
237 /**
238 * Returns array of blog ids
239 *
240 * @return array
241 */
242 public function get_optimization_blogs() {
243 $objects = array();
244
245 if ($this->is_multisite_mode()) {
246 $all_sites = false;
247 $selected_sites = array();
248
249 // support both arrays and single values in data site id parameter.
250 if (isset($this->data['site_id']) && !is_array($this->data['site_id'])) {
251 $this->data['site_id'] = array($this->data['site_id']);
252 }
253
254 $optimization_sites = (isset($this->data['site_id'])) ? $this->data['site_id'] : $this->options->get_wpo_sites_option();
255
256 // check selected sites field.
257 if (!empty($optimization_sites)) {
258 foreach ($optimization_sites as $site_id) {
259 if ('all' == $site_id) {
260 $all_sites = true;
261 } else {
262 $selected_sites[] = $site_id;
263 }
264 }
265 }
266
267 $sites = $this->get_sites();
268 if (!empty($sites)) {
269 foreach ($sites as $site) {
270 if ($all_sites || (in_array($site->blog_id, $selected_sites))) {
271 $objects[] = $site->blog_id;
272 }
273 }
274 } else {
275 $objects[] = 1;
276 }
277 } else {
278 $objects[] = 1;
279 }
280
281 return apply_filters('get_optimization_blogs', $objects);
282 }
283
284 /**
285 * Returns true if optimization works in multisite mode
286 *
287 * @return bool
288 */
289 public function is_multisite_mode() {
290 return WP_Optimize()->is_multisite_mode();
291 }
292
293 /**
294 * Returns list of all sites in multisite
295 *
296 * @return array
297 */
298 public function get_sites() {
299 return WP_Optimize()->get_sites();
300 }
301
302 /**
303 * Wrapper for switch_to_blog Wordpress MU function
304 *
305 * @param int $new_blog new blog id.
306 * @return bool|void - if on multisite, then always true (see https://codex.wordpress.org/Function_Reference/switch_to_blog)
307 */
308 public function switch_to_blog($new_blog) {
309 if (function_exists('switch_to_blog') && $this->is_multisite_mode()) {
310 return switch_to_blog($new_blog);
311 }
312 }
313
314 /**
315 * Wrapper for restore_current_blog Wordpress MU function
316 *
317 * @return bool
318 */
319 public function restore_current_blog() {
320 if (function_exists('restore_current_blog') && $this->is_multisite_mode()) {
321 return restore_current_blog();
322 }
323 }
324
325 /**
326 * This function adds output to the current registered output
327 *
328 * @param array $output Array of various outputs.
329 */
330 public function register_output($output) {
331 $this->output[] = $output;
332 }
333
334 /**
335 * This function adds meta-data associated with the result to the registered output
336 *
337 * @param string $key The key value.
338 * @param string $value The value to be passed.
339 */
340 public function register_meta($key, $value) {
341 $this->meta[$key] = $value;
342 }
343
344 /**
345 * Get meta-data added to the registered output.
346 *
347 * @return array
348 */
349 public function get_meta() {
350 return $this->meta;
351 }
352
353 public function init() {
354
355 $this->output = array();
356 $this->meta = array();
357 $this->sql_commands = array();
358
359 list ($retention_enabled, $retention_period) = $this->optimizer->get_retain_info();
360
361 $this->retention_enabled = $retention_enabled;
362 $this->retention_period = $retention_period;
363
364 list($revisions_retention_enabled, $revisions_retention_count) = $this->optimizer->get_revisions_retain_info();
365 $this->revisions_retention_enabled = $revisions_retention_enabled;
366 $this->revisions_retention_count = $revisions_retention_count;
367
368 }
369
370
371 /**
372 * The next three functions reflect the fact that historically, WP-Optimize has not, for all optimizations, used the same ID consistently throughout forms, saved settings, and saved settings for scheduled clean-ups. Mostly, it has; but some flexibility is needed for the exceptions.
373 */
374 public function get_setting_id() {
375 return empty($this->setting_id) ? 'user-'.$this->id : 'user-'.$this->setting_id;
376 }
377
378 public function get_dom_id() {
379 return empty($this->dom_id) ? 'clean-'.$this->id : $this->dom_id;
380 }
381
382 public function get_auto_id() {
383 return empty($this->auto_id) ? $this->id : $this->auto_id;
384 }
385
386 public function get_changes_table_data() {
387 return empty($this->changes_table_data) ? false : true;
388 }
389
390 public function get_run_sort_order() {
391 return empty($this->run_sort_order) ? 0 : $this->run_sort_order;
392 }
393
394 /**
395 * Only used if $available_for_auto is true, in which case this function should be over-ridden
396 *
397 * @return string Error message.
398 */
399 public function get_auto_option_description() {
400 return 'Error: missing scheduled option description ('.$this->id.')';
401 }
402
403 /**
404 * What is returned must be at least convertible to an array
405 *
406 * @return object Results object.
407 */
408 public function get_results() {
409
410 // As yet, we have no need for a dedicated object type for our results.
411 $results = new stdClass;
412
413 $results->sql_commands = $this->sql_commands;
414 $results->output = $this->output;
415 $results->meta = $this->meta;
416
417 return apply_filters('wp_optimize_optimization_results', $results, $this->id, $this);
418 }
419
420 /**
421 * Generate information about optimization required for show it.
422 *
423 * @param bool $ajax_get_info if true then information about optimization will not generated, i.e. get_optimization_info() won't call.
424 *
425 * @return array
426 */
427 public function get_settings_html($ajax_get_info = false) {
428
429 $wpo_user_selection = $this->options->get_main_settings();
430 $setting_id = $this->get_setting_id();
431 $dom_id = $this->get_dom_id();
432
433 // N.B. Some of the optimizations used to have an onclick call to fCheck(). But that function was commented out, so did nothing.
434 $settings_label = $this->settings_label();
435
436 $setting_activated = ((empty($wpo_user_selection[$setting_id]) || 'false' == $wpo_user_selection[$setting_id]) ? false : true);
437
438 $info = ($ajax_get_info && $this->support_ajax_get_info) ? '...' : $this->get_optimization_info()->output;
439
440 $settings_html = array(
441 'dom_id' => $dom_id,
442 'activated' => $setting_activated,
443 'settings_label' => $settings_label,
444 'info' => $info,
445 'support_ajax_get_info' => $this->support_ajax_get_info
446 );
447
448 if (empty($settings_label)) {
449 // Error_log, as this is a defect.
450 error_log("Optimization with setting ID ".$setting_id." lacks a settings label (method: settings_label())");
451 }
452
453 return $settings_html;
454 }
455
456 /**
457 * Wrap $text as a link for preview action. If preview is not supported then return just $text.
458 *
459 * @param string $text
460 * @param array $attributes
461 *
462 * @return string
463 */
464 public function get_preview_link($text, $attributes = array()) {
465 // if preview is not supported then return just $text.
466 if (false == $this->support_preview || false == WP_Optimize::is_premium()) return $text;
467
468 $attributes = array_merge(
469 array(
470 'title' => __('Preview found items', 'wp-optimize'),
471 'data-id' => $this->id,
472 'data-title' => $this->settings_label(),
473 ),
474 $attributes
475 );
476
477 $str_attr = '';
478
479 foreach ($attributes as $key => $value) {
480 $str_attr .= ' '.$key.'="'.esc_attr($value).'"';
481 }
482
483 $link = '<a href="#" class="wpo-optimization-preview"'.$str_attr.'>'.$text.'</a>';
484
485 return $link;
486 }
487 }
488