PluginProbe
Database Reset / 1.3
Database Reset v1.3
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.3, at wp-reset.php

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