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() ) { |
||
| 351 | |||
| 352 | |||
| 353 | /** |
||
| 354 | * Render text field. |
||
| 355 | * |
||
| 356 | * @since 1.9 |
||
| 357 | * @access public |
||
| 358 | * |
||
| 359 | * @param array $field |
||
| 360 | * @param array $form |
||
| 361 | * @param array $args |
||
| 362 | * |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | public static function render_text_field( $field, $form = null, $args = array() ) { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Render submit field. |
||
| 382 | * |
||
| 383 | * @since 1.9 |
||
| 384 | * @access public |
||
| 385 | * |
||
| 386 | * @param array $field |
||
| 387 | * @param array $form |
||
| 388 | * @param array $args |
||
| 389 | * |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | public static function render_submit_field( $field, $form = null, $args = array() ) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Render checkbox field. |
||
| 398 | * |
||
| 399 | * @since 1.9 |
||
| 400 | * @access public |
||
| 401 | * |
||
| 402 | * @param array $field |
||
| 403 | * @param array $form |
||
| 404 | * @param array $args |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | public static function render_checkbox_field( $field, $form = null, $args = array() ) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Render email field. |
||
| 425 | * |
||
| 426 | * @since 1.9 |
||
| 427 | * @access public |
||
| 428 | * |
||
| 429 | * @param array $field |
||
| 430 | * @param array $form |
||
| 431 | * @param array $args |
||
| 432 | * |
||
| 433 | * @return string |
||
| 434 | */ |
||
| 435 | public static function render_email_field( $field, $form = null, $args = array() ) { |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Render number field. |
||
| 441 | * |
||
| 442 | * @since 1.9 |
||
| 443 | * @access public |
||
| 444 | * |
||
| 445 | * @param array $field |
||
| 446 | * @param array $form |
||
| 447 | * @param array $args |
||
| 448 | * |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | public static function render_number_field( $field, $form = null, $args = array() ) { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Render password field. |
||
| 457 | * |
||
| 458 | * @since 1.9 |
||
| 459 | * @access public |
||
| 460 | * |
||
| 461 | * @param array $field |
||
| 462 | * @param array $form |
||
| 463 | * @param array $args |
||
| 464 | * |
||
| 465 | * @return string |
||
| 466 | */ |
||
| 467 | public static function render_password_field( $field, $form = null, $args = array() ) { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Render button field. |
||
| 473 | * |
||
| 474 | * @since 1.9 |
||
| 475 | * @access public |
||
| 476 | * |
||
| 477 | * @param array $field |
||
| 478 | * @param array $form |
||
| 479 | * @param array $args |
||
| 480 | * |
||
| 481 | * @return string |
||
| 482 | */ |
||
| 483 | public static function render_button_field( $field, $form = null, $args = array() ) { |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Render hidden field. |
||
| 489 | * |
||
| 490 | * @since 1.9 |
||
| 491 | * @access public |
||
| 492 | * |
||
| 493 | * @param array $field |
||
| 494 | * @param array $form |
||
| 495 | * @param array $args |
||
| 496 | * |
||
| 497 | * @return string |
||
| 498 | */ |
||
| 499 | public static function render_hidden_field( $field, $form = null, $args = array() ) { |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Render textarea field. |
||
| 507 | * |
||
| 508 | * @since 1.9 |
||
| 509 | * @access public |
||
| 510 | * |
||
| 511 | * @param array $field |
||
| 512 | * @param array $form |
||
| 513 | * @param array $args |
||
| 514 | * |
||
| 515 | * @return string |
||
| 516 | */ |
||
| 517 | public static function render_textarea_field( $field, $form = null, $args = array() ) { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Render select field. |
||
| 535 | * |
||
| 536 | * @since 1.9 |
||
| 537 | * @access public |
||
| 538 | * |
||
| 539 | * @param array $field |
||
| 540 | * @param array $form |
||
| 541 | * @param array $args |
||
| 542 | * |
||
| 543 | * @return string |
||
| 544 | */ |
||
| 545 | public static function render_select_field( $field, $form = null, $args = array() ) { |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Render multi select field. |
||
| 578 | * |
||
| 579 | * @since 1.9 |
||
| 580 | * @access public |
||
| 581 | * |
||
| 582 | * @param array $field |
||
| 583 | * @param array $form |
||
| 584 | * @param array $args |
||
| 585 | * |
||
| 586 | * @return string |
||
| 587 | */ |
||
| 588 | public static function render_multi_select_field( $field, $form = null, $args = array() ) { |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Render radio field. |
||
| 597 | * |
||
| 598 | * @since 1.9 |
||
| 599 | * @access public |
||
| 600 | * |
||
| 601 | * @param array $field |
||
| 602 | * @param array $form |
||
| 603 | * @param array $args |
||
| 604 | * |
||
| 605 | * @return string |
||
| 606 | */ |
||
| 607 | public static function render_radio_field( $field, $form = null, $args = array() ) { |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Render multi checkbox field. |
||
| 644 | * |
||
| 645 | * @since 1.9 |
||
| 646 | * @access public |
||
| 647 | * |
||
| 648 | * @param array $field |
||
| 649 | * @param array $form |
||
| 650 | * @param array $args |
||
| 651 | * |
||
| 652 | * @return string |
||
| 653 | */ |
||
| 654 | public static function render_multi_checkbox_field( $field, $form = null, $args = array() ) { |
||
| 696 | |||
| 697 | |||
| 698 | /** |
||
| 699 | * Render group field |
||
| 700 | * |
||
| 701 | * @since 1.9 |
||
| 702 | * @access public |
||
| 703 | * |
||
| 704 | * @param array $fields |
||
| 705 | * @param array $form |
||
| 706 | * @param array $args |
||
| 707 | * |
||
| 708 | * @return string |
||
| 709 | */ |
||
| 710 | public static function render_group_field( $fields, $form = null, $args = array() ) { |
||
| 864 | |||
| 865 | |||
| 866 | /** |
||
| 867 | * Render wrapper |
||
| 868 | * |
||
| 869 | * @since 1.9 |
||
| 870 | * @access private |
||
| 871 | * |
||
| 872 | * @param array $field |
||
| 873 | * @param array $form |
||
| 874 | * @param array $args |
||
| 875 | * |
||
| 876 | * @return string |
||
| 877 | */ |
||
| 878 | public static function render_field_wrapper( $field, $form = null, $args = array() ) { |
||
| 908 | |||
| 909 | |||
| 910 | /** |
||
| 911 | * Render label |
||
| 912 | * |
||
| 913 | * @since 1.9 |
||
| 914 | * @access private |
||
| 915 | * |
||
| 916 | * @param array $field |
||
| 917 | * @param array $form |
||
| 918 | * @param array $args |
||
| 919 | * |
||
| 920 | * @return string |
||
| 921 | */ |
||
| 922 | private function render_label( $field, $form = null, $args = array() ) { |
||
| 944 | |||
| 945 | /** |
||
| 946 | * Get field attribute string from field arguments. |
||
| 947 | * |
||
| 948 | * @since 1.9 |
||
| 949 | * @access private |
||
| 950 | * |
||
| 951 | * @param array $attributes |
||
| 952 | * |
||
| 953 | * @return array|string |
||
| 954 | */ |
||
| 955 | private function get_attributes( $attributes ) { |
||
| 970 | |||
| 971 | /** |
||
| 972 | * Set default values for fields |
||
| 973 | * |
||
| 974 | * @since 1.0 |
||
| 975 | * @access private |
||
| 976 | * |
||
| 977 | * @param array $field |
||
| 978 | * @param array $form |
||
| 979 | * @param bool $fire_filter |
||
| 980 | * |
||
| 981 | * @return array |
||
| 982 | * |
||
| 983 | * @todo make third parameter of this function an array and update logic for $fire_filter |
||
| 984 | */ |
||
| 985 | public static function set_default_values( $field, $form = null, $fire_filter = true ) { |
||
| 1089 | |||
| 1090 | |||
| 1091 | /** |
||
| 1092 | * Set responsive fields. |
||
| 1093 | * |
||
| 1094 | * @since 1.9 |
||
| 1095 | * @access private |
||
| 1096 | * |
||
| 1097 | * @param $form |
||
| 1098 | * |
||
| 1099 | * @return mixed |
||
| 1100 | */ |
||
| 1101 | private function set_responsive_field( &$form ) { |
||
| 1160 | |||
| 1161 | |||
| 1162 | /** |
||
| 1163 | * Check if current field is part of sub section or not. |
||
| 1164 | * |
||
| 1165 | * @since 1.9 |
||
| 1166 | * @access private |
||
| 1167 | * |
||
| 1168 | * @param $field |
||
| 1169 | * |
||
| 1170 | * @return bool |
||
| 1171 | */ |
||
| 1172 | private function is_sub_section( $field ) { |
||
| 1180 | |||
| 1181 | |||
| 1182 | /** |
||
| 1183 | * Get field type. |
||
| 1184 | * |
||
| 1185 | * @since 1.9 |
||
| 1186 | * @access private |
||
| 1187 | * |
||
| 1188 | * @param array $field |
||
| 1189 | * @param array $form |
||
| 1190 | * @param array $args |
||
| 1191 | * |
||
| 1192 | * @return bool |
||
| 1193 | */ |
||
| 1194 | public static function get_field_type( $field, $form = null, $args = array() ) { |
||
| 1213 | |||
| 1214 | /** |
||
| 1215 | * Get field name. |
||
| 1216 | * |
||
| 1217 | * @since 1.9 |
||
| 1218 | * |
||
| 1219 | * @param array $field |
||
| 1220 | * @param array $form |
||
| 1221 | * @param array $args |
||
| 1222 | * |
||
| 1223 | * @return string |
||
| 1224 | */ |
||
| 1225 | public static function get_field_name( $field, $form = null, $args = array() ) { |
||
| 1239 | |||
| 1240 | /** |
||
| 1241 | * Get repeater field id. |
||
| 1242 | * |
||
| 1243 | * @since 1.9 |
||
| 1244 | * |
||
| 1245 | * @param array $field |
||
| 1246 | * @param array $fields |
||
| 1247 | * @param int|bool $default |
||
| 1248 | * |
||
| 1249 | * @return string |
||
| 1250 | */ |
||
| 1251 | public static function get_repeater_field_name( $field, $fields , $default = false ) { |
||
| 1277 | |||
| 1278 | |||
| 1279 | /** |
||
| 1280 | * Get repeater field value. |
||
| 1281 | * |
||
| 1282 | * @since 1.9 |
||
| 1283 | * @access public |
||
| 1284 | * |
||
| 1285 | * @param array $field |
||
| 1286 | * @param array $field_value_group |
||
| 1287 | * @param array $fields |
||
| 1288 | * |
||
| 1289 | * @return mixed |
||
| 1290 | */ |
||
| 1291 | public static function get_repeater_field_value( $field, $field_value_group, $fields ) { |
||
| 1314 | } |
||
| 1315 |