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 | |||
| 42 | // Add label, before and after field. |
||
| 43 | 'label' => '', |
||
| 44 | 'label_position' => 'before', |
||
| 45 | |||
| 46 | // Add description to field as tooltip. |
||
| 47 | 'tooltip' => '', |
||
| 48 | |||
| 49 | // Show multiple fields in same row with in sub section. |
||
| 50 | 'sub_section_start' => false, |
||
| 51 | 'sub_section_end' => false, |
||
| 52 | |||
| 53 | // Add custom attributes. |
||
| 54 | 'field_attributes' => array(), |
||
| 55 | 'wrapper_attributes' => array(), |
||
| 56 | |||
| 57 | // Show/Hide field in before/after modal view. |
||
| 58 | 'show_without_modal' => false, |
||
| 59 | 'show_within_modal' => true, |
||
| 60 | |||
| 61 | // Params to edit field html. |
||
| 62 | 'before_field' => '', |
||
| 63 | 'after_field' => '', |
||
| 64 | 'before_field_wrapper' => '', |
||
| 65 | 'after_field_wrapper' => '', |
||
| 66 | 'before_label' => '', |
||
| 67 | 'after_label' => '', |
||
| 68 | |||
| 69 | // Manually render field. |
||
| 70 | 'callback' => '', |
||
| 71 | |||
| 72 | ); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The defaults for all sections. |
||
| 76 | * |
||
| 77 | * @since 1.9 |
||
| 78 | * @access static |
||
| 79 | */ |
||
| 80 | static $section_defaults = array( |
||
| 81 | 'type' => 'section', |
||
| 82 | 'label' => '', |
||
| 83 | 'name' => '', |
||
| 84 | 'section_attributes' => array(), |
||
| 85 | |||
| 86 | // Manually render section. |
||
| 87 | 'callback' => '', |
||
| 88 | ); |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The defaults for all blocks. |
||
| 92 | * |
||
| 93 | * @since 1.9 |
||
| 94 | * @access static |
||
| 95 | */ |
||
| 96 | static $block_defaults = array( |
||
| 97 | 'type' => 'block', |
||
| 98 | 'label' => '', |
||
| 99 | 'name' => '', |
||
| 100 | 'block_attributes' => array(), |
||
| 101 | |||
| 102 | // Manually render section. |
||
| 103 | 'callback' => '', |
||
| 104 | ); |
||
| 105 | |||
| 106 | |||
| 107 | private function __construct() { |
||
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * Get instance. |
||
| 113 | * |
||
| 114 | * @return static |
||
| 115 | */ |
||
| 116 | public static function get_instance() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Initialize this module |
||
| 126 | * |
||
| 127 | * @since 1.9 |
||
| 128 | * @access static |
||
| 129 | */ |
||
| 130 | public function init() { |
||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * Render custom field. |
||
| 137 | * |
||
| 138 | * @since 1.0 |
||
| 139 | * @access private |
||
| 140 | * |
||
| 141 | * @param array $field |
||
| 142 | * |
||
| 143 | * @return bool |
||
| 144 | */ |
||
| 145 | private function render_custom_field( $field ) { |
||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * Render `{{form_fields}}` tag. |
||
| 165 | * |
||
| 166 | * @since 1.9 |
||
| 167 | * @access private |
||
| 168 | * |
||
| 169 | * @param string $form_html |
||
| 170 | * @param array $form |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | public function render_tags( $form_html, $form ) { |
||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * Render section. |
||
| 224 | * |
||
| 225 | * @since 1.9 |
||
| 226 | * @access public |
||
| 227 | * |
||
| 228 | * @param array $section |
||
| 229 | * @param array $form |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public static function render_section( $section, $form = null ) { |
||
| 252 | |||
| 253 | |||
| 254 | /** |
||
| 255 | * Render block. |
||
| 256 | * |
||
| 257 | * @since 1.9 |
||
| 258 | * @access public |
||
| 259 | * |
||
| 260 | * @param array $section |
||
| 261 | * @param array $form |
||
| 262 | * |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public static function render_block( $section, $form = null ) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Render tag |
||
| 284 | * |
||
| 285 | * @since 1.9 |
||
| 286 | * @access public |
||
| 287 | * |
||
| 288 | * @param $field |
||
| 289 | * @param $form |
||
| 290 | * |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | public static function render_tag( $field, $form = null ) { |
||
| 305 | |||
| 306 | |||
| 307 | /** |
||
| 308 | * Render text field. |
||
| 309 | * |
||
| 310 | * @since 1.9 |
||
| 311 | * @access private |
||
| 312 | * |
||
| 313 | * @param array $field |
||
| 314 | * |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | public static function render_text_field( $field ) { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Render submit field. |
||
| 335 | * |
||
| 336 | * @since 1.9 |
||
| 337 | * @access private |
||
| 338 | * |
||
| 339 | * @param array $field |
||
| 340 | * |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | public static function render_submit_field( $field ) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Render checkbox field. |
||
| 349 | * |
||
| 350 | * @since 1.9 |
||
| 351 | * @access private |
||
| 352 | * |
||
| 353 | * @param array $field |
||
| 354 | * |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | public static function render_checkbox_field( $field ) { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Render email field. |
||
| 363 | * |
||
| 364 | * @since 1.9 |
||
| 365 | * @access private |
||
| 366 | * |
||
| 367 | * @param array $field |
||
| 368 | * |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | public static function render_email_field( $field ) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Render number field. |
||
| 377 | * |
||
| 378 | * @since 1.9 |
||
| 379 | * @access private |
||
| 380 | * |
||
| 381 | * @param array $field |
||
| 382 | * |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | public static function render_number_field( $field ) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Render password field. |
||
| 391 | * |
||
| 392 | * @since 1.9 |
||
| 393 | * @access private |
||
| 394 | * |
||
| 395 | * @param array $field |
||
| 396 | * |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | public static function render_password_field( $field ) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Render button field. |
||
| 405 | * |
||
| 406 | * @since 1.9 |
||
| 407 | * @access private |
||
| 408 | * |
||
| 409 | * @param array $field |
||
| 410 | * |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | public static function render_button_field( $field ) { |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Render textarea field. |
||
| 419 | * |
||
| 420 | * @since 1.9 |
||
| 421 | * @access private |
||
| 422 | * |
||
| 423 | * @param array $field |
||
| 424 | * |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | public static function render_textarea_field( $field ) { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Render select field. |
||
| 445 | * |
||
| 446 | * @since 1.9 |
||
| 447 | * @access private |
||
| 448 | * |
||
| 449 | * @param array $field |
||
| 450 | * |
||
| 451 | * @return string |
||
| 452 | */ |
||
| 453 | public static function render_select_field( $field ) { |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Render multi select field. |
||
| 475 | * |
||
| 476 | * @since 1.9 |
||
| 477 | * @access private |
||
| 478 | * |
||
| 479 | * @param array $field |
||
| 480 | * |
||
| 481 | * @return string |
||
| 482 | */ |
||
| 483 | public static function render_multi_select_field( $field ) { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Render text field. |
||
| 492 | * |
||
| 493 | * @since 1.9 |
||
| 494 | * @access private |
||
| 495 | * |
||
| 496 | * @param array $field |
||
| 497 | * |
||
| 498 | * @return string |
||
| 499 | */ |
||
| 500 | public static function render_radio_field( $field ) { |
||
| 518 | |||
| 519 | |||
| 520 | /** |
||
| 521 | * Render wrapper |
||
| 522 | * |
||
| 523 | * @since 1.9 |
||
| 524 | * @access private |
||
| 525 | * |
||
| 526 | * @param $field |
||
| 527 | * |
||
| 528 | * @return string |
||
| 529 | */ |
||
| 530 | private function render_field_wrapper( $field ) { |
||
| 560 | |||
| 561 | |||
| 562 | /** |
||
| 563 | * Render label |
||
| 564 | * |
||
| 565 | * @since 1.9 |
||
| 566 | * @access private |
||
| 567 | * |
||
| 568 | * @param $field |
||
| 569 | * |
||
| 570 | * @return string |
||
| 571 | */ |
||
| 572 | private function render_label( $field ) { |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Get field attribute string from field arguments. |
||
| 597 | * |
||
| 598 | * @since 1.9 |
||
| 599 | * @access private |
||
| 600 | * |
||
| 601 | * @param array $attributes |
||
| 602 | * |
||
| 603 | * @return array|string |
||
| 604 | */ |
||
| 605 | private function get_attributes( $attributes ) { |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Set default values for fields |
||
| 623 | * |
||
| 624 | * @since 1.0 |
||
| 625 | * @access private |
||
| 626 | * |
||
| 627 | * @param array $field |
||
| 628 | * @param array $form |
||
| 629 | * @param bool $fire_filter |
||
| 630 | * |
||
| 631 | * @return array |
||
| 632 | */ |
||
| 633 | private function set_default_values( $field, $form = null, $fire_filter = true ) { |
||
| 717 | |||
| 718 | |||
| 719 | /** |
||
| 720 | * Set responsive fields. |
||
| 721 | * |
||
| 722 | * @since 1.9 |
||
| 723 | * @access private |
||
| 724 | * |
||
| 725 | * @param $form |
||
| 726 | * |
||
| 727 | * @return mixed |
||
| 728 | */ |
||
| 729 | private function set_responsive_field( &$form ) { |
||
| 788 | |||
| 789 | |||
| 790 | /** |
||
| 791 | * Check if current field is part of sub section or not. |
||
| 792 | * |
||
| 793 | * @since 1.9 |
||
| 794 | * @access private |
||
| 795 | * |
||
| 796 | * @param $field |
||
| 797 | * |
||
| 798 | * @return bool |
||
| 799 | */ |
||
| 800 | private function is_sub_section( $field ) { |
||
| 808 | |||
| 809 | |||
| 810 | /** |
||
| 811 | * Get field type. |
||
| 812 | * |
||
| 813 | * @since 1.9 |
||
| 814 | * @access private |
||
| 815 | * |
||
| 816 | * @param $field |
||
| 817 | * |
||
| 818 | * @return bool |
||
| 819 | */ |
||
| 820 | private function get_field_type( $field ) { |
||
| 836 | |||
| 837 | /** |
||
| 838 | * Is the element a button? |
||
| 839 | * |
||
| 840 | * @since 1.9 |
||
| 841 | * @access static |
||
| 842 | * |
||
| 843 | * @param array $element |
||
| 844 | * |
||
| 845 | * @return bool |
||
| 846 | */ |
||
| 847 | static function is_button( $element ) { |
||
| 850 | } |
||
| 851 | // @todo auto fill field values. |
||
| 852 | // @todo set clearfix class for section also |