PluginProbe
Database Reset / 3.0.1
Database Reset v3.0.1
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 / class-db-resetter.php

class-db-resetter.php in Database Reset 3.0.1, at class-db-resetter.php

216 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! class_exists( 'DB_Resetter' ) ) :
4
5 class DB_Resetter {
6
7 private $backup;
8 private $blog_data;
9 private $theme_data;
10 private $preserved;
11 private $selected;
12 private $reactivate;
13 private $user;
14 private $wp_tables;
15
16 public function __construct() {
17 $this->set_wp_tables();
18 $this->set_user();
19 }
20
21 public function reset( array $tables ) {
22 $this->validate_selected( $tables );
23 $this->set_backup();
24 $this->reinstall();
25 $this->restore_backup();
26 }
27
28 private function validate_selected( array $tables ) {
29 if ( ! empty( $tables ) && is_array( $tables ) ) {
30 $this->selected = array_flip( $tables );
31 return;
32 }
33
34 throw new Exception( __( 'You did not select any database tables', 'wordpress-database-reset' ) );
35 }
36
37 private function set_backup() {
38 $this->set_tables_to_preserve( $this->selected );
39 $this->back_up_tables( $this->preserved );
40 $this->set_blog_data();
41
42 if ( $this->should_restore_theme_data() ) {
43 $this->set_theme_data();
44 }
45 }
46
47 private function set_tables_to_preserve( array $tables ) {
48 $this->preserved = array_diff_key( $this->wp_tables, $tables );
49 }
50
51 private function back_up_tables( array $tables ) {
52 global $wpdb;
53 $this->backup = array();
54
55 foreach ( $tables as $table ) {
56 $this->backup[ $table ] = $wpdb->get_results( "SELECT * FROM {$table}" );
57 }
58 }
59
60 private function set_blog_data() {
61 $this->blog_data = array(
62 'name' => get_option( 'blogname' ),
63 'public' => get_option( 'blog_public' ),
64 'site_url' => get_option( 'siteurl' )
65 );
66 }
67
68 private function should_restore_theme_data() {
69 return ( 'true' === $this->reactivate );
70 }
71
72 private function set_theme_data() {
73 $this->theme_data = array(
74 'active-plugins' => get_option( 'active_plugins' ),
75 'current-theme' => get_option( 'current_theme' ),
76 'stylesheet' => get_option( 'stylesheet' ),
77 'template' => get_option( 'template' )
78 );
79 }
80
81 private function reinstall() {
82 $this->drop_wp_tables();
83 $this->install_wp();
84 $this->update_user_settings();
85 }
86
87 private function drop_wp_tables() {
88 global $wpdb;
89
90 foreach ( $this->wp_tables as $wp_table ) {
91 $wpdb->query( "DROP TABLE {$wp_table}" );
92 }
93 }
94
95 private function install_wp() {
96 return db_reset_install(
97 $this->blog_data[ 'name' ],
98 $this->user->user_login,
99 $this->user->user_email,
100 $this->blog_data[ 'public' ],
101 $this->blog_data[ 'site_url' ]
102 );
103 }
104
105 private function update_user_settings() {
106 global $wpdb;
107
108 $wpdb->query(
109 $wpdb->prepare(
110 "UPDATE $wpdb->users
111 SET user_pass = '%s', user_activation_key = ''
112 WHERE ID = '%d'",
113 $this->user->user_pass, $this->user->ID
114 )
115 );
116
117 $this->remove_password_nag( $this->user->ID );
118 }
119
120 private function remove_password_nag( $user_id ) {
121 if ( get_user_meta( $user_id, 'default_password_nag' ) ) {
122 delete_user_meta( $user_id, 'default_password_nag' );
123 }
124 }
125
126 private function restore_backup() {
127 $this->delete_backup_table_rows( $this->preserved );
128 $this->restore_backup_tables( $this->backup );
129 $this->remove_user_session_tokens();
130 $this->reset_user_auth_cookie();
131 $this->assert_theme_data_needs_reset();
132 }
133
134 private function delete_backup_table_rows( array $tables ) {
135 global $wpdb;
136
137 foreach ( $tables as $table ) {
138 $wpdb->query( "DELETE FROM {$table}" );
139 }
140 }
141
142 private function restore_backup_tables( array $tables ) {
143 global $wpdb;
144
145 foreach ( $tables as $table => $data ) {
146 foreach ( $data as $row ) {
147 $columns = $values = array();
148
149 foreach ( $row as $column => $value ) {
150 $columns[] = $column;
151 $values[] = esc_sql( $value );
152 }
153
154 $wpdb->query( "INSERT INTO $table (" . implode( ', ', $columns ) . ") VALUES ('" . implode( "', '", $values ) . "')" );
155 }
156 }
157 }
158
159 private function remove_user_session_tokens() {
160 if ( get_user_meta( $this->user->ID, 'session_tokens' ) ) {
161 delete_user_meta( $this->user->ID, 'session_tokens' );
162 }
163 }
164
165 private function reset_user_auth_cookie() {
166 if ( ! is_command_line() ) {
167 wp_clear_auth_cookie();
168 wp_set_auth_cookie( $this->user->ID );
169 }
170 }
171
172 private function assert_theme_data_needs_reset() {
173 if ( $this->should_restore_theme_data() ) {
174 $this->restore_theme_data();
175 }
176 }
177
178 private function restore_theme_data() {
179 update_option( 'active_plugins', $this->theme_data['active-plugins'] );
180 update_option( 'template', $this->theme_data['template'] );
181 update_option( 'stylesheet', $this->theme_data['stylesheet'] );
182
183 if ( ! empty( $this->theme_data['current-theme'] ) ) {
184 update_option( 'current_theme', $this->theme_data['current-theme'] );
185 }
186 }
187
188 public function set_reactivate( $with_theme_data ) {
189 $this->reactivate = $with_theme_data;
190 }
191
192 private function set_wp_tables() {
193 global $wpdb;
194 $this->wp_tables = $wpdb->tables();
195 }
196
197 public function get_wp_tables() {
198 return $this->wp_tables;
199 }
200
201 private function set_user() {
202 global $current_user;
203
204 $this->user = ( 0 !== $current_user->ID ) ?
205 wp_get_current_user() :
206 get_userdata( 1 );
207 }
208
209 public function get_user() {
210 return $this->user;
211 }
212
213 }
214
215 endif;
216