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 textarea field. |
||
| 468 | * |
||
| 469 | * @since 1.9 |
||
| 470 | * @access private |
||
| 471 | * |
||
| 472 | * @param array $field |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | public static function render_textarea_field( $field ) { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Render select field. |
||
| 494 | * |
||
| 495 | * @since 1.9 |
||
| 496 | * @access private |
||
| 497 | * |
||
| 498 | * @param array $field |
||
| 499 | * |
||
| 500 | * @return string |
||
| 501 | */ |
||
| 502 | public static function render_select_field( $field ) { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Render multi select field. |
||
| 535 | * |
||
| 536 | * @since 1.9 |
||
| 537 | * @access private |
||
| 538 | * |
||
| 539 | * @param array $field |
||
| 540 | * |
||
| 541 | * @return string |
||
| 542 | */ |
||
| 543 | public static function render_multi_select_field( $field ) { |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Render text field. |
||
| 552 | * |
||
| 553 | * @since 1.9 |
||
| 554 | * @access private |
||
| 555 | * |
||
| 556 | * @param array $field |
||
| 557 | * |
||
| 558 | * @return string |
||
| 559 | */ |
||
| 560 | public static function render_radio_field( $field ) { |
||
| 585 | |||
| 586 | |||
| 587 | /** |
||
| 588 | * Render wrapper |
||
| 589 | * |
||
| 590 | * @since 1.9 |
||
| 591 | * @access private |
||
| 592 | * |
||
| 593 | * @param $field |
||
| 594 | * |
||
| 595 | * @return string |
||
| 596 | */ |
||
| 597 | private function render_field_wrapper( $field ) { |
||
| 627 | |||
| 628 | |||
| 629 | /** |
||
| 630 | * Render label |
||
| 631 | * |
||
| 632 | * @since 1.9 |
||
| 633 | * @access private |
||
| 634 | * |
||
| 635 | * @param $field |
||
| 636 | * |
||
| 637 | * @return string |
||
| 638 | */ |
||
| 639 | private function render_label( $field ) { |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Get field attribute string from field arguments. |
||
| 664 | * |
||
| 665 | * @since 1.9 |
||
| 666 | * @access private |
||
| 667 | * |
||
| 668 | * @param array $attributes |
||
| 669 | * |
||
| 670 | * @return array|string |
||
| 671 | */ |
||
| 672 | private function get_attributes( $attributes ) { |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Set default values for fields |
||
| 690 | * |
||
| 691 | * @since 1.0 |
||
| 692 | * @access private |
||
| 693 | * |
||
| 694 | * @param array $field |
||
| 695 | * @param array $form |
||
| 696 | * @param bool $fire_filter |
||
| 697 | * |
||
| 698 | * @return array |
||
| 699 | */ |
||
| 700 | private function set_default_values( $field, $form = null, $fire_filter = true ) { |
||
| 794 | |||
| 795 | |||
| 796 | /** |
||
| 797 | * Set responsive fields. |
||
| 798 | * |
||
| 799 | * @since 1.9 |
||
| 800 | * @access private |
||
| 801 | * |
||
| 802 | * @param $form |
||
| 803 | * |
||
| 804 | * @return mixed |
||
| 805 | */ |
||
| 806 | private function set_responsive_field( &$form ) { |
||
| 865 | |||
| 866 | |||
| 867 | /** |
||
| 868 | * Check if current field is part of sub section or not. |
||
| 869 | * |
||
| 870 | * @since 1.9 |
||
| 871 | * @access private |
||
| 872 | * |
||
| 873 | * @param $field |
||
| 874 | * |
||
| 875 | * @return bool |
||
| 876 | */ |
||
| 877 | private function is_sub_section( $field ) { |
||
| 885 | |||
| 886 | |||
| 887 | /** |
||
| 888 | * Get field type. |
||
| 889 | * |
||
| 890 | * @since 1.9 |
||
| 891 | * @access private |
||
| 892 | * |
||
| 893 | * @param $field |
||
| 894 | * |
||
| 895 | * @return bool |
||
| 896 | */ |
||
| 897 | public static function get_field_type( $field ) { |
||
| 913 | |||
| 914 | /** |
||
| 915 | * Is the element a button? |
||
| 916 | * |
||
| 917 | * @since 1.9 |
||
| 918 | * @access static |
||
| 919 | * |
||
| 920 | * @param array $element |
||
| 921 | * |
||
| 922 | * @return bool |
||
| 923 | */ |
||
| 924 | static function is_button( $element ) { |
||
| 927 | } |