PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 2.2.11
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v2.2.11
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-optimizer.php

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

506 lines 16.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 * This class invokes optimiazations. The optimizations themselves live in the 'optimizations' sub-directory of the plugin. The proper way to obtain access to the instance is via WP_Optimize()->get_optimizer()
7 */
8 class WP_Optimizer {
9
10 public function get_retain_info() {
11
12 $options = WP_Optimize()->get_options();
13
14 $retain_enabled = $options->get_option('retention-enabled', 'false');
15 $retain_period = (($retain_enabled) ? $options->get_option('retention-period', '2') : null);
16
17 return array($retain_enabled, $retain_period);
18 }
19
20 public function get_optimizations_list() {
21
22 $optimizations = array();
23
24 $optimizations_dir = WPO_PLUGIN_MAIN_PATH.'/optimizations';
25
26 if ($dh = opendir($optimizations_dir)) {
27 while (($file = readdir($dh)) !== false) {
28 if ('.' == $file || '..' == $file || '.php' != substr($file, -4, 4) || !is_file($optimizations_dir.'/'.$file) || 'inactive-' == substr($file, 0, 9)) continue;
29 $optimizations[] = substr($file, 0, (strlen($file) - 4));
30 }
31 closedir($dh);
32 }
33
34 return apply_filters('wp_optimize_get_optimizations_list', $optimizations);
35
36 }
37
38 /**
39 * Currently, there is only one sort rule (so, the parameter's value is ignored)
40 *
41 * @param array $optimizations An array of optimizations (i.e. WP_Optimization instances).
42 * @param string $sort_on Specify sort.
43 * @param string $sort_rule Sort Rule.
44 * @return array
45 */
46 public function sort_optimizations($optimizations, $sort_on = 'ui_sort_order', $sort_rule = 'traditional') {
47 if ('run_sort_order' == $sort_on) {
48 uasort($optimizations, array($this, 'sort_optimizations_run_traditional'));
49 } else {
50 uasort($optimizations, array($this, 'sort_optimizations_ui_traditional'));
51 }
52 return $optimizations;
53 }
54
55 public function sort_optimizations_ui_traditional($a, $b) {
56 return $this->sort_optimizations_traditional($a, $b, 'ui_sort_order');
57 }
58
59 public function sort_optimizations_run_traditional($a, $b) {
60 return $this->sort_optimizations_traditional($a, $b, 'run_sort_order');
61 }
62
63 public function sort_optimizations_traditional($a, $b, $sort_on = 'ui_sort_order') {
64
65 if (!is_a($a, 'WP_Optimization')) return (!is_a($b, 'WP_Optimization')) ? 0 : 1;
66 if (!is_a($b, 'WP_Optimization')) return -1;
67
68 $sort_order_a = empty($a->$sort_on) ? 0 : $a->$sort_on;
69 $sort_order_b = empty($b->$sort_on) ? 0 : $b->$sort_on;
70
71 if ($sort_order_a == $sort_order_b) return 0;
72
73 return ($sort_order_a < $sort_order_b) ? (-1) : 1;
74
75 }
76
77 /**
78 * This method returns an array of available optimizations.
79 * Each array key is an optimization ID, and the value is an object,
80 * as returned by get_optimization()
81 *
82 * @return [array] array of optimizations
83 */
84 public function get_optimizations() {
85
86 $optimizations = $this->get_optimizations_list();
87
88 $optimization_objects = array();
89
90 foreach ($optimizations as $optimization) {
91 $optimization_objects[$optimization] = $this->get_optimization($optimization);
92 }
93
94 return apply_filters('wp_optimize_get_optimizations', $optimization_objects);
95
96 }
97
98 /**
99 * This method returns an object for a specific optimization.
100 *
101 * @param string $which_optimization An optimization ID.
102 * @param array $data An array of anny options $data.
103 * @return array WP_Error Will return the optimization, or a WP_Error object if it was not found.
104 */
105 public function get_optimization($which_optimization, $data = array()) {
106
107 $optimization_class = apply_filters('wp_optimize_optimization_class', 'WP_Optimization_'.$which_optimization);
108
109 if (!class_exists('WP_Optimization')) include_once(WPO_PLUGIN_MAIN_PATH.'/includes/class-wp-optimization.php');
110
111 if (!class_exists($optimization_class)) {
112 $optimization_file = WPO_PLUGIN_MAIN_PATH.'/optimizations/'.$which_optimization.'.php';
113 $class_file = apply_filters('wp_optimize_optimization_class_file', $optimization_file);
114 if (!preg_match('/^[a-z]+$/', $which_optimization) || !file_exists($class_file)) {
115 return new WP_Error('no_such_optimization', __('No such optimization', 'wp-optimize'), $which_optimization);
116 }
117
118 include_once($class_file);
119
120 if (!class_exists($optimization_class)) {
121 return new WP_Error('no_such_optimization', __('No such optimization', 'wp-optimize'), $which_optimization);
122 }
123 }
124
125 // set sites option for Multisite cron job.
126 if (defined('DOING_CRON') && DOING_CRON && is_multisite()) {
127 $options = WP_Optimize()->get_options();
128 $data['optimization_sites'] = $options->get_option('wpo-sites-cron', array('all'));
129 }
130
131 $optimization = new $optimization_class($data);
132
133 return $optimization;
134
135 }
136
137 /**
138 * The method to call to perform an optimization.
139 *
140 * @param string|object $which_optimization An optimization ID, or a WP_Optimization object.
141 * @return array Array of results from the optimization.
142 */
143 public function do_optimization($which_optimization) {
144
145 $optimization = (is_object($which_optimization) && is_a($which_optimization, 'WP_Optimization')) ? $which_optimization : $this->get_optimization($which_optimization);
146
147 if (is_wp_error($optimization)) {
148 WP_Optimize()->log('Error occurred. Unknown optimization.');
149 return $optimization;
150 }
151
152 $this->change_time_limit();
153
154 $optimization->init();
155
156 if (apply_filters('wp_optimize_do_optimization', true, $which_optimization, $optimization)) {
157
158 $optimization->before_optimize();
159
160 if ($optimization->run_multisite) {
161 foreach ($optimization->blogs_ids as $blog_id) {
162 $optimization->switch_to_blog($blog_id);
163 $optimization->optimize();
164 $optimization->restore_current_blog();
165 }
166 } else {
167 $optimization->optimize();
168 }
169
170 $optimization->after_optimize();
171
172 }
173
174 do_action('wp_optimize_after_optimization', $which_optimization, $optimization);
175
176 $results = $optimization->get_results();
177
178 return $results;
179 }
180
181 /**
182 * The method to call to get information about an optimization.
183 * As with do_optimization, it is somewhat modelled after the template interface
184 *
185 * @param string|object $which_optimization An optimization ID, or a WP_Optimization object.
186 * @return array returns the optimization information
187 */
188 public function get_optimization_info($which_optimization) {
189
190 $optimization = (is_object($which_optimization) && is_a($which_optimization, 'WP_Optimization')) ? $which_optimization : $this->get_optimization($which_optimization);
191
192 if (is_wp_error($optimization)) return $optimization;
193
194 $this->change_time_limit();
195
196 $optimization->before_get_info();
197
198 if ($optimization->run_multisite) {
199 foreach ($optimization->blogs_ids as $blog_id) {
200 $optimization->switch_to_blog($blog_id);
201 $optimization->get_info();
202 $optimization->restore_current_blog();
203 }
204 } else {
205 $optimization->get_info();
206 }
207
208 $optimization->after_get_info();
209
210 return $optimization->get_results();
211 }
212
213 /**
214 * THis runs the list of optimizations.
215 *
216 * @param array $optimization_options Whether to do an optimization depends on what keys are set (legacy - can be changed hopefully).
217 * @param string $which_option Specify which option.
218 * @return array Returns an array of result objects.
219 */
220 public function do_optimizations($optimization_options, $which_option = 'dom') {
221
222 $results = array();
223
224 if (empty($optimization_options)) return $results;
225
226 $optimizations = $this->sort_optimizations($this->get_optimizations(), 'run_sort_order');
227
228 foreach ($optimizations as $optimization_id => $optimization) {
229 $option_id = call_user_func(array($optimization, 'get_'.$which_option.'_id'));
230
231 if (isset($optimization_options[$option_id])) {
232 // if options saved as a string then compare with string (for support different versions)
233 if (is_string($optimization_options[$option_id]) && 'false' === $optimization_options[$option_id]) continue;
234
235 if ('auto' == $which_option && empty($optimization->available_for_auto)) continue;
236
237 $this->change_time_limit();
238
239 $results[$optimization_id] = $this->do_optimization($optimization);
240 }
241 }
242
243 // Run action after all optimizations completed.
244 do_action('wp_optimize_after_optimizations');
245
246 return $results;
247
248 }
249
250 public function get_table_prefix($allow_override = false) {
251 $wpdb = $GLOBALS['wpdb'];
252 if (is_multisite() && !defined('MULTISITE')) {
253 // In this case (which should only be possible on installs upgraded from pre WP 3.0 WPMU), $wpdb->get_blog_prefix() cannot be made to return the right thing. $wpdb->base_prefix is not explicitly marked as public, so we prefer to use get_blog_prefix if we can, for future compatibility.
254 $prefix = $wpdb->base_prefix;
255 } else {
256 $prefix = $wpdb->get_blog_prefix(0);
257 }
258 return ($allow_override) ? apply_filters('wp_optimize_get_table_prefix', $prefix) : $prefix;
259 }
260
261 /**
262 * Returns information about database tables.
263 *
264 * @return mixed
265 */
266 public function get_tables() {
267 static $tables_info = null;
268
269 if (null !== $tables_info) return $tables_info;
270
271 $table_status = WP_Optimize()->get_db_info()->get_show_table_status();
272
273 // Filter on the site's DB prefix (was not done in releases up to 1.9.1).
274 $table_prefix = $this->get_table_prefix();
275
276 if (is_array($table_status)) {
277 foreach ($table_status as $index => $table) {
278 $table_name = $table->Name;
279
280 $include_table = (0 === stripos($table_name, $table_prefix));
281
282 $include_table = apply_filters('wp_optimize_get_tables_include_table', $include_table, $table_name, $table_prefix);
283
284 if (!$include_table) {
285 unset($table_status[$index]);
286 continue;
287 }
288
289 $table_status[$index]->Engine = WP_Optimize()->get_db_info()->get_table_type($table_name);
290
291 $table_status[$index]->is_optimizable = WP_Optimize()->get_db_info()->is_table_optimizable($table_name);
292 $table_status[$index]->is_type_supported = WP_Optimize()->get_db_info()->is_table_type_optimize_supported($table_name);
293 // add information about corrupted tables.
294 $table_status[$index]->is_needing_repair = WP_Optimize()->get_db_info()->is_table_needing_repair($table_name);
295 // add information about using table by any of installed plugins.
296 $table_status[$index]->plugin = WP_Optimize()->get_db_info()->get_table_plugin($table_name);
297 $table_status[$index]->is_using = WP_Optimize()->get_db_info()->is_table_using_by_plugin($table_name);
298 // if table belongs to any plugin then add plugins status.
299 if ($table_status[$index]->plugin) {
300 $table_status[$index]->plugin_status = WP_Optimize()->get_db_info()->get_plugin_status($table_status[$index]->plugin);
301 }
302 }
303 }
304
305 $tables_info = apply_filters('wp_optimize_get_tables', $table_status);
306 return $tables_info;
307 }
308
309 /**
310 * Returns information about single table by table name.
311 *
312 * @param String $table_name table name
313 * @return Object|Boolean table information object.
314 */
315 public function get_table($table_name) {
316
317 $db_info = WP_Optimize()->get_db_info();
318
319 $table = $db_info->get_table_status($table_name);
320
321 if (false === $table) return false;
322
323 $table->is_optimizable = $db_info->is_table_optimizable($table_name);
324 $table->is_type_supported = $db_info->is_table_type_optimize_supported($table_name);
325 $table->is_needing_repair = $db_info->is_table_needing_repair($table_name);
326
327 // add information about using table by any of installed plugins.
328 $table->plugin = $db_info->get_table_plugin($table_name);
329 $table->is_using = $db_info->is_table_using_by_plugin($table_name);
330 // if table belongs to any plugin then add plugins status.
331 if ($table->plugin) {
332 $table->plugin_status = $db_info->get_plugin_status($table->plugin);
333 }
334
335 $table = apply_filters('wp_optimize_get_table', $table);
336 return $table;
337 }
338
339 /**
340 * This function grabs a list of tables
341 * and information regarding each table and returns
342 * the results to optimizations-table.php and optimizationstable.php
343 *
344 * @return Array - an array of data such as table list, innodb info and data free
345 */
346 public function get_table_information() {
347 // Get table information.
348 $tablesstatus = $this->get_tables();
349
350 // Set defaults.
351 $table_information = array();
352 $table_information['total_gain'] = 0;
353 $table_information['inno_db_tables'] = 0;
354 $table_information['non_inno_db_tables'] = 0;
355 $table_information['table_list'] = '';
356 $table_information['is_optimizable'] = true;
357
358 // Make a list of tables to optimize.
359 foreach ($tablesstatus as $each_table) {
360 $table_information['table_list'] .= $each_table->Name.'|';
361
362 // check if table type supported.
363 if (!$each_table->is_type_supported) continue;
364
365 // check if table is optimizable.
366 if (!$each_table->is_optimizable) {
367 $table_information['is_optimizable'] = false;
368 }
369
370 // calculate total gain value.
371 $table_information['total_gain'] += $each_table->Data_free;
372
373 // count InnoDB tables.
374 if ('InnoDB' == $each_table->Engine) {
375 $table_information['inno_db_tables']++;
376 } else {
377 $table_information['non_inno_db_tables']++;
378 }
379 }
380 return $table_information;
381 }
382
383 /**
384 * What sort of linkback to enable or disable: valid values are 'trackbacks' or 'comments', and whether to enable or disable.
385 *
386 * @param string $type Specify the type of linkbacks.
387 * @param boolean $enable If it is enabled or disabled.
388 */
389 public function enable_linkbacks($type, $enable = true) {
390
391 $wpdb = $GLOBALS['wpdb'];
392
393 $new_status = $enable ? 'open' : 'closed';
394
395 switch ($type) {
396 case "trackbacks":
397 $thissql = "UPDATE `".$wpdb->posts."` SET ping_status='".$new_status."' WHERE post_status = 'publish' AND post_type = 'post';";
398 $trackbacks = $wpdb->query($thissql);
399 break;
400
401 case "comments":
402 $thissql = "UPDATE `".$wpdb->posts."` SET comment_status='".$new_status."' WHERE post_status = 'publish' AND post_type = 'post';";
403 $comments = $wpdb->query($thissql);
404 break;
405
406 default:
407 break;
408 }
409
410 }
411
412 /**
413 * This function will return total database size and a possible gain of db in KB.
414 *
415 * @return string total db size gained.
416 */
417 public function get_current_db_size() {
418
419 $wp_optimize = WP_Optimize();
420
421 $wpdb = $GLOBALS['wpdb'];
422 $total_gain = 0;
423 $total_size = 0;
424 $no = 0;
425 $row_usage = 0;
426 $data_usage = 0;
427 $index_usage = 0;
428 $overhead_usage = 0;
429
430 $tablesstatus = $this->get_tables();
431
432 foreach ($tablesstatus as $tablestatus) {
433 $row_usage += $tablestatus->Rows;
434 $data_usage += $tablestatus->Data_length;
435 $index_usage += $tablestatus->Index_length;
436
437 if ('InnoDB' != $tablestatus->Engine) {
438 $overhead_usage += $tablestatus->Data_free;
439 $total_gain += $tablestatus->Data_free;
440 }
441 }
442
443 $total_size = ($data_usage + $index_usage);
444 return array($wp_optimize->format_size($total_size), $wp_optimize->format_size($total_gain));
445 }
446
447 /**
448 * This function will return total saved data in KB.
449 *
450 * @param string $current How big the data is.
451 * @return string Returns new total value.
452 */
453 public function update_total_cleaned($current) {
454
455 $options = WP_Optimize()->get_options();
456
457 $previously_saved = floatval($options->get_option('total-cleaned', '0'));
458 $converted_current = floatval($current);
459
460 $total_now = strval($previously_saved + $converted_current);
461
462 $options->update_option('total-cleaned', $total_now);
463
464 return $total_now;
465 }
466
467
468 public function trackback_comment_actions($options) {
469
470 $output = array();
471
472 if (isset($options['comments'])) {
473 if (!$options['comments']) {
474 $this->enable_linkbacks('comments', false);
475 $output[] = __('Comments have now been disabled on all current and previously published posts.', 'wp-optimize');
476 } else {
477 $this->enable_linkbacks('comments');
478 $output[] = __('Comments have now been enabled on all current and previously published posts.', 'wp-optimize');
479 }
480 }
481
482 if (isset($options['trackbacks'])) {
483 if (!$options['trackbacks']) {
484 $this->enable_linkbacks('trackbacks', false);
485 $output[] = __('Trackbacks have now been disabled on all current and previously published posts.', 'wp-optimize');
486 } else {
487 $this->enable_linkbacks('trackbacks');
488 $output[] = __('Trackbacks have now been enabled on all current and previously published posts.', 'wp-optimize');
489 }
490 }
491
492 return array('output' => $output);
493 }
494
495 /**
496 * Try to change PHP script time limit.
497 */
498 private function change_time_limit() {
499 $time_limit = (defined('WP_OPTIMIZE_SET_TIME_LIMIT') && WP_OPTIMIZE_SET_TIME_LIMIT > 15) ? WP_OPTIMIZE_SET_TIME_LIMIT : 1800;
500
501 // Try to reduce the chances of PHP self-terminating via reaching max_execution_time.
502 // @codingStandardsIgnoreLine
503 @set_time_limit($time_limit);
504 }
505 }
506