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

190 lines 4.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 $nonce;
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 $this->resetter = new DB_Resetter();
19 $this->version = $version;
20
21 $this->set_request( $_REQUEST );
22 $this->set_view_variables();
23 }
24
25 private function set_request( array $request ) {
26 $this->request = $request;
27 }
28
29 private function set_view_variables() {
30 $this->set_code();
31 $this->set_nonce();
32 $this->set_user();
33 $this->set_wp_tables();
34 }
35
36 private function set_code() {
37 $this->code = $this->generate_code();
38 }
39
40 private function set_nonce() {
41 $this->nonce = strtolower( str_replace( '_', '-', __CLASS__ ) );
42 }
43
44 private function set_user() {
45 $this->user = $this->resetter->get_user();
46 }
47
48 private function set_wp_tables() {
49 $this->wp_tables = $this->resetter->get_wp_tables();
50 }
51
52 private function generate_code( $length = 5 ) {
53 return substr( md5( time() ), 1, $length );
54 }
55
56 public function run() {
57 add_action( 'admin_init', array( $this, 'reset' ) );
58 add_action( 'admin_menu', array( $this, 'add_tools_menu' ) );
59 }
60
61 public function reset() {
62 if ( $this->form_is_safe_to_submit() ) {
63 try {
64 $this->resetter->set_reactivate( $this->request[ 'db-reset-reactivate-theme-data' ] );
65 $this->resetter->reset( $this->request[ 'db-reset-tables' ] );
66 $this->handle_after_reset();
67 } catch ( Exception $e ) {
68 $this->notice_error = $e->getMessage();
69 }
70 }
71 }
72
73 private function form_is_safe_to_submit() {
74 return isset( $this->request['db-reset-code-confirm'] ) &&
75 $this->assert_request_variables_not_empty() &&
76 $this->assert_correct_code();
77 }
78
79 private function handle_after_reset() {
80 if ( empty( $this->request[ 'db-reset-reactivate-theme-data' ] ) ) {
81 wp_redirect( admin_url() );
82 exit;
83 }
84
85 $this->notice_success = __( 'The selected tables were reset', 'wordpress-database-reset' );
86 }
87
88 private function assert_request_variables_not_empty() {
89 $this->set_empty_request_key( 'db-reset-tables', array() );
90 $this->set_empty_request_key( 'db-reset-reactivate-theme-data', false );
91
92 return true;
93 }
94
95 private function set_empty_request_key( $key, $default ) {
96 if ( ! array_key_exists( $key, $this->request ) ) {
97 $this->request[ $key ] = $default;
98 }
99 }
100
101 private function assert_correct_code() {
102 if ( $this->request['db-reset-code'] !==
103 $this->request['db-reset-code-confirm'] ) {
104 $this->notice_error = __( 'You entered the wrong security code', 'wordpress-database-reset' );
105 return false;
106 }
107
108 return true;
109 }
110
111 public function add_tools_menu() {
112 $plugin_page = add_management_page(
113 __( 'Database Reset', 'wordpress-database-reset' ),
114 __( 'Database Reset', 'wordpress-database-reset' ),
115 'update_core',
116 'database-reset',
117 array( $this, 'render' )
118 );
119
120 add_action( 'load-' . $plugin_page, array( $this, 'load_assets' ) );
121 }
122
123 public function render() {
124 require_once( DB_RESET_PATH . '/views/index.php' );
125 }
126
127 public function load_assets() {
128 $this->load_stylesheets();
129 $this->load_javascript();
130 }
131
132 private function load_stylesheets() {
133 wp_enqueue_style(
134 'bsmselect',
135 plugins_url( 'assets/css/bsmselect.css', __FILE__ ),
136 array(),
137 $this->version
138 );
139
140 wp_enqueue_style(
141 'database-reset',
142 plugins_url( 'assets/css/database-reset.css', __FILE__ ),
143 array('bsmselect'),
144 $this->version
145 );
146 }
147
148 private function load_javascript() {
149 wp_enqueue_script(
150 'bsmselect',
151 plugins_url( 'assets/js/bsmselect.js', __FILE__ ),
152 array( 'jquery' ),
153 $this->version,
154 true
155 );
156
157 wp_enqueue_script(
158 'bsmselect-compatibility',
159 plugins_url( 'assets/js/bsmselect.compatibility.js', __FILE__ ),
160 array( 'bsmselect' ),
161 $this->version,
162 true
163 );
164
165 wp_enqueue_script(
166 'database-reset',
167 plugins_url( 'assets/js/database-reset.js', __FILE__ ),
168 array( 'bsmselect', 'bsmselect-compatibility' ),
169 $this->version,
170 true
171 );
172
173 wp_localize_script(
174 'database-reset',
175 'dbReset',
176 $this->load_javascript_vars()
177 );
178 }
179
180 private function load_javascript_vars() {
181 return array(
182 'confirmAlert' => __( 'Are you sure you want to continue?', 'wordpress-database-reset' ),
183 'selectTable' => __( 'Select Tables', 'wordpress-database-reset' )
184 );
185 }
186
187 }
188
189 endif;
190