admin.class.php
9.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
if ( ! empty( $_SERVER['SCRIPT_FILENAME'] ) && basename( __FILE__ ) == basename( $_SERVER['SCRIPT_FILENAME'] ) ) {
die ( 'Please do not load this screen directly. Thanks!' );
}
/**
* WooDojo Administration Class
*
* All functionality pertaining to the administration sections of WooDojo.
*
* @package WordPress
* @subpackage WooDojo
* @category Administration
* @author WooThemes
* @since 1.0.0
*
* TABLE OF CONTENTS
*
* var $model
*
* - __construct()
* - admin_screen_register()
* - admin_menu_order()
* - admin_screen()
* - admin_head()
* - admin_page_load()
* - admin_styles()
* - admin_styles_global()
* - admin_scripts()
* - ajax_component_toggle()
* - ajax_component_display_toggle()
* - ajax_get_closed_components()
*/
class WooDojo_Admin extends WooDojo_Base {
public $model;
public $hook;
private $whitelist;
/**
* __construct function.
*
* @access public
* @since 1.0.0
* @return void
*/
public function __construct() {
parent::__construct();
add_action( 'admin_menu', array( &$this, 'admin_screen_register' ) );
add_action( 'wp_ajax_woodojo_component_toggle', array( &$this, 'ajax_component_toggle' ) );
add_action( 'wp_ajax_woodojo_component_display_toggle', array( &$this, 'ajax_component_display_toggle' ) );
add_action( 'wp_ajax_woodojo_get_closed_components', array( &$this, 'ajax_get_closed_components' ) );
// Only these models and screens can be loaded.
$this->whitelist = array( 'main', 'more-information' );
} // End __construct()
/**
* admin_screen_register function.
*
* @description Register the admin screen and run the necessary procedures.
* @access public
* @since 1.0.0
* @return void
*/
public function admin_screen_register () {
$hook = add_menu_page( $this->name, $this->name, 'manage_options', $this->token, array( $this, 'admin_screen' ), $this->assets_url . 'images/menu-icon.png' );
add_action( 'load-' . $hook, array( $this, 'admin_page_load' ) );
add_filter( 'custom_menu_order', '__return_true' );
add_filter( 'menu_order', array( $this, 'admin_menu_order' ) );
add_action( 'admin_print_styles-' . $hook, array( $this, 'admin_styles' ) );
add_action( 'admin_print_scripts-' . $hook, array( $this, 'admin_scripts' ) );
// Global styles.
add_action( 'admin_print_styles', array( $this, 'admin_styles_global' ) );
do_action( $this->token . '_admin_menu' );
$this->hook = $hook; // Store the hook for later use.
} // End admin_screen_register()
/**
* admin_menu_order function.
*
* @description Move the menu item to be the second item in the menu.
* @access public
* @since 1.0.0
* @param mixed $menu_order
* @return void
*/
public function admin_menu_order ( $menu_order ) {
$new_menu_order = array();
foreach ( $menu_order as $index => $item ) {
if ( $item != $this->token )
$new_menu_order[] = $item;
if ( $index == 0 )
$new_menu_order[] = $this->token;
}
return $new_menu_order;
} // End admin_menu_order()
/**
* admin_screen function.
*
* @description Load the main admin screen.
* @access public
* @since 1.0.0
* @return void
*/
public function admin_screen () {
$screen = 'main';
if ( isset( $_GET['screen'] ) && ( '' != $_GET['screen'] ) ) {
$screen = esc_attr( trim( $_GET['screen'] ) );
}
$filetoken = 'main';
$filetoken = str_replace( ' ', '-', strtolower( $screen ) );
if ( in_array( $filetoken, $this->whitelist ) && file_exists( $this->screens_path . $filetoken . '.php' ) ) {
require_once( $this->screens_path . $filetoken . '.php' );
} else {
return false;
}
} // End admin_screen()
/**
* admin_page_load function.
*
* @description Run when the admin screen loads.
* @access public
* @since 1.0.0
* @uses global $woodojo->settings
* @uses global $woodojo->api->refresh()
* @return void
*/
public function admin_page_load () {
global $woodojo;
require_once( 'model.class.php' );
// Get the settings.
$woodojo->settings = $woodojo->api->get_settings();
$screen = 'main';
if ( isset( $_GET['screen'] ) && ( '' != $_GET['screen'] ) ) {
$screen = esc_attr( trim( $_GET['screen'] ) );
}
$default = 'WooDojo_Model';
$filetoken = 'model';
// Force refresh if necessary.
if ( $screen == 'main' & $woodojo->settings->refresh == true ) {
$woodojo->api->refresh();
}
$filetoken = str_replace( ' ', '-', strtolower( $screen ) );
$classname = $default . '_' . str_replace( ' ', '', ucwords( str_replace( '-', ' ', $screen ) ) );
if ( ( $default != $classname ) && in_array( $filetoken, $this->whitelist ) && file_exists( $this->models_path . $filetoken . '.class.php' ) ) {
require_once( $this->models_path . $filetoken . '.class.php' );
} else {
return false;
}
$this->model = new $classname();
$this->model->admin_page_hook = $this->hook; // Send the admin page hook to the model.
} // End admin_page_load()
/**
* admin_styles function.
*
* @access public
* @since 1.0.0
* @return void
*/
public function admin_styles () {
wp_register_style( $this->token . '-admin', $this->assets_url . 'css/admin.css', '', '1.2.4', 'screen' );
wp_enqueue_style( $this->token . '-admin' );
} // End admin_styles()
/**
* admin_styles_global function.
*
* @access public
* @since 1.0.0
* @return void
*/
public function admin_styles_global () {
wp_register_style( $this->token . '-global', $this->assets_url . 'css/global.css', '', '1.2.4', 'screen' );
wp_enqueue_style( $this->token . '-global' );
} // End admin_styles_global()
/**
* admin_scripts function.
*
* @access public
* @since 1.0.0
* @return void
*/
public function admin_scripts () {
wp_register_script( $this->token . '-admin', $this->assets_url . 'js/admin.js', array( 'jquery' ), '1.2.4', false );
wp_enqueue_script( $this->token . '-admin' );
$translation_strings = WooDojo_Utils::load_common_l10n();
$ajax_vars = array( $this->token . '_component_toggle_nonce' => wp_create_nonce( $this->token . '_component_toggle_nonce' ) );
$data = array_merge( $translation_strings, $ajax_vars );
/* Specify variables to be made available to the admin.js file. */
wp_localize_script( $this->token . '-admin', $this->token . '_localized_data', $data );
} // End admin_scripts()
/**
* ajax_component_toggle function.
*
* @access public
* @since 1.0.0
* @return void
*/
public function ajax_component_toggle () {
$nonce = $_POST[$this->token . '_component_toggle_nonce'];
//Add nonce security to the request
if ( ! wp_verify_nonce( $nonce, $this->token . '_component_toggle_nonce' ) )
die();
// Make sure our model is available.
$this->admin_page_load();
// Component activation.
if ( isset( $_POST['task'] ) && ( $_POST['task'] == 'activate-component' ) ) {
echo $this->model->activate_component( trim( esc_attr( $_POST['component'] ) ), trim( esc_attr( $_POST['type'] ) ), false );
}
// Component deactivation.
if ( isset( $_POST['task'] ) && ( $_POST['task'] == 'deactivate-component' ) ) {
echo $this->model->deactivate_component( trim( esc_attr( $_POST['component'] ) ), trim( esc_attr( $_POST['type'] ) ), false );
}
die(); // WordPress may print out a spurious zero without this can be particularly bad if using JSON
} // End ajax_component_toggle()
/**
* ajax_component_display_toggle function.
*
* @access public
* @since 1.0.0
* @return void
*/
public function ajax_component_display_toggle () {
$nonce = $_POST[$this->token . '_component_toggle_nonce'];
//Add nonce security to the request
if ( ! wp_verify_nonce( $nonce, $this->token . '_component_toggle_nonce' ) )
die();
// Get stored list of closed components.
$closed = get_option( $this->token . '_closed_components', array() );
$component = (array)$_POST['component'];
array_map( 'esc_attr', $component );
array_map( 'trim', $component );
$status = esc_attr( trim( $_POST['status'] ) );
foreach ( $component as $k => $v ) {
if ( in_array( $v, $closed ) && ( $status == 'open' ) ) {
foreach ( $closed as $i => $j ) {
if ( $j == $v ) {
unset( $closed[$i] );
break;
}
}
}
if ( ( $status == 'closed' ) && ! in_array( $v, $closed ) ) {
$closed[] = $v;
}
}
// Update the database.
echo update_option( $this->token . '_closed_components', $closed );
die(); // WordPress may print out a spurious zero without this can be particularly bad if using JSON
} // End ajax_component_toggle()
/**
* ajax_get_closed_components function.
*
* @access public
* @since 1.0.0
* @return void
*/
public function ajax_get_closed_components () {
$nonce = $_POST[$this->token . '_component_toggle_nonce'];
//Add nonce security to the request
if ( ! wp_verify_nonce( $nonce, $this->token . '_component_toggle_nonce' ) )
die();
// Get stored list of closed components.
$closed = get_option( $this->token . '_closed_components', array() );
echo json_encode( $closed );
die(); // WordPress may print out a spurious zero without this can be particularly bad if using JSON
} // End ajax_get_closed_components()
}
?>