| @@ -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,91 +13,243 @@ | ||
| 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 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' ) ); | |
| 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')); | |
| 53 | 68 | } |
| 54 | 69 | |
| 55 | - public function reset() { | |
| 56 | - if ( $this->form_is_safe_to_submit() ) { | |
| 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 ★★★★★</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 | + if (false === current_user_can('administrator')) { | |
| 130 | + wp_die('Sorry, you have to be an admin to run this action.'); | |
| 131 | + } | |
| 132 | + | |
| 133 | + $plugin_slug = 'wp-reset/wp-reset.php'; | |
| 134 | + $plugin_zip = 'https://downloads.wordpress.org/plugin/wp-reset.latest-stable.zip'; | |
| 135 | + | |
| 136 | + @include_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 137 | + @include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; | |
| 138 | + @include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; | |
| 139 | + @include_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 140 | + @include_once ABSPATH . 'wp-admin/includes/misc.php'; | |
| 141 | + echo '<style> | |
| 142 | + body{ | |
| 143 | + font-family: sans-serif; | |
| 144 | + font-size: 14px; | |
| 145 | + line-height: 1.5; | |
| 146 | + color: #444; | |
| 147 | + } | |
| 148 | + </style>'; | |
| 149 | + | |
| 150 | + echo '<div style="margin: 20px; color:#444;">'; | |
| 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>'; | |
| 152 | + echo 'Starting ...<br><br>'; | |
| 153 | + | |
| 154 | + wp_cache_flush(); | |
| 155 | + $upgrader = new Plugin_Upgrader(); | |
| 156 | + echo 'Check if WP Reset is already installed ... <br />'; | |
| 157 | + if ($this->is_plugin_installed($plugin_slug)) { | |
| 158 | + echo 'WP Reset is already installed! <br /><br />Making sure it\'s the latest version.<br />'; | |
| 159 | + $upgrader->upgrade($plugin_slug); | |
| 160 | + $installed = true; | |
| 161 | + } else { | |
| 162 | + echo 'Installing WP Reset.<br />'; | |
| 163 | + $installed = $upgrader->install($plugin_zip); | |
| 164 | + } | |
| 165 | + wp_cache_flush(); | |
| 166 | + | |
| 167 | + if (!is_wp_error($installed) && $installed) { | |
| 168 | + echo 'Activating WP Reset.<br />'; | |
| 169 | + $activate = activate_plugin($plugin_slug); | |
| 170 | + | |
| 171 | + if (is_null($activate)) { | |
| 172 | + echo 'WP Reset Activated.<br />'; | |
| 173 | + | |
| 174 | + echo '<script>setTimeout(function() { top.location = "tools.php?page=wp-reset"; }, 1000);</script>'; | |
| 175 | + echo '<br>If you are not redirected in a few seconds - <a href="tools.php?page=wp-reset" target="_parent">click here</a>.'; | |
| 176 | + } | |
| 177 | + } else { | |
| 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>.'; | |
| 179 | + } | |
| 180 | + | |
| 181 | + echo '</div>'; | |
| 182 | + } // install_wpr | |
| 183 | + | |
| 184 | + | |
| 185 | + function is_plugin_installed($slug) | |
| 186 | + { | |
| 187 | + if (!function_exists('get_plugins')) { | |
| 188 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 189 | + } | |
| 190 | + $all_plugins = get_plugins(); | |
| 191 | + | |
| 192 | + if (!empty($all_plugins[$slug])) { | |
| 193 | + return true; | |
| 194 | + } else { | |
| 195 | + return false; | |
| 196 | + } | |
| 197 | + } // is_plugin_installed | |
| 198 | + | |
| 199 | + | |
| 200 | + public function reset() | |
| 201 | + { | |
| 202 | + if ($this->form_is_safe_to_submit()) { | |
| 57 | 203 | try { |
| 58 | - $this->resetter->set_reactivate( $this->request[ 'db-reset-reactivate-theme-data' ] ); | |
| 59 | - $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']); | |
| 60 | 206 | $this->handle_after_reset(); |
| 61 | - } catch ( Exception $e ) { | |
| 207 | + } catch (Exception $e) { | |
| 62 | 208 | $this->notice_error = $e->getMessage(); |
| 63 | 209 | } |
| 64 | 210 | } |
| 65 | 211 | } |
| 66 | 212 | |
| 67 | - private function form_is_safe_to_submit() { | |
| 68 | - return isset( $this->request['db-reset-code-confirm'] ) && | |
| 69 | - $this->assert_request_variables_not_empty() && | |
| 70 | - $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(); | |
| 71 | 218 | } |
| 72 | 219 | |
| 73 | - private function handle_after_reset() { | |
| 74 | - if ( empty( $this->request[ 'db-reset-reactivate-theme-data' ] ) ) { | |
| 75 | - 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()); | |
| 76 | 224 | exit; |
| 77 | 225 | } |
| 78 | 226 | |
| 79 | - $this->notice_success = __( 'The selected tables were reset', 'wordpress-database-reset' ); | |
| 227 | + $this->notice_success = __('The selected tables were reset', 'wordpress-database-reset'); | |
| 80 | 228 | } |
| 81 | 229 | |
| 82 | - private function assert_request_variables_not_empty() { | |
| 83 | - $this->set_empty_request_key( 'db-reset-tables', array() ); | |
| 84 | - $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); | |
| 85 | 234 | |
| 86 | 235 | return true; |
| 87 | 236 | } |
| 88 | 237 | |
| 89 | - private function set_empty_request_key( $key, $default ) { | |
| 90 | - if ( ! array_key_exists( $key, $this->request ) ) { | |
| 91 | - $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; | |
| 92 | 242 | } |
| 93 | 243 | } |
| 94 | 244 | |
| 95 | - private function assert_correct_code() { | |
| 96 | - if ( $this->request['db-reset-code'] !== | |
| 97 | - $this->request['db-reset-code-confirm'] ) { | |
| 98 | - $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'); | |
| 99 | 252 | return false; |
| 100 | 253 | } |
| 101 | 254 | |
| 102 | 255 | return true; |
| @@ -101,33 +254,37 @@ | ||
| 101 | 254 | |
| 102 | 255 | return true; |
| 103 | 256 | } |
| 104 | 257 | |
| 105 | - public function add_tools_menu() { | |
| 258 | + public function add_tools_menu() | |
| 259 | + { | |
| 106 | 260 | $plugin_page = add_management_page( |
| 107 | - __( 'Database Reset', 'wordpress-database-reset' ), | |
| 108 | - __( 'Database Reset', 'wordpress-database-reset' ), | |
| 261 | + __('Database Reset', 'wordpress-database-reset'), | |
| 262 | + __('Database Reset', 'wordpress-database-reset'), | |
| 109 | 263 | 'manage_options', |
| 110 | 264 | 'database-reset', |
| 111 | - array( $this, 'render' ) | |
| 265 | + array($this, 'render') | |
| 112 | 266 | ); |
| 113 | 267 | |
| 114 | - add_action( 'load-' . $plugin_page, array( $this, 'load_assets' ) ); | |
| 268 | + add_action('load-' . $plugin_page, array($this, 'load_assets')); | |
| 115 | 269 | } |
| 116 | 270 | |
| 117 | - public function render() { | |
| 118 | - require_once( DB_RESET_PATH . '/views/index.php' ); | |
| 271 | + public function render() | |
| 272 | + { | |
| 273 | + require_once(DB_RESET_PATH . '/views/index.php'); | |
| 119 | 274 | } |
| 120 | 275 | |
| 121 | - public function load_assets() { | |
| 276 | + public function load_assets() | |
| 277 | + { | |
| 122 | 278 | $this->load_stylesheets(); |
| 123 | 279 | $this->load_javascript(); |
| 124 | 280 | } |
| 125 | 281 | |
| 126 | - private function load_stylesheets() { | |
| 282 | + private function load_stylesheets() | |
| 283 | + { | |
| 127 | 284 | wp_enqueue_style( |
| 128 | 285 | 'bsmselect', |
| 129 | - plugins_url( 'assets/css/bsmselect.css', __FILE__ ), | |
| 286 | + plugins_url('assets/css/bsmselect.css', __FILE__), | |
| 130 | 287 | array(), |
| 131 | 288 | $this->version |
| 132 | 289 | ); |
| 133 | 290 | |
| @@ -132,19 +289,22 @@ | ||
| 132 | 289 | ); |
| 133 | 290 | |
| 134 | 291 | wp_enqueue_style( |
| 135 | 292 | 'database-reset', |
| 136 | - plugins_url( 'assets/css/database-reset.css', __FILE__ ), | |
| 293 | + plugins_url('assets/css/database-reset.css', __FILE__), | |
| 137 | 294 | array('bsmselect'), |
| 138 | 295 | $this->version |
| 139 | 296 | ); |
| 140 | 297 | } |
| 141 | 298 | |
| 142 | - private function load_javascript() { | |
| 299 | + private function load_javascript() | |
| 300 | + { | |
| 301 | + wp_enqueue_script('jquery-ui-dialog'); | |
| 302 | + | |
| 143 | 303 | wp_enqueue_script( |
| 144 | 304 | 'bsmselect', |
| 145 | - plugins_url( 'assets/js/bsmselect.js', __FILE__ ), | |
| 146 | - array( 'jquery' ), | |
| 305 | + plugins_url('assets/js/bsmselect.js', __FILE__), | |
| 306 | + array('jquery'), | |
| 147 | 307 | $this->version, |
| 148 | 308 | true |
| 149 | 309 | ); |
| 150 | 310 | |
| @@ -149,10 +309,10 @@ | ||
| 149 | 309 | ); |
| 150 | 310 | |
| 151 | 311 | wp_enqueue_script( |
| 152 | 312 | 'bsmselect-compatibility', |
| 153 | - plugins_url( 'assets/js/bsmselect.compatibility.js', __FILE__ ), | |
| 154 | - array( 'bsmselect' ), | |
| 313 | + plugins_url('assets/js/bsmselect.compatibility.js', __FILE__), | |
| 314 | + array('bsmselect'), | |
| 155 | 315 | $this->version, |
| 156 | 316 | true |
| 157 | 317 | ); |
| 158 | 318 | |
| @@ -157,14 +317,16 @@ | ||
| 157 | 317 | ); |
| 158 | 318 | |
| 159 | 319 | wp_enqueue_script( |
| 160 | 320 | 'database-reset', |
| 161 | - plugins_url( 'assets/js/database-reset.js', __FILE__ ), | |
| 162 | - array( 'bsmselect', 'bsmselect-compatibility' ), | |
| 321 | + plugins_url('assets/js/database-reset.js', __FILE__), | |
| 322 | + array('bsmselect', 'bsmselect-compatibility'), | |
| 163 | 323 | $this->version, |
| 164 | 324 | true |
| 165 | 325 | ); |
| 166 | 326 | |
| 327 | + wp_enqueue_style('wp-jquery-ui-dialog'); | |
| 328 | + | |
| 167 | 329 | wp_localize_script( |
| 168 | 330 | 'database-reset', |
| 169 | 331 | 'dbReset', |
| 170 | 332 | $this->load_javascript_vars() |
| @@ -170,14 +332,17 @@ | ||
| 170 | 332 | $this->load_javascript_vars() |
| 171 | 333 | ); |
| 172 | 334 | } |
| 173 | 335 | |
| 174 | - private function load_javascript_vars() { | |
| 336 | + private function load_javascript_vars() | |
| 337 | + { | |
| 175 | 338 | return array( |
| 176 | - 'confirmAlert' => __( 'Are you sure you want to continue?', 'wordpress-database-reset' ), | |
| 177 | - 'selectTable' => __( 'Select Tables', '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'), | |
| 342 | + 'wprInstallUrl' => add_query_arg(array('action' => 'install_wpr'), admin_url('admin.php')), | |
| 343 | + 'wprDialogTitle' => '<img alt="WP Reset" title="WP Reset" src="' . plugins_url('assets/images/wp-reset-logo.png', DB_RESET_FILE) . '">', | |
| 178 | 344 | ); |
| 179 | 345 | } |
| 180 | - | |
| 181 | 346 | } |
| 182 | 347 | |
| 183 | 348 | endif; |