class-subscribe-and-connect-settings-api.php
20.5 KB
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
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Subscribe & Connect Settings API Class
*
* A settings API (wrapping the WordPress Settings API).
*
* @package WordPress
* @subpackage Subscribe_And_Connect
* @category Settings
* @author WooThemes
* @since 1.0.0
*
* TABLE OF CONTENTS
*
* public $token
* public $name
* private $_settings
* public $sections
* public $fields
* private $_errors
*
* private $_has_range
* private $_has_imageselector
*
* - __construct()
* - setup_settings()
* - init_sections()
* - init_fields()
* - create_sections()
* - create_fields()
* - determine_method()
* - parse_fields()
* - settings_screen()
* - get_settings()
* - settings_fields()
* - settings_errors()
* - settings_description()
* - form_field_text()
* - form_field_checkbox()
* - form_field_textarea()
* - form_field_select()
* - form_field_radio()
* - form_field_multicheck()
* - form_field_network()
* - form_field_info()
* - validate_fields()
* - validate_field_text()
* - validate_field_checkbox()
* - validate_field_multicheck()
* - validate_field_url()
* - check_field_text()
* - add_error()
* - parse_errors()
* - get_array_field_types()
*/
class Subscribe_And_Connect_Settings_API {
public $token;
public $name;
private $_settings;
public $sections;
public $fields;
private $_errors;
/**
* __construct function.
*
* @access public
* @return void
*/
public function __construct () {
$this->token = 'subscribe-and-connect';
$this->name = __( 'Subscribe & Connect', 'subscribe-and-connect' );
$this->sections = array();
$this->fields = array();
$this->remaining_fields = array();
$this->_errors = array();
add_action( 'admin_print_styles', array( $this, 'enqueue_styles' ) );
} // End __construct()
/**
* setup_settings function.
*
* @access public
* @return void
*/
public function setup_settings () {
$this->init_sections();
$this->init_fields();
$this->get_settings();
if ( is_admin() ) $this->settings_fields(); // Run this only in the admin.
} // End setup_settings()
/**
* init_sections function.
*
* @access public
* @return void
*/
public function init_sections () {
// Override this function in your class and assign the array of sections to $this->sections.
_e( 'Override init_sections() in your class.', 'subscribe-and-connect' );
} // End init_sections()
/**
* init_fields function.
*
* @access public
* @return void
*/
public function init_fields () {
// Override this function in your class and assign the array of sections to $this->fields.
_e( 'Override init_fields() in your class.', 'subscribe-and-connect' );
} // End init_fields()
/**
* create_sections function.
*
* @access public
* @return void
*/
public function create_sections () {
// if ( ! function_exists( 'add_settings_section' ) ) return;
if ( 0 < count( $this->sections ) ) {
foreach ( $this->sections as $k => $v ) {
add_settings_section( $k, $v['name'], array( $this, 'section_description' ), $this->token );
}
}
} // End create_sections()
/**
* create_fields function.
*
* @access public
* @return void
*/
public function create_fields () {
// if ( ! function_exists( 'add_settings_field' ) ) return;
if ( 0 < count( $this->sections ) ) {
foreach ( $this->fields as $k => $v ) {
$method = $this->determine_method( $v, 'form' );
$name = $v['name'];
if ( 'info' == $v['type']/* || 'network' == $v['type']*/ ) { $name = ''; }
add_settings_field( $k, $name, $method, $this->token, $v['section'], array( 'key' => $k, 'data' => $v ) );
}
}
} // End create_fields()
/**
* determine_method function.
*
* @access protected
* @param array $data
* @return array or string
*/
protected function determine_method ( $data, $type = 'form' ) {
$method = '';
if ( ! in_array( $type, array( 'form', 'validate', 'check' ) ) ) { return; }
// Check for custom functions.
if ( isset( $data[$type] ) ) {
if ( function_exists( $data[$type] ) ) {
$method = $data[$type];
}
if ( '' == $method && method_exists( $this, $data[$type] ) ) {
if ( $type == 'form' ) {
$method = array( $this, $data[$type] );
} else {
$method = $data[$type];
}
}
}
if ( '' == $method && method_exists ( $this, $type . '_field_' . $data['type'] ) ) {
if ( $type == 'form' ) {
$method = array( $this, $type . '_field_' . $data['type'] );
} else {
$method = $type . '_field_' . $data['type'];
}
}
if ( '' == $method ) {
if ( $type == 'form' ) {
$method = array( $this, $type . '_field_text' );
} else {
$method = $type . '_field_text';
}
}
return $method;
} // End determine_method()
/**
* settings_screen function.
*
* @access public
* @return void
*/
public function settings_screen () {
settings_fields( $this->token );
do_settings_sections( $this->token );
} // End settings_screen()
/**
* get_settings function.
*
* @access public
* @return void
*/
public function get_settings () {
if ( ! is_array( $this->_settings ) ) {
$this->_settings = get_option( $this->token, array() );
}
foreach ( $this->fields as $k => $v ) {
if ( ! isset( $this->_settings[$k] ) && isset( $v['default'] ) ) {
$this->_settings[$k] = $v['default'];
}
if ( $v['type'] == 'checkbox' && $this->_settings[$k] != true ) {
$this->_settings[$k] = 0;
}
}
return $this->_settings;
} // End get_settings()
/**
* settings_fields function.
*
* @access public
* @return void
*/
public function settings_fields () {
// if ( ! function_exists( 'register_setting' ) ) return;
$this->create_sections();
$this->create_fields();
register_setting( $this->token, $this->token, array( $this, 'validate_fields' ) );
} // End settings_fields()
/**
* settings_errors function.
*
* @access public
* @since 1.0.0
* @return void
*/
public function settings_errors () {
echo settings_errors( $this->token . '-errors' );
} // End settings_errors()
/**
* section_description function.
*
* @access public
* @return void
*/
public function section_description ( $section ) {
if ( isset( $this->sections[$section['id']]['description'] ) ) {
echo wpautop( esc_html( $this->sections[$section['id']]['description'] ) );
}
} // End section_description_main()
/**
* form_field_text function.
*
* @access public
* @since 1.0.0
* @param array $args
* @return void
*/
public function form_field_text ( $args ) {
do_action( 'subscribe_and_connect_field_text_before', $args );
$options = $this->get_settings();
echo '<input id="' . esc_attr( $args['key'] ) . '" name="' . $this->token . '[' . esc_attr( $args['key'] ) . ']" size="40" type="text" value="' . esc_attr( $options[$args['key']] ) . '" />' . "\n";
if ( isset( $args['data']['description'] ) ) {
echo '<p class="description">' . wp_kses_post( $args['data']['description'] ) . '</p>' . "\n";
}
do_action( 'subscribe_and_connect_field_text_after', $args );
} // End form_field_text()
/**
* A hidden input field.
*
* @access public
* @since 1.0.0
* @param array $args
* @return void
*/
public function form_field_hidden ( $args ) {
do_action( 'subscribe_and_connect_field_hidden_before', $args );
$options = $this->get_settings();
echo '<input id="' . esc_attr( $args['key'] ) . '" name="' . $this->token . '[' . esc_attr( $args['key'] ) . ']" size="40" type="hidden" value="' . esc_attr( $options[$args['key']] ) . '" />' . "\n";
do_action( 'subscribe_and_connect_field_hidden_after', $args );
} // End form_field_hidden()
/**
* form_field_checkbox function.
*
* @access public
* @since 1.0.0
* @param array $args
* @return void
*/
public function form_field_checkbox ( $args ) {
do_action( 'subscribe_and_connect_field_checkbox_before', $args );
$options = $this->get_settings();
$has_description = false;
if ( isset( $args['data']['description'] ) ) {
$has_description = true;
echo '<label for="' . $this->token . '[' . esc_attr( $args['key'] ) . ']">' . "\n";
}
echo '<input id="' . $args['key'] . '" name="' . $this->token . '[' . esc_attr( $args['key'] ) . ']" type="checkbox" value="1"' . checked( esc_attr( $options[$args['key']] ), '1', false ) . ' />' . "\n";
if ( $has_description ) {
echo wp_kses_post( $args['data']['description'] ) . '</label>' . "\n";
}
do_action( 'subscribe_and_connect_field_checkbox_after', $args );
} // End form_field_text()
/**
* form_field_textarea function.
*
* @access public
* @since 1.0.0
* @param array $args
* @return void
*/
public function form_field_textarea ( $args ) {
do_action( 'subscribe_and_connect_field_textarea_before', $args );
$options = $this->get_settings();
echo '<textarea id="' . esc_attr( $args['key'] ) . '" name="' . $this->token . '[' . esc_attr( $args['key'] ) . ']" cols="42" rows="5">' . esc_html( $options[$args['key']] ) . '</textarea>' . "\n";
if ( isset( $args['data']['description'] ) ) {
echo '<p class="description">' . wp_kses_post( $args['data']['description'] ) . '</p>' . "\n";
}
do_action( 'subscribe_and_connect_field_textarea_after', $args );
} // End form_field_textarea()
/**
* form_field_select function.
*
* @access public
* @since 1.0.0
* @param array $args
* @return void
*/
public function form_field_select ( $args ) {
do_action( 'subscribe_and_connect_field_select_before', $args );
$options = $this->get_settings();
if ( isset( $args['data']['options'] ) && ( count( (array)$args['data']['options'] ) > 0 ) ) {
$html = '';
$html .= '<select id="' . esc_attr( $args['key'] ) . '" name="' . esc_attr( $this->token ) . '[' . esc_attr( $args['key'] ) . ']">' . "\n";
foreach ( $args['data']['options'] as $k => $v ) {
$html .= '<option value="' . esc_attr( $k ) . '"' . selected( esc_attr( $options[$args['key']] ), $k, false ) . '>' . $v . '</option>' . "\n";
}
$html .= '</select>' . "\n";
echo $html;
if ( isset( $args['data']['description'] ) ) {
echo '<p class="description">' . wp_kses_post( $args['data']['description'] ) . '</p>' . "\n";
}
}
do_action( 'subscribe_and_connect_field_select_after', $args );
} // End form_field_select()
/**
* form_field_radio function.
*
* @access public
* @since 1.0.0
* @param array $args
* @return void
*/
public function form_field_radio ( $args ) {
do_action( 'subscribe_and_connect_field_radio_before', $args );
$options = $this->get_settings();
if ( isset( $args['data']['options'] ) && ( count( (array)$args['data']['options'] ) > 0 ) ) {
$html = '';
foreach ( $args['data']['options'] as $k => $v ) {
$html .= '<p><label><input type="radio" name="' . $this->token . '[' . esc_attr( $args['key'] ) . ']" value="' . esc_attr( $k ) . '"' . checked( esc_attr( $options[$args['key']] ), $k, false ) . ' /> ' . $v . '<br />' . "\n";
}
echo $html;
if ( isset( $args['data']['description'] ) ) {
echo '<span class="description">' . wp_kses_post( $args['data']['description'] ) . '</span></label></p>' . "\n";
}
}
do_action( 'subscribe_and_connect_field_radio_after', $args );
} // End form_field_radio()
/**
* form_field_multicheck function.
*
* @access public
* @since 1.0.0
* @param array $args
* @return void
*/
public function form_field_multicheck ( $args ) {
do_action( 'subscribe_and_connect_field_multicheck_before', $args );
$options = $this->get_settings();
if ( isset( $args['data']['options'] ) && ( count( (array)$args['data']['options'] ) > 0 ) ) {
$html = '<div class="multicheck-container" style="height: 100px; overflow-y: auto;">' . "\n";
foreach ( $args['data']['options'] as $k => $v ) {
$checked = '';
if ( in_array( $k, (array)$options[$args['key']] ) ) { $checked = ' checked="checked"'; }
$html .= '<input type="checkbox" name="' . esc_attr( $this->token ) . '[' . esc_attr( $args['key'] ) . '][]" class="multicheck multicheck-' . esc_attr( $args['key'] ) . '" value="' . esc_attr( $k ) . '"' . $checked . ' /> ' . $v . '<br />' . "\n";
}
$html .= '</div>' . "\n";
echo $html;
if ( isset( $args['data']['description'] ) ) {
echo '<p class="description">' . wp_kses_post( $args['data']['description'] ) . '</p>' . "\n";
}
}
do_action( 'subscribe_and_connect_field_multicheck_after', $args );
} // End form_field_multicheck()
/**
* form_field_network function.
*
* @access public
* @since 1.0.0
* @param array $args
* @return void
*/
public function form_field_network ( $args ) {
$options = $this->get_settings();
$networks = Subscribe_And_Connect_Utils::get_supported_networks();
$html = '';
$order = '';
if ( isset( $options['networks_order'] ) ) {
$order = $options['networks_order'];
}
$networks = Subscribe_And_Connect_Utils::get_networks_in_order( $networks, $order );
if ( 0 < count( $networks ) ) {
$html .= '<table class="form-table widefat subscribe-and-connect-network-fields"><thead><tr><th class="title">' . __( 'Social Network', 'woothemes-wc' ) . '</th><th class="title">' . __( 'Your URL', 'woothemes-wc' ) . '</th></tr></thead><tbody>' . "\n";
foreach ( $networks as $k => $v ) {
$html .= $this->_single_network_field( $k, $v, $args, $options );
}
$html .= '</tbody></table>' . "\n";
}
// Used to store the placeholder image temporarily, for use with JavaScript.
$html .= '<img src="' . esc_url( Subscribe_And_Connect_Utils::get_placeholder_image() ) . '" class="subscribe-and-connect-placeholder-image" style="display: none;" width="0" height="0" />' . "\n";
echo $html;
if ( isset( $args['data']['description'] ) ) {
echo '<p class="description">' . wp_kses_post( $args['data']['description'] ) . '</p>' . "\n";
}
} // End form_field_network()
/**
* Output a single instance of a "network" field type.
* @access private
* @since 1.0.0
* @param array $data Data for the network to be output.
* @param array $args Arguments used when generating the field instance.
* @param array $networks Supported networks to list.
* @param int $i Itterator.
* @param bool $show_remove Whether or not to show the "Remove" link.
* @return string Formatted HTML.
*/
private function _single_network_field ( $key, $value, $args, $options ) {
$html = '';
$data = array( 'url' => '', 'image_id' => '' );
if ( isset( $options[$args['key']][$key] ) ) $data = $options[$args['key']][$key];
$html .= '<tr id="' . esc_attr( $key ) . '" class="subscribe-and-connect-network-item">' . "\n";
$html .= '<th class="title">' . "\n";
$html .= '<label for="' . esc_attr( $this->token ) . '[' . esc_attr( $args['key'] ) . '][' . esc_attr( $key ) . '][url]">' . esc_html( $value ) . '</label>' . "\n";
$html .= '</th><td class="url">' . "\n";
$html .= '<input type="text" class="regular-text input-text url" name="' . esc_attr( $this->token ) . '[' . esc_attr( $args['key'] ) . '][' . esc_attr( $key ) . '][url]" placeholder="' . sprintf( __( 'Place your %s URL here', 'subscribe-and-connect' ), esc_attr( $value ) ) . '" value="' . esc_attr( $data['url'] ) . '" />' . "\n";
$html .= '</td></tr>' . "\n";
return $html;
} // End _single_network_field()
/**
* form_field_info function.
*
* @access public
* @since 1.0.0
* @param array $args
* @return void
*/
public function form_field_info ( $args ) {
$class = '';
if ( isset( $args['data']['class'] ) ) {
$class = ' ' . esc_attr( $args['data']['class'] );
}
$html = '<div id="' . $args['key'] . '" class="subscribe-and-connect-info-box' . $class . '">' . "\n";
if ( isset( $args['data']['name'] ) && ( $args['data']['name'] != '' ) ) {
$html .= '<h3 class="title">' . esc_html( $args['data']['name'] ) . '</h3>' . "\n";
}
if ( isset( $args['data']['description'] ) && ( $args['data']['description'] != '' ) ) {
$html .= '<p>' . wp_kses_post( $args['data']['description'] ) . '</p>' . "\n";
}
$html .= '</div>' . "\n";
echo $html;
} // End form_field_info()
/**
* validate_fields function.
*
* @access public
* @since 1.0.0
* @param array $input
* @uses $this->parse_errors()
* @return array $options
*/
public function validate_fields ( $input ) {
$options = $this->get_settings();
foreach ( $this->fields as $k => $v ) {
// Make sure checkboxes are present even when false.
if ( $v['type'] == 'checkbox' && ! isset( $input[$k] ) ) { $input[$k] = false; }
if ( isset( $input[$k] ) ) {
// Perform checks on required fields.
if ( isset( $v['required'] ) && ( true == $v['required'] ) ) {
if ( in_array( $v['type'], $this->get_array_field_types() ) && ( count( (array) $input[$k] ) <= 0 ) ) {
$this->add_error( $k, $v );
continue;
} else {
if ( $input[$k] == '' ) {
$this->add_error( $k, $v );
continue;
}
}
}
$value = $input[$k];
// Check if the field is valid.
$method = $this->determine_method( $v, 'check' );
if ( method_exists( $this, $method ) ) {
$is_valid = $this->$method( $value );
}
if ( ! $is_valid ) {
$this->add_error( $k, $v );
continue;
}
$method = $this->determine_method( $v, 'validate' );
if ( method_exists( $this, $method ) ) {
$options[$k] = $this->$method( $value );
}
}
}
// Parse error messages into the Settings API.
$this->parse_errors();
return $options;
} // End validate_fields()
/**
* validate_field_text function.
*
* @access public
* @since 1.0.0
* @param string $input
* @return string
*/
public function validate_field_text ( $input ) {
return trim( esc_attr( $input ) );
} // End validate_field_text()
/**
* validate_field_checkbox function.
*
* @access public
* @since 1.0.0
* @param string $input
* @return string
*/
public function validate_field_checkbox ( $input ) {
if ( ! isset( $input ) ) {
return 0;
} else {
return (bool)$input;
}
} // End validate_field_checkbox()
/**
* validate_field_multicheck function.
*
* @access public
* @since 1.0.0
* @param string $input
* @return string
*/
public function validate_field_multicheck ( $input ) {
$input = (array) $input;
$input = array_map( 'esc_attr', $input );
return $input;
} // End validate_field_multicheck()
/**
* validate_field_url function.
*
* @access public
* @since 1.0.0
* @param string $input
* @return string
*/
public function validate_field_url ( $input ) {
return trim( esc_url( $input ) );
} // End validate_field_url()
/**
* validate_field_network function.
*
* @access public
* @since 1.0.0
* @param string $input
* @return string
*/
public function validate_field_network ( $input ) {
$input = (array) $input;
// TODO
return $input;
} // End validate_field_network()
/**
* check_field_text function.
* @param string $input String of the value to be validated.
* @since 1.1.0
* @return boolean Is the value valid?
*/
public function check_field_text ( $input ) {
$is_valid = true;
return $is_valid;
} // End check_field_text()
/**
* add_error function.
*
* @access protected
* @since 1.0.0
* @param string $key
* @param array $data
* @return void
*/
protected function add_error ( $key, $data ) {
if ( isset( $data['error_message'] ) ) {
$message = $data['error_message'];
} else {
$message = sprintf( __( '%s is a required field', 'subscribe-and-connect' ), $data['name'] );
}
$this->_errors[$key] = $message;
} // End add_error()
/**
* If there are error messages logged, parse them.
* @access protected
* @since 1.0.0
* @return void
*/
protected function parse_errors () {
if ( count ( $this->_errors ) > 0 ) {
foreach ( $this->_errors as $k => $v ) {
add_settings_error( $this->token . '-errors', $k, $v, 'error' );
}
} else {
$message = sprintf( __( '%s settings updated', 'subscribe-and-connect' ), $this->name );
add_settings_error( $this->token . '-errors', $this->token, $message, 'updated' );
}
} // End parse_errors()
/**
* get_array_field_types function.
*
* @description Return an array of field types expecting an array value returned.
* @access protected
* @since 1.0.0
* @return void
*/
protected function get_array_field_types () {
return array( 'multicheck', 'network' );
} // End get_array_field_types()
/**
* Load in CSS styles where necessary.
* @access public
* @since 1.0.0
* @return void
*/
public function enqueue_styles () {
global $subscribe_and_connect;
wp_enqueue_style( $this->token . '-admin' );
wp_enqueue_style( 'subscribe-and-connect-settings-api', $subscribe_and_connect->context->__get( 'plugin_url' ) . 'assets/css/settings.css', '', '1.0.0' );
} // End enqueue_styles()
} // End Class
?>