PluginProbe ʕ •ᴥ•ʔ
Menu Icons by Themeisle – Add Icons to Navigation Menus / 0.7.0
Menu Icons by Themeisle – Add Icons to Navigation Menus v0.7.0
0.13.24 trunk 0.1.0 0.1.1 0.1.2 0.1.3 0.1.4 0.1.5 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.11.2 0.11.3 0.11.4 0.11.5 0.12.0 0.12.1 0.12.10 0.12.11 0.12.12 0.12.2 0.12.3 0.12.4 0.12.5 0.12.6 0.12.7 0.12.8 0.12.9 0.13.0 0.13.1 0.13.10 0.13.11 0.13.12 0.13.13 0.13.14 0.13.15 0.13.16 0.13.17 0.13.18 0.13.19 0.13.2 0.13.20 0.13.21 0.13.22 0.13.23 0.13.3 0.13.4 0.13.5 0.13.6 0.13.7 0.13.8 0.13.9 0.2.0 0.2.1 0.2.2 0.2.3 0.3.0 0.3.1 0.3.2 0.4.0 0.5.0 0.5.1 0.6.0 0.7.0 0.8.0 0.8.1 0.9.0 0.9.2
menu-icons / includes / library / form-fields.php
menu-icons / includes / library Last commit date
form-fields.php 11 years ago functions.php 12 years ago
form-fields.php
572 lines
1 <?php
2
3 /**
4 * Form Fields
5 *
6 * A prototype for the new "Form Fields" plugin, a standalone plugin and
7 * extension for the upcoming "Settings" plugin, a rewrite of KC Settings.
8 *
9 * @author Dzikri Aziz <kvcrvt@gmail.com>
10 */
11
12 /**
13 * Form Fields
14 */
15 abstract class Kucrut_Form_Field {
16
17 /**
18 * Holds field & argument defaults
19 *
20 * @since 0.1.0
21 * @var array
22 * @access protected
23 */
24 protected static $defaults = array(
25 'field' => array(
26 'id' => '',
27 'type' => 'text',
28 'value' => null,
29 'default' => null,
30 'attributes' => array(),
31 'description' => '',
32 'choices' => array(),
33 ),
34 'args' => array(
35 'keys' => array(),
36 'inline_description' => false,
37 ),
38 );
39
40 /**
41 * Holds field attributes
42 *
43 * @since 0.1.0
44 * @var array
45 * @access protected
46 */
47 protected static $types = array(
48 'text' => 'Kucrut_Form_Field_Text',
49 'number' => 'Kucrut_Form_Field_Text',
50 'url' => 'Kucrut_Form_Field_Text',
51 'color' => 'Kucrut_Form_Field_Text',
52 'date' => 'Kucrut_Form_Field_Text',
53 'hidden' => 'Kucrut_Form_Field_Text',
54 'checkbox' => 'Kucrut_Form_Field_Checkbox',
55 'radio' => 'Kucrut_Form_Field_Radio',
56 'textarea' => 'Kucrut_Form_Field_Textarea',
57 'select' => 'Kucrut_Form_Field_Select',
58 'select_multiple' => 'Kucrut_Form_Field_Select_Multiple',
59 'select_pages' => 'Kucrut_Form_Field_Select_Pages',
60 'special' => 'Kucrut_Form_Field_Special',
61 );
62
63 /**
64 * Holds forbidden attributes
65 *
66 * @since 0.1.0
67 * @var array
68 * @access protected
69 */
70 protected static $forbidden_attributes = array(
71 'id', 'name', 'value', 'checked', 'multiple',
72 );
73
74 /**
75 * Holds allowed html tags
76 *
77 * @since 0.1.0
78 * @var array
79 * @access protected
80 */
81 protected $allowed_html = array(
82 'a' => array(
83 'href' => true,
84 'target' => true,
85 'title' => true,
86 ),
87 'code' => true,
88 'em' => true,
89 'p' => array( 'class' => true ),
90 'span' => array( 'class' => true ),
91 'strong' => true,
92 );
93
94 /**
95 * Holds constructed field
96 *
97 * @since 0.1.0
98 * @var array
99 * @access protected
100 */
101 protected $field;
102
103
104 /**
105 * Holds field attributes
106 *
107 * @since 0.1.0
108 * @var array
109 * @access protected
110 */
111 protected $attributes = array();
112
113
114 /**
115 * Loader
116 *
117 * @param string URL path to this directory
118 */
119 final public static function load( $url_path = null ) {
120 // Set URL path for assets
121 if ( ! is_null( $url_path ) ) {
122 self::$url_path = $url_path;
123 }
124 else {
125 self::$url_path = plugin_dir_url( __FILE__ );
126 }
127
128 // Supported field types
129 self::$types = apply_filters(
130 'form_field_types',
131 self::$types
132 );
133 }
134
135
136 /**
137 * Create field
138 *
139 * @param array $field Field array
140 * @param array $args Extra field arguments
141 */
142 final public static function create( Array $field, $args = array() ) {
143 $field = wp_parse_args( $field, self::$defaults['field'] );
144 if ( ! isset( self::$types[ $field['type'] ] )
145 || ! is_subclass_of( self::$types[ $field['type'] ], __CLASS__ )
146 ) {
147 trigger_error(
148 sprintf(
149 __( '%1$s: Type %2$s is not supported, reverting to text.', 'menu-icons' ),
150 __CLASS__,
151 $field['type']
152 ),
153 E_USER_WARNING
154 );
155 $field['type'] = 'text';
156 }
157
158 if ( is_null( $field['value'] ) && ! is_null( $field['default'] ) ) {
159 $field['value'] = $field['default'];
160 }
161
162 foreach ( self::$forbidden_attributes as $key ) {
163 unset( $field['attributes'][ $key ] );
164 }
165
166 $args = (object) wp_parse_args( $args, self::$defaults['args'] );
167 $class = self::$types[ $field['type'] ];
168
169 return new $class( $field, $args );
170 }
171
172
173 /**
174 * Constructor
175 *
176 * @since 0.1.0
177 * @param array $field Field array
178 * @param object $args Extra field arguments
179 */
180 public function __construct( $field, $args ) {
181 $this->field = $field;
182 $this->args = $args;
183
184 if ( ! is_array( $this->args->keys ) ) {
185 $this->args->keys = array();
186 }
187 $this->args->keys[] = $field['id'];
188
189 $this->attributes['id'] = $this->create_id();
190 $this->attributes['name'] = $this->create_name();
191
192 $this->attributes = wp_parse_args(
193 $this->attributes,
194 (array) $field['attributes']
195 );
196
197 $this->set_properties();
198 }
199
200
201 /**
202 * Attribute
203 *
204 * @since 0.1.0
205 * @param string $key Attribute key
206 * @return mixed NULL if attribute doesn't exist
207 */
208 public function __get( $key ) {
209 foreach ( array( 'attributes', 'field' ) as $group ) {
210 if ( isset( $this->{$group}[ $key ] ) ) {
211 return $this->{$group}[ $key ];
212 }
213 }
214
215 return null;
216 }
217
218
219 /**
220 * Create id/name attribute
221 *
222 * @since 0.1.0
223 * @param string $format Attribute format
224 */
225 protected function create_id_name( $format ) {
226 return call_user_func_array(
227 'sprintf',
228 array_merge(
229 array( $format ),
230 $this->args->keys
231 )
232 );
233 }
234
235
236 /**
237 * Create id attribute
238 *
239 * @since 0.1.0
240 * @access protected
241 * @return string
242 */
243 protected function create_id() {
244 $format = implode( '-', $this->args->keys );
245
246 return $this->create_id_name( $format );
247 }
248
249
250 /**
251 * Create name attribute
252 *
253 * @since 0.1.0
254 * @access protected
255 * @return string
256 */
257 protected function create_name() {
258 $format = '%s';
259 $format .= str_repeat( '[%s]', ( count( $this->args->keys ) - 1 ) );
260
261 return $this->create_id_name( $format );
262 }
263
264
265 /**
266 * Set field properties
267 *
268 * @since 0.1.0
269 */
270 protected function set_properties() {}
271
272
273 /**
274 * Build field attributes
275 *
276 * @since 0.1.0
277 * @param array $excludes Attributes to be excluded
278 * @return string
279 */
280 protected function build_attributes( $excludes = array() ) {
281 $excludes = array_filter( (array) $excludes );
282 $attributes = '';
283
284 foreach ( $this->attributes as $key => $value ) {
285 if ( in_array( $key, $excludes ) ) {
286 continue;
287 }
288
289 if ( 'class' === $key ) {
290 $value = implode( ' ', (array) $value );
291 }
292
293 $attributes .= sprintf(
294 ' %s="%s"',
295 esc_attr( $key ),
296 esc_attr( $value )
297 );
298 }
299
300 return $attributes;
301 }
302
303
304 /**
305 * Print field
306 *
307 * @since 0.1.0
308 */
309 abstract public function render();
310
311
312 /**
313 * Print field description
314 *
315 * @since 0.1.0
316 */
317 public function description() {
318 if ( ! empty( $this->field['description'] ) ) {
319 $tag = ( ! empty( $this->args->inline_description ) ) ? 'span' : 'p';
320
321 printf(
322 '<%1$s class="description">%2$s</%1$s>',
323 $tag,
324 wp_kses( $this->field['description'], $this->allowed_html )
325 );
326 }
327 }
328 }
329
330
331 /**
332 * Field: text
333 */
334 class Kucrut_Form_Field_Text extends Kucrut_Form_Field {
335
336 protected $template = '<input type="%s" value="%s"%s />';
337
338
339 protected function set_properties() {
340 if ( ! is_string( $this->field['value'] ) ) {
341 $this->field['value'] = '';
342 }
343
344 if ( in_array( $this->field['type'], array( 'text', 'url' ) ) ) {
345 if ( ! isset( $this->attributes['class'] ) ) {
346 $this->attributes['class'] = array();
347 }
348 $this->attributes['class'] = array_unique(
349 array_merge(
350 array( 'regular-text' ),
351 $this->attributes['class']
352 )
353 );
354 }
355 }
356
357
358 public function render() {
359 printf(
360 $this->template,
361 esc_attr( $this->field['type'] ),
362 esc_attr( $this->field['value'] ),
363 $this->build_attributes()
364 );
365 $this->description();
366 }
367 }
368
369
370 /**
371 * Field: Textarea
372 */
373 class Kucrut_Form_Field_Textarea extends Kucrut_Form_Field {
374
375 protected $template = '<textarea%s>%s</textarea>';
376
377 protected $attributes = array(
378 'class' => 'widefat',
379 'cols' => 50,
380 'rows' => 5,
381 );
382
383
384 public function render() {
385 printf(
386 $this->template,
387 $this->build_attributes(),
388 esc_textarea( $this->field['value'] )
389 );
390 }
391 }
392
393
394 /**
395 * Field: Checkbox
396 */
397 class Kucrut_Form_Field_Checkbox extends Kucrut_Form_Field {
398
399 protected $template = '<label><input type="%s" value="%s"%s%s /> %s</label><br />';
400
401
402 protected function set_properties() {
403 $this->field['value'] = array_filter( (array) $this->field['value'] );
404 $this->attributes['name'] .= '[]';
405 }
406
407
408 protected function checked( $value ) {
409 return checked( in_array( $value, $this->field['value'] ), true, false );
410 }
411
412
413 public function render() {
414 foreach ( $this->field['choices'] as $value => $label ) {
415 printf(
416 $this->template,
417 $this->field['type'],
418 esc_attr( $value ),
419 $this->checked( $value ),
420 $this->build_attributes( 'id' ),
421 esc_html( $label )
422 );
423 }
424 }
425 }
426
427
428 /**
429 * Field: Radio
430 */
431 class Kucrut_Form_Field_Radio extends Kucrut_Form_Field_Checkbox {
432
433 protected function set_properties() {
434 if ( ! is_string( $this->field['value'] ) ) {
435 $this->field['value'] = '';
436 }
437 }
438
439
440 protected function checked( $value ) {
441 return checked( $value, $this->field['value'], false );
442 }
443 }
444
445
446 /**
447 * Field: Select
448 */
449 class Kucrut_Form_Field_Select extends Kucrut_Form_Field {
450
451 protected $template = '<option value="%s"%s>%s</option>';
452
453
454 protected function set_properties() {
455 if ( ! is_string( $this->field['value'] ) ) {
456 $this->field['value'] = '';
457 }
458 }
459
460
461 protected function selected( $value ) {
462 return selected( ( $value == $this->field['value'] ), true, false );
463 }
464
465
466 public function render() {
467 ?>
468 <select<?php echo $this->build_attributes() // xss ok ?>>
469 <?php foreach ( $this->field['choices'] as $index => $choice ) : ?>
470 <?php
471 if ( is_array( $choice ) ) {
472 $value = $choice['value'];
473 $label = $choice['label'];
474 }
475 else {
476 $value = $index;
477 $label = $choice;
478 }
479 ?>
480 <?php printf(
481 $this->template,
482 esc_attr( $value ),
483 $this->selected( $value ),
484 esc_html( $label )
485 ) ?>
486 <?php endforeach; ?>
487 </select>
488 <?php
489 }
490 }
491
492
493 /**
494 * Field: Multiple Select
495 */
496 class Kucrut_Form_Field_Select_Multiple extends Kucrut_Form_Field_Select {
497
498 protected function set_properties() {
499 $this->field['value'] = array_filter( (array) $this->field['value'] );
500 $this->attributes['name'] .= '[]';
501 $this->attributes['multiple'] = 'multiple';
502 }
503
504
505 protected function selected( $value ) {
506 return selected( in_array( $value, $this->field['value'] ), true, false );
507 }
508 }
509
510
511 /**
512 * Field: Select Pages
513 */
514 class Kucrut_Form_Field_Select_Pages extends Kucrut_Form_Field_Select {
515
516 protected $wp_dropdown_pages_args = array(
517 'depth' => 0,
518 'child_of' => 0,
519 'option_none_value' => '',
520 );
521
522
523 public function __construct( $field, $args ) {
524 $this->wp_dropdown_pages_args['show_option_none'] = __( '&mdash; Select &mdash;', 'postmedia' );
525 parent::__construct( $field, $args );
526 }
527
528
529 public function set_properties() {
530 parent::set_properties();
531
532 if ( empty( $this->args->wp_dropdown_pages_args ) ) {
533 $this->args->wp_dropdown_pages_args = array();
534 }
535
536 // Apply defeaults
537 $this->args->wp_dropdown_pages_args = wp_parse_args(
538 $this->args->wp_dropdown_pages_args,
539 $this->wp_dropdown_pages_args
540 );
541
542 // Force some args
543 $this->args->wp_dropdown_pages_args = array_merge(
544 $this->args->wp_dropdown_pages_args,
545 array(
546 'echo' => true,
547 'name' => $this->attributes['name'],
548 'id' => $this->attributes['id'],
549 'selected' => $this->field['value'],
550 )
551 );
552 }
553
554
555 public function render() {
556 wp_dropdown_pages( $this->args->wp_dropdown_pages_args );
557 }
558 }
559
560
561 /**
562 * Field: Special (Callback)
563 */
564 class Kucrut_Form_Field_Special extends Kucrut_Form_Field {
565 public function render() {
566 call_user_func_array(
567 $this->field['render_cb'],
568 array( $this )
569 );
570 }
571 }
572