| 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, $current_user; |
| 143 |
$current_user = $this->user; |
| 144 |
|
| 145 |
$user_id = $this->reset_users ? 1 : $this->user->ID; |
| 146 |
|
| 147 |
$wpdb->query( |
| 148 |
$wpdb->prepare( |
| 149 |
"UPDATE $wpdb->users |
| 150 |
SET user_pass = '%s', user_activation_key = '' |
| 151 |
WHERE ID = '%d'", |
| 152 |
$this->user->user_pass, |
| 153 |
$user_id |
| 154 |
) |
| 155 |
); |
| 156 |
|
| 157 |
if ($this->reset_users) { |
| 158 |
wp_clear_auth_cookie(); |
| 159 |
wp_set_auth_cookie($user_id); |
| 160 |
|
| 161 |
if (get_user_meta($user_id, 'default_password_nag')) { |
| 162 |
update_user_meta($user_id, 'default_password_nag', false); |
| 163 |
} |
| 164 |
|
| 165 |
if (get_user_meta($user_id, $wpdb->prefix . 'default_password_nag')) { |
| 166 |
update_user_meta($user_id, $wpdb->prefix . 'default_password_nag', false); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
private function restore_backup() |
| 172 |
{ |
| 173 |
$this->delete_backup_table_rows($this->preserved); |
| 174 |
$this->restore_backup_tables($this->backup); |
| 175 |
$this->restore_user_session_tokens(); |
| 176 |
$this->assert_theme_plugin_data_needs_reset(); |
| 177 |
} |
| 178 |
|
| 179 |
private function delete_backup_table_rows(array $tables) |
| 180 |
{ |
| 181 |
global $wpdb; |
| 182 |
|
| 183 |
foreach ($tables as $table) { |
| 184 |
$wpdb->query("DELETE FROM {$table}"); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
private function restore_backup_tables(array $tables) |
| 189 |
{ |
| 190 |
global $wpdb; |
| 191 |
|
| 192 |
foreach ($tables as $table => $data) { |
| 193 |
foreach ($data as $row) { |
| 194 |
$columns = $values = array(); |
| 195 |
|
| 196 |
foreach ($row as $column => $value) { |
| 197 |
$columns[] = $column; |
| 198 |
$values[] = esc_sql($value); |
| 199 |
} |
| 200 |
|
| 201 |
$wpdb->query("INSERT INTO $table (" . implode(', ', $columns) . ") VALUES ('" . implode("', '", $values) . "')"); |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
private function restore_user_session_tokens() |
| 207 |
{ |
| 208 |
add_user_meta($this->user->ID, 'session_tokens', $this->user_data['session_tokens']); |
| 209 |
} |
| 210 |
|
| 211 |
private function assert_theme_plugin_data_needs_reset() |
| 212 |
{ |
| 213 |
if ($this->should_restore_theme_plugin_data()) { |
| 214 |
$this->restore_theme_plugin_data(); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
private function restore_theme_plugin_data() |
| 219 |
{ |
| 220 |
update_option('active_plugins', $this->theme_plugin_data['active-plugins']); |
| 221 |
update_option('template', $this->theme_plugin_data['template']); |
| 222 |
update_option('stylesheet', $this->theme_plugin_data['stylesheet']); |
| 223 |
|
| 224 |
if (!empty($this->theme_plugin_data['current-theme'])) { |
| 225 |
update_option('current_theme', $this->theme_plugin_data['current-theme']); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
public function set_reactivate($with_theme_plugin_data) |
| 230 |
{ |
| 231 |
$this->reactivate = $with_theme_plugin_data; |
| 232 |
} |
| 233 |
|
| 234 |
private function set_wp_tables() |
| 235 |
{ |
| 236 |
global $wpdb; |
| 237 |
$this->wp_tables = $wpdb->tables(); |
| 238 |
} |
| 239 |
|
| 240 |
public function get_wp_tables() |
| 241 |
{ |
| 242 |
return $this->wp_tables; |
| 243 |
} |
| 244 |
|
| 245 |
private function set_user() |
| 246 |
{ |
| 247 |
global $current_user; |
| 248 |
|
| 249 |
$this->user = (0 !== $current_user->ID) ? |
| 250 |
wp_get_current_user() : |
| 251 |
get_userdata(1); |
| 252 |
} |
| 253 |
|
| 254 |
public function get_user() |
| 255 |
{ |
| 256 |
return $this->user; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
endif; |
| 261 |
|