PluginProbe ʕ •ᴥ•ʔ
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.0.5
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.0.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 3.8.0 4.0.0 4.0.1 4.1.0 4.1.1 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 3.2.2 trunk 0.7.0 1.8.9.10 1.8.9.7 1.8.9.8 1.8.9.9 1.9 1.9.1 2.0.1 2.1.0 2.1.1 2.2.0 2.2.1 2.2.10 2.2.11 2.2.12 2.2.13 2.2.2 2.2.3 2.2.4 2.2.6 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 3.0.0 3.0.1 3.0.10 3.0.11 3.0.12 3.0.13 3.0.14 3.0.15 3.0.16 3.0.18 3.0.19 3.0.2 3.0.3 3.0.4 3.0.5 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 3.1.11 3.1.12 3.1.2 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.14 3.2.15 3.2.16 3.2.17 3.2.18 3.2.19
wp-optimize / optimizations / orphanedtables.php
wp-optimize / optimizations Last commit date
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