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 ) { |
||
| 209 | $field_value = isset( $entry->metas[ $field->id ] ) ? $entry->metas[ $field->id ] : false; |
||
| 210 | |||
| 211 | if ( FrmAppHelper::pro_is_installed() ) { |
||
| 212 | FrmProEntriesHelper::get_dynamic_list_values( $field, $entry, $field_value ); |
||
| 213 | } |
||
| 214 | |||
| 215 | if ( $field->form_id == $entry->form_id || empty($atts['embedded_field_id']) ) { |
||
| 216 | return self::display_value($field_value, $field, $atts); |
||
| 217 | } |
||
| 218 | |||
| 219 | // this is an embeded form |
||
| 220 | $val = ''; |
||
| 221 | |||
| 222 | if ( strpos($atts['embedded_field_id'], 'form') === 0 ) { |
||
| 223 | //this is a repeating section |
||
| 224 | $child_entries = FrmEntry::getAll( array( 'it.parent_item_id' => $entry->id ) ); |
||
| 225 | } else { |
||
| 226 | // get all values for this field |
||
| 227 | $child_values = isset( $entry->metas[ $atts['embedded_field_id'] ] ) ? $entry->metas[ $atts['embedded_field_id'] ] : false; |
||
| 228 | |||
| 229 | if ( $child_values ) { |
||
| 230 | $child_entries = FrmEntry::getAll( array( 'it.id' => (array) $child_values ) ); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | $field_value = array(); |
||
| 235 | |||
| 236 | if ( ! isset($child_entries) || ! $child_entries || ! FrmAppHelper::pro_is_installed() ) { |
||
| 237 | return $val; |
||
| 238 | } |
||
| 239 | |||
| 240 | foreach ( $child_entries as $child_entry ) { |
||
| 241 | $atts['item_id'] = $child_entry->id; |
||
| 242 | $atts['post_id'] = $child_entry->post_id; |
||
| 243 | |||
| 244 | // get the value for this field -- check for post values as well |
||
| 245 | $entry_val = FrmProEntryMetaHelper::get_post_or_meta_value($child_entry, $field); |
||
| 246 | |||
| 247 | if ( $entry_val ) { |
||
| 248 | // foreach entry get display_value |
||
| 249 | $field_value[] = self::display_value($entry_val, $field, $atts); |
||
| 250 | } |
||
| 251 | |||
| 252 | unset($child_entry); |
||
| 253 | } |
||
| 254 | |||
| 255 | $val = implode(', ', (array) $field_value ); |
||
| 256 | return self::kses( $val ); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @since 2.05.02 |
||
| 261 | */ |
||
| 262 | public static function kses( $value, $allowed_tags = array() ) { |
||
| 263 | $allowed_tags = apply_filters( 'frm_striphtml_allowed_tags', $allowed_tags ); |
||
| 264 | return wp_kses( $value, $allowed_tags ); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Prepare the saved value for display |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | public static function display_value( $value, $field, $atts = array() ) { |
||
| 338 | |||
| 339 | public static function set_posted_value( $field, $value, $args ) { |
||
| 350 | |||
| 351 | public static function get_posted_value( $field, &$value, $args ) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Check if field has an "Other" option and if any other values are posted |
||
| 365 | * |
||
| 366 | * @since 2.0 |
||
| 367 | * |
||
| 368 | * @param object $field |
||
| 369 | * @param string|array $value |
||
| 370 | * @param array $args |
||
| 371 | */ |
||
| 372 | public static function maybe_set_other_validation( $field, &$value, &$args ) { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Sets radio or checkbox value equal to "other" value if it is set - FOR REPEATING SECTIONS |
||
| 396 | * |
||
| 397 | * @since 2.0 |
||
| 398 | * |
||
| 399 | * @param object $field |
||
| 400 | * @param string|array $value |
||
| 401 | * @param array $args |
||
| 402 | */ |
||
| 403 | public static function set_other_repeating_vals( $field, &$value, &$args ) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Modify value used for validation |
||
| 423 | * This function essentially removes the "Other" radio or checkbox value from the $value being validated. |
||
| 424 | * It also adds any text from the free text fields to the value |
||
| 425 | * |
||
| 426 | * Needs to accommodate for times when other opt is selected, but no other free text is entered |
||
| 427 | * |
||
| 428 | * @since 2.0 |
||
| 429 | * |
||
| 430 | * @param string|array $value |
||
| 431 | * @param string|array $other_vals (usually of posted values) |
||
| 432 | * @param object $field |
||
| 433 | * @param array $args |
||
| 434 | */ |
||
| 435 | public static function set_other_validation_val( &$value, $other_vals, $field, &$args ) { |
||
| 471 | |||
| 472 | public static function enqueue_scripts( $params ) { |
||
| 476 | |||
| 477 | // Add submitted values to a string for spam checking |
||
| 478 | public static function entry_array_to_string( $values ) { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get the browser from the user agent |
||
| 498 | * |
||
| 499 | * @since 2.04 |
||
| 500 | * |
||
| 501 | * @param string $u_agent |
||
| 502 | * |
||
| 503 | * @return string |
||
| 504 | */ |
||
| 505 | public static function get_browser( $u_agent ) { |
||
| 572 | |||
| 573 | } |
||
| 574 |