PluginProbe
ManageWP Worker / 3.9.2
ManageWP Worker v3.9.2
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / plugins / cleanup / cleanup.php

cleanup.php in ManageWP Worker 3.9.2, at plugins/cleanup/cleanup.php

186 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*************************************************************
3 *
4 *
5 *
6 * ManageWP Worker Plugin
7 *
8 *
9 * Copyright (c) 2011 Prelovac Media
10 * www.prelovac.com
11 **************************************************************/
12
13 add_filter('mmb_stats_filter', mmb_get_extended_info);
14
15
16 function mmb_get_extended_info($stats)
17 {
18 $stats['num_revisions'] = mmb_num_revisions();
19 $stats['overhead'] = mmb_get_overhead();
20 $stats['num_spam_comments'] = mmb_num_spam_comments();
21 return $stats;
22 }
23
24 /* Revisions */
25
26 mmb_add_action('cleanup_delete', 'cleanup_delete_worker');
27
28 function cleanup_delete_worker($params = array()){
29 global $mmb_core;
30
31 $params_array = explode('_', $params['actions']);
32 $return_array = array();
33 foreach ($params_array as $param){
34 switch ($param){
35 case 'revision' :
36 if(mmb_delete_all_revisions()){
37 $return_array['revision'] = 'Revisions deleted.';
38 }else{
39 $return_array['revision'] = 'Revisions not deleted.';
40 }
41 break;
42 case 'overhead' :
43 if(mmb_clear_overhead()){
44 $return_array['overhead'] = 'Overhead cleared.';
45 }else{
46 $return_array['overhead'] = 'Overhead not cleared.';
47 }
48 break;
49 case 'comment' :
50 if(mmb_delete_spam_comments()){
51 $return_array['comment'] = 'Comments deleted';
52 }else{
53 $return_array['comment'] = 'Comments not deleted';
54 }
55 break;
56 default:
57 break;
58 }
59
60 }
61
62 unset($params);
63
64 mmb_response($return_array, true);
65 }
66
67 function mmb_num_revisions() {
68 global $wpdb;
69 $sql = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'revision'";
70 $num_revisions = $wpdb->get_var($wpdb->prepare($sql));
71 return $num_revisions;
72 }
73
74 function mmb_select_all_revisions() {
75 global $wpdb;
76 $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'revision'";
77 $revisions = $wpdb->get_results($wpdb->prepare($sql));
78 return $revisions;
79 }
80
81 function mmb_delete_all_revisions() {
82 global $wpdb;
83 $sql = "DELETE a,b,c FROM $wpdb->posts a LEFT JOIN $wpdb->term_relationships b ON (a.ID = b.object_id) LEFT JOIN $wpdb->postmeta c ON (a.ID = c.post_id) WHERE a.post_type = 'revision'";
84 $revisions = $wpdb->query($wpdb->prepare($sql));
85 return $revisions;
86 }
87
88
89
90 /* Optimize */
91
92 function mmb_get_overhead()
93 {
94 global $wpdb, $mmb_core;
95 $tot_data = 0;
96 $tot_idx = 0;
97 $tot_all = 0;
98 $query = 'SHOW TABLE STATUS FROM '. DB_NAME;
99 $tables = $wpdb->get_results($wpdb->prepare($query),ARRAY_A);
100 foreach($tables as $table)
101 {
102 if($wpdb->base_prefix != $wpdb->prefix){
103 if(preg_match('/^'.$wpdb->prefix.'*/Ui', $table['Name'])){
104 $tot_data = $table['Data_length'];
105 $tot_idx = $table['Index_length'];
106 $total = $tot_data + $tot_idx;
107 $total = $total / 1024 ;
108 $total = round ($total,3);
109 $gain= $table['Data_free'];
110 $gain = $gain / 1024 ;
111 $total_gain += $gain;
112 $gain = round ($gain,3);
113 }
114 } else if(preg_match('/^'.$wpdb->prefix.'[0-9]{1,20}_*/Ui', $table['Name'])){
115 continue;
116 }
117 else {
118 $tot_data = $table['Data_length'];
119 $tot_idx = $table['Index_length'];
120 $total = $tot_data + $tot_idx;
121 $total = $total / 1024 ;
122 $total = round ($total,3);
123 $gain= $table['Data_free'];
124 $gain = $gain / 1024 ;
125 $total_gain += $gain;
126 $gain = round ($gain,3);
127 }
128 }
129 return round($total_gain,3);
130 }
131
132
133 function mmb_clear_overhead()
134 {
135 global $wpdb;
136 $tables = $wpdb->get_col("SHOW TABLES");
137 foreach ($tables as $table_name) {
138 if($wpdb->base_prefix != $wpdb->prefix){
139 if(preg_match('/^'.$wpdb->prefix.'*/Ui', $table_name)){
140 $table_string .= $table_name . ",";
141 }
142 } else if(preg_match('/^'.$wpdb->prefix.'[0-9]{1,20}_*/Ui', $table_name)){
143 continue;
144 }
145 else
146 $table_string .= $table_name . ",";
147 }
148 $table_string = substr($table_string,0,strlen($table_string)-1); //remove last ,
149
150 $table_string = rtrim($table_string);
151
152 $query = "OPTIMIZE TABLE $table_string";
153
154 $optimize = $wpdb->query($query);
155 return $optimize ? true : false;
156 }
157
158
159
160
161 /* Spam Comments */
162
163 function mmb_num_spam_comments()
164 {
165 global $wpdb;
166 $sql = "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = 'spam'";
167 $num_spams = $wpdb->get_var($wpdb->prepare($sql));
168 return $num_spams;
169 }
170
171 function mmb_delete_spam_comments()
172 {
173 global $wpdb;
174 $sql = "DELETE FROM $wpdb->comments WHERE comment_approved = 'spam'";
175 $spams = $wpdb->query($wpdb->prepare($sql));
176 return $sql;
177 }
178
179
180 function mmb_get_spam_comments() {
181 global $wpdb;
182 $sql = "SELECT * FROM $wpdb->comments as a LEFT JOIN $wpdb->commentmeta as b WHERE a.comment_ID = b.comment_id AND a.comment_approved = 'spam'";
183 $spams = $wpdb->get_results($wpdb->prepare($sql));
184 return $spams;
185 }
186 ?>