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

396 lines 12.0 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);
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('install_plugins_table_api_args_featured', array($this, 'featured_plugins_tab'));
65 add_filter('install_plugins_table_api_args_recommended', array($this, 'featured_plugins_tab'));
66 add_filter('plugin_action_links_' . plugin_basename(DB_RESET_FILE), array($this, 'plugin_action_links'));
67 add_filter('plugin_row_meta', array($this, 'plugin_meta_links'), 10, 2);
68 add_filter('admin_footer_text', array($this, 'admin_footer_text'));
69 }
70
71
72 // additional powered by text in admin footer; only on plugin's page
73 static function admin_footer_text($text)
74 {
75 $current_screen = get_current_screen();
76
77 if ($current_screen->id != 'tools_page_database-reset') {
78 return $text;
79 }
80
81 $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;
82
83 return $text;
84 } // admin_footer_text
85
86
87 // add settings link to plugins page
88 function plugin_action_links($links)
89 {
90 $settings_link = '<a href="' . admin_url('tools.php?page=database-reset') . '" title="' . __('Reset Database', 'wordpress-database-reset') . '">' . __('Reset Database', 'wordpress-database-reset') . '</a>';
91
92 array_unshift($links, $settings_link);
93
94 return $links;
95 } // plugin_action_links
96
97
98 // add links to plugin's description in plugins table
99 function plugin_meta_links($links, $file)
100 {
101 $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>';
102
103
104 if ($file == plugin_basename(DB_RESET_FILE)) {
105 $links[] = $support_link;
106 }
107
108 return $links;
109 } // plugin_meta_links
110
111
112 // helper function for adding plugins to fav list
113 function featured_plugins_tab($args)
114 {
115 add_filter('plugins_api_result', array($this, 'plugins_api_result'), 10, 3);
116
117 return $args;
118 } // featured_plugins_tab
119
120
121 // add our plugins to recommended list
122 function plugins_api_result($res, $action, $args)
123 {
124 remove_filter('plugins_api_result', array($this, 'plugins_api_result'), 10, 3);
125
126 $res = $this->add_plugin_favs('under-construction-page', $res);
127 $res = $this->add_plugin_favs('simple-author-box', $res);
128 $res = $this->add_plugin_favs('eps-301-redirects', $res);
129
130 return $res;
131 } // plugins_api_result
132
133
134 function add_plugin_favs($plugin_slug, $res)
135 {
136 // check if plugin is already on the list
137 if (!empty($res->plugins) && is_array($res->plugins)) {
138 foreach ($res->plugins as $plugin) {
139 if (is_object($plugin) && !empty($plugin->slug) && $plugin->slug == $plugin_slug) {
140 return $res;
141 }
142 } // foreach
143 }
144
145 $plugin_info = get_transient('wf-plugin-info-' . $plugin_slug);
146
147 if (!$plugin_info) {
148 $plugin_info = plugins_api('plugin_information', array(
149 'slug' => $plugin_slug,
150 'is_ssl' => is_ssl(),
151 'fields' => array(
152 'banners' => true,
153 'reviews' => true,
154 'downloaded' => true,
155 'active_installs' => true,
156 'icons' => true,
157 'short_description' => true,
158 )
159 ));
160 if (!is_wp_error($plugin_info)) {
161 set_transient('wf-plugin-info-' . $plugin_slug, $plugin_info, DAY_IN_SECONDS * 7);
162 }
163 }
164
165 if (!empty($res->plugins) && is_array($res->plugins) && $plugin_info && is_object($plugin_info)) {
166 array_unshift($res->plugins, $plugin_info);
167 }
168
169 return $res;
170 } // add_plugin_featured
171
172
173 // auto download / install / activate WPR plugin
174 function install_wpr()
175 {
176 if (false === current_user_can('administrator')) {
177 wp_die('Sorry, you have to be an admin to run this action.');
178 }
179
180 $plugin_slug = 'wp-reset/wp-reset.php';
181 $plugin_zip = 'https://downloads.wordpress.org/plugin/wp-reset.latest-stable.zip';
182
183 @include_once ABSPATH . 'wp-admin/includes/plugin.php';
184 @include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
185 @include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
186 @include_once ABSPATH . 'wp-admin/includes/file.php';
187 @include_once ABSPATH . 'wp-admin/includes/misc.php';
188 echo '<style>
189 body{
190 font-family: sans-serif;
191 font-size: 14px;
192 line-height: 1.5;
193 color: #444;
194 }
195 </style>';
196
197 echo '<div style="margin: 20px; color:#444;">';
198 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>';
199 echo 'Starting ...<br><br>';
200
201 wp_cache_flush();
202 $upgrader = new Plugin_Upgrader();
203 echo 'Check if WP Reset is already installed ... <br />';
204 if ($this->is_plugin_installed($plugin_slug)) {
205 echo 'WP Reset is already installed! <br /><br />Making sure it\'s the latest version.<br />';
206 $upgrader->upgrade($plugin_slug);
207 $installed = true;
208 } else {
209 echo 'Installing WP Reset.<br />';
210 $installed = $upgrader->install($plugin_zip);
211 }
212 wp_cache_flush();
213
214 if (!is_wp_error($installed) && $installed) {
215 echo 'Activating WP Reset.<br />';
216 $activate = activate_plugin($plugin_slug);
217
218 if (is_null($activate)) {
219 echo 'WP Reset Activated.<br />';
220
221 echo '<script>setTimeout(function() { top.location = "tools.php?page=wp-reset"; }, 1000);</script>';
222 echo '<br>If you are not redirected in a few seconds - <a href="tools.php?page=wp-reset" target="_parent">click here</a>.';
223 }
224 } else {
225 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>.';
226 }
227
228 echo '</div>';
229 } // install_wpr
230
231
232 function is_plugin_installed($slug)
233 {
234 if (!function_exists('get_plugins')) {
235 require_once ABSPATH . 'wp-admin/includes/plugin.php';
236 }
237 $all_plugins = get_plugins();
238
239 if (!empty($all_plugins[$slug])) {
240 return true;
241 } else {
242 return false;
243 }
244 } // is_plugin_installed
245
246
247 public function reset()
248 {
249 if ($this->form_is_safe_to_submit()) {
250 try {
251 $this->resetter->set_reactivate($this->request['db-reset-reactivate-theme-data']);
252 $this->resetter->reset($this->request['db-reset-tables']);
253 $this->handle_after_reset();
254 } catch (Exception $e) {
255 $this->notice_error = $e->getMessage();
256 }
257 }
258 }
259
260 private function form_is_safe_to_submit()
261 {
262 return isset($this->request['db-reset-code-confirm']) &&
263 $this->assert_request_variables_not_empty() &&
264 $this->assert_correct_code();
265 }
266
267 private function handle_after_reset()
268 {
269 if (empty($this->request['db-reset-reactivate-theme-data'])) {
270 wp_redirect(admin_url());
271 exit;
272 }
273
274 $this->notice_success = __('The selected tables were reset', 'wordpress-database-reset');
275 }
276
277 private function assert_request_variables_not_empty()
278 {
279 $this->set_empty_request_key('db-reset-tables', array());
280 $this->set_empty_request_key('db-reset-reactivate-theme-data', false);
281
282 return true;
283 }
284
285 private function set_empty_request_key($key, $default)
286 {
287 if (!array_key_exists($key, $this->request)) {
288 $this->request[$key] = $default;
289 }
290 }
291
292 private function assert_correct_code()
293 {
294 if (
295 $this->request['db-reset-code'] !==
296 $this->request['db-reset-code-confirm']
297 ) {
298 $this->notice_error = __('You entered the wrong security code', 'wordpress-database-reset');
299 return false;
300 }
301
302 return true;
303 }
304
305 public function add_tools_menu()
306 {
307 $plugin_page = add_management_page(
308 __('Database Reset', 'wordpress-database-reset'),
309 __('Database Reset', 'wordpress-database-reset'),
310 'manage_options',
311 'database-reset',
312 array($this, 'render')
313 );
314
315 add_action('load-' . $plugin_page, array($this, 'load_assets'));
316 }
317
318 public function render()
319 {
320 require_once(DB_RESET_PATH . '/views/index.php');
321 }
322
323 public function load_assets()
324 {
325 $this->load_stylesheets();
326 $this->load_javascript();
327 }
328
329 private function load_stylesheets()
330 {
331 wp_enqueue_style(
332 'bsmselect',
333 plugins_url('assets/css/bsmselect.css', __FILE__),
334 array(),
335 $this->version
336 );
337
338 wp_enqueue_style(
339 'database-reset',
340 plugins_url('assets/css/database-reset.css', __FILE__),
341 array('bsmselect'),
342 $this->version
343 );
344 }
345
346 private function load_javascript()
347 {
348 wp_enqueue_script('jquery-ui-dialog');
349
350 wp_enqueue_script(
351 'bsmselect',
352 plugins_url('assets/js/bsmselect.js', __FILE__),
353 array('jquery'),
354 $this->version,
355 true
356 );
357
358 wp_enqueue_script(
359 'bsmselect-compatibility',
360 plugins_url('assets/js/bsmselect.compatibility.js', __FILE__),
361 array('bsmselect'),
362 $this->version,
363 true
364 );
365
366 wp_enqueue_script(
367 'database-reset',
368 plugins_url('assets/js/database-reset.js', __FILE__),
369 array('bsmselect', 'bsmselect-compatibility'),
370 $this->version,
371 true
372 );
373
374 wp_enqueue_style('wp-jquery-ui-dialog');
375
376 wp_localize_script(
377 'database-reset',
378 'dbReset',
379 $this->load_javascript_vars()
380 );
381 }
382
383 private function load_javascript_vars()
384 {
385 return array(
386 'confirmAlert' => __('Are you sure you want to continue? There is NO UNDO!', 'wordpress-database-reset'),
387 'selectTable' => __('Select Tables', 'wordpress-database-reset'),
388 'selectOneTable' => __('Please select at least one table to reset.', 'wordpress-database-reset'),
389 'wprInstallUrl' => add_query_arg(array('action' => 'install_wpr'), admin_url('admin.php')),
390 'wprDialogTitle' => '<img alt="WP Reset" title="WP Reset" src="' . plugins_url('assets/images/wp-reset-logo.png', DB_RESET_FILE) . '">',
391 );
392 }
393 }
394
395 endif;
396