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

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

192 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) 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'];
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 // translators: %s is a table name
86 $this->register_meta('message', sprintf(__('The table "%s" does not exist.', 'wp-optimize'), $this->data['optimization_table']));
87 return false;
88
89 }
90 } else {
91 $tables = $this->optimizer->get_tables();
92
93 foreach ($tables as $table) {
94 $this->optimize_table($table, $force);
95 }
96 }
97 }
98
99 /**
100 * Optimize table and generate log and output information.
101 *
102 * @param object $table_obj table object returned by $this->optimizer->get_tables().
103 * @param bool $force if true then will optimize
104 */
105 private function optimize_table($table_obj, $force = false) {
106
107 // if not forced and table is not optimizable then exit.
108 if (!$force && (!$table_obj->is_optimizable || !$table_obj->is_type_supported)) return;
109
110 if ($table_obj->is_type_supported) {
111 $this->logger->info('Optimizing: ' . $table_obj->Name);
112 $this->query('OPTIMIZE TABLE `' . $table_obj->Name . '`');
113
114 // For InnoDB Data_free doesn't contain free size.
115 if ('InnoDB' !== $table_obj->Engine) {
116 $this->optimizer->update_total_cleaned(strval($table_obj->Data_free));
117 }
118
119 $this->register_output(__('Optimized table:', 'wp-optimize') . ' ' . $table_obj->Name);
120 }
121 }
122
123 /**
124 * Before get_info() actions.
125 */
126 public function before_get_info() {
127 $this->table_information['total_gain'] = 0;
128 $this->table_information['inno_db_tables'] = 0;
129 $this->table_information['non_inno_db_tables'] = 0;
130 $this->table_information['table_list'] = '';
131 $this->table_information['is_optimizable'] = true;
132 }
133
134 /**
135 * Get information to be displayed onscreen before optimization.
136 */
137 public function get_info() {
138 $table_information = $this->optimizer->get_table_information(get_current_blog_id());
139
140 $this->table_information['total_gain'] += $table_information['total_gain'];
141 $this->table_information['inno_db_tables'] += $table_information['inno_db_tables'];
142 $this->table_information['non_inno_db_tables'] += $table_information['non_inno_db_tables'];
143 $this->table_information['table_list'] .= $table_information['table_list'];
144 if (!$table_information['is_optimizable']) {
145 $this->table_information['is_optimizable'] = false;
146 }
147 }
148
149 /**
150 * Return info about optimization.
151 */
152 public function after_get_info() {
153 // This gathers information to be displayed onscreen before optimization.
154 $tablesstatus = $this->table_information;
155
156 // Check if database is not optimizable.
157 if (false === $tablesstatus['is_optimizable']) {
158 if (isset($this->data['optimization_table']) && '' !== $this->data['optimization_table']) {
159 // This is used for grabbing information before optimizations.
160 $this->register_output(__('Total gain:', 'wp-optimize').' '.WP_Optimize()->format_size(($tablesstatus['total_gain'])));
161 }
162
163 if ($tablesstatus['inno_db_tables'] > 0) {
164 // Output message for how many InnoDB tables will not be optimized.
165 // translators: %d is number of InnoDB tables
166 $this->register_output(sprintf(__('Tables using the InnoDB engine (%d) will not be optimized.', 'wp-optimize'), $tablesstatus['inno_db_tables']));
167
168 if ($tablesstatus['non_inno_db_tables'] > 0) {
169 // translators: %s is number of Non InnoDB tables
170 $this->register_output(sprintf(__('Other tables will be optimized (%s).', 'wp-optimize'), $tablesstatus['non_inno_db_tables']));
171 }
172
173 $faq_url = apply_filters('wpo_faq_url', 'https://teamupdraft.com/documentation/wp-optimize/topics/database-optimization/faqs/');
174 $force_db_option = $this->options->get_option('innodb-force-optimize', 'false');
175 $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>');
176 }
177 } else {
178 // translators: %s is number of tables
179 $this->register_output(sprintf(__('Tables will be optimized (%s).', 'wp-optimize'), $tablesstatus['non_inno_db_tables'] + $tablesstatus['inno_db_tables']));
180 }
181 }
182
183 /**
184 * Return settings label
185 *
186 * @return string
187 */
188 public function settings_label() {
189 return __('Optimize database tables', 'wp-optimize');
190 }
191 }
192