SHARE
TWEET

power-tools.php

ovizii Jul 1st, 2011 382 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. * Plugin Name: WPMU Power Tools
  4. * Plugin URI: http://plugins.paidtoblog.com/wpmu-power-tools/
  5. * Description: A few powerfull tools that every WPMU Admin should have.
  6. * Author: Brian Freeman (aka MrBrian)
  7. * Version: 0.7
  8. */
  9.  
  10. /*
  11. Change Log:
  12. ----------------------------------------------------------------------
  13. ----------------------------------------------------------------------
  14. 0.7 - 01/10/11
  15. ----------------------------------------------------------------------
  16. - Changed underscores to dashes (thanks Dustin Rue)
  17. - Check for blank output (props Warren)
  18.  
  19. 0.6 - 12/10/10
  20. ----------------------------------------------------------------------
  21. - Fixed logic bug with deleting blogs after 30 days (a very bad typo...)
  22. - Updated deprecated functions (thanks tom!)
  23.  
  24. 0.5 - 8/18/10
  25. ----------------------------------------------------------------------
  26. - Updated for WP 3.0
  27. - Removed performance mode
  28. - Bugfix with progress bar
  29. - Bugfix with unused blogs cleanup
  30.  
  31. 0.4 - 1/04/10
  32. ----------------------------------------------------------------------
  33. - Added performance mode for PHP Code Executor for very large/complex WPMU databases, such as multi-db with 4000+ blogs
  34. - Tweaked the progress bar to be more informative during operations
  35. - added $current_site global
  36. - Minor fixes/tweaks
  37.  
  38. 0.3 - 10/02/08
  39. ----------------------------------------------------------------------
  40. - Initial Release.
  41.  
  42. */
  43.  
  44.  
  45. /* Some sample snippet codes for PHP Executor (feel free to send in yours)
  46. *
  47.  
  48. //The code below will output all users that have the firestats plugin installed.
  49. $plugin_file = 'firestats/firestats-wordpress.php';
  50. if ( @is_plugin_active($plugin_file) ) {
  51. $user = get_users_of_blog($blog_id);
  52. echo 'activated for:'.$user[0]->user_login.'<br>';
  53. }
  54.  
  55. //Migrate all users of one wordpress theme to another one
  56. $old_theme = get_option('current_theme');
  57. if($old_theme == 'Old Theme Name')
  58. update_option('current_theme', 'New Theme Name');
  59.  
  60. //For Donncha's Sitewide Tags plugin: Populate tags blog with all posts made before the plugin was added
  61. $posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'publish'" );
  62. if ( !empty($posts) ) {
  63.     foreach ( $posts as $post ) {
  64.         $post_id = $post['ID'];
  65.         if($post_id != 1 && $post_id != 2)
  66.             sitewide_tags_post($post_id, get_post($post_id));
  67.     }
  68. }
  69.  
  70. *
  71. */
  72.  
  73. //------------------------------------------------------------------------//
  74. //---Hook-----------------------------------------------------------------//
  75. //------------------------------------------------------------------------//
  76. add_action( 'admin_menu', 'power_tools_plug_pages' );
  77. //------------------------------------------------------------------------//
  78. //---Functions------------------------------------------------------------//
  79. //------------------------------------------------------------------------//
  80.  
  81. function power_tools_plug_pages()
  82. {
  83.     add_submenu_page( 'ms-admin.php', 'Power Tools', 'Power Tools', 'manage_options', 'power-tools', 'power_tools_page_output' );
  84. }
  85.  
  86. //------------------------------------------------------------------------//
  87. //---Page Output Functions------------------------------------------------//
  88. //------------------------------------------------------------------------//
  89.  
  90. function power_tools_page_output()
  91. {
  92.     global $blog_id, $wpdb, $wp_roles, $wp_rewrite, $current_user, $current_site;
  93.    
  94.     if ( !isset( $_GET['action'] ) )
  95.                 $_GET['action'] = '';
  96.  
  97. ?>
  98. <div class="wrap">
  99. <?php
  100.     switch ( $_GET['action'] ) {
  101.             //---------------------------------------------------//
  102.         default:
  103. ?>
  104.                         <h2><?php _e( 'Power Tools' ) ?></h2>
  105.                         <h3><?php _e( 'PHP Code Executor' ) ?></h3>
  106.                         <div class="wrap">
  107.             <form method="post" action="ms-admin.php?page=power-tools&action=phpexec">
  108.             <p>
  109.             You can use the form below to execute lines of PHP code on the server once or for each and every blog. This can be highly usefull, for example, if you need make changes to each and every blog automagically. <strong>This tool is intended for advanced users only!</strong></p>
  110.             <table class="form-table">
  111.             <tr valign="top">
  112.             <th scope="row"><?php _e( 'PHP Code:' ) ?></th>
  113.             <td>
  114.             <?php echo htmlentities( '<?php' ); ?>
  115.             <br>
  116.             <textarea name="power_tools_phpexec_code" type="text" rows="5" wrap="soft" id="power_tools_phpexec_code" style="width: 95%"></textarea>
  117.             <br>
  118.             <?php echo htmlentities( '?>' ); ?>
  119.             <br>
  120.                                 <b>Globals available:</b> $blog_id, $wpdb, $wp_roles, $wp_rewrite, $current_user, $current_site</td>
  121.             </tr>
  122.             <tr>
  123.             <th scope="row"><?php _e( 'Options:' ) ?></th>
  124.                         <td>
  125.                         <input type="radio" name="power_tools_phpexec_option" id="power_tools_phpexec_option" value="runonce" checked/> Run this code once (Blog ID: <b><?php echo
  126.             $blog_id; ?></b>)
  127.       <br>
  128.                         <input type="radio" name="power_tools_phpexec_option" id="power_tools_phpexec_option" value="runall"/> Execute this code on all blogs
  129.                         </td>
  130.                         </tr>
  131.             </table>
  132.             <p class="submit">
  133.             <input type="submit" name="Submit" value="<?php _e( 'Execute' ) ?>" />
  134.             </p>
  135.             </form>
  136.             </div>
  137.            
  138.             <br>
  139.            
  140.                         <h3><?php _e( 'WPMU PowerClean' ) ?></h3>
  141.                         <div class="wrap">
  142.             <form method="post" action="ms-admin.php?page=power-tools&action=cleanup">
  143.             <p>This is a very powerful cleanup tool that will remove unnecessary data left over by WPMU. Select the options below that you want and hit PowerClean to start! <b>Be sure to do a full backup before running this operation!</b></p>
  144.             <table class="form-table">
  145.             <tr valign="top">
  146.             <th scope="row"><?php _e( 'Options:' ) ?></th>
  147.                         <td>
  148.                         <input type="checkbox" name="power_tools_blogcleanup_option_spamblogs" id="power_tools_blogcleanup_option_spamblogs" /> Blogs marked as spam<br>
  149.                         <input type="checkbox" name="power_tools_blogcleanup_option_spamusers" id="power_tools_blogcleanup_option_spamusers" /> Users marked as spam<br>
  150.                         <input type="checkbox" name="power_tools_blogcleanup_option_spamcomments" id="power_tools_blogcleanup_option_spamcomments" /> Comments marked as spam/delete <b>(intensive)</b><br>
  151.                         <input type="checkbox" name="power_tools_blogcleanup_option_deletedblogs" id="power_tools_blogcleanup_option_deletedblogs" /> Blogs that have been marked for deletion by their owners<br>
  152.                         <input type="checkbox" name="power_tools_blogcleanup_option_oldblogs" id="power_tools_blogcleanup_option_oldblogs" /> Blogs with 0 posts that are older than 30 days <b>(intensive)</b><br>
  153.                         <input type="checkbox" name="power_tools_blogcleanup_option_oldsignups" id="power_tools_blogcleanup_option_oldsignups" /> Blog/user signups older than 30 days and still awaiting activation via email<br>
  154.                         </td>
  155.                         </tr>
  156.             </table>
  157.             <p class="submit">
  158.             <input type="submit" name="Submit" value="<?php _e( 'PowerClean' ) ?>" />
  159.             </p>
  160.             </form>
  161.             </div>
  162.                         <?php
  163.             break;
  164.             //---------------------------------------------------//
  165.         case "phpexec":
  166.             @set_time_limit(0);
  167.             @ini_set('display_errors','1');
  168.  
  169.             $phpexec_code = stripslashes( $_POST["power_tools_phpexec_code"] );
  170.             echo '<p>Executing...</p>';
  171.             if ( $_POST['power_tools_phpexec_option'] == 'runonce' ) {
  172.                 eval( $phpexec_code );
  173.                 echo '<p>Completed. &nbsp;&nbsp;<a href="ms-admin.php?page=power-tools">&laquo; Go back</a></p>';
  174.             } else {
  175.                 $blogs = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
  176.                 $blog_count = count( $blogs );
  177.                 foreach ( $blogs as $blog ) {
  178.                   $blog_exec_count++;
  179.                   power_tools_progress_bar( $blog_exec_count, $blog_count, "Current blog ID: <b>$blog</b>" );
  180.                   switch_to_blog( $blog );
  181.                   eval( $phpexec_code );
  182.                 }
  183.                 echo '<p>Completed operation on ' . number_format( $blog_count ) .
  184.                     ' blogs. &nbsp;&nbsp;<a href="ms-admin.php?page=power-tools">&laquo; Go back</a></p>';
  185.             }
  186.             break;
  187.         case "cleanup":
  188.             @set_time_limit(0);
  189.             @ini_set('display_errors','1');
  190.  
  191.             $cleanup_spamblogs = isset( $_POST['power_tools_blogcleanup_option_spamblogs'] ) ? true:
  192.             false;
  193.             $cleanup_spamusers = isset( $_POST['power_tools_blogcleanup_option_spamusers'] ) ? true:
  194.             false;
  195.             $cleanup_spamcomments = isset( $_POST['power_tools_blogcleanup_option_spamcomments'] ) ? true:
  196.             false;
  197.             $cleanup_deletedblogs = isset( $_POST['power_tools_blogcleanup_option_deletedblogs'] ) ? true:
  198.             false;
  199.             $cleanup_oldblogs = isset( $_POST['power_tools_blogcleanup_option_oldblogs'] ) ? true:
  200.             false;
  201.             $cleanup_oldsignups = isset( $_POST['power_tools_blogcleanup_option_oldsignups'] ) ? true:
  202.             false;
  203.  
  204.             if ( $cleanup_spamblogs ) {
  205.                 $blogs = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs WHERE spam = 1" );
  206.                 $cnt = count($blogs);
  207.                 if ( !empty($blogs) ) {
  208.                     foreach ( $blogs as $blog ) {
  209.                         power_tools_progress_bar( $cleanup_spamblogs_count, $cnt, 'Cleaning blogs marked as spam... (blog ID: <b>' . $blog . '</b>)' );
  210.                         wpmu_delete_blog( $blog, true );
  211.                         $cleanup_spamblogs_count++;
  212.                     }
  213.                 }
  214.             }
  215.  
  216.             if ( $cleanup_spamusers ) {
  217.                 $users = $wpdb->get_col( "SELECT ID FROM $wpdb->users WHERE spam = 1" );
  218.                 $cnt = count($users);
  219.                 if ( !empty($users) ) {
  220.                     foreach ( $users as $user ) {
  221.                         power_tools_progress_bar( $cleanup_spamusers_count, $cnt, 'Cleaning users marked as spam... (user ID: <b>' . $user . '</b>)' );
  222.                         wpmu_delete_user( $user );
  223.                         $cleanup_spamusers_count++;
  224.                     }
  225.                 }
  226.             }
  227.  
  228.             if ( $cleanup_spamcomments ) {
  229.                 $blogs = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
  230.                 $cnt = count($blogs);
  231.                 if ( !empty($blogs) ) {
  232.                     foreach ( $blogs as $blog ) {
  233.                         power_tools_progress_bar( $cleanup_spamcomments_count, $cnt, 'Cleaning comments marked as spam... (blog ID: <b>' . $blog . '</b>)' );
  234.                         $wpdb->query( "DELETE FROM {$wpdb->base_prefix}{$blog}_comments WHERE comment_approved = 'spam' OR comment_approved = 'delete'" );
  235.                         $cleanup_spamcomments_count = ( $cleanup_spamcomments_count + $wpdb->rows_affected );
  236.                     }
  237.                 }
  238.             }
  239.  
  240.  
  241.             if ( $cleanup_deletedblogs ) {
  242.                 $blogs = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs WHERE deleted = 1" );
  243.                 $cnt = count($blogs);
  244.                 if ( !empty($blogs) ) {
  245.                     foreach ( $blogs as $blog ) {
  246.                         power_tools_progress_bar( $cleanup_deletedblogs_count, $cnt, 'Cleaning blogs marked for deletion... (blog ID: <b>' . $blog . '</b>)' );
  247.                         wpmu_delete_blog( $blog, true );
  248.                         $cleanup_deletedblogs_count++;
  249.                     }
  250.                 }
  251.             }
  252.  
  253.             if ( $cleanup_oldblogs ) {
  254.                 $blogs = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs WHERE DATE(registered) < DATE_SUB(curdate(), INTERVAL 30 DAY)" );
  255.                 $cnt = count($blogs);
  256.                 if ( !empty($blogs) ) {
  257.                     foreach ( $blogs as $blog ) {
  258.                         power_tools_progress_bar( $cleanup_oldblogs_count, $cnt, 'Cleaning blogs older than 30 days with 0 posts... (checking blog ID: <b>' . $blog . '</b>)' );
  259.                         $posts = $wpdb->get_col( "SELECT ID FROM {$wpdb->base_prefix}{$blog}_posts" );
  260.                         if ($posts[0]['post_title'] == 'Hello world!' && $posts[1]['post_title'] == 'About' && count($posts) == 2) { //wordpress by default adds one post and one page, so we check for a post ID of 3
  261.                             wpmu_delete_blog( $blog, true );
  262.                             $cleanup_oldblogs_count++;
  263.                         }
  264.                     }
  265.                 }
  266.             }
  267.  
  268.             if ( $cleanup_oldsignups ) {
  269.                 power_tools_progress_bar( 1, 100, 'Cleaning stale signups...' );
  270.                 $wpdb->query( "DELETE FROM $wpdb->signups WHERE active = 0 AND DATE(registered) < DATE_SUB(curdate(), INTERVAL 30 DAY)" );
  271.                 $cleanup_oldsignups_count = $wpdb->rows_affected;
  272.                 power_tools_progress_bar( 1, 100, 'Finished.' );
  273.             }
  274.  
  275.             echo '<br>-----Results of cleanup-----<br>';
  276.             echo number_format( $cleanup_spamblogs_count ) . ' spam blogs.<br>';
  277.             echo number_format( $cleanup_spamusers_count ) . ' spam users.<br>';
  278.             echo number_format( $cleanup_spamcomments_count ) . ' spam comments.<br>';
  279.             echo number_format( $cleanup_deletedblogs_count ) . ' deleted blogs.<br>';
  280.             echo number_format( $cleanup_oldblogs_count ) . ' abandoned blogs.<br>';
  281.             echo number_format( $cleanup_oldsignups_count ) . ' old inactive signups.<br>';
  282.             echo '<p><a href="ms-admin.php?page=power-tools">&laquo; Go back</a></p>';
  283.     }
  284. ?>
  285. </div>
  286. <?php
  287. }
  288.  
  289. function power_tools_progress_bar( $intCurrentCount = 1, $intTotalCount = 100, $strStatus )
  290. {
  291.     static $intNumberRuns = 0;
  292.     static $intDisplayedCurrentPercent = 0;
  293.     $strProgressBar = '';
  294.     $dblPercentIncrease = ( 100 / $intTotalCount );
  295.     $intCurrentPercent = intval( $intCurrentCount * $dblPercentIncrease );
  296.     $intNumberRuns++;
  297.  
  298.     if ( 1 == $intNumberRuns ) {
  299.         $strProgressBar = <<< BAR
  300. <table width='50%' id='progress_bar' summary='progress_bar' align='center'><tbody><tr>
  301. <td id='progress_bar_complete' width='0%' align='center' style='background:#CCFFCC;'>&nbsp;</td>
  302. <td style='background:#FFCCCC;'>&nbsp;</td>
  303. </tr></tbody></table>
  304. <p id='progress_bar_status'>&nbsp;</p>
  305. <script type='text/javascript' language='javascript'>
  306. function power_tools_progress_bar_update(intCurrentPercent,strStatus)
  307. {
  308.     document.getElementById('progress_bar_complete').style.width = intCurrentPercent+'%';
  309.     document.getElementById('progress_bar_complete').innerHTML = intCurrentPercent+'%';
  310.     document.getElementById('progress_bar_status').innerHTML = strStatus;
  311. }
  312. </script>
  313. BAR;
  314.     }
  315.     else
  316.         if ( $intDisplayedCurrentPercent <> $intCurrentPercent ) {
  317.             $intDisplayedCurrentPercent = $intCurrentPercent;
  318.             $strProgressBar = <<< BAR
  319. <script type='text/javascript' language='javascript'>
  320. power_tools_progress_bar_update($intCurrentPercent,'$strStatus');
  321. </script>
  322. BAR;
  323.         }
  324.     echo $strProgressBar;
  325.     flush();
  326.     ob_flush();
  327. }
  328.  
  329. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top