class-advanced-import-admin.php
4 years ago
class-advanced-import-tracking.php
4 years ago
class-elementor-import.php
4 years ago
class-reset.php
4 years ago
index.php
4 years ago
class-reset.php
265 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 | * |
| 17 | * @package Advanced_Import |
| 18 | * @subpackage Advanced_Import/admin |
| 19 | * @author Addons Press <addonspress.com> |
| 20 | */ |
| 21 | class Advanced_Import_Reset_WordPress { |
| 22 | |
| 23 | /** |
| 24 | * Initialize the class and set its properties. |
| 25 | * |
| 26 | * @since 1.0.0 |
| 27 | */ |
| 28 | public function __construct() {} |
| 29 | |
| 30 | /** |
| 31 | * Main Advanced_Import_Reset_WordPress Instance |
| 32 | * Initialize the class and set its properties. |
| 33 | * |
| 34 | * @since 1.0.0 |
| 35 | * @return object $instance Advanced_Import_Reset_WordPress Instance |
| 36 | */ |
| 37 | public static function instance() { |
| 38 | |
| 39 | // Store the instance locally to avoid private static replication |
| 40 | static $instance = null; |
| 41 | |
| 42 | // Only run these methods if they haven't been ran previously |
| 43 | if ( null === $instance ) { |
| 44 | $instance = new Advanced_Import_Reset_WordPress(); |
| 45 | |
| 46 | } |
| 47 | |
| 48 | // Always return the instance |
| 49 | return $instance; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Check if user can reset |
| 54 | */ |
| 55 | private function can_reset() { |
| 56 | if ( ! empty( $_GET['ai_reset_wordpress'] ) && ! empty( $_GET['ai_reset_wordpress_nonce'] ) ) { |
| 57 | /*Security*/ |
| 58 | if ( ! wp_verify_nonce( wp_unslash( $_GET['ai_reset_wordpress_nonce'] ), 'ai_reset_wordpress' ) ) { // WPCS: input var ok, sanitization ok. |
| 59 | return false; |
| 60 | } |
| 61 | if ( ! current_user_can( 'manage_options' ) ) { |
| 62 | return false; |
| 63 | } |
| 64 | return true; |
| 65 | } |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Attempt to deactivate the plugins which gives errors while reseting. |
| 71 | * We may add other plugins after testing/reported |
| 72 | */ |
| 73 | private function deactivate_plugins() { |
| 74 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 75 | if ( ! function_exists( 'deactivate_plugins' ) ) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | if ( is_plugin_active( 'elementor/elementor.php' ) ) { |
| 80 | deactivate_plugins( 'elementor/elementor.php' ); |
| 81 | } |
| 82 | |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Hide a notice if the GET variable is set. |
| 87 | */ |
| 88 | public function hide_reset_notice() { |
| 89 | if ( isset( $_GET['advanced-import-hide-notice'] ) && isset( $_GET['_advanced_import_notice_nonce'] ) ) { |
| 90 | /*Security*/ |
| 91 | if ( ! wp_verify_nonce( $_GET['_advanced_import_notice_nonce'], 'advanced_import_hide_notice_nonce' ) ) { |
| 92 | wp_die( __( 'Action failed. Please refresh the page and retry.', 'advanced-import' ) ); |
| 93 | } |
| 94 | |
| 95 | if ( ! current_user_can( 'manage_options' ) ) { |
| 96 | wp_die( __( 'Cheatin’ huh?', 'advanced-import' ) ); |
| 97 | } |
| 98 | |
| 99 | $hide_notice = sanitize_text_field( $_GET['advanced-import-hide-notice'] ); |
| 100 | |
| 101 | if ( ! empty( $hide_notice ) && 'reset_notice' == $hide_notice ) { |
| 102 | advanced_import_update_option( 'advanced_import_reset_notice', 1 ); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Reset actions when a reset button is clicked. |
| 109 | */ |
| 110 | public function reset_wizard_actions() { |
| 111 | global $wpdb, $current_user; |
| 112 | |
| 113 | if ( ! empty( $_GET['ai_reset_wordpress'] ) && ! empty( $_GET['ai_reset_wordpress_nonce'] ) && $this->can_reset() ) { |
| 114 | /*Security*/ |
| 115 | if ( ! wp_verify_nonce( wp_unslash( $_GET['ai_reset_wordpress_nonce'] ), 'ai_reset_wordpress' ) ) { // WPCS: input var ok, sanitization ok. |
| 116 | wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'advanced-import' ) ); |
| 117 | } |
| 118 | if ( ! current_user_can( 'manage_options' ) ) { |
| 119 | wp_die( esc_html__( 'No permission to reset WordPress', 'advanced-import' ) ); |
| 120 | } |
| 121 | |
| 122 | require_once ABSPATH . '/wp-admin/includes/upgrade.php'; |
| 123 | |
| 124 | $template = get_option( 'template' ); |
| 125 | $blogname = get_option( 'blogname' ); |
| 126 | $admin_email = get_option( 'admin_email' ); |
| 127 | $blog_public = get_option( 'blog_public' ); |
| 128 | |
| 129 | $current_url = advanced_import_current_url(); |
| 130 | |
| 131 | if ( 'admin' != $current_user->user_login ) { |
| 132 | $user = get_user_by( 'login', 'admin' ); |
| 133 | } |
| 134 | |
| 135 | if ( empty( $user->user_level ) || $user->user_level < 10 ) { |
| 136 | $user = $current_user; |
| 137 | } |
| 138 | |
| 139 | // Drop tables. |
| 140 | $drop_tables = $wpdb->get_col( sprintf( "SHOW TABLES LIKE '%s%%'", str_replace( '_', '\_', $wpdb->prefix ) ) ); |
| 141 | foreach ( $drop_tables as $table ) { |
| 142 | $wpdb->query( "DROP TABLE IF EXISTS $table" ); |
| 143 | } |
| 144 | |
| 145 | // Installs the site. |
| 146 | $result = wp_install( $blogname, $user->user_login, $user->user_email, $blog_public ); |
| 147 | |
| 148 | // Updates the user password with a old one. |
| 149 | $wpdb->update( |
| 150 | $wpdb->users, |
| 151 | array( |
| 152 | 'user_pass' => $user->user_pass, |
| 153 | 'user_activation_key' => '', |
| 154 | ), |
| 155 | array( 'ID' => $result['user_id'] ) |
| 156 | ); |
| 157 | |
| 158 | // Set up the Password change nag. |
| 159 | $default_password_nag = get_user_option( 'default_password_nag', $result['user_id'] ); |
| 160 | if ( $default_password_nag ) { |
| 161 | update_user_option( $result['user_id'], 'default_password_nag', false, true ); |
| 162 | } |
| 163 | |
| 164 | // Switch current theme. |
| 165 | $current_theme = wp_get_theme( $template ); |
| 166 | if ( $current_theme->exists() ) { |
| 167 | switch_theme( $template ); |
| 168 | } |
| 169 | |
| 170 | // Activate required plugins. |
| 171 | $required_plugins = (array) apply_filters( 'advanced_import_' . $template . '_required_plugins', array() ); |
| 172 | if ( is_array( $required_plugins ) ) { |
| 173 | if ( ! in_array( plugin_basename( ADVANCED_IMPORT_PATH . '/advanced-import.php' ), $required_plugins ) ) { |
| 174 | $required_plugins = array_merge( $required_plugins, array( ADVANCED_IMPORT_PATH . '/advanced-import.php' ) ); |
| 175 | } |
| 176 | activate_plugins( $required_plugins, '', is_network_admin(), true ); |
| 177 | } |
| 178 | |
| 179 | // Update the cookies. |
| 180 | wp_clear_auth_cookie(); |
| 181 | wp_set_auth_cookie( $result['user_id'] ); |
| 182 | |
| 183 | // Redirect to demo importer page to display reset success notice. |
| 184 | wp_safe_redirect( $current_url . '&reset=true&from=ai-reset-wp' ); |
| 185 | exit(); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Reset wizard notice. |
| 191 | */ |
| 192 | public function reset_wizard_notice() { |
| 193 | |
| 194 | $screen = get_current_screen(); |
| 195 | if ( ! in_array( $screen->base, advanced_import_admin()->hook_suffix ) ) { |
| 196 | return; |
| 197 | } |
| 198 | $current_url = advanced_import_current_url(); |
| 199 | $reset_url = wp_nonce_url( |
| 200 | add_query_arg( 'ai_reset_wordpress', 'true', $current_url ), |
| 201 | 'ai_reset_wordpress', |
| 202 | 'ai_reset_wordpress_nonce' |
| 203 | ); |
| 204 | |
| 205 | $demo_notice_dismiss = get_option( 'advanced_import_reset_notice' ); |
| 206 | |
| 207 | // Output reset wizard notice. |
| 208 | if ( ! $demo_notice_dismiss ) { |
| 209 | ?> |
| 210 | <div id="message" class="updated ai-import-message"> |
| 211 | <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> |
| 212 | <p class="submit"><?php wp_nonce_field( 'advanced-import-reset', 'advanced-import-reset' ); ?><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> |
| 213 | </div> |
| 214 | <?php |
| 215 | } elseif ( isset( $_GET['reset'] ) && 'true' === $_GET['reset'] ) { |
| 216 | $user = get_user_by( 'id', 1 ); |
| 217 | ?> |
| 218 | <div id="message" class="notice notice-info is-dismissible"> |
| 219 | <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> |
| 220 | </div> |
| 221 | <?php |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Before Reset Ajax callback |
| 227 | */ |
| 228 | public function before_reset() { |
| 229 | /*check for security*/ |
| 230 | if ( ! current_user_can( 'upload_files' ) ) { |
| 231 | wp_send_json_error( |
| 232 | array( |
| 233 | 'message' => esc_html__( 'Sorry, you are not allowed to install demo on this site.', 'advanced-import' ), |
| 234 | ) |
| 235 | ); |
| 236 | } |
| 237 | check_admin_referer( 'advanced-import-reset' ); |
| 238 | |
| 239 | /*Deactivate troubleshoot plugins before reset*/ |
| 240 | $this->deactivate_plugins(); |
| 241 | |
| 242 | do_action( 'advanced_import_before_reset' ); |
| 243 | wp_send_json_success( |
| 244 | array( |
| 245 | 'message' => esc_html__( 'Success', 'advanced-import' ), |
| 246 | ) |
| 247 | ); |
| 248 | } |
| 249 | |
| 250 | |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Begins execution of the plugin. |
| 255 | * |
| 256 | * Since everything within the plugin is registered via hooks, |
| 257 | * then kicking off the plugin from this point in the file does |
| 258 | * not affect the page life cycle. |
| 259 | * |
| 260 | * @since 1.0.0 |
| 261 | */ |
| 262 | function advanced_import_reset_wordpress() { |
| 263 | return Advanced_Import_Reset_WordPress::instance(); |
| 264 | } |
| 265 |