PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.0.9
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.0.9
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 3.8.0 All 110 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.0.9, at includes/class-wp-optimization.php

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