PluginProbe
Database Reset / 3.19
Database Reset v3.19
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-reset-admin.php

class-db-reset-admin.php in Database Reset 3.19, at class-db-reset-admin.php

349 lines 10.7 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_Reset_Admin')) :
4
5 class DB_Reset_Admin
6 {
7
8 private $code;
9 private $notice_error;
10 private $notice_success;
11 private $request;
12 private $resetter;
13 private $user;
14 private $version;
15 private $wp_tables;
16
17 public function __construct($version)
18 {
19 $this->resetter = new DB_Resetter();
20 $this->version = $version;
21
22 $this->set_request($_REQUEST);
23 $this->set_view_variables();
24 }
25
26 private function set_request(array $request)
27 {
28 $this->request = $request;
29 }
30
31 private function set_view_variables()
32 {
33 $this->set_code();
34 $this->set_user();
35 $this->set_wp_tables();
36 }
37
38 private function set_code()
39 {
40 $this->code = $this->generate_code();
41 }
42
43 private function set_user()
44 {
45 $this->user = $this->resetter->get_user();
46 }
47
48 private function set_wp_tables()
49 {
50 $this->wp_tables = $this->resetter->get_wp_tables();
51 }
52
53 private function generate_code($length = 5)
54 {
55 return strtoupper(substr(md5(time()), 1, $length));
56 }
57
58 public function run()
59 {
60 add_action('admin_init', array($this, 'reset'));
61 add_action('admin_menu', array($this, 'add_tools_menu'));
62 add_action('admin_action_install_wpr', array($this, 'install_wpr'));
63
64 add_filter('plugin_action_links_' . plugin_basename(DB_RESET_FILE), array($this, 'plugin_action_links'));
65 add_filter('plugin_row_meta', array($this, 'plugin_meta_links'), 10, 2);
66 add_filter('admin_footer_text', array($this, 'admin_footer_text'));
67 add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
68 }
69
70 public function admin_enqueue_scripts() {
71 $current_screen = get_current_screen();
72 if ($current_screen->id != 'plugins') {
73 return;
74 }
75
76 wp_enqueue_script('db-reset', plugins_url('assets/js/database-reset-plugins.js', __FILE__), array('jquery'), $this->version, true);
77
78 $vars = array();
79 $vars['info_link'] = admin_url('plugin-install.php?tab=plugin-information&plugin=wp-reset&TB_iframe=true&width=600&height=800');
80 $vars['install_link'] = wp_nonce_url(add_query_arg(array('action' => 'install-plugin', 'plugin' => 'wp-reset'), admin_url('update.php')), 'install-plugin_wp-reset');
81
82 wp_localize_script('db-reset', 'db_reset', $vars);
83 } // admin_enqueue
84
85
86 // additional powered by text in admin footer; only on plugin's page
87 static function admin_footer_text($text)
88 {
89 $current_screen = get_current_screen();
90
91 if ($current_screen->id != 'tools_page_database-reset') {
92 return $text;
93 }
94
95 $text = '<i><a href="https://wordpress.org/plugins/wordpress-database-reset/" target="_blank">WP Database Reset</a> v' . DB_RESET_VERSION . ' by <a href="https://www.webfactoryltd.com/" title="' . __('Visit our site to get more great plugins', 'wordpress-database-reset') . '" target="_blank">' . __('WebFactory Ltd', 'wordpress-database-reset') . '</a>. Please <a href="https://wordpress.org/support/plugin/wordpress-database-reset/reviews/#new-post" target="_blank">rate the plugin &starf;&starf;&starf;&starf;&starf;</a>. Thank you!</i> ' . $text;
96
97 return $text;
98 } // admin_footer_text
99
100
101 // add settings link to plugins page
102 function plugin_action_links($links)
103 {
104 $settings_link = '<a href="' . admin_url('tools.php?page=database-reset') . '" title="' . __('Reset Database', 'wordpress-database-reset') . '">' . __('Reset Database', 'wordpress-database-reset') . '</a>';
105
106 array_unshift($links, $settings_link);
107
108 return $links;
109 } // plugin_action_links
110
111
112 // add links to plugin's description in plugins table
113 function plugin_meta_links($links, $file)
114 {
115 $support_link = '<a target="_blank" href="https://wordpress.org/support/plugin/wordpress-database-reset" title="' . __('Get help', 'wordpress-database-reset') . '">' . __('Support', 'wordpress-database-reset') . '</a>';
116
117
118 if ($file == plugin_basename(DB_RESET_FILE)) {
119 $links[] = $support_link;
120 }
121
122 return $links;
123 } // plugin_meta_links
124
125
126 // auto download / install / activate WPR plugin
127 function install_wpr()
128 {
129 if (false === current_user_can('administrator')) {
130 wp_die('Sorry, you have to be an admin to run this action.');
131 }
132
133 $plugin_slug = 'wp-reset/wp-reset.php';
134 $plugin_zip = 'https://downloads.wordpress.org/plugin/wp-reset.latest-stable.zip';
135
136 @include_once ABSPATH . 'wp-admin/includes/plugin.php';
137 @include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
138 @include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
139 @include_once ABSPATH . 'wp-admin/includes/file.php';
140 @include_once ABSPATH . 'wp-admin/includes/misc.php';
141 echo '<style>
142 body{
143 font-family: sans-serif;
144 font-size: 14px;
145 line-height: 1.5;
146 color: #444;
147 }
148 </style>';
149
150 echo '<div style="margin: 20px; color:#444;">';
151 echo 'If things are not done in a minute <a target="_parent" href="' . admin_url('plugin-install.php?s=wp-reset&tab=search&type=term') . '">install the plugin manually via Plugins page</a><br><br>';
152 echo 'Starting ...<br><br>';
153
154 wp_cache_flush();
155 $upgrader = new Plugin_Upgrader();
156 echo 'Check if WP Reset is already installed ... <br />';
157 if ($this->is_plugin_installed($plugin_slug)) {
158 echo 'WP Reset is already installed! <br /><br />Making sure it\'s the latest version.<br />';
159 $upgrader->upgrade($plugin_slug);
160 $installed = true;
161 } else {
162 echo 'Installing WP Reset.<br />';
163 $installed = $upgrader->install($plugin_zip);
164 }
165 wp_cache_flush();
166
167 if (!is_wp_error($installed) && $installed) {
168 echo 'Activating WP Reset.<br />';
169 $activate = activate_plugin($plugin_slug);
170
171 if (is_null($activate)) {
172 echo 'WP Reset Activated.<br />';
173
174 echo '<script>setTimeout(function() { top.location = "tools.php?page=wp-reset"; }, 1000);</script>';
175 echo '<br>If you are not redirected in a few seconds - <a href="tools.php?page=wp-reset" target="_parent">click here</a>.';
176 }
177 } else {
178 echo 'Could not install WP Reset. You\'ll have to <a target="_parent" href="' . admin_url('plugin-install.php?s=wp-reset&tab=search&type=term') . '">download and install manually</a>.';
179 }
180
181 echo '</div>';
182 } // install_wpr
183
184
185 function is_plugin_installed($slug)
186 {
187 if (!function_exists('get_plugins')) {
188 require_once ABSPATH . 'wp-admin/includes/plugin.php';
189 }
190 $all_plugins = get_plugins();
191
192 if (!empty($all_plugins[$slug])) {
193 return true;
194 } else {
195 return false;
196 }
197 } // is_plugin_installed
198
199
200 public function reset()
201 {
202 if ($this->form_is_safe_to_submit()) {
203 try {
204 $this->resetter->set_reactivate($this->request['db-reset-reactivate-theme-data']);
205 $this->resetter->reset($this->request['db-reset-tables']);
206 $this->handle_after_reset();
207 } catch (Exception $e) {
208 $this->notice_error = $e->getMessage();
209 }
210 }
211 }
212
213 private function form_is_safe_to_submit()
214 {
215 return isset($this->request['db-reset-code-confirm']) &&
216 $this->assert_request_variables_not_empty() &&
217 $this->assert_correct_code();
218 }
219
220 private function handle_after_reset()
221 {
222 if (empty($this->request['db-reset-reactivate-theme-data'])) {
223 wp_redirect(admin_url());
224 exit;
225 }
226
227 $this->notice_success = __('The selected tables were reset', 'wordpress-database-reset');
228 }
229
230 private function assert_request_variables_not_empty()
231 {
232 $this->set_empty_request_key('db-reset-tables', array());
233 $this->set_empty_request_key('db-reset-reactivate-theme-data', false);
234
235 return true;
236 }
237
238 private function set_empty_request_key($key, $default)
239 {
240 if (!array_key_exists($key, $this->request)) {
241 $this->request[$key] = $default;
242 }
243 }
244
245 private function assert_correct_code()
246 {
247 if (
248 $this->request['db-reset-code'] !==
249 $this->request['db-reset-code-confirm']
250 ) {
251 $this->notice_error = __('You entered the wrong security code', 'wordpress-database-reset');
252 return false;
253 }
254
255 return true;
256 }
257
258 public function add_tools_menu()
259 {
260 $plugin_page = add_management_page(
261 __('Database Reset', 'wordpress-database-reset'),
262 __('Database Reset', 'wordpress-database-reset'),
263 'manage_options',
264 'database-reset',
265 array($this, 'render')
266 );
267
268 add_action('load-' . $plugin_page, array($this, 'load_assets'));
269 }
270
271 public function render()
272 {
273 require_once(DB_RESET_PATH . '/views/index.php');
274 }
275
276 public function load_assets()
277 {
278 $this->load_stylesheets();
279 $this->load_javascript();
280 }
281
282 private function load_stylesheets()
283 {
284 wp_enqueue_style(
285 'bsmselect',
286 plugins_url('assets/css/bsmselect.css', __FILE__),
287 array(),
288 $this->version
289 );
290
291 wp_enqueue_style(
292 'database-reset',
293 plugins_url('assets/css/database-reset.css', __FILE__),
294 array('bsmselect'),
295 $this->version
296 );
297 }
298
299 private function load_javascript()
300 {
301 wp_enqueue_script('jquery-ui-dialog');
302
303 wp_enqueue_script(
304 'bsmselect',
305 plugins_url('assets/js/bsmselect.js', __FILE__),
306 array('jquery'),
307 $this->version,
308 true
309 );
310
311 wp_enqueue_script(
312 'bsmselect-compatibility',
313 plugins_url('assets/js/bsmselect.compatibility.js', __FILE__),
314 array('bsmselect'),
315 $this->version,
316 true
317 );
318
319 wp_enqueue_script(
320 'database-reset',
321 plugins_url('assets/js/database-reset.js', __FILE__),
322 array('bsmselect', 'bsmselect-compatibility'),
323 $this->version,
324 true
325 );
326
327 wp_enqueue_style('wp-jquery-ui-dialog');
328
329 wp_localize_script(
330 'database-reset',
331 'dbReset',
332 $this->load_javascript_vars()
333 );
334 }
335
336 private function load_javascript_vars()
337 {
338 return array(
339 'confirmAlert' => __('Are you sure you want to continue? There is NO UNDO!', 'wordpress-database-reset'),
340 'selectTable' => __('Select Tables', 'wordpress-database-reset'),
341 'selectOneTable' => __('Please select at least one table to reset.', 'wordpress-database-reset'),
342 'wprInstallUrl' => add_query_arg(array('action' => 'install_wpr'), admin_url('admin.php')),
343 'wprDialogTitle' => '<img alt="WP Reset" title="WP Reset" src="' . plugins_url('assets/images/wp-reset-logo.png', DB_RESET_FILE) . '">',
344 );
345 }
346 }
347
348 endif;
349