PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.19
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.19
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 / optimizations / optimizetables.php

optimizetables.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.19, at optimizations/optimizetables.php

187 lines 6.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 class WP_Optimization_optimizetables extends WP_Optimization {
6
7 protected $auto_id = 'optimize';
8
9 protected $setting_id = 'optimize';
10
11 protected $dom_id = 'optimize-db';
12
13 public $available_for_saving = true;
14
15 public $available_for_auto = true;
16
17 public $setting_default = true;
18
19 public $changes_table_data = true;
20
21 public $ui_sort_order = 500;
22
23 public $run_sort_order = 100000;
24
25 public $run_multisite = true;
26
27 public $support_preview = false;
28
29 private $table_information = array();
30
31 /**
32 * Optimize method.
33 */
34 public function optimize() {
35 // We don't need run anything in this method to avoid issues on multisite installations.
36 }
37
38 /**
39 * Run optimization.
40 */
41 public function after_optimize() {
42 // check if force optimize sent.
43 $force = (isset($this->data['optimization_force']) && $this->data['optimization_force']) ? true : false;
44
45 // check if single table name posted or optimize all tables.
46 if (isset($this->data['optimization_table']) && '' != $this->data['optimization_table']) {
47 $table = $this->optimizer->get_table($this->data['optimization_table']);
48
49 if (false !== $table) {
50 $this->optimize_table($table, $force);
51
52 // Exit if the UI elements aren't required
53 if (isset($this->data['include_ui_elements']) && !$this->data['include_ui_elements']) return;
54
55 $wp_optimize = WP_Optimize();
56 $tablestatus = $wp_optimize->get_db_info()->get_table_status($table->Name, true);
57
58 $is_optimizable = $wp_optimize->get_db_info()->is_table_optimizable($table->Name);
59
60 $tableinfo = array(
61 'rows' => number_format_i18n($tablestatus->Rows),
62 'data_size' => $wp_optimize->format_size($tablestatus->Data_length),
63 'index_size' => $wp_optimize->format_size($tablestatus->Index_length),
64 'overhead' => $is_optimizable ? $wp_optimize->format_size($tablestatus->Data_free) : '-',
65 'type' => $table->Engine,
66 'is_optimizable' => $is_optimizable,
67 );
68
69 $this->register_meta('tableinfo', $tableinfo);
70
71 $tables = $this->optimizer->get_tables(true);
72
73 $overhead_usage = 0;
74
75 foreach ($tables as $table) {
76 if ($table->is_optimizable) {
77 $overhead_usage += $table->Data_free;
78 }
79 }
80
81 $this->register_meta('overhead', $overhead_usage);
82 $this->register_meta('overhead_formatted', $wp_optimize->format_size($overhead_usage));
83 } else {
84 $this->register_meta('error', 1);
85 $this->register_meta('message', sprintf(__('The table "%s" does not exist.', 'wp-optimize'), $this->data['optimization_table']));
86 return false;
87
88 }
89 } else {
90 $tables = $this->optimizer->get_tables();
91
92 foreach ($tables as $table) {
93 $this->optimize_table($table, $force);
94 }
95 }
96 }
97
98 /**
99 * Optimize table and generate log and output information.
100 *
101 * @param object $table_obj table object returned by $this->optimizer->get_tables().
102 * @param bool $force if true then will optimize
103 */
104 private function optimize_table($table_obj, $force = false) {
105
106 // if not forced and table is not optimizable then exit.
107 if (false == $force && (false == $table_obj->is_optimizable || false == $table_obj->is_type_supported)) return;
108
109 if ($table_obj->is_type_supported) {
110 $this->logger->info('Optimizing: ' . $table_obj->Name);
111 $this->query('OPTIMIZE TABLE `' . $table_obj->Name . '`');
112
113 // For InnoDB Data_free doesn't contain free size.
114 if ('InnoDB' != $table_obj->Engine) {
115 $this->optimizer->update_total_cleaned(strval($table_obj->Data_free));
116 }
117
118 $this->register_output(__('Optimized table:', 'wp-optimize') . ' ' . $table_obj->Name);
119 }
120 }
121
122 /**
123 * Before get_info() actions.
124 */
125 public function before_get_info() {
126 $this->table_information['total_gain'] = 0;
127 $this->table_information['inno_db_tables'] = 0;
128 $this->table_information['non_inno_db_tables'] = 0;
129 $this->table_information['table_list'] = '';
130 $this->table_information['is_optimizable'] = true;
131 }
132
133 /**
134 * Get information to be disbalyed onscreen before optimization.
135 */
136 public function get_info() {
137 $table_information = $this->optimizer->get_table_information(get_current_blog_id());
138
139 $this->table_information['total_gain'] += $table_information['total_gain'];
140 $this->table_information['inno_db_tables'] += $table_information['inno_db_tables'];
141 $this->table_information['non_inno_db_tables'] += $table_information['non_inno_db_tables'];
142 $this->table_information['table_list'] .= $table_information['table_list'];
143 if (!$table_information['is_optimizable']) {
144 $this->table_information['is_optimizable'] = false;
145 }
146 }
147
148 /**
149 * Return info about optimization.
150 */
151 public function after_get_info() {
152 // This gathers information to be displayed onscreen before optimization.
153 $tablesstatus = $this->table_information;
154
155 // Check if database is not optimizable.
156 if (false === $tablesstatus['is_optimizable']) {
157 if (isset($this->data['optimization_table']) && '' != $this->data['optimization_table']) {
158 // This is used for grabbing information before optimizations.
159 $this->register_output(__('Total gain:', 'wp-optimize').' '.WP_Optimize()->format_size(($tablesstatus['total_gain'])));
160 }
161
162 if ($tablesstatus['inno_db_tables'] > 0) {
163 // Output message for how many InnoDB tables will not be optimized.
164 $this->register_output(sprintf(__('Tables using the InnoDB engine (%d) will not be optimized.'), $tablesstatus['inno_db_tables']));
165
166 if ($tablesstatus['non_inno_db_tables'] > 0) {
167 $this->register_output(sprintf(__('Other tables will be optimized (%s).', 'wp-optimize'), $tablesstatus['non_inno_db_tables']));
168 }
169
170 $faq_url = apply_filters('wpo_faq_url', 'https://getwpo.com/faqs/');
171 $force_db_option = $this->options->get_option('innodb-force-optimize', 'false');
172 $this->register_output('<input id="innodb_force_optimize" name="innodb-force-optimize" type="checkbox" value="true" '.checked($force_db_option, 'true').'><label for="innodb_force_optimize">'.__('Optimize InnoDB tables anyway.', 'wp-optimize').'</label><br><a href="'.$faq_url.'" target="_blank">'.__('Warning: you should read the FAQ on the risks of this operation first.', 'wp-optimize').'</a>');
173 }
174 } else {
175 $this->register_output(sprintf(__('Tables will be optimized (%s).', 'wp-optimize'), $tablesstatus['non_inno_db_tables'] + $tablesstatus['inno_db_tables']));
176 }
177 }
178
179 public function get_auto_option_description() {
180 return __('Optimize database tables', 'wp-optimize');
181 }
182
183 public function settings_label() {
184 return __('Optimize database tables', 'wp-optimize');
185 }
186 }
187