WordPress.org

Plugin Directory

Changeset 496737


Ignore:
Timestamp:
01/29/12 04:59:01 (2 years ago)
Author:
sivel
Message:
  • Fixes a deprecated notice in WordPress 3.3 when using get_userdatabylogin
  • Removes the $auto_reactivate variable
  • Look for REACTIVATE_WP_RESET constant to be defined in wp-config.php for auto reactivation after reset
  • Look for global $reactivate_wp_reset_additional for additional plugins to auto reactivate after reset
  • Add a Reset link to the admin bar under the site title
Location:
wordpress-reset/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wordpress-reset/trunk/readme.txt

    r225278 r496737  
    33Donate Link: http://sivel.net/donate 
    44Tags: wordpress-reset, wordpress, reset, admin 
    5 Requires at least: 2.7 
    6 Tested up to: 3.0 
     5Requires at least: 2.8 
     6Tested up to: 3.3 
    77Stable tag: 1.2 
    88 
    9 Resets the WordPress database back to it's defaults.  Deletes all customizations and content. Does not modify files only resets the database. 
     9Resets the WordPress database back to it's defaults. Deletes all customizations and content. Does not modify files only resets the database. 
    1010 
    1111== Description == 
    1212 
    13 Resets the WordPress database back to it's defaults.  Deletes all customizations and content. Does not modify files only resets the database. 
     13Resets the WordPress database back to it's defaults. Deletes all customizations and content. Does not modify files only resets the database. 
    1414 
    1515This plugin is very helpful for plugin and theme developers. 
    1616 
    17 If the admin user exists and has level_10 permissions it will be recreated with its current password and email address.  If the admin user does not exist or is a dummy account without admin permissions the username that is logged in will be recreated with its email address and current password. The blog name is also kept. 
     17If the admin user exists and has level_10 permissions it will be recreated with its current password and email address. If the admin user does not exist or is a dummy account without admin permissions the username that is logged in will be recreated with its email address and current password. The blog name is also kept. 
    1818 
    19 The plugin will add an entry to the favorites drop down and also reactivate itself after the reset.  There is however a configuration option to keep it from reactivating after the reset. 
    20  
    21 Props to [Jonathan Dingman](http://www.ginside.com/) for the idea behind this plugin, testing and feedback. 
     19The plugin will add an entry to the Admin Bar under the site title and has the ability to reactivate itself and other plugins after the reset. 
    2220 
    2321== Installation == 
     
    2826== Frequently Asked Questions == 
    2927 
    30 = How can I keep the plugin from automatically reactivating after the reset? = 
     28= How can I get this plugin to automatically reactive after the reset? = 
    3129 
    32 Open the wordpress-reset.php file in an editor and modify the line that reads `var $auto_reactivate = true;` to be `var $auto_reactivate = false;` 
     30Add `define( 'REACTIVATE_WP_RESET', true );` to `wp-config.php` above `/* That's all, stop editing! Happy blogging. */` 
     31 
     32= Can this plugin reactivate other plugins automatically after performing the reset? = 
     33 
     34Add an array called `$reactivate_wp_reset_additional` to the global scope by placing it in `wp-config.php` above `/* That's all, stop editing! Happy blogging. */` that contains the plugin basenames of the plugins to activate, such as: 
     35 
     36` 
     37$reactivate_wp_reset_additional = array( 
     38    'hello.php', 
     39    'akismet/akismet.php' 
     40); 
     41` 
    3342 
    3443== Upgrade == 
     
    4049== Usage == 
    4150 
    42 1. Visit the WordPress Reset Tools page by either clicking the link in the favorites dropdown menu or Tools>WordPress Reset 
     511. Visit the WordPress Reset Tools page by either clicking the link in the Admin Bar or Tools>WordPress Reset 
    43521. Type 'reset' in the text field and click reset. 
    4453 
    4554== Upgrade Notice == 
    4655 
    47 This release fixes deprecated notices for WordPress 3.0, correctly disables the password nag and removes the randomly generate password from the new blog email. 
     56= 1.3 = 
     57 
     58Fixes a deprecated notice in WordPress 3.3, removed the $auto_reactivate variable, and look for REACTIVATE_WP_RESET to be defined in wp-config.php, as well as the ability to activate additional plugins using a global $reactivate_wp_reset_additional array defined in wp-config.php 
    4859 
    4960== Changelog == 
    5061 
    51 = 1.2 (2010-04-04: = 
     62= 1.3 (2012-01-29): = 
     63* Fixes a deprecated notice in WordPress 3.3 when using get_userdatabylogin 
     64* Removes the $auto_reactivate variable 
     65* Look for REACTIVATE_WP_RESET constant to be defined in wp-config.php for auto reactivation after reset 
     66* Look for global $reactivate_wp_reset_additional for additional plugins to auto reactivate after reset 
     67* Add a Reset link to the admin bar under the site title 
     68 
     69= 1.2 (2010-04-04): = 
    5270* Updates to fix deprecated notices for WP 3.0 
    5371* Updates for 3.0 to disable password nag 
  • wordpress-reset/trunk/wordpress-reset.php

    r225278 r496737  
    33Plugin Name: WordPress Reset 
    44Plugin URI: http://sivel.net/wordpress/wordpress-reset/ 
    5 Description: Resets the WordPress database back to it's defaults.  Deletes all customizations and content.  Does not modify files only resets the database. 
     5Description: Resets the WordPress database back to it's defaults. Deletes all customizations and content. Does not modify files only resets the database. 
    66Author: Matt Martz 
    7 Version: 1.2 
     7Version: 1.3 
    88Author URI: http://sivel.net/ 
    99 
    10         Copyright (c) 2009-2010 Matt Martz (http://sivel.net) 
    11         WordPress Reset is released under the GNU General Public License (GPL) 
    12         http://www.gnu.org/licenses/gpl-2.0.txt 
     10    Copyright (c) 2009-2012 Matt Martz (http://sivel.net) 
     11    WordPress Reset is released under the GNU General Public License (GPL) 
     12    http://www.gnu.org/licenses/gpl-2.0.txt 
    1313*/ 
    1414 
     
    1717 
    1818class WordPressReset { 
    19     // Set to true by default, this allows the plugin to be automatically reactivated after the reset 
    20     var $auto_reactivate = true; 
    21  
    2219    // Action/Filter Hooks 
    2320    function WordPressReset() { 
    24         add_action('admin_menu', array(&$this, 'add_page')); 
    25         add_action('admin_footer', array(&$this, 'footer')); 
    26         add_action('admin_head', array(&$this, 'head')); 
    27         add_action('init', array(&$this, 'init')); 
    28         add_filter('favorite_actions', array(&$this, 'favorites'), 100); 
    29         add_filter('wp_mail', array(&$this, 'hijack_mail'), 1); 
     21        add_action( 'admin_menu', array( &$this, 'add_page' ) ); 
     22        add_action( 'admin_init', array( &$this, 'admin_init' ) ); 
     23        add_filter( 'favorite_actions', array( &$this, 'favorites' ), 100 ); 
     24        add_action( 'wp_before_admin_bar_render', array( &$this, 'admin_bar_link' ) ); 
     25        add_filter( 'wp_mail', array( &$this, 'hijack_mail' ), 1 ); 
    3026    } 
    3127 
    3228    // favorite_actions filter hook operations 
    3329    // While this plugin is active put a link to the reset page in the favorites drop down. 
    34     function favorites($actions) { 
    35         $reset['tools.php?page=wordpress-reset'] = array('WordPress Reset', 'level_10'); 
    36         return array_merge($reset, $actions); 
    37     } 
    38  
    39     // init action hook operations 
     30    function favorites( $actions ) { 
     31        $reset['tools.php?page=wordpress-reset'] = array( 'WordPress Reset', 'level_10' ); 
     32        return array_merge( $reset, $actions ); 
     33    } 
     34 
     35    // wp_before_admin_bar_render action hook operations 
     36    // While this plugin is active put a link to the reset page in the admin bar under the site title 
     37    function admin_bar_link() { 
     38        global $wp_admin_bar; 
     39        $wp_admin_bar->add_menu( 
     40            array( 
     41                'parent' => 'site-name', 
     42                'id'     => 'wordpress-reset', 
     43                'title'  => 'Reset Site', 
     44                'href'   => admin_url( 'tools.php?page=wordpress-reset' ) 
     45            ) 
     46        ); 
     47    } 
     48 
     49    // admin_init action hook operations 
    4050    // Checks for wordpress_reset post value and if there deletes all wp tables 
    4151    // and performs an install, populating the users previous password also 
    42     function init() { 
     52    function admin_init() { 
    4353        global $current_user; 
    44         if ( isset($_POST['wordpress_reset']) && $_POST['wordpress_reset'] == 'true' && isset($_POST['wordpress_reset_confirm']) && $_POST['wordpress_reset_confirm'] == 'reset' ) { 
    45             require_once(ABSPATH . '/wp-admin/includes/upgrade.php'); 
    46  
    47             $blogname = get_option('blogname'); 
    48             $admin_email = get_option('admin_email'); 
    49             $blog_public = get_option('blog_public'); 
     54 
     55        $wordpress_reset = ( isset( $_POST['wordpress_reset'] ) && $_POST['wordpress_reset'] == 'true' ) ? true : false; 
     56        $wordpress_reset_confirm = ( isset( $_POST['wordpress_reset_confirm'] ) && $_POST['wordpress_reset_confirm'] == 'reset' ) ? true : false; 
     57        $valid_nonce = ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'wordpress_reset' ) ) ? true : false; 
     58 
     59        if ( $wordpress_reset && $wordpress_reset_confirm && $valid_nonce ) { 
     60            require_once( ABSPATH . '/wp-admin/includes/upgrade.php' ); 
     61 
     62            $blogname = get_option( 'blogname' ); 
     63            $admin_email = get_option( 'admin_email' ); 
     64            $blog_public = get_option( 'blog_public' ); 
    5065 
    5166            if ( $current_user->user_login != 'admin' ) 
    52                 $user = get_userdatabylogin('admin'); 
    53             if ( ! isset($user) || $user->user_level < 10 ) 
     67                $user = get_user_by( 'login', 'admin' ); 
     68 
     69            if ( ! isset( $user ) || $user->user_level < 10 ) 
    5470                $user = $current_user; 
    5571 
    56             global $wpdb; 
    57             $tables = $wpdb->get_col("SHOW TABLES LIKE '{$wpdb->prefix}%'"); 
    58             foreach ($tables as $table) { 
    59                 $wpdb->query("DROP TABLE $table"); 
    60             } 
    61  
    62             $result = wp_install($blogname, $user->user_login, $user->user_email, $blog_public); 
    63             extract($result, EXTR_SKIP); 
    64  
    65             $query = $wpdb->prepare("UPDATE $wpdb->users SET user_pass = %s, user_activation_key = '' WHERE ID = %d", $user->user_pass, $user_id); 
    66             $wpdb->query($query); 
    67  
    68             $get_user_meta = function_exists('get_user_meta') ? 'get_user_meta' : 'get_usermeta'; 
    69             $update_user_meta = function_exists('update_user_meta') ? 'update_user_meta' : 'update_usermeta'; 
    70  
    71             if ( $get_user_meta($user_id, 'default_password_nag') ) 
    72                 $update_user_meta($user_id, 'default_password_nag', false); 
    73             if ( $get_user_meta($user_id, $wpdb->prefix . 'default_password_nag') ) 
    74                 $update_user_meta($user_id, $wpdb->prefix . 'default_password_nag', false); 
    75  
    76             if ( $this->auto_reactivate == true ) 
    77                 update_option('active_plugins', array(plugin_basename(__FILE__))); 
     72            global $wpdb, $reactivate_wp_reset_additional; 
     73            $tables = $wpdb->get_col( "SHOW TABLES LIKE '{$wpdb->prefix}%'" ); 
     74            foreach ( $tables as $table ) { 
     75                $wpdb->query( "DROP TABLE $table" ); 
     76            } 
     77 
     78            $result = wp_install( $blogname, $user->user_login, $user->user_email, $blog_public ); 
     79            extract( $result, EXTR_SKIP ); 
     80 
     81            $query = $wpdb->prepare( "UPDATE $wpdb->users SET user_pass = %s, user_activation_key = '' WHERE ID = %d", $user->user_pass, $user_id ); 
     82            $wpdb->query( $query ); 
     83 
     84            $get_user_meta = function_exists( 'get_user_meta' ) ? 'get_user_meta' : 'get_usermeta'; 
     85            $update_user_meta = function_exists( 'update_user_meta' ) ? 'update_user_meta' : 'update_usermeta'; 
     86 
     87            if ( $get_user_meta( $user_id, 'default_password_nag' ) ) 
     88                $update_user_meta( $user_id, 'default_password_nag', false ); 
     89 
     90            if ( $get_user_meta( $user_id, $wpdb->prefix . 'default_password_nag' ) ) 
     91                $update_user_meta( $user_id, $wpdb->prefix . 'default_password_nag', false ); 
     92 
     93            if ( defined( 'REACTIVATE_WP_RESET' ) && REACTIVATE_WP_RESET === true ) 
     94                @activate_plugin( plugin_basename( __FILE__ ) ); 
     95 
     96            if ( ! empty( $reactivate_wp_reset_additional ) ) { 
     97                foreach ( $reactivate_wp_reset_additional as $plugin ) { 
     98                    $plugin = plugin_basename( $plugin ); 
     99                    if ( ! is_wp_error( validate_plugin( $plugin ) ) ) 
     100                        @activate_plugin( $plugin ); 
     101                } 
     102            } 
    78103 
    79104            wp_clear_auth_cookie(); 
    80             wp_set_auth_cookie($user_id); 
    81  
    82             wp_redirect(admin_url() . '?reset'); 
     105            wp_set_auth_cookie( $user_id ); 
     106 
     107            wp_redirect( admin_url() . '?reset' ); 
    83108            exit(); 
    84109        } 
    85110 
    86         if ( array_key_exists('reset', $_GET) && stristr($_SERVER['HTTP_REFERER'], 'wordpress-reset') ) { 
    87             $user = get_userdata(1); 
    88             add_action('admin_notices',create_function('$a', 'echo \'<div id="message" class="updated fade"><p><strong>WordPress has been reset back to defaults.  The user "' . $user->user_login . '" was recreated with its previous password.</strong></p></div>\';')); 
    89             do_action('wordpress_reset_post', $user); 
     111        if ( array_key_exists( 'reset', $_GET ) && stristr( $_SERVER['HTTP_REFERER'], 'wordpress-reset' ) ) { 
     112            add_action( 'admin_notices', function () { 
     113                $user = get_user_by( 'id', 1 ); 
     114                echo '<div id="message" class="updated fade"><p><strong>WordPress has been reset back to defaults. The user "' . $user->user_login . '" was recreated with its previous password.</strong></p></div>'; 
     115                do_action( 'wordpress_reset_post', $user ); 
     116            } ); 
    90117        } 
    91118    } 
    92119 
    93120    // Overwrite the password, because we actually reset it after this email goes out 
    94     function hijack_mail($args) { 
    95         if ( stristr($args['message'], 'Your new WordPress blog has been successfully set up at') ) 
    96             $args['message'] = preg_replace('/Password:.+/', 'Password: Previously specified password', $args['message']); 
     121    function hijack_mail( $args ) { 
     122        if ( preg_match( '/Your new WordPress (blog|site) has been successfully set up at/i', $args['message'] ) ) { 
     123            $args['message'] = str_replace( 'Your new WordPress site has been successfully set up at:', 'Your WordPress site has been successfully reset, and can be accessed at:', $args['message'] ); 
     124            $args['message'] = preg_replace( '/Password:.+/', 'Password: previously specified password', $args['message'] ); 
     125        } 
    97126        return $args; 
    98127    } 
    99128 
    100     // admin_head action hook operations 
     129    // admin_print_scripts action hook operations 
    101130    // Enqueue jQuery to the head 
    102     function head() { 
    103         wp_enqueue_script('jquery'); 
     131    function admin_js() { 
     132        wp_enqueue_script( 'jquery' ); 
    104133    } 
    105134 
    106135    // admin_footer action hook operations 
    107136    // Do some jQuery stuff to warn the user before submission 
    108     function footer() { 
     137    function footer_js() { 
    109138    ?> 
    110139    <script type="text/javascript"> 
     
    112141        jQuery('#wordpress_reset_submit').click(function(){ 
    113142            if ( jQuery('#wordpress_reset_confirm').val() == 'reset' ) { 
    114                 var message = 'This action is not reversable.\n\nClicking "OK" will reset your database back to it\'s defaults.  Click "Cancel" to abort.' 
     143                var message = 'This action is not reversable.\n\nClicking "OK" will reset your database back to it\'s defaults. Click "Cancel" to abort.' 
    115144                var reset = confirm(message); 
    116145                if ( reset ) { 
     
    121150                } 
    122151            } else { 
    123                 alert('Invalid confirmation word.  Please type the word \'reset\' in the confirmation field.'); 
     152                alert('Invalid confirmation word. Please type the word \'reset\' in the confirmation field.'); 
    124153                return false; 
    125154            } 
     
    133162    // Add the settings page 
    134163    function add_page() { 
    135         if ( current_user_can('level_10') && function_exists('add_management_page') ) : 
    136             add_management_page('WordPress Reset', 'WordPress Reset', 'level_10', 'wordpress-reset', array(&$this, 'admin_page')); 
    137         endif; 
     164        if ( current_user_can( 'level_10' ) && function_exists( 'add_management_page' ) ) 
     165            $hook = add_management_page( 'Reset', 'Reset', 'level_10', 'wordpress-reset', array( &$this, 'admin_page' ) ); 
     166            add_action( "admin_print_scripts-{$hook}", array( &$this, 'admin_js' ) ); 
     167            add_action( "admin_footer-{$hook}", array( &$this, 'footer_js' ) ); 
    138168    } 
    139169 
     
    141171    // The settings page 
    142172    function admin_page() { 
    143         global $current_user; 
    144         if ( isset($_POST['wordpress_reset_confirm']) && $_POST['wordpress_reset_confirm'] != 'reset' ) 
    145             echo '<div id="message" class="error fade"><p><strong>Invalid confirmation word.  Please type the word \'reset\' in the confirmation field.</strong></p></div>'; 
     173        global $current_user, $reactivate_wp_reset_additional; 
     174        if ( isset( $_POST['wordpress_reset_confirm'] ) && $_POST['wordpress_reset_confirm'] != 'reset' ) 
     175            echo '<div class="error fade"><p><strong>Invalid confirmation word. Please type the word \'reset\' in the confirmation field.</strong></p></div>'; 
     176        elseif ( isset( $_POST['_wpnonce'] ) ) 
     177            echo '<div class="error fade"><p><strong>Invalid nonce. Please try again.</strong></p></div>'; 
     178 
     179        $missing = array(); 
     180        if ( ! empty( $reactivate_wp_reset_additional ) ) { 
     181            foreach ( $reactivate_wp_reset_additional as $key => $plugin ) { 
     182                if ( is_wp_error( validate_plugin( $plugin ) ) ) { 
     183                    unset( $reactivate_wp_reset_additional[$key] ); 
     184                    $missing[] = $plugin; 
     185                } 
     186            } 
     187        } 
     188 
     189        $will_reactivate = ( defined( 'REACTIVATE_WP_RESET') && REACTIVATE_WP_RESET === true ) ? true : false; 
    146190    ?> 
    147191    <div class="wrap"> 
    148         <h2>WordPress Reset</h2> 
    149         <br /> 
     192        <div id="icon-tools" class="icon32"><br /></div> 
     193        <h2>Reset</h2> 
     194        <h3>Details about the reset</h3> 
     195        <p><strong>After completing this reset you will taken to the dashboard.</strong></p> 
     196        <?php $admin = get_user_by( 'login', 'admin' ); ?> 
     197        <?php if ( ! isset( $admin->user_login ) || $admin->user_level < 10 ) : $user = $current_user; ?> 
     198        <p>The 'admin' user does not exist. The user '<strong><?php echo esc_html( $user->user_login ); ?></strong>' will be recreated with its <strong>current password</strong> with user level 10.</p> 
     199        <?php else : ?> 
     200        <p>The '<strong>admin</strong>' user exists and will be recreated with its <strong>current password</strong>.</p> 
     201        <?php endif; ?> 
     202        <p>This plugin <strong>will<?php echo $will_reactivate ? '' : ' not';?> be automatically reactivated</strong> after the reset. <?php echo ! $will_reactivate ? "To have this plugin auto-reactivate add <span class='code'>define( 'REACTIVATE_WP_RESET', true );</span> to <span class='code'>wp-config.php</span>." : ''; ?></p> 
     203        <?php if ( ! empty( $reactivate_wp_reset_additional ) ) : ?> 
     204        <p>The following <strong>additional plugins will be reactivated</strong>: 
     205            <ul style="list-style-type: disc;"> 
     206            <?php foreach ( $reactivate_wp_reset_additional as $plugin ) : $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); ?> 
     207                <li style="margin: 5px 0 0 30px;"><strong><?php echo esc_html( $plugin_data['Name'] ); ?></strong></li> 
     208            <?php endforeach; unset( $reactivate_wp_reset_additional, $plugin, $plugin_data ); ?> 
     209            </ul> 
     210        </p> 
     211        <?php endif; ?> 
     212        <?php if ( ! empty( $missing ) ) : ?> 
     213        <p>The following <strong>additional plugins are missing</strong> and cannot be reactivated: 
     214            <ul style="list-style-type: disc;"> 
     215            <?php foreach ( $missing as $plugin ) : ?> 
     216                <li style="margin: 5px 0 0 30px;"><strong><?php echo esc_html( $plugin ); ?></strong></li> 
     217            <?php endforeach; unset( $missing, $plugin ); ?> 
     218            </ul> 
     219        </p> 
     220        <?php endif; ?> 
     221        <h3>Reset</h3> 
     222        <p>Type '<strong>reset</strong>' in the confirmation field to confirm the reset and then click the reset button:</p> 
    150223        <form id="wordpress_reset_form" action="" method="post"> 
     224            <?php wp_nonce_field( 'wordpress_reset' ); ?> 
    151225            <input id="wordpress_reset" type="hidden" name="wordpress_reset" value="true" /> 
    152             Type 'reset' in the confirmation field to confirm the reset and then click the reset button:<br /><br /> 
    153             <input id="wordpress_reset_confirm" type="text" name="wordpress_reset_confirm" value="" />&nbsp;&nbsp;<input id="wordpress_reset_submit" type="submit" name="Submit" class="button-primary" value="Reset" /><br /><br /> 
    154             <ul> 
    155                 <li><strong>After completing this reset you will taken to the dashboard.</strong></li> 
    156 <?php 
    157             $admin = get_userdatabylogin('admin'); 
    158             if ( ! isset($admin->user_login) || $admin->user_level < 10 ) : 
    159                 $user = $current_user; 
    160 ?> 
    161                 <li>The 'admin' user does not exist.  The user '<strong><?php echo $user->user_login; ?></strong>' will be recreated with its current password with user level 10.</li> 
    162             <?php else : ?> 
    163                 <li>The '<strong>admin</strong>' user exists and will be recreated with its current password.</li> 
    164             <?php endif; ?> 
    165                 <li>This plugin <strong>will<?php echo $this->auto_reactivate ? '</strong> ' : ' not</strong> ';?>be automatically reactivated after the reset.</li> 
    166             </ul> 
     226            <input id="wordpress_reset_confirm" type="text" name="wordpress_reset_confirm" value="" /> 
     227            <p class="submit"> 
     228                <input id="wordpress_reset_submit" style="width: 80px;" type="submit" name="Submit" class="button-primary" value="Reset" /> 
     229            </p> 
    167230        </form> 
    168231    </div> 
     
    176239// End if for is_admin 
    177240endif; 
    178 ?> 
Note: See TracChangeset for help on using the changeset viewer.