-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-settings-api.php
1698 lines (1424 loc) · 47.9 KB
/
class-settings-api.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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Settings API.
*
* Functions to register, read, write and update settings.
* Portions of this code have been inspired by Easy Digital Downloads, WordPress Settings Sandbox, WordPress Settings API class, etc.
*
* @link https://webberzone.com
*
* @package Plugin_Name
* @subpackage Admin
*/
// Set the namespace - Replace Plugin_Name with the name of your plugin.
namespace Plugin_Name_Admin;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( ! class_exists( 'Settings_API' ) ) :
/**
* Settings API wrapper class
*
* @version 2.2.0
*/
class Settings_API {
/**
* Current version number
*
* @var string
*/
const VERSION = '2.2.0';
/**
* Settings Key.
*
* @var string Settings Key.
*/
public $settings_key;
/**
* Prefix which is used for creating the unique filters and actions.
*
* @var string Prefix.
*/
public $prefix;
/**
* Translation strings.
*
* @var array Translation strings.
*/
public $translation_strings;
/**
* Menu type.
*
* @see add_custom_menu_page()
*
* @var string Menu slug.
*/
public $menu_type;
/**
* The slug name of the parent of the menu.
*
* @var string Menu slug of the parent.
*/
public $parent_slug;
/**
* The slug name to refer to this menu by (should be unique for this menu).
*
* @var string Menu slug.
*/
public $menu_slug;
/**
* Default navigation tab.
*
* @var string Default navigation tab.
*/
protected $default_tab;
/**
* Settings page.
*
* @var string Settings page.
*/
public $settings_page;
/**
* Admin Footer Text. Displayed at the bottom of the plugin settings page.
*
* @var string Admin Footer Text.
*/
protected $admin_footer_text;
/**
* Array containing the settings' sections.
*
* @var array Settings sections array.
*/
protected $settings_sections = array();
/**
* Array containing the settings' fields.
*
* @var array Settings fields array.
*/
protected $registered_settings = array();
/**
* Array containing the settings' fields that need to be upgraded to the current Settings API.
*
* @var array Settings fields array.
*/
protected $upgraded_settings = array();
/**
* Help sidebar content.
*
* @var string Admin Footer Text.
*/
protected $help_sidebar;
/**
* Array of help tabs.
*
* @var array Settings sections array.
*/
protected $help_tabs = array();
/**
* Main constructor class.
*
* @param string $settings_key Settings key.
* @param string $prefix Prefix. Used for actions and filters.
* @param mixed $args {
* Array or string of arguments. Default is blank array.
* @type array $translation_strings Translation strings.
* @type array $settings_sections Settings sections.
* @type array $props Properties.
* @type array $registered_settings Registered settings.
* @type array $upgraded_settings Upgraded settings.
* }
*/
public function __construct( $settings_key, $prefix, $args ) {
if ( ! defined( 'WZ_SETTINGS_API_VERSION' ) ) {
define( 'WZ_SETTINGS_API_VERSION', self::VERSION );
}
$this->settings_key = $settings_key;
$this->prefix = $prefix;
$defaults = array(
'translation_strings' => array(),
'props' => array(),
'settings_sections' => array(),
'registered_settings' => array(),
'upgraded_settings' => array(),
);
$args = wp_parse_args( $args, $defaults );
$this->hooks();
$this->set_translation_strings( $args['translation_strings'] );
$this->set_props( $args['props'] );
$this->set_sections( $args['settings_sections'] );
$this->set_registered_settings( $args['registered_settings'] );
$this->set_upgraded_settings( $args['upgraded_settings'] );
}
/**
* Adds the functions to the appropriate WordPress hooks.
*/
public function hooks() {
add_action( 'admin_menu', array( $this, 'admin_menu' ), 11 );
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_footer_text', array( $this, 'admin_footer_text' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
}
/**
* Sets properties.
*
* @param array|string $args {
* Array or string of arguments. Default is blank array.
*
* @type string $menu_type Admin menu type. See add_custom_menu_page() for options.
* @type string $menu_parent Parent menu slug.
* @type string $menu_slug Admin menu slug.
* @type string $default_tab Default tab.
* @type string $admin_footer_text Admin footer text.
* @type string $help_sidebar Help sidebar.
* @type array $help_tabs Help tabs.
* }
*/
public function set_props( $args ) {
$defaults = array(
'menu_type' => 'options',
'parent_slug' => 'options-general.php',
'menu_slug' => '',
'default_tab' => 'general',
'admin_footer_text' => '',
'help_sidebar' => '',
'help_tabs' => array(),
);
$args = wp_parse_args( $args, $defaults );
foreach ( $args as $name => $value ) {
$this->$name = $value;
}
}
/**
* Sets translation strings.
*
* @param array $strings {
* Array of translation strings.
*
* @type string $page_title Page title.
* @type string $menu_title Menu title.
* @type string $page_header Page header.
* @type string $reset_message Reset message.
* @type string $success_message Success message.
* @type string $save_changes Save changes button label.
* @type string $reset_settings Reset settings button label.
* @type string $reset_button_confirm Reset button confirmation message.
* @type string $checkbox_modified Checkbox modified label.
* }
*
* @return void
*/
public function set_translation_strings( $strings ) {
// Args prefixed with an underscore are reserved for internal use.
$defaults = array(
'page_title' => '',
'menu_title' => '',
'page_header' => '',
'reset_message' => __( 'Settings have been reset to their default values. Reload this page to view the updated settings.' ),
'success_message' => __( 'Settings updated.' ),
'save_changes' => __( 'Save Changes' ),
'reset_settings' => __( 'Reset all settings' ),
'reset_button_confirm' => __( 'Do you really want to reset all these settings to their default values?' ),
'checkbox_modified' => __( 'Modified from default setting' ),
);
$strings = wp_parse_args( $strings, $defaults );
$this->translation_strings = $strings;
}
/**
* Set settings sections
*
* @param array $sections Setting sections array.
* @return object Class object.
*/
public function set_sections( $sections ) {
$this->settings_sections = (array) $sections;
return $this;
}
/**
* Add a single section
*
* @param array $section New Section.
* @return object Object of the class instance.
*/
public function add_section( $section ) {
$this->settings_sections[] = $section;
return $this;
}
/**
* Set the settings fields for registered settings.
*
* @param array $registered_settings Registered settings array.
* @return object Object of the class instance.
*/
public function set_registered_settings( $registered_settings ) {
$this->registered_settings = (array) $registered_settings;
return $this;
}
/**
* Set the settings fields for settings to upgrade.
*
* @param array $upgraded_settings Settings array.
* @return object Object of the class instance.
*/
public function set_upgraded_settings( $upgraded_settings = array() ) {
$this->upgraded_settings = (array) $upgraded_settings;
return $this;
}
/**
* Add a menu page to the WordPress admin area.
*
* @param array $menu Array of settings for the menu page.
*/
public function add_custom_menu_page( $menu ) {
$defaults = array(
// Modes: submenu, management, options, theme, plugins, users, dashboard, posts, media, links, pages, comments.
'type' => 'submenu',
// Submenu default settings.
'parent_slug' => 'options-general.php',
'page_title' => '',
'menu_title' => '',
'capability' => 'manage_options',
'menu_slug' => '',
'function' => array( $this, 'plugin_settings' ),
// Menu default settings.
'icon_url' => 'dashicons-admin-generic',
'position' => null,
);
$menu = wp_parse_args( $menu, $defaults );
switch ( $menu['type'] ) {
case 'submenu':
$menu_page = add_submenu_page(
$menu['parent_slug'],
$menu['page_title'],
$menu['menu_title'],
$menu['capability'],
$menu['menu_slug'],
$menu['function'],
$menu['position']
);
break;
case 'management':
case 'options':
case 'theme':
case 'plugins':
case 'users':
case 'dashboard':
case 'posts':
case 'media':
case 'links':
case 'pages':
case 'comments':
$f = 'add_' . $menu['type'] . '_page';
if ( function_exists( $f ) ) {
$menu_page = $f(
$menu['page_title'],
$menu['menu_title'],
$menu['capability'],
$menu['menu_slug'],
$menu['function'],
$menu['position']
);
}
break;
default:
$menu_page = add_menu_page(
$menu['page_title'],
$menu['menu_title'],
$menu['capability'],
$menu['menu_slug'],
$menu['function'],
$menu['icon_url'],
$menu['position']
);
break;
}
return $menu_page;
}
/**
* Add admin menu.
*/
public function admin_menu() {
$menu = array(
'type' => $this->menu_type,
'parent_slug' => $this->parent_slug,
'page_title' => $this->translation_strings['page_title'],
'menu_title' => $this->translation_strings['menu_title'],
'capability' => 'manage_options',
'menu_slug' => $this->menu_slug,
'function' => array( $this, 'plugin_settings' ),
);
$this->settings_page = $this->add_custom_menu_page( $menu );
// Load the settings contextual help.
add_action( 'load-' . $this->settings_page, array( $this, 'settings_help' ) );
}
/**
* Enqueue scripts and styles.
*
* @param string $hook The current admin page.
*/
public function admin_enqueue_scripts( $hook ) {
if ( $hook === $this->settings_page ) {
$this->enqueue_scripts_styles();
}
}
/**
* Enqueues all scripts, styles, settings, and templates necessary to use the Settings API.
*/
public function enqueue_scripts_styles() {
$minimize = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_media();
wp_enqueue_script( 'wp-color-picker' );
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'jquery-ui-autocomplete' );
wp_enqueue_script( 'jquery-ui-tabs' );
wp_enqueue_code_editor(
array(
'type' => 'text/html',
'codemirror' => array(
'indentUnit' => 2,
'tabSize' => 2,
),
)
);
wp_enqueue_script(
'wz-admin-js',
plugins_url( 'js/admin-scripts' . $minimize . '.js', __FILE__ ),
array( 'jquery' ),
self::VERSION,
true
);
wp_enqueue_script(
'wz-codemirror-js',
plugins_url( 'js/apply-codemirror' . $minimize . '.js', __FILE__ ),
array( 'jquery' ),
self::VERSION,
true
);
wp_enqueue_script(
'wz-taxonomy-suggest-js',
plugins_url( 'js/taxonomy-suggest' . $minimize . '.js', __FILE__ ),
array( 'jquery' ),
self::VERSION,
true
);
}
/**
* Initialize and registers the settings sections and fileds to WordPress
*
* Usually this should be called at `admin_init` hook.
*
* This public function gets the initiated settings sections and fields. Then
* registers them to WordPress and ready for use.
*/
public function admin_init() {
$settings_key = $this->settings_key;
if ( false === get_option( $settings_key ) ) {
add_option( $settings_key, $this->settings_defaults() );
}
foreach ( $this->registered_settings as $section => $settings ) {
add_settings_section(
"{$settings_key}_{$section}", // ID used to identify this section and with which to register options.
__return_null(), // No title, we will handle this via a separate function.
'__return_false', // No callback function needed. We'll process this separately.
"{$settings_key}_{$section}" // Page on which these options will be added.
);
foreach ( $settings as $setting ) {
$args = wp_parse_args(
$setting,
array(
'section' => $section,
'id' => null,
'name' => '',
'desc' => '',
'type' => null,
'options' => '',
'max' => null,
'min' => null,
'step' => null,
'size' => null,
'field_class' => '',
'field_attributes' => '',
'placeholder' => '',
)
);
$id = $args['id'];
$name = $args['name'];
$type = isset( $args['type'] ) ? $args['type'] : 'text';
$callback = method_exists( $this, "callback_{$type}" ) ? array( $this, "callback_{$type}" ) : array( $this, 'callback_missing' );
add_settings_field(
"{$settings_key}[{$id}]", // ID of the settings field. We save it within the settings array.
$name, // Label of the setting.
$callback, // Function to handle the setting.
"{$settings_key}_{$section}", // Page to display the setting. In our case it is the section as defined above.
"{$settings_key}_{$section}", // Name of the section.
$args
);
}
}
// Register the settings into the options table.
register_setting( $settings_key, $settings_key, array( $this, 'settings_sanitize' ) );
}
/**
* Flattens $this->registered_settings into $setting[id] => $setting[type] format.
*
* @return array Default settings
*/
public function get_registered_settings_types() {
$options = array();
// Populate some default values.
foreach ( $this->registered_settings as $tab => $settings ) {
foreach ( $settings as $option ) {
$options[ $option['id'] ] = $option['type'];
}
}
/**
* Filters the settings array.
*
* @param array $options Default settings.
*/
return apply_filters( $this->prefix . '_get_settings_types', $options );
}
/**
* Default settings.
*
* @return array Default settings
*/
public function settings_defaults() {
$options = array();
// Populate some default values.
foreach ( $this->registered_settings as $tab => $settings ) {
foreach ( $settings as $option ) {
// When checkbox is set to true, set this to 1.
if ( 'checkbox' === $option['type'] && ! empty( $option['options'] ) ) {
$options[ $option['id'] ] = 1;
} else {
$options[ $option['id'] ] = 0;
}
// If an option is set.
if ( in_array( $option['type'], array( 'textarea', 'css', 'html', 'text', 'url', 'csv', 'color', 'numbercsv', 'postids', 'posttypes', 'number', 'wysiwyg', 'file', 'password' ), true ) && isset( $option['options'] ) ) {
$options[ $option['id'] ] = $option['options'];
}
if ( in_array( $option['type'], array( 'multicheck', 'radio', 'select', 'radiodesc', 'thumbsizes' ), true ) && isset( $option['default'] ) ) {
$options[ $option['id'] ] = $option['default'];
}
}
}
$upgraded_settings = $this->upgraded_settings;
if ( false !== $upgraded_settings ) {
$options = array_merge( $options, $upgraded_settings );
}
/**
* Filters the default settings array.
*
* @param array $options Default settings.
*/
return apply_filters( $this->prefix . '_settings_defaults', $options );
}
/**
* Get the default option for a specific key
*
* @param string $key Key of the option to fetch.
* @return mixed
*/
public function get_default_option( $key = '' ) {
$default_settings = $this->settings_defaults();
if ( array_key_exists( $key, $default_settings ) ) {
return $default_settings[ $key ];
} else {
return false;
}
}
/**
* Reset settings.
*
* @return void
*/
public function settings_reset() {
delete_option( $this->settings_key );
}
/**
* Get field description for display.
*
* @param array $args settings Arguments array.
*/
public function get_field_description( $args ) {
if ( ! empty( $args['desc'] ) ) {
$desc = '<p class="description">' . wp_kses_post( $args['desc'] ) . '</p>';
} else {
$desc = '';
}
/**
* After Settings Output filter
*
* @param string $desc Description of the field.
* @param array Arguments array.
*/
$desc = apply_filters( $this->prefix . '_setting_field_description', $desc, $args );
return $desc;
}
/**
* Get the value of a settings field.
*
* @param string $option Settings field name.
* @param string $default_value Default text if it's not found.
* @return string
*/
public function get_option( $option, $default_value = '' ) {
$options = get_option( $this->settings_key );
if ( isset( $options[ $option ] ) ) {
return $options[ $option ];
}
return $default_value;
}
/**
* Miscellaneous callback funcion
*
* @param array $args Arguments array.
* @return void
*/
public function callback_missing( $args ) {
/* translators: 1: Code. */
printf( esc_html__( 'The callback function used for the %1$s setting is missing.' ), '<strong>' . esc_attr( $args['id'] ) . '</strong>' );
}
/**
* Header Callback
*
* Renders the header.
*
* @param array $args Arguments passed by the setting.
* @return void
*/
public function callback_header( $args ) {
$html = $this->get_field_description( $args );
/**
* After Settings Output filter
*
* @param string $html HTML string.
* @param array Arguments array.
*/
echo apply_filters( $this->prefix . '_after_setting_output', $html, $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Descriptive text callback.
*
* Renders descriptive text onto the settings field.
*
* @param array $args Array of arguments.
* @return void
*/
public function callback_descriptive_text( $args ) {
$this->callback_header( $args );
}
/**
* Display text fields.
*
* @param array $args Array of arguments.
*/
public function callback_text( $args ) {
$value = $this->get_option( $args['id'], $args['options'] );
$size = sanitize_html_class( ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular' );
$class = sanitize_html_class( $args['field_class'] );
$placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="' . $args['placeholder'] . '"';
$disabled = ! empty( $args['disabled'] ) ? ' disabled="disabled"' : '';
$readonly = ( isset( $args['readonly'] ) && true === $args['readonly'] ) ? ' readonly="readonly"' : '';
$attributes = $disabled . $readonly;
foreach ( (array) $args['field_attributes'] as $attribute => $val ) {
$attributes .= sprintf( ' %1$s="%2$s"', $attribute, esc_attr( $val ) );
}
$html = sprintf(
'<input type="text" id="%1$s[%2$s]" name="%1$s[%2$s]" class="%3$s" value="%4$s" %5$s %6$s />',
$this->settings_key,
sanitize_key( $args['id'] ),
$class . ' ' . $size . '-text',
esc_attr( stripslashes( $value ) ),
$attributes,
$placeholder
);
$html .= $this->get_field_description( $args );
/** This filter has been defined in class-settings-api.php */
echo apply_filters( $this->prefix . '_after_setting_output', $html, $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Display url fields.
*
* @param array $args Array of arguments.
*/
public function callback_url( $args ) {
$this->callback_text( $args );
}
/**
* Display csv fields.
*
* @param array $args Array of arguments.
*/
public function callback_csv( $args ) {
$this->callback_text( $args );
}
/**
* Display color fields.
*
* @param array $args Array of arguments.
*/
public function callback_color( $args ) {
$this->callback_text( $args );
}
/**
* Display numbercsv fields.
*
* @param array $args Array of arguments.
*/
public function callback_numbercsv( $args ) {
$this->callback_text( $args );
}
/**
* Display postids fields.
*
* @param array $args Array of arguments.
*/
public function callback_postids( $args ) {
$this->callback_text( $args );
}
/**
* Display textarea.
*
* @param array $args Array of arguments.
* @return void
*/
public function callback_textarea( $args ) {
$value = $this->get_option( $args['id'], $args['options'] );
$class = sanitize_html_class( $args['field_class'] );
$html = sprintf(
'<textarea class="%4$s" cols="50" rows="5" id="%1$s[%2$s]" name="%1$s[%2$s]">%3$s</textarea>',
$this->settings_key,
sanitize_key( $args['id'] ),
esc_textarea( stripslashes( $value ) ),
'large-text ' . $class
);
$html .= $this->get_field_description( $args );
/** This filter has been defined in class-settings-api.php */
echo apply_filters( $this->prefix . '_after_setting_output', $html, $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Display CSS fields.
*
* @param array $args Array of arguments.
* @return void
*/
public function callback_css( $args ) {
$this->callback_textarea( $args );
}
/**
* Display HTML fields.
*
* @param array $args Array of arguments.
* @return void
*/
public function callback_html( $args ) {
$this->callback_textarea( $args );
}
/**
* Display checkboxes.
*
* @param array $args Array of arguments.
* @return void
*/
public function callback_checkbox( $args ) {
$value = $this->get_option( $args['id'], $args['options'] );
$checked = ! empty( $value ) ? checked( 1, $value, false ) : '';
$default = isset( $args['options'] ) ? (int) $args['options'] : '';
$html = sprintf( '<input type="hidden" name="%1$s[%2$s]" value="-1" />', $this->settings_key, sanitize_key( $args['id'] ) );
$html .= sprintf( '<input type="checkbox" id="%1$s[%2$s]" name="%1$s[%2$s]" value="1" %3$s />', $this->settings_key, sanitize_key( $args['id'] ), $checked );
$html .= ( (bool) $value !== (bool) $default ) ? '<em style="color:orange">' . $this->translation_strings['checkbox_modified'] . '</em>' : ''; // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
$html .= $this->get_field_description( $args );
/** This filter has been defined in class-settings-api.php */
echo apply_filters( $this->prefix . '_after_setting_output', $html, $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Multicheck Callback
*
* Renders multiple checkboxes.
*
* @param array $args Array of arguments.
* @return void
*/
public function callback_multicheck( $args ) {
$html = '';
$value = $this->get_option( $args['id'], $args['options'] );
if ( ! empty( $args['options'] ) ) {
$html .= sprintf( '<input type="hidden" name="%1$s[%2$s]" value="-1" />', $this->settings_key, $args['id'] );
foreach ( $args['options'] as $key => $option ) {
if ( isset( $value[ $key ] ) ) {
$enabled = $key;
} else {
$enabled = null;
}
$html .= sprintf(
'<input name="%1$s[%2$s][%3$s]" id="%1$s[%2$s][%3$s]" type="checkbox" value="%4$s" %5$s /> ',
$this->settings_key,
sanitize_key( $args['id'] ),
sanitize_key( $key ),
esc_attr( $key ),
checked( $key, $enabled, false )
);
$html .= sprintf(
'<label for="%1$s[%2$s][%3$s]">%4$s</label> <br />',
$this->settings_key,
sanitize_key( $args['id'] ),
sanitize_key( $key ),
$option
);
}
$html .= $this->get_field_description( $args );
}
/** This filter has been defined in class-settings-api.php */
echo apply_filters( $this->prefix . '_after_setting_output', $html, $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Radio Callback
*
* Renders radio boxes.
*
* @param array $args Array of arguments.
* @return void
*/
public function callback_radio( $args ) {
$html = '';
$value = $this->get_option( $args['id'], $args['default'] );
foreach ( $args['options'] as $key => $option ) {
$html .= sprintf(
'<input name="%1$s[%2$s]" id="%1$s[%2$s][%3$s]" type="radio" value="%3$s" %4$s /> ',
$this->settings_key,
sanitize_key( $args['id'] ),
$key,
checked( $value, $key, false )
);
$html .= sprintf(
'<label for="%1$s[%2$s][%3$s]">%4$s</label> <br />',
$this->settings_key,
sanitize_key( $args['id'] ),
$key,
$option
);
}
$html .= $this->get_field_description( $args );
/** This filter has been defined in class-settings-api.php */
echo apply_filters( $this->prefix . '_after_setting_output', $html, $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Radio callback with description.
*
* Renders radio boxes with each item having it separate description.
*
* @param array $args Array of arguments.
* @return void
*/
public function callback_radiodesc( $args ) {
$html = '';
$value = $this->get_option( $args['id'], $args['default'] );
foreach ( $args['options'] as $option ) {
$html .= sprintf(
'<input name="%1$s[%2$s]" id="%1$s[%2$s][%3$s]" type="radio" value="%3$s" %4$s /> ',
$this->settings_key,
sanitize_key( $args['id'] ),
$option['id'],
checked( $value, $option['id'], false )
);
$html .= sprintf(
'<label for="%1$s[%2$s][%3$s]">%4$s</label>',
$this->settings_key,
sanitize_key( $args['id'] ),
$option['id'],
$option['name']
);
$html .= ': <em>' . wp_kses_post( $option['description'] ) . '</em> <br />';
}
$html .= $this->get_field_description( $args );
/** This filter has been defined in class-settings-api.php */
echo apply_filters( $this->prefix . '_after_setting_output', $html, $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Radio callback with description.
*
* Renders radio boxes with each item having it separate description.
*
* @param array $args Array of arguments.
* @return void
*/
public function callback_thumbsizes( $args ) {
$html = '';
$value = $this->get_option( $args['id'], $args['default'] );
foreach ( $args['options'] as $name => $option ) {
$html .= sprintf(
'<input name="%1$s[%2$s]" id="%1$s[%2$s][%3$s]" type="radio" value="%3$s" %4$s /> ',
$this->settings_key,
sanitize_key( $args['id'] ),
$name,
checked( $value, $name, false )
);
$html .= sprintf(
'<label for="%1$s[%2$s][%3$s]">%3$s (%4$sx%5$s%6$s)</label> <br />',
$this->settings_key,
sanitize_key( $args['id'] ),
$name,