_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_userdatabylogin('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' ) { $this->_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 ( count($this->_tables) > 0 ) { foreach ($this->_tables as $table) { $wpdb->query("DELETE FROM " . $table); } $this->_backup_tables($backup_tables, 'reset'); } if ( ! empty($this->_active_plugins) ) { $wpdb->update($wpdb->options, array('option_value' => $this->_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_userdatabylogin('admin'); // Generate a random value for the input box $random_string = $this->_rand_string(); ?>
.
.
.
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') ?>.
' . __('Have any cool ideas for this plugin? Contact me either by Twitter or by GitHub.', 'wp-reset') . '
'; $contextual_help .= '' . __('If this plugin becomes non-functional in any way due to WordPress upgrades, rest assured I will update it.', 'wp-reset') . '
'; $contextual_help .= '' . __('Goodbye for now.', 'wp-reset') . '
'; } return $contextual_help; } /** * 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__)); wp_enqueue_script('bsmselect-sortable', plugins_url('js/jquery.bsmselect.sortable.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; } /** * Generates a random value for our input box * * @access private * @param $length Length of the random string value * @return $random_string */ function _rand_string($length = 5) { $random_string = ''; $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $size = strlen($chars); for ($i = 0; $i < $length; $i++) { $random_string .= $chars[rand(0, $size-1)]; } return $random_string; } } $cb_wp_reset = new cb_wp_reset(); register_activation_hook( __FILE__, array('cb_wp_reset', 'plugin_activate') ); endif;