PluginProbe
Database Reset / 3.17
Database Reset v3.17
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.17, at class-db-resetter.php

252 lines 6.1 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
8 private $backup;
9 private $blog_data;
10 private $theme_plugin_data;
11 private $user_data;
12 private $preserved;
13 private $selected;
14 private $reactivate;
15 private $user;
16 private $wp_tables;
17 private $reset_users = false;
18
19 public function __construct()
20 {
21 $this->set_wp_tables();
22 $this->set_user();
23 }
24
25 public function reset(array $tables)
26 {
27 if (wp_verify_nonce(@$_REQUEST['submit_reset_form'], 'reset_nounce') && current_user_can('administrator')) {
28 // Check if current user is Admin and check the nonce
29
30 if (in_array('users', $tables)) {
31 $this->reset_users = true;
32 }
33
34 $this->validate_selected($tables);
35 $this->set_backup();
36 $this->reinstall();
37 $this->restore_backup();
38 } else {
39 throw new Exception(__('Please reload the page and try again. Double check your security code.', 'wordpress-database-reset'));
40 }
41 }
42
43 private function validate_selected(array $tables)
44 {
45 if (!empty($tables) && is_array($tables)) {
46 $this->selected = array_flip($tables);
47 return;
48 }
49
50 throw new Exception(__('You did not select any database tables', 'wordpress-database-reset'));
51 }
52
53 private function set_backup()
54 {
55 $this->set_tables_to_preserve($this->selected);
56 $this->back_up_tables($this->preserved);
57 $this->set_user_session_tokens();
58 $this->set_blog_data();
59
60 if ($this->should_restore_theme_plugin_data()) {
61 $this->set_theme_plugin_data();
62 }
63 }
64
65 private function set_tables_to_preserve(array $tables)
66 {
67 $this->preserved = array_diff_key($this->wp_tables, $tables);
68 }
69
70 private function back_up_tables(array $tables)
71 {
72 global $wpdb;
73 $this->backup = array();
74
75 foreach ($tables as $table) {
76 $this->backup[$table] = $wpdb->get_results("SELECT * FROM {$table}");
77 }
78 }
79
80 private function set_user_session_tokens()
81 {
82 if (get_user_meta($this->user->ID, 'session_tokens')) {
83 $this->user_data = array(
84 'session_tokens' => get_user_meta($this->user->ID, 'session_tokens', true)
85 );
86 }
87 }
88
89 private function set_blog_data()
90 {
91 $this->blog_data = array(
92 'name' => get_option('blogname'),
93 'public' => get_option('blog_public'),
94 'site_url' => get_option('siteurl')
95 );
96 }
97
98 private function should_restore_theme_plugin_data()
99 {
100 return ('true' === $this->reactivate);
101 }
102
103 private function set_theme_plugin_data()
104 {
105 $this->theme_plugin_data = array(
106 'active-plugins' => get_option('active_plugins'),
107 'current-theme' => get_option('current_theme'),
108 'stylesheet' => get_option('stylesheet'),
109 'template' => get_option('template')
110 );
111 }
112
113 private function reinstall()
114 {
115 $this->drop_wp_tables();
116 $this->install_wp();
117 $this->update_user_settings();
118 }
119
120 private function drop_wp_tables()
121 {
122 global $wpdb;
123
124 foreach ($this->wp_tables as $wp_table) {
125 $wpdb->query("DROP TABLE {$wp_table}");
126 }
127 }
128
129 private function install_wp()
130 {
131 return db_reset_install(
132 $this->blog_data['name'],
133 $this->user->user_login,
134 $this->user->user_email,
135 $this->blog_data['public'],
136 $this->blog_data['site_url']
137 );
138 }
139
140 private function update_user_settings()
141 {
142 global $wpdb;
143
144 $user_id = $this->reset_users ? 1 : $this->user->ID;
145
146 $wpdb->query(
147 $wpdb->prepare(
148 "UPDATE $wpdb->users
149 SET user_pass = '%s', user_activation_key = ''
150 WHERE ID = '%d'",
151 $this->user->user_pass,
152 $user_id
153 )
154 );
155
156 if ($this->reset_users) {
157 wp_clear_auth_cookie();
158 wp_set_auth_cookie(true);
159 }
160 }
161
162 private function restore_backup()
163 {
164 $this->delete_backup_table_rows($this->preserved);
165 $this->restore_backup_tables($this->backup);
166 $this->restore_user_session_tokens();
167 $this->assert_theme_plugin_data_needs_reset();
168 }
169
170 private function delete_backup_table_rows(array $tables)
171 {
172 global $wpdb;
173
174 foreach ($tables as $table) {
175 $wpdb->query("DELETE FROM {$table}");
176 }
177 }
178
179 private function restore_backup_tables(array $tables)
180 {
181 global $wpdb;
182
183 foreach ($tables as $table => $data) {
184 foreach ($data as $row) {
185 $columns = $values = array();
186
187 foreach ($row as $column => $value) {
188 $columns[] = $column;
189 $values[] = esc_sql($value);
190 }
191
192 $wpdb->query("INSERT INTO $table (" . implode(', ', $columns) . ") VALUES ('" . implode("', '", $values) . "')");
193 }
194 }
195 }
196
197 private function restore_user_session_tokens()
198 {
199 add_user_meta($this->user->ID, 'session_tokens', $this->user_data['session_tokens']);
200 }
201
202 private function assert_theme_plugin_data_needs_reset()
203 {
204 if ($this->should_restore_theme_plugin_data()) {
205 $this->restore_theme_plugin_data();
206 }
207 }
208
209 private function restore_theme_plugin_data()
210 {
211 update_option('active_plugins', $this->theme_plugin_data['active-plugins']);
212 update_option('template', $this->theme_plugin_data['template']);
213 update_option('stylesheet', $this->theme_plugin_data['stylesheet']);
214
215 if (!empty($this->theme_plugin_data['current-theme'])) {
216 update_option('current_theme', $this->theme_plugin_data['current-theme']);
217 }
218 }
219
220 public function set_reactivate($with_theme_plugin_data)
221 {
222 $this->reactivate = $with_theme_plugin_data;
223 }
224
225 private function set_wp_tables()
226 {
227 global $wpdb;
228 $this->wp_tables = $wpdb->tables();
229 }
230
231 public function get_wp_tables()
232 {
233 return $this->wp_tables;
234 }
235
236 private function set_user()
237 {
238 global $current_user;
239
240 $this->user = (0 !== $current_user->ID) ?
241 wp_get_current_user() :
242 get_userdata(1);
243 }
244
245 public function get_user()
246 {
247 return $this->user;
248 }
249 }
250
251 endif;
252