PluginProbe
Database Reset / 2.3.2
Database Reset v2.3.2
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 2.3.2, at wp-reset.php

454 lines 17.2 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: 2.3.2
7 Author: Chris Berthe ☻
8 Author URI: https://github.com/chrisberthe
9 License: GNU General Public License
10 Text Domain: wp-reset
11 */
12
13 if ( ! class_exists( 'CB_WP_Reset' ) && is_admin() ) :
14
15 class CB_WP_Reset {
16
17 /**
18 * Nonce value
19 */
20 private $_nonce = 'wp-reset-nonce';
21
22 /**
23 * Tables to preserve
24 */
25 private $_tables;
26
27 /**
28 * WordPress database tables
29 */
30 private $_wp_tables;
31
32 /**
33 * Loads default options
34 *
35 * @return void
36 */
37 function __construct() {
38 add_action( 'init', array( $this, 'init_language' ) );
39 add_action( 'admin_init', array( $this, 'wp_reset_init' ) );
40 add_action( 'admin_init', array( $this, '_redirect_user' ) );
41 add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
42 add_filter( 'wp_mail', array( $this, '_fix_mail' ) );
43 }
44
45 /**
46 * Handles the admin page functionality
47 *
48 * @access public
49 * @uses wp_install Located in includes/upgrade.php (line 22)
50 */
51 function wp_reset_init() {
52 global $wpdb, $current_user, $pagenow;
53
54 // Grab the WordPress database tables
55 $this->_wp_tables = $wpdb->tables();
56
57 // Check for valid input - goes ahead and drops / resets tables
58 if ( isset( $_POST['wp-random-value'], $_POST['wp-reset-input'] ) && $_POST['wp-random-value'] == $_POST['wp-reset-input']
59 && check_admin_referer( 'wp-nonce-submit', $this->_nonce ) ) {
60
61 require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );
62
63 // No tables were selected
64 if ( empty( $_POST['tables'] ) ) {
65 wp_redirect( admin_url( $pagenow ) . '?page=wp-reset&reset=no-select' );
66 exit();
67 }
68
69 // Get current options
70 $blog_title = get_option( 'blogname' );
71 $public = get_option( 'blog_public' );
72
73 $admin_user = get_user_by( 'login', 'admin' );
74 $user = ( ! $admin_user || ! user_can( $admin_user->ID, 'update_core' ) ) ? $current_user : $admin_user;
75
76 // Get the selected tables
77 $tables = ( isset( $_POST['tables'] ) ) ? array_flip( $_POST['tables'] ) : array();
78
79 // Compare the selected tables against the ones in the database
80 $this->_tables = array_diff_key( $this->_wp_tables, $tables );
81
82 // Preserve the data from the tables that are unique
83 if ( 0 < count( $this->_tables ) ) {
84 $backup_tables = $this->_back_up_tables( $this->_tables );
85 }
86
87 // Grab the currently active plugins and theme
88 if ( ! empty($_POST['wp-reset-check']) ) {
89 $current_data = array(
90 'active-plugins' => get_option( 'active_plugins' ),
91 'current-theme' => get_option( 'current_theme' ),
92 'template' => get_option( 'template' ),
93 'stylesheet' => get_option( 'stylesheet' )
94 );
95 }
96
97 // Run through the database columns, drop all the tables and
98 // install wp with previous settings
99 if ( ! empty( $this->_wp_tables ) ) {
100 foreach ( $this->_wp_tables as $wp_table ) {
101 $wpdb->query( "DROP TABLE {$wp_table}" );
102 }
103 $keys = wp_install( $blog_title, $user->user_login, $user->user_email, $public );
104 $this->_wp_update_user( $user, $keys );
105 }
106
107 // Delete and replace tables with the backed up table data
108 if ( ! empty( $backup_tables ) ) {
109 foreach ( $this->_tables as $table ) {
110 $wpdb->query( "DELETE FROM " . $table );
111 }
112 $this->_restore_tables( $backup_tables );
113 }
114
115 if ( get_user_meta( $current_user->ID, 'session_tokens' ) ) {
116 delete_user_meta( $current_user->ID, 'session_tokens' );
117 }
118
119 wp_clear_auth_cookie();
120 wp_set_auth_cookie( $current_user->ID );
121
122 if ( ! empty( $current_data ) ) {
123 update_option( 'active_plugins', $current_data['active-plugins'] );
124
125 if ( ! empty( $current_data['current-theme'] ) ) {
126 update_option( 'current_theme', $current_data['current-theme'] );
127 }
128
129 update_option( 'template', $current_data['template'] );
130 update_option( 'stylesheet', $current_data['stylesheet'] );
131
132 wp_redirect( admin_url( $pagenow ) . '?page=wp-reset&reset=success' );
133 exit();
134 }
135
136 wp_redirect( admin_url() );
137 exit();
138 }
139 }
140
141 /**
142 * Displays the admin page
143 *
144 * @access public
145 * @return void
146 */
147 function show_admin_page() {
148 global $current_user;
149
150 // Return to see if admin object exists
151 $admin_user = get_user_by( 'login', 'admin' );
152 $random_string = wp_generate_password( 5, false ); ?>
153
154 <?php if ( isset( $_POST['wp-random-value'], $_POST['wp-reset-input'] ) && $_POST['wp-random-value'] != $_POST['wp-reset-input'] ) : ?>
155 <div class="error"><p><strong><?php _e( 'You entered the wrong value - please try again', 'wp-reset' ) ?>.</strong></p></div>
156 <?php elseif ( isset( $_GET['reset'] ) && 'no-select' == $_GET['reset'] ) : ?>
157 <div class="error"><p><strong><?php _e( 'You did not select any database tables', 'wp-reset' ) ?>.</strong></p></div>
158 <?php elseif ( isset( $_GET['reset'] ) && 'success' == $_GET['reset'] ) : ?>
159 <div class="updated"><p><strong><?php _e( 'The WordPress database has been reset successfully', 'wp-reset' ) ?>.</strong></p></div>
160 <?php endif ?>
161
162 <div class="wrap">
163 <?php screen_icon() ?>
164 <h2><?php _e( 'Database Reset', 'wp-reset' ) ?></h2>
165 <form action="" method="POST" id="wp-reset-form">
166 <p><?php _e( 'Please choose from the following database tables the ones you would like to reset', 'wp-reset' ) ?>:</p>
167 <div id="select-buttons">
168 <span><a href='#' id="select-all"><?php _e( 'Select All', 'wp-reset' ) ?></a></span>
169 <select id="wp-tables" multiple="multiple" name="tables[]">
170 <?php foreach ( $this->_wp_tables as $key => $value ) : ?>
171 <option value="<?php echo $key ?>"><?php echo $key ?></option>
172 <?php endforeach ?>
173 </select>
174 </div>
175 <p><?php _e( 'Type in (or copy/paste) the generated value into the text box', 'wp-reset' ) ?>:&nbsp;&nbsp;<strong><?php echo $random_string ?></strong></p>
176 <?php wp_nonce_field( 'wp-nonce-submit', $this->_nonce ) ?>
177 <input type="hidden" name="wp-random-value" value="<?php echo $random_string ?>" id="wp-random-value" />
178 <input type="text" name="wp-reset-input" value="" id="wp-reset-input" />
179 <input type="submit" name="wp-reset-submit" value="<?php _e( 'Reset Database', 'wp-reset' ) ?>" id="wp-reset-submit" class="button-primary" />
180 <img src="<?php echo plugins_url( 'css/i/ajax-loader.gif', __FILE__ ) ?>" alt="loader" id="loader" style="display: none" />
181 <div id="reactivate" style="display: none">
182 <p>
183 <label for="wp-reset-check">
184 <input type="checkbox" name="wp-reset-check" id="wp-reset-check" checked="checked" value="true" />
185 <?php _e( 'Reactivate current plugins and theme after reset?', 'wp-reset' ) ?>
186 </label>
187 </p>
188 </div>
189 </form>
190
191 <?php if ( ! $admin_user || ! user_can( $admin_user->ID, 'update_core' ) ) : ?>
192 <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>
193 <?php else : ?>
194 <p><?php _e( 'The default user <strong><u>admin</u></strong> will be recreated with its current password upon resetting', 'wp-reset' ) ?>.</p>
195 <?php endif ?>
196
197 <p><?php _e( 'Note that once you reset the database, all users will be deleted except the initial admin user.', 'wp-reset' ) ?></p>
198 </div>
199 <?php }
200
201 /**
202 * Add JavaScript to the bottom of the plugin page
203 *
204 * @access public
205 * @return bool TRUE on reset confirmation
206 */
207 function add_admin_javascript() {
208 ?>
209 <script type="text/javascript">
210 /* <![CDATA[ */
211 (function( $ ) {
212
213 $( '#wp-tables' ).bsmSelect({
214 animate: true,
215 title: "<?php _e( 'Select Table', 'wp-reset' ) ?>",
216 plugins: [$.bsmSelect.plugins.compatibility()]
217 });
218
219 $( '#select-all' ).click(function() {
220 $( '#wp-tables' ).children()
221 .attr( 'selected', 'selected' )
222 .end()
223 .change();
224
225 return false;
226 });
227
228 $( '#wp-reset-submit' ).click(function() {
229 var message = "<?php _e( 'Clicking OK will result in your database being reset to its initial settings. Continue?', 'wp-reset' ) ?>",
230 reset = confirm( message );
231
232 if (reset) {
233 $( '#wp-reset-form' ).submit();
234 $( '#loader' ).show();
235 } else {
236 return false;
237 }
238 });
239
240 $( '#wp-tables' ).change(function() {
241 $( '#reactivate' ).toggle( $( "option[value='options']:selected", this ).length > 0 );
242 });
243
244 })( jQuery );
245 /* ]]> */
246 </script>
247 <?php
248 }
249
250 /**
251 * Adds our submenu item to the Tools menu
252 *
253 * @access public
254 * @return void
255 */
256 function add_admin_menu() {
257 if ( current_user_can( 'update_core' ) ) {
258 $this->_hook = add_submenu_page( 'tools.php', 'Database Reset', 'Database Reset', 'update_core', 'wp-reset', array( $this, 'show_admin_page' ) );
259 add_action( 'load-' . $this->_hook, array( $this, 'option_page_actions' ) );
260 }
261 }
262
263 /**
264 * Fires actions on option page load
265 *
266 * @access public
267 * @return void
268 */
269 function option_page_actions() {
270 add_action( 'admin_enqueue_scripts', array( $this, 'add_plugin_styles_and_scripts' ) );
271 add_action( 'admin_footer', array( $this, 'add_admin_javascript' ) );
272 $this->_add_help_screen();
273 }
274
275 /**
276 * Adds v3.3 style help menu for plugin page
277 *
278 * @access private
279 * @return void
280 */
281 function _add_help_screen() {
282 get_current_screen()->add_help_tab( array(
283 'id' => 'overview',
284 'title' => __( 'Overview' ),
285 'content' =>
286 '<p>' . __( 'This plugin allows you to securely and easily reinitialize the WordPress database back to its default settings without actually having to reinstall WordPress from scratch. This plugin will come in handy for both theme and plugin developers. Two possible use case scenarios would be to:') . '</p>' .
287 '<p>' . __( '<strong>1.</strong> Erase excess junk in the <code>wp_options</code> table that accumulates over time.<br /><strong>2.</strong> Revert back to a fresh install of the WordPress database after experimenting with various back-end options.' ) . '</p>'
288 ) );
289 get_current_screen()->add_help_tab( array(
290 'id' => 'instructions',
291 'title' => __( 'Instructions' ),
292 'content' =>
293 '<p>' . __( 'Performing a database reset is quite straightforward.' ) . '</p>' .
294 '<p>' . __( 'Select the different tables you would like to reinitialize from the drop down list. You can select any number of tables. If you know you would like to reset the entire database, simply click the <code>Select All</code> button.' ) . '</p>' .
295 '<p>' . __( 'Next you will have to enter the <code>auto generated value</code> into the text box. Clicking on the <code>Reset Database</code> button will result in a pop-up.' ) . '</p>' .
296 '<p>' . __( 'Once you are sure you would like to proceed, click <code>OK</code> to reset.' ) . '</p>'
297 ) );
298
299 get_current_screen()->set_help_sidebar(
300 '<p><strong>' . __( 'Contact information:' ) . '</strong></p>' .
301 '<p>' . __( 'Any ideas on features or ways to improve this plugin? Contact me at <a href="http://github.com/chrisberthe/" target="_blank">GitHub</a> or <a href="http://twitter.com/chrisberthe/" target="_blank">Twitter</a>.' ) . '</p>'
302 );
303 }
304
305 /**
306 * Adds any plugin styles to our page
307 *
308 * @access public
309 * @return void
310 */
311 function add_plugin_styles_and_scripts() {
312 wp_enqueue_style( 'wordpress-reset-css', plugins_url( 'css/wp-reset.css', __FILE__ ) );
313 wp_enqueue_style( 'bsmselect-css', plugins_url( 'css/jquery.bsmselect.css', __FILE__ ) );
314
315 wp_enqueue_script( 'bsmselect', plugins_url( 'js/jquery.bsmselect.js', __FILE__ ) );
316 wp_enqueue_script( 'bsmselect-compatibility', plugins_url( 'js/jquery.bsmselect.compatibility.js', __FILE__ ) );
317 }
318
319 /**
320 * Load language path
321 *
322 * @access public
323 * @return void
324 */
325 function init_language() {
326 $language_dir = basename( dirname( __FILE__ ) ) . '/languages';
327 load_plugin_textdomain( 'wp-reset', false, $language_dir );
328 }
329
330 /**
331 * For activation hook
332 *
333 * @access public
334 * @return void
335 */
336 function plugin_activate() {
337 add_option( 'wp-reset-activated', true );
338 }
339
340 /**
341 * Redirects the user after the plugin is activated
342 *
343 * @access private
344 * @return void
345 */
346 function _redirect_user() {
347 if ( get_option( 'wp-reset-activated', false ) ) {
348 delete_option( 'wp-reset-activated' );
349 wp_redirect( admin_url( 'tools.php' ) . '?page=wp-reset' );
350 }
351 }
352
353 /**
354 * Changes the password to a sentence rather than
355 * an auto-generated password that is sent by email
356 * right after the installation is complete
357 *
358 * @access private
359 * @return $mail Version with password changed
360 */
361 function _fix_mail( $mail ) {
362 $subject = __( 'WordPress Database Reset', 'wp-reset' );
363 $message = __( 'The tables you selected have been successfully reset to their default settings:', 'wp-reset' );
364 $password = __( 'Password: The password you chose during the install.', 'wp-reset' );
365
366 if ( stristr( $mail['message'], 'Your new WordPress site has been successfully set up at:' ) ) {
367 $mail['subject'] = preg_replace( '/New WordPress Site/', $subject, $mail['subject'] );
368 $mail['message'] = preg_replace( '/Your new WordPress site has been successfully set up at:+/', $message, $mail['message'] );
369 $mail['message'] = preg_replace( '/Password:\s.+/', $password, $mail['message'] );
370 }
371 return $mail;
372 }
373
374 /**
375 * Preserves all the results from the tables the user
376 * did not select from the drop-down.
377 *
378 * @access private
379 * @return array Backed up data if type backup
380 */
381 function _back_up_tables( $tables ) {
382 global $wpdb;
383
384 if ( is_array( $tables ) ) {
385 $backup_tables = array();
386
387 foreach ( $tables as $table ) {
388 $backup_tables[$table] = $wpdb->get_results( "SELECT * FROM " . $table );
389 }
390
391 return $backup_tables;
392 }
393 return;
394 }
395
396 /**
397 * Restores the data from the tables the user did not
398 * select during the backing up phase.
399 *
400 * @access private
401 * @return void
402 */
403 function _restore_tables( $tables ) {
404 global $wpdb;
405
406 if ( is_array( $tables ) ) {
407 foreach ( $tables as $table_name => $table_data ) {
408 foreach ( $table_data as $row ) {
409 $columns = $values = array();
410
411 foreach ( $row as $column => $value ) {
412 $columns[] = $column;
413 $values[] = esc_sql( $value );
414 }
415
416 $wpdb->query( "INSERT INTO $table_name (" . implode( ', ', $columns ) . ") VALUES ('" . implode( "', '", $values ) . "')" );
417 }
418 }
419 }
420 return;
421 }
422
423 /**
424 * Updates the user password and clears / sets
425 * the authentication cookie for the user
426 *
427 * @access private
428 * @param $user Current or admin user
429 * @param $keys Array returned by wp_install()
430 * @return true on install success, false otherwise
431 */
432 function _wp_update_user( $user, $keys ) {
433 global $wpdb;
434 extract( $keys, EXTR_SKIP );
435
436 $query = $wpdb->prepare( "UPDATE $wpdb->users SET user_pass = '%s', user_activation_key = '' WHERE ID = '%d'", $user->user_pass, $user_id );
437
438 if ( $wpdb->query( $query ) ) {
439 // Remove password reminder after installing
440 if ( get_user_meta( $user_id, 'default_password_nag' ) ) {
441 delete_user_meta( $user_id, 'default_password_nag' );
442 }
443
444 return true;
445 }
446 return false;
447 }
448 }
449
450 $cb_wp_reset = new CB_WP_Reset();
451 register_activation_hook( __FILE__, array( 'cb_wp_reset', 'plugin_activate' ) );
452
453 endif;
454