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 +128 -139 3.13.25 View file →
@@ -1,9 +1,10 @@
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 9 private $notice_error;
9 10 private $notice_success;
@@ -12,63 +13,87 @@
12 13 private $user;
13 14 private $version;
14 15 private $wp_tables;
15 16
16 - public function __construct( $version ) {
17 + public function __construct($version)
18 + {
17 19 $this->resetter = new DB_Resetter();
18 20 $this->version = $version;
19 21
20 - $this->set_request( $_REQUEST );
22 + $this->set_request($_REQUEST); //phpcs:ignore
21 23 $this->set_view_variables();
22 24 }
23 25
24 - private function set_request( array $request ) {
26 + private function set_request(array $request)
27 + {
25 28 $this->request = $request;
26 29 }
27 30
28 - private function set_view_variables() {
31 + private function set_view_variables()
32 + {
29 33 $this->set_code();
30 34 $this->set_user();
31 35 $this->set_wp_tables();
32 36 }
33 37
34 - private function set_code() {
38 + private function set_code()
39 + {
35 40 $this->code = $this->generate_code();
36 41 }
37 42
38 - private function set_user() {
43 + private function set_user()
44 + {
39 45 $this->user = $this->resetter->get_user();
40 46 }
41 47
42 - private function set_wp_tables() {
48 + private function set_wp_tables()
49 + {
43 50 $this->wp_tables = $this->resetter->get_wp_tables();
44 51 }
45 52
46 - private function generate_code( $length = 5 ) {
47 - return strtoupper( substr( md5( time() ), 1, $length ) );
53 + private function generate_code($length = 5)
54 + {
55 + return strtoupper(substr(md5(time()), 1, $length));
48 56 }
49 57
50 - public function run() {
51 - add_action( 'admin_init', array( $this, 'reset' ) );
52 - add_action( 'admin_menu', array( $this, 'add_tools_menu' ) );
53 - add_action( 'admin_action_install_wpr', array( $this, 'install_wpr' ) );
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'));
54 63
55 - add_filter( 'install_plugins_table_api_args_featured', array( $this, 'featured_plugins_tab' ) );
56 - add_filter( 'plugin_action_links_' . plugin_basename( DB_RESET_FILE ), array($this, 'plugin_action_links' ) );
57 - add_filter( 'plugin_row_meta', array( $this, 'plugin_meta_links' ), 10, 2 );
58 - add_filter( 'admin_footer_text', array( $this, 'admin_footer_text' ) );
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
70 + public function admin_enqueue_scripts() {
71 + $current_screen = get_current_screen();
72 + if ($current_screen->id != 'plugins') {
73 + return;
74 + }
61 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 +
62 86 // additional powered by text in admin footer; only on plugin's page
63 - static function admin_footer_text($text) {
87 + static function admin_footer_text($text)
88 + {
64 89 $current_screen = get_current_screen();
65 90
66 - if ( $current_screen->id != 'tools_page_database-reset' ) {
91 + if ($current_screen->id != 'tools_page_database-reset') {
67 92 return $text;
68 93 }
69 94
70 - $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;
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;
71 96
72 97 return $text;
73 98 } // admin_footer_text
74 99
@@ -73,12 +98,13 @@
73 98 } // admin_footer_text
74 99
75 100
76 101 // add settings link to plugins page
77 - function plugin_action_links($links) {
102 + function plugin_action_links($links)
103 + {
78 104 $settings_link = '<a href="' . admin_url('tools.php?page=database-reset') . '" title="' . __('Reset Database', 'wordpress-database-reset') . '">' . __('Reset Database', 'wordpress-database-reset') . '</a>';
79 105
80 - array_unshift( $links, $settings_link );
106 + array_unshift($links, $settings_link);
81 107
82 108 return $links;
83 109 } // plugin_action_links
84 110
@@ -83,13 +109,14 @@
83 109 } // plugin_action_links
84 110
85 111
86 112 // add links to plugin's description in plugins table
87 - function plugin_meta_links($links, $file) {
113 + function plugin_meta_links($links, $file)
114 + {
88 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>';
89 116
90 117
91 - if ( $file == plugin_basename( DB_RESET_FILE ) ) {
118 + if ($file == plugin_basename(DB_RESET_FILE)) {
92 119 $links[] = $support_link;
93 120 }
94 121
95 122 return $links;
@@ -94,66 +121,14 @@
94 121
95 122 return $links;
96 123 } // plugin_meta_links
97 124
98 -
99 - // helper function for adding plugins to fav list
100 - function featured_plugins_tab($args) {
101 - add_filter( 'plugins_api_result', array( $this, 'plugins_api_result'), 10, 3 );
102 125
103 - return $args;
104 - } // featured_plugins_tab
105 -
106 -
107 - // add our plugins to recommended list
108 - function plugins_api_result($res, $action, $args) {
109 - remove_filter( 'plugins_api_result', array( $this , 'plugins_api_result' ), 10, 3 );
110 -
111 - $res = $this->add_plugin_favs( 'eps-301-redirects', $res );
112 - $res = $this->add_plugin_favs( 'wp-htaccess-editor', $res );
113 - $res = $this->add_plugin_favs( 'under-construction-page', $res );
114 -
115 - return $res;
116 - } // plugins_api_result
117 -
118 -
119 - // add single plugin to list of favs
120 - function add_plugin_favs($plugin_slug, $res) {
121 - if (!empty($res->plugins) && is_array($res->plugins)) {
122 - foreach ($res->plugins as $plugin) {
123 - if (is_object($plugin) && !empty($plugin->slug) && $plugin->slug == $plugin_slug) {
124 - return $res;
125 - }
126 - } // foreach
127 - }
128 -
129 - if ($plugin_info = get_transient('wf-plugin-info-' . $plugin_slug)) {
130 - array_unshift($res->plugins, $plugin_info);
131 - } else {
132 - $plugin_info = plugins_api('plugin_information', array(
133 - 'slug' => $plugin_slug,
134 - 'is_ssl' => is_ssl(),
135 - 'fields' => array(
136 - 'banners' => true,
137 - 'reviews' => true,
138 - 'downloaded' => true,
139 - 'active_installs' => true,
140 - 'icons' => true,
141 - 'short_description' => true,
142 - )
143 - ));
144 - if (!is_wp_error($plugin_info)) {
145 - $res->plugins[] = $plugin_info;
146 - set_transient('wf-plugin-info-' . $plugin_slug, $plugin_info, DAY_IN_SECONDS * 7);
147 - }
148 - }
149 -
150 - return $res;
151 - } // add_plugin_favs
152 -
153 -
154 126 // auto download / install / activate WPR plugin
155 - function install_wpr() {
127 + function install_wpr()
128 + {
129 + check_ajax_referer('install_wpr');
130 +
156 131 if (false === current_user_can('administrator')) {
157 132 wp_die('Sorry, you have to be an admin to run this action.');
158 133 }
159 134
@@ -174,9 +149,9 @@
174 149 }
175 150 </style>';
176 151
177 152 echo '<div style="margin: 20px; color:#444;">';
178 - 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>';
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>';
179 154 echo 'Starting ...<br><br>';
180 155
181 156 wp_cache_flush();
182 157 $upgrader = new Plugin_Upgrader();
@@ -201,9 +176,9 @@
201 176 echo '<script>setTimeout(function() { top.location = "tools.php?page=wp-reset"; }, 1000);</script>';
202 177 echo '<br>If you are not redirected in a few seconds - <a href="tools.php?page=wp-reset" target="_parent">click here</a>.';
203 178 }
204 179 } else {
205 - 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>.';
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>.';
206 181 }
207 182
208 183 echo '</div>';
209 184 } // install_wpr
@@ -208,15 +183,16 @@
208 183 echo '</div>';
209 184 } // install_wpr
210 185
211 186
212 - function is_plugin_installed($slug) {
213 - if ( !function_exists( 'get_plugins' ) ) {
187 + function is_plugin_installed($slug)
188 + {
189 + if (!function_exists('get_plugins')) {
214 190 require_once ABSPATH . 'wp-admin/includes/plugin.php';
215 191 }
216 192 $all_plugins = get_plugins();
217 -
218 - if ( !empty( $all_plugins[$slug] ) ) {
193 +
194 + if (!empty($all_plugins[$slug])) {
219 195 return true;
220 196 } else {
221 197 return false;
222 198 }
@@ -222,52 +198,60 @@
222 198 }
223 199 } // is_plugin_installed
224 200
225 201
226 - public function reset() {
227 - if ( $this->form_is_safe_to_submit() ) {
202 + public function reset()
203 + {
204 + if ($this->form_is_safe_to_submit()) {
228 205 try {
229 - $this->resetter->set_reactivate( $this->request[ 'db-reset-reactivate-theme-data' ] );
230 - $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']);
231 208 $this->handle_after_reset();
232 - } catch ( Exception $e ) {
209 + } catch (Exception $e) {
233 210 $this->notice_error = $e->getMessage();
234 211 }
235 212 }
236 213 }
237 214
238 - private function form_is_safe_to_submit() {
239 - return isset( $this->request['db-reset-code-confirm'] ) &&
240 - $this->assert_request_variables_not_empty() &&
241 - $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();
242 220 }
243 221
244 - private function handle_after_reset() {
245 - if ( empty( $this->request[ 'db-reset-reactivate-theme-data' ] ) ) {
246 - 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());
247 226 exit;
248 227 }
249 228
250 - $this->notice_success = __( 'The selected tables were reset', 'wordpress-database-reset' );
229 + $this->notice_success = __('The selected tables were reset', 'wordpress-database-reset');
251 230 }
252 231
253 - private function assert_request_variables_not_empty() {
254 - $this->set_empty_request_key( 'db-reset-tables', array() );
255 - $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);
256 236
257 237 return true;
258 238 }
259 239
260 - private function set_empty_request_key( $key, $default ) {
261 - if ( ! array_key_exists( $key, $this->request ) ) {
262 - $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;
263 244 }
264 245 }
265 246
266 - private function assert_correct_code() {
267 - if ( $this->request['db-reset-code'] !==
268 - $this->request['db-reset-code-confirm'] ) {
269 - $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');
270 254 return false;
271 255 }
272 256
273 257 return true;
@@ -272,33 +256,37 @@
272 256
273 257 return true;
274 258 }
275 259
276 - public function add_tools_menu() {
260 + public function add_tools_menu()
261 + {
277 262 $plugin_page = add_management_page(
278 - __( 'Database Reset', 'wordpress-database-reset' ),
279 - __( 'Database Reset', 'wordpress-database-reset' ),
263 + __('Database Reset', 'wordpress-database-reset'),
264 + __('Database Reset', 'wordpress-database-reset'),
280 265 'manage_options',
281 266 'database-reset',
282 - array( $this, 'render' )
267 + array($this, 'render')
283 268 );
284 269
285 - add_action( 'load-' . $plugin_page, array( $this, 'load_assets' ) );
270 + add_action('load-' . $plugin_page, array($this, 'load_assets'));
286 271 }
287 272
288 - public function render() {
289 - require_once( DB_RESET_PATH . '/views/index.php' );
273 + public function render()
274 + {
275 + require_once(DB_RESET_PATH . '/views/index.php');
290 276 }
291 277
292 - public function load_assets() {
278 + public function load_assets()
279 + {
293 280 $this->load_stylesheets();
294 281 $this->load_javascript();
295 282 }
296 283
297 - private function load_stylesheets() {
284 + private function load_stylesheets()
285 + {
298 286 wp_enqueue_style(
299 287 'bsmselect',
300 - plugins_url( 'assets/css/bsmselect.css', __FILE__ ),
288 + plugins_url('assets/css/bsmselect.css', __FILE__),
301 289 array(),
302 290 $this->version
303 291 );
304 292
@@ -303,21 +291,22 @@
303 291 );
304 292
305 293 wp_enqueue_style(
306 294 'database-reset',
307 - plugins_url( 'assets/css/database-reset.css', __FILE__ ),
295 + plugins_url('assets/css/database-reset.css', __FILE__),
308 296 array('bsmselect'),
309 297 $this->version
310 298 );
311 299 }
312 300
313 - private function load_javascript() {
314 - wp_enqueue_script( 'jquery-ui-dialog' );
315 -
301 + private function load_javascript()
302 + {
303 + wp_enqueue_script('jquery-ui-dialog');
304 +
316 305 wp_enqueue_script(
317 306 'bsmselect',
318 - plugins_url( 'assets/js/bsmselect.js', __FILE__ ),
319 - array( 'jquery' ),
307 + plugins_url('assets/js/bsmselect.js', __FILE__),
308 + array('jquery'),
320 309 $this->version,
321 310 true
322 311 );
323 312
@@ -322,10 +311,10 @@
322 311 );
323 312
324 313 wp_enqueue_script(
325 314 'bsmselect-compatibility',
326 - plugins_url( 'assets/js/bsmselect.compatibility.js', __FILE__ ),
327 - array( 'bsmselect' ),
315 + plugins_url('assets/js/bsmselect.compatibility.js', __FILE__),
316 + array('bsmselect'),
328 317 $this->version,
329 318 true
330 319 );
331 320
@@ -330,15 +319,15 @@
330 319 );
331 320
332 321 wp_enqueue_script(
333 322 'database-reset',
334 - plugins_url( 'assets/js/database-reset.js', __FILE__ ),
335 - array( 'bsmselect', 'bsmselect-compatibility' ),
323 + plugins_url('assets/js/database-reset.js', __FILE__),
324 + array('bsmselect', 'bsmselect-compatibility'),
336 325 $this->version,
337 326 true
338 327 );
339 328
340 - wp_enqueue_style( 'wp-jquery-ui-dialog' );
329 + wp_enqueue_style('wp-jquery-ui-dialog');
341 330
342 331 wp_localize_script(
343 332 'database-reset',
344 333 'dbReset',
@@ -345,17 +334,17 @@
345 334 $this->load_javascript_vars()
346 335 );
347 336 }
348 337
349 - private function load_javascript_vars() {
338 + private function load_javascript_vars()
339 + {
350 340 return array(
351 - 'confirmAlert' => __( 'Are you sure you want to continue? There is NO UNDO!', 'wordpress-database-reset' ),
352 - 'selectTable' => __( 'Select Tables', 'wordpress-database-reset' ),
353 - 'selectOneTable' => __( 'Please select at least one table to reset.', 'wordpress-database-reset' ),
354 - 'wprInstallUrl' => add_query_arg(array('action' => 'install_wpr'), admin_url('admin.php')),
355 - 'wprDialogTitle' => '<img alt="WP Reset" title="WP Reset" src="' . plugins_url( 'assets/images/wp-reset-logo.png', DB_RESET_FILE ) . '">',
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) . '">',
356 346 );
357 347 }
358 -
359 348 }
360 349
361 350 endif;