-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwp-custom-settings.php
575 lines (476 loc) · 12.5 KB
/
wp-custom-settings.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
<?php
/**
* WP Custom Settings
*
* @package wp-custom-settings
* @author Chandra Patel
* @license GPL-2.0-or-later
*
* @wordpress-plugin
* Plugin Name: WP Custom Settings
* Plugin URI: /~https://github.com/chandrapatel/wp-custom-settings
* Description: Allows developers to create a custom admin menu page with settings using Settings API without registering callbacks to every settings section and field.
* Version: 0.1
* Requires at least: 5.0
* Requires PHP: 7.0
* Author: Chandra Patel
* Author URI: https://chandrapatel.in
* Text Domain: wp-custom-settings
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
namespace WP_Custom_Settings;
/**
* Manage to add menu page, register setting, settings section, and fields.
*/
class WP_Custom_Settings {
/**
* An array of arguments to add the menu page.
*
* @var array
*/
private $page;
/**
* An array of arguments to register setting.
*
* @var array
*/
private $setting;
/**
* An array of settings sections and it's fields.
*
* @var array
*/
private $sections;
/**
* Initialize class properties and add action to register setting and menu page.
*
* @param array $page An array of arguments to add the menu page.
* @param array $setting An array of arguments to register setting.
* @param array $sections An array of settings sections and it's fields.
*/
public function __construct( array $page, array $setting, array $sections ) {
$this->page = wp_parse_args(
$page,
[
'page_title' => __( 'Custom Settings', 'wp-custom-settings' ),
'menu_title' => __( 'Custom Settings', 'wp-custom-settings' ),
'capability' => 'manage_options',
'menu_slug' => 'wp-custom-settings-page',
'icon_url' => '',
'position' => null,
]
);
$this->setting = wp_parse_args(
$setting,
[
'option_group' => 'wp_custom_setting_group',
'option_name' => 'wp_custom_setting_options',
'args' => [],
]
);
$this->sections = $sections;
add_action( 'admin_init', array( $this, 'register_setting' ) );
add_action( 'admin_menu', array( $this, 'add_menu_page' ) );
}
/**
* Register setting, add sections and fields.
*
* @return void
*/
public function register_setting() {
register_setting( $this->setting['option_group'], $this->setting['option_name'], $this->setting['args'] );
if ( empty( $this->sections ) || ! is_array( $this->sections ) ) {
return;
}
foreach ( $this->sections as $section ) {
if ( ! $section instanceof WP_Custom_Settings_Section ) {
continue;
}
add_settings_section( $section->id, $section->title, [ $section, 'display' ], $this->page['menu_slug'] );
$fields = $section->fields;
if ( empty( $fields ) || ! is_array( $fields ) ) {
continue;
}
foreach ( $fields as $field ) {
if ( ! is_object( $field ) ) {
continue;
}
// Set option name used to save all settings field value in a single option name in the options table.
// Couldn't find a better way to get option name in WP_Custom_Settings_Field class so setting it through the method.
$field->set_option_name( $this->setting['option_name'] );
add_settings_field( $field->id, $field->title, [ $field, 'display' ], $this->page['menu_slug'], $section->id, $field->args );
}
}
}
/**
* Register menu page.
*
* @return void
*/
public function add_menu_page() {
add_menu_page(
$this->page['page_title'],
$this->page['menu_title'],
$this->page['capability'],
$this->page['menu_slug'],
[ $this, 'display_menu_page' ],
$this->page['icon_url'],
$this->page['position']
);
}
/**
* Display menu page.
*
* @return void
*/
public function display_menu_page() {
// Check user capabilities.
if ( ! current_user_can( $this->page['capability'] ) ) {
return;
}
// Check if the user has submitted the settings.
// WordPress will add the "settings-updated" $_GET parameter to the URL.
if ( isset( $_GET['settings-updated'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
// Add settings saved message with the class of "updated".
add_settings_error(
"{$this->page['menu_slug']}_messages",
"{$this->page['menu_slug']}_message",
__( 'Settings Saved', 'wp-custom-settings' ),
'updated'
);
}
// Show error/update messages.
settings_errors( "{$this->page['menu_slug']}_messages" );
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<form action="options.php" method="post">
<?php
// Output security fields for the registered setting.
settings_fields( $this->setting['option_group'] );
// Output setting sections and their fields.
// Sections are registered for registered setting, each field is registered to a specific section.
do_settings_sections( $this->page['menu_slug'] );
// Output save settings button.
submit_button( __( 'Save Settings', 'wp-custom-settings' ) );
?>
</form>
</div>
<?php
}
}
/**
* Manage settings section properties and rendering.
*/
class WP_Custom_Settings_Section {
/**
* Section ID.
*
* @var string
*/
private $id;
/**
* Section title.
*
* @var string
*/
private $title;
/**
* Section description.
*
* @var string
*/
private $description;
/**
* An array of Section's fields.
*
* @var array
*/
private $fields;
/**
* Initialize class properties required to add settings section.
*
* @param string $id Section ID.
* @param string $title Section title.
* @param string $description Section description.
* @param array $fields An array of Section's fields.
*/
public function __construct( $id, $title, $description, $fields ) {
$this->id = $id;
$this->title = $title;
$this->description = $description;
$this->fields = $fields;
}
/**
* Return value of requested property.
*
* @param string $property_name Property Name.
*
* @return mixed
*/
public function __get( $property_name ) {
return $this->$property_name;
}
/**
* Settings section display callback.
*
* @param array $args Display arguments.
*
* @return void
*/
public function display( $args ) {
printf(
'<p id="%1$s">%2$s</p>',
esc_attr( $this->id ),
esc_html( $this->description )
);
}
}
/**
* Manage settings field properties and rendering.
*/
class WP_Custom_Settings_Field {
/**
* Field type.
*
* @var string
*/
private $type;
/**
* Field ID.
*
* @var string
*/
private $id;
/**
* Field title.
*
* @var string
*/
private $title;
/**
* An array of additional field args.
*
* @var array
*/
private $args;
/**
* Option Name. Used to save all settings field value in the single option name in the options table.
*
* @var string
*/
private $option_name;
/**
* Initialize class properties required to add settings field.
*
* @param string $type Field Type.
* @param string $id Field ID.
* @param string $title Field title.
* @param array $args An array of addtional field args.
*/
public function __construct( string $type, string $id, string $title, array $args = [] ) {
$this->type = $type;
$this->id = $id;
$this->title = $title;
$this->args = $args;
}
/**
* Return value of requested property.
*
* @param string $property_name Property Name.
*
* @return mixed
*/
public function __get( $property_name ) {
return $this->$property_name;
}
/**
* Set option name used to save all settings field value in a single option name in the options table.
*
* @param string $option_name Option Name.
*
* @return void
*/
public function set_option_name( $option_name ) {
$this->option_name = $option_name;
}
/**
* Settings section display callback.
*
* @param array $args Display arguments.
*
* @return void
*/
public function display( $args ) {
$options = get_option( $this->option_name );
$field_value = ( isset( $options[ $this->id ] ) ) ? $options[ $this->id ] : '';
switch ( $this->type ) {
case 'textarea':
$this->display_textarea_field( $field_value );
break;
case 'select':
$this->display_select_field( $field_value );
break;
case 'checkbox':
$this->display_checkbox_field( $field_value );
break;
case 'radio':
$this->display_radio_field( $field_value );
break;
case 'text':
case 'password':
case 'email':
case 'url':
case 'tel':
case 'number':
case 'color':
case 'date':
case 'datetime-local':
case 'month':
case 'week':
case 'time':
default:
$this->display_input_field( $field_value );
break;
}
if ( ! empty( $args['description'] ) ) {
printf(
'<p class="description">%s</p>',
esc_html( $args['description'] )
);
}
}
/**
* Display text field.
*
* @param string $field_value Field value.
*
* @return void
*/
private function display_input_field( $field_value ) {
$class = isset( $this->args['class'] ) ? $this->args['class'] : 'regular-text';
printf(
'<input type="%1$s" name="%2$s[%3$s]" id="%3$s" value="%4$s" class="%5$s" />',
esc_attr( $this->type ),
esc_attr( $this->option_name ),
esc_attr( $this->id ),
esc_attr( $field_value ),
esc_attr( $class )
);
}
/**
* Display textarea field.
*
* @param string $field_value Field value.
*
* @return void
*/
private function display_textarea_field( $field_value ) {
$class = isset( $this->args['class'] ) ? $this->args['class'] : 'regular-text';
printf(
'<textarea name="%1$s[%2$s]" id="%2$s" class="%4$s">%3$s</textarea>',
esc_attr( $this->option_name ),
esc_attr( $this->id ),
esc_textarea( $field_value ),
esc_attr( $class )
);
}
/**
* Display select field.
*
* @param string $field_value Field value.
*
* @return void
*/
private function display_select_field( $field_value ) {
$class = isset( $this->args['class'] ) ? $this->args['class'] : 'regular-text';
$options = isset( $this->args['options'] ) ? $this->args['options'] : [];
if ( empty( $options ) || ! is_array( $options ) ) {
printf(
'<p class="description" style="color: red;">%s</p>',
esc_html__( 'Options are missing or does not in array format.' )
);
}
printf(
'<select id="%1$s" name="%2$s[%1$s]" class="%3$s">',
esc_attr( $this->id ),
esc_attr( $this->option_name ),
esc_attr( $class )
);
foreach ( $options as $value => $label ) {
printf(
'<option value="%1$s" %2$s>%3$s</option>',
esc_attr( $value ),
selected( $field_value, $value, false ),
esc_html( $label )
);
}
echo '</select>';
}
/**
* Display checkbox input type.
*
* @param string $field_value Field value.
*
* @return void
*/
private function display_checkbox_field( $field_value ) {
$class = isset( $this->args['class'] ) ? $this->args['class'] : '';
$label = isset( $this->args['label'] ) ? $this->args['label'] : '';
$value = isset( $this->args['value'] ) ? $this->args['value'] : '';
printf(
'<input type="hidden" name="%1$s[%2$s]" value="" />',
esc_attr( $this->option_name ),
esc_attr( $this->id )
);
printf(
'<input type="checkbox" name="%1$s[%2$s]" id="%2$s" value="%3$s" class="%4$s" %5$s />',
esc_attr( $this->option_name ),
esc_attr( $this->id ),
esc_attr( $value ),
esc_attr( $class ),
checked( $field_value, $value, false )
);
printf(
'<label for="%1$s">%2$s</label><br>',
esc_attr( $this->id ),
esc_html( $label )
);
}
/**
* Display radio input type.
*
* @param string $field_value Field value.
*
* @return void
*/
private function display_radio_field( $field_value ) {
$class = isset( $this->args['class'] ) ? $this->args['class'] : '';
$options = isset( $this->args['options'] ) ? $this->args['options'] : [];
if ( empty( $options ) || ! is_array( $options ) ) {
printf(
'<p class="description" style="color: red;">%s</p>',
esc_html__( 'Options are missing or does not in array format.' )
);
}
$count = 1;
foreach ( $options as $value => $label ) {
printf(
'<input type="radio" name="%1$s[%2$s]" id="%3$s" value="%4$s" class="%5$s" %6$s />',
esc_attr( $this->option_name ),
esc_attr( $this->id ),
esc_attr( $this->id . $count ),
esc_attr( $value ),
esc_attr( $class ),
checked( $field_value, $value, false )
);
printf(
'<label for="%1$s">%2$s</label><br>',
esc_attr( $this->id . $count ),
esc_html( $label )
);
$count++;
}
}
}