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