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