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 elements |
||
| 73 | * |
||
| 74 | * @since 1.9 |
||
| 75 | * @access static |
||
| 76 | */ |
||
| 77 | static $section_defaults = array( |
||
| 78 | 'label' => '', |
||
| 79 | 'name' => '', |
||
| 80 | 'field_attributes' => array(), |
||
| 81 | |||
| 82 | // Manually render section. |
||
| 83 | 'callback' => '', |
||
| 84 | ); |
||
| 85 | |||
| 86 | |||
| 87 | private function __construct() { |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * Get instance. |
||
| 93 | * |
||
| 94 | * @return static |
||
| 95 | */ |
||
| 96 | public static function get_instance() { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Initialize this module |
||
| 106 | * |
||
| 107 | * @since 1.9 |
||
| 108 | * @access static |
||
| 109 | */ |
||
| 110 | public function init() { |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * Render custom field. |
||
| 117 | * |
||
| 118 | * @since 1.0 |
||
| 119 | * @access private |
||
| 120 | * |
||
| 121 | * @param array $field |
||
| 122 | * |
||
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | private function render_custom_field( $field ) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Render tag |
||
| 144 | * |
||
| 145 | * @since 1.9 |
||
| 146 | * @access public |
||
| 147 | * |
||
| 148 | * @param $field |
||
| 149 | * @param $form |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | public static function render_tag( $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 ) { |
||
| 229 | |||
| 230 | |||
| 231 | /** |
||
| 232 | * Render section. |
||
| 233 | * |
||
| 234 | * @since 1.9 |
||
| 235 | * @access public |
||
| 236 | * |
||
| 237 | * @param array $section |
||
| 238 | * @param array $form |
||
| 239 | * |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | public static function render_section( $section, $form = null ) { |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * Render text field. |
||
| 266 | * |
||
| 267 | * @since 1.9 |
||
| 268 | * @access private |
||
| 269 | * |
||
| 270 | * @param array $field |
||
| 271 | * |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public static function render_text_field( $field ) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Render submit field. |
||
| 292 | * |
||
| 293 | * @since 1.9 |
||
| 294 | * @access private |
||
| 295 | * |
||
| 296 | * @param array $field |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | public static function render_submit_field( $field ) { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Render checkbox field. |
||
| 306 | * |
||
| 307 | * @since 1.9 |
||
| 308 | * @access private |
||
| 309 | * |
||
| 310 | * @param array $field |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public static function render_checkbox_field( $field ) { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Render email field. |
||
| 320 | * |
||
| 321 | * @since 1.9 |
||
| 322 | * @access private |
||
| 323 | * |
||
| 324 | * @param array $field |
||
| 325 | * |
||
| 326 | * @return string |
||
| 327 | */ |
||
| 328 | public static function render_email_field( $field ) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Render number field. |
||
| 334 | * |
||
| 335 | * @since 1.9 |
||
| 336 | * @access private |
||
| 337 | * |
||
| 338 | * @param array $field |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | public static function render_number_field( $field ) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Render password field. |
||
| 348 | * |
||
| 349 | * @since 1.9 |
||
| 350 | * @access private |
||
| 351 | * |
||
| 352 | * @param array $field |
||
| 353 | * |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | public static function render_password_field( $field ) { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Render textarea field. |
||
| 362 | * |
||
| 363 | * @since 1.9 |
||
| 364 | * @access private |
||
| 365 | * |
||
| 366 | * @param array $field |
||
| 367 | * |
||
| 368 | * @return string |
||
| 369 | */ |
||
| 370 | public static function render_textarea_field( $field ) { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Render select field. |
||
| 388 | * |
||
| 389 | * @since 1.9 |
||
| 390 | * @access private |
||
| 391 | * |
||
| 392 | * @param array $field |
||
| 393 | * |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | public static function render_select_field( $field ) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Render multi select field. |
||
| 418 | * |
||
| 419 | * @since 1.9 |
||
| 420 | * @access private |
||
| 421 | * |
||
| 422 | * @param array $field |
||
| 423 | * |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | public static function render_multi_select_field( $field ) { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Render text field. |
||
| 435 | * |
||
| 436 | * @since 1.9 |
||
| 437 | * @access private |
||
| 438 | * |
||
| 439 | * @param array $field |
||
| 440 | * |
||
| 441 | * @return string |
||
| 442 | */ |
||
| 443 | public static function render_radio_field( $field ) { |
||
| 461 | |||
| 462 | |||
| 463 | /** |
||
| 464 | * Render wrapper |
||
| 465 | * |
||
| 466 | * @since 1.9 |
||
| 467 | * @access private |
||
| 468 | * |
||
| 469 | * @param $field |
||
| 470 | * |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | private function render_field_wrapper( $field ) { |
||
| 504 | |||
| 505 | |||
| 506 | /** |
||
| 507 | * Render label |
||
| 508 | * |
||
| 509 | * @since 1.9 |
||
| 510 | * @access private |
||
| 511 | * |
||
| 512 | * @param $field |
||
| 513 | * |
||
| 514 | * @return string |
||
| 515 | */ |
||
| 516 | private function render_label( $field ) { |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Get field attribute string from field arguments. |
||
| 541 | * |
||
| 542 | * @since 1.9 |
||
| 543 | * @access private |
||
| 544 | * |
||
| 545 | * @param array $attributes |
||
| 546 | * |
||
| 547 | * @return array|string |
||
| 548 | */ |
||
| 549 | private function get_attributes( $attributes ) { |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Get field attribute string from field arguments. |
||
| 567 | * |
||
| 568 | * @since 1.9 |
||
| 569 | * @access private |
||
| 570 | * |
||
| 571 | * @param $field |
||
| 572 | * |
||
| 573 | * @return array|string |
||
| 574 | */ |
||
| 575 | private function get_field_attributes( $field ) { |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Get row attribute string from field arguments. |
||
| 581 | * |
||
| 582 | * @since 1.9 |
||
| 583 | * @access private |
||
| 584 | * |
||
| 585 | * @param $field |
||
| 586 | * |
||
| 587 | * @return array|string |
||
| 588 | */ |
||
| 589 | private function get_row_attributes( $field ) { |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Set default values for fields |
||
| 595 | * |
||
| 596 | * @since 1.0 |
||
| 597 | * @access private |
||
| 598 | * |
||
| 599 | * @param array $field |
||
| 600 | * @param array $form |
||
| 601 | * |
||
| 602 | * @return array |
||
| 603 | */ |
||
| 604 | private function set_default_values( $field, $form = null ) { |
||
| 635 | |||
| 636 | |||
| 637 | /** |
||
| 638 | * Set responsive fields. |
||
| 639 | * |
||
| 640 | * @since 1.9 |
||
| 641 | * @access private |
||
| 642 | * |
||
| 643 | * @param $form |
||
| 644 | * |
||
| 645 | * @return mixed |
||
| 646 | */ |
||
| 647 | private function set_responsive_field( &$form ) { |
||
| 706 | |||
| 707 | |||
| 708 | /** |
||
| 709 | * Check if current feld is part of sub section or not. |
||
| 710 | * |
||
| 711 | * @since 1.9 |
||
| 712 | * @access private |
||
| 713 | * |
||
| 714 | * @param $field |
||
| 715 | * |
||
| 716 | * @return bool |
||
| 717 | */ |
||
| 718 | private function is_sub_section( $field ) { |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Is the element a button? |
||
| 732 | * |
||
| 733 | * @since 1.9 |
||
| 734 | * @access static |
||
| 735 | * |
||
| 736 | * @param array $element |
||
| 737 | * |
||
| 738 | * @return bool |
||
| 739 | */ |
||
| 740 | static function is_button( $element ) { |
||
| 743 | } |
||
| 744 | // @todo auto fill field values. |
||
| 745 | // @todo set clearfix class for section also |