class-reset.php
205 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Reset WordPress |
| 5 | * |
| 6 | * @link https://addonspress.com/ |
| 7 | * @since 1.0.0 |
| 8 | * |
| 9 | * @package Advanced_Import |
| 10 | * @subpackage Advanced_Import/admin |
| 11 | */ |
| 12 | |
| 13 | /** |
| 14 | * The admin-specific functionality of the plugin. |
| 15 | * Reset WordPress |
| 16 | * @package Advanced_Import |
| 17 | * @subpackage Advanced_Import/admin |
| 18 | * @author Addons Press <addonspress.com> |
| 19 | */ |
| 20 | class Advanced_Import_Reset_WordPress { |
| 21 | |
| 22 | /** |
| 23 | * Initialize the class and set its properties. |
| 24 | * |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | public function __construct( ) {} |
| 28 | |
| 29 | /** |
| 30 | * Main Advanced_Import_Reset_WordPress Instance |
| 31 | * Initialize the class and set its properties. |
| 32 | * |
| 33 | * @since 1.0.0 |
| 34 | * @return object $instance Advanced_Import_Reset_WordPress Instance |
| 35 | */ |
| 36 | public static function instance() { |
| 37 | |
| 38 | // Store the instance locally to avoid private static replication |
| 39 | static $instance = null; |
| 40 | |
| 41 | // Only run these methods if they haven't been ran previously |
| 42 | if ( null === $instance ) { |
| 43 | $instance = new Advanced_Import_Reset_WordPress; |
| 44 | |
| 45 | } |
| 46 | |
| 47 | // Always return the instance |
| 48 | return $instance; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Hide a notice if the GET variable is set. |
| 53 | */ |
| 54 | public function hide_reset_notice() { |
| 55 | if ( isset( $_GET['advanced-import-hide-notice'] ) && isset( $_GET['_advanced_import_notice_nonce'] ) ) { |
| 56 | /*Security*/ |
| 57 | if ( ! wp_verify_nonce( $_GET['_advanced_import_notice_nonce'], 'advanced_import_hide_notice_nonce' ) ) { |
| 58 | wp_die( __( 'Action failed. Please refresh the page and retry.', 'advanced-import' ) ); |
| 59 | } |
| 60 | |
| 61 | if ( ! current_user_can( 'manage_options' ) ) { |
| 62 | wp_die( __( 'Cheatin’ huh?', 'advanced-import' ) ); |
| 63 | } |
| 64 | |
| 65 | $hide_notice = sanitize_text_field( $_GET['advanced-import-hide-notice'] ); |
| 66 | |
| 67 | if ( ! empty( $hide_notice ) && 'reset_notice' == $hide_notice ) { |
| 68 | update_option( 'advanced_import_reset_notice', 1 ); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Reset actions when a reset button is clicked. |
| 75 | */ |
| 76 | public function reset_wizard_actions() { |
| 77 | global $wpdb, $current_user; |
| 78 | |
| 79 | if ( ! empty( $_GET['ai_reset_wordpress'] ) && !empty($_GET['ai_reset_wordpress_nonce'] )) { |
| 80 | /*Security*/ |
| 81 | if ( ! wp_verify_nonce( wp_unslash( $_GET['ai_reset_wordpress_nonce'] ), 'ai_reset_wordpress' ) ) { // WPCS: input var ok, sanitization ok. |
| 82 | wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'advanced-import' ) ); |
| 83 | } |
| 84 | if ( ! current_user_can( 'manage_options' ) ) { |
| 85 | wp_die( esc_html__( 'No permission to reset WordPress', 'advanced-import' ) ); |
| 86 | } |
| 87 | |
| 88 | require_once ABSPATH . '/wp-admin/includes/upgrade.php'; |
| 89 | |
| 90 | $template = get_option( 'template' ); |
| 91 | $blogname = get_option( 'blogname' ); |
| 92 | $admin_email = get_option( 'admin_email' ); |
| 93 | $blog_public = get_option( 'blog_public' ); |
| 94 | |
| 95 | $current_url = advanced_import_current_url(); |
| 96 | |
| 97 | if ( 'admin' != $current_user->user_login ) { |
| 98 | $user = get_user_by( 'login', 'admin' ); |
| 99 | } |
| 100 | |
| 101 | if ( empty( $user->user_level ) || $user->user_level < 10 ) { |
| 102 | $user = $current_user; |
| 103 | } |
| 104 | |
| 105 | // Drop tables. |
| 106 | $drop_tables = $wpdb->get_col( sprintf( "SHOW TABLES LIKE '%s%%'", str_replace( '_', '\_', $wpdb->prefix ) ) ); |
| 107 | foreach ( $drop_tables as $table ) { |
| 108 | $wpdb->query( "DROP TABLE IF EXISTS $table" ); |
| 109 | } |
| 110 | |
| 111 | // Installs the site. |
| 112 | $result = wp_install( $blogname, $user->user_login, $user->user_email, $blog_public ); |
| 113 | |
| 114 | // Updates the user password with a old one. |
| 115 | $wpdb->update( |
| 116 | $wpdb->users, |
| 117 | array( |
| 118 | 'user_pass' => $user->user_pass, |
| 119 | 'user_activation_key' => '', |
| 120 | ), |
| 121 | array( 'ID' => $result['user_id'] ) |
| 122 | ); |
| 123 | |
| 124 | // Set up the Password change nag. |
| 125 | $default_password_nag = get_user_option( 'default_password_nag', $result['user_id'] ); |
| 126 | if ( $default_password_nag ) { |
| 127 | update_user_option( $result['user_id'], 'default_password_nag', false, true ); |
| 128 | } |
| 129 | |
| 130 | // Switch current theme. |
| 131 | $current_theme = wp_get_theme( $template ); |
| 132 | if ( $current_theme->exists() ) { |
| 133 | switch_theme( $template ); |
| 134 | } |
| 135 | |
| 136 | // Activate required plugins. |
| 137 | $required_plugins = (array) apply_filters( 'advanced_import_' . $template . '_required_plugins', array() ); |
| 138 | if ( is_array( $required_plugins ) ) { |
| 139 | if ( ! in_array( plugin_basename( ADVANCED_IMPORT_PATH.'/advanced-import.php'), $required_plugins ) ) { |
| 140 | $required_plugins = array_merge( $required_plugins, array( ADVANCED_IMPORT_PATH.'/advanced-import.php') ); |
| 141 | } |
| 142 | activate_plugins( $required_plugins, '', is_network_admin(), true ); |
| 143 | } |
| 144 | |
| 145 | // Update the cookies. |
| 146 | wp_clear_auth_cookie(); |
| 147 | wp_set_auth_cookie( $result['user_id'] ); |
| 148 | |
| 149 | // Redirect to demo importer page to display reset success notice. |
| 150 | wp_safe_redirect($current_url.'&reset=true&from=ai-reset-wp'); |
| 151 | exit(); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Reset wizard notice. |
| 157 | */ |
| 158 | public function reset_wizard_notice() { |
| 159 | |
| 160 | $screen = get_current_screen(); |
| 161 | if (!in_array( $screen->base, advanced_import_admin()->hook_suffix )){ |
| 162 | return; |
| 163 | } |
| 164 | $current_url = advanced_import_current_url(); |
| 165 | $reset_url = wp_nonce_url( |
| 166 | add_query_arg( 'ai_reset_wordpress', 'true', $current_url ), |
| 167 | 'ai_reset_wordpress', |
| 168 | 'ai_reset_wordpress_nonce' |
| 169 | ); |
| 170 | |
| 171 | $demo_notice_dismiss = get_option( 'advanced_import_reset_notice' ); |
| 172 | |
| 173 | // Output reset wizard notice. |
| 174 | if ( ! $demo_notice_dismiss ) { |
| 175 | ?> |
| 176 | <div id="message" class="updated ai-import-message"> |
| 177 | <p><?php _e( '<strong>WordPress Reset</strong> – If no important data on your site. You can reset the WordPress back to default again!', 'advanced-import' ); ?></p> |
| 178 | <p class="submit"><a href="<?php echo esc_url( $reset_url ); ?>" class="button button-primary ai-wp-reset"><?php esc_html_e( 'Run the Reset Wizard', 'advanced-import' ); ?></a> <a class="button-secondary skip" href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'advanced-import-hide-notice', 'reset_notice', $current_url ), 'advanced_import_hide_notice_nonce', '_advanced_import_notice_nonce' ) ); ?>"><?php esc_attr_e( 'Hide this notice', 'advanced-import' ); ?></a></p> |
| 179 | </div> |
| 180 | <?php |
| 181 | } elseif ( isset( $_GET['reset'] ) && 'true' === $_GET['reset'] ) { |
| 182 | $user = get_user_by( 'id', 1 ); |
| 183 | ?> |
| 184 | <div id="message" class="notice notice-info is-dismissible"> |
| 185 | <p><?php printf( __( 'WordPress has been reset back to defaults. The user <strong>"%1$s"</strong> was recreated with its previous password.', 'advanced-import' ), $user->user_login ); ?></p> |
| 186 | </div> |
| 187 | <?php |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Begins execution of the plugin. |
| 196 | * |
| 197 | * Since everything within the plugin is registered via hooks, |
| 198 | * then kicking off the plugin from this point in the file does |
| 199 | * not affect the page life cycle. |
| 200 | * |
| 201 | * @since 1.0.0 |
| 202 | */ |
| 203 | function advanced_import_reset_wordpress() { |
| 204 | return Advanced_Import_Reset_WordPress::instance(); |
| 205 | } |