/wp-content/plugins/worker/plugins/cleanup/cleanup.php

https://bitbucket.org/anneivycat/ebcookhouse · PHP · 224 lines · 172 code · 37 blank · 15 comment · 30 complexity · d74181bc44bfc9e6c1a3bfea7443c6cf MD5 · raw file

  1. <?php
  2. /*************************************************************
  3. *
  4. *
  5. *
  6. * ManageWP Worker Plugin
  7. *
  8. *
  9. * Copyright (c) 2011 Prelovac Media
  10. * www.prelovac.com
  11. **************************************************************/
  12. if(basename($_SERVER['SCRIPT_FILENAME']) == "cleanup.php"):
  13. exit;
  14. endif;
  15. add_filter('mmb_stats_filter', 'mmb_get_extended_info');
  16. function mmb_get_extended_info($stats)
  17. {
  18. global $mmb_core;
  19. $params = get_option('mmb_stats_filter');
  20. $filter = isset($params['plugins']['cleanup']) ? $params['plugins']['cleanup'] : array();
  21. $stats['num_revisions'] = mmb_num_revisions($filter['revisions']);
  22. //$stats['num_revisions'] = 5;
  23. $stats['overhead'] = mmb_handle_overhead(false);
  24. $stats['num_spam_comments'] = mmb_num_spam_comments();
  25. return $stats;
  26. }
  27. /* Revisions */
  28. mmb_add_action('cleanup_delete', 'cleanup_delete_worker');
  29. function cleanup_delete_worker($params = array())
  30. {
  31. global $mmb_core;
  32. $revision_params = get_option('mmb_stats_filter');
  33. $revision_filter = isset($revision_params['plugins']['cleanup']) ? $revision_params['plugins']['cleanup'] : array();
  34. $params_array = explode('_', $params['actions']);
  35. $return_array = array();
  36. foreach ($params_array as $param) {
  37. switch ($param) {
  38. case 'revision':
  39. if (mmb_delete_all_revisions($revision_filter['revisions'])) {
  40. $return_array['revision'] = 'OK';
  41. } else {
  42. $return_array['revision_error'] = 'Failed, please try again';
  43. }
  44. break;
  45. case 'overhead':
  46. if (mmb_handle_overhead(true)) {
  47. $return_array['overhead'] = 'OK';
  48. } else {
  49. $return_array['overhead_error'] = 'Failed, please try again';
  50. }
  51. break;
  52. case 'comment':
  53. if (mmb_delete_spam_comments()) {
  54. $return_array['comment'] = 'OK';
  55. } else {
  56. $return_array['comment_error'] = 'Failed, please try again';
  57. }
  58. break;
  59. default:
  60. break;
  61. }
  62. }
  63. unset($params);
  64. mmb_response($return_array, true);
  65. }
  66. function mmb_num_revisions($filter)
  67. {
  68. global $wpdb;
  69. $sql = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'revision'";
  70. $num_revisions = $wpdb->get_var($sql);
  71. if(isset($filter['num_to_keep']) && !empty($filter['num_to_keep'])){
  72. $num_rev = str_replace("r_","",$filter['num_to_keep']);
  73. if($num_revisions < $num_rev){
  74. return 0;
  75. }
  76. return ($num_revisions - $num_rev);
  77. }else{
  78. return $num_revisions;
  79. }
  80. }
  81. function mmb_select_all_revisions()
  82. {
  83. global $wpdb;
  84. $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'revision'";
  85. $revisions = $wpdb->get_results($sql);
  86. return $revisions;
  87. }
  88. function mmb_delete_all_revisions($filter)
  89. {
  90. global $wpdb, $mmb_core;
  91. $where = '';
  92. if(isset($filter['num_to_keep']) && !empty($filter['num_to_keep'])){
  93. $num_rev = str_replace("r_","",$filter['num_to_keep']);
  94. $select_posts = "SELECT ID FROM $wpdb->posts WHERE post_type = 'revision' ORDER BY post_date DESC LIMIT ".$num_rev;
  95. $select_posts_res = $wpdb->get_results($select_posts);
  96. $notin = '';
  97. $n = 0;
  98. foreach($select_posts_res as $keep_post){
  99. $notin.=$keep_post->ID;
  100. $n++;
  101. if(count($select_posts_res)>$n){
  102. $notin.=',';
  103. }
  104. }
  105. $where = " AND a.ID NOT IN (".$notin.")";
  106. }
  107. $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'".$where;
  108. $revisions = $wpdb->query($sql);
  109. return $revisions;
  110. }
  111. /* Optimize */
  112. function mmb_handle_overhead($clear = false)
  113. {
  114. global $wpdb, $mmb_core;
  115. $tot_data = 0;
  116. $tot_idx = 0;
  117. $tot_all = 0;
  118. $query = 'SHOW TABLE STATUS';
  119. $tables = $wpdb->get_results($query, ARRAY_A);
  120. $total_gain = 0;
  121. $table_string = '';
  122. foreach ($tables as $table) {
  123. if (isset($table['Engine']) && in_array($table['Engine'], array(
  124. 'MyISAM',
  125. 'ISAM',
  126. 'HEAP',
  127. 'MEMORY',
  128. 'ARCHIVE'
  129. ))) {
  130. if ($wpdb->base_prefix != $wpdb->prefix) {
  131. if (preg_match('/^' . $wpdb->prefix . '*/Ui', $table['Name'])) {
  132. if ($table['Data_free'] > 0) {
  133. $total_gain += $table['Data_free'] / 1024;
  134. $table_string .= $table['Name'] . ",";
  135. }
  136. }
  137. } else if (preg_match('/^' . $wpdb->prefix . '[0-9]{1,20}_*/Ui', $table['Name'])) {
  138. continue;
  139. } else {
  140. if ($table['Data_free'] > 0) {
  141. $total_gain += $table['Data_free'] / 1024;
  142. $table_string .= $table['Name'] . ",";
  143. }
  144. }
  145. } elseif (isset($table['Engine']) && $table['Engine'] == 'InnoDB') {
  146. //$total_gain += $table['Data_free'] > 100*1024*1024 ? $table['Data_free'] / 1024 : 0;
  147. }
  148. }
  149. if ($clear) {
  150. $table_string = substr($table_string, 0, strlen($table_string) - 1); //remove last ,
  151. $table_string = rtrim($table_string);
  152. $query = "OPTIMIZE TABLE $table_string";
  153. $optimize = $wpdb->query($query);
  154. return $optimize === FALSE ? false : true;
  155. } else
  156. return round($total_gain, 3);
  157. }
  158. /* Spam Comments */
  159. function mmb_num_spam_comments()
  160. {
  161. global $wpdb;
  162. $sql = "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = 'spam'";
  163. $num_spams = $wpdb->get_var($sql);
  164. return $num_spams;
  165. }
  166. function mmb_delete_spam_comments()
  167. {
  168. global $wpdb;
  169. $spams = 1;
  170. $total = 0;
  171. while ($spams) {
  172. $sql = "DELETE FROM $wpdb->comments WHERE comment_approved = 'spam' LIMIT 200";
  173. $spams = $wpdb->query($sql);
  174. $total += $spams;
  175. if ($spams)
  176. usleep(100000);
  177. }
  178. return $total;
  179. }
  180. function mmb_get_spam_comments()
  181. {
  182. global $wpdb;
  183. $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'";
  184. $spams = $wpdb->get_results($sql);
  185. return $spams;
  186. }
  187. ?>