| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: WP Reset |
| 4 |
Plugin URI: https://github.com/chrisberthe/wordpress-database-reset |
| 5 |
Description: A plugin that allows you to reset the database to WordPress's initial state. |
| 6 |
Version: 1.2.2 |
| 7 |
Author: Chris Berthe |
| 8 |
Author URI: https://github.com/chrisberthe |
| 9 |
License: GNU General Public License |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( ! class_exists('cb_wp_reset') && is_admin() ) : |
| 13 |
|
| 14 |
class cb_wp_reset |
| 15 |
{ |
| 16 |
/** |
| 17 |
* Nonce value |
| 18 |
*/ |
| 19 |
private $_nonce = 'wp-reset-nonce'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Loads default options |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
function __construct() |
| 27 |
{ |
| 28 |
add_action('init', array($this, 'init_language')); |
| 29 |
add_action('admin_init', array($this, 'wp_reset_init')); |
| 30 |
add_action('admin_init', array($this, '_redirect_user')); |
| 31 |
add_action('admin_footer', array($this, 'add_admin_javascript')); |
| 32 |
add_action('admin_menu', array($this, 'add_admin_menu')); |
| 33 |
add_filter('contextual_help', array($this, 'add_contextual_help'), 10, 2); |
| 34 |
add_filter('wp_mail', array($this, '_fix_mail')); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Handles the admin page functionality |
| 39 |
* |
| 40 |
* @access public |
| 41 |
* @uses wp_install Located in includes/upgrade.php (line 22) |
| 42 |
*/ |
| 43 |
function wp_reset_init() |
| 44 |
{ |
| 45 |
global $wpdb, $current_user, $pagenow; |
| 46 |
|
| 47 |
if ( isset($_POST['wp-random-value'], $_POST['wp-reset-input']) && $_POST['wp-random-value'] == $_POST['wp-reset-input'] |
| 48 |
&& check_admin_referer('wp-nonce-submit', $this->_nonce) ) |
| 49 |
{ |
| 50 |
require_once( ABSPATH . '/wp-admin/includes/upgrade.php' ); |
| 51 |
|
| 52 |
$blog_title = get_option('blogname'); |
| 53 |
$public = get_option('blog_public'); |
| 54 |
|
| 55 |
$admin_user = get_userdatabylogin('admin'); |
| 56 |
$user = ( ! $admin_user || ! user_can( $admin_user->ID, 'update_core' ) ) ? $current_user : $admin_user; |
| 57 |
|
| 58 |
// Run through the database columns and drop all the tables |
| 59 |
if ( $db_tables = $wpdb->get_col("SHOW TABLES LIKE '{$wpdb->prefix}%'") ) |
| 60 |
{ |
| 61 |
foreach ( $db_tables as $db_table ) |
| 62 |
{ |
| 63 |
$wpdb->query("DROP TABLE {$db_table}"); |
| 64 |
} |
| 65 |
|
| 66 |
// Return user keys and import variables |
| 67 |
$keys = wp_install($blog_title, $user->user_login, $user->user_email, $public); |
| 68 |
$this->_wp_update_user($user, $keys); |
| 69 |
} |
| 70 |
|
| 71 |
// Reactivate the plugin after reinstalling |
| 72 |
if ( isset($_POST['wp-reset-check']) && $_POST['wp-reset-check'] == 'true' ) |
| 73 |
{ |
| 74 |
update_option('active_plugins', array(plugin_basename(__FILE__))); |
| 75 |
wp_redirect(admin_url($pagenow) . '?page=wp-reset&reset=success'); exit(); |
| 76 |
} |
| 77 |
|
| 78 |
// If the wp-reset-check isn't checked just redirect user to dashboard |
| 79 |
wp_redirect(admin_url()); exit(); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Displays the admin page |
| 85 |
* |
| 86 |
* @access public |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
function show_admin_page() |
| 90 |
{ |
| 91 |
global $current_user; |
| 92 |
|
| 93 |
// Return to see if admin object exists |
| 94 |
$admin_user = get_userdatabylogin('admin'); |
| 95 |
|
| 96 |
// Generate a random value for the input box |
| 97 |
$random_string = $this->_rand_string(); |
| 98 |
?> |
| 99 |
<?php if ( isset($_POST['wp-random-value'], $_POST['wp-reset-input']) && $_POST['wp-random-value'] != $_POST['wp-reset-input'] ) : ?> |
| 100 |
<div class="error"><p><strong><?php _e('You entered the wrong value - please try again', 'wp-reset') ?>.</strong></p></div> |
| 101 |
<?php elseif ( isset($_GET['reset']) && $_GET['reset'] == 'success' ) : ?> |
| 102 |
<div class="updated"><p><strong><?php _e('The WordPress database has been reset successfully', 'wp-reset') ?>.</strong></p></div> |
| 103 |
<?php endif ?> |
| 104 |
|
| 105 |
<div class="wrap"> |
| 106 |
<?php screen_icon() ?> |
| 107 |
<h2><?php _e('Database Reset', 'wp-reset') ?></h2> |
| 108 |
<p><?php _e('Please type in (or copy/paste) the generated value into the text box', 'wp-reset') ?>: <strong><?php echo $random_string ?></strong></p> |
| 109 |
<form action="" method="POST" id="wp-reset-form"> |
| 110 |
<?php wp_nonce_field('wp-nonce-submit', $this->_nonce) ?> |
| 111 |
<input type="hidden" name="wp-random-value" value="<?php echo $random_string ?>" id="wp-random-value" /> |
| 112 |
<input type="text" name="wp-reset-input" value="" id="wp-reset-input" /> |
| 113 |
<input type="submit" name="wp-reset-submit" value="<?php _e('Reset Database', 'wp-reset') ?>" id="wp-reset-submit" class="button-primary" /> |
| 114 |
<p> |
| 115 |
<label for="wp-reset-check"> |
| 116 |
<input type="checkbox" name="wp-reset-check" id="wp-reset-check" checked="checked" value="true" /> |
| 117 |
<?php _e('Reactivate plugin after resetting?', 'wp-reset') ?> |
| 118 |
</label> |
| 119 |
</p> |
| 120 |
</form> |
| 121 |
|
| 122 |
<?php if ( ! $admin_user || ! user_can( $admin_user->ID, 'update_core' ) ) : ?> |
| 123 |
<p style="margin-top: 25px"><?php printf(__('The default user <strong><u>admin</u></strong> was never created for this WordPress install. So <strong><u>%s</u></strong> will be recreated with its current password instead', 'wp-reset'), $current_user->user_login) ?>.</p> |
| 124 |
<?php else : ?> |
| 125 |
<p><?php _e('The default user <strong><u>admin</u></strong> will be recreated with its current password upon resetting', 'wp-reset') ?>.</p> |
| 126 |
<?php endif; ?> |
| 127 |
|
| 128 |
<p><?php _e('Note that once you reset the database, all users will be deleted except the initial admin user.', 'wp-reset') ?></p> |
| 129 |
</div> |
| 130 |
<?php } |
| 131 |
|
| 132 |
/** |
| 133 |
* Add JavaScript to the bottom of the plugin page |
| 134 |
* |
| 135 |
* @access public |
| 136 |
* @return bool TRUE on reset confirmation |
| 137 |
*/ |
| 138 |
function add_admin_javascript() |
| 139 |
{ |
| 140 |
?> |
| 141 |
<script type="text/javascript"> |
| 142 |
/* <![CDATA[ */ |
| 143 |
jQuery('#wp-reset-submit').click(function() { |
| 144 |
var message = "<?php _e('Clicking OK will result in your database being reset to its initial settings. Continue?', 'wp-reset') ?>"; |
| 145 |
var reset = confirm(message); |
| 146 |
|
| 147 |
if ( reset ) { |
| 148 |
jQuery('#wp-reset-form').submit(); |
| 149 |
} else { |
| 150 |
return false; |
| 151 |
} |
| 152 |
}); |
| 153 |
/* ]]> */ |
| 154 |
</script> |
| 155 |
<?php |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Adds our submenu item to the Tools menu |
| 160 |
* |
| 161 |
* @access public |
| 162 |
* @return void |
| 163 |
*/ |
| 164 |
function add_admin_menu() |
| 165 |
{ |
| 166 |
if ( current_user_can( 'update_core' ) ) |
| 167 |
{ |
| 168 |
$this->_hook = add_submenu_page('tools.php', 'Database Reset', 'Database Reset', 'update_core', 'wp-reset', array($this, 'show_admin_page')); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Adds the contextual help for our plugin page |
| 174 |
* |
| 175 |
* @access public |
| 176 |
* @param $contextual_help Hook text to display |
| 177 |
* @param $screen_id ID of the current admin screen |
| 178 |
* @return $contextual_help String The help text |
| 179 |
*/ |
| 180 |
function add_contextual_help($contextual_help, $screen_id) |
| 181 |
{ |
| 182 |
if ($screen_id == $this->_hook) |
| 183 |
{ |
| 184 |
$contextual_help = '<p>' . __('Have any cool ideas for this plugin? Contact me either by <a href="http://twitter.com/#!/chrisberthe">Twitter</a> or by <a href="https://github.com/chrisberthe">GitHub</a>.', 'wp-reset') . '</p>'; |
| 185 |
$contextual_help .= '<p>' . __('If this plugin becomes non-functional in any way due to WordPress upgrades, rest assured I will update it.', 'wp-reset') . '</p>'; |
| 186 |
$contextual_help .= '<p>' . __('Goodbye for now.', 'wp-reset') . '</p>'; |
| 187 |
} |
| 188 |
|
| 189 |
return $contextual_help; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Load language path |
| 194 |
* |
| 195 |
* @access public |
| 196 |
* @return void |
| 197 |
*/ |
| 198 |
function init_language() |
| 199 |
{ |
| 200 |
$language_dir = basename(dirname(__FILE__)) . '/languages'; |
| 201 |
load_plugin_textdomain('wp-reset', FALSE, $language_dir); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* For activation hook |
| 206 |
* |
| 207 |
* @access public |
| 208 |
* @return void |
| 209 |
*/ |
| 210 |
function plugin_activate() |
| 211 |
{ |
| 212 |
add_option('wp-reset-activated', true); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Redirects the user after the plugin is activated |
| 217 |
* |
| 218 |
* @access private |
| 219 |
* @return void |
| 220 |
*/ |
| 221 |
function _redirect_user() |
| 222 |
{ |
| 223 |
if ( get_option('wp-reset-activated', false) ) |
| 224 |
{ |
| 225 |
delete_option('wp-reset-activated'); |
| 226 |
wp_redirect(admin_url('tools.php') . '?page=wp-reset'); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Changes the password to a sentence rather than |
| 232 |
* an auto-generated password that is sent by email |
| 233 |
* right after the installation is complete |
| 234 |
* |
| 235 |
* @access private |
| 236 |
* @return $mail Version with password changed |
| 237 |
*/ |
| 238 |
function _fix_mail($mail) |
| 239 |
{ |
| 240 |
$subject = __('WordPress Database Reset', 'wp-reset'); |
| 241 |
$message = __('The WordPress database has been successfully reset to its default settings:', 'wp-reset'); |
| 242 |
$password = __('Password: The password you chose during the install.', 'wp-reset'); |
| 243 |
|
| 244 |
if ( stristr($mail['message'], 'Your new WordPress site has been successfully set up at:') ) |
| 245 |
{ |
| 246 |
$mail['subject'] = preg_replace('/New WordPress Site/', $subject, $mail['subject']); |
| 247 |
$mail['message'] = preg_replace('/Your new WordPress site has been successfully set up at:+/', $message, $mail['message']); |
| 248 |
$mail['message'] = preg_replace('/Password:\s.+/', $password, $mail['message']); |
| 249 |
} |
| 250 |
|
| 251 |
return $mail; |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Updates the user password and clears / sets |
| 256 |
* the authentication cookie for the user |
| 257 |
* |
| 258 |
* @access private |
| 259 |
* @param $user Current or admin user |
| 260 |
* @param $keys Array returned by wp_install() |
| 261 |
* @return TRUE on install success, FALSE otherwise |
| 262 |
*/ |
| 263 |
function _wp_update_user($user, $keys) |
| 264 |
{ |
| 265 |
global $wpdb; |
| 266 |
extract($keys, EXTR_SKIP); |
| 267 |
|
| 268 |
// Set the old password back to the user |
| 269 |
$query = $wpdb->prepare("UPDATE $wpdb->users SET user_pass = '%s', user_activation_key = '' WHERE ID = '%d'", $user->user_pass, $user_id); |
| 270 |
|
| 271 |
if ( $wpdb->query($query) ) |
| 272 |
{ |
| 273 |
// Set the default_password_nag to nothing |
| 274 |
// so it doesn't pop up with the password reminder after installing |
| 275 |
if ( get_user_meta($user_id, 'default_password_nag') ) delete_user_meta($user_id, 'default_password_nag'); |
| 276 |
|
| 277 |
wp_clear_auth_cookie(); |
| 278 |
wp_set_auth_cookie($user_id); |
| 279 |
|
| 280 |
return TRUE; |
| 281 |
} |
| 282 |
|
| 283 |
return FALSE; |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Generates a random value for our input box |
| 288 |
* |
| 289 |
* @access private |
| 290 |
* @param $length Length of the random string value |
| 291 |
* @return $random_string |
| 292 |
*/ |
| 293 |
function _rand_string($length = 5) |
| 294 |
{ |
| 295 |
$random_string = ''; |
| 296 |
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 297 |
$size = strlen($chars); |
| 298 |
|
| 299 |
for ($i = 0; $i < $length; $i++) |
| 300 |
{ |
| 301 |
$random_string .= $chars[rand(0, $size-1)]; |
| 302 |
} |
| 303 |
|
| 304 |
return $random_string; |
| 305 |
} |
| 306 |
|
| 307 |
} |
| 308 |
|
| 309 |
$cb_wp_reset = new cb_wp_reset(); |
| 310 |
|
| 311 |
register_activation_hook( __FILE__, array('cb_wp_reset', 'plugin_activate') ); |
| 312 |
|
| 313 |
endif; |