PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 2.2.2
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v2.2.2
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 2.2.2, at includes/class-wp-optimization.php

380 lines 9.5 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 protected $support_ajax_get_info = false; // set to true if optimization support getting info about optimization asynchronously.
32
33 /**
34 * 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 optimisations.
35 *
36 * @var [$changes_table_data
37 */
38 protected $changes_table_data;
39
40 protected $optimizer;
41
42 protected $options;
43
44 protected $logger;
45
46 protected $data;
47
48 /**
49 * Blogs ids for optimization.
50 *
51 * @var $blogs_ids
52 */
53 public $blogs_ids;
54
55 /**
56 * Count of blogs for optimization.
57 *
58 * @var $blogs_count
59 */
60 public $blogs_count;
61
62 /**
63 * Store count of optimized items.
64 *
65 * @var $processed_count
66 */
67 public $processed_count;
68
69 /**
70 * Store found items for optimization. Used in get_info() and related functions.
71 *
72 * @var $found_count;
73 */
74 public $found_count;
75
76 public $retention_enabled;
77
78 public $retention_period;
79
80 /**
81 * Results. These should be accessed via get_results()
82 *
83 * @var $output
84 */
85 private $output;
86
87 private $meta;
88
89 private $sql_commands;
90
91 protected $wpdb;
92
93 /**
94 * This is abstracted so as to provide future possibilities, e.g. logging.
95 *
96 * @param string $sql The quesry for SQL to be ran.
97 * @return array Return array of results
98 */
99 protected function query($sql) {
100 $this->sql_commands[] = $sql;
101 do_action('wp_optimize_optimization_query', $sql, $this);
102 $result = $this->wpdb->query($sql);
103 return apply_filters('wp_optimize_optimization_query_result', $result, $sql, $this);
104 }
105
106 /**
107 * Do actions before get_info() function.
108 */
109 public function before_get_info() {
110 $this->found_count = 0;
111 }
112
113 /**
114 * Do actions after get_info() function.
115 */
116 public function after_get_info() {
117
118 }
119
120 abstract public function get_info();
121
122 /**
123 * Do actions before optimize() function.
124 */
125 public function before_optimize() {
126 $this->processed_count = 0;
127 }
128
129 /**
130 * Do actions after optimize() function.
131 */
132 public function after_optimize() {
133
134 }
135
136 abstract public function optimize();
137
138 abstract public function settings_label();
139
140 /**
141 * WP_Optimization constructor.
142 *
143 * @param array $data initial data for optimization.
144 */
145 public function __construct($data = array()) {
146 $class_name = get_class($this);
147 // Remove the prefixed WP_Optimization_.
148 $this->id = substr($class_name, 16);
149 $this->data = $data;
150 $this->optimizer = WP_Optimize()->get_optimizer();
151 $this->options = WP_Optimize()->get_options();
152 $this->logger = WP_Optimize()->get_logger();
153 $wpdb = $GLOBALS['wpdb'];
154 $this->wpdb = $wpdb;
155
156 $this->blogs_ids = $this->get_optimization_blogs();
157 }
158
159 /**
160 * This triggers the do_optimization function
161 * within class-wp-optimizer.php to kick off the optimizations.
162 * It also passed the data array from the wpadmin.js.
163 *
164 * @return array array of results that includes sql_commands, output and meta
165 */
166 public function do_optimization() {
167 return $this->optimizer->do_optimization($this);
168 }
169
170 /**
171 * This gathers the optimization information to be displayed
172 * before triggering any optimizations
173 *
174 * @return array Returns an array of optimization information
175 */
176 public function get_optimization_info() {
177 return $this->optimizer->get_optimization_info($this);
178 }
179
180 /**
181 * Returns array of blog ids
182 *
183 * @return array
184 */
185 public function get_optimization_blogs() {
186 $objects = array();
187
188 if ($this->is_multisite_mode()) {
189 $all_sites = false;
190 $selected_sites = array();
191
192 $optimization_sites = (isset($this->data['site_id'])) ? $this->data['site_id'] : $this->options->get_wpo_sites_option();
193
194 // check selected sites field.
195 if (!empty($optimization_sites)) {
196 foreach ($optimization_sites as $site_id) {
197 if ('all' == $site_id) {
198 $all_sites = true;
199 } else {
200 $selected_sites[] = $site_id;
201 }
202 }
203 }
204
205 $sites = $this->get_sites();
206 if (!empty($sites)) {
207 foreach ($sites as $site) {
208 if ($all_sites || (in_array($site->blog_id, $selected_sites))) {
209 $objects[] = $site->blog_id;
210 }
211 }
212 } else {
213 $objects[] = 1;
214 }
215 } else {
216 $objects[] = 1;
217 }
218
219 return apply_filters('get_optimization_blogs', $objects);
220 }
221
222 /**
223 * Returns true if optimization works in multisite mode
224 *
225 * @return bool
226 */
227 public function is_multisite_mode() {
228 return WP_Optimize()->is_multisite_mode();
229 }
230
231 /**
232 * Returns list of all sites in multisite
233 *
234 * @return array
235 */
236 public function get_sites() {
237 return WP_Optimize()->get_sites();
238 }
239
240 /**
241 * Wrapper for switch_to_blog Wordpress MU function
242 *
243 * @param int $new_blog new blog id.
244 * @return bool|void - if on multisite, then always true (see https://codex.wordpress.org/Function_Reference/switch_to_blog)
245 */
246 public function switch_to_blog($new_blog) {
247 if (function_exists('switch_to_blog') && $this->is_multisite_mode()) {
248 return switch_to_blog($new_blog);
249 }
250 }
251
252 /**
253 * Wrapper for restore_current_blog Wordpress MU function
254 *
255 * @return bool
256 */
257 public function restore_current_blog() {
258 if (function_exists('restore_current_blog') && $this->is_multisite_mode()) {
259 return restore_current_blog();
260 }
261 }
262
263 /**
264 * This function adds output to the current registered output
265 *
266 * @param array $output Array of various outputs.
267 */
268 public function register_output($output) {
269 $this->output[] = $output;
270 }
271
272 /**
273 * This function adds meta-data associated with the result to the registered output
274 *
275 * @param string $key The key value.
276 * @param string $value The value to be passed.
277 */
278 public function register_meta($key, $value) {
279 $this->meta[$key] = $value;
280 }
281
282 public function init() {
283
284 $this->output = array();
285 $this->meta = array();
286 $this->sql_commands = array();
287
288 list ($retention_enabled, $retention_period) = $this->optimizer->get_retain_info();
289
290 $this->retention_enabled = $retention_enabled;
291 $this->retention_period = $retention_period;
292 }
293
294
295 /**
296 * 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 automatic clean-ups. Mostly, it has; but some flexibility is needed for the exceptions.
297 */
298 public function get_setting_id() {
299 return empty($this->setting_id) ? 'user-'.$this->id : 'user-'.$this->setting_id;
300 }
301
302 public function get_dom_id() {
303 return empty($this->dom_id) ? 'clean-'.$this->id : $this->dom_id;
304 }
305
306 public function get_auto_id() {
307 return empty($this->auto_id) ? $this->id : $this->auto_id;
308 }
309
310 public function get_changes_table_data() {
311 return empty($this->changes_table_data) ? false : true;
312 }
313
314 public function get_run_sort_order() {
315 return empty($this->run_sort_order) ? 0 : $this->run_sort_order;
316 }
317
318 /**
319 * Only used if $available_for_auto is true, in which case this function should be over-ridden
320 *
321 * @return string Error message.
322 */
323 public function get_auto_option_description() {
324 return 'Error: missing automatic option description ('.$this->id.')';
325 }
326
327 /**
328 * What is returned must be at least convertible to an array
329 *
330 * @return array Array of results.
331 */
332 public function get_results() {
333
334 // As yet, we have no need for a dedicated object type for our results.
335 $results = new stdClass;
336
337 $results->sql_commands = $this->sql_commands;
338 $results->output = $this->output;
339 $results->meta = $this->meta;
340
341 return apply_filters('wp_optimize_optimization_results', $results, $this->id, $this);
342 }
343
344 /**
345 * Generate information about optimization required for show it.
346 *
347 * @param bool $ajax_get_info if true then information about optimization will not generated, i.e. get_optimization_info() won't call.
348 *
349 * @return array
350 */
351 public function get_settings_html($ajax_get_info = false) {
352
353 $wpo_user_selection = $this->options->get_main_settings();
354 $setting_id = $this->get_setting_id();
355 $dom_id = $this->get_dom_id();
356
357 // N.B. Some of the optimizations used to have an onclick call to fCheck(). But that function was commented out, so did nothing.
358 $settings_label = $this->settings_label();
359
360 $setting_activated = ((empty($wpo_user_selection[$setting_id]) || 'false' == $wpo_user_selection[$setting_id]) ? false : true);
361
362 $info = ($ajax_get_info && $this->support_ajax_get_info) ? '...' : $this->get_optimization_info()->output;
363
364 $settings_html = array(
365 'dom_id' => $dom_id,
366 'activated' => $setting_activated,
367 'settings_label' => $settings_label,
368 'info' => $info,
369 'support_ajax_get_info' => $this->support_ajax_get_info
370 );
371
372 if (empty($settings_label)) {
373 // Error_log, as this is a defect.
374 error_log("Optimization with setting ID ".$setting_id." lacks a settings label (method: settings_label())");
375 }
376
377 return $settings_html;
378 }
379 }
380