_nonce) ) { require_once( ABSPATH . '/wp-admin/includes/upgrade.php' ); $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; // 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); } // Reactivate the plugin after reinstalling if ( isset($_POST['wp-reset-check']) && $_POST['wp-reset-check'] == 'true' ) { update_option('active_plugins', array(plugin_basename(__FILE__))); 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(); ?>

.

.

:  

_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')); } } /** * Adds the contextual help for our plugin page * * @access public * @param $contextual_help Hook text to display * @param $screen_id ID of the current admin screen * @return $contextual_help String The help text */ function add_contextual_help($contextual_help, $screen_id) { if ($screen_id == $this->_hook) { $contextual_help = '

' . __('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; } /** * 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 WordPress database has been successfully reset to its 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; } /** * 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) ) { // Set the default_password_nag to nothing // 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;