-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathform-fields.php
More file actions
601 lines (493 loc) · 11.8 KB
/
form-fields.php
File metadata and controls
601 lines (493 loc) · 11.8 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
<?php
/**
* Form Fields
*
* A prototype for the new "Form Fields" plugin, a standalone plugin and
* extension for the upcoming "Settings" plugin, a rewrite of KC Settings.
*
* @author Dzikri Aziz <kvcrvt@gmail.com>
*/
/**
* Form Fields
*/
abstract class Kucrut_Form_Field {
/**
* Holds field & argument defaults
*
* @since 0.1.0
* @var array
* @access protected
*/
protected static $defaults = array(
'field' => array(
'id' => '',
'type' => 'text',
'value' => null,
'default' => null,
'attributes' => array(),
'description' => '',
'choices' => array(),
),
'args' => array(
'keys' => array(),
'inline_description' => false,
),
);
/**
* Holds field attributes
*
* @since 0.1.0
* @var array
* @access protected
*/
protected static $types = array(
'text' => 'Kucrut_Form_Field_Text',
'number' => 'Kucrut_Form_Field_Text',
'url' => 'Kucrut_Form_Field_Text',
'color' => 'Kucrut_Form_Field_Text',
'date' => 'Kucrut_Form_Field_Text',
'hidden' => 'Kucrut_Form_Field_Text',
'checkbox' => 'Kucrut_Form_Field_Checkbox',
'radio' => 'Kucrut_Form_Field_Radio',
'textarea' => 'Kucrut_Form_Field_Textarea',
'select' => 'Kucrut_Form_Field_Select',
'select_multiple' => 'Kucrut_Form_Field_Select_Multiple',
'select_pages' => 'Kucrut_Form_Field_Select_Pages',
'special' => 'Kucrut_Form_Field_Special',
);
/**
* Holds forbidden attributes
*
* @since 0.1.0
* @var array
* @access protected
*/
protected static $forbidden_attributes = array(
'id',
'name',
'value',
'checked',
'multiple',
);
/**
* URL path to this directory
*
* @since 0.1.0
* @var string
* @access protected
*/
protected static $url_path;
/**
* Holds allowed html tags
*
* @since 0.1.0
* @var array
* @access protected
*/
protected $allowed_html = array(
'a' => array(
'href' => true,
'target' => true,
'title' => true,
),
'code' => true,
'em' => true,
'p' => array( 'class' => true ),
'span' => array( 'class' => true ),
'strong' => true,
);
/**
* Holds constructed field
*
* @since 0.1.0
* @var array
* @access protected
*/
protected $field;
/**
* Holds field attributes
*
* @since 0.1.0
* @var array
* @access protected
*/
protected $attributes = array();
/**
* Holds field arguments
*
* @since 0.1.0
* @var stdClass
* @access protected
*/
protected $args;
/**
* Loader
*
* @param string URL path to this directory
*/
final public static function load( $url_path = null ) {
// Set URL path for assets
if ( ! is_null( $url_path ) ) {
self::$url_path = $url_path;
} else {
self::$url_path = plugin_dir_url( __FILE__ );
}
// Supported field types
self::$types = apply_filters(
'form_field_types',
self::$types
);
}
/**
* Create field
*
* @param array $field Field array
* @param array $args Extra field arguments
*/
final public static function create( array $field, $args = array() ) {
$field = wp_parse_args( $field, self::$defaults['field'] );
if ( ! isset( self::$types[ $field['type'] ] )
|| ! is_subclass_of( self::$types[ $field['type'] ], __CLASS__ )
) {
trigger_error(
sprintf(
// translators: %1$s - the name of the class, %2$s - the type of the field.
esc_html__( '%1$s: Type %2$s is not supported, reverting to text.', 'menu-icons' ),
__CLASS__,
esc_html( $field['type'] )
),
E_USER_WARNING
);
$field['type'] = 'text';
}
if ( is_null( $field['value'] ) && ! is_null( $field['default'] ) ) {
$field['value'] = $field['default'];
}
foreach ( self::$forbidden_attributes as $key ) {
unset( $field['attributes'][ $key ] );
}
$args = (object) wp_parse_args( $args, self::$defaults['args'] );
$class = self::$types[ $field['type'] ];
return new $class( $field, $args );
}
/**
* Constructor
*
* @since 0.1.0
* @param array $field Field array
* @param object $args Extra field arguments
*/
public function __construct( $field, $args ) {
$this->field = $field;
$this->args = $args;
if ( ! is_array( $this->args->keys ) ) {
$this->args->keys = array();
}
$this->args->keys[] = $field['id'];
$this->attributes['id'] = $this->create_id();
$this->attributes['name'] = $this->create_name();
$this->attributes = wp_parse_args(
$this->attributes,
(array) $field['attributes']
);
$this->set_properties();
}
/**
* Attribute
*
* @since 0.1.0
* @param string $key Attribute key
* @return mixed NULL if attribute doesn't exist
*/
public function __get( $key ) {
foreach ( array( 'attributes', 'field' ) as $group ) {
if ( isset( $this->{$group}[ $key ] ) ) {
return $this->{$group}[ $key ];
}
}
return null;
}
/**
* Create id/name attribute
*
* @since 0.1.0
* @param string $format Attribute format
*/
protected function create_id_name( $format ) {
return call_user_func_array(
'sprintf',
array_merge(
array( $format ),
$this->args->keys
)
);
}
/**
* Create id attribute
*
* @since 0.1.0
* @access protected
* @return string
*/
protected function create_id() {
$format = implode( '-', $this->args->keys );
return $this->create_id_name( $format );
}
/**
* Create name attribute
*
* @since 0.1.0
* @access protected
* @return string
*/
protected function create_name() {
$format = '%s';
$format .= str_repeat( '[%s]', ( count( $this->args->keys ) - 1 ) );
return $this->create_id_name( $format );
}
/**
* Set field properties
*
* @since 0.1.0
*/
protected function set_properties() {}
/**
* Build field attributes
*
* @since 0.1.0
* @param array $excludes Attributes to be excluded
* @return string
*/
protected function build_attributes( $excludes = array() ) {
$excludes = array_filter( (array) $excludes );
$attributes = '';
foreach ( $this->attributes as $key => $value ) {
if ( in_array( $key, $excludes, true ) ) {
continue;
}
if ( 'class' === $key ) {
$value = implode( ' ', (array) $value );
}
$attributes .= sprintf(
' %s="%s"',
esc_attr( $key ),
esc_attr( $value )
);
}
return $attributes;
}
/**
* Print field
*
* @since 0.1.0
*/
abstract public function render();
/**
* Print field description
*
* @since 0.1.0
*/
public function description() {
if ( ! empty( $this->field['description'] ) ) {
$tag = ( ! empty( $this->args->inline_description ) ) ? 'span' : 'p';
printf( // WPCS: XSS ok.
'<%1$s class="description">%2$s</%1$s>',
$tag,
wp_kses( $this->field['description'], $this->allowed_html )
);
}
}
}
/**
* Field: text
*/
class Kucrut_Form_Field_Text extends Kucrut_Form_Field {
protected $template = '<input type="%s" value="%s"%s />';
protected function set_properties() {
if ( ! is_string( $this->field['value'] ) ) {
$this->field['value'] = '';
}
if ( in_array( $this->field['type'], array( 'text', 'url' ), true ) ) {
if ( ! isset( $this->attributes['class'] ) ) {
$this->attributes['class'] = array();
}
$this->attributes['class'] = array_unique(
array_merge(
array( 'regular-text' ),
$this->attributes['class']
)
);
}
}
public function render() {
printf( // WPCS: xss ok
$this->template,
esc_attr( $this->field['type'] ),
esc_attr( $this->field['value'] ),
$this->build_attributes()
);
$this->description();
}
}
/**
* Field: Textarea
*/
class Kucrut_Form_Field_Textarea extends Kucrut_Form_Field {
protected $template = '<textarea%s>%s</textarea>';
protected $attributes = array(
'class' => 'widefat',
'cols' => 50,
'rows' => 5,
);
protected function set_properties() {
if ( ! is_string( $this->field['value'] ) ) {
$this->field['value'] = '';
}
}
public function render() {
printf( // WPCS: XSS ok.
$this->template,
$this->build_attributes(),
esc_textarea( $this->field['value'] )
);
}
}
/**
* Field: Checkbox
*/
class Kucrut_Form_Field_Checkbox extends Kucrut_Form_Field {
protected $template = '<label><input type="%s" value="%s"%s%s /> %s</label><br />';
protected function set_properties() {
$this->field['value'] = array_filter( (array) $this->field['value'] );
$this->attributes['name'] .= '[]';
}
protected function checked( $value ) {
return checked( in_array( $value, $this->field['value'], true ), true, false );
}
public function render() {
foreach ( $this->field['choices'] as $value => $label ) {
printf( // WPCS: XSS ok.
$this->template,
$this->field['type'],
esc_attr( $value ),
$this->checked( $value ),
$this->build_attributes( 'id' ),
esc_html( $label )
);
}
}
}
/**
* Field: Radio
*/
class Kucrut_Form_Field_Radio extends Kucrut_Form_Field_Checkbox {
protected function set_properties() {
if ( ! is_string( $this->field['value'] ) ) {
$this->field['value'] = '';
}
}
protected function checked( $value ) {
return checked( $value, $this->field['value'], false );
}
}
/**
* Field: Select
*/
class Kucrut_Form_Field_Select extends Kucrut_Form_Field {
protected $template = '<option value="%s"%s>%s</option>';
protected function set_properties() {
if ( ! is_string( $this->field['value'] ) ) {
$this->field['value'] = '';
}
}
protected function selected( $value ) {
return selected( ( $value === $this->field['value'] ), true, false );
}
public function render() {
?>
<select<?php echo $this->build_attributes() // xss ok ?>>
<?php foreach ( $this->field['choices'] as $index => $choice ) : ?>
<?php
if ( is_array( $choice ) ) {
$value = $choice['value'];
$label = $choice['label'];
} else {
$value = $index;
$label = $choice;
}
?>
<?php
printf( // WPCS: XSS ok.
$this->template,
esc_attr( $value ),
$this->selected( $value ),
esc_html( $label )
);
?>
<?php endforeach; ?>
</select>
<?php
}
}
/**
* Field: Multiple Select
*/
class Kucrut_Form_Field_Select_Multiple extends Kucrut_Form_Field_Select {
protected function set_properties() {
$this->field['value'] = array_filter( (array) $this->field['value'] );
$this->attributes['name'] .= '[]';
$this->attributes['multiple'] = 'multiple';
}
protected function selected( $value ) {
return selected( in_array( $value, $this->field['value'], true ), true, false );
}
}
/**
* Field: Select Pages
*/
class Kucrut_Form_Field_Select_Pages extends Kucrut_Form_Field_Select {
protected $wp_dropdown_pages_args = array(
'depth' => 0,
'child_of' => 0,
'option_none_value' => '',
);
public function __construct( $field, $args ) {
$this->wp_dropdown_pages_args['show_option_none'] = __( '— Select —', 'menu-icons' );
parent::__construct( $field, $args );
}
public function set_properties() {
parent::set_properties();
if ( empty( $this->args->wp_dropdown_pages_args ) ) {
$this->args->wp_dropdown_pages_args = array();
}
// Apply defeaults
$this->args->wp_dropdown_pages_args = wp_parse_args(
$this->args->wp_dropdown_pages_args,
$this->wp_dropdown_pages_args
);
// Force some args
$this->args->wp_dropdown_pages_args = array_merge(
$this->args->wp_dropdown_pages_args,
array(
'echo' => true,
'name' => $this->attributes['name'],
'id' => $this->attributes['id'],
'selected' => $this->field['value'],
)
);
}
public function render() {
wp_dropdown_pages( $this->args->wp_dropdown_pages_args ); // WPCS: XSS ok.
}
}
/**
* Field: Special (Callback)
*/
class Kucrut_Form_Field_Special extends Kucrut_Form_Field {
public function render() {
call_user_func_array(
$this->field['render_cb'],
array( $this )
);
}
}