Advertisement
Imperative-Ideas

Remove Zombie Users (WordPress)

Dec 25th, 2013
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.24 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Clean Up Users
  4. Plugin URI: http://www.dagondesign.com/articles/clean-up-users-plugin-for-wordpress/
  5. Description: Removes registered users that have no posts or comments. Originally by Dagon Design (http://www.dagondesign.com/), the plugin was not maintained for 3.x. Updated by Imperative Ideas. Works up to WordPress 3.8.
  6. Author: Imperative Ideas
  7. Version: 1.1
  8. Author URI: http://imperativeideas.com/
  9.  
  10.  
  11. Changelog:
  12. v.1.1 - Updated query to user $wpdb and ensured meta_capabilities key was able to be called by secure sites not using the wp_ prefix.
  13. */
  14.  
  15.  
  16. $ddcuu_version = '1.1';
  17.  
  18. function ddcuu_add_options_pages() {
  19.     if (function_exists('add_options_page')) {
  20.         add_options_page("Clean Up Users", 'Clean Empty Users', 8, __FILE__, 'ddcuu_options_page');
  21.     }
  22. }
  23.  
  24. function ddcuu_options_page() {
  25.  
  26.     // user roles
  27.     $skip_admins = TRUE;
  28.     $skip_editors = TRUE;
  29.     $skip_authors = TRUE;
  30.     $skip_contributors = TRUE;
  31.  
  32.  
  33.     global $wpdb, $dddu_version;
  34.     $tp = $wpdb->prefix;
  35.  
  36.  
  37.     $result = "";
  38.  
  39.     if (isset($_POST['info_update'])) {
  40.  
  41.         // start processing
  42.  
  43.         ?><div id="message" class="updated fade"><p><strong><?php
  44.  
  45.                 echo "Action Complete - View Results Below";
  46.  
  47.                 ?></strong></p></div><?php
  48.  
  49.  
  50.         $result = '';
  51.  
  52.         $dddu_confirm = (bool)$_POST['dddu_confirm'];
  53.  
  54.  
  55.  
  56.         if ($dddu_confirm) {
  57.  
  58.             $skip_check = '';
  59.             if ($skip_admins) $skip_check .= " AND LOCATE('administrator', {$tp}usermeta.meta_value) = 0 ";
  60.             if ($skip_editors) $skip_check .= " AND LOCATE('editor', {$tp}usermeta.meta_value) = 0 ";
  61.             if ($skip_authors) $skip_check .= " AND LOCATE('author', {$tp}usermeta.meta_value) = 0 ";
  62.             if ($skip_contributors) $skip_check .= " AND LOCATE('contributor', {$tp}usermeta.meta_value) = 0 ";
  63.  
  64.             // list of users with no posts and no comments
  65.             $getusers = "
  66.                 SELECT $wpdb->users.ID, $wpdb->users.user_login
  67.                FROM $wpdb->users
  68.                LEFT JOIN $wpdb->posts ON ($wpdb->users.ID = $wpdb->posts.post_author)
  69.                LEFT JOIN $wpdb->comments ON ($wpdb->users.ID = $wpdb->comments.user_id)
  70.                LEFT JOIN $wpdb->usermeta ON ($wpdb->users.ID = $wpdb->usermeta.user_id)
  71.                WHERE $wpdb->posts.post_author is NULL
  72.                AND $wpdb->comments.user_id is NULL
  73.                AND $wpdb->usermeta.meta_key = '{$tp}capabilities'
  74.             ";
  75.  
  76.             $userlist= $wpdb->get_results($getusers, OBJECT);
  77.  
  78.             foreach ($userlist as $u) {
  79.                 wp_delete_user($u->ID);
  80.             }
  81.  
  82.             $result = 'Users deleted: ' . count($userlist);
  83.  
  84.  
  85.  
  86.         } else {
  87.  
  88.             $result = 'No option selected!';
  89.  
  90.         }
  91.  
  92.  
  93.  
  94.         // end processing
  95.  
  96.  
  97.     } ?>
  98.  
  99.     <div class=wrap>
  100.  
  101.     <h2>Clean Up Users<?php echo $dddu_version; ?></h2>
  102.  
  103.     <p>A simple plugin that deletes any standard users who do not have comments. For most blogs, these types of users are SPAM. Remember to back up your database before executing this plugin!</p>
  104.  
  105.     <p style="color: red;">This plugin has been rewritten from scratch with lots of additional options, it is available at <a href="https://github.com/isarmstrong/clean-zombie-users">Github</a></p>
  106.  
  107.     <?php
  108.  
  109.     if ($result != "") {
  110.         echo '<div style="border: 1px solid #888888; padding: 5px;">';
  111.         echo '<strong>Results</strong>:<br /> ' . trim($result) . '</div>';
  112.     }
  113.  
  114.     ?>
  115.  
  116.  
  117.     <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>"  >
  118.         <input type="hidden" name="info_update" id="info_update" value="true" />
  119.  
  120.  
  121.         <div style="padding: 0 0 15px 12px;">
  122.  
  123.             <?php print $formatinfo; ?>
  124.             <h3>Confirmation:</h3>
  125.             <input type="checkbox" name="dddu_confirm" id="dddu_confirm" />
  126.             <label name=""dddu_confirm">I understand that this will permanently delete users from my database</label>
  127.         </div>
  128.  
  129.  
  130.         <div class="submit">
  131.             <input type="submit" name="info_update" value="Remove Zombie Users" />
  132.         </div>
  133.     </form>
  134.     </div><?php
  135. }
  136.  
  137.  
  138. add_action('admin_menu', 'ddcuu_add_options_pages');
  139.  
  140. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement