Complex classes like FrmEntriesHelper 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 FrmEntriesHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class FrmEntriesHelper { |
||
| 7 | |||
| 8 | public static function setup_new_vars( $fields, $form = '', $reset = false, $args = array() ) { |
||
| 9 | $values = array( 'name' => '', 'description' => '', 'item_key' => '' ); |
||
| 10 | |||
| 11 | $values['fields'] = array(); |
||
| 12 | if ( empty($fields) ) { |
||
| 13 | return apply_filters('frm_setup_new_entry', $values); |
||
| 14 | } |
||
| 15 | |||
| 16 | foreach ( (array) $fields as $field ) { |
||
| 17 | self::prepare_field_default_value( $field ); |
||
| 18 | $new_value = self::get_field_value_for_new_entry( $field, $reset, $args ); |
||
| 19 | |||
| 20 | $field_array = array( |
||
| 21 | 'id' => $field->id, |
||
| 22 | 'value' => $new_value, |
||
| 23 | 'default_value' => $field->default_value, |
||
| 24 | 'name' => $field->name, |
||
| 25 | 'description' => $field->description, |
||
| 26 | 'type' => apply_filters('frm_field_type', $field->type, $field, $new_value), |
||
| 27 | 'options' => $field->options, |
||
| 28 | 'required' => $field->required, |
||
| 29 | 'field_key' => $field->field_key, |
||
| 30 | 'field_order' => $field->field_order, |
||
| 31 | 'form_id' => $field->form_id, |
||
| 32 | 'parent_form_id' => isset( $args['parent_form_id'] ) ? $args['parent_form_id'] : $field->form_id, |
||
| 33 | 'reset_value' => $reset, |
||
| 34 | 'in_embed_form' => isset( $args['in_embed_form'] ) ? $args['in_embed_form'] : '0', |
||
| 35 | ); |
||
| 36 | |||
| 37 | $opt_defaults = FrmFieldsHelper::get_default_field_opts($field_array['type'], $field, true); |
||
| 38 | $opt_defaults['required_indicator'] = ''; |
||
| 39 | $opt_defaults['original_type'] = $field->type; |
||
| 40 | |||
| 41 | foreach ( $opt_defaults as $opt => $default_opt ) { |
||
| 42 | $field_array[ $opt ] = ( isset( $field->field_options[ $opt ] ) && $field->field_options[ $opt ] != '' ) ? $field->field_options[ $opt ] : $default_opt; |
||
| 43 | unset($opt, $default_opt); |
||
| 44 | } |
||
| 45 | |||
| 46 | unset($opt_defaults); |
||
| 47 | |||
| 48 | if ( $field_array['custom_html'] == '' ) { |
||
| 49 | $field_array['custom_html'] = FrmFieldsHelper::get_default_html($field->type); |
||
| 50 | } |
||
| 51 | |||
| 52 | $field_array = apply_filters('frm_setup_new_fields_vars', $field_array, $field, $args ); |
||
| 53 | $field_array = array_merge( $field->field_options, $field_array ); |
||
| 54 | |||
| 55 | $values['fields'][] = $field_array; |
||
| 56 | |||
| 57 | if ( ! $form || ! isset($form->id) ) { |
||
| 58 | $form = FrmForm::getOne($field->form_id); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | $form->options = maybe_unserialize( $form->options ); |
||
| 63 | if ( is_array( $form->options ) ) { |
||
| 64 | $values = array_merge( $values, $form->options ); |
||
| 65 | } |
||
| 66 | |||
| 67 | $form_defaults = FrmFormsHelper::get_default_opts(); |
||
| 68 | |||
| 69 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 70 | $form_defaults['custom_style'] = ( $frm_settings->load_style != 'none' ); |
||
| 71 | |||
| 72 | $values = array_merge( $form_defaults, $values ); |
||
| 73 | |||
| 74 | return apply_filters( 'frm_setup_new_entry', $values ); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @since 2.05 |
||
| 79 | */ |
||
| 80 | private static function prepare_field_default_value( &$field ) { |
||
| 81 | //If checkbox, multi-select dropdown, or checkbox data from entries field, the value should be an array |
||
| 82 | $return_array = FrmField::is_field_with_multiple_values( $field ); |
||
| 83 | |||
| 84 | // Do any shortcodes in default value and allow customization of default value |
||
| 85 | $field->default_value = apply_filters( 'frm_get_default_value', $field->default_value, $field, true, $return_array ); |
||
| 86 | // Calls FrmProFieldsHelper::get_default_value |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Set the value for each field |
||
| 91 | * This function is used when the form is first loaded and on all page turns *for a new entry* |
||
| 92 | * |
||
| 93 | * @since 2.0.13 |
||
| 94 | * |
||
| 95 | * @param object $field - this is passed by reference since it is an object |
||
| 96 | * @param boolean $reset |
||
| 97 | * @param array $args |
||
| 98 | * @return string|array $new_value |
||
| 99 | */ |
||
| 100 | private static function get_field_value_for_new_entry( $field, $reset, $args ) { |
||
| 101 | $new_value = $field->default_value; |
||
| 102 | |||
| 103 | if ( ! $reset && self::value_is_posted( $field, $args ) ) { |
||
| 104 | self::get_posted_value( $field, $new_value, $args ); |
||
| 105 | } else if ( FrmField::is_option_true( $field, 'clear_on_focus' ) ) { |
||
| 106 | // If clear on focus is selected, the value should be blank (unless it was posted, of course) |
||
| 107 | |||
| 108 | // TODO: move to Pro |
||
| 109 | if ( 'address' == $field->type && isset( $new_value['country'] ) ) { |
||
| 110 | $new_value = array( 'country' => $new_value['country'] ); |
||
| 111 | } else { |
||
| 112 | $new_value = ''; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | if ( ! is_array( $new_value ) ) { |
||
| 117 | $new_value = str_replace('"', '"', $new_value); |
||
| 118 | } |
||
| 119 | |||
| 120 | return $new_value; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Check if a field has a posted value |
||
| 125 | * |
||
| 126 | * @since 2.01.0 |
||
| 127 | * @param object $field |
||
| 128 | * @param array $args |
||
| 129 | * @return boolean $value_is_posted |
||
| 130 | */ |
||
| 131 | public static function value_is_posted( $field, $args ) { |
||
| 145 | |||
| 146 | public static function setup_edit_vars( $values, $record ) { |
||
| 152 | |||
| 153 | public static function get_admin_params( $form = null ) { |
||
| 157 | |||
| 158 | public static function set_current_form( $form_id ) { |
||
| 162 | |||
| 163 | public static function get_current_form( $form_id = 0 ) { |
||
| 167 | |||
| 168 | public static function get_current_form_id() { |
||
| 172 | |||
| 173 | public static function maybe_get_entry( &$entry ) { |
||
| 177 | |||
| 178 | public static function replace_default_message( $message, $atts ) { |
||
| 207 | |||
| 208 | public static function prepare_display_value( $entry, $field, $atts ) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Prepare the saved value for display |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public static function display_value( $value, $field, $atts = array() ) { |
||
| 332 | |||
| 333 | public static function set_posted_value( $field, $value, $args ) { |
||
| 344 | |||
| 345 | public static function get_posted_value( $field, &$value, $args ) { |
||
| 346 | $field_id = is_object( $field ) ? $field->id : $field; |
||
| 347 | |||
| 348 | if ( empty($args['parent_field_id']) ) { |
||
| 349 | $value = isset( $_POST['item_meta'][ $field_id ] ) ? $_POST['item_meta'][ $field_id ] : ''; |
||
| 350 | } else { |
||
| 351 | $value = isset( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field_id ] ) ? $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field_id ] : ''; |
||
| 352 | } |
||
| 353 | FrmAppHelper::sanitize_value( 'wp_kses_post', $value ); |
||
| 354 | $value = stripslashes_deep( $value ); |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Check if field has an "Other" option and if any other values are posted |
||
| 359 | * |
||
| 360 | * @since 2.0 |
||
| 361 | * |
||
| 362 | * @param object $field |
||
| 363 | * @param string|array $value |
||
| 364 | * @param array $args |
||
| 365 | */ |
||
| 366 | public static function maybe_set_other_validation( $field, &$value, &$args ) { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Sets radio or checkbox value equal to "other" value if it is set - FOR REPEATING SECTIONS |
||
| 390 | * |
||
| 391 | * @since 2.0 |
||
| 392 | * |
||
| 393 | * @param object $field |
||
| 394 | * @param string|array $value |
||
| 395 | * @param array $args |
||
| 396 | */ |
||
| 397 | public static function set_other_repeating_vals( $field, &$value, &$args ) { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Modify value used for validation |
||
| 417 | * This function essentially removes the "Other" radio or checkbox value from the $value being validated. |
||
| 418 | * It also adds any text from the free text fields to the value |
||
| 419 | * |
||
| 420 | * Needs to accommodate for times when other opt is selected, but no other free text is entered |
||
| 421 | * |
||
| 422 | * @since 2.0 |
||
| 423 | * |
||
| 424 | * @param string|array $value |
||
| 425 | * @param string|array $other_vals (usually of posted values) |
||
| 426 | * @param object $field |
||
| 427 | * @param array $args |
||
| 428 | */ |
||
| 429 | public static function set_other_validation_val( &$value, $other_vals, $field, &$args ) { |
||
| 465 | |||
| 466 | public static function enqueue_scripts( $params ) { |
||
| 470 | |||
| 471 | // Add submitted values to a string for spam checking |
||
| 472 | public static function entry_array_to_string( $values ) { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Get the browser from the user agent |
||
| 492 | * |
||
| 493 | * @since 2.04 |
||
| 494 | * |
||
| 495 | * @param string $u_agent |
||
| 496 | * |
||
| 497 | * @return string |
||
| 498 | */ |
||
| 499 | public static function get_browser( $u_agent ) { |
||
| 566 | |||
| 567 | } |
||
| 568 |