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 | // Add label, before and after field. |
||
| 40 | 'label' => '', |
||
| 41 | 'label_position' => 'before', |
||
| 42 | |||
| 43 | // Add description to field as tooltip. |
||
| 44 | 'tooltip' => '', |
||
| 45 | |||
| 46 | // Show multiple fields in same row with in sub section. |
||
| 47 | 'sub_section_start' => false, |
||
| 48 | 'sub_section_end' => false, |
||
| 49 | |||
| 50 | // Add custom attributes. |
||
| 51 | 'field_attributes' => array(), |
||
| 52 | 'wrapper_attributes' => array(), |
||
| 53 | |||
| 54 | // Show/Hide field in before/after modal view. |
||
| 55 | 'show_without_modal' => false, |
||
| 56 | 'show_within_modal' => true, |
||
| 57 | |||
| 58 | // Params to edit field html. |
||
| 59 | 'before_field' => '', |
||
| 60 | 'after_field' => '', |
||
| 61 | 'before_field_wrapper' => '', |
||
| 62 | 'after_field_wrapper' => '', |
||
| 63 | 'before_label' => '', |
||
| 64 | 'after_label' => '', |
||
| 65 | |||
| 66 | // Manually render field. |
||
| 67 | 'callback' => '', |
||
| 68 | |||
| 69 | ); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The defaults for all sections. |
||
| 73 | * |
||
| 74 | * @since 1.9 |
||
| 75 | * @access static |
||
| 76 | */ |
||
| 77 | static $section_defaults = array( |
||
| 78 | 'label' => '', |
||
| 79 | 'name' => '', |
||
| 80 | 'section_attributes' => array(), |
||
| 81 | |||
| 82 | // Manually render section. |
||
| 83 | 'callback' => '', |
||
| 84 | ); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The defaults for all blocks. |
||
| 88 | * |
||
| 89 | * @since 1.9 |
||
| 90 | * @access static |
||
| 91 | */ |
||
| 92 | static $block_defaults = array( |
||
| 93 | 'label' => '', |
||
| 94 | 'name' => '', |
||
| 95 | 'block_attributes' => array(), |
||
| 96 | |||
| 97 | // Manually render section. |
||
| 98 | 'callback' => '', |
||
| 99 | ); |
||
| 100 | |||
| 101 | |||
| 102 | private function __construct() { |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * Get instance. |
||
| 108 | * |
||
| 109 | * @return static |
||
| 110 | */ |
||
| 111 | public static function get_instance() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Initialize this module |
||
| 121 | * |
||
| 122 | * @since 1.9 |
||
| 123 | * @access static |
||
| 124 | */ |
||
| 125 | public function init() { |
||
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * Render custom field. |
||
| 132 | * |
||
| 133 | * @since 1.0 |
||
| 134 | * @access private |
||
| 135 | * |
||
| 136 | * @param array $field |
||
| 137 | * |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | private function render_custom_field( $field ) { |
||
| 156 | |||
| 157 | |||
| 158 | /** |
||
| 159 | * Render `{{form_fields}}` tag. |
||
| 160 | * |
||
| 161 | * @since 1.9 |
||
| 162 | * @access private |
||
| 163 | * |
||
| 164 | * @param string $form_html |
||
| 165 | * @param array $form |
||
| 166 | * |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | public function render_tags( $form_html, $form ) { |
||
| 215 | |||
| 216 | |||
| 217 | /** |
||
| 218 | * Render section. |
||
| 219 | * |
||
| 220 | * @since 1.9 |
||
| 221 | * @access public |
||
| 222 | * |
||
| 223 | * @param array $section |
||
| 224 | * @param array $form |
||
| 225 | * |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | public static function render_section( $section, $form = null ) { |
||
| 247 | |||
| 248 | |||
| 249 | /** |
||
| 250 | * Render block. |
||
| 251 | * |
||
| 252 | * @since 1.9 |
||
| 253 | * @access public |
||
| 254 | * |
||
| 255 | * @param array $section |
||
| 256 | * @param array $form |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | public static function render_block( $section, $form = null ) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Render tag |
||
| 279 | * |
||
| 280 | * @since 1.9 |
||
| 281 | * @access public |
||
| 282 | * |
||
| 283 | * @param $field |
||
| 284 | * @param $form |
||
| 285 | * |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public static function render_tag( $field, $form = null ) { |
||
| 300 | |||
| 301 | |||
| 302 | /** |
||
| 303 | * Render text field. |
||
| 304 | * |
||
| 305 | * @since 1.9 |
||
| 306 | * @access private |
||
| 307 | * |
||
| 308 | * @param array $field |
||
| 309 | * |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | public static function render_text_field( $field ) { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Render submit field. |
||
| 330 | * |
||
| 331 | * @since 1.9 |
||
| 332 | * @access private |
||
| 333 | * |
||
| 334 | * @param array $field |
||
| 335 | * |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | public static function render_submit_field( $field ) { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Render checkbox field. |
||
| 344 | * |
||
| 345 | * @since 1.9 |
||
| 346 | * @access private |
||
| 347 | * |
||
| 348 | * @param array $field |
||
| 349 | * |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | public static function render_checkbox_field( $field ) { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Render email field. |
||
| 358 | * |
||
| 359 | * @since 1.9 |
||
| 360 | * @access private |
||
| 361 | * |
||
| 362 | * @param array $field |
||
| 363 | * |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | public static function render_email_field( $field ) { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Render number field. |
||
| 372 | * |
||
| 373 | * @since 1.9 |
||
| 374 | * @access private |
||
| 375 | * |
||
| 376 | * @param array $field |
||
| 377 | * |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | public static function render_number_field( $field ) { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Render password field. |
||
| 386 | * |
||
| 387 | * @since 1.9 |
||
| 388 | * @access private |
||
| 389 | * |
||
| 390 | * @param array $field |
||
| 391 | * |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | public static function render_password_field( $field ) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Render textarea field. |
||
| 400 | * |
||
| 401 | * @since 1.9 |
||
| 402 | * @access private |
||
| 403 | * |
||
| 404 | * @param array $field |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | public static function render_textarea_field( $field ) { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Render select field. |
||
| 426 | * |
||
| 427 | * @since 1.9 |
||
| 428 | * @access private |
||
| 429 | * |
||
| 430 | * @param array $field |
||
| 431 | * |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | public static function render_select_field( $field ) { |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Render multi select field. |
||
| 456 | * |
||
| 457 | * @since 1.9 |
||
| 458 | * @access private |
||
| 459 | * |
||
| 460 | * @param array $field |
||
| 461 | * |
||
| 462 | * @return string |
||
| 463 | */ |
||
| 464 | public static function render_multi_select_field( $field ) { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Render text field. |
||
| 473 | * |
||
| 474 | * @since 1.9 |
||
| 475 | * @access private |
||
| 476 | * |
||
| 477 | * @param array $field |
||
| 478 | * |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | public static function render_radio_field( $field ) { |
||
| 499 | |||
| 500 | |||
| 501 | /** |
||
| 502 | * Render wrapper |
||
| 503 | * |
||
| 504 | * @since 1.9 |
||
| 505 | * @access private |
||
| 506 | * |
||
| 507 | * @param $field |
||
| 508 | * |
||
| 509 | * @return string |
||
| 510 | */ |
||
| 511 | private function render_field_wrapper( $field ) { |
||
| 542 | |||
| 543 | |||
| 544 | /** |
||
| 545 | * Render label |
||
| 546 | * |
||
| 547 | * @since 1.9 |
||
| 548 | * @access private |
||
| 549 | * |
||
| 550 | * @param $field |
||
| 551 | * |
||
| 552 | * @return string |
||
| 553 | */ |
||
| 554 | private function render_label( $field ) { |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Get field attribute string from field arguments. |
||
| 579 | * |
||
| 580 | * @since 1.9 |
||
| 581 | * @access private |
||
| 582 | * |
||
| 583 | * @param array $attributes |
||
| 584 | * |
||
| 585 | * @return array|string |
||
| 586 | */ |
||
| 587 | private function get_attributes( $attributes ) { |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Set default values for fields |
||
| 605 | * |
||
| 606 | * @since 1.0 |
||
| 607 | * @access private |
||
| 608 | * |
||
| 609 | * @param array $field |
||
| 610 | * @param array $form |
||
| 611 | * @param bool $fire_filter |
||
| 612 | * |
||
| 613 | * @return array |
||
| 614 | */ |
||
| 615 | private function set_default_values( $field, $form = null, $fire_filter = true ) { |
||
| 681 | |||
| 682 | |||
| 683 | /** |
||
| 684 | * Set responsive fields. |
||
| 685 | * |
||
| 686 | * @since 1.9 |
||
| 687 | * @access private |
||
| 688 | * |
||
| 689 | * @param $form |
||
| 690 | * |
||
| 691 | * @return mixed |
||
| 692 | */ |
||
| 693 | private function set_responsive_field( &$form ) { |
||
| 752 | |||
| 753 | |||
| 754 | /** |
||
| 755 | * Check if current field is part of sub section or not. |
||
| 756 | * |
||
| 757 | * @since 1.9 |
||
| 758 | * @access private |
||
| 759 | * |
||
| 760 | * @param $field |
||
| 761 | * |
||
| 762 | * @return bool |
||
| 763 | */ |
||
| 764 | private function is_sub_section( $field ) { |
||
| 772 | |||
| 773 | |||
| 774 | /** |
||
| 775 | * Get field type. |
||
| 776 | * |
||
| 777 | * @since 1.9 |
||
| 778 | * @access private |
||
| 779 | * |
||
| 780 | * @param $field |
||
| 781 | * |
||
| 782 | * @return bool |
||
| 783 | */ |
||
| 784 | private function get_field_type( $field ) { |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Is the element a button? |
||
| 803 | * |
||
| 804 | * @since 1.9 |
||
| 805 | * @access static |
||
| 806 | * |
||
| 807 | * @param array $element |
||
| 808 | * |
||
| 809 | * @return bool |
||
| 810 | */ |
||
| 811 | static function is_button( $element ) { |
||
| 814 | } |
||
| 815 | // @todo auto fill field values. |
||
| 816 | // @todo set clearfix class for section also |