PluginProbe
Database Reset / 3.21
Database Reset v3.21
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 +225 -66 3.03.21 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,243 @@
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);
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 + 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()) {
63 203 try {
64 - $this->resetter->set_reactivate( $this->request[ 'db-reset-reactivate-theme-data' ] );
65 - $this->resetter->reset( $this->request[ 'db-reset-tables' ] );
204 + $this->resetter->set_reactivate($this->request['db-reset-reactivate-theme-data']);
205 + $this->resetter->reset($this->request['db-reset-tables']);
66 206 $this->handle_after_reset();
67 - } catch ( Exception $e ) {
207 + } catch (Exception $e) {
68 208 $this->notice_error = $e->getMessage();
69 209 }
70 210 }
71 211 }
72 212
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();
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();
77 218 }
78 219
79 - private function handle_after_reset() {
80 - if ( empty( $this->request[ 'db-reset-reactivate-theme-data' ] ) ) {
81 - wp_redirect( admin_url() );
220 + private function handle_after_reset()
221 + {
222 + if (empty($this->request['db-reset-reactivate-theme-data'])) {
223 + wp_redirect(admin_url());
82 224 exit;
83 225 }
84 226
85 - $this->notice_success = __( 'The selected tables were reset', 'wordpress-database-reset' );
227 + $this->notice_success = __('The selected tables were reset', 'wordpress-database-reset');
86 228 }
87 229
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 );
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);
91 234
92 235 return true;
93 236 }
94 237
95 - private function set_empty_request_key( $key, $default ) {
96 - if ( ! array_key_exists( $key, $this->request ) ) {
97 - $this->request[ $key ] = $default;
238 + private function set_empty_request_key($key, $default)
239 + {
240 + if (!array_key_exists($key, $this->request)) {
241 + $this->request[$key] = $default;
98 242 }
99 243 }
100 244
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' );
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');
105 252 return false;
106 253 }
107 254
108 255 return true;
@@ -107,33 +254,37 @@
107 254
108 255 return true;
109 256 }
110 257
111 - public function add_tools_menu() {
258 + public function add_tools_menu()
259 + {
112 260 $plugin_page = add_management_page(
113 - __( 'Database Reset', 'wordpress-database-reset' ),
114 - __( 'Database Reset', 'wordpress-database-reset' ),
115 - 'update_core',
261 + __('Database Reset', 'wordpress-database-reset'),
262 + __('Database Reset', 'wordpress-database-reset'),
263 + 'manage_options',
116 264 'database-reset',
117 - array( $this, 'render' )
265 + array($this, 'render')
118 266 );
119 267
120 - add_action( 'load-' . $plugin_page, array( $this, 'load_assets' ) );
268 + add_action('load-' . $plugin_page, array($this, 'load_assets'));
121 269 }
122 270
123 - public function render() {
124 - require_once( DB_RESET_PATH . '/views/index.php' );
271 + public function render()
272 + {
273 + require_once(DB_RESET_PATH . '/views/index.php');
125 274 }
126 275
127 - public function load_assets() {
276 + public function load_assets()
277 + {
128 278 $this->load_stylesheets();
129 279 $this->load_javascript();
130 280 }
131 281
132 - private function load_stylesheets() {
282 + private function load_stylesheets()
283 + {
133 284 wp_enqueue_style(
134 285 'bsmselect',
135 - plugins_url( 'assets/css/bsmselect.css', __FILE__ ),
286 + plugins_url('assets/css/bsmselect.css', __FILE__),
136 287 array(),
137 288 $this->version
138 289 );
139 290
@@ -138,19 +289,22 @@
138 289 );
139 290
140 291 wp_enqueue_style(
141 292 'database-reset',
142 - plugins_url( 'assets/css/database-reset.css', __FILE__ ),
293 + plugins_url('assets/css/database-reset.css', __FILE__),
143 294 array('bsmselect'),
144 295 $this->version
145 296 );
146 297 }
147 298
148 - private function load_javascript() {
299 + private function load_javascript()
300 + {
301 + wp_enqueue_script('jquery-ui-dialog');
302 +
149 303 wp_enqueue_script(
150 304 'bsmselect',
151 - plugins_url( 'assets/js/bsmselect.js', __FILE__ ),
152 - array( 'jquery' ),
305 + plugins_url('assets/js/bsmselect.js', __FILE__),
306 + array('jquery'),
153 307 $this->version,
154 308 true
155 309 );
156 310
@@ -155,10 +309,10 @@
155 309 );
156 310
157 311 wp_enqueue_script(
158 312 'bsmselect-compatibility',
159 - plugins_url( 'assets/js/bsmselect.compatibility.js', __FILE__ ),
160 - array( 'bsmselect' ),
313 + plugins_url('assets/js/bsmselect.compatibility.js', __FILE__),
314 + array('bsmselect'),
161 315 $this->version,
162 316 true
163 317 );
164 318
@@ -163,14 +317,16 @@
163 317 );
164 318
165 319 wp_enqueue_script(
166 320 'database-reset',
167 - plugins_url( 'assets/js/database-reset.js', __FILE__ ),
168 - array( 'bsmselect', 'bsmselect-compatibility' ),
321 + plugins_url('assets/js/database-reset.js', __FILE__),
322 + array('bsmselect', 'bsmselect-compatibility'),
169 323 $this->version,
170 324 true
171 325 );
172 326
327 + wp_enqueue_style('wp-jquery-ui-dialog');
328 +
173 329 wp_localize_script(
174 330 'database-reset',
175 331 'dbReset',
176 332 $this->load_javascript_vars()
@@ -176,14 +332,17 @@
176 332 $this->load_javascript_vars()
177 333 );
178 334 }
179 335
180 - private function load_javascript_vars() {
336 + private function load_javascript_vars()
337 + {
181 338 return array(
182 - 'confirmAlert' => __( 'Are you sure you want to continue?', 'wordpress-database-reset' ),
183 - 'selectTable' => __( 'Select Tables', 'wordpress-database-reset' )
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) . '">',
184 344 );
185 345 }
186 -
187 346 }
188 347
189 348 endif;