forked from gambitph/Titan-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-admin-panel.php
424 lines (333 loc) · 10.7 KB
/
class-admin-panel.php
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class TitanFrameworkAdminPanel {
private $defaultSettings = array(
'name' => '', // Name of the menu item
'title' => '', // Title displayed on the top of the admin panel
'parent' => null, // id of parent, if blank, then this is a top level menu
'id' => '', // Unique ID of the menu item
'capability' => 'manage_options', // User role
'icon' => 'dashicons-admin-generic', // Menu icon for top level menus only http://melchoyce.github.io/dashicons/
'position' => null, // Menu position. Can be used for both top and sub level menus
'use_form' => true, // If false, options will not be wrapped in a form
'desc' => '', // Description displayed below the title
);
public $settings;
public $options = array();
public $tabs = array();
public $owner;
public $panelID;
private $activeTab = null;
private static $idsUsed = array();
function __construct( $settings, $owner ) {
$this->owner = $owner;
if ( ! is_admin() ) {
return;
}
// If we are just initializing, do not create our admin panels yet. During this phase all we want is to get all the options to create
if ( TitanFramework::$initializing ) {
return;
}
$this->settings = array_merge( $this->defaultSettings, $settings );
// $this->options = $options;
if ( empty( $this->settings['name'] ) ) {
return;
}
if ( empty( $this->settings['title'] ) ) {
$this->settings['title'] = $this->settings['name'];
}
if ( empty( $this->settings['id'] ) ) {
$prefix = '';
if ( ! empty( $this->settings['parent'] ) ) {
$prefix = str_replace( ' ', '-', trim( strtolower( $this->settings['parent'] ) ) ) . '-';
}
$this->settings['id'] = $prefix . str_replace( ' ', '-', trim( strtolower( $this->settings['name'] ) ) );
$this->settings['id'] = str_replace( '&', '-', $this->settings['id'] );
}
// make sure all our IDs are unique
$suffix = '';
while ( in_array( $this->settings['id'] . $suffix, self::$idsUsed ) ) {
if ( $suffix === '' ) {
$suffix = 2;
} else {
$suffix ;
}
}
$this->settings['id'] .= $suffix;
// keep track of all IDs used
self::$idsUsed[] = $this->settings['id'];
$priority = -1;
if ( $this->settings['parent'] ) {
$priority = intval( $this->settings['position'] );
}
add_action( 'admin_menu', array( $this, 'register' ), $priority );
}
public function createAdminPanel( $settings ) {
$settings['parent'] = $this->settings['id'];
return $this->owner->createAdminPanel( $settings );
}
public function register() {
// Parent menu
if ( empty( $this->settings['parent'] ) ) {
$this->panelID = add_menu_page( $this->settings['name'],
$this->settings['name'],
$this->settings['capability'],
$this->settings['id'],
array( $this, 'createAdminPage' ),
$this->settings['icon'],
$this->settings['position'] );
// Sub menu
} else {
$this->panelID = add_submenu_page( $this->settings['parent'],
$this->settings['name'],
$this->settings['name'],
$this->settings['capability'],
$this->settings['id'],
array( $this, 'createAdminPage' ) );
}
add_action( 'load-' . $this->panelID, array( $this, 'saveOptions' ) );
add_action( 'load-' . $this->panelID, array( $this, 'addTitanCredit' ) );
}
public function addTitanCredit() {
add_filter( 'admin_footer_text', array( $this, 'addTitanCreditText' ) );
}
public function addTitanCreditText() {
echo __( "<em>Options Page Created with <a href='http://titanframework.net?utm_source=admin&utm_medium=admin footer'>Titan Framework</a></em>", TF_I18NDOMAIN );
}
public function getOptionNamespace() {
return $this->owner->optionNamespace;
}
public function saveOptions() {
if ( ! $this->verifySecurity() ) {
return;
}
$message = '';
/*
* Save
*/
if ( $_POST['action'] == 'save' ) {
// we are in a tab
$activeTab = $this->getActiveTab();
if ( ! empty( $activeTab ) ) {
foreach ( $activeTab->options as $option ) {
if ( empty( $option->settings['id'] ) ) {
continue;
}
if ( ! empty( $_POST[$this->getOptionNamespace() . '_' . $option->settings['id']] ) ) {
$value = $_POST[$this->getOptionNamespace() . '_' . $option->settings['id']];
} else {
$value = '';
}
// $value = $option->cleanValueForSaving( $value );
$this->owner->setOption( $option->settings['id'], $value );
}
}
foreach ( $this->options as $option ) {
if ( empty( $option->settings['id'] ) ) {
continue;
}
if ( ! empty( $_POST[$this->getOptionNamespace() . '_' . $option->settings['id']] ) ) {
$value = $_POST[$this->getOptionNamespace() . '_' . $option->settings['id']];
} else {
$value = '';
}
$this->owner->setOption( $option->settings['id'], $value );
}
// Hook 'tf_pre_save_options_{namespace}' - action pre-saving
do_action( 'tf_pre_save_options_' . $this->getOptionNamespace(), $this );
$this->owner->saveOptions();
$message = 'saved';
/*
* Reset
*/
} else if ( $_POST['action'] == 'reset' ) {
// we are in a tab
$activeTab = $this->getActiveTab();
if ( ! empty( $activeTab ) ) {
foreach ( $activeTab->options as $option ) {
if ( empty( $option->settings['id'] ) ) {
continue;
}
$this->owner->setOption( $option->settings['id'], $option->settings['default'] );
}
}
foreach ( $this->options as $option ) {
if ( empty( $option->settings['id'] ) ) {
continue;
}
$this->owner->setOption( $option->settings['id'], $option->settings['default'] );
}
// Hook 'tf_pre_reset_options_{namespace}' - action pre-saving
do_action( 'tf_pre_reset_options_' . $this->getOptionNamespace(), $this );
$this->owner->saveOptions();
$message = 'reset';
}
/*
* Redirect to prevent refresh saving
*/
// urlencode to allow special characters in the url
$url = wp_get_referer();
$activeTab = $this->getActiveTab();
$url = add_query_arg( 'page', urlencode( $this->settings['id'] ), $url );
if ( ! empty( $activeTab ) ) {
$url = add_query_arg( 'tab', urlencode( $activeTab->settings['id'] ), $url );
}
if ( ! empty( $message ) ) {
$url = add_query_arg( 'message', $message, $url );
}
do_action( 'tf_admin_options_saved_' . $this->getOptionNamespace() );
wp_redirect( $url );
}
private function verifySecurity() {
if ( empty( $_POST ) || empty( $_POST['action'] ) ) {
return false;
}
$screen = get_current_screen();
if ( $screen->id != $this->panelID ) {
return false;
}
if ( ! current_user_can( $this->settings['capability'] ) ) {
return false;
}
if ( ! check_admin_referer( $this->settings['id'], TF . '_nonce' ) ) {
return false;
}
return true;
}
public function getActiveTab() {
if ( ! count( $this->tabs ) ) {
return '';
}
if ( ! empty( $this->activeTab ) ) {
return $this->activeTab;
}
if ( empty( $_GET['tab'] ) ) {
$this->activeTab = $this->tabs[0];
return $this->activeTab;
}
foreach ( $this->tabs as $tab ) {
if ( $tab->settings['id'] == $_GET['tab'] ) {
$this->activeTab = $tab;
return $this->activeTab;
}
}
$this->activeTab = $this->tabs[0];
return $this->activeTab;
}
public function createAdminPage() {
do_action( 'tf_admin_page_before' );
do_action( 'tf_admin_page_before_' . $this->getOptionNamespace() );
?>
<div class="wrap">
<h2><?php echo $this->settings['title'] ?></h2>
<?php
if ( ! empty( $this->settings['desc'] ) ) {
?><p class='description'><?php echo $this->settings['desc'] ?></p><?php
}
?>
<div class='titan-framework-panel-wrap'>
<?php
do_action( 'tf_admin_page_start' );
do_action( 'tf_admin_page_start_' . $this->getOptionNamespace() );
if ( count( $this->tabs ) ):
?>
<h2 class="nav-tab-wrapper">
<?php
do_action( 'tf_admin_page_tab_start' );
do_action( 'tf_admin_page_tab_start_' . $this->getOptionNamespace() );
foreach ( $this->tabs as $tab ) {
$tab->displayTab();
}
do_action( 'tf_admin_page_tab_end' );
do_action( 'tf_admin_page_tab_end_' . $this->getOptionNamespace() );
?>
</h2>
<?php
endif;
?>
<div class='options-container'>
<?php
// Display notification if we did something
if ( ! empty( $_GET['message'] ) ) {
if ( $_GET['message'] == 'saved' ) {
echo TitanFrameworkAdminNotification::formNotification( __( 'Settings saved.', TF_I18NDOMAIN ), esc_html( $_GET['message'] ) );
} else if ( $_GET['message'] == 'reset' ) {
echo TitanFrameworkAdminNotification::formNotification( __( 'Settings reset to default.', TF_I18NDOMAIN ), esc_html( $_GET['message'] ) );
}
}
if ( $this->settings['use_form'] ):
?>
<form method='post'>
<?php
endif;
if ( $this->settings['use_form'] ) {
// security
wp_nonce_field( $this->settings['id'], TF . '_nonce' );
}
?>
<table class='form-table'>
<tbody>
<?php
do_action( 'tf_admin_page_table_start' );
do_action( 'tf_admin_page_table_start_' . $this->getOptionNamespace() );
$activeTab = $this->getActiveTab();
if ( ! empty( $activeTab ) ) {
if ( ! empty( $activeTab->settings['desc'] ) ) {
?><p class='description'><?php echo $activeTab->settings['desc'] ?></p><?php
}
$activeTab->displayOptions();
}
foreach ( $this->options as $option ) {
$option->display();
}
do_action( 'tf_admin_page_table_end' );
do_action( 'tf_admin_page_table_end_' . $this->getOptionNamespace() );
?>
</tbody>
</table>
<?php
if ( $this->settings['use_form'] ):
?>
</form>
<?php
endif;
// Reset form. We use JS to trigger a reset from other buttons within the main form
// This is used by class-option-save.php
if ( $this->settings['use_form'] ):
?>
<form method='post' id='tf-reset-form'>
<?php
// security
wp_nonce_field( $this->settings['id'], TF . '_nonce' );
?>
<input type='hidden' name='action' value='reset'/>
</form>
<?php
endif;
do_action( 'tf_admin_page_end' );
do_action( 'tf_admin_page_end_' . $this->getOptionNamespace() );
?>
<div class='options-container'>
</div>
</div>
</div>
</div>
<?php
do_action( 'tf_admin_page_after' );
do_action( 'tf_admin_page_after_' . $this->getOptionNamespace() );
}
public function createTab( $settings ) {
$obj = new TitanFrameworkAdminTab( $settings, $this );
$this->tabs[] = $obj;
return $obj;
}
public function createOption( $settings ) {
if ( ! apply_filters( 'tf_create_option_continue_' . $this->getOptionNamespace(), true, $settings ) ) {
return null;
}
$obj = TitanFrameworkOption::factory( $settings, $this );
$this->options[] = $obj;
do_action( 'tf_create_option_' . $this->getOptionNamespace(), $obj );
return $obj;
}
}