_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 ( ! isset($_POST['tables']) && 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 ( count($this->_tables) > 0 ) { $backup_tables = $this->_backup_tables($this->_tables); } // Grab the currently active plugins if ( isset($_POST['wp-reset-check']) && $_POST['wp-reset-check'] == 'true' ) { $active_plugins = $wpdb->get_var($wpdb->prepare("SELECT option_value FROM $wpdb->options WHERE option_name = %s", 'active_plugins')); } // Run through the database columns and drop all the tables if ( $db_tables = $wpdb->get_col("SHOW TABLES LIKE '{$wpdb->prefix}%'") ) { foreach ($db_tables as $db_table) { $wpdb->query("DROP TABLE {$db_table}"); } // Return user keys and import variables $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 ( $backup_tables ) { foreach ($this->_tables as $table) { $wpdb->query("DELETE FROM " . $table); } $this->_backup_tables($backup_tables, 'reset'); } if ( ! empty($active_plugins) ) { $wpdb->update($wpdb->options, array('option_value' => $active_plugins), array('option_name' => 'active_plugins')); wp_redirect(admin_url($pagenow) . '?page=wp-reset&reset=success'); exit(); } // If the wp-reset-check isn't checked just redirect user to dashboard 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'); // Generate a random value for the input box $random_string = wp_generate_password(5, false); ?>
.
.
.
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') ?>.
' . __( 'Thank you for downloading the WordPress Database Reset plugin.' ) . '
'; $help .= '' . __( '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:') . '
'; $help .= '' . __( '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.' ) . '
' . __( 'You can learn more on how to use this plugin by clicking the Instructions tab to the left.' ) . '
' . __( 'Performing a database reset is quite straightforward.' ) . '
'; $help .= '' . __( '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.' ) . '
' . __( '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. Also resets these * results back after reinstalling WordPress. * * @access private * @return array Backed up data if type backup, void if reset */ function _backup_tables($tables, $type = 'backup') { global $wpdb; if ( is_array($tables) ) { switch ($type) { case 'backup': $backup_tables = array(); foreach ($tables as $table) { $backup_tables[$table] = $wpdb->get_results("SELECT * FROM " . $table); } return $backup_tables; break; case 'reset': // Outer array of tables foreach ($tables as $table_name => $table_data) { // Array of table rows foreach ($table_data as $row) { $columns = $values = array(); // Loop through current object row foreach ($row as $column => $value) { $columns[] = $column; $values[] = $wpdb->escape($value); } $wpdb->query("INSERT INTO $table_name (" . implode(', ', $columns) . ") VALUES ('" . implode("', '", $values) . "')"); } } break; } } 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); // Set the old password back to the user $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) ) { // Delete the default_password_nag // so it doesn't pop up with the password reminder after installing if ( get_user_meta($user_id, 'default_password_nag') ) delete_user_meta($user_id, 'default_password_nag'); wp_clear_auth_cookie(); wp_set_auth_cookie($user_id); return TRUE; } return FALSE; } } $cb_wp_reset = new cb_wp_reset(); register_activation_hook( __FILE__, array('cb_wp_reset', 'plugin_activate') ); endif;