-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclass-boo-settings-helper.php
1344 lines (1028 loc) · 35.5 KB
/
class-boo-settings-helper.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
/**
* Name: Boo Settings API helper class
*
* Version: 5.3
* Author: RaoAbid | BooSpot
*
* @author RaoAbid | BooSpot
* @link https://github.com/boospot/boo-settings-helper
*/
if ( ! class_exists( 'Boo_Settings_Helper' ) ):
class Boo_Settings_Helper {
public $debug = false;
public $log = false;
public $plugin_basename = '';
public $action_links = array();
public $config_menu = array();
public $field_types = array();
protected $is_tabs = false;
public $slug;
protected $active_tab;
protected $sections_count;
protected $sections_ids;
protected $fields_ids;
// flag for options processing
protected $is_settings_saved_once = false;
protected $sanitized_data;
protected $prefix = '';
protected $is_simple_options = true;
/**
* settings sections array
*
* @var array
*/
protected $settings_sections = array();
/**
* Settings fields array
*
* @var array
*/
protected $settings_fields = array();
public function __construct( $config_array = null ) {
if ( ! empty( $config_array ) && is_array( $config_array ) ) {
$this->set_properties( $config_array );
}
add_action( 'admin_init', array( $this, 'admin_init' ) );
}
public function setup_hooks() {
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
}
protected function get_default_config() {
return array(
'tabs' => false,
'menu' => $this->get_default_config_menu(),
);
}
/**
* Set Properties of the class
*/
protected function set_properties( array $config_array ) {
// Normalise config array
$config_array = wp_parse_args( $config_array, $this->get_default_config() );
if ( isset( $config_array['prefix'] ) ) {
$this->prefix = ( ! empty( $config_array['prefix'] ) ) ? sanitize_key( $config_array['prefix'] ) : '';
}
if ( isset( $config_array['tabs'] ) ) {
$this->set_tabs( $config_array['tabs'] );
}
// Do we have menu config, if yes, call the method
if ( isset( $config_array['menu'] ) ) {
$this->set_menu( $config_array['menu'] );
}
// Do we have sections config, if yes, call the method
if ( isset( $config_array['sections'] ) ) {
$this->set_sections( $config_array['sections'] );
}
// Do we have fields config, if yes, call the method
if ( isset( $config_array['fields'] ) ) {
$this->set_fields( $config_array['fields'] );
}
if ( isset( $config_array['links'] ) ) {
$this->set_links( $config_array['links'] );
}
$this->set_active_tab();
}
/**
*
*/
public function set_active_tab() {
$this->active_tab =
( isset( $_GET['tab'] ) )
? sanitize_key( $_GET['tab'] )
: $this->settings_sections[0]['id'];
}
public function set_links( array $config_links ) {
if (
isset( $config_links['plugin_basename'] ) &&
! empty( $config_links['plugin_basename'] )
) {
$this->plugin_basename = $config_links['plugin_basename'];
$this->action_links = isset( $config_links['action_links'] ) ? $config_links['action_links'] : true;
$prefix = is_network_admin() ? 'network_admin_' : '';
add_filter(
"{$prefix}plugin_action_links_{$this->plugin_basename}",
array( $this, 'plugin_action_links' ),
10, // priority
4 // parameters
);
}
}
public function get_default_settings_url() {
if ( $this->config_menu['submenu'] ) {
$options_base_file_name = $this->config_menu['parent'];
if ( in_array( $options_base_file_name, array(
'options-general.php',
'edit-comments.php',
'plugins.php',
'edit.php',
'upload.php',
'themes.php',
'users.php',
'tools.php'
) ) ) {
return admin_url( "{$options_base_file_name}?page={$this->config_menu['slug']}" );
} else {
return admin_url( "admin.php?page={$this->config_menu['slug']}" );
}
} else {
return admin_url( "admin.php?page={$this->config_menu['slug']}" );
}
}
public function get_default_settings_link() {
return array(
'<a href="' . $this->get_default_settings_url() . '">' . __( 'Settings' ) . '</a>',
);
}
/**
* Register "settings" for plugin option page in plugins list
*
* @param array $links plugin links
*
* @return array possibly modified $links
*/
public function plugin_action_links( $links, $plugin_file, $plugin_data, $context ) {
/**
* Documentation :
* https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name)
*/
// BOOL of settings is given true | false
if ( is_bool( $this->action_links ) ) {
// FALSE: If it is false, no need to go further
if ( ! $this->action_links ) {
return $links;
}
// TRUE: if Settings link is not defined, lets create one
if ( $this->action_links ) {
return array_merge( $links, $this->get_default_settings_link() );
}
} // if ( is_bool( $this->config['settings_link'] ) )
// Admin URL of settings is given
if ( ! is_bool( $this->action_links ) && ! is_array( $this->action_links ) ) {
$settings_link = array(
'<a href="' . admin_url( esc_url( $this->action_links ) ) . '">' . __( 'Settings' ) . '</a>',
);
return array_merge( $settings_link, $links );
}
// Array of settings_link is given
if ( is_array( $this->action_links ) ) {
$settings_link_array = array();
foreach ( $this->action_links as $link ) {
$link_text = isset( $link['text'] ) ? sanitize_text_field( $link['text'] ) : __( 'Settings', '' );
$link_url_un_clean = isset( $link['url'] ) ? $link['url'] : '#';
$link_type = isset( $link['type'] ) ? sanitize_key( $link['type'] ) : 'default';
switch ( $link_type ) {
case ( 'external' ):
$link_url = esc_url_raw( $link_url_un_clean );
break;
case ( 'internal' ):
$link_url = admin_url( esc_url( $link_url_un_clean ) );
break;
default:
$link_url = $this->get_default_settings_url();
}
$settings_link_array[] = '<a href="' . $link_url . '">' . $link_text . '</a>';
}
return array_merge( $settings_link_array, $links );
} // if ( $this->action_links ) )
// if nothing is returned so far, return original $links
return $links;
}
public function set_tabs( $config_tabs ) {
$this->is_tabs = ( (bool) $config_tabs ) ? true : false;
}
/**
* Register plugin option page
*/
public function set_menu( $config_menu ) {
$this->config_menu = array_merge_recursive( $this->config_menu, wp_parse_args( $config_menu, $this->get_default_config_menu() ) );
$this->slug = $this->config_menu['slug'] =
isset( $this->config_menu['slug'] )
? sanitize_key( $this->config_menu['slug'] )
: sanitize_title( $this->config_menu['page_title'] );
// Is it a main menu or sub_menu
if ( ! $this->config_menu['submenu'] ) {
add_menu_page(
$this->config_menu['page_title'],
$this->config_menu['menu_title'],
$this->config_menu['capability'],
$this->config_menu['slug'], //slug
array( $this, 'display_page' ),
$this->config_menu['icon'],
$this->config_menu['position']
);
} else {
add_submenu_page(
$this->config_menu['parent'],
$this->config_menu['page_title'],
$this->config_menu['menu_title'],
$this->config_menu['capability'],
$this->config_menu['slug'], // slug
array( $this, 'display_page' )
);
}
}
/**
* Get default config for menu
* @return array $default
*/
public function get_default_config_menu() {
return apply_filters( 'boo_settings_filter_default_menu_array', array(
//The name of this page
'page_title' => __( 'Plugin Options' ),
// //The Menu Title in Wp Admin
'menu_title' => __( 'Plugin Options' ),
// The capability needed to view the page
'capability' => 'manage_options',
// dashicons id or url to icon
// https://developer.wordpress.org/resource/dashicons/
'icon' => '',
// Required for submenu
'submenu' => false,
// position
'position' => 100,
// For sub menu, we can define parent menu slug (Defaults to Options Page)
'parent' => 'options-general.php',
) );
}
//DEBUG
public function write_log( $type, $log_line ) {
$hash = '';
$fn = plugin_dir_path( __FILE__ ) . '/' . $type . '-' . $hash . '.log';
$log_in_file = file_put_contents( $fn, date( 'Y-m-d H:i:s' ) . ' - ' . $log_line . PHP_EOL, FILE_APPEND );
}
/*
* @return array configured field types
*/
public function get_field_types() {
foreach ( $this->settings_fields as $sections_fields ) {
foreach ( $sections_fields as $field ) {
$this->field_types[] = isset( $field['type'] ) ? sanitize_key( $field['type'] ) : 'text';
}
}
return array_unique( $this->field_types );
}
/**
* @return bool true if its plugin options page is loaded
*/
protected function is_menu_page_loaded() {
$current_screen = get_current_screen();
return substr( $current_screen->id, - strlen( $this->config_menu['slug'] ) ) === $this->config_menu['slug'];
}
/**
* Enqueue scripts and styles
*/
function admin_enqueue_scripts() {
// Conditionally Load scripts and styles for field types configured
// Load scripts for only plugin menu page
if ( ! $this->is_menu_page_loaded() ) {
return null;
}
// Load Color Picker if required
if ( in_array( 'color', $this->get_field_types() ) ) {
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'wp-color-picker' );
}
if ( in_array( 'media', $this->get_field_types() ) || in_array( 'file', $this->get_field_types() ) ) {
wp_enqueue_media();
}
wp_enqueue_script( 'jquery' );
}
/**
* Set settings sections
*
* @param array $sections setting sections array
*/
function set_sections( array $sections ) {
$this->settings_sections = array_merge_recursive( $this->settings_sections, $sections );
$this->sections_count = count( $this->settings_sections );
$this->sections_ids = array_values( $this->settings_sections );
return $this;
}
/**
* Set settings fields
*
* @param array $fields settings fields array
*/
public function set_fields( $fields ) {
$this->settings_fields = array_merge_recursive( $this->settings_fields, $fields );
$this->normalize_fields();
$this->setup_hooks();
return $this;
}
public function get_markup_placeholder( $placeholder ) {
return ' placeholder="' . esc_html( $placeholder ) . '" ';
}
public function get_sanitize_callback_method( $type ) {
return ( method_exists( $this, "sanitize_{$type}" ) )
? array( $this, "sanitize_{$type}" )
: array( $this, "sanitize_text" );
}
public function get_field_markup_callback_method( $type ) {
return ( method_exists( $this, "callback_{$type}" ) )
? array( $this, "callback_{$type}" )
: array( $this, "callback_text" );
}
public function get_field_markup_callback_name( $type ) {
return ( method_exists( $this, "callback_{$type}" ) )
? "callback_{$type}"
: "callback_text";
}
public function get_default_field_args( $field, $section = 'default' ) {
$type = isset( $field['type'] ) ? $field['type'] : 'text';
return array(
'id' => $field['id'],
'label' => '',
'desc' => '',
'type' => 'text',
'placeholder' => '',
'default' => '',
'options' => array(),
'callback' => '',
'sanitize_callback' => '',
'value' => '',
'show_in_rest' => true,
'class' => $field['id'],
'std' => '',
'size' => 'regular',
// Auto Calculated
'name' => $this->prefix . $field['id'],
'label_for' => $this->prefix . $field['id'],
'section' => $section,
);
}
public function normalize_fields() {
foreach ( $this->settings_fields as $section_id => $fields ) {
if ( is_array( $fields ) && ! empty( $fields ) ) {
foreach ( $fields as $i => $field ) {
$this->settings_fields[ $section_id ][ $i ] =
wp_parse_args(
$field,
$this->get_default_field_args( $field, $section_id )
);
}
}
}
}
/**
*
*/
public function get_page_id_for_sections( $section_id = '' ) {
// return $this->config_menu['slug'] . '_' . $section_id;
return $this->config_menu['slug'];
}
public function get_options_group( $section_id = '' ) {
return str_replace( '-', '_', $this->slug ) . "_" . $section_id;
// return str_replace( '-', '_', $this->slug );
}
function display_page() {
// Save Default options in DB with default values
// $this->set_default_db_options();
if ( 'options-general.php' != $this->config_menu['parent'] ) {
settings_errors();
}
echo '<div class="wrap">';
echo "<h1>" . get_admin_page_title() . "</h1>";
// If Debug is ON
if ( $this->debug ) {
echo "<b>TYPES of fields</b>";
$this->var_dump_pretty( $this->get_field_types() );
if ( $this->is_tabs ) {
echo "<b>Active Tab Options Array</b>";
$this->var_dump_pretty( get_option( $this->active_tab ) );
}
}
?>
<div class="metabox-holder">
<?php
if ( $this->is_tabs ) {
$this->show_navigation();
}
?>
<form method="post" action="options.php">
<?php
if ( $this->is_tabs ) {
foreach ( $this->settings_sections as $section ) :
if ( $section['id'] !== $this->active_tab ) {
continue;
}
// for tabs
settings_fields( $this->get_options_group( $section['id'] ) );
do_settings_sections( $this->get_page_id_for_sections( $section['id'] ) );
endforeach; // end foreach
} else {
// for tab-less
settings_fields( $this->get_options_group() );
do_settings_sections( $this->get_page_id_for_sections() );
}
?>
<div style="padding-left: 10px">
<?php submit_button(); ?>
</div>
</form>
</div>
<?php
// Call General Scripts
$this->script_general();
?>
</div>
<?php
}
public function add_settings_section() {
//register settings sections
foreach ( $this->settings_sections as $section ) {
if ( $this->is_tabs ) {
if ( $section['id'] !== $this->active_tab ) {
continue;
}
}
// Callback for Section Description
if ( isset( $section['callback'] ) && is_callable( $section['callback'] ) ) {
$callback = $section['callback'];
} else if ( isset( $section['desc'] ) && ! empty( $section['desc'] ) ) {
$callback = function () use ( $section ) {
echo "<div class='inside'>" . esc_html( $section['desc'] ) . "</div>";
};
} else {
$callback = null;
}
add_settings_section(
$section['id'],
$section['title'],
$callback,
$this->get_page_id_for_sections( $section['id'] ) // page
);
}
}
public function add_settings_field_loop() {
//register settings fields
foreach ( $this->settings_fields as $section_id => $fields ) {
if ( $this->is_tabs ) {
if ( $section_id !== $this->active_tab ) {
continue;
}
}
foreach ( $fields as $field ) :
$field['value'] = get_option( $field['name'], $field['default'] );
add_settings_field(
$field['name'],
$field['label'],
( is_callable( $field['callback'] ) )
? $field['callback']
: $this->get_field_markup_callback_method( $field['type'] ),
$this->get_page_id_for_sections( $section_id ), // page
$section_id, // section
$field // args
);
endforeach;
}
}
public function register_settings() {
// creates our settings in the options table
foreach ( $this->settings_fields as $section_id => $fields ) :
foreach ( $fields as $field ) :
register_setting(
( $this->is_tabs ) ?
$this->get_options_group( $field['section'] )
: $this->get_options_group(), // options_group
$field['name'], // options_id
array(
// 'type' => $field['type'],
'description' => $field['desc'],
'sanitize_callback' => ( is_callable( $field['sanitize_callback'] ) )
? $field['sanitize_callback']
: $this->get_sanitize_callback_method( $field['type'] ),
'show_in_rest' => $field['show_in_rest'],
'default' => $field['default'],
)
);
endforeach;
endforeach;
}
function sanitize_text( $value ) {
return ( ! empty( $value ) ) ? sanitize_text_field( $value ) : '';
}
function sanitize_number( $value ) {
return ( is_numeric( $value ) ) ? $value : 0;
}
function sanitize_editor( $value ) {
return wp_kses_post( $value );
}
function sanitize_textarea( $value ) {
return sanitize_textarea_field( $value );
}
function sanitize_checkbox( $value ) {
return ( $value === '1' ) ? 1 : 0;
}
function sanitize_select( $value ) {
return $this->sanitize_text( $value );
}
function sanitize_user_role( $value ) {
return sanitize_key( $value );
}
function sanitize_radio( $value ) {
return $this->sanitize_text( $value );
}
function sanitize_multicheck( $value ) {
return ( is_array( $value ) ) ? array_map( 'sanitize_text_field', $value ) : array();
}
function sanitize_color( $value ) {
if ( false === strpos( $value, 'rgba' ) ) {
return sanitize_hex_color( $value );
} else {
// By now we know the string is formatted as an rgba color so we need to further sanitize it.
$value = trim( $value, ' ' );
$red = $green = $blue = $alpha = '';
sscanf( $value, 'rgba(%d,%d,%d,%f)', $red, $green, $blue, $alpha );
return 'rgba(' . $red . ',' . $green . ',' . $blue . ',' . $alpha . ')';
}
}
function sanitize_password( $value ) {
$password_get_info = password_get_info( $value );
if ( isset( $password_get_info['algo'] ) && $password_get_info['algo'] ) {
unset( $password_get_info );
return $value;
// do nothing, we have got already stored hashed password
} else {
unset( $password_get_info );
return password_hash( $value, PASSWORD_DEFAULT );
}
}
function sanitize_url( $value ) {
return esc_url_raw( $value );
}
function sanitize_file( $value ) {
// TODO: if the option to store file as file url
return esc_url_raw( $value );
}
function sanitize_html( $value ) {
// nothing to save
return '';
}
function sanitize_posts( $value ) {
// Only store post id
return absint( $value );
}
function sanitize_pages( $value ) {
// Only store page id
return absint( $value );
}
function sanitize_media( $value ) {
// Only store media id
return absint( $value );
}
/**
* Displays a text field for a settings field
*
* @param array $args settings field args
*/
function callback_text( $args ) {
$html = sprintf(
'<input
type="%1$s"
class="%2$s-text %8$s"
id="%3$s[%4$s]"
name="%7$s"
value="%5$s"
%6$s
/>',
$args['type'],
$args['size'],
$args['section'],
$args['id'],
$args['value'],
$this->get_markup_placeholder( $args['placeholder'] ),
$args['name'],
$args['class']
);
$html .= $this->get_field_description( $args );
echo $html;
unset( $html );
}
/**
* Initialize and registers the settings sections and fileds to WordPress
*
* Usually this should be called at `admin_init` hook.
*
* This function gets the initiated settings sections and fields. Then
* registers them to WordPress and ready for use.
*/
function admin_init() {
$this->add_settings_section();
$this->add_settings_field_loop();
$this->register_settings();
}
/**
* Get field description for display
*
* @param array $args settings field args
*/
public function get_field_description( $args ) {
return sprintf( '<p class="description">%s</p>', $args['desc'] );
}
/**
* Displays a url field for a settings field
*
* @param array $args settings field args
*/
function callback_url( $args ) {
$this->callback_text( $args );
}
/**
* Displays a number field for a settings field
*
* @param array $args settings field args
*/
function callback_number( $args ) {
$min = ( isset( $args['options']['min'] ) && ! empty( $args['options']['min'] ) ) ? ' min="' . $args['options']['min'] . '"' : '';
$max = ( isset( $args['options']['max'] ) && ! empty( $args['options']['max'] ) ) ? ' max="' . $args['options']['max'] . '"' : '';
$step = ( isset( $args['options']['step'] ) && ! empty( $args['options']['step'] ) ) ? ' step="' . $args['options']['step'] . '"' : '';
$html = sprintf(
'<input
type="%1$s"
class="%2$s-text"
id="%3$s[%4$s]"
name="%10$s"
value="%5$s"
%6$s
%7$s
%8$s
%9$s
/>',
$args['type'],
$args['size'],
$args['section'],
$args['id'],
$args['value'],
$this->get_markup_placeholder( $args['placeholder'] ),
$min,
$max,
$step,
$args['name']
);
$html .= $this->get_field_description( $args );
echo $html;
unset( $html, $min, $max, $step );
}
/**
* Displays a checkbox for a settings field
*
* @param array $args settings field args
*/
function callback_checkbox( $args ) {
$html = '<fieldset>';
$html .= sprintf( '<label for="%1$s[%2$s]">', $args['section'], $args['id'] );
$html .= sprintf( '<input type="checkbox" class="checkbox" id="%1$s[%2$s]" name="%4$s" value="1" %3$s />', $args['section'], $args['id'], checked( $args['value'], '1', false ), $args['name'] );
$html .= sprintf( '%1$s</label>', $args['desc'] );
$html .= '</fieldset>';
echo $html;
}
/**
* Displays a multicheckbox for a settings field
*
* @param array $args settings field args
*/
function callback_multicheck( $args ) {
$value = $args['value'];
if ( empty( $value ) ) {
$value = $args['default'];
}
$html = '<fieldset>';
foreach ( $args['options'] as $key => $label ) {
$checked = isset( $value[ $key ] ) ? $value[ $key ] : '0';
$html .= sprintf( '<label for="%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key );
$html .= sprintf( '<input type="checkbox" class="checkbox" id="%1$s[%2$s][%3$s]" name="%5$s[%3$s]" value="%3$s" %4$s />', $args['section'], $args['id'], $key, checked( $checked, $key, false ), $args['name'] );
$html .= sprintf( '%1$s</label><br>', $label );
}
$html .= $this->get_field_description( $args );
$html .= '</fieldset>';
echo $html;
unset( $value, $html );
}
/**
* Displays a radio button for a settings field
*
* @param array $args settings field args
*/
function callback_radio( $args ) {
$value = $args['value'];
if ( empty( $value ) ) {
$value = is_array( $args['default'] ) ? $args['default'] : array();
}
$html = '<fieldset>';
foreach ( $args['options'] as $key => $label ) {
$html .= sprintf( '<label for="%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key );
$html .= sprintf( '<input type="radio" class="radio" id="%1$s[%2$s][%3$s]" name="%5$s" value="%3$s" %4$s />', $args['section'], $args['id'], $key, checked( $args['value'], $key, false ), $args['name'] );
$html .= sprintf( '%1$s</label><br>', $label );
}
$html .= $this->get_field_description( $args );
$html .= '</fieldset>';
echo $html;
unset( $value, $html );
}
/**
* Displays a selectbox for a settings field
*
* @param array $args settings field args
*/
function callback_select( $args ) {
$html = sprintf( '<select class="%1$s-text %5$s" name="%4$s" id="%2$s[%3$s]">', $args['size'], $args['section'], $args['id'], $args['name'], $args['class'] );
foreach ( $args['options'] as $key => $label ) {
$html .=
sprintf( '<option value="%1s"%2s>%3s</option>',
$key,
selected( $args['value'], $key, false ),
$label
);
}
$html .= sprintf( '</select>' );
$html .= $this->get_field_description( $args );
echo $html;
unset( $html );
}
/**
* Displays a textarea for a settings field
*
* @param array $args settings field args
*/
function callback_textarea( $args ) {
$html = sprintf(
'<textarea
rows="5"
cols="55"
class="%1$s-text"
id="%2$s"
name="%5$s"
%3$s
>%4$s</textarea>',
$args['size'], $args['id'], $this->get_markup_placeholder( $args['placeholder'] ), $args['value'], $args['name'] );
$html .= $this->get_field_description( $args );
echo $html;
}