| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 4 |
|
| 5 |
class WP_Optimization_orphanedtables 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 |
private $last_message; |
| 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 the data contain action attribute then lead to innoDb conversion. |
| 31 |
if (isset($this->data['optimization_action']) && 'toinnodb' === $this->data['optimization_action']) { |
| 32 |
$table = $this->optimizer->get_table($this->data['optimization_table']); |
| 33 |
if (false === $table) { |
| 34 |
$this->register_meta('error', 1); |
| 35 |
// translators: %s is the table name |
| 36 |
$this->register_meta('message', sprintf(__('The table "%s" does not exist.', 'wp-optimize'), $this->data['optimization_table'])); |
| 37 |
return false; |
| 38 |
} |
| 39 |
$result = $this->convert_table($table); |
| 40 |
|
| 41 |
if (false === $result) { |
| 42 |
$this->register_meta('message', $this->last_message); |
| 43 |
return false; |
| 44 |
} |
| 45 |
$this->register_meta('success', 1); |
| 46 |
return true; |
| 47 |
} |
| 48 |
|
| 49 |
// check if single table name posted or optimize all tables. |
| 50 |
if (isset($this->data['optimization_table']) && '' !== $this->data['optimization_table']) { |
| 51 |
$table = $this->optimizer->get_table($this->data['optimization_table']); |
| 52 |
|
| 53 |
if (false === $table) { |
| 54 |
$this->register_meta('error', 1); |
| 55 |
// translators: %s is the table name |
| 56 |
$this->register_meta('message', sprintf(__('The table "%s" does not exist.', 'wp-optimize'), $this->data['optimization_table'])); |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
$result = $this->delete_table($table); |
| 61 |
|
| 62 |
if (false === $result) { |
| 63 |
$this->register_meta('message', $this->last_message); |
| 64 |
return false; |
| 65 |
} |
| 66 |
$this->register_meta('success', 1); |
| 67 |
} else { |
| 68 |
// delete all orphaned tables if table name was not selected. |
| 69 |
$tables = $this->optimizer->get_tables(); |
| 70 |
$deleted = 0; |
| 71 |
|
| 72 |
foreach ($tables as $table) { |
| 73 |
if ($table->is_using) continue; |
| 74 |
|
| 75 |
if ($this->delete_table($table)) { |
| 76 |
$deleted++; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
// translators: %s is the number of deleted orphaned tables |
| 81 |
$this->register_output(sprintf(_n('%s orphaned table deleted', '%s orphaned tables deleted', $deleted, 'wp-optimize'), $deleted)); |
| 82 |
|
| 83 |
if ($deleted > 0) { |
| 84 |
// translators: %s is the number of tables |
| 85 |
$this->register_output(sprintf(_n('Deleting %s orphaned table was unsuccessful', 'Repairing %s orphaned tables were unsuccessful', $deleted, 'wp-optimize'), $deleted)); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
/** |
| 92 |
* Alter table engine to InnoDB. |
| 93 |
* |
| 94 |
* @param object $table_obj object contains information about database table. |
| 95 |
* |
| 96 |
* @return bool |
| 97 |
*/ |
| 98 |
private function convert_table($table_obj) { |
| 99 |
global $wpdb; |
| 100 |
$inno_db = 0; |
| 101 |
|
| 102 |
// check InnoDB is Active |
| 103 |
$mysql_engine = $wpdb->get_results('SHOW ENGINES'); |
| 104 |
foreach ($mysql_engine as $check) { |
| 105 |
if ('InnoDB' === $check->Engine && ('DEFAULT' === $check->Support || 'YES' === $check->Support)) { |
| 106 |
$inno_db=1; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
if (0 === $inno_db) return false; |
| 112 |
// If InnoDB is active then convert MyISAM to InnoDB. |
| 113 |
else { |
| 114 |
$table_name = esc_sql($table_obj->Name); |
| 115 |
$sql_query = "ALTER TABLE `{$table_name}` ENGINE=InnoDB"; |
| 116 |
$this->logger->info($sql_query); |
| 117 |
$result = $wpdb->query($sql_query); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Statement is safe, not using user input |
| 118 |
} |
| 119 |
// check if alter query finished successfully. |
| 120 |
if ('' !== $wpdb->last_error) { |
| 121 |
$this->last_message = $wpdb->last_error; |
| 122 |
$this->logger->info($wpdb->last_error); |
| 123 |
} |
| 124 |
|
| 125 |
return (bool) $result; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Drop table from database. |
| 130 |
* |
| 131 |
* @param object $table_obj object contains information about database table. |
| 132 |
* |
| 133 |
* @return bool |
| 134 |
*/ |
| 135 |
private function delete_table($table_obj) { |
| 136 |
global $wpdb; |
| 137 |
|
| 138 |
// don't delete table if it in use and plugin active. |
| 139 |
if (!$table_obj->can_be_removed) return true; |
| 140 |
|
| 141 |
$table_name = esc_sql($table_obj->Name); |
| 142 |
$sql_query = "DROP TABLE `{$table_name}`"; |
| 143 |
|
| 144 |
$this->logger->info($sql_query); |
| 145 |
|
| 146 |
$result = $wpdb->query($sql_query); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Statement is safe, not using user input |
| 147 |
|
| 148 |
// check if drop query finished successfully. |
| 149 |
if ('' !== $wpdb->last_error) { |
| 150 |
$this->last_message = $wpdb->last_error; |
| 151 |
$this->logger->info($wpdb->last_error); |
| 152 |
} |
| 153 |
|
| 154 |
return (bool) $result; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Get count of unused database tables, i.e. not using by any of installed plugin. |
| 159 |
* |
| 160 |
* @return int |
| 161 |
*/ |
| 162 |
public function get_unused_tables_count() { |
| 163 |
$tablesinfo = $this->optimizer->get_tables(); |
| 164 |
|
| 165 |
$unused_tables = 0; |
| 166 |
|
| 167 |
if (!empty($tablesinfo)) { |
| 168 |
foreach ($tablesinfo as $tableinfo) { |
| 169 |
if (!$tableinfo->is_using) { |
| 170 |
$unused_tables++; |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
return $unused_tables; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Register info about optimization. |
| 180 |
*/ |
| 181 |
public function get_info() { |
| 182 |
|
| 183 |
$corrupted_tables = $this->get_unused_tables_count(); |
| 184 |
|
| 185 |
if (0 === $corrupted_tables) { |
| 186 |
$this->register_output(__('No corrupted tables found', 'wp-optimize')); |
| 187 |
} else { |
| 188 |
// translators: %s is the number of corrupted tables |
| 189 |
$this->register_output(sprintf(_n('%s corrupted table found', '%s corrupted tables found', $corrupted_tables, 'wp-optimize'), $corrupted_tables)); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Returns settings label. |
| 195 |
* |
| 196 |
* @return string |
| 197 |
*/ |
| 198 |
public function settings_label() { |
| 199 |
return __('Delete orphaned database tables', 'wp-optimize'); |
| 200 |
} |
| 201 |
} |
| 202 |
|