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 | 'name' => '', |
||
| 31 | 'data_type' => '', |
||
| 32 | 'value' => '', |
||
| 33 | 'required' => false, |
||
| 34 | 'options' => array(), |
||
| 35 | |||
| 36 | // Set default value to field. |
||
| 37 | 'default' => '', |
||
| 38 | |||
| 39 | // Field with wrapper. |
||
| 40 | 'wrapper' => true, |
||
| 41 | 'wrapper_type' => 'p', |
||
| 42 | |||
| 43 | // Add label, before and after field. |
||
| 44 | 'label' => '', |
||
| 45 | 'label_position' => 'before', |
||
| 46 | |||
| 47 | // Add description to field as tooltip. |
||
| 48 | 'tooltip' => '', |
||
| 49 | |||
| 50 | // Show multiple fields in same row with in sub section. |
||
| 51 | 'sub_section_start' => false, |
||
| 52 | 'sub_section_end' => false, |
||
| 53 | |||
| 54 | // Add custom attributes. |
||
| 55 | 'field_attributes' => array(), |
||
| 56 | 'wrapper_attributes' => array(), |
||
| 57 | |||
| 58 | // Show/Hide field in before/after modal view. |
||
| 59 | 'show_without_modal' => false, |
||
| 60 | 'show_within_modal' => true, |
||
| 61 | |||
| 62 | // Params to edit field html. |
||
| 63 | 'before_field' => '', |
||
| 64 | 'after_field' => '', |
||
| 65 | 'before_field_wrapper' => '', |
||
| 66 | 'after_field_wrapper' => '', |
||
| 67 | 'before_label' => '', |
||
| 68 | 'after_label' => '', |
||
| 69 | |||
| 70 | // Manually render field. |
||
| 71 | 'callback' => '', |
||
| 72 | |||
| 73 | ); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The defaults for all sections. |
||
| 77 | * |
||
| 78 | * @since 1.9 |
||
| 79 | * @access static |
||
| 80 | */ |
||
| 81 | static $section_defaults = array( |
||
| 82 | 'type' => 'section', |
||
| 83 | 'label' => '', |
||
| 84 | 'name' => '', |
||
| 85 | 'section_attributes' => array(), |
||
| 86 | |||
| 87 | // Manually render section. |
||
| 88 | 'callback' => '', |
||
| 89 | ); |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The defaults for all blocks. |
||
| 93 | * |
||
| 94 | * @since 1.9 |
||
| 95 | * @access static |
||
| 96 | */ |
||
| 97 | static $block_defaults = array( |
||
| 98 | 'type' => 'block', |
||
| 99 | 'label' => '', |
||
| 100 | 'name' => '', |
||
| 101 | 'block_attributes' => array(), |
||
| 102 | |||
| 103 | // Manually render section. |
||
| 104 | 'callback' => '', |
||
| 105 | ); |
||
| 106 | |||
| 107 | |||
| 108 | private function __construct() { |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * Get instance. |
||
| 114 | * |
||
| 115 | * @return static |
||
| 116 | */ |
||
| 117 | public static function get_instance() { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Initialize this module |
||
| 127 | * |
||
| 128 | * @since 1.9 |
||
| 129 | * @access static |
||
| 130 | */ |
||
| 131 | public function init() { |
||
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * Render custom field. |
||
| 138 | * |
||
| 139 | * @since 1.0 |
||
| 140 | * @access private |
||
| 141 | * |
||
| 142 | * @param array $field |
||
| 143 | * @param array $form |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | private function render_custom_field( $field, $form = null ) { |
||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * Render `{{form_fields}}` tag. |
||
| 169 | * |
||
| 170 | * @since 1.9 |
||
| 171 | * @access private |
||
| 172 | * |
||
| 173 | * @param string $form_html |
||
| 174 | * @param array $form |
||
| 175 | * |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | public function render_tags( $form_html, $form ) { |
||
| 222 | |||
| 223 | |||
| 224 | /** |
||
| 225 | * Render section. |
||
| 226 | * |
||
| 227 | * @since 1.9 |
||
| 228 | * @access public |
||
| 229 | * |
||
| 230 | * @param array $section |
||
| 231 | * @param array $form |
||
| 232 | * @param array $args Helper argument to render section. |
||
| 233 | * |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | public static function render_section( $section, $form = null, $args = array() ) { |
||
| 260 | |||
| 261 | |||
| 262 | /** |
||
| 263 | * Render block. |
||
| 264 | * |
||
| 265 | * @since 1.9 |
||
| 266 | * @access public |
||
| 267 | * |
||
| 268 | * @param array $block |
||
| 269 | * @param array $form |
||
| 270 | * @param array $args Helper argument to render section. |
||
| 271 | * |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public static function render_block( $block, $form = null, $args = array() ) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Render tag |
||
| 298 | * |
||
| 299 | * @since 1.9 |
||
| 300 | * @access public |
||
| 301 | * |
||
| 302 | * @param array $field |
||
| 303 | * @param array $form |
||
| 304 | * @param array $args Helper argument to render section. |
||
| 305 | * |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | public static function render_tag( $field, $form = null, $args = array() ) { |
||
| 344 | |||
| 345 | |||
| 346 | /** |
||
| 347 | * Render text field. |
||
| 348 | * |
||
| 349 | * @since 1.9 |
||
| 350 | * @access private |
||
| 351 | * |
||
| 352 | * @param array $field |
||
| 353 | * |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | public static function render_text_field( $field ) { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Render submit field. |
||
| 373 | * |
||
| 374 | * @since 1.9 |
||
| 375 | * @access private |
||
| 376 | * |
||
| 377 | * @param array $field |
||
| 378 | * |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | public static function render_submit_field( $field ) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Render checkbox field. |
||
| 387 | * |
||
| 388 | * @since 1.9 |
||
| 389 | * @access private |
||
| 390 | * |
||
| 391 | * @param array $field |
||
| 392 | * |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | public static function render_checkbox_field( $field ) { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Render email field. |
||
| 412 | * |
||
| 413 | * @since 1.9 |
||
| 414 | * @access private |
||
| 415 | * |
||
| 416 | * @param array $field |
||
| 417 | * |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | public static function render_email_field( $field ) { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Render number field. |
||
| 426 | * |
||
| 427 | * @since 1.9 |
||
| 428 | * @access private |
||
| 429 | * |
||
| 430 | * @param array $field |
||
| 431 | * |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | public static function render_number_field( $field ) { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Render password field. |
||
| 440 | * |
||
| 441 | * @since 1.9 |
||
| 442 | * @access private |
||
| 443 | * |
||
| 444 | * @param array $field |
||
| 445 | * |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | public static function render_password_field( $field ) { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Render button field. |
||
| 454 | * |
||
| 455 | * @since 1.9 |
||
| 456 | * @access private |
||
| 457 | * |
||
| 458 | * @param array $field |
||
| 459 | * |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | public static function render_button_field( $field ) { |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Render hidden field. |
||
| 468 | * |
||
| 469 | * @since 1.9 |
||
| 470 | * @access public |
||
| 471 | * |
||
| 472 | * @param array $field |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | public static function render_hidden_field( $field ) { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Render textarea field. |
||
| 484 | * |
||
| 485 | * @since 1.9 |
||
| 486 | * @access private |
||
| 487 | * |
||
| 488 | * @param array $field |
||
| 489 | * |
||
| 490 | * @return string |
||
| 491 | */ |
||
| 492 | public static function render_textarea_field( $field ) { |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Render select field. |
||
| 510 | * |
||
| 511 | * @since 1.9 |
||
| 512 | * @access private |
||
| 513 | * |
||
| 514 | * @param array $field |
||
| 515 | * |
||
| 516 | * @return string |
||
| 517 | */ |
||
| 518 | public static function render_select_field( $field ) { |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Render multi select field. |
||
| 551 | * |
||
| 552 | * @since 1.9 |
||
| 553 | * @access private |
||
| 554 | * |
||
| 555 | * @param array $field |
||
| 556 | * |
||
| 557 | * @return string |
||
| 558 | */ |
||
| 559 | public static function render_multi_select_field( $field ) { |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Render text field. |
||
| 568 | * |
||
| 569 | * @since 1.9 |
||
| 570 | * @access private |
||
| 571 | * |
||
| 572 | * @param array $field |
||
| 573 | * |
||
| 574 | * @return string |
||
| 575 | */ |
||
| 576 | public static function render_radio_field( $field ) { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Render multi checkbox field. |
||
| 604 | * |
||
| 605 | * @since 1.9 |
||
| 606 | * @access private |
||
| 607 | * |
||
| 608 | * @param array $field |
||
| 609 | * |
||
| 610 | * @return string |
||
| 611 | */ |
||
| 612 | public static function render_multi_checkbox_field( $field ) { |
||
| 640 | |||
| 641 | |||
| 642 | /** |
||
| 643 | * Render wrapper |
||
| 644 | * |
||
| 645 | * @since 1.9 |
||
| 646 | * @access private |
||
| 647 | * |
||
| 648 | * @param $field |
||
| 649 | * |
||
| 650 | * @return string |
||
| 651 | */ |
||
| 652 | private function render_field_wrapper( $field ) { |
||
| 682 | |||
| 683 | |||
| 684 | /** |
||
| 685 | * Render label |
||
| 686 | * |
||
| 687 | * @since 1.9 |
||
| 688 | * @access private |
||
| 689 | * |
||
| 690 | * @param $field |
||
| 691 | * |
||
| 692 | * @return string |
||
| 693 | */ |
||
| 694 | private function render_label( $field ) { |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Get field attribute string from field arguments. |
||
| 719 | * |
||
| 720 | * @since 1.9 |
||
| 721 | * @access private |
||
| 722 | * |
||
| 723 | * @param array $attributes |
||
| 724 | * |
||
| 725 | * @return array|string |
||
| 726 | */ |
||
| 727 | private function get_attributes( $attributes ) { |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Set default values for fields |
||
| 745 | * |
||
| 746 | * @since 1.0 |
||
| 747 | * @access private |
||
| 748 | * |
||
| 749 | * @param array $field |
||
| 750 | * @param array $form |
||
| 751 | * @param bool $fire_filter |
||
| 752 | * |
||
| 753 | * @return array |
||
| 754 | */ |
||
| 755 | private function set_default_values( $field, $form = null, $fire_filter = true ) { |
||
| 849 | |||
| 850 | |||
| 851 | /** |
||
| 852 | * Set responsive fields. |
||
| 853 | * |
||
| 854 | * @since 1.9 |
||
| 855 | * @access private |
||
| 856 | * |
||
| 857 | * @param $form |
||
| 858 | * |
||
| 859 | * @return mixed |
||
| 860 | */ |
||
| 861 | private function set_responsive_field( &$form ) { |
||
| 920 | |||
| 921 | |||
| 922 | /** |
||
| 923 | * Check if current field is part of sub section or not. |
||
| 924 | * |
||
| 925 | * @since 1.9 |
||
| 926 | * @access private |
||
| 927 | * |
||
| 928 | * @param $field |
||
| 929 | * |
||
| 930 | * @return bool |
||
| 931 | */ |
||
| 932 | private function is_sub_section( $field ) { |
||
| 940 | |||
| 941 | |||
| 942 | /** |
||
| 943 | * Get field type. |
||
| 944 | * |
||
| 945 | * @since 1.9 |
||
| 946 | * @access private |
||
| 947 | * |
||
| 948 | * @param $field |
||
| 949 | * |
||
| 950 | * @return bool |
||
| 951 | */ |
||
| 952 | public static function get_field_type( $field ) { |
||
| 968 | |||
| 969 | /** |
||
| 970 | * Is the element a button? |
||
| 971 | * |
||
| 972 | * @since 1.9 |
||
| 973 | * @access static |
||
| 974 | * |
||
| 975 | * @param array $element |
||
| 976 | * |
||
| 977 | * @return bool |
||
| 978 | */ |
||
| 979 | static function is_button( $element ) { |
||
| 982 | } |