attachments.php
8 years ago
autodraft.php
7 years ago
commentmeta.php
7 years ago
inactive-tags.php
8 years ago
optimizetables.php
7 years ago
orphandata.php
6 years ago
orphanedtables.php
7 years ago
pingbacks.php
7 years ago
postmeta.php
7 years ago
repairtables.php
7 years ago
revisions.php
7 years ago
spam.php
7 years ago
trackbacks.php
7 years ago
transient.php
7 years ago
trash.php
7 years ago
unapproved.php
7 years ago
orphanedtables.php
134 lines
| 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 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->delete_table($table); |
| 35 | |
| 36 | $this->register_meta('success', $result); |
| 37 | |
| 38 | if (false === $result) { |
| 39 | $this->register_meta('message', $this->last_message); |
| 40 | } |
| 41 | } else { |
| 42 | // delete all orphaned tables if table name was not selected. |
| 43 | $tables = $this->optimizer->get_tables(); |
| 44 | $deleted = 0; |
| 45 | |
| 46 | foreach ($tables as $table) { |
| 47 | if ($table->is_using) continue; |
| 48 | |
| 49 | if ($this->delete_table($table)) { |
| 50 | $deleted++; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | $this->register_output(sprintf(_n('%s orphaned table deleted', '%s orphaned tables deleted', $deleted), $deleted)); |
| 55 | |
| 56 | if ($deleted > 0) { |
| 57 | $this->register_output(sprintf(_n('Deleting %s orphaned table was unsuccessful', 'Repairing %s orphaned tables were unsuccessful', $deleted), $deleted)); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Drop table from database. |
| 64 | * |
| 65 | * @param object $table_obj object contains information about database table. |
| 66 | * |
| 67 | * @return bool |
| 68 | */ |
| 69 | private function delete_table($table_obj) { |
| 70 | global $wpdb; |
| 71 | |
| 72 | // don't delete table if it in use and plugin active. |
| 73 | if (!$table_obj->can_be_removed) return true; |
| 74 | |
| 75 | $sql_query = "DROP TABLE `{$table_obj->Name}`"; |
| 76 | |
| 77 | $this->logger->info($sql_query); |
| 78 | |
| 79 | $result = $wpdb->query($sql_query); |
| 80 | |
| 81 | // check if drop query finished successfully. |
| 82 | if ('' != $wpdb->last_error) { |
| 83 | $this->last_message = $wpdb->last_error; |
| 84 | $this->logger->info($wpdb->last_error); |
| 85 | } |
| 86 | |
| 87 | return $result; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get count of unused database tables, i.e. not using by any of installed plugin. |
| 92 | * |
| 93 | * @return int |
| 94 | */ |
| 95 | public function get_unused_tables_count() { |
| 96 | $tablesinfo = $this->optimizer->get_tables(); |
| 97 | |
| 98 | $unused_tables = 0; |
| 99 | |
| 100 | if (!empty($tablesinfo)) { |
| 101 | foreach ($tablesinfo as $tableinfo) { |
| 102 | if (false == $tableinfo->is_using) { |
| 103 | $unused_tables++; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return $unused_tables; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Register info about optimization. |
| 113 | */ |
| 114 | public function get_info() { |
| 115 | |
| 116 | $corrupted_tables = $this->get_unused_tables_count(); |
| 117 | |
| 118 | if (0 == $corrupted_tables) { |
| 119 | $this->register_output(__('No corrupted tables found', 'wp-optimize')); |
| 120 | } else { |
| 121 | $this->register_output(sprintf(_n('%s corrupted table found', '%s corrupted tables found', $corrupted_tables), $corrupted_tables)); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Returns settings label. |
| 127 | * |
| 128 | * @return string |
| 129 | */ |
| 130 | public function settings_label() { |
| 131 | return __('Delete orphaned database tables', 'wp-optimize'); |
| 132 | } |
| 133 | } |
| 134 |