Complex classes like Give_Fields_API often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Give_Fields_API, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Give_Fields_API { |
||
|
|
|||
| 13 | /** |
||
| 14 | * Instance. |
||
| 15 | * |
||
| 16 | * @since 1.9 |
||
| 17 | * @access private |
||
| 18 | * @var Give_Fields_API |
||
| 19 | */ |
||
| 20 | static private $instance; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The defaults for all elements |
||
| 24 | * |
||
| 25 | * @since 1.9 |
||
| 26 | * @access static |
||
| 27 | */ |
||
| 28 | static $field_defaults = array( |
||
| 29 | 'type' => '', |
||
| 30 | 'id' => '', |
||
| 31 | 'data_type' => '', |
||
| 32 | 'value' => null, |
||
| 33 | 'required' => false, |
||
| 34 | 'options' => array(), |
||
| 35 | |||
| 36 | // Set default value to field. |
||
| 37 | 'default' => null, |
||
| 38 | |||
| 39 | // Set checkbox value. |
||
| 40 | 'cbvalue' => 'on', |
||
| 41 | |||
| 42 | // Field with wrapper. |
||
| 43 | 'wrapper' => true, |
||
| 44 | 'wrapper_type' => 'p', |
||
| 45 | |||
| 46 | // Add label, before and after field. |
||
| 47 | 'label' => '', |
||
| 48 | 'label_position' => 'before', |
||
| 49 | 'label_tooltip' => '', |
||
| 50 | |||
| 51 | // Show multiple fields in same row with in sub section. |
||
| 52 | 'sub_section_start' => false, |
||
| 53 | 'sub_section_end' => false, |
||
| 54 | |||
| 55 | // Sortable. |
||
| 56 | 'sortable' => false, |
||
| 57 | 'sortable-icon' => false, |
||
| 58 | |||
| 59 | // Add custom attributes. |
||
| 60 | 'label_attributes' => array(), |
||
| 61 | 'field_attributes' => array(), |
||
| 62 | 'wrapper_attributes' => array(), |
||
| 63 | |||
| 64 | // Show/Hide field in before/after modal view. |
||
| 65 | 'show_without_modal' => false, |
||
| 66 | 'show_within_modal' => true, |
||
| 67 | |||
| 68 | // Params to edit field html. |
||
| 69 | 'before_field' => '', |
||
| 70 | 'after_field' => '', |
||
| 71 | 'before_field_wrapper' => '', |
||
| 72 | 'after_field_wrapper' => '', |
||
| 73 | 'before_field_label' => '', |
||
| 74 | 'after_field_label' => '', |
||
| 75 | |||
| 76 | // Manually render field. |
||
| 77 | 'callback' => '', |
||
| 78 | |||
| 79 | ); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The defaults for all sections. |
||
| 83 | * |
||
| 84 | * @since 1.9 |
||
| 85 | * @access static |
||
| 86 | */ |
||
| 87 | static $section_defaults = array( |
||
| 88 | 'type' => 'section', |
||
| 89 | 'label' => '', |
||
| 90 | 'id' => '', |
||
| 91 | 'section_attributes' => array(), |
||
| 92 | |||
| 93 | // Manually render section. |
||
| 94 | 'callback' => '', |
||
| 95 | ); |
||
| 96 | |||
| 97 | /** |
||
| 98 | * The defaults for all blocks. |
||
| 99 | * |
||
| 100 | * @since 1.9 |
||
| 101 | * @access static |
||
| 102 | */ |
||
| 103 | static $block_defaults = array( |
||
| 104 | 'type' => 'block', |
||
| 105 | 'label' => '', |
||
| 106 | 'id' => '', |
||
| 107 | 'block_attributes' => array(), |
||
| 108 | |||
| 109 | // Manually render section. |
||
| 110 | 'callback' => '', |
||
| 111 | ); |
||
| 112 | |||
| 113 | |||
| 114 | private function __construct() { |
||
| 116 | |||
| 117 | |||
| 118 | /** |
||
| 119 | * Get instance. |
||
| 120 | * |
||
| 121 | * @return static |
||
| 122 | */ |
||
| 123 | public static function get_instance() { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Initialize this module |
||
| 133 | * |
||
| 134 | * @since 1.9 |
||
| 135 | * @access static |
||
| 136 | */ |
||
| 137 | public function init() { |
||
| 140 | |||
| 141 | |||
| 142 | /** |
||
| 143 | * Render custom field. |
||
| 144 | * |
||
| 145 | * @since 1.0 |
||
| 146 | * @access private |
||
| 147 | * |
||
| 148 | * @param array $field |
||
| 149 | * @param array $form |
||
| 150 | * |
||
| 151 | * @return bool |
||
| 152 | */ |
||
| 153 | private function render_custom_field( $field, $form = null ) { |
||
| 171 | |||
| 172 | |||
| 173 | /** |
||
| 174 | * Render `{{form_fields}}` tag. |
||
| 175 | * |
||
| 176 | * @since 1.9 |
||
| 177 | * @access private |
||
| 178 | * |
||
| 179 | * @param string $form_html |
||
| 180 | * @param array $form |
||
| 181 | * |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | public function render_tags( $form_html, $form ) { |
||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * Render section. |
||
| 232 | * |
||
| 233 | * @since 1.9 |
||
| 234 | * @access public |
||
| 235 | * |
||
| 236 | * @param array $section |
||
| 237 | * @param array $form |
||
| 238 | * @param array $args Helper argument to render section. |
||
| 239 | * |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | public static function render_section( $section, $form = null, $args = array() ) { |
||
| 266 | |||
| 267 | |||
| 268 | /** |
||
| 269 | * Render block. |
||
| 270 | * |
||
| 271 | * @since 1.9 |
||
| 272 | * @access public |
||
| 273 | * |
||
| 274 | * @param array $block |
||
| 275 | * @param array $form |
||
| 276 | * @param array $args Helper argument to render section. |
||
| 277 | * |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | public static function render_block( $block, $form = null, $args = array() ) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Render tag |
||
| 304 | * |
||
| 305 | * @since 1.9 |
||
| 306 | * @access public |
||
| 307 | * |
||
| 308 | * @param array $field |
||
| 309 | * @param array $form |
||
| 310 | * @param array $args Helper argument to render section. |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public static function render_tag( $field, $form = null, $args = array() ) { |
||
| 315 | // Enqueue scripts. |
||
| 316 | Give_Form_API::enqueue_scripts(); |
||
| 317 | |||
| 318 | if( ! empty( $field['sortable'] ) ) { |
||
| 319 | wp_enqueue_script('jquery-ui-sortable'); |
||
| 320 | } |
||
| 321 | |||
| 322 | // Set default values if necessary. |
||
| 323 | if ( ! isset( $args['set_default'] ) || (bool) $args['set_default'] ) { |
||
| 324 | $field = self::$instance->set_default_values( $field, $form ); |
||
| 325 | } |
||
| 326 | |||
| 327 | $field_html = ''; |
||
| 328 | $functions_name = "render_{$field['type']}_field"; |
||
| 329 | |||
| 330 | if ( 'section' === self::$instance->get_field_type( $field ) ) { |
||
| 331 | $field_html = self::$instance->render_section( $field, $form, array( 'set_default' => false ) ); |
||
| 332 | |||
| 333 | } elseif ( method_exists( self::$instance, $functions_name ) ) { |
||
| 334 | $field_html = self::$instance->{$functions_name}( $field, $form, $args ); |
||
| 335 | |||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Filter the custom field type html. |
||
| 340 | * Developer can use this hook to render custom field type. |
||
| 341 | * |
||
| 342 | * @since 1.9 |
||
| 343 | * |
||
| 344 | * @param string $field_html |
||
| 345 | * @param array $field |
||
| 346 | * @param array $form |
||
| 347 | */ |
||
| 348 | $field_html = apply_filters( |
||
| 349 | "give_field_api_render_{$field['type']}_field", |
||
| 350 | $field_html, |
||
| 351 | $field, |
||
| 352 | $form, |
||
| 353 | $args |
||
| 354 | ); |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Filter the all field type html. |
||
| 358 | * |
||
| 359 | * @since 1.9 |
||
| 360 | * |
||
| 361 | * @param string $field_html |
||
| 362 | * @param array $field |
||
| 363 | * @param array $form |
||
| 364 | */ |
||
| 365 | $field_html = apply_filters( |
||
| 366 | "give_field_api_render_field", |
||
| 367 | $field_html, |
||
| 368 | $field, |
||
| 369 | $form, |
||
| 370 | $args |
||
| 371 | ); |
||
| 372 | |||
| 373 | return $field_html; |
||
| 374 | } |
||
| 375 | |||
| 376 | |||
| 377 | /** |
||
| 378 | * Render text field. |
||
| 379 | * |
||
| 380 | * @since 1.9 |
||
| 381 | * @access public |
||
| 382 | * |
||
| 383 | * @param array $field |
||
| 384 | * @param array $form |
||
| 385 | * @param array $args |
||
| 386 | * |
||
| 387 | * @return string |
||
| 388 | */ |
||
| 389 | public static function render_text_field( $field, $form = null, $args = array() ) { |
||
| 390 | $field_wrapper = self::$instance->render_field_wrapper( $field ); |
||
| 391 | $field['field_attributes']['name'] = self::get_field_name( $field ); |
||
| 392 | $field['field_attributes']['type'] = $field['type']; |
||
| 393 | |||
| 394 | if( ! empty( $field['required'] ) ) { |
||
| 395 | $field['field_attributes']['required'] = 'required'; |
||
| 396 | $field['field_attributes']['aria-required'] = 'true'; |
||
| 397 | } |
||
| 398 | |||
| 399 | ob_start(); |
||
| 400 | |||
| 401 | echo '<input ' . self::$instance->get_attributes( $field['field_attributes'] ) . '>'; |
||
| 402 | |||
| 403 | return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper ); |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Render submit field. |
||
| 408 | * |
||
| 409 | * @since 1.9 |
||
| 410 | * @access public |
||
| 411 | * |
||
| 412 | * @param array $field |
||
| 413 | * @param array $form |
||
| 414 | * @param array $args |
||
| 415 | * |
||
| 416 | * @return string |
||
| 417 | */ |
||
| 418 | public static function render_submit_field( $field, $form = null, $args = array() ) { |
||
| 419 | return self::$instance->render_text_field( $field ); |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Render checkbox field. |
||
| 424 | * |
||
| 425 | * @since 1.9 |
||
| 426 | * @access public |
||
| 427 | * |
||
| 428 | * @param array $field |
||
| 429 | * @param array $form |
||
| 430 | * @param array $args |
||
| 431 | * |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | public static function render_checkbox_field( $field, $form = null, $args = array() ) { |
||
| 435 | $field_wrapper = self::$instance->render_field_wrapper( $field ); |
||
| 436 | ob_start(); |
||
| 437 | ?> |
||
| 438 | <input |
||
| 439 | type="checkbox" |
||
| 440 | name="<?php echo self::get_field_name( $field ); ?>" |
||
| 441 | <?php echo( $field['required'] ? 'required=""' : '' ); ?> |
||
| 442 | <?php echo self::$instance->get_attributes( $field['field_attributes'] ); ?> |
||
| 443 | > |
||
| 444 | <?php |
||
| 445 | |||
| 446 | return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper ); |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Render email field. |
||
| 451 | * |
||
| 452 | * @since 1.9 |
||
| 453 | * @access public |
||
| 454 | * |
||
| 455 | * @param array $field |
||
| 456 | * @param array $form |
||
| 457 | * @param array $args |
||
| 458 | * |
||
| 459 | * @return string |
||
| 460 | */ |
||
| 461 | public static function render_email_field( $field, $form = null, $args = array() ) { |
||
| 462 | return self::$instance->render_text_field( $field ); |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Render number field. |
||
| 467 | * |
||
| 468 | * @since 1.9 |
||
| 469 | * @access public |
||
| 470 | * |
||
| 471 | * @param array $field |
||
| 472 | * @param array $form |
||
| 473 | * @param array $args |
||
| 474 | * |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | public static function render_number_field( $field, $form = null, $args = array() ) { |
||
| 478 | return self::$instance->render_text_field( $field ); |
||
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Render password field. |
||
| 483 | * |
||
| 484 | * @since 1.9 |
||
| 485 | * @access public |
||
| 486 | * |
||
| 487 | * @param array $field |
||
| 488 | * @param array $form |
||
| 489 | * @param array $args |
||
| 490 | * |
||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | public static function render_password_field( $field, $form = null, $args = array() ) { |
||
| 494 | return self::$instance->render_text_field( $field ); |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Render button field. |
||
| 499 | * |
||
| 500 | * @since 1.9 |
||
| 501 | * @access public |
||
| 502 | * |
||
| 503 | * @param array $field |
||
| 504 | * @param array $form |
||
| 505 | * @param array $args |
||
| 506 | * |
||
| 507 | * @return string |
||
| 508 | */ |
||
| 509 | public static function render_button_field( $field, $form = null, $args = array() ) { |
||
| 510 | return self::$instance->render_text_field( $field ); |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Render hidden field. |
||
| 515 | * |
||
| 516 | * @since 1.9 |
||
| 517 | * @access public |
||
| 518 | * |
||
| 519 | * @param array $field |
||
| 520 | * @param array $form |
||
| 521 | * @param array $args |
||
| 522 | * |
||
| 523 | * @return string |
||
| 524 | */ |
||
| 525 | public static function render_hidden_field( $field, $form = null, $args = array() ) { |
||
| 526 | $field['wrapper'] = false; |
||
| 527 | |||
| 528 | return self::$instance->render_text_field( $field ); |
||
| 529 | } |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Render textarea field. |
||
| 533 | * |
||
| 534 | * @since 1.9 |
||
| 535 | * @access public |
||
| 536 | * |
||
| 537 | * @param array $field |
||
| 538 | * @param array $form |
||
| 539 | * @param array $args |
||
| 540 | * |
||
| 541 | * @return string |
||
| 542 | */ |
||
| 543 | public static function render_textarea_field( $field, $form = null, $args = array() ) { |
||
| 544 | $field_wrapper = self::$instance->render_field_wrapper( $field ); |
||
| 545 | ob_start(); |
||
| 546 | ?> |
||
| 547 | <textarea |
||
| 548 | name="<?php echo self::get_field_name( $field ); ?>" |
||
| 549 | <?php echo( $field['required'] ? 'required=""' : '' ); ?> |
||
| 550 | <?php echo self::$instance->get_attributes( $field['field_attributes'] ); ?> |
||
| 551 | ><?php echo $field ['value']; ?></textarea> |
||
| 552 | |||
| 553 | |||
| 554 | <?php |
||
| 555 | |||
| 556 | return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper ); |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Render select field. |
||
| 561 | * |
||
| 562 | * @since 1.9 |
||
| 563 | * @access public |
||
| 564 | * |
||
| 565 | * @param array $field |
||
| 566 | * @param array $form |
||
| 567 | * @param array $args |
||
| 568 | * |
||
| 569 | * @return string |
||
| 570 | */ |
||
| 571 | public static function render_select_field( $field, $form = null, $args = array() ) { |
||
| 572 | $field_wrapper = self::$instance->render_field_wrapper( $field ); |
||
| 573 | ob_start(); |
||
| 574 | |||
| 575 | $options_html = ''; |
||
| 576 | foreach ( $field['options'] as $key => $option ) { |
||
| 577 | $options_html .= '<option '. self::get_attributes( $option['field_attributes'] ). '>' . $option['label'] . '</option>'; |
||
| 578 | } |
||
| 579 | ?> |
||
| 580 | |||
| 581 | <select |
||
| 582 | name="<?php echo self::get_field_name( $field ); ?>" |
||
| 583 | <?php echo( $field['required'] ? 'required=""' : '' ); ?> |
||
| 584 | <?php echo self::$instance->get_attributes( $field['field_attributes'] ); ?> |
||
| 585 | ><?php echo $options_html; ?></select> |
||
| 586 | <?php |
||
| 587 | |||
| 588 | return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper ); |
||
| 589 | } |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Render multi select field. |
||
| 593 | * |
||
| 594 | * @since 1.9 |
||
| 595 | * @access public |
||
| 596 | * |
||
| 597 | * @param array $field |
||
| 598 | * @param array $form |
||
| 599 | * @param array $args |
||
| 600 | * |
||
| 601 | * @return string |
||
| 602 | */ |
||
| 603 | public static function render_multi_select_field( $field, $form = null, $args = array() ) { |
||
| 604 | $field['field_attributes'] = array_merge( $field['field_attributes'], array( 'multiple' => 'multiple' ) ); |
||
| 605 | $field['id'] = "{$field['id']}[]"; |
||
| 606 | |||
| 607 | return self::$instance->render_select_field( $field ); |
||
| 608 | } |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Render radio field. |
||
| 612 | * |
||
| 613 | * @since 1.9 |
||
| 614 | * @access public |
||
| 615 | * |
||
| 616 | * @param array $field |
||
| 617 | * @param array $form |
||
| 618 | * @param array $args |
||
| 619 | * |
||
| 620 | * @return string |
||
| 621 | */ |
||
| 622 | public static function render_radio_field( $field, $form = null, $args = array() ) { |
||
| 623 | $field['wrapper_type'] = 'p' === $field['wrapper_type'] |
||
| 624 | ? 'fieldset' |
||
| 625 | : $field['wrapper_type']; |
||
| 626 | |||
| 627 | |||
| 628 | $field_wrapper = self::$instance->render_field_wrapper( $field ); |
||
| 629 | ob_start(); |
||
| 630 | |||
| 631 | $id_base = $field['field_attributes']['id']; |
||
| 632 | unset( $field['field_attributes']['id'] ); |
||
| 633 | |||
| 634 | echo '<ul>'; |
||
| 635 | foreach ( $field['options'] as $key => $option ) : |
||
| 636 | $option['field_attributes']['type'] = $field['type']; |
||
| 637 | $option['field_attributes']['name'] = self::get_field_name( $field ); |
||
| 638 | $option['field_attributes']['id'] = "{$id_base}-{$key}"; |
||
| 639 | ?> |
||
| 640 | <li> |
||
| 641 | <label class="give-label" for="<?php echo $option['field_attributes']['id']; ?>"> |
||
| 642 | <input <?php echo self::$instance->get_attributes( $option['field_attributes'] ); ?> |
||
| 643 | ><?php echo $option['label']; ?> |
||
| 644 | </label> |
||
| 645 | </li> |
||
| 646 | <?php |
||
| 647 | endforeach; |
||
| 648 | echo '</ul>'; |
||
| 649 | |||
| 650 | return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper ); |
||
| 651 | } |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Render multi checkbox field. |
||
| 655 | * |
||
| 656 | * Note: |
||
| 657 | * You can reorder checklist if sortable with help of give_reorder_array function. |
||
| 658 | * |
||
| 659 | * @since 1.9 |
||
| 660 | * @access public |
||
| 661 | * |
||
| 662 | * @param array $field |
||
| 663 | * @param array $form |
||
| 664 | * @param array $args |
||
| 665 | * |
||
| 666 | * @return string |
||
| 667 | */ |
||
| 668 | public static function render_multi_checkbox_field( $field, $form = null, $args = array() ) { |
||
| 669 | // Field wrapper. |
||
| 670 | $field['wrapper_type'] = 'p' === $field['wrapper_type'] |
||
| 671 | ? 'fieldset' |
||
| 672 | : $field['wrapper_type']; |
||
| 673 | |||
| 674 | // Field value. |
||
| 675 | $field['value'] = is_array( $field['value'] ) |
||
| 676 | ? $field['value'] |
||
| 677 | : array(); |
||
| 678 | |||
| 679 | // Field type. |
||
| 680 | $field['field_attributes']['type'] = 'checkbox'; |
||
| 681 | |||
| 682 | // Field name. |
||
| 683 | $field['field_attributes']['name'] = self::get_field_name( $field ) . '[]'; |
||
| 684 | |||
| 685 | $field_wrapper = self::$instance->render_field_wrapper( $field ); |
||
| 686 | ob_start(); |
||
| 687 | |||
| 688 | $id_base = $field['field_attributes']['id']; |
||
| 689 | unset( $field['field_attributes']['id'] ); |
||
| 690 | |||
| 691 | echo '<ul class="give-checklist-fields" data-give-sortable-list="' . absint( $field['sortable'] ) . '" data-give-sortable-icon="' . absint( $field['sortable-icon'] ) . '">'; |
||
| 692 | |||
| 693 | foreach ( $field['options'] as $key => $option ) : |
||
| 694 | // Set basic values for field. |
||
| 695 | $option = is_array( $option ) ? $option : array( 'label' => $option ); |
||
| 696 | $option['field_attributes']['id'] = "give-{$id_base}-{$key}"; |
||
| 697 | $option['field_attributes']['data-give-required'] = ( $field['required'] ? 1 : 0 ); |
||
| 698 | $option['field_attributes']['value'] = empty( $option['field_attributes']['value'] ) |
||
| 699 | ? $key |
||
| 700 | : $option['field_attributes']['value']; |
||
| 701 | |||
| 702 | // Check if field checked or not. |
||
| 703 | if ( |
||
| 704 | ! empty( $field['value'] ) && in_array( $key, $field['value'] ) |
||
| 705 | || ( |
||
| 706 | ( ! empty( $field['repeater_default_template'] ) || ! empty( $field['repeater_template'] ) ) |
||
| 707 | && is_array( $option ) |
||
| 708 | && ! empty( $option['checked'] ) |
||
| 709 | ) |
||
| 710 | ) { |
||
| 711 | $option['field_attributes']['checked'] = 'checked'; |
||
| 712 | } |
||
| 713 | |||
| 714 | // Add extra attribute per checkbox. |
||
| 715 | $option['field_attributes'] = is_array( $option ) && ! empty( $option['field_attributes'] ) |
||
| 716 | ? array_merge( $field['field_attributes'], $option['field_attributes'] ) |
||
| 717 | : $field['field_attributes']; |
||
| 718 | |||
| 719 | // Add field specific class. |
||
| 720 | $option['field_attributes']['class'] = trim( "{$option['field_attributes']['class']} give-{$key}" ); |
||
| 721 | ?> |
||
| 722 | <li> |
||
| 723 | <label class="give-label" for="<?php echo $option['field_attributes']['id']; ?>"> |
||
| 724 | <input <?php echo self::$instance->get_attributes( $option['field_attributes'] ); ?>><?php echo( ! is_array( $option ) ? $option : ( isset( $option['label'] ) ? $option['label'] : '' ) ); ?> |
||
| 725 | </label> |
||
| 726 | </li> |
||
| 727 | <?php |
||
| 728 | endforeach; |
||
| 729 | |||
| 730 | echo '</ul>'; |
||
| 731 | |||
| 732 | return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper ); |
||
| 733 | } |
||
| 734 | |||
| 735 | |||
| 736 | /** |
||
| 737 | * Render group field |
||
| 738 | * |
||
| 739 | * @since 1.9 |
||
| 740 | * @access public |
||
| 741 | * |
||
| 742 | * @param array $fields |
||
| 743 | * @param array $form |
||
| 744 | * @param array $args |
||
| 745 | * |
||
| 746 | * @return string |
||
| 747 | */ |
||
| 748 | public static function render_group_field( $fields, $form = null, $args = array() ) { |
||
| 749 | // Bailout. |
||
| 750 | if ( ! isset( $fields['fields'] ) || empty( $fields['fields'] ) ) { |
||
| 751 | return ''; |
||
| 752 | } |
||
| 753 | |||
| 754 | $group_numbering = isset( $fields['options']['group_numbering'] ) ? (int) $fields['options']['group_numbering'] : 0; |
||
| 755 | $close_tabs = isset( $fields['options']['close_tabs'] ) ? (int) $fields['options']['close_tabs'] : 0; |
||
| 756 | $repeater_field_values = $fields['value']; |
||
| 757 | $header_title = isset( $fields['options']['header_title'] ) |
||
| 758 | ? $fields['options']['header_title'] |
||
| 759 | : esc_attr__( 'Group', 'give' ); |
||
| 760 | |||
| 761 | $add_default_donation_field = false; |
||
| 762 | |||
| 763 | // Check if level is not created or we have to add default level. |
||
| 764 | if ( is_array( $repeater_field_values ) && ( $fields_count = count( $repeater_field_values ) ) ) { |
||
| 765 | $repeater_field_values = array_values( $repeater_field_values ); |
||
| 766 | } else { |
||
| 767 | $fields_count = 1; |
||
| 768 | $add_default_donation_field = true; |
||
| 769 | } |
||
| 770 | |||
| 771 | $field_wrapper = self::$instance->render_field_wrapper( $fields ); |
||
| 772 | |||
| 773 | ob_start(); |
||
| 774 | ?> |
||
| 775 | <div class="give-repeatable-field-section" id="<?php echo "{$fields['id']}_field"; ?>" |
||
| 776 | data-group-numbering="<?php echo $group_numbering; ?>" data-close-tabs="<?php echo $close_tabs; ?>"> |
||
| 777 | <?php if ( ! empty( $fields['label'] ) ) : ?> |
||
| 778 | <p class="give-repeater-field-name"><?php echo $fields['label']; ?></p> |
||
| 779 | <?php endif; ?> |
||
| 780 | |||
| 781 | <?php if ( ! empty( $fields['description'] ) ) : ?> |
||
| 782 | <p class="give-repeater-field-description"><?php echo $fields['description']; ?></p> |
||
| 783 | <?php endif; ?> |
||
| 784 | |||
| 785 | <table class="give-repeatable-fields-section-wrapper" cellspacing="0"> |
||
| 786 | <tbody class="container"<?php echo " data-rf-row-count=\"{$fields_count}\""; ?>> |
||
| 787 | <!--Repeater field group template--> |
||
| 788 | <tr class="give-template give-row"> |
||
| 789 | <td class="give-repeater-field-wrap give-column" colspan="2"> |
||
| 790 | <div class="give-row-head give-move"> |
||
| 791 | <button type="button" class="give-toggle-btn"> |
||
| 792 | <span class="give-toggle-indicator"></span> |
||
| 793 | </button> |
||
| 794 | <span class="give-remove" title="<?php esc_html_e( 'Remove Group', 'give' ); ?>">-</span> |
||
| 795 | <h2> |
||
| 796 | <span data-header-title="<?php echo $header_title; ?>"><?php echo $header_title; ?></span> |
||
| 797 | </h2> |
||
| 798 | </div> |
||
| 799 | <div class="give-row-body"> |
||
| 800 | <?php |
||
| 801 | foreach ( $fields['fields'] as $field_id => $field ) : |
||
| 802 | $field['id'] = ! empty( $field['id'] ) ? $field['id'] : $field_id; |
||
| 803 | $field['repeat'] = true; |
||
| 804 | $field['repeater_template'] = true; |
||
| 805 | |||
| 806 | $field['repeater_field_name'] = self::get_repeater_field_name( $field, $fields ); |
||
| 807 | $field['field_attributes']['id'] = str_replace( array( '[', ']' ), array( '_', '', ), $field['repeater_field_name'] ); |
||
| 808 | |||
| 809 | echo self::render_tag( $field, $form ); |
||
| 810 | endforeach; |
||
| 811 | ?> |
||
| 812 | </div> |
||
| 813 | </td> |
||
| 814 | </tr> |
||
| 815 | |||
| 816 | <?php if ( ! empty( $repeater_field_values ) ) : ?> |
||
| 817 | <!--Stored repeater field group--> |
||
| 818 | <?php foreach ( $repeater_field_values as $index => $field_group ) : ?> |
||
| 819 | <tr class="give-row"> |
||
| 820 | <td class="give-repeater-field-wrap give-column" colspan="2"> |
||
| 821 | <div class="give-row-head give-move"> |
||
| 822 | <button type="button" class="give-toggle-btn"> |
||
| 823 | <span class="give-toggle-indicator"></span> |
||
| 824 | </button> |
||
| 825 | <sapn class="give-remove" title="<?php esc_html_e( 'Remove Group', 'give' ); ?>">- |
||
| 826 | </sapn> |
||
| 827 | <h2> |
||
| 828 | <span data-header-title="<?php echo $header_title; ?>"><?php echo $header_title; ?></span> |
||
| 829 | </h2> |
||
| 830 | </div> |
||
| 831 | <div class="give-row-body"> |
||
| 832 | <?php |
||
| 833 | foreach ( $fields['fields'] as $field_id => $field ) : |
||
| 834 | $field['id'] = ! empty( $field['id'] ) ? $field['id'] : $field_id; |
||
| 835 | $field['repeat'] = true; |
||
| 836 | |||
| 837 | $field['repeater_field_name'] = self::get_repeater_field_name( $field, $fields, $index ); |
||
| 838 | $field['value'] = self::get_repeater_field_value( $field, $field_group, $fields ); |
||
| 839 | $field['field_attributes']['id'] = str_replace( array( '[', ']' ), array( '_', '', ), $field['repeater_field_name'] ); |
||
| 840 | |||
| 841 | echo self::render_tag( $field, $form ); |
||
| 842 | endforeach; |
||
| 843 | ?> |
||
| 844 | </div> |
||
| 845 | </td> |
||
| 846 | </tr> |
||
| 847 | <?php endforeach; ?> |
||
| 848 | |||
| 849 | <?php elseif ( $add_default_donation_field ) : ?> |
||
| 850 | <!--Default repeater field group--> |
||
| 851 | <tr class="give-row"> |
||
| 852 | <td class="give-repeater-field-wrap give-column" colspan="2"> |
||
| 853 | <div class="give-row-head give-move"> |
||
| 854 | <button type="button" class="give-toggle-btn"> |
||
| 855 | <span class="give-toggle-indicator"></span> |
||
| 856 | </button> |
||
| 857 | <sapn class="give-remove" title="<?php esc_html_e( 'Remove Group', 'give' ); ?>">- |
||
| 858 | </sapn> |
||
| 859 | <h2> |
||
| 860 | <span data-header-title="<?php echo $header_title; ?>"><?php echo $header_title; ?></span> |
||
| 861 | </h2> |
||
| 862 | </div> |
||
| 863 | <div class="give-row-body"> |
||
| 864 | <?php |
||
| 865 | foreach ( $fields['fields'] as $field_id => $field ) : |
||
| 866 | $field['id'] = ! empty( $field['id'] ) ? $field['id'] : $field_id; |
||
| 867 | $field['repeat'] = true; |
||
| 868 | $field['repeater_default_template'] = true; |
||
| 869 | |||
| 870 | $field['repeater_field_name'] = self::get_repeater_field_name( $field, $fields, 0 ); |
||
| 871 | $field['field_attributes']['id'] = str_replace( array( '[', ']' ), array( '_', '', ), $field['repeater_field_name'] ); |
||
| 872 | |||
| 873 | echo self::render_tag( $field, $form ); |
||
| 874 | endforeach; |
||
| 875 | ?> |
||
| 876 | </div> |
||
| 877 | </td> |
||
| 878 | </tr> |
||
| 879 | <?php endif; ?> |
||
| 880 | </tbody> |
||
| 881 | <tfoot> |
||
| 882 | <tr> |
||
| 883 | <?php |
||
| 884 | $add_row_btn_title = isset( $fields['options']['add_button'] ) |
||
| 885 | ? $add_row_btn_title = $fields['options']['add_button'] |
||
| 886 | : esc_html__( 'Add Row', 'give' ); |
||
| 887 | ?> |
||
| 888 | <td colspan="2" class="give-add-repeater-field-section-row-wrap"> |
||
| 889 | <button class="button button-primary give-add-repeater-field-section-row"><?php echo $add_row_btn_title; ?></button> |
||
| 890 | </td> |
||
| 891 | </tr> |
||
| 892 | </tfoot> |
||
| 893 | </table> |
||
| 894 | </div> |
||
| 895 | <?php |
||
| 896 | |||
| 897 | return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper ); |
||
| 898 | } |
||
| 899 | |||
| 900 | |||
| 901 | /** |
||
| 902 | * Render wrapper |
||
| 903 | * |
||
| 904 | * @since 1.9 |
||
| 905 | * @access private |
||
| 906 | * |
||
| 907 | * @param array $field |
||
| 908 | * @param array $form |
||
| 909 | * @param array $args |
||
| 910 | * |
||
| 911 | * @return string |
||
| 912 | */ |
||
| 913 | public static function render_field_wrapper( $field, $form = null, $args = array() ) { |
||
| 914 | ob_start(); |
||
| 915 | |||
| 916 | // Label: before field. |
||
| 917 | if ( 'before' === $field['label_position'] ) { |
||
| 918 | echo self::$instance->render_label( $field ); |
||
| 919 | } |
||
| 920 | |||
| 921 | echo "{$field['before_field']}{{form_field}}{$field['after_field']}"; |
||
| 922 | |||
| 923 | // Label: before field. |
||
| 924 | if ( 'after' === $field['label_position'] ) { |
||
| 925 | echo self::$instance->render_label( $field ); |
||
| 926 | } |
||
| 927 | |||
| 928 | $field_with_label = ob_get_clean(); |
||
| 929 | |||
| 930 | ob_start(); |
||
| 931 | |||
| 932 | if ( $field['wrapper'] ) : |
||
| 933 | |||
| 934 | echo $field['before_field_wrapper']; |
||
| 935 | |||
| 936 | echo '<' . $field['wrapper_type'] . ' ' . self::$instance->get_attributes( $field['wrapper_attributes'] ) . '>'; |
||
| 937 | echo $field_with_label; |
||
| 938 | echo "</{$field['wrapper_type']}>"; |
||
| 939 | |||
| 940 | echo $field['after_field_wrapper']; |
||
| 941 | else : |
||
| 942 | echo $field_with_label; |
||
| 943 | endif; |
||
| 944 | |||
| 945 | return ob_get_clean(); |
||
| 946 | } |
||
| 947 | |||
| 948 | |||
| 949 | /** |
||
| 950 | * Render label |
||
| 951 | * |
||
| 952 | * @since 1.9 |
||
| 953 | * @access private |
||
| 954 | * |
||
| 955 | * @param array $field |
||
| 956 | * @param array $form |
||
| 957 | * @param array $args |
||
| 958 | * |
||
| 959 | * @return string |
||
| 960 | */ |
||
| 961 | private function render_label( $field, $form = null, $args = array() ) { |
||
| 962 | ob_start(); |
||
| 963 | ?> |
||
| 964 | <?php if ( ! empty( $field['label'] ) ) : ?> |
||
| 965 | <?php echo $field['before_field_label']; ?> |
||
| 966 | <label for="<?php echo $field['field_attributes']['id']; ?>" <?php echo self::get_attributes( $field['label_attributes'] ); ?>> |
||
| 967 | |||
| 968 | <?php echo $field['label']; ?> |
||
| 969 | |||
| 970 | <?php if ( $field['required'] ) : ?> |
||
| 971 | <span class="give-required-indicator">*</span> |
||
| 972 | <?php endif; ?> |
||
| 973 | |||
| 974 | <?php if ( $field['label_tooltip'] ) : ?> |
||
| 975 | <span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php echo $field['label_tooltip'] ?>"></span> |
||
| 976 | <?php endif; ?> |
||
| 977 | </label> |
||
| 978 | <?php echo $field['after_field_label']; ?> |
||
| 979 | <?php endif; ?> |
||
| 980 | <?php |
||
| 981 | return ob_get_clean(); |
||
| 982 | } |
||
| 983 | |||
| 984 | /** |
||
| 985 | * Get field attribute string from field arguments. |
||
| 986 | * |
||
| 987 | * @since 1.9 |
||
| 988 | * @access private |
||
| 989 | * |
||
| 990 | * @param array $attributes |
||
| 991 | * |
||
| 992 | * @return array|string |
||
| 993 | */ |
||
| 994 | private function get_attributes( $attributes ) { |
||
| 995 | $field_attributes_val = ''; |
||
| 996 | |||
| 997 | if ( ! empty( $attributes ) ) { |
||
| 998 | foreach ( $attributes as $attribute_name => $attribute_val ) { |
||
| 999 | $field_attributes_val[] = "{$attribute_name}=\"{$attribute_val}\""; |
||
| 1000 | } |
||
| 1001 | } |
||
| 1002 | |||
| 1003 | if ( ! empty( $field_attributes_val ) ) { |
||
| 1004 | $field_attributes_val = implode( ' ', $field_attributes_val ); |
||
| 1005 | } |
||
| 1006 | |||
| 1007 | return $field_attributes_val; |
||
| 1008 | } |
||
| 1009 | |||
| 1010 | /** |
||
| 1011 | * Set default values for fields |
||
| 1012 | * |
||
| 1013 | * @since 1.0 |
||
| 1014 | * @access private |
||
| 1015 | * |
||
| 1016 | * @param array $field |
||
| 1017 | * @param array $form |
||
| 1018 | * @param array $args |
||
| 1019 | * |
||
| 1020 | * @return array |
||
| 1021 | */ |
||
| 1022 | public static function set_default_values( $field, $form = null, $args = array() ) { |
||
| 1131 | |||
| 1132 | |||
| 1133 | /** |
||
| 1134 | * Set responsive fields. |
||
| 1135 | * |
||
| 1136 | * @since 1.9 |
||
| 1137 | * @access private |
||
| 1138 | * |
||
| 1139 | * @param $form |
||
| 1140 | * |
||
| 1141 | * @return mixed |
||
| 1142 | */ |
||
| 1143 | private function set_responsive_field( &$form ) { |
||
| 1202 | |||
| 1203 | |||
| 1204 | /** |
||
| 1205 | * Check if current field is part of sub section or not. |
||
| 1206 | * |
||
| 1207 | * @since 1.9 |
||
| 1208 | * @access private |
||
| 1209 | * |
||
| 1210 | * @param $field |
||
| 1211 | * |
||
| 1212 | * @return bool |
||
| 1213 | */ |
||
| 1214 | private function is_sub_section( $field ) { |
||
| 1222 | |||
| 1223 | |||
| 1224 | /** |
||
| 1225 | * Get field type. |
||
| 1226 | * |
||
| 1227 | * @since 1.9 |
||
| 1228 | * @access private |
||
| 1229 | * |
||
| 1230 | * @param array $field |
||
| 1231 | * @param array $form |
||
| 1232 | * @param array $args |
||
| 1233 | * |
||
| 1234 | * @return bool |
||
| 1235 | */ |
||
| 1236 | public static function get_field_type( $field, $form = null, $args = array() ) { |
||
| 1255 | |||
| 1256 | /** |
||
| 1257 | * Get field name. |
||
| 1258 | * |
||
| 1259 | * @since 1.9 |
||
| 1260 | * |
||
| 1261 | * @param array $field |
||
| 1262 | * @param array $form |
||
| 1263 | * @param array $args |
||
| 1264 | * |
||
| 1265 | * @return string |
||
| 1266 | */ |
||
| 1267 | public static function get_field_name( $field, $form = null, $args = array() ) { |
||
| 1281 | |||
| 1282 | /** |
||
| 1283 | * Get repeater field id. |
||
| 1284 | * |
||
| 1285 | * @since 1.9 |
||
| 1286 | * |
||
| 1287 | * @param array $field |
||
| 1288 | * @param array $fields |
||
| 1289 | * @param int|bool $default |
||
| 1290 | * |
||
| 1291 | * @return string |
||
| 1292 | */ |
||
| 1293 | public static function get_repeater_field_name( $field, $fields , $default = false ) { |
||
| 1319 | |||
| 1320 | |||
| 1321 | /** |
||
| 1322 | * Get repeater field value. |
||
| 1323 | * |
||
| 1324 | * @since 1.9 |
||
| 1325 | * @access public |
||
| 1326 | * |
||
| 1327 | * @param array $field |
||
| 1328 | * @param array $field_value_group |
||
| 1329 | * @param array $fields |
||
| 1330 | * |
||
| 1331 | * @return mixed |
||
| 1332 | */ |
||
| 1333 | public static function get_repeater_field_value( $field, $field_value_group, $fields ) { |
||
| 1356 | } |
||
| 1357 | |||
| 1358 | // @todo implement required and aria-required for all form fields. required should be custom tag because we want custom validation for form instead html5. |
||
| 1359 |