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 +125 -138 3.13.21 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);
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,12 @@
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 + {
156 129 if (false === current_user_can('administrator')) {
157 130 wp_die('Sorry, you have to be an admin to run this action.');
158 131 }
159 132
@@ -174,9 +147,9 @@
174 147 }
175 148 </style>';
176 149
177 150 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>';
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>';
179 152 echo 'Starting ...<br><br>';
180 153
181 154 wp_cache_flush();
182 155 $upgrader = new Plugin_Upgrader();
@@ -201,9 +174,9 @@
201 174 echo '<script>setTimeout(function() { top.location = "tools.php?page=wp-reset"; }, 1000);</script>';
202 175 echo '<br>If you are not redirected in a few seconds - <a href="tools.php?page=wp-reset" target="_parent">click here</a>.';
203 176 }
204 177 } 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>.';
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>.';
206 179 }
207 180
208 181 echo '</div>';
209 182 } // install_wpr
@@ -208,15 +181,16 @@
208 181 echo '</div>';
209 182 } // install_wpr
210 183
211 184
212 - function is_plugin_installed($slug) {
213 - if ( !function_exists( 'get_plugins' ) ) {
185 + function is_plugin_installed($slug)
186 + {
187 + if (!function_exists('get_plugins')) {
214 188 require_once ABSPATH . 'wp-admin/includes/plugin.php';
215 189 }
216 190 $all_plugins = get_plugins();
217 -
218 - if ( !empty( $all_plugins[$slug] ) ) {
191 +
192 + if (!empty($all_plugins[$slug])) {
219 193 return true;
220 194 } else {
221 195 return false;
222 196 }
@@ -222,52 +196,60 @@
222 196 }
223 197 } // is_plugin_installed
224 198
225 199
226 - public function reset() {
227 - if ( $this->form_is_safe_to_submit() ) {
200 + public function reset()
201 + {
202 + if ($this->form_is_safe_to_submit()) {
228 203 try {
229 - $this->resetter->set_reactivate( $this->request[ 'db-reset-reactivate-theme-data' ] );
230 - $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']);
231 206 $this->handle_after_reset();
232 - } catch ( Exception $e ) {
207 + } catch (Exception $e) {
233 208 $this->notice_error = $e->getMessage();
234 209 }
235 210 }
236 211 }
237 212
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();
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();
242 218 }
243 219
244 - private function handle_after_reset() {
245 - if ( empty( $this->request[ 'db-reset-reactivate-theme-data' ] ) ) {
246 - 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());
247 224 exit;
248 225 }
249 226
250 - $this->notice_success = __( 'The selected tables were reset', 'wordpress-database-reset' );
227 + $this->notice_success = __('The selected tables were reset', 'wordpress-database-reset');
251 228 }
252 229
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 );
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);
256 234
257 235 return true;
258 236 }
259 237
260 - private function set_empty_request_key( $key, $default ) {
261 - if ( ! array_key_exists( $key, $this->request ) ) {
262 - $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;
263 242 }
264 243 }
265 244
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' );
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');
270 252 return false;
271 253 }
272 254
273 255 return true;
@@ -272,33 +254,37 @@
272 254
273 255 return true;
274 256 }
275 257
276 - public function add_tools_menu() {
258 + public function add_tools_menu()
259 + {
277 260 $plugin_page = add_management_page(
278 - __( 'Database Reset', 'wordpress-database-reset' ),
279 - __( 'Database Reset', 'wordpress-database-reset' ),
261 + __('Database Reset', 'wordpress-database-reset'),
262 + __('Database Reset', 'wordpress-database-reset'),
280 263 'manage_options',
281 264 'database-reset',
282 - array( $this, 'render' )
265 + array($this, 'render')
283 266 );
284 267
285 - add_action( 'load-' . $plugin_page, array( $this, 'load_assets' ) );
268 + add_action('load-' . $plugin_page, array($this, 'load_assets'));
286 269 }
287 270
288 - public function render() {
289 - require_once( DB_RESET_PATH . '/views/index.php' );
271 + public function render()
272 + {
273 + require_once(DB_RESET_PATH . '/views/index.php');
290 274 }
291 275
292 - public function load_assets() {
276 + public function load_assets()
277 + {
293 278 $this->load_stylesheets();
294 279 $this->load_javascript();
295 280 }
296 281
297 - private function load_stylesheets() {
282 + private function load_stylesheets()
283 + {
298 284 wp_enqueue_style(
299 285 'bsmselect',
300 - plugins_url( 'assets/css/bsmselect.css', __FILE__ ),
286 + plugins_url('assets/css/bsmselect.css', __FILE__),
301 287 array(),
302 288 $this->version
303 289 );
304 290
@@ -303,21 +289,22 @@
303 289 );
304 290
305 291 wp_enqueue_style(
306 292 'database-reset',
307 - plugins_url( 'assets/css/database-reset.css', __FILE__ ),
293 + plugins_url('assets/css/database-reset.css', __FILE__),
308 294 array('bsmselect'),
309 295 $this->version
310 296 );
311 297 }
312 298
313 - private function load_javascript() {
314 - wp_enqueue_script( 'jquery-ui-dialog' );
315 -
299 + private function load_javascript()
300 + {
301 + wp_enqueue_script('jquery-ui-dialog');
302 +
316 303 wp_enqueue_script(
317 304 'bsmselect',
318 - plugins_url( 'assets/js/bsmselect.js', __FILE__ ),
319 - array( 'jquery' ),
305 + plugins_url('assets/js/bsmselect.js', __FILE__),
306 + array('jquery'),
320 307 $this->version,
321 308 true
322 309 );
323 310
@@ -322,10 +309,10 @@
322 309 );
323 310
324 311 wp_enqueue_script(
325 312 'bsmselect-compatibility',
326 - plugins_url( 'assets/js/bsmselect.compatibility.js', __FILE__ ),
327 - array( 'bsmselect' ),
313 + plugins_url('assets/js/bsmselect.compatibility.js', __FILE__),
314 + array('bsmselect'),
328 315 $this->version,
329 316 true
330 317 );
331 318
@@ -330,15 +317,15 @@
330 317 );
331 318
332 319 wp_enqueue_script(
333 320 'database-reset',
334 - plugins_url( 'assets/js/database-reset.js', __FILE__ ),
335 - array( 'bsmselect', 'bsmselect-compatibility' ),
321 + plugins_url('assets/js/database-reset.js', __FILE__),
322 + array('bsmselect', 'bsmselect-compatibility'),
336 323 $this->version,
337 324 true
338 325 );
339 326
340 - wp_enqueue_style( 'wp-jquery-ui-dialog' );
327 + wp_enqueue_style('wp-jquery-ui-dialog');
341 328
342 329 wp_localize_script(
343 330 'database-reset',
344 331 'dbReset',
@@ -345,17 +332,17 @@
345 332 $this->load_javascript_vars()
346 333 );
347 334 }
348 335
349 - private function load_javascript_vars() {
336 + private function load_javascript_vars()
337 + {
350 338 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' ),
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'),
354 342 '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 ) . '">',
343 + 'wprDialogTitle' => '<img alt="WP Reset" title="WP Reset" src="' . plugins_url('assets/images/wp-reset-logo.png', DB_RESET_FILE) . '">',
356 344 );
357 345 }
358 -
359 346 }
360 347
361 348 endif;