PluginProbe
Database Reset / 1.0
Database Reset v1.0
2.3.2 3.0 3.0.1 3.0.2 3.1 3.15 3.16 3.17 3.18 3.19 3.20 3.21 3.22 3.23 3.24 3.25 trunk 1.0 1.2 1.2.1 1.2.2 1.3 1.4 2.0 2.1 All 27 releases
wordpress-database-reset / wp-reset.php

wp-reset.php in Database Reset 1.0, at wp-reset.php

304 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: WP Reset
4 Plugin URI: https://github.com/chrisberthe/wp-reset
5 Description: A plugin that allows you to reset the database to WordPress's initial state.
6 Version: 1.0
7 Author: Chris Berthe ☻
8 Author URI: https://github.com/chrisberthe
9 License: GNU General Public License
10 */
11
12 if ( ! class_exists('WP_Reset') && is_admin() ) :
13
14 class 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_password_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 || $admin_user->wp_user_level < 10 ) ? $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 // Reactivate the plugin after reinstalling
71 update_option('active_plugins', array(plugin_basename(__FILE__)));
72
73 wp_redirect(admin_url($pagenow) . '?page=wp-reset&reset=success'); exit();
74 }
75 }
76 }
77
78 /**
79 * Displays the admin page
80 *
81 * @access public
82 * @return void
83 */
84 function show_admin_page()
85 {
86 global $current_user;
87
88 // Return to see if admin object exists
89 $admin_user = get_userdatabylogin('admin');
90
91 // Generate a random value for the input box
92 $random_string = $this->_rand_string();
93 ?>
94 <?php if ( isset($_POST['wp-random-value'], $_POST['wp-reset-input']) && $_POST['wp-random-value'] != $_POST['wp-reset-input'] ) : ?>
95 <div class="error"><p><strong><?php _e('You entered the wrong value - please try again', 'wp-reset') ?>.</strong></p></div>
96 <?php elseif ( isset($_GET['reset']) && $_GET['reset'] == 'success' ) : ?>
97 <div class="updated"><p><strong><?php _e('The WordPress database has been reset successfully', 'wp-reset') ?>.</strong></p></div>
98 <?php endif ?>
99
100 <div class="wrap">
101 <?php screen_icon() ?>
102 <h2><?php _e('Database Reset', 'wp-reset') ?></h2>
103 <p>Please type in (or copy/paste) the generated value into the text box:&nbsp;&nbsp;<strong><?php echo $random_string ?></strong></p>
104 <form action="" method="POST" id="wp-reset-form">
105 <?php wp_nonce_field('wp-nonce-submit', $this->_nonce) ?>
106 <input type="hidden" name="wp-random-value" value="<?php echo $random_string ?>" id="wp-random-value" />
107 <input type="text" name="wp-reset-input" value="" id="wp-reset-input" />
108 <input type="submit" name="wp-reset-submit" value="<?php _e('Reset Database', 'wp-reset') ?>" id="wp-reset-submit" class="button-primary" />
109 </form>
110
111 <?php if ( ! $admin_user || $admin_user->wp_user_level < 10 ) : ?>
112 <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>
113 <?php else : ?>
114 <p><?php _e('The default user <strong><u>admin</u></strong> will be recreated with its current password upon resetting', 'wp-reset') ?>.</p>
115 <?php endif; ?>
116
117 <p><?php _e('Note that once you reset the database, all users will be deleted except the initial admin user. The plugin will also reactivate itself after resetting', 'wp-reset') ?>.</p>
118 </div>
119 <?php }
120
121 /**
122 * Add JavaScript to the bottom of the plugin page
123 *
124 * @access public
125 * @return bool TRUE on reset confirmation
126 */
127 function add_admin_javascript()
128 {
129 ?>
130 <script type="text/javascript">
131 /* <![CDATA[ */
132 jQuery('#wp-reset-submit').click(function() {
133 var message = "<?php _e('Clicking OK will result in your database being reset to its initial settings. Continue?', 'wp-reset') ?>";
134 var reset = confirm(message);
135
136 if ( reset ) {
137 jQuery('#wp-reset-form').submit();
138 } else {
139 return false;
140 }
141 });
142 /* ]]> */
143 </script>
144 <?php
145 }
146
147 /**
148 * Adds our submenu item to the Tools menu
149 *
150 * @access public
151 * @return void
152 */
153 function add_admin_menu()
154 {
155 global $current_user;
156
157 if ( current_user_can('update_core') && $current_user->wp_user_level == 10)
158 {
159 $this->_hook = add_submenu_page('tools.php', 'Database Reset', 'Database Reset', 'update_core', 'wp-reset', array($this, 'show_admin_page'));
160 }
161 }
162
163 /**
164 * Adds the contextual help for our plugin page
165 *
166 * @access public
167 * @param $contextual_help Hook text to display
168 * @param $screen_id ID of the current admin screen
169 * @return $contextual_help String The help text
170 */
171 function add_contextual_help($contextual_help, $screen_id)
172 {
173 if ($screen_id == $this->_hook)
174 {
175 $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>';
176 $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>';
177 $contextual_help .= '<p>' . __('Goodbye for now.', 'wp-reset') . '</p>';
178 }
179
180 return $contextual_help;
181 }
182
183 /**
184 * Load language path
185 *
186 * @access public
187 * @return void
188 */
189 function init_language()
190 {
191 $language_dir = basename(dirname(__FILE__)) . '/languages';
192 load_plugin_textdomain('wp-reset', FALSE, $language_dir);
193 }
194
195 /**
196 * For activation hook
197 *
198 * @access public
199 * @return void
200 */
201 function plugin_activate()
202 {
203 add_option('wp-reset-activated', true);
204 }
205
206 /**
207 * Redirects the user after the plugin is activated
208 *
209 * @access private
210 * @return void
211 */
212 function _redirect_user()
213 {
214 if ( get_option('wp-reset-activated', false) )
215 {
216 delete_option('wp-reset-activated');
217 wp_redirect(admin_url('tools.php') . '?page=wp-reset');
218 }
219 }
220
221 /**
222 * Changes the password to a sentence rather than
223 * an auto-generated password that is sent by email
224 * right after the installation is complete
225 *
226 * @access private
227 * @return $mail Version with password changed
228 */
229 function _fix_password_mail($mail)
230 {
231 $subject = __('WordPress Database Reset', 'wp-reset');
232 $message = __('The WordPress database has been successfully reset to its default settings:', 'wp-reset');
233 $password = __('Password: The password you chose during the install.', 'wp-reset');
234
235 if ( stristr($mail['message'], 'Your new WordPress site has been successfully set up at:') )
236 {
237 $mail['subject'] = preg_replace('/New WordPress Site/', $subject, $mail['subject']);
238 $mail['message'] = preg_replace('/Your new WordPress site has been successfully set up at:+/', $message, $mail['message']);
239 $mail['message'] = preg_replace('/Password:\s.+/', $password, $mail['message']);
240 }
241
242 return $mail;
243 }
244
245 /**
246 * Updates the user password and clears / sets
247 * the authentication cookie for the user
248 *
249 * @access private
250 * @param $user Current or admin user
251 * @param $keys Array returned by wp_install()
252 * @return TRUE on install success, FALSE otherwise
253 */
254 function _wp_update_user($user, $keys)
255 {
256 global $wpdb;
257 extract($keys, EXTR_SKIP);
258
259 // Set the old password back to the user
260 $query = $wpdb->prepare("UPDATE $wpdb->users SET user_pass = '%s', user_activation_key = '' WHERE ID = '%d'", $user->user_pass, $user_id);
261
262 if ( $wpdb->query($query) )
263 {
264 // Set the default_password_nag to nothing
265 // so it doesn't pop up with the password reminder after installing
266 if ( get_user_meta($user_id, 'default_password_nag') ) delete_user_meta($user_id, 'default_password_nag');
267
268 wp_clear_auth_cookie();
269 wp_set_auth_cookie($user_id);
270
271 return TRUE;
272 }
273
274 return FALSE;
275 }
276
277 /**
278 * Generates a random value for our input box
279 *
280 * @access private
281 * @param $length Length of the random string value
282 * @return $random_string
283 */
284 function _rand_string($length = 5)
285 {
286 $random_string = '';
287 $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
288 $size = strlen($chars);
289
290 for ($i = 0; $i < $length; $i++)
291 {
292 $random_string .= $chars[rand(0, $size-1)];
293 }
294
295 return $random_string;
296 }
297
298 }
299
300 $wp_reset = new WP_Reset();
301
302 register_activation_hook( __FILE__, array('WP_Reset', 'plugin_activate') );
303
304 endif;