PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 2.2.11
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v2.2.11
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 3.8.0 All 110 releases
wp-optimize / optimizations / orphanedtables.php

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

127 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /**
16 * Display or hide optimization in optimizations list.
17 *
18 * @return bool
19 */
20 public function display_in_optimizations_list() {
21 return false;
22 }
23
24 /**
25 * Run optimization.
26 */
27 public function optimize() {
28 // check if single table name posted or optimize all tables.
29 if (isset($this->data['optimization_table']) && '' != $this->data['optimization_table']) {
30 $table = $this->optimizer->get_table($this->data['optimization_table']);
31
32 $result = (false === $table) ? false : $this->delete_table($table);
33
34 $this->register_meta('success', $result);
35 } else {
36 // delete all orphaned tables if table name was not selected.
37 $tables = $this->optimizer->get_tables();
38 $deleted = 0;
39
40 foreach ($tables as $table) {
41 if ($table->is_using) continue;
42
43 if ($this->delete_table($table)) {
44 $deleted++;
45 }
46 }
47
48 $this->register_output(sprintf(_n('%s orphaned table deleted', '%s orphaned tables deleted', $deleted), $deleted));
49
50 if ($deleted > 0) {
51 $this->register_output(sprintf(_n('Deleting %s orphaned table was unsuccessful', 'Repairing %s orphaned tables were unsuccessful', $deleted), $deleted));
52 }
53 }
54 }
55
56 /**
57 * Drop table from database.
58 *
59 * @param object $table_obj object contains information about database table.
60 *
61 * @return bool
62 */
63 private function delete_table($table_obj) {
64 global $wpdb;
65
66 // don't delete table if it in use.
67 if ($table_obj->is_using) return true;
68
69 $sql_query = $wpdb->prepare('DROP TABLE %s', $table_obj->Name);
70
71 $this->logger->info($sql_query);
72
73 $result = $wpdb->query($sql_query);
74
75 // check if drop query finished successfully.
76 if ('' != $wpdb->last_error) {
77 $this->logger->info($wpdb->last_error);
78 }
79
80 return $result;
81 }
82
83 /**
84 * Get count of unused database tables, i.e. not using by any of installed plugin.
85 *
86 * @return int
87 */
88 public function get_unused_tables_count() {
89 $tablesinfo = $this->optimizer->get_tables();
90
91 $unused_tables = 0;
92
93 if (!empty($tablesinfo)) {
94 foreach ($tablesinfo as $tableinfo) {
95 if (false == $tableinfo->is_using) {
96 $unused_tables++;
97 }
98 }
99 }
100
101 return $unused_tables;
102 }
103
104 /**
105 * Register info about optimization.
106 */
107 public function get_info() {
108
109 $corrupted_tables = $this->get_unused_tables_count();
110
111 if (0 == $corrupted_tables) {
112 $this->register_output(__('No corrupted tables found', 'wp-optimize'));
113 } else {
114 $this->register_output(sprintf(_n('%s corrupted table found', '%s corrupted tables found', $corrupted_tables), $corrupted_tables));
115 }
116 }
117
118 /**
119 * Returns settings label.
120 *
121 * @return string
122 */
123 public function settings_label() {
124 return __('Delete orphaned database tables', 'wp-optimize');
125 }
126 }
127