_wp_tables = $wpdb->tables(); // Check for valid input - goes ahead and drops / resets tables if ( isset( $_POST['wp-random-value'], $_POST['wp-reset-input'] ) && $_POST['wp-random-value'] == $_POST['wp-reset-input'] && check_admin_referer( 'wp-nonce-submit', $this->_nonce ) ) { require_once( ABSPATH . '/wp-admin/includes/upgrade.php' ); // No tables were selected if ( empty( $_POST['tables'] ) ) { wp_redirect( admin_url( $pagenow ) . '?page=wp-reset&reset=no-select' ); exit(); } // Get current options $blog_title = get_option( 'blogname' ); $public = get_option( 'blog_public' ); $admin_user = get_user_by( 'login', 'admin' ); $user = ( ! $admin_user || ! user_can( $admin_user->ID, 'update_core' ) ) ? $current_user : $admin_user; // Get the selected tables $tables = ( isset( $_POST['tables'] ) ) ? array_flip( $_POST['tables'] ) : array(); // Compare the selected tables against the ones in the database $this->_tables = array_diff_key( $this->_wp_tables, $tables ); // Preserve the data from the tables that are unique if ( 0 < count( $this->_tables ) ) { $backup_tables = $this->_back_up_tables( $this->_tables ); } // Grab the currently active plugins and theme if ( ! empty($_POST['wp-reset-check']) ) { $current_data = array( 'active-plugins' => get_option( 'active_plugins' ), 'current-theme' => get_option( 'current_theme' ), 'template' => get_option( 'template' ), 'stylesheet' => get_option( 'stylesheet' ) ); } // Run through the database columns, drop all the tables and // install wp with previous settings if ( ! empty( $this->_wp_tables ) ) { foreach ( $this->_wp_tables as $wp_table ) { $wpdb->query( "DROP TABLE {$wp_table}" ); } $keys = wp_install( $blog_title, $user->user_login, $user->user_email, $public ); $this->_wp_update_user( $user, $keys ); } // Delete and replace tables with the backed up table data if ( ! empty( $backup_tables ) ) { foreach ( $this->_tables as $table ) { $wpdb->query( "DELETE FROM " . $table ); } $this->_restore_tables( $backup_tables ); } if ( get_user_meta( $current_user->ID, 'session_tokens' ) ) { delete_user_meta( $current_user->ID, 'session_tokens' ); } wp_clear_auth_cookie(); wp_set_auth_cookie( $current_user->ID ); if ( ! empty( $current_data ) ) { update_option( 'active_plugins', $current_data['active-plugins'] ); if ( ! empty( $current_data['current-theme'] ) ) { update_option( 'current_theme', $current_data['current-theme'] ); } update_option( 'template', $current_data['template'] ); update_option( 'stylesheet', $current_data['stylesheet'] ); wp_redirect( admin_url( $pagenow ) . '?page=wp-reset&reset=success' ); exit(); } wp_redirect( admin_url() ); exit(); } } /** * Displays the admin page * * @access public * @return void */ function show_admin_page() { global $current_user; // Return to see if admin object exists $admin_user = get_user_by( 'login', 'admin' ); $random_string = wp_generate_password( 5, false ); ?>

.

.

.

:

:  

_nonce ) ?>
ID, 'update_core' ) ) : ?>

admin was never created for this WordPress install. So %s will be recreated with its current password instead', 'wp-reset' ), $current_user->user_login ) ?>.

admin will be recreated with its current password upon resetting', 'wp-reset' ) ?>.

_hook = add_submenu_page( 'tools.php', 'Database Reset', 'Database Reset', 'update_core', 'wp-reset', array( $this, 'show_admin_page' ) ); add_action( 'load-' . $this->_hook, array( $this, 'option_page_actions' ) ); } } /** * Fires actions on option page load * * @access public * @return void */ function option_page_actions() { add_action( 'admin_enqueue_scripts', array( $this, 'add_plugin_styles_and_scripts' ) ); add_action( 'admin_footer', array( $this, 'add_admin_javascript' ) ); $this->_add_help_screen(); } /** * Adds v3.3 style help menu for plugin page * * @access private * @return void */ function _add_help_screen() { get_current_screen()->add_help_tab( array( 'id' => 'overview', 'title' => __( 'Overview' ), 'content' => '

' . __( 'This plugin allows you to securely and easily reinitialize the WordPress database back to its default settings without actually having to reinstall WordPress from scratch. This plugin will come in handy for both theme and plugin developers. Two possible use case scenarios would be to:') . '

' . '

' . __( '1. Erase excess junk in the wp_options table that accumulates over time.
2. Revert back to a fresh install of the WordPress database after experimenting with various back-end options.' ) . '

' ) ); get_current_screen()->add_help_tab( array( 'id' => 'instructions', 'title' => __( 'Instructions' ), 'content' => '

' . __( 'Performing a database reset is quite straightforward.' ) . '

' . '

' . __( 'Select the different tables you would like to reinitialize from the drop down list. You can select any number of tables. If you know you would like to reset the entire database, simply click the Select All button.' ) . '

' . '

' . __( 'Next you will have to enter the auto generated value into the text box. Clicking on the Reset Database button will result in a pop-up.' ) . '

' . '

' . __( 'Once you are sure you would like to proceed, click OK to reset.' ) . '

' ) ); get_current_screen()->set_help_sidebar( '

' . __( 'Contact information:' ) . '

' . '

' . __( 'Any ideas on features or ways to improve this plugin? Contact me at GitHub or Twitter.' ) . '

' ); } /** * Adds any plugin styles to our page * * @access public * @return void */ function add_plugin_styles_and_scripts() { wp_enqueue_style( 'wordpress-reset-css', plugins_url( 'css/wp-reset.css', __FILE__ ) ); wp_enqueue_style( 'bsmselect-css', plugins_url( 'css/jquery.bsmselect.css', __FILE__ ) ); wp_enqueue_script( 'bsmselect', plugins_url( 'js/jquery.bsmselect.js', __FILE__ ) ); wp_enqueue_script( 'bsmselect-compatibility', plugins_url( 'js/jquery.bsmselect.compatibility.js', __FILE__ ) ); } /** * Load language path * * @access public * @return void */ function init_language() { $language_dir = basename( dirname( __FILE__ ) ) . '/languages'; load_plugin_textdomain( 'wp-reset', false, $language_dir ); } /** * For activation hook * * @access public * @return void */ function plugin_activate() { add_option( 'wp-reset-activated', true ); } /** * Redirects the user after the plugin is activated * * @access private * @return void */ function _redirect_user() { if ( get_option( 'wp-reset-activated', false ) ) { delete_option( 'wp-reset-activated' ); wp_redirect( admin_url( 'tools.php' ) . '?page=wp-reset' ); } } /** * Changes the password to a sentence rather than * an auto-generated password that is sent by email * right after the installation is complete * * @access private * @return $mail Version with password changed */ function _fix_mail( $mail ) { $subject = __( 'WordPress Database Reset', 'wp-reset' ); $message = __( 'The tables you selected have been successfully reset to their default settings:', 'wp-reset' ); $password = __( 'Password: The password you chose during the install.', 'wp-reset' ); if ( stristr( $mail['message'], 'Your new WordPress site has been successfully set up at:' ) ) { $mail['subject'] = preg_replace( '/New WordPress Site/', $subject, $mail['subject'] ); $mail['message'] = preg_replace( '/Your new WordPress site has been successfully set up at:+/', $message, $mail['message'] ); $mail['message'] = preg_replace( '/Password:\s.+/', $password, $mail['message'] ); } return $mail; } /** * Preserves all the results from the tables the user * did not select from the drop-down. * * @access private * @return array Backed up data if type backup */ function _back_up_tables( $tables ) { global $wpdb; if ( is_array( $tables ) ) { $backup_tables = array(); foreach ( $tables as $table ) { $backup_tables[$table] = $wpdb->get_results( "SELECT * FROM " . $table ); } return $backup_tables; } return; } /** * Restores the data from the tables the user did not * select during the backing up phase. * * @access private * @return void */ function _restore_tables( $tables ) { global $wpdb; if ( is_array( $tables ) ) { foreach ( $tables as $table_name => $table_data ) { foreach ( $table_data as $row ) { $columns = $values = array(); foreach ( $row as $column => $value ) { $columns[] = $column; $values[] = esc_sql( $value ); } $wpdb->query( "INSERT INTO $table_name (" . implode( ', ', $columns ) . ") VALUES ('" . implode( "', '", $values ) . "')" ); } } } return; } /** * Updates the user password and clears / sets * the authentication cookie for the user * * @access private * @param $user Current or admin user * @param $keys Array returned by wp_install() * @return true on install success, false otherwise */ function _wp_update_user( $user, $keys ) { global $wpdb; extract( $keys, EXTR_SKIP ); $query = $wpdb->prepare( "UPDATE $wpdb->users SET user_pass = '%s', user_activation_key = '' WHERE ID = '%d'", $user->user_pass, $user_id ); if ( $wpdb->query( $query ) ) { // Remove password reminder after installing if ( get_user_meta( $user_id, 'default_password_nag' ) ) { delete_user_meta( $user_id, 'default_password_nag' ); } return true; } return false; } } $cb_wp_reset = new CB_WP_Reset(); register_activation_hook( __FILE__, array( 'cb_wp_reset', 'plugin_activate' ) ); endif;