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