PluginProbe
Database Reset / 3.1
Database Reset v3.1
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.1, at class-db-reset-admin.php

362 lines 11.9 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 private $code;
8 private $notice_error;
9 private $notice_success;
10 private $request;
11 private $resetter;
12 private $user;
13 private $version;
14 private $wp_tables;
15
16 public function __construct( $version ) {
17 $this->resetter = new DB_Resetter();
18 $this->version = $version;
19
20 $this->set_request( $_REQUEST );
21 $this->set_view_variables();
22 }
23
24 private function set_request( array $request ) {
25 $this->request = $request;
26 }
27
28 private function set_view_variables() {
29 $this->set_code();
30 $this->set_user();
31 $this->set_wp_tables();
32 }
33
34 private function set_code() {
35 $this->code = $this->generate_code();
36 }
37
38 private function set_user() {
39 $this->user = $this->resetter->get_user();
40 }
41
42 private function set_wp_tables() {
43 $this->wp_tables = $this->resetter->get_wp_tables();
44 }
45
46 private function generate_code( $length = 5 ) {
47 return strtoupper( substr( md5( time() ), 1, $length ) );
48 }
49
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' ) );
54
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' ) );
59 }
60
61
62 // additional powered by text in admin footer; only on plugin's page
63 static function admin_footer_text($text) {
64 $current_screen = get_current_screen();
65
66 if ( $current_screen->id != 'tools_page_database-reset' ) {
67 return $text;
68 }
69
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;
71
72 return $text;
73 } // admin_footer_text
74
75
76 // add settings link to plugins page
77 function plugin_action_links($links) {
78 $settings_link = '<a href="' . admin_url('tools.php?page=database-reset') . '" title="' . __('Reset Database', 'wordpress-database-reset') . '">' . __('Reset Database', 'wordpress-database-reset') . '</a>';
79
80 array_unshift( $links, $settings_link );
81
82 return $links;
83 } // plugin_action_links
84
85
86 // add links to plugin's description in plugins table
87 function plugin_meta_links($links, $file) {
88 $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
90
91 if ( $file == plugin_basename( DB_RESET_FILE ) ) {
92 $links[] = $support_link;
93 }
94
95 return $links;
96 } // plugin_meta_links
97
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
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 // auto download / install / activate WPR plugin
155 function install_wpr() {
156 if (false === current_user_can('administrator')) {
157 wp_die('Sorry, you have to be an admin to run this action.');
158 }
159
160 $plugin_slug = 'wp-reset/wp-reset.php';
161 $plugin_zip = 'https://downloads.wordpress.org/plugin/wp-reset.latest-stable.zip';
162
163 @include_once ABSPATH . 'wp-admin/includes/plugin.php';
164 @include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
165 @include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
166 @include_once ABSPATH . 'wp-admin/includes/file.php';
167 @include_once ABSPATH . 'wp-admin/includes/misc.php';
168 echo '<style>
169 body{
170 font-family: sans-serif;
171 font-size: 14px;
172 line-height: 1.5;
173 color: #444;
174 }
175 </style>';
176
177 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>';
179 echo 'Starting ...<br><br>';
180
181 wp_cache_flush();
182 $upgrader = new Plugin_Upgrader();
183 echo 'Check if WP Reset is already installed ... <br />';
184 if ($this->is_plugin_installed($plugin_slug)) {
185 echo 'WP Reset is already installed! <br /><br />Making sure it\'s the latest version.<br />';
186 $upgrader->upgrade($plugin_slug);
187 $installed = true;
188 } else {
189 echo 'Installing WP Reset.<br />';
190 $installed = $upgrader->install($plugin_zip);
191 }
192 wp_cache_flush();
193
194 if (!is_wp_error($installed) && $installed) {
195 echo 'Activating WP Reset.<br />';
196 $activate = activate_plugin($plugin_slug);
197
198 if (is_null($activate)) {
199 echo 'WP Reset Activated.<br />';
200
201 echo '<script>setTimeout(function() { top.location = "tools.php?page=wp-reset"; }, 1000);</script>';
202 echo '<br>If you are not redirected in a few seconds - <a href="tools.php?page=wp-reset" target="_parent">click here</a>.';
203 }
204 } 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>.';
206 }
207
208 echo '</div>';
209 } // install_wpr
210
211
212 function is_plugin_installed($slug) {
213 if ( !function_exists( 'get_plugins' ) ) {
214 require_once ABSPATH . 'wp-admin/includes/plugin.php';
215 }
216 $all_plugins = get_plugins();
217
218 if ( !empty( $all_plugins[$slug] ) ) {
219 return true;
220 } else {
221 return false;
222 }
223 } // is_plugin_installed
224
225
226 public function reset() {
227 if ( $this->form_is_safe_to_submit() ) {
228 try {
229 $this->resetter->set_reactivate( $this->request[ 'db-reset-reactivate-theme-data' ] );
230 $this->resetter->reset( $this->request[ 'db-reset-tables' ] );
231 $this->handle_after_reset();
232 } catch ( Exception $e ) {
233 $this->notice_error = $e->getMessage();
234 }
235 }
236 }
237
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();
242 }
243
244 private function handle_after_reset() {
245 if ( empty( $this->request[ 'db-reset-reactivate-theme-data' ] ) ) {
246 wp_redirect( admin_url() );
247 exit;
248 }
249
250 $this->notice_success = __( 'The selected tables were reset', 'wordpress-database-reset' );
251 }
252
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 );
256
257 return true;
258 }
259
260 private function set_empty_request_key( $key, $default ) {
261 if ( ! array_key_exists( $key, $this->request ) ) {
262 $this->request[ $key ] = $default;
263 }
264 }
265
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' );
270 return false;
271 }
272
273 return true;
274 }
275
276 public function add_tools_menu() {
277 $plugin_page = add_management_page(
278 __( 'Database Reset', 'wordpress-database-reset' ),
279 __( 'Database Reset', 'wordpress-database-reset' ),
280 'manage_options',
281 'database-reset',
282 array( $this, 'render' )
283 );
284
285 add_action( 'load-' . $plugin_page, array( $this, 'load_assets' ) );
286 }
287
288 public function render() {
289 require_once( DB_RESET_PATH . '/views/index.php' );
290 }
291
292 public function load_assets() {
293 $this->load_stylesheets();
294 $this->load_javascript();
295 }
296
297 private function load_stylesheets() {
298 wp_enqueue_style(
299 'bsmselect',
300 plugins_url( 'assets/css/bsmselect.css', __FILE__ ),
301 array(),
302 $this->version
303 );
304
305 wp_enqueue_style(
306 'database-reset',
307 plugins_url( 'assets/css/database-reset.css', __FILE__ ),
308 array('bsmselect'),
309 $this->version
310 );
311 }
312
313 private function load_javascript() {
314 wp_enqueue_script( 'jquery-ui-dialog' );
315
316 wp_enqueue_script(
317 'bsmselect',
318 plugins_url( 'assets/js/bsmselect.js', __FILE__ ),
319 array( 'jquery' ),
320 $this->version,
321 true
322 );
323
324 wp_enqueue_script(
325 'bsmselect-compatibility',
326 plugins_url( 'assets/js/bsmselect.compatibility.js', __FILE__ ),
327 array( 'bsmselect' ),
328 $this->version,
329 true
330 );
331
332 wp_enqueue_script(
333 'database-reset',
334 plugins_url( 'assets/js/database-reset.js', __FILE__ ),
335 array( 'bsmselect', 'bsmselect-compatibility' ),
336 $this->version,
337 true
338 );
339
340 wp_enqueue_style( 'wp-jquery-ui-dialog' );
341
342 wp_localize_script(
343 'database-reset',
344 'dbReset',
345 $this->load_javascript_vars()
346 );
347 }
348
349 private function load_javascript_vars() {
350 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 ) . '">',
356 );
357 }
358
359 }
360
361 endif;
362