icon-picker
8 years ago
menu-item-custom-fields
8 years ago
compat.php
8 years ago
form-fields.php
8 years ago
functions.php
8 years ago
form-fields.php
576 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', |
| 72 | 'name', |
| 73 | 'value', |
| 74 | 'checked', |
| 75 | 'multiple', |
| 76 | ); |
| 77 | |
| 78 | /** |
| 79 | * Holds allowed html tags |
| 80 | * |
| 81 | * @since 0.1.0 |
| 82 | * @var array |
| 83 | * @access protected |
| 84 | */ |
| 85 | protected $allowed_html = array( |
| 86 | 'a' => array( |
| 87 | 'href' => true, |
| 88 | 'target' => true, |
| 89 | 'title' => true, |
| 90 | ), |
| 91 | 'code' => true, |
| 92 | 'em' => true, |
| 93 | 'p' => array( 'class' => true ), |
| 94 | 'span' => array( 'class' => true ), |
| 95 | 'strong' => true, |
| 96 | ); |
| 97 | |
| 98 | /** |
| 99 | * Holds constructed field |
| 100 | * |
| 101 | * @since 0.1.0 |
| 102 | * @var array |
| 103 | * @access protected |
| 104 | */ |
| 105 | protected $field; |
| 106 | |
| 107 | |
| 108 | /** |
| 109 | * Holds field attributes |
| 110 | * |
| 111 | * @since 0.1.0 |
| 112 | * @var array |
| 113 | * @access protected |
| 114 | */ |
| 115 | protected $attributes = array(); |
| 116 | |
| 117 | |
| 118 | /** |
| 119 | * Loader |
| 120 | * |
| 121 | * @param string URL path to this directory |
| 122 | */ |
| 123 | final public static function load( $url_path = null ) { |
| 124 | // Set URL path for assets |
| 125 | if ( ! is_null( $url_path ) ) { |
| 126 | self::$url_path = $url_path; |
| 127 | } else { |
| 128 | self::$url_path = plugin_dir_url( __FILE__ ); |
| 129 | } |
| 130 | |
| 131 | // Supported field types |
| 132 | self::$types = apply_filters( |
| 133 | 'form_field_types', |
| 134 | self::$types |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | /** |
| 140 | * Create field |
| 141 | * |
| 142 | * @param array $field Field array |
| 143 | * @param array $args Extra field arguments |
| 144 | */ |
| 145 | final public static function create( array $field, $args = array() ) { |
| 146 | $field = wp_parse_args( $field, self::$defaults['field'] ); |
| 147 | if ( ! isset( self::$types[ $field['type'] ] ) |
| 148 | || ! is_subclass_of( self::$types[ $field['type'] ], __CLASS__ ) |
| 149 | ) { |
| 150 | trigger_error( |
| 151 | sprintf( |
| 152 | esc_html__( '%1$s: Type %2$s is not supported, reverting to text.', 'menu-icons' ), |
| 153 | __CLASS__, |
| 154 | esc_html( $field['type'] ) |
| 155 | ), |
| 156 | E_USER_WARNING |
| 157 | ); |
| 158 | $field['type'] = 'text'; |
| 159 | } |
| 160 | |
| 161 | if ( is_null( $field['value'] ) && ! is_null( $field['default'] ) ) { |
| 162 | $field['value'] = $field['default']; |
| 163 | } |
| 164 | |
| 165 | foreach ( self::$forbidden_attributes as $key ) { |
| 166 | unset( $field['attributes'][ $key ] ); |
| 167 | } |
| 168 | |
| 169 | $args = (object) wp_parse_args( $args, self::$defaults['args'] ); |
| 170 | $class = self::$types[ $field['type'] ]; |
| 171 | |
| 172 | return new $class( $field, $args ); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /** |
| 177 | * Constructor |
| 178 | * |
| 179 | * @since 0.1.0 |
| 180 | * @param array $field Field array |
| 181 | * @param object $args Extra field arguments |
| 182 | */ |
| 183 | public function __construct( $field, $args ) { |
| 184 | $this->field = $field; |
| 185 | $this->args = $args; |
| 186 | |
| 187 | if ( ! is_array( $this->args->keys ) ) { |
| 188 | $this->args->keys = array(); |
| 189 | } |
| 190 | $this->args->keys[] = $field['id']; |
| 191 | |
| 192 | $this->attributes['id'] = $this->create_id(); |
| 193 | $this->attributes['name'] = $this->create_name(); |
| 194 | |
| 195 | $this->attributes = wp_parse_args( |
| 196 | $this->attributes, |
| 197 | (array) $field['attributes'] |
| 198 | ); |
| 199 | |
| 200 | $this->set_properties(); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | /** |
| 205 | * Attribute |
| 206 | * |
| 207 | * @since 0.1.0 |
| 208 | * @param string $key Attribute key |
| 209 | * @return mixed NULL if attribute doesn't exist |
| 210 | */ |
| 211 | public function __get( $key ) { |
| 212 | foreach ( array( 'attributes', 'field' ) as $group ) { |
| 213 | if ( isset( $this->{$group}[ $key ] ) ) { |
| 214 | return $this->{$group}[ $key ]; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return null; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | /** |
| 223 | * Create id/name attribute |
| 224 | * |
| 225 | * @since 0.1.0 |
| 226 | * @param string $format Attribute format |
| 227 | */ |
| 228 | protected function create_id_name( $format ) { |
| 229 | return call_user_func_array( |
| 230 | 'sprintf', |
| 231 | array_merge( |
| 232 | array( $format ), |
| 233 | $this->args->keys |
| 234 | ) |
| 235 | ); |
| 236 | } |
| 237 | |
| 238 | |
| 239 | /** |
| 240 | * Create id attribute |
| 241 | * |
| 242 | * @since 0.1.0 |
| 243 | * @access protected |
| 244 | * @return string |
| 245 | */ |
| 246 | protected function create_id() { |
| 247 | $format = implode( '-', $this->args->keys ); |
| 248 | |
| 249 | return $this->create_id_name( $format ); |
| 250 | } |
| 251 | |
| 252 | |
| 253 | /** |
| 254 | * Create name attribute |
| 255 | * |
| 256 | * @since 0.1.0 |
| 257 | * @access protected |
| 258 | * @return string |
| 259 | */ |
| 260 | protected function create_name() { |
| 261 | $format = '%s'; |
| 262 | $format .= str_repeat( '[%s]', ( count( $this->args->keys ) - 1 ) ); |
| 263 | |
| 264 | return $this->create_id_name( $format ); |
| 265 | } |
| 266 | |
| 267 | |
| 268 | /** |
| 269 | * Set field properties |
| 270 | * |
| 271 | * @since 0.1.0 |
| 272 | */ |
| 273 | protected function set_properties() {} |
| 274 | |
| 275 | |
| 276 | /** |
| 277 | * Build field attributes |
| 278 | * |
| 279 | * @since 0.1.0 |
| 280 | * @param array $excludes Attributes to be excluded |
| 281 | * @return string |
| 282 | */ |
| 283 | protected function build_attributes( $excludes = array() ) { |
| 284 | $excludes = array_filter( (array) $excludes ); |
| 285 | $attributes = ''; |
| 286 | |
| 287 | foreach ( $this->attributes as $key => $value ) { |
| 288 | if ( in_array( $key, $excludes, true ) ) { |
| 289 | continue; |
| 290 | } |
| 291 | |
| 292 | if ( 'class' === $key ) { |
| 293 | $value = implode( ' ', (array) $value ); |
| 294 | } |
| 295 | |
| 296 | $attributes .= sprintf( |
| 297 | ' %s="%s"', |
| 298 | esc_attr( $key ), |
| 299 | esc_attr( $value ) |
| 300 | ); |
| 301 | } |
| 302 | |
| 303 | return $attributes; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | /** |
| 308 | * Print field |
| 309 | * |
| 310 | * @since 0.1.0 |
| 311 | */ |
| 312 | abstract public function render(); |
| 313 | |
| 314 | |
| 315 | /** |
| 316 | * Print field description |
| 317 | * |
| 318 | * @since 0.1.0 |
| 319 | */ |
| 320 | public function description() { |
| 321 | if ( ! empty( $this->field['description'] ) ) { |
| 322 | $tag = ( ! empty( $this->args->inline_description ) ) ? 'span' : 'p'; |
| 323 | |
| 324 | printf( // WPCS: XSS ok. |
| 325 | '<%1$s class="description">%2$s</%1$s>', |
| 326 | $tag, |
| 327 | wp_kses( $this->field['description'], $this->allowed_html ) |
| 328 | ); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | |
| 334 | /** |
| 335 | * Field: text |
| 336 | */ |
| 337 | class Kucrut_Form_Field_Text extends Kucrut_Form_Field { |
| 338 | |
| 339 | protected $template = '<input type="%s" value="%s"%s />'; |
| 340 | |
| 341 | |
| 342 | protected function set_properties() { |
| 343 | if ( ! is_string( $this->field['value'] ) ) { |
| 344 | $this->field['value'] = ''; |
| 345 | } |
| 346 | |
| 347 | if ( in_array( $this->field['type'], array( 'text', 'url' ), true ) ) { |
| 348 | if ( ! isset( $this->attributes['class'] ) ) { |
| 349 | $this->attributes['class'] = array(); |
| 350 | } |
| 351 | $this->attributes['class'] = array_unique( |
| 352 | array_merge( |
| 353 | array( 'regular-text' ), |
| 354 | $this->attributes['class'] |
| 355 | ) |
| 356 | ); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | |
| 361 | public function render() { |
| 362 | printf( // WPCS: xss ok |
| 363 | $this->template, |
| 364 | esc_attr( $this->field['type'] ), |
| 365 | esc_attr( $this->field['value'] ), |
| 366 | $this->build_attributes() |
| 367 | ); |
| 368 | $this->description(); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | |
| 373 | /** |
| 374 | * Field: Textarea |
| 375 | */ |
| 376 | class Kucrut_Form_Field_Textarea extends Kucrut_Form_Field { |
| 377 | |
| 378 | protected $template = '<textarea%s>%s</textarea>'; |
| 379 | |
| 380 | protected $attributes = array( |
| 381 | 'class' => 'widefat', |
| 382 | 'cols' => 50, |
| 383 | 'rows' => 5, |
| 384 | ); |
| 385 | |
| 386 | |
| 387 | public function render() { |
| 388 | printf( // WPCS: XSS ok. |
| 389 | $this->template, |
| 390 | $this->build_attributes(), |
| 391 | esc_textarea( $this->field['value'] ) |
| 392 | ); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | |
| 397 | /** |
| 398 | * Field: Checkbox |
| 399 | */ |
| 400 | class Kucrut_Form_Field_Checkbox extends Kucrut_Form_Field { |
| 401 | |
| 402 | protected $template = '<label><input type="%s" value="%s"%s%s /> %s</label><br />'; |
| 403 | |
| 404 | |
| 405 | protected function set_properties() { |
| 406 | $this->field['value'] = array_filter( (array) $this->field['value'] ); |
| 407 | $this->attributes['name'] .= '[]'; |
| 408 | } |
| 409 | |
| 410 | |
| 411 | protected function checked( $value ) { |
| 412 | return checked( in_array( $value, $this->field['value'], true ), true, false ); |
| 413 | } |
| 414 | |
| 415 | |
| 416 | public function render() { |
| 417 | foreach ( $this->field['choices'] as $value => $label ) { |
| 418 | printf( // WPCS: XSS ok. |
| 419 | $this->template, |
| 420 | $this->field['type'], |
| 421 | esc_attr( $value ), |
| 422 | $this->checked( $value ), |
| 423 | $this->build_attributes( 'id' ), |
| 424 | esc_html( $label ) |
| 425 | ); |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | |
| 431 | /** |
| 432 | * Field: Radio |
| 433 | */ |
| 434 | class Kucrut_Form_Field_Radio extends Kucrut_Form_Field_Checkbox { |
| 435 | |
| 436 | protected function set_properties() { |
| 437 | if ( ! is_string( $this->field['value'] ) ) { |
| 438 | $this->field['value'] = ''; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | |
| 443 | protected function checked( $value ) { |
| 444 | return checked( $value, $this->field['value'], false ); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | |
| 449 | /** |
| 450 | * Field: Select |
| 451 | */ |
| 452 | class Kucrut_Form_Field_Select extends Kucrut_Form_Field { |
| 453 | |
| 454 | protected $template = '<option value="%s"%s>%s</option>'; |
| 455 | |
| 456 | |
| 457 | protected function set_properties() { |
| 458 | if ( ! is_string( $this->field['value'] ) ) { |
| 459 | $this->field['value'] = ''; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | |
| 464 | protected function selected( $value ) { |
| 465 | return selected( ( $value === $this->field['value'] ), true, false ); |
| 466 | } |
| 467 | |
| 468 | |
| 469 | public function render() { |
| 470 | ?> |
| 471 | <select<?php echo $this->build_attributes() // xss ok ?>> |
| 472 | <?php foreach ( $this->field['choices'] as $index => $choice ) : ?> |
| 473 | <?php |
| 474 | if ( is_array( $choice ) ) { |
| 475 | $value = $choice['value']; |
| 476 | $label = $choice['label']; |
| 477 | } else { |
| 478 | $value = $index; |
| 479 | $label = $choice; |
| 480 | } |
| 481 | ?> |
| 482 | <?php |
| 483 | printf( // WPCS: XSS ok. |
| 484 | $this->template, |
| 485 | esc_attr( $value ), |
| 486 | $this->selected( $value ), |
| 487 | esc_html( $label ) |
| 488 | ); |
| 489 | ?> |
| 490 | <?php endforeach; ?> |
| 491 | </select> |
| 492 | <?php |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | |
| 497 | /** |
| 498 | * Field: Multiple Select |
| 499 | */ |
| 500 | class Kucrut_Form_Field_Select_Multiple extends Kucrut_Form_Field_Select { |
| 501 | |
| 502 | protected function set_properties() { |
| 503 | $this->field['value'] = array_filter( (array) $this->field['value'] ); |
| 504 | $this->attributes['name'] .= '[]'; |
| 505 | $this->attributes['multiple'] = 'multiple'; |
| 506 | } |
| 507 | |
| 508 | |
| 509 | protected function selected( $value ) { |
| 510 | return selected( in_array( $value, $this->field['value'], true ), true, false ); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | |
| 515 | /** |
| 516 | * Field: Select Pages |
| 517 | */ |
| 518 | class Kucrut_Form_Field_Select_Pages extends Kucrut_Form_Field_Select { |
| 519 | |
| 520 | protected $wp_dropdown_pages_args = array( |
| 521 | 'depth' => 0, |
| 522 | 'child_of' => 0, |
| 523 | 'option_none_value' => '', |
| 524 | ); |
| 525 | |
| 526 | |
| 527 | public function __construct( $field, $args ) { |
| 528 | $this->wp_dropdown_pages_args['show_option_none'] = __( '— Select —', 'menu-icons' ); |
| 529 | parent::__construct( $field, $args ); |
| 530 | } |
| 531 | |
| 532 | |
| 533 | public function set_properties() { |
| 534 | parent::set_properties(); |
| 535 | |
| 536 | if ( empty( $this->args->wp_dropdown_pages_args ) ) { |
| 537 | $this->args->wp_dropdown_pages_args = array(); |
| 538 | } |
| 539 | |
| 540 | // Apply defeaults |
| 541 | $this->args->wp_dropdown_pages_args = wp_parse_args( |
| 542 | $this->args->wp_dropdown_pages_args, |
| 543 | $this->wp_dropdown_pages_args |
| 544 | ); |
| 545 | |
| 546 | // Force some args |
| 547 | $this->args->wp_dropdown_pages_args = array_merge( |
| 548 | $this->args->wp_dropdown_pages_args, |
| 549 | array( |
| 550 | 'echo' => true, |
| 551 | 'name' => $this->attributes['name'], |
| 552 | 'id' => $this->attributes['id'], |
| 553 | 'selected' => $this->field['value'], |
| 554 | ) |
| 555 | ); |
| 556 | } |
| 557 | |
| 558 | |
| 559 | public function render() { |
| 560 | wp_dropdown_pages( $this->args->wp_dropdown_pages_args ); // WPCS: XSS ok. |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | |
| 565 | /** |
| 566 | * Field: Special (Callback) |
| 567 | */ |
| 568 | class Kucrut_Form_Field_Special extends Kucrut_Form_Field { |
| 569 | public function render() { |
| 570 | call_user_func_array( |
| 571 | $this->field['render_cb'], |
| 572 | array( $this ) |
| 573 | ); |
| 574 | } |
| 575 | } |
| 576 |