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
<?php
/**
* Class for registering settings and sections and for display of the settings form(s).
* For detailed instructions see: https://github.com/keesiemeijer/WP-Settings
*
* @version 2.0
*
* @author keesiemeijer
*/
if ( !class_exists( 'WC_Quick_Donation_Settings' ) ) {
class WC_Quick_Donation_Settings {
/**
* Version of WP_Settings_Settings class
*
* @since 2.0
* @var
*/
public $version = 2.0;
/**
* Current settings page.
*
* @since 2.0
* @var array
*/
public $current_page = array();
/**
* Debug errors and notices.
*
* @since 2.0
* @var string
*/
public $debug = '';
/**
* Admin pages.
*
* @since 2.0
* @var array
*/
private $pages = array();
/**
* Admin pages.
*
* @since 2.0
* @var array
*/
private $fields;
/**
* Unique plugin admin page hook suffix.
*
* @since 2.0
* @var array
*/
private $page_hook;
/**
* Fields that need Javascript. (e.g. colorpicker)
*
* @since 2.0
* @var array
*/
private $script_types;
/**
* Fields that need the label_argument in add_settings_field()
*
* @since 2.0
* @var array
*/
private $label_for = array( 'text', 'select', 'textarea' );
/**
* array of Javascrips needed for the current settings page
*
* @since 2.0
* @var array
*/
private $load_scripts = array();
/**
* Multiple forms on one settings page.
*
* @since 2.0
* @var bool
*/
private $multiple_forms = false;
/**
* valid admin pages and fields arrays.
*
* @since 2.0
* @var bool
*/
private $valid_pages = false;
/**
* Registers settings using the WorPres settings Api.
*
* @uses WP_Settings_Settings_Fields class
* @since 2.0 *
* @param array $pages Array with admin pages.
* @param string $page_hook Unique plugin admin page hook suffix.
*/
public function init( $pages, $page_hook = '' ) {
$this->pages = (array) $pages;
$this->page_hook = trim( sanitize_title( (string) $page_hook ) );
// Debug strings don't use Gettext functions for translation.
if ( !class_exists( 'WC_Quick_Donation_Fields' ) ) {
$this->debug .= "Error: class WC_Quick_Donation_Fields doesn't exist<br/>";
}
if ( '' === $this->page_hook ) {
$this->debug .= "Error: parameter 'page_hook' not provided in settings_admin_init()<br/>";
}
// Let external scripts do debugging .
$this->debug .= apply_filters( "{$this->page_hook}_debug", $this->debug, $this->pages );
if ( $this->debug ) {
return $this->valid_pages = false; // Don't display the form and navigation.
}
// Passed validation (required to show form and navigation).
$this->valid_pages = true;
$this->current_page = $this->get_current_admin_page();
if ( isset( $this->current_page['multiform'] ) && $this->current_page['multiform'] ) {
$this->multiple_forms = ( count( $this->current_page['sections'] ) > 1 ) ? true : false;
}
// Instanciate the form fields.
$this->fields = new WC_Quick_Donation_Fields( get_settings_errors() );
// Array of fields that needs the 'label_for' parameter (add_settings_field()).
$this->label_for = apply_filters( "{$this->page_hook}_label_for", $this->label_for );
// Array of fields that needs javascript (e.g. 'color_picker').
$this->script_types = apply_filters( "{$this->page_hook}_script_field_types", array() );
$this->add_settings_sections();
// Register all the settings.
$this->register_settings();
// only load javascript if it's needed for the current admin page
if ( !empty( $this->load_scripts ) ) {
$this->load_scripts = array_unique( $this->load_scripts );
add_action( 'admin_print_scripts-' . $this->page_hook, array( $this, 'enqueue_scripts' ) );
}
} // admin_init()
/**
* Adds setting sections
*
* @since 2.0
* @return void
*/
private function add_settings_sections() {
foreach ( $this->current_page['sections'] as $section ) {
$section_description = '__return_false';
if ( isset( $section['desc'] ) && $section['desc'] ) {
$section_description = array( $this, 'render_section_description' );
}
$title = ( isset( $section['title'] ) ) ? $section['title'] : ''; // optional
// Add page hook to sections and page ids.
$page_id = ( $this->multiple_forms ) ? $section['id'] : $this->current_page['id'];
$page_id = $this->page_hook . '_' . $page_id;
$section_id = $this->page_hook . '_' . $section['id'];
// Add database option(s) to debug messages.
$this->debug .= ( '' === $this->debug ) ? 'Database option(s) created for this page:<br/>' : '';
$this->debug .= "database option: " . $section_id . '<br/>'; // database option name
add_settings_section( $section_id, $title, $section_description, $page_id );
if ( isset( $section['fields'] ) && !empty( $section['fields'] ) ) {
// Add fields to sections.
$this->add_settings_fields( $section_id, $section['fields'], $page_id );
}
}
}
/**
* Adds all fields to a settings section.
*
* @since 2.0
* @param string $sections_id ID of section to add fields to.
* @param array $fields Array with section fields
* @param string $page_id Page id.
* @param bool $use_defaults Use default values for the settings fields.
*/
private function add_settings_fields( $sections_id, $fields, $page_id ) {
$opt_defaults = array();
$defaults = array(
'section' => $sections_id,
'id' => '',
'type' => '',
'label' => '',
'desc' => '',
'size' => false,
'options' => '',
'default' => '',
'content' => '',
'attr' => false,
'before' => '',
'after' => '',
'_type' => '',
);
// Check if database option exist (use defaults if it doesn't).
$use_defaults = ( false === get_option( $sections_id ) ) ? true : false;
foreach ( $fields as $field ) {
// Field (rows) can be added by external scripts.
$multiple = ( isset( $field['fields'] ) && $field['fields'] ) ? true : false;
$options = ( $multiple ) ? (array) $field['fields'] : array( $field );
foreach ( $options as $key => $opt ) {
$args = wp_parse_args( $opt, $defaults );
$args['default'] = ( $use_defaults ) ? $args['default'] : '';
$opt_defaults[ $opt['id'] ] = $args['default'];
if ( in_array( $args['type'], $this->script_types ) ) {
$this->load_scripts[] = $args['type']; // field needs javascript
}
if ( in_array( $args['type'] , $this->label_for ) ) {
$args['label_for'] = $sections_id . '_' . $args['id'];
}
if ( $multiple ) {
$field['fields'][$key] = $args;
}
}
if ( $multiple ) {
$args = $field;
}
// ability to add fields with an action hook
if ( !method_exists( $this->fields, 'callback_' . $field['type'] ) ) {
$args['callback'] = $field['type'];
$args['page_hook'] = $this->page_hook;
$field['type'] = 'extra_field';
}
if ( method_exists( $this->fields, 'callback_' . $field['type'] ) ) {
add_settings_field(
$sections_id . '[' . $field['id'] . ']',
isset( $args['label'] ) ? $args['label'] : '',
array( $this->fields, 'callback_' . $field['type'] ),
$page_id,
$sections_id,
$args
);
}
}
// add the option or validation errors show twice on the first submit (todo: Why?).
if ( $use_defaults ) {
add_option( $sections_id, $opt_defaults );
}
}
/**
* Registers settings
*
* @since 2.0
* @return void
*/
private function register_settings() {
foreach ( $this->pages as $page ) {
foreach ( $page['sections'] as $section ) {
// Use section ids for multiple forms.
if ( isset( $page['multiform'] ) && $page['multiform'] ) {
$page['id'] = ( count( $page['sections'] ) > 1 ) ? $section['id'] : $page['id'];
}
$page_id = $this->page_hook . '_' . $page['id'];
$sections_id = $this->page_hook . '_' . $section['id'];
if ( isset( $section['validate_callback'] ) && $section['validate_callback'] ) {
register_setting( $page_id, $sections_id, $section['validate_callback'] );
} else {
register_setting( $page_id, $sections_id );
}
}
}
}
/**
* Gets all settings from all sections
*
* @since 2.0
* @return array Array with settings.
*/
public function get_settings( $section = '' ) {
$settings = array();
if ( !empty( $section ) ) {
return get_option( $this->page_hook . '_' . $section );
}
foreach ( (array) $this->pages as $page ) {
if ( !isset( $page['sections'] ) ) {
continue;
}
foreach ( $page['sections'] as $section ) {
if ( !isset( $section['id'] ) ) {
continue;
}
$option = get_option( $this->page_hook . '_' . $section['id'] );
if ( $option ) {
unset( $option['section_id'] );
$settings[ $section['id'] ] = $option;
}
}
}
return $settings;
}
/**
* Returns the current settings page.
*
* @since 2.0
* @param array $admin_pages. Array of settings pages.
* @return array Current settings page.
*/
public function get_current_admin_page() {
foreach ( (array) $this->pages as $page ) {
if ( isset( $_GET['tab'] ) && $_GET['tab'] ) {
if ( ( $_GET['tab'] === $page['id'] ) || ( $_GET['tab'] === $page['slug'] ) )
$current_page = $page;
}
}
// Set the first settings page as current if it's not a tab.
if ( empty( $current_page ) ) {
$current_page = $this->pages[0];
}
return $current_page;
}
/**
* Adds a admin page.
*
* @since 2.0
* @param array $page Page array.
* @return array Admin pages array with the page added.
*/
public function add_page( $page ) {
return $this->pages[] = $page;
}
/**
* Adds multiple admin pages.
*
* @since 2.0
* @param array $pages Array with pages.
* @return array Admin pages array with the pages added.
*/
public function add_pages( $pages ) {
foreach ( $pages as $page ) {
$this->add_page( $page );
}
return $this->pages;
}
/**
* Adds a section to an admin page.
*
* @since 2.0
* @param string $page Page id.
* @param array $section Section array.
* @return array Admin pages array with the section added.
*/
public function add_section( $page, $section ) {
foreach ( $this->pages as $key => $_page ) {
if ( $page !== $_page['id'] ) {
continue;
}
if ( isset( $this->pages[ $key ][ $page ]['sections'] ) ) {
$this->pages[ $key ]['sections'] = array();
}
$this->pages[ $key ]['sections'][] = $section;
}
return $this->pages;
}
/**
* Adds multiple sections to an admin page.
*
* @since 2.0
* @param array $string Page id
* @param array $sections Array with sections.
* @return array Admin pages array with the sections added.
*/
public function add_sections( $page, $sections ) {
foreach ( $sections as $section ) {
$this->pages = $this->add_section( $page, $section );
}
return $this->pages;
}
/**
* Adds a form field to a section.
*
* @since 2.0
* @param string $page Page id.
* @param string $section Section id.
* @param array $field Field array.
* @return array Admin pages array with the field added.
*/
public function add_field( $page, $section, $field ) {
foreach ( $this->pages as $key => $_page ) {
if ( $page !== $_page['id'] ) {
continue;
}
if ( !isset( $this->pages[ $key ]['sections'] ) ) {
continue;
}
$_sections = $this->pages[ $key ]['sections'];
foreach ( $_sections as $_key => $_section ) {
if ( $section !== $_section['id'] ) {
continue;
}
if ( !isset( $this->pages[ $key ]['sections'][ $_key ]['fields'] ) ) {
$this->pages[ $key ]['sections'][ $_key ]['fields'] = array();
}
$this->pages[ $key ]['sections'][ $_key ]['fields'][] = $field;
}
}
return $this->pages;
}
/**
* Adds multiple form fields to a section.
*
* @since 2.0
* @param string $page Page id.
* @param string $section Section id.
* @param array $fields Array with fields.
* @return array Admin pages array with the fields added.
*/
public function add_fields( $page, $section, $fields ) {
foreach ( $fields as $field ) {
$this->pages = $this->add_field( $page, $section, $field );
}
return $this->pages;
}
/**
* Enqueue javascript for fields that need them.
*
* @since 2.0
* @return void
*/
public function enqueue_scripts() {
$screen = get_current_screen();
// Only load scripts on plugin pages.
if ( $screen->id === $this->page_hook ) {
do_action( "{$this->page_hook}_scripts", $this->load_scripts );
}
}
/**
* Display the description of a section.
*
* @since 2.0
* @param array $section Description of section.
* @return void
*/
public function render_section_description( $section ) {
foreach ( $this->current_page['sections'] as $setting ) {
if ( $this->page_hook . '_' . $setting['id'] === $section['id'] )
echo $setting['desc'];
}
}
/**
* Display Plugin Title and if needed tabbed navigation.
*
* @since 2.0
* @param string $plugin_title Plugin title.
* @param string $tab_id Page id. Manually set the active tab.
* @return void
*/
public function render_header( $plugin_title = '', $tab_id = false ) {
if ( !empty( $plugin_title ) )
echo get_screen_icon() . '<h2>' . (string) $plugin_title . '</h2>';
// if ( !$this->valid_pages )
// return;
$page_title_count = 0;
foreach ( $this->pages as $page ) {
if ( isset( $page['title'] ) && $page['title'] )
++$page_title_count;
}
$html = '';
$current = $this->current_page;
$page_ids = wp_list_pluck( $this->pages, 'id' );
$cur_tab_id = ( $tab_id ) ? (string) $tab_id : $current['id'];
$cur_tab_id = ( in_array( $cur_tab_id, $page_ids ) ) ? $cur_tab_id : $current['id'];
$i = 0;
foreach ( $this->pages as $page ) {
if ( ( isset( $page['title'] ) && $page['title'] ) ) {
if ( $page_title_count > 1 ) {
$html .= ( 0 === $i ) ? '<h2 class="nav-tab-wrapper">' : '';
$active = '';
if ( $cur_tab_id === $page['id'] )
$active = ' nav-tab-active';
// Get the url of the current settings page.
$tab_url = remove_query_arg( array( 'tab', 'settings-updated' ) );
// Add query arg 'tab' if it's not the first settings page.
if ( $this->pages[0]['id'] !== $page['id'] )
$tab_url = add_query_arg( 'tab', $page['slug'], $tab_url );
$html .= sprintf(
'<a href="%1$s" class="nav-tab%2$s" id="%3$s-tab">%4$s</a>',
esc_url( $tab_url ),
$active,
esc_attr( $page['id'] ),
$page['title']
);
$html .= ( ++$i === $page_title_count ) ? '</h3>' : '';
}
if ( $page_title_count === 1 ) {
if ( isset( $current['title'] ) && $current['title'] === $page['title'] ) {
$html .= '<h3>' . $page['title'] . '</h3>';
break;
}
}
}
}
echo $html;
} // render_header()
/**
* Displays the form(s) and sections.
*
* @since 2.0
* @return void
*/
public function render_form() {
if ( !$this->valid_pages ) {
return;
}
$page = $this->current_page;
if ( !empty( $page ) ) {
$ids = wp_list_pluck( $page['sections'], 'id' );
$forms = ( $this->multiple_forms ) ? $page['sections'] : array( $page );
// Section ids hidden inputs.
$section_ids = '';
foreach ( $ids as $id ) {
$section_ids .= "<input id='{$this->page_hook}_{$id}_section_id' type='hidden' value='{$id}' name='{$this->page_hook}_{$id}[section_id]'>";
}
foreach ( $forms as $form ) {
echo '<form method="post" action="options.php">';
// print hidden section_id fields
echo $section_ids;
// lets you add additional fields
echo apply_filters( "{$this->page_hook}_form_fields", '', $form['id'], $form );
settings_fields( $this->page_hook . '_' . $form['id'] );
do_settings_sections( $this->page_hook . '_' . $form['id'] );
$submit = ( isset( $form['submit'] ) && $form['submit'] ) ? $form['submit'] : '';
if ( ( '' === $submit ) && isset( $page['submit'] ) && $page['submit'] ) {
$submit = $page['submit'];
}
$text = isset( $submit['text'] ) ? $submit['text'] : null;
$type = isset( $submit['$type'] ) ? $submit['text'] : 'primary';
$name = isset( $submit['$name'] ) ? $submit['name'] : 'submit';
$other_attributes = array( 'id' => $form['id'] );
submit_button( $text, $type, $name, true, $other_attributes );
echo '</form>';
}
}
} // render_form()
} // class
} // class exists