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
wordpress-database-reset / class-db-reset-admin.php

class-db-reset-admin.php in Database Reset 3.25, at class-db-reset-admin.php

351 lines 10.8 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
8 private $code;
9 private $notice_error;
10 private $notice_success;
11 private $request;
12 private $resetter;
13 private $user;
14 private $version;
15 private $wp_tables;
16
17 public function __construct($version)
18 {
19 $this->resetter = new DB_Resetter();
20 $this->version = $version;
21
22 $this->set_request($_REQUEST); //phpcs:ignore
23 $this->set_view_variables();
24 }
25
26 private function set_request(array $request)
27 {
28 $this->request = $request;
29 }
30
31 private function set_view_variables()
32 {
33 $this->set_code();
34 $this->set_user();
35 $this->set_wp_tables();
36 }
37
38 private function set_code()
39 {
40 $this->code = $this->generate_code();
41 }
42
43 private function set_user()
44 {
45 $this->user = $this->resetter->get_user();
46 }
47
48 private function set_wp_tables()
49 {
50 $this->wp_tables = $this->resetter->get_wp_tables();
51 }
52
53 private function generate_code($length = 5)
54 {
55 return strtoupper(substr(md5(time()), 1, $length));
56 }
57
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'));
68 }
69
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()) {
205 try {
206 $this->resetter->set_reactivate($this->request['db-reset-reactivate-theme-data']);
207 $this->resetter->reset($this->request['db-reset-tables']);
208 $this->handle_after_reset();
209 } catch (Exception $e) {
210 $this->notice_error = $e->getMessage();
211 }
212 }
213 }
214
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();
220 }
221
222 private function handle_after_reset()
223 {
224 if (empty($this->request['db-reset-reactivate-theme-data'])) {
225 wp_safe_redirect(admin_url());
226 exit;
227 }
228
229 $this->notice_success = __('The selected tables were reset', 'wordpress-database-reset');
230 }
231
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);
236
237 return true;
238 }
239
240 private function set_empty_request_key($key, $default)
241 {
242 if (!array_key_exists($key, $this->request)) {
243 $this->request[$key] = $default;
244 }
245 }
246
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');
254 return false;
255 }
256
257 return true;
258 }
259
260 public function add_tools_menu()
261 {
262 $plugin_page = add_management_page(
263 __('Database Reset', 'wordpress-database-reset'),
264 __('Database Reset', 'wordpress-database-reset'),
265 'manage_options',
266 'database-reset',
267 array($this, 'render')
268 );
269
270 add_action('load-' . $plugin_page, array($this, 'load_assets'));
271 }
272
273 public function render()
274 {
275 require_once(DB_RESET_PATH . '/views/index.php');
276 }
277
278 public function load_assets()
279 {
280 $this->load_stylesheets();
281 $this->load_javascript();
282 }
283
284 private function load_stylesheets()
285 {
286 wp_enqueue_style(
287 'bsmselect',
288 plugins_url('assets/css/bsmselect.css', __FILE__),
289 array(),
290 $this->version
291 );
292
293 wp_enqueue_style(
294 'database-reset',
295 plugins_url('assets/css/database-reset.css', __FILE__),
296 array('bsmselect'),
297 $this->version
298 );
299 }
300
301 private function load_javascript()
302 {
303 wp_enqueue_script('jquery-ui-dialog');
304
305 wp_enqueue_script(
306 'bsmselect',
307 plugins_url('assets/js/bsmselect.js', __FILE__),
308 array('jquery'),
309 $this->version,
310 true
311 );
312
313 wp_enqueue_script(
314 'bsmselect-compatibility',
315 plugins_url('assets/js/bsmselect.compatibility.js', __FILE__),
316 array('bsmselect'),
317 $this->version,
318 true
319 );
320
321 wp_enqueue_script(
322 'database-reset',
323 plugins_url('assets/js/database-reset.js', __FILE__),
324 array('bsmselect', 'bsmselect-compatibility'),
325 $this->version,
326 true
327 );
328
329 wp_enqueue_style('wp-jquery-ui-dialog');
330
331 wp_localize_script(
332 'database-reset',
333 'dbReset',
334 $this->load_javascript_vars()
335 );
336 }
337
338 private function load_javascript_vars()
339 {
340 return array(
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) . '">',
346 );
347 }
348 }
349
350 endif;
351