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

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

165 lines 4.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('ABSPATH')) die('No direct access allowed');
4
5 class WP_Optimization_repairtables extends WP_Optimization {
6
7 public $available_for_auto = false;
8
9 public $setting_default = true;
10
11 public $changes_table_data = true;
12
13 public $run_multisite = false;
14
15 public $support_preview = false;
16
17 /**
18 * Display or hide optimization in optimizations list.
19 *
20 * @return bool
21 */
22 public function display_in_optimizations_list() {
23 return false;
24 }
25
26 /**
27 * Run optimization.
28 */
29 public function optimize() {
30 // check if single table name posted or optimize all tables.
31 if (isset($this->data['optimization_table']) && '' !== $this->data['optimization_table']) {
32 $table = $this->optimizer->get_table($this->data['optimization_table']);
33
34 $result = (false === $table) ? false : $this->repair_table($table);
35
36 if ($result) {
37 $wp_optimize = WP_Optimize();
38
39 $tablestatus = $wp_optimize->get_db_info()->get_table_status($table->Name, true);
40
41 $is_optimizable = $wp_optimize->get_db_info()->is_table_optimizable($table->Name);
42 $is_type_supported = $wp_optimize->get_db_info()->is_table_type_optimize_supported($table->Name);
43
44 $tableinfo = array(
45 'rows' => number_format_i18n($tablestatus->Rows),
46 'data_size' => $wp_optimize->format_size($tablestatus->Data_length),
47 'index_size' => $wp_optimize->format_size($tablestatus->Index_length),
48 'overhead' => $is_optimizable ? $wp_optimize->format_size($tablestatus->Data_free) : '-',
49 'type' => $tablestatus->Engine,
50 'is_optimizable' => $is_optimizable,
51 'is_type_supported' => $is_type_supported,
52 );
53
54 $this->register_meta('tableinfo', $tableinfo);
55 }
56
57 $this->register_meta('success', $result);
58 } else {
59 $tables = $this->optimizer->get_tables();
60 $repaired = $corrupted = 0;
61
62 foreach ($tables as $table) {
63 if (!$table->is_needing_repair) continue;
64
65 if ($this->repair_table($table)) {
66 $repaired++;
67 } else {
68 $corrupted++;
69 }
70 }
71
72 // translators: %s is number of tables repaired.
73 $this->register_output(sprintf(_n('%s table repaired', '%s tables repaired', $repaired, 'wp-optimize'), $repaired));
74
75 if ($corrupted > 0) {
76 // translators: %s is number of tables
77 $this->register_output(sprintf(_n('Repairing %s table was unsuccessful', 'Repairing %s tables were unsuccessful', $corrupted, 'wp-optimize'), $corrupted));
78 }
79 }
80 }
81
82 /**
83 * Repair table.
84 *
85 * @param object $table_obj object contains information about database table.
86 *
87 * @return bool
88 */
89 private function repair_table($table_obj) {
90 global $wpdb;
91
92 $success = false;
93
94 if (!$table_obj->is_needing_repair) return true;
95
96 $this->logger->info('REPAIR TABLE `'.$table_obj->Name. '`');
97
98 $results = $wpdb->get_results('REPAIR TABLE `'. esc_sql($table_obj->Name) . '`');
99
100 if (!empty($results)) {
101 foreach ($results as $row) {
102 if ('status' === strtolower($row->Msg_type) && 'ok' === strtolower($row->Msg_text)) {
103 $success = true;
104 }
105
106 $this->logger->info($row->Msg_text);
107 }
108 }
109
110 // update info in options table and use it to notify user about corrupted tables.
111 $this->get_corrupted_tables_count();
112
113 return $success;
114 }
115
116 /**
117 * Returns count of corrupted tables and update corrupted-tables-count value in options used to show
118 * information for user in sidebar about corrupted tables count.
119 *
120 * @return int
121 */
122 public function get_corrupted_tables_count() {
123 $tablesinfo = $this->optimizer->get_tables();
124
125 $corrupted_tables = 0;
126
127 if (!empty($tablesinfo)) {
128 foreach ($tablesinfo as $tableinfo) {
129 if ($tableinfo->is_needing_repair) {
130 $corrupted_tables++;
131 }
132 }
133 }
134
135 // save results to options table and use it to notify user about corrupted tables.
136 $this->options->update_option('corrupted-tables-count', $corrupted_tables);
137
138 return $corrupted_tables;
139 }
140
141 /**
142 * Register info about optimization.
143 */
144 public function get_info() {
145
146 $corrupted_tables = $this->get_corrupted_tables_count();
147
148 if (0 === $corrupted_tables) {
149 $this->register_output(__('No corrupted tables found', 'wp-optimize'));
150 } else {
151 // translators: %s is number of corrupted tables
152 $this->register_output(sprintf(_n('%s corrupted table found', '%s corrupted tables found', $corrupted_tables, 'wp-optimize'), $corrupted_tables));
153 }
154 }
155
156 /**
157 * Returns settings label.
158 *
159 * @return string
160 */
161 public function settings_label() {
162 return __('Repair database tables', 'wp-optimize');
163 }
164 }
165