Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like FrmField 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 FrmField, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class FrmField { |
||
| 7 | static $use_cache = true; |
||
| 8 | static $transient_size = 200; |
||
| 9 | |||
| 10 | public static function field_selection() { |
||
| 24 | |||
| 25 | public static function pro_field_selection() { |
||
| 26 | return apply_filters('frm_pro_available_fields', array( |
||
| 27 | 'end_divider' => array( |
||
| 28 | 'name' => __( 'End Section', 'formidable' ), |
||
| 29 | 'switch_from' => 'divider', |
||
| 30 | ), |
||
| 31 | 'divider' => __( 'Section', 'formidable' ), |
||
| 32 | 'break' => __( 'Page Break', 'formidable' ), |
||
| 33 | 'file' => __( 'File Upload', 'formidable' ), |
||
| 34 | 'rte' => __( 'Rich Text', 'formidable' ), |
||
| 35 | 'number' => __( 'Number', 'formidable' ), |
||
| 36 | 'phone' => __( 'Phone Number', 'formidable' ), |
||
| 37 | 'date' => __( 'Date', 'formidable' ), |
||
| 38 | 'time' => __( 'Time', 'formidable' ), |
||
| 39 | 'image' => __( 'Image URL', 'formidable' ), |
||
| 40 | 'scale' => __( 'Scale', 'formidable' ), |
||
| 41 | 'data' => __( 'Dynamic Field', 'formidable' ), |
||
| 42 | 'lookup' => __( 'Lookup', 'formidable' ), |
||
| 43 | 'form' => __( 'Embed Form', 'formidable' ), |
||
| 44 | 'hidden' => __( 'Hidden Field', 'formidable' ), |
||
| 45 | 'user_id' => __( 'User ID (hidden)', 'formidable' ), |
||
| 46 | 'password' => __( 'Password', 'formidable' ), |
||
| 47 | 'html' => __( 'HTML', 'formidable' ), |
||
| 48 | 'tag' => __( 'Tags', 'formidable' ), |
||
| 49 | 'credit_card' => __( 'Credit Card', 'formidable' ), |
||
| 50 | 'address' => __( 'Address', 'formidable' ), |
||
| 51 | )); |
||
| 52 | } |
||
| 53 | |||
| 54 | public static function create( $values, $return = true ) { |
||
| 55 | global $wpdb, $frm_duplicate_ids; |
||
| 56 | |||
| 57 | $new_values = array(); |
||
| 58 | $key = isset($values['field_key']) ? $values['field_key'] : $values['name']; |
||
| 59 | $new_values['field_key'] = FrmAppHelper::get_unique_key( $key, $wpdb->prefix . 'frm_fields', 'field_key' ); |
||
| 60 | |||
| 61 | foreach ( array( 'name', 'description', 'type', 'default_value' ) as $col ) { |
||
| 62 | $new_values[ $col ] = $values[ $col ]; |
||
| 63 | } |
||
| 64 | |||
| 65 | $new_values['options'] = $values['options']; |
||
| 66 | |||
| 67 | $new_values['field_order'] = isset($values['field_order']) ? (int) $values['field_order'] : null; |
||
| 68 | $new_values['required'] = isset($values['required']) ? (int) $values['required'] : 0; |
||
| 69 | $new_values['form_id'] = isset($values['form_id']) ? (int) $values['form_id'] : null; |
||
| 70 | $new_values['field_options'] = $values['field_options']; |
||
| 71 | $new_values['created_at'] = current_time('mysql', 1); |
||
| 72 | |||
| 73 | if ( isset( $values['id'] ) ) { |
||
| 74 | $frm_duplicate_ids[ $values['field_key'] ] = $new_values['field_key']; |
||
| 75 | $new_values = apply_filters('frm_duplicated_field', $new_values); |
||
| 76 | } |
||
| 77 | |||
| 78 | foreach ( $new_values as $k => $v ) { |
||
| 79 | if ( is_array( $v ) ) { |
||
| 80 | $new_values[ $k ] = serialize( $v ); |
||
| 81 | } |
||
| 82 | unset( $k, $v ); |
||
| 83 | } |
||
| 84 | |||
| 85 | //if(isset($values['id']) and is_numeric($values['id'])) |
||
| 86 | // $new_values['id'] = $values['id']; |
||
| 87 | |||
| 88 | $query_results = $wpdb->insert( $wpdb->prefix . 'frm_fields', $new_values ); |
||
| 89 | $new_id = 0; |
||
| 90 | if ( $query_results ) { |
||
| 91 | self::delete_form_transient( $new_values['form_id'] ); |
||
| 92 | $new_id = $wpdb->insert_id; |
||
| 93 | } |
||
| 94 | |||
| 95 | if ( ! $return ) { |
||
| 96 | return; |
||
| 97 | } |
||
| 98 | |||
| 99 | if ( $query_results ) { |
||
| 100 | if ( isset( $values['id'] ) ) { |
||
| 101 | $frm_duplicate_ids[ $values['id'] ] = $new_id; |
||
| 102 | } |
||
| 103 | return $new_id; |
||
| 104 | } else { |
||
| 105 | return false; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | public static function duplicate( $old_form_id, $form_id, $copy_keys = false, $blog_id = false ) { |
||
| 110 | global $frm_duplicate_ids; |
||
| 111 | |||
| 112 | $where = array( array( 'or' => 1, 'fi.form_id' => $old_form_id, 'fr.parent_form_id' => $old_form_id ) ); |
||
| 113 | $fields = self::getAll( $where, 'field_order', '', $blog_id ); |
||
| 114 | |||
| 115 | foreach ( (array) $fields as $field ) { |
||
| 116 | $new_key = ($copy_keys) ? $field->field_key : ''; |
||
| 117 | if ( $copy_keys && substr($field->field_key, -1) == 2 ) { |
||
| 118 | $new_key = rtrim($new_key, 2); |
||
| 119 | } |
||
| 120 | |||
| 121 | $values = array(); |
||
| 122 | FrmFieldsHelper::fill_field( $values, $field, $form_id, $new_key ); |
||
| 123 | |||
| 124 | // If this is a repeating section, create new form |
||
| 125 | if ( self::is_repeating_field( $field ) ) { |
||
| 126 | // create the repeatable form |
||
| 127 | $new_repeat_form_id = apply_filters( 'frm_create_repeat_form', 0, array( 'parent_form_id' => $form_id, 'field_name' => $field->name ) ); |
||
| 128 | |||
| 129 | // Save old form_select |
||
| 130 | $old_repeat_form_id = $field->field_options['form_select']; |
||
| 131 | |||
| 132 | // Update form_select for repeating field |
||
| 133 | $values['field_options']['form_select'] = $new_repeat_form_id; |
||
| 134 | } |
||
| 135 | |||
| 136 | // If this is a field inside of a repeating section, associate it with the correct form |
||
| 137 | if ( $field->form_id != $old_form_id && isset( $old_repeat_form_id ) && isset( $new_repeat_form_id ) && $field->form_id == $old_repeat_form_id ) { |
||
| 138 | $values['form_id'] = $new_repeat_form_id; |
||
| 139 | } |
||
| 140 | |||
| 141 | $values = apply_filters('frm_duplicated_field', $values); |
||
| 142 | $new_id = self::create($values); |
||
| 143 | $frm_duplicate_ids[ $field->id ] = $new_id; |
||
| 144 | $frm_duplicate_ids[ $field->field_key ] = $new_id; |
||
| 145 | unset($field); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | public static function update( $id, $values ) { |
||
| 150 | global $wpdb; |
||
| 151 | |||
| 152 | $id = absint( $id ); |
||
| 153 | |||
| 154 | if ( isset( $values['field_key'] ) ) { |
||
| 155 | $values['field_key'] = FrmAppHelper::get_unique_key( $values['field_key'], $wpdb->prefix . 'frm_fields', 'field_key', $id ); |
||
| 156 | } |
||
| 157 | |||
| 158 | if ( isset($values['required']) ) { |
||
| 159 | $values['required'] = (int) $values['required']; |
||
| 160 | } |
||
| 161 | |||
| 162 | self::preserve_phone_format_backslashes( $values ); |
||
| 163 | |||
| 164 | $values = apply_filters( 'frm_clean_' . $values['type'] . '_field_options_before_update', $values ); |
||
| 165 | |||
| 166 | // serialize array values |
||
| 167 | foreach ( array( 'default_value', 'field_options', 'options' ) as $opt ) { |
||
| 168 | if ( isset( $values[ $opt ] ) && is_array( $values[ $opt ] ) ) { |
||
| 169 | $values[ $opt ] = serialize( $values[ $opt ] ); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | $query_results = $wpdb->update( $wpdb->prefix . 'frm_fields', $values, array( 'id' => $id ) ); |
||
| 174 | |||
| 175 | $form_id = 0; |
||
| 176 | if ( isset( $values['form_id'] ) ) { |
||
| 177 | $form_id = absint( $values['form_id'] ); |
||
| 178 | } else { |
||
| 179 | $field = self::getOne($id); |
||
| 180 | if ( $field ) { |
||
| 181 | $form_id = $field->form_id; |
||
| 182 | } |
||
| 183 | unset($field); |
||
| 184 | } |
||
| 185 | unset($values); |
||
| 186 | |||
| 187 | if ( $query_results ) { |
||
| 188 | wp_cache_delete( $id, 'frm_field' ); |
||
| 189 | if ( $form_id ) { |
||
| 190 | self::delete_form_transient($form_id); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | return $query_results; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Keep backslashes in the phone format option |
||
| 199 | * |
||
| 200 | * @since 2.0.8 |
||
| 201 | * @param $values array - pass by reference |
||
| 202 | */ |
||
| 203 | private static function preserve_phone_format_backslashes( &$values ) { |
||
| 208 | |||
| 209 | public static function destroy( $id ) { |
||
| 225 | |||
| 226 | public static function delete_form_transient( $form_id ) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * If $field is numeric, get the field object |
||
| 250 | */ |
||
| 251 | public static function maybe_get_field( &$field ) { |
||
| 256 | |||
| 257 | public static function getOne( $id ) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Get the field type by key or id |
||
| 286 | * @param int|string The field id or key |
||
| 287 | * @param mixed $col The name of the column in the fields database table |
||
| 288 | */ |
||
| 289 | public static function &get_type( $id, $col = 'type' ) { |
||
| 299 | |||
| 300 | public static function get_all_types_in_form( $form_id, $type, $limit = '', $inc_sub = 'exclude' ) { |
||
| 340 | |||
| 341 | public static function get_all_for_form( $form_id, $limit = '', $inc_embed = 'exclude', $inc_repeat = 'include' ) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * If repeating fields should be included, adjust $where accordingly |
||
| 383 | * |
||
| 384 | * @param string $inc_repeat |
||
| 385 | * @param array $where - pass by reference |
||
| 386 | */ |
||
| 387 | private static function maybe_include_repeating_fields( $inc_repeat, &$where ) { |
||
| 394 | |||
| 395 | public static function include_sub_fields( &$results, $inc_embed, $type = 'all' ) { |
||
| 421 | |||
| 422 | public static function getAll( $where = array(), $order_by = '', $limit = '', $blog_id = false ) { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @since 2.0.8 |
||
| 478 | */ |
||
| 479 | private static function format_field_results( &$results ) { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Unserialize all the serialized field data |
||
| 501 | * @since 2.0 |
||
| 502 | */ |
||
| 503 | private static function prepare_options( &$results ) { |
||
| 509 | |||
| 510 | /** |
||
| 511 | * If a form has too many fields, thay won't all save into a single transient. |
||
| 512 | * We'll break them into groups of 200 |
||
| 513 | * @since 2.0.1 |
||
| 514 | */ |
||
| 515 | private static function get_fields_from_transients( $form_id, $args ) { |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Called by get_fields_from_transients |
||
| 523 | * @since 2.0.1 |
||
| 524 | */ |
||
| 525 | private static function get_next_transient( &$fields, $base_name, $next = 0 ) { |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Save the transients in chunks for large forms |
||
| 542 | * @since 2.0.1 |
||
| 543 | */ |
||
| 544 | private static function set_field_transient( &$fields, $form_id, $next = 0, $args = array() ) { |
||
| 563 | |||
| 564 | public static function getIds( $where = '', $order_by = '', $limit = '' ) { |
||
| 581 | |||
| 582 | public static function is_no_save_field( $type ) { |
||
| 585 | |||
| 586 | public static function no_save_fields() { |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Check if this field can hold an array of values |
||
| 592 | * |
||
| 593 | * @since 2.0.9 |
||
| 594 | * |
||
| 595 | * @param array|object $field |
||
| 596 | * @return boolean |
||
| 597 | */ |
||
| 598 | public static function is_field_with_multiple_values( $field ) { |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Check if this is a multiselect dropdown field |
||
| 626 | * |
||
| 627 | * @since 2.0.9 |
||
| 628 | * @return boolean |
||
| 629 | */ |
||
| 630 | public static function is_multiple_select( $field ) { |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Check if a field is read only. Read only can be set in the field options, |
||
| 640 | * but disabled with the shortcode options |
||
| 641 | * |
||
| 642 | * @since 2.0.9 |
||
| 643 | */ |
||
| 644 | public static function is_read_only( $field ) { |
||
| 648 | |||
| 649 | /** |
||
| 650 | * @since 2.0.9 |
||
| 651 | */ |
||
| 652 | public static function is_required( $field ) { |
||
| 657 | |||
| 658 | /** |
||
| 659 | * @since 2.0.9 |
||
| 660 | */ |
||
| 661 | public static function is_option_true( $field, $option ) { |
||
| 668 | |||
| 669 | /** |
||
| 670 | * @since 2.0.9 |
||
| 671 | */ |
||
| 672 | public static function is_option_empty( $field, $option ) { |
||
| 679 | |||
| 680 | public static function is_option_true_in_array( $field, $option ) { |
||
| 683 | |||
| 684 | public static function is_option_true_in_object( $field, $option ) { |
||
| 687 | |||
| 688 | public static function is_option_empty_in_array( $field, $option ) { |
||
| 691 | |||
| 692 | public static function is_option_empty_in_object( $field, $option ) { |
||
| 695 | |||
| 696 | public static function is_option_value_in_object( $field, $option ) { |
||
| 699 | |||
| 700 | /** |
||
| 701 | * @since 2.0.18 |
||
| 702 | */ |
||
| 703 | public static function get_option( $field, $option ) { |
||
| 711 | |||
| 712 | public static function get_option_in_array( $field, $option ) { |
||
| 715 | |||
| 716 | public static function get_option_in_object( $field, $option ) { |
||
| 719 | |||
| 720 | /** |
||
| 721 | * @since 2.0.09 |
||
| 722 | */ |
||
| 723 | public static function is_repeating_field( $field ) { |
||
| 731 | |||
| 732 | /** |
||
| 733 | * @param string $key |
||
| 734 | * @return int field id |
||
| 735 | */ |
||
| 736 | public static function get_id_by_key( $key ) { |
||
| 740 | |||
| 741 | /** |
||
| 742 | * @param string $id |
||
| 743 | * @return string |
||
| 744 | */ |
||
| 745 | public static function get_key_by_id( $id ) { |
||
| 748 | } |
||
| 749 |