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 | 'id' => '', |
||
| 31 | 'data_type' => '', |
||
| 32 | 'value' => '', |
||
| 33 | 'required' => false, |
||
| 34 | 'options' => array(), |
||
| 35 | |||
| 36 | // Set default value to field. |
||
| 37 | 'default' => '', |
||
| 38 | |||
| 39 | // Set checkbox value. |
||
| 40 | 'cbvalue' => 'on', |
||
| 41 | |||
| 42 | // Field with wrapper. |
||
| 43 | 'wrapper' => true, |
||
| 44 | 'wrapper_type' => 'p', |
||
| 45 | |||
| 46 | // Add label, before and after field. |
||
| 47 | 'label' => '', |
||
| 48 | 'label_position' => 'before', |
||
| 49 | |||
| 50 | // Add description to field as tooltip. |
||
| 51 | 'tooltip' => '', |
||
| 52 | |||
| 53 | // Show multiple fields in same row with in sub section. |
||
| 54 | 'sub_section_start' => false, |
||
| 55 | 'sub_section_end' => false, |
||
| 56 | |||
| 57 | // Add custom attributes. |
||
| 58 | 'field_attributes' => array(), |
||
| 59 | 'wrapper_attributes' => array(), |
||
| 60 | |||
| 61 | // Show/Hide field in before/after modal view. |
||
| 62 | 'show_without_modal' => false, |
||
| 63 | 'show_within_modal' => true, |
||
| 64 | |||
| 65 | // Params to edit field html. |
||
| 66 | 'before_field' => '', |
||
| 67 | 'after_field' => '', |
||
| 68 | 'before_field_wrapper' => '', |
||
| 69 | 'after_field_wrapper' => '', |
||
| 70 | 'before_label' => '', |
||
| 71 | 'after_label' => '', |
||
| 72 | |||
| 73 | // Manually render field. |
||
| 74 | 'callback' => '', |
||
| 75 | |||
| 76 | ); |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The defaults for all sections. |
||
| 80 | * |
||
| 81 | * @since 1.9 |
||
| 82 | * @access static |
||
| 83 | */ |
||
| 84 | static $section_defaults = array( |
||
| 85 | 'type' => 'section', |
||
| 86 | 'label' => '', |
||
| 87 | 'id' => '', |
||
| 88 | 'section_attributes' => array(), |
||
| 89 | |||
| 90 | // Manually render section. |
||
| 91 | 'callback' => '', |
||
| 92 | ); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The defaults for all blocks. |
||
| 96 | * |
||
| 97 | * @since 1.9 |
||
| 98 | * @access static |
||
| 99 | */ |
||
| 100 | static $block_defaults = array( |
||
| 101 | 'type' => 'block', |
||
| 102 | 'label' => '', |
||
| 103 | 'id' => '', |
||
| 104 | 'block_attributes' => array(), |
||
| 105 | |||
| 106 | // Manually render section. |
||
| 107 | 'callback' => '', |
||
| 108 | ); |
||
| 109 | |||
| 110 | |||
| 111 | private function __construct() { |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * Get instance. |
||
| 117 | * |
||
| 118 | * @return static |
||
| 119 | */ |
||
| 120 | public static function get_instance() { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Initialize this module |
||
| 130 | * |
||
| 131 | * @since 1.9 |
||
| 132 | * @access static |
||
| 133 | */ |
||
| 134 | public function init() { |
||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * Render custom field. |
||
| 141 | * |
||
| 142 | * @since 1.0 |
||
| 143 | * @access private |
||
| 144 | * |
||
| 145 | * @param array $field |
||
| 146 | * @param array $form |
||
| 147 | * |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | private function render_custom_field( $field, $form = null ) { |
||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * Render `{{form_fields}}` tag. |
||
| 172 | * |
||
| 173 | * @since 1.9 |
||
| 174 | * @access private |
||
| 175 | * |
||
| 176 | * @param string $form_html |
||
| 177 | * @param array $form |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function render_tags( $form_html, $form ) { |
||
| 182 | // Bailout: If form does not contain any field. |
||
| 183 | if ( empty( $form['fields'] ) ) { |
||
| 184 | str_replace( '{{form_fields}}', '', $form_html ); |
||
| 185 | |||
| 186 | return $form_html; |
||
| 187 | } |
||
| 188 | |||
| 189 | $fields_html = ''; |
||
| 190 | |||
| 191 | // Set responsive fields. |
||
| 192 | self::$instance->set_responsive_field( $form ); |
||
| 193 | |||
| 194 | // Render fields. |
||
| 195 | foreach ( $form['fields'] as $key => $field ) { |
||
| 196 | // Set default value. |
||
| 197 | $field['id'] = empty( $field['id'] ) ? $key : $field['id']; |
||
| 198 | |||
| 199 | // Render custom form with callback. |
||
| 200 | if ( $field_html = self::$instance->render_custom_field( $field, $form ) ) { |
||
| 201 | $fields_html .= $field_html; |
||
| 202 | } |
||
| 203 | |||
| 204 | switch ( true ) { |
||
| 205 | // Block. |
||
| 206 | case ( 'block' === self::get_field_type( $field ) ): |
||
| 207 | $fields_html .= self::$instance->render_block( $field, $form ); |
||
| 208 | break; |
||
| 209 | |||
| 210 | // Section. |
||
| 211 | case ( 'section' === self::get_field_type( $field ) ): |
||
| 212 | $fields_html .= self::$instance->render_section( $field, $form ); |
||
| 213 | break; |
||
| 214 | |||
| 215 | // Field |
||
| 216 | default: |
||
| 217 | $fields_html .= self::render_tag( $field, $form ); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | $form_html = str_replace( '{{form_fields}}', $fields_html, $form_html ); |
||
| 222 | |||
| 223 | return $form_html; |
||
| 224 | } |
||
| 225 | |||
| 226 | |||
| 227 | /** |
||
| 228 | * Render section. |
||
| 229 | * |
||
| 230 | * @since 1.9 |
||
| 231 | * @access public |
||
| 232 | * |
||
| 233 | * @param array $section |
||
| 234 | * @param array $form |
||
| 235 | * @param array $args Helper argument to render section. |
||
| 236 | * |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | public static function render_section( $section, $form = null, $args = array() ) { |
||
| 263 | |||
| 264 | |||
| 265 | /** |
||
| 266 | * Render block. |
||
| 267 | * |
||
| 268 | * @since 1.9 |
||
| 269 | * @access public |
||
| 270 | * |
||
| 271 | * @param array $block |
||
| 272 | * @param array $form |
||
| 273 | * @param array $args Helper argument to render section. |
||
| 274 | * |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | public static function render_block( $block, $form = null, $args = array() ) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Render tag |
||
| 301 | * |
||
| 302 | * @since 1.9 |
||
| 303 | * @access public |
||
| 304 | * |
||
| 305 | * @param array $field |
||
| 306 | * @param array $form |
||
| 307 | * @param array $args Helper argument to render section. |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | public static function render_tag( $field, $form = null, $args = array() ) { |
||
| 312 | // Enqueue scripts. |
||
| 313 | Give_Form_API::enqueue_scripts(); |
||
| 314 | |||
| 315 | // Set default values if necessary. |
||
| 316 | if ( ! isset( $args['set_default'] ) || (bool) $args['set_default'] ) { |
||
| 317 | $field = self::$instance->set_default_values( $field, $form ); |
||
| 318 | } |
||
| 319 | |||
| 320 | $field_html = ''; |
||
| 321 | $functions_name = "render_{$field['type']}_field"; |
||
| 322 | |||
| 323 | if ( 'section' === self::$instance->get_field_type( $field ) ) { |
||
| 324 | $field_html = self::$instance->render_section( $field, $form, array( 'set_default' => false ) ); |
||
| 325 | |||
| 326 | } elseif ( method_exists( self::$instance, $functions_name ) ) { |
||
| 327 | $field_html = self::$instance->{$functions_name}( $field ); |
||
| 328 | |||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Filter the custom field type html. |
||
| 333 | * Developer can use this hook to render custom field type. |
||
| 334 | * |
||
| 335 | * @since 1.9 |
||
| 336 | * |
||
| 337 | * @param string $field_html |
||
| 338 | * @param array $field |
||
| 339 | * @param array $form |
||
| 340 | */ |
||
| 341 | $field_html = apply_filters( |
||
| 342 | "give_field_api_render_{$field['type']}_field", |
||
| 343 | $field_html, |
||
| 344 | $field, |
||
| 345 | $form |
||
| 346 | ); |
||
| 347 | |||
| 348 | return $field_html; |
||
| 349 | } |
||
| 350 | |||
| 351 | |||
| 352 | /** |
||
| 353 | * Render text field. |
||
| 354 | * |
||
| 355 | * @since 1.9 |
||
| 356 | * @access public |
||
| 357 | * |
||
| 358 | * @param array $field |
||
| 359 | * |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | public static function render_text_field( $field ) { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Render submit field. |
||
| 379 | * |
||
| 380 | * @since 1.9 |
||
| 381 | * @access public |
||
| 382 | * |
||
| 383 | * @param array $field |
||
| 384 | * |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | public static function render_submit_field( $field ) { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Render checkbox field. |
||
| 393 | * |
||
| 394 | * @since 1.9 |
||
| 395 | * @access public |
||
| 396 | * |
||
| 397 | * @param array $field |
||
| 398 | * |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | public static function render_checkbox_field( $field ) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Render email field. |
||
| 418 | * |
||
| 419 | * @since 1.9 |
||
| 420 | * @access public |
||
| 421 | * |
||
| 422 | * @param array $field |
||
| 423 | * |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | public static function render_email_field( $field ) { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Render number field. |
||
| 432 | * |
||
| 433 | * @since 1.9 |
||
| 434 | * @access public |
||
| 435 | * |
||
| 436 | * @param array $field |
||
| 437 | * |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | public static function render_number_field( $field ) { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Render password field. |
||
| 446 | * |
||
| 447 | * @since 1.9 |
||
| 448 | * @access public |
||
| 449 | * |
||
| 450 | * @param array $field |
||
| 451 | * |
||
| 452 | * @return string |
||
| 453 | */ |
||
| 454 | public static function render_password_field( $field ) { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Render button field. |
||
| 460 | * |
||
| 461 | * @since 1.9 |
||
| 462 | * @access public |
||
| 463 | * |
||
| 464 | * @param array $field |
||
| 465 | * |
||
| 466 | * @return string |
||
| 467 | */ |
||
| 468 | public static function render_button_field( $field ) { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Render hidden field. |
||
| 474 | * |
||
| 475 | * @since 1.9 |
||
| 476 | * @access public |
||
| 477 | * |
||
| 478 | * @param array $field |
||
| 479 | * |
||
| 480 | * @return string |
||
| 481 | */ |
||
| 482 | public static function render_hidden_field( $field ) { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Render textarea field. |
||
| 490 | * |
||
| 491 | * @since 1.9 |
||
| 492 | * @access public |
||
| 493 | * |
||
| 494 | * @param array $field |
||
| 495 | * |
||
| 496 | * @return string |
||
| 497 | */ |
||
| 498 | public static function render_textarea_field( $field ) { |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Render select field. |
||
| 516 | * |
||
| 517 | * @since 1.9 |
||
| 518 | * @access public |
||
| 519 | * |
||
| 520 | * @param array $field |
||
| 521 | * |
||
| 522 | * @return string |
||
| 523 | */ |
||
| 524 | public static function render_select_field( $field ) { |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Render multi select field. |
||
| 557 | * |
||
| 558 | * @since 1.9 |
||
| 559 | * @access public |
||
| 560 | * |
||
| 561 | * @param array $field |
||
| 562 | * |
||
| 563 | * @return string |
||
| 564 | */ |
||
| 565 | public static function render_multi_select_field( $field ) { |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Render radio field. |
||
| 574 | * |
||
| 575 | * @since 1.9 |
||
| 576 | * @access public |
||
| 577 | * |
||
| 578 | * @param array $field |
||
| 579 | * |
||
| 580 | * @return string |
||
| 581 | */ |
||
| 582 | public static function render_radio_field( $field ) { |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Render multi checkbox field. |
||
| 619 | * |
||
| 620 | * @since 1.9 |
||
| 621 | * @access public |
||
| 622 | * |
||
| 623 | * @param array $field |
||
| 624 | * |
||
| 625 | * @return string |
||
| 626 | */ |
||
| 627 | public static function render_multi_checkbox_field( $field ) { |
||
| 664 | |||
| 665 | |||
| 666 | /** |
||
| 667 | * Render group field |
||
| 668 | * |
||
| 669 | * @since 1.9 |
||
| 670 | * @access public |
||
| 671 | * |
||
| 672 | * @param array $fields |
||
| 673 | * @param array $form |
||
| 674 | * |
||
| 675 | * @return string |
||
| 676 | */ |
||
| 677 | public static function render_group_field( $fields, $form = null ) { |
||
| 824 | |||
| 825 | |||
| 826 | /** |
||
| 827 | * Render wrapper |
||
| 828 | * |
||
| 829 | * @since 1.9 |
||
| 830 | * @access private |
||
| 831 | * |
||
| 832 | * @param $field |
||
| 833 | * |
||
| 834 | * @return string |
||
| 835 | */ |
||
| 836 | public static function render_field_wrapper( $field ) { |
||
| 866 | |||
| 867 | |||
| 868 | /** |
||
| 869 | * Render label |
||
| 870 | * |
||
| 871 | * @since 1.9 |
||
| 872 | * @access private |
||
| 873 | * |
||
| 874 | * @param $field |
||
| 875 | * |
||
| 876 | * @return string |
||
| 877 | */ |
||
| 878 | private function render_label( $field ) { |
||
| 900 | |||
| 901 | /** |
||
| 902 | * Get field attribute string from field arguments. |
||
| 903 | * |
||
| 904 | * @since 1.9 |
||
| 905 | * @access private |
||
| 906 | * |
||
| 907 | * @param array $attributes |
||
| 908 | * |
||
| 909 | * @return array|string |
||
| 910 | */ |
||
| 911 | private function get_attributes( $attributes ) { |
||
| 926 | |||
| 927 | /** |
||
| 928 | * Set default values for fields |
||
| 929 | * |
||
| 930 | * @since 1.0 |
||
| 931 | * @access private |
||
| 932 | * |
||
| 933 | * @param array $field |
||
| 934 | * @param array $form |
||
| 935 | * @param bool $fire_filter |
||
| 936 | * |
||
| 937 | * @return array |
||
| 938 | */ |
||
| 939 | public static function set_default_values( $field, $form = null, $fire_filter = true ) { |
||
| 1043 | |||
| 1044 | |||
| 1045 | /** |
||
| 1046 | * Set responsive fields. |
||
| 1047 | * |
||
| 1048 | * @since 1.9 |
||
| 1049 | * @access private |
||
| 1050 | * |
||
| 1051 | * @param $form |
||
| 1052 | * |
||
| 1053 | * @return mixed |
||
| 1054 | */ |
||
| 1055 | private function set_responsive_field( &$form ) { |
||
| 1114 | |||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * Check if current field is part of sub section or not. |
||
| 1118 | * |
||
| 1119 | * @since 1.9 |
||
| 1120 | * @access private |
||
| 1121 | * |
||
| 1122 | * @param $field |
||
| 1123 | * |
||
| 1124 | * @return bool |
||
| 1125 | */ |
||
| 1126 | private function is_sub_section( $field ) { |
||
| 1134 | |||
| 1135 | |||
| 1136 | /** |
||
| 1137 | * Get field type. |
||
| 1138 | * |
||
| 1139 | * @since 1.9 |
||
| 1140 | * @access private |
||
| 1141 | * |
||
| 1142 | * @param $field |
||
| 1143 | * |
||
| 1144 | * @return bool |
||
| 1145 | */ |
||
| 1146 | public static function get_field_type( $field ) { |
||
| 1165 | |||
| 1166 | /** |
||
| 1167 | * Get field name. |
||
| 1168 | * |
||
| 1169 | * @since 1.9 |
||
| 1170 | * |
||
| 1171 | * @param array $field |
||
| 1172 | * |
||
| 1173 | * @return string |
||
| 1174 | */ |
||
| 1175 | public static function get_field_name( $field ) { |
||
| 1180 | |||
| 1181 | /** |
||
| 1182 | * Get repeater field id. |
||
| 1183 | * |
||
| 1184 | * @since 1.9 |
||
| 1185 | * |
||
| 1186 | * @param array $field |
||
| 1187 | * @param array $fields |
||
| 1188 | * @param int|bool $default |
||
| 1189 | * |
||
| 1190 | * @return string |
||
| 1191 | */ |
||
| 1192 | public static function get_repeater_field_name( $field, $fields , $default = false ) { |
||
| 1218 | } |
||
| 1219 |