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 FrmFieldsHelper 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 FrmFieldsHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class FrmFieldsHelper { |
||
| 7 | |||
| 8 | public static function setup_new_vars( $type = '', $form_id = '' ) { |
||
| 9 | |||
| 10 | if ( strpos($type, '|') ) { |
||
| 11 | list($type, $setting) = explode('|', $type); |
||
| 12 | } |
||
| 13 | |||
| 14 | $defaults = self::get_default_field_opts($type, $form_id); |
||
| 15 | $defaults['field_options']['custom_html'] = self::get_default_html($type); |
||
| 16 | |||
| 17 | $values = array(); |
||
| 18 | |||
| 19 | foreach ( $defaults as $var => $default ) { |
||
| 20 | if ( $var == 'field_options' ) { |
||
| 21 | $values['field_options'] = array(); |
||
| 22 | foreach ( $default as $opt_var => $opt_default ) { |
||
| 23 | $values['field_options'][ $opt_var ] = $opt_default; |
||
| 24 | unset($opt_var, $opt_default); |
||
| 25 | } |
||
| 26 | } else { |
||
| 27 | $values[ $var ] = $default; |
||
| 28 | } |
||
| 29 | unset($var, $default); |
||
| 30 | } |
||
| 31 | |||
| 32 | if ( isset( $setting ) && ! empty( $setting ) ) { |
||
| 33 | if ( in_array( $type, array( 'data', 'lookup' ) ) ) { |
||
| 34 | $values['field_options']['data_type'] = $setting; |
||
| 35 | } else { |
||
| 36 | $values['field_options'][ $setting ] = 1; |
||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | if ( $type == 'radio' || $type == 'checkbox' ) { |
||
| 41 | $values['options'] = serialize( array( |
||
| 42 | __( 'Option 1', 'formidable' ), |
||
| 43 | __( 'Option 2', 'formidable' ), |
||
| 44 | ) ); |
||
| 45 | } else if ( $type == 'select' ) { |
||
| 46 | $values['options'] = serialize( array( |
||
| 47 | '', |
||
| 48 | __( 'Option 1', 'formidable' ), |
||
| 49 | ) ); |
||
| 50 | } else if ( $type == 'textarea' ) { |
||
| 51 | $values['field_options']['max'] = '5'; |
||
| 52 | } else if ( $type == 'captcha' ) { |
||
| 53 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 54 | $values['invalid'] = $frm_settings->re_msg; |
||
| 55 | $values['field_options']['label'] = 'none'; |
||
| 56 | } else if ( 'url' == $type ) { |
||
| 57 | $values['name'] = __( 'Website', 'formidable' ); |
||
| 58 | } |
||
| 59 | |||
| 60 | $fields = FrmField::field_selection(); |
||
| 61 | $fields = array_merge($fields, FrmField::pro_field_selection()); |
||
| 62 | |||
| 63 | if ( isset( $fields[ $type ] ) ) { |
||
| 64 | $values['name'] = is_array( $fields[ $type ] ) ? $fields[ $type ]['name'] : $fields[ $type ]; |
||
| 65 | } |
||
| 66 | |||
| 67 | unset($fields); |
||
| 68 | |||
| 69 | return $values; |
||
| 70 | } |
||
| 71 | |||
| 72 | public static function get_html_id( $field, $plus = '' ) { |
||
| 73 | return apply_filters( 'frm_field_html_id', 'field_' . $field['field_key'] . $plus, $field ); |
||
| 74 | } |
||
| 75 | |||
| 76 | public static function setup_edit_vars( $record, $doing_ajax = false ) { |
||
| 77 | $values = array( |
||
| 78 | 'id' => $record->id, |
||
| 79 | 'form_id' => $record->form_id, |
||
| 80 | ); |
||
| 81 | $defaults = array( |
||
| 82 | 'name' => $record->name, |
||
| 83 | 'description' => $record->description, |
||
| 84 | 'field_key' => $record->field_key, |
||
| 85 | 'type' => $record->type, |
||
| 86 | 'default_value' => $record->default_value, |
||
| 87 | 'field_order' => $record->field_order, |
||
| 88 | 'required' => $record->required, |
||
| 89 | ); |
||
| 90 | |||
| 91 | if ( $doing_ajax ) { |
||
| 92 | $values = $values + $defaults; |
||
| 93 | $values['form_name'] = ''; |
||
| 94 | } else { |
||
| 95 | foreach ( $defaults as $var => $default ) { |
||
| 96 | $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'htmlspecialchars' ); |
||
| 97 | unset($var, $default); |
||
| 98 | } |
||
| 99 | |||
| 100 | $values['form_name'] = $record->form_id ? FrmForm::getName( $record->form_id ) : ''; |
||
| 101 | } |
||
| 102 | |||
| 103 | unset( $defaults ); |
||
| 104 | |||
| 105 | $values['options'] = $record->options; |
||
| 106 | $values['field_options'] = $record->field_options; |
||
| 107 | |||
| 108 | $defaults = self::get_default_field_opts($values['type'], $record, true); |
||
| 109 | |||
| 110 | if ( $values['type'] == 'captcha' ) { |
||
| 111 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 112 | $defaults['invalid'] = $frm_settings->re_msg; |
||
| 113 | } |
||
| 114 | |||
| 115 | foreach ( $defaults as $opt => $default ) { |
||
| 116 | $values[ $opt ] = isset( $record->field_options[ $opt ] ) ? $record->field_options[ $opt ] : $default; |
||
| 117 | unset($opt, $default); |
||
| 118 | } |
||
| 119 | |||
| 120 | $values['custom_html'] = ( isset( $record->field_options['custom_html'] ) ) ? $record->field_options['custom_html'] : self::get_default_html( $record->type ); |
||
| 121 | |||
| 122 | return apply_filters( 'frm_setup_edit_field_vars', $values, array( 'doing_ajax' => $doing_ajax ) ); |
||
| 123 | } |
||
| 124 | |||
| 125 | public static function get_default_field_opts( $type, $field, $limit = false ) { |
||
| 126 | $field_options = array( |
||
| 127 | 'size' => '', |
||
| 128 | 'max' => '', |
||
| 129 | 'label' => '', |
||
| 130 | 'blank' => '', |
||
| 131 | 'required_indicator' => '*', |
||
| 132 | 'invalid' => '', |
||
| 133 | 'separate_value' => 0, |
||
| 134 | 'clear_on_focus' => 0, |
||
| 135 | 'default_blank' => 0, |
||
| 136 | 'classes' => '', |
||
| 137 | 'custom_html' => '', |
||
| 138 | 'captcha_size' => 'normal', |
||
| 139 | 'captcha_theme' => 'light', |
||
| 140 | ); |
||
| 141 | |||
| 142 | if ( $limit ) { |
||
| 143 | return $field_options; |
||
| 144 | } |
||
| 145 | |||
| 146 | global $wpdb; |
||
| 147 | |||
| 148 | $form_id = ( is_numeric( $field ) ) ? $field : $field->form_id; |
||
| 149 | |||
| 150 | $key = is_numeric( $field ) ? FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_fields', 'field_key' ) : $field->field_key; |
||
| 151 | |||
| 152 | $field_count = FrmDb::get_var( 'frm_fields', array( 'form_id' => $form_id ), 'field_order', array( 'order_by' => 'field_order DESC' ) ); |
||
| 153 | |||
| 154 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 155 | return array( |
||
| 156 | 'name' => __( 'Untitled', 'formidable' ), |
||
| 157 | 'description' => '', |
||
| 158 | 'field_key' => $key, |
||
| 159 | 'type' => $type, |
||
| 160 | 'options' => '', |
||
| 161 | 'default_value' => '', |
||
| 162 | 'field_order' => $field_count + 1, |
||
| 163 | 'required' => false, |
||
| 164 | 'blank' => $frm_settings->blank_msg, |
||
| 165 | 'unique_msg' => $frm_settings->unique_msg, |
||
| 166 | 'invalid' => __( 'This field is invalid', 'formidable' ), |
||
| 167 | 'form_id' => $form_id, |
||
| 168 | 'field_options' => $field_options, |
||
| 169 | ); |
||
| 170 | } |
||
| 171 | |||
| 172 | public static function fill_field( &$values, $field, $form_id, $new_key = '' ) { |
||
| 173 | global $wpdb; |
||
| 174 | |||
| 175 | $values['field_key'] = FrmAppHelper::get_unique_key( $new_key, $wpdb->prefix . 'frm_fields', 'field_key' ); |
||
| 176 | $values['form_id'] = $form_id; |
||
| 177 | $values['options'] = maybe_serialize($field->options); |
||
| 178 | $values['default_value'] = maybe_serialize($field->default_value); |
||
| 179 | |||
| 180 | foreach ( array( 'name', 'description', 'type', 'field_order', 'field_options', 'required' ) as $col ) { |
||
| 181 | $values[ $col ] = $field->{$col}; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @since 2.0 |
||
| 187 | */ |
||
| 188 | public static function get_error_msg( $field, $error ) { |
||
| 189 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 190 | $default_settings = $frm_settings->default_options(); |
||
| 191 | $field_name = is_array( $field ) ? $field['name'] : $field->name; |
||
| 192 | |||
| 193 | $conf_msg = __( 'The entered values do not match', 'formidable' ); |
||
| 194 | $defaults = array( |
||
| 195 | 'unique_msg' => array( |
||
| 196 | 'full' => $default_settings['unique_msg'], |
||
| 197 | 'part' => sprintf( __('%s must be unique', 'formidable' ), $field_name ), |
||
| 198 | ), |
||
| 199 | 'invalid' => array( |
||
| 200 | 'full' => __( 'This field is invalid', 'formidable' ), |
||
| 201 | 'part' => sprintf( __('%s is invalid', 'formidable' ), $field_name ), |
||
| 202 | ), |
||
| 203 | 'blank' => array( |
||
| 204 | 'full' => $frm_settings->blank_msg, |
||
| 205 | 'part' => $frm_settings->blank_msg, |
||
| 206 | ), |
||
| 207 | 'conf_msg' => array( |
||
| 208 | 'full' => $conf_msg, |
||
| 209 | 'part' => $conf_msg, |
||
| 210 | ), |
||
| 211 | ); |
||
| 212 | |||
| 213 | $msg = FrmField::get_option( $field, $error ); |
||
| 214 | $msg = empty( $msg ) ? $defaults[ $error ]['part'] : $msg; |
||
| 215 | $msg = do_shortcode( $msg ); |
||
| 216 | return $msg; |
||
| 217 | } |
||
| 218 | |||
| 219 | public static function get_form_fields( $form_id, $error = array() ) { |
||
| 220 | $fields = FrmField::get_all_for_form( $form_id ); |
||
| 221 | $fields = apply_filters( 'frm_get_paged_fields', $fields, $form_id, $error ); |
||
| 222 | return $fields; |
||
| 223 | } |
||
| 224 | |||
| 225 | public static function get_default_html( $type = 'text' ) { |
||
| 226 | if ( apply_filters( 'frm_normal_field_type_html', true, $type ) ) { |
||
| 227 | $input = ( in_array( $type, array( 'radio', 'checkbox', 'data' ) ) ) ? '<div class="frm_opt_container">[input]</div>' : '[input]'; |
||
| 228 | $for = ''; |
||
| 229 | if ( ! in_array( $type, array( 'radio', 'checkbox', 'data', 'scale' ) ) ) { |
||
| 230 | $for = 'for="field_[key]"'; |
||
| 231 | } |
||
| 232 | |||
| 233 | $default_html = <<<DEFAULT_HTML |
||
| 234 | <div id="frm_field_[id]_container" class="frm_form_field form-field [required_class][error_class]"> |
||
| 235 | <label $for class="frm_primary_label">[field_name] |
||
| 236 | <span class="frm_required">[required_label]</span> |
||
| 237 | </label> |
||
| 238 | $input |
||
| 239 | [if description]<div class="frm_description">[description]</div>[/if description] |
||
| 240 | [if error]<div class="frm_error">[error]</div>[/if error] |
||
| 241 | </div> |
||
| 242 | DEFAULT_HTML; |
||
| 243 | } else { |
||
| 244 | $default_html = apply_filters('frm_other_custom_html', '', $type); |
||
| 245 | } |
||
| 246 | |||
| 247 | return apply_filters('frm_custom_html', $default_html, $type); |
||
| 248 | } |
||
| 249 | |||
| 250 | public static function replace_shortcodes( $html, $field, $errors = array(), $form = false, $args = array() ) { |
||
| 251 | $html = apply_filters('frm_before_replace_shortcodes', $html, $field, $errors, $form); |
||
| 252 | |||
| 253 | $defaults = array( |
||
| 254 | 'field_name' => 'item_meta[' . $field['id'] . ']', |
||
| 255 | 'field_id' => $field['id'], |
||
| 256 | 'field_plus_id' => '', |
||
| 257 | 'section_id' => '', |
||
| 258 | ); |
||
| 259 | $args = wp_parse_args($args, $defaults); |
||
| 260 | $field_name = $args['field_name']; |
||
| 261 | $field_id = $args['field_id']; |
||
| 262 | $html_id = self::get_html_id($field, $args['field_plus_id']); |
||
| 263 | |||
| 264 | if ( FrmField::is_multiple_select($field) ) { |
||
| 265 | $field_name .= '[]'; |
||
| 266 | } |
||
| 267 | |||
| 268 | //replace [id] |
||
| 269 | $html = str_replace('[id]', $field_id, $html); |
||
| 270 | |||
| 271 | // Remove the for attribute for captcha |
||
| 272 | if ( $field['type'] == 'captcha' ) { |
||
| 273 | $html = str_replace(' for="field_[key]"', '', $html); |
||
| 274 | } |
||
| 275 | |||
| 276 | // set the label for |
||
| 277 | $html = str_replace('field_[key]', $html_id, $html); |
||
| 278 | |||
| 279 | //replace [key] |
||
| 280 | $html = str_replace('[key]', $field['field_key'], $html); |
||
| 281 | |||
| 282 | //replace [description] and [required_label] and [error] |
||
| 283 | $required = FrmField::is_required( $field ) ? $field['required_indicator'] : ''; |
||
| 284 | if ( ! is_array( $errors ) ) { |
||
| 285 | $errors = array(); |
||
| 286 | } |
||
| 287 | $error = isset( $errors[ 'field' . $field_id ] ) ? $errors[ 'field' . $field_id ] : false; |
||
| 288 | |||
| 289 | //If field type is section heading, add class so a bottom margin can be added to either the h3 or description |
||
| 290 | if ( $field['type'] == 'divider' ) { |
||
| 291 | if ( FrmField::is_option_true( $field, 'description' ) ) { |
||
| 292 | $html = str_replace( 'frm_description', 'frm_description frm_section_spacing', $html ); |
||
| 293 | } else { |
||
| 294 | $html = str_replace('[label_position]', '[label_position] frm_section_spacing', $html); |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | $codes = array( |
||
| 299 | 'description' => $field['description'], |
||
| 300 | 'required_label' => $required, |
||
| 301 | 'error' => $error, |
||
| 302 | ); |
||
| 303 | foreach ( $codes as $code => $value ) { |
||
| 304 | self::remove_inline_conditions( ( $value && $value != '' ), $code, $value, $html ); |
||
| 305 | } |
||
| 306 | |||
| 307 | //replace [required_class] |
||
| 308 | $required_class = FrmField::is_required( $field ) ? ' frm_required_field' : ''; |
||
| 309 | $html = str_replace('[required_class]', $required_class, $html); |
||
| 310 | |||
| 311 | //replace [label_position] |
||
| 312 | $field['label'] = self::label_position( $field['label'], $field, $form ); |
||
| 313 | self::add_class_to_label( $field, $html ); |
||
| 314 | |||
| 315 | //replace [field_name] |
||
| 316 | $html = str_replace('[field_name]', $field['name'], $html); |
||
| 317 | |||
| 318 | self::add_field_div_classes( $field_id, $field, $errors, $html ); |
||
| 319 | |||
| 320 | //replace [entry_key] |
||
| 321 | $entry_key = FrmAppHelper::simple_get( 'entry', 'sanitize_title' ); |
||
| 322 | $html = str_replace('[entry_key]', $entry_key, $html); |
||
| 323 | |||
| 324 | if ( $form ) { |
||
| 325 | $form = (array) $form; |
||
| 326 | |||
| 327 | //replace [form_key] |
||
| 328 | $html = str_replace('[form_key]', $form['form_key'], $html); |
||
| 329 | |||
| 330 | //replace [form_name] |
||
| 331 | $html = str_replace('[form_name]', $form['name'], $html); |
||
| 332 | } |
||
| 333 | |||
| 334 | self::process_wp_shortcodes( $html ); |
||
| 335 | |||
| 336 | //replace [input] |
||
| 337 | preg_match_all("/\[(input|deletelink)\b(.*?)(?:(\/))?\]/s", $html, $shortcodes, PREG_PATTERN_ORDER); |
||
| 338 | global $frm_vars; |
||
| 339 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 340 | |||
| 341 | foreach ( $shortcodes[0] as $short_key => $tag ) { |
||
| 342 | $atts = FrmShortcodeHelper::get_shortcode_attribute_array( $shortcodes[2][ $short_key ] ); |
||
| 343 | $tag = self::get_shortcode_tag( $shortcodes, $short_key, array( |
||
| 344 | 'conditional' => false, |
||
| 345 | 'conditional_check' => false, |
||
| 346 | ) ); |
||
| 347 | |||
| 348 | $replace_with = ''; |
||
| 349 | |||
| 350 | if ( $tag == 'input' ) { |
||
| 351 | if ( isset($atts['opt']) ) { |
||
| 352 | $atts['opt']--; |
||
| 353 | } |
||
| 354 | |||
| 355 | $field['input_class'] = isset($atts['class']) ? $atts['class'] : ''; |
||
| 356 | if ( isset($atts['class']) ) { |
||
| 357 | unset($atts['class']); |
||
| 358 | } |
||
| 359 | |||
| 360 | $field['shortcodes'] = $atts; |
||
| 361 | ob_start(); |
||
| 362 | include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/input.php' ); |
||
| 363 | $replace_with = ob_get_contents(); |
||
| 364 | ob_end_clean(); |
||
| 365 | } else if ( $tag == 'deletelink' && FrmAppHelper::pro_is_installed() ) { |
||
| 366 | $replace_with = FrmProEntriesController::entry_delete_link($atts); |
||
| 367 | } |
||
| 368 | |||
| 369 | $html = str_replace( $shortcodes[0][ $short_key ], $replace_with, $html ); |
||
| 370 | } |
||
| 371 | |||
| 372 | $html .= "\n"; |
||
| 373 | |||
| 374 | //Return html if conf_field to prevent loop |
||
| 375 | if ( isset($field['conf_field']) && $field['conf_field'] == 'stop' ) { |
||
| 376 | return $html; |
||
| 377 | } |
||
| 378 | |||
| 379 | //If field is in repeating section |
||
| 380 | if ( $args['section_id'] ) { |
||
| 381 | $html = apply_filters( 'frm_replace_shortcodes', $html, $field, array( |
||
| 382 | 'errors' => $errors, |
||
| 383 | 'form' => $form, |
||
| 384 | 'field_name' => $field_name, |
||
| 385 | 'field_id' => $field_id, |
||
| 386 | 'field_plus_id' => $args['field_plus_id'], |
||
| 387 | 'section_id' => $args['section_id'], |
||
| 388 | ) ); |
||
| 389 | } else { |
||
| 390 | $html = apply_filters( 'frm_replace_shortcodes', $html, $field, array( |
||
| 391 | 'errors' => $errors, |
||
| 392 | 'form' => $form, |
||
| 393 | ) ); |
||
| 394 | } |
||
| 395 | |||
| 396 | self::remove_collapse_shortcode( $html ); |
||
| 397 | |||
| 398 | return $html; |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Get the class to use for the label position |
||
| 403 | * @since 2.05 |
||
| 404 | */ |
||
| 405 | private static function &label_position( $position, $field, $form ) { |
||
| 406 | if ( $position && $position != '' ) { |
||
| 407 | return $position; |
||
| 408 | } |
||
| 409 | |||
| 410 | $position = FrmStylesController::get_style_val( 'position', $form ); |
||
| 411 | if ( $position == 'none' ) { |
||
| 412 | $position = 'top'; |
||
| 413 | } elseif ( $position == 'no_label' ) { |
||
| 414 | $position = 'none'; |
||
| 415 | } elseif ( $position == 'inside' && ! self::is_placeholder_field_type( $field['type'] ) ) { |
||
| 416 | $position = 'top'; |
||
| 417 | } |
||
| 418 | |||
| 419 | $position = apply_filters( 'frm_html_label_position', $position, $field, $form ); |
||
| 420 | $position = ( ! empty( $position ) ) ? $position : 'top'; |
||
| 421 | |||
| 422 | return $position; |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Check if this field type allows placeholders |
||
| 427 | * @since 2.05 |
||
| 428 | */ |
||
| 429 | public static function is_placeholder_field_type( $type ) { |
||
| 430 | return ! in_array( $type, array( 'select', 'radio', 'checkbox', 'hidden' ) ); |
||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Add the label position class into the HTML |
||
| 435 | * If the label position is inside, add a class to show the label if the field has a value. |
||
| 436 | * |
||
| 437 | * @since 2.05 |
||
| 438 | */ |
||
| 439 | private static function add_class_to_label( $field, &$html ) { |
||
| 440 | $label_class = in_array( $field['type'], array( 'divider', 'end_divider', 'break' ) ) ? $field['label'] : ' frm_primary_label'; |
||
| 441 | $html = str_replace( '[label_position]', $label_class, $html ); |
||
| 442 | if ( $field['label'] == 'inside' && $field['value'] != '' ) { |
||
| 443 | $html = str_replace( 'frm_primary_label', 'frm_primary_label frm_visible', $html ); |
||
| 444 | } |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * This filters shortcodes in the field HTML |
||
| 449 | * |
||
| 450 | * @since 2.02.11 |
||
| 451 | */ |
||
| 452 | private static function process_wp_shortcodes( &$html ) { |
||
| 453 | if ( apply_filters( 'frm_do_html_shortcodes', true ) ) { |
||
| 454 | $html = do_shortcode( $html ); |
||
| 455 | } |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Add classes to a field div |
||
| 460 | * |
||
| 461 | * @since 2.02.05 |
||
| 462 | * |
||
| 463 | * @param string $field_id |
||
| 464 | * @param array $field |
||
| 465 | * @param array $errors |
||
| 466 | * @param string $html |
||
| 467 | */ |
||
| 468 | private static function add_field_div_classes( $field_id, $field, $errors, &$html ) { |
||
| 469 | $classes = self::get_field_div_classes( $field_id, $field, $errors, $html ); |
||
| 470 | |||
| 471 | if ( $field['type'] == 'html' && strpos( $html, '[error_class]' ) === false ) { |
||
| 472 | // there is no error_class shortcode for HTML fields |
||
| 473 | $html = str_replace( 'class="frm_form_field', 'class="frm_form_field ' . $classes, $html ); |
||
| 474 | } |
||
| 475 | $html = str_replace( '[error_class]', $classes, $html ); |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Get the classes for a field div |
||
| 480 | * |
||
| 481 | * @since 2.02.05 |
||
| 482 | * |
||
| 483 | * @param string $field_id |
||
| 484 | * @param array $field |
||
| 485 | * @param array $errors |
||
| 486 | * @param string $html |
||
| 487 | * @return string $classes |
||
| 488 | */ |
||
| 489 | private static function get_field_div_classes( $field_id, $field, $errors, $html ) { |
||
| 490 | // Add error class |
||
| 491 | $classes = isset( $errors[ 'field' . $field_id ] ) ? ' frm_blank_field' : ''; |
||
| 492 | |||
| 493 | // Add label position class |
||
| 494 | $classes .= ' frm_' . $field['label'] . '_container'; |
||
| 495 | |||
| 496 | // Add CSS layout classes |
||
| 497 | if ( ! empty( $field['classes'] ) ) { |
||
| 498 | if ( ! strpos( $html, 'frm_form_field ') ) { |
||
| 499 | $classes .= ' frm_form_field'; |
||
| 500 | } |
||
| 501 | $classes .= ' ' . $field['classes']; |
||
| 502 | } |
||
| 503 | |||
| 504 | // Get additional classes |
||
| 505 | $classes = apply_filters( 'frm_field_div_classes', $classes, $field, array( 'field_id' => $field_id ) ); |
||
| 506 | |||
| 507 | return $classes; |
||
| 508 | } |
||
| 509 | |||
| 510 | public static function remove_inline_conditions( $no_vars, $code, $replace_with, &$html ) { |
||
| 511 | if ( $no_vars ) { |
||
| 512 | $html = str_replace( '[if ' . $code . ']', '', $html ); |
||
| 513 | $html = str_replace( '[/if ' . $code . ']', '', $html ); |
||
| 514 | } else { |
||
| 515 | $html = preg_replace( '/(\[if\s+' . $code . '\])(.*?)(\[\/if\s+' . $code . '\])/mis', '', $html ); |
||
| 516 | } |
||
| 517 | |||
| 518 | $html = str_replace( '[' . $code . ']', $replace_with, $html ); |
||
| 519 | } |
||
| 520 | |||
| 521 | public static function get_shortcode_tag( $shortcodes, $short_key, $args ) { |
||
| 522 | $args = wp_parse_args( $args, array( |
||
| 523 | 'conditional' => false, |
||
| 524 | 'conditional_check' => false, |
||
| 525 | 'foreach' => false, |
||
| 526 | ) ); |
||
| 527 | if ( ( $args['conditional'] || $args['foreach'] ) && ! $args['conditional_check'] ) { |
||
| 528 | $args['conditional_check'] = true; |
||
| 529 | } |
||
| 530 | |||
| 531 | $prefix = ''; |
||
| 532 | if ( $args['conditional_check'] ) { |
||
| 533 | if ( $args['conditional'] ) { |
||
| 534 | $prefix = 'if '; |
||
| 535 | } else if ( $args['foreach'] ) { |
||
| 536 | $prefix = 'foreach '; |
||
| 537 | } |
||
| 538 | } |
||
| 539 | |||
| 540 | $with_tags = $args['conditional_check'] ? 3 : 2; |
||
| 541 | if ( ! empty( $shortcodes[ $with_tags ][ $short_key ] ) ) { |
||
| 542 | $tag = str_replace( '[' . $prefix, '', $shortcodes[0][ $short_key ] ); |
||
| 543 | $tag = str_replace(']', '', $tag); |
||
| 544 | $tags = explode(' ', $tag); |
||
| 545 | if ( is_array($tags) ) { |
||
| 546 | $tag = $tags[0]; |
||
| 547 | } |
||
| 548 | } else { |
||
| 549 | $tag = $shortcodes[ $with_tags - 1 ][ $short_key ]; |
||
| 550 | } |
||
| 551 | |||
| 552 | return $tag; |
||
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Remove [collapse_this] if it's still included after all processing |
||
| 557 | * @since 2.0.8 |
||
| 558 | */ |
||
| 559 | private static function remove_collapse_shortcode( &$html ) { |
||
| 560 | if ( preg_match( '/\[(collapse_this)\]/s', $html ) ) { |
||
| 561 | $html = str_replace( '[collapse_this]', '', $html ); |
||
| 562 | } |
||
| 563 | } |
||
| 564 | |||
| 565 | public static function get_checkbox_id( $field, $opt_key ) { |
||
| 566 | $id = $field['id']; |
||
| 567 | if ( isset( $field['in_section'] ) && $field['in_section'] ) { |
||
| 568 | $id .= '-' . $field['in_section']; |
||
| 569 | } |
||
| 570 | return 'frm_checkbox_' . $id . '-' . $opt_key; |
||
| 571 | } |
||
| 572 | |||
| 573 | public static function display_recaptcha( $field ) { |
||
| 574 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 575 | $lang = apply_filters( 'frm_recaptcha_lang', $frm_settings->re_lang, $field ); |
||
| 576 | |||
| 577 | $class_prefix = ''; |
||
| 578 | $api_js_url = 'https://www.google.com/recaptcha/api.js?'; |
||
| 579 | |||
| 580 | $allow_mutiple = $frm_settings->re_multi; |
||
| 581 | if ( $allow_mutiple ) { |
||
| 582 | $api_js_url .= '&onload=frmRecaptcha&render=explicit'; |
||
| 583 | $class_prefix = 'frm-'; |
||
| 584 | } |
||
| 585 | |||
| 586 | if ( ! empty( $lang ) ) { |
||
| 587 | $api_js_url .= '&hl=' . $lang; |
||
| 588 | } |
||
| 589 | $api_js_url = apply_filters( 'frm_recaptcha_js_url', $api_js_url ); |
||
| 590 | |||
| 591 | wp_register_script( 'recaptcha-api', $api_js_url, array( 'formidable' ), true ); |
||
| 592 | wp_enqueue_script( 'recaptcha-api' ); |
||
| 593 | |||
| 594 | // for reverse compatibility |
||
| 595 | $field['captcha_size'] = ( $field['captcha_size'] == 'default' ) ? 'normal' : $field['captcha_size']; |
||
| 596 | $field['captcha_size'] = ( $frm_settings->re_type == 'invisible' ) ? 'invisible' : $field['captcha_size']; |
||
| 597 | |||
| 598 | ?> |
||
| 599 | <div id="field_<?php echo esc_attr( $field['field_key'] ) ?>" class="<?php echo esc_attr( $class_prefix ) ?>g-recaptcha" data-sitekey="<?php echo esc_attr( $frm_settings->pubkey ) ?>" data-size="<?php echo esc_attr( $field['captcha_size'] ) ?>" data-theme="<?php echo esc_attr( $field['captcha_theme'] ) ?>" <?php if ( 'invisible' === $field['captcha_size'] && ! $allow_mutiple ) { echo 'data-callback="frmAfterRecaptcha"'; } ?>></div> |
||
| 600 | <?php |
||
| 601 | } |
||
| 602 | |||
| 603 | public static function show_single_option( $field ) { |
||
| 604 | $field_name = $field['name']; |
||
| 605 | $html_id = self::get_html_id($field); |
||
| 606 | foreach ( $field['options'] as $opt_key => $opt ) { |
||
| 607 | $field_val = apply_filters('frm_field_value_saved', $opt, $opt_key, $field); |
||
| 608 | $opt = apply_filters('frm_field_label_seen', $opt, $opt_key, $field); |
||
| 609 | |||
| 610 | // If this is an "Other" option, get the HTML for it |
||
| 611 | if ( self::is_other_opt( $opt_key ) ) { |
||
| 612 | // Get string for Other text field, if needed |
||
| 613 | $other_val = self::get_other_val( compact( 'opt_key', 'field' ) ); |
||
| 614 | require( FrmAppHelper::plugin_path() . '/pro/classes/views/frmpro-fields/other-option.php' ); |
||
| 615 | } else { |
||
| 616 | require( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/single-option.php' ); |
||
| 617 | } |
||
| 618 | } |
||
| 619 | } |
||
| 620 | |||
| 621 | public static function get_term_link( $tax_id ) { |
||
| 622 | $tax = get_taxonomy($tax_id); |
||
| 623 | if ( ! $tax ) { |
||
| 624 | return; |
||
| 625 | } |
||
| 626 | |||
| 627 | $link = sprintf( |
||
| 628 | __( 'Please add options from the WordPress "%1$s" page', 'formidable' ), |
||
| 629 | '<a href="' . esc_url( admin_url( 'edit-tags.php?taxonomy=' . $tax->name ) ) . '" target="_blank">' . ( empty( $tax->labels->name ) ? __( 'Categories' ) : $tax->labels->name ) . '</a>' |
||
|
|
|||
| 630 | ); |
||
| 631 | unset($tax); |
||
| 632 | |||
| 633 | return $link; |
||
| 634 | } |
||
| 635 | |||
| 636 | public static function value_meets_condition( $observed_value, $cond, $hide_opt ) { |
||
| 637 | $hide_opt = self::get_value_for_comparision( $hide_opt ); |
||
| 638 | $observed_value = self::get_value_for_comparision( $observed_value ); |
||
| 639 | |||
| 640 | if ( is_array($observed_value) ) { |
||
| 641 | return self::array_value_condition($observed_value, $cond, $hide_opt); |
||
| 642 | } |
||
| 643 | |||
| 644 | $m = false; |
||
| 645 | if ( $cond == '==' ) { |
||
| 646 | $m = $observed_value == $hide_opt; |
||
| 647 | } else if ( $cond == '!=' ) { |
||
| 648 | $m = $observed_value != $hide_opt; |
||
| 649 | } else if ( $cond == '>' ) { |
||
| 650 | $m = $observed_value > $hide_opt; |
||
| 651 | } else if ( $cond == '<' ) { |
||
| 652 | $m = $observed_value < $hide_opt; |
||
| 653 | } else if ( $cond == 'LIKE' || $cond == 'not LIKE' ) { |
||
| 654 | $m = stripos($observed_value, $hide_opt); |
||
| 655 | if ( $cond == 'not LIKE' ) { |
||
| 656 | $m = ( $m === false ) ? true : false; |
||
| 657 | } else { |
||
| 658 | $m = ( $m === false ) ? false : true; |
||
| 659 | } |
||
| 660 | } |
||
| 661 | return $m; |
||
| 662 | } |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Trim and sanitize the values |
||
| 666 | * @since 2.05 |
||
| 667 | */ |
||
| 668 | private static function get_value_for_comparision( $value ) { |
||
| 669 | // Remove white space from hide_opt |
||
| 670 | if ( ! is_array( $value ) ) { |
||
| 671 | $value = trim( $value ); |
||
| 672 | } |
||
| 673 | |||
| 674 | return wp_kses_post( $value ); |
||
| 675 | } |
||
| 676 | |||
| 677 | public static function array_value_condition( $observed_value, $cond, $hide_opt ) { |
||
| 678 | $m = false; |
||
| 679 | if ( $cond == '==' ) { |
||
| 680 | if ( is_array($hide_opt) ) { |
||
| 681 | $m = array_intersect($hide_opt, $observed_value); |
||
| 682 | $m = empty($m) ? false : true; |
||
| 683 | } else { |
||
| 684 | $m = in_array($hide_opt, $observed_value); |
||
| 685 | } |
||
| 686 | } else if ( $cond == '!=' ) { |
||
| 687 | $m = ! in_array($hide_opt, $observed_value); |
||
| 688 | } else if ( $cond == '>' ) { |
||
| 689 | $min = min($observed_value); |
||
| 690 | $m = $min > $hide_opt; |
||
| 691 | } else if ( $cond == '<' ) { |
||
| 692 | $max = max($observed_value); |
||
| 693 | $m = $max < $hide_opt; |
||
| 694 | } else if ( $cond == 'LIKE' || $cond == 'not LIKE' ) { |
||
| 695 | foreach ( $observed_value as $ob ) { |
||
| 696 | $m = strpos($ob, $hide_opt); |
||
| 697 | if ( $m !== false ) { |
||
| 698 | $m = true; |
||
| 699 | break; |
||
| 700 | } |
||
| 701 | } |
||
| 702 | |||
| 703 | if ( $cond == 'not LIKE' ) { |
||
| 704 | $m = ( $m === false ) ? true : false; |
||
| 705 | } |
||
| 706 | } |
||
| 707 | |||
| 708 | return $m; |
||
| 709 | } |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Replace a few basic shortcodes and field ids |
||
| 713 | * @since 2.0 |
||
| 714 | * @return string |
||
| 715 | */ |
||
| 716 | public static function basic_replace_shortcodes( $value, $form, $entry ) { |
||
| 717 | if ( strpos($value, '[sitename]') !== false ) { |
||
| 718 | $new_value = wp_specialchars_decode( FrmAppHelper::site_name(), ENT_QUOTES ); |
||
| 719 | $value = str_replace('[sitename]', $new_value, $value); |
||
| 720 | } |
||
| 721 | |||
| 722 | $value = apply_filters('frm_content', $value, $form, $entry); |
||
| 723 | $value = do_shortcode($value); |
||
| 724 | |||
| 725 | return $value; |
||
| 726 | } |
||
| 727 | |||
| 728 | public static function get_shortcodes( $content, $form_id ) { |
||
| 729 | if ( FrmAppHelper::pro_is_installed() ) { |
||
| 730 | return FrmProDisplaysHelper::get_shortcodes($content, $form_id); |
||
| 731 | } |
||
| 732 | |||
| 733 | $fields = FrmField::getAll( array( |
||
| 734 | 'fi.form_id' => (int) $form_id, |
||
| 735 | 'fi.type not' => FrmField::no_save_fields(), |
||
| 736 | ) ); |
||
| 737 | |||
| 738 | $tagregexp = self::allowed_shortcodes($fields); |
||
| 739 | |||
| 740 | preg_match_all("/\[(if )?($tagregexp)\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?/s", $content, $matches, PREG_PATTERN_ORDER); |
||
| 741 | |||
| 742 | return $matches; |
||
| 743 | } |
||
| 744 | |||
| 745 | public static function allowed_shortcodes( $fields = array() ) { |
||
| 746 | $tagregexp = array( 'editlink', 'id', 'key', 'ip', 'siteurl', 'sitename', 'admin_email', 'post[-|_]id', 'created[-|_]at', 'updated[-|_]at', 'updated[-|_]by', 'parent[-|_]id' ); |
||
| 747 | |||
| 748 | foreach ( $fields as $field ) { |
||
| 749 | $tagregexp[] = $field->id; |
||
| 750 | $tagregexp[] = $field->field_key; |
||
| 751 | } |
||
| 752 | |||
| 753 | $tagregexp = implode('|', $tagregexp); |
||
| 754 | return $tagregexp; |
||
| 755 | } |
||
| 756 | |||
| 757 | public static function replace_content_shortcodes( $content, $entry, $shortcodes ) { |
||
| 758 | $shortcode_values = array( |
||
| 759 | 'id' => $entry->id, |
||
| 760 | 'key' => $entry->item_key, |
||
| 761 | 'ip' => $entry->ip, |
||
| 762 | ); |
||
| 763 | |||
| 764 | foreach ( $shortcodes[0] as $short_key => $tag ) { |
||
| 765 | if ( empty( $tag ) ) { |
||
| 766 | continue; |
||
| 767 | } |
||
| 768 | |||
| 769 | $atts = FrmShortcodeHelper::get_shortcode_attribute_array( $shortcodes[3][ $short_key ] ); |
||
| 770 | |||
| 771 | if ( ! empty( $shortcodes[3][ $short_key ] ) ) { |
||
| 772 | $tag = str_replace( array( '[', ']' ), '', $shortcodes[0][ $short_key ] ); |
||
| 773 | $tags = explode(' ', $tag); |
||
| 774 | if ( is_array($tags) ) { |
||
| 775 | $tag = $tags[0]; |
||
| 776 | } |
||
| 777 | } else { |
||
| 778 | $tag = $shortcodes[2][ $short_key ]; |
||
| 779 | } |
||
| 780 | |||
| 781 | switch ( $tag ) { |
||
| 782 | case 'id': |
||
| 783 | case 'key': |
||
| 784 | case 'ip': |
||
| 785 | $replace_with = $shortcode_values[ $tag ]; |
||
| 786 | break; |
||
| 787 | |||
| 788 | case 'user_agent': |
||
| 789 | case 'user-agent': |
||
| 790 | $entry->description = maybe_unserialize($entry->description); |
||
| 791 | $replace_with = FrmEntriesHelper::get_browser( $entry->description['browser'] ); |
||
| 792 | break; |
||
| 793 | |||
| 794 | case 'created_at': |
||
| 795 | case 'created-at': |
||
| 796 | case 'updated_at': |
||
| 797 | case 'updated-at': |
||
| 798 | if ( isset($atts['format']) ) { |
||
| 799 | $time_format = ' '; |
||
| 800 | } else { |
||
| 801 | $atts['format'] = get_option('date_format'); |
||
| 802 | $time_format = ''; |
||
| 803 | } |
||
| 804 | |||
| 805 | $this_tag = str_replace('-', '_', $tag); |
||
| 806 | $replace_with = FrmAppHelper::get_formatted_time($entry->{$this_tag}, $atts['format'], $time_format); |
||
| 807 | unset($this_tag); |
||
| 808 | break; |
||
| 809 | |||
| 810 | case 'created_by': |
||
| 811 | case 'created-by': |
||
| 812 | case 'updated_by': |
||
| 813 | case 'updated-by': |
||
| 814 | $this_tag = str_replace('-', '_', $tag); |
||
| 815 | $replace_with = self::get_display_value( $entry->{$this_tag}, (object) array( 'type' => 'user_id' ), $atts ); |
||
| 816 | unset($this_tag); |
||
| 817 | break; |
||
| 818 | |||
| 819 | case 'admin_email': |
||
| 820 | case 'siteurl': |
||
| 821 | case 'frmurl': |
||
| 822 | case 'sitename': |
||
| 823 | case 'get': |
||
| 824 | $replace_with = self::dynamic_default_values( $tag, $atts ); |
||
| 825 | break; |
||
| 826 | |||
| 827 | default: |
||
| 828 | $field = FrmField::getOne( $tag ); |
||
| 829 | if ( ! $field ) { |
||
| 830 | break; |
||
| 831 | } |
||
| 832 | |||
| 833 | $sep = isset($atts['sep']) ? $atts['sep'] : ', '; |
||
| 834 | |||
| 835 | $replace_with = FrmEntryMeta::get_meta_value( $entry, $field->id ); |
||
| 836 | |||
| 837 | $atts['entry_id'] = $entry->id; |
||
| 838 | $atts['entry_key'] = $entry->item_key; |
||
| 839 | |||
| 840 | if ( isset($atts['show']) && $atts['show'] == 'field_label' ) { |
||
| 841 | $replace_with = $field->name; |
||
| 842 | } else if ( isset($atts['show']) && $atts['show'] == 'description' ) { |
||
| 843 | $replace_with = $field->description; |
||
| 844 | } else { |
||
| 845 | $string_value = $replace_with; |
||
| 846 | if ( is_array( $replace_with ) ) { |
||
| 847 | $string_value = implode( $sep, $replace_with ); |
||
| 848 | } |
||
| 849 | |||
| 850 | if ( empty( $string_value ) && $string_value != '0' ) { |
||
| 851 | $replace_with = ''; |
||
| 852 | } else { |
||
| 853 | $replace_with = self::get_display_value( $replace_with, $field, $atts ); |
||
| 854 | } |
||
| 855 | } |
||
| 856 | |||
| 857 | unset($field); |
||
| 858 | } |
||
| 859 | |||
| 860 | if ( isset($replace_with) ) { |
||
| 861 | $content = str_replace( $shortcodes[0][ $short_key ], $replace_with, $content ); |
||
| 862 | } |
||
| 863 | |||
| 864 | unset($atts, $conditional, $replace_with); |
||
| 865 | } |
||
| 866 | |||
| 867 | return $content; |
||
| 868 | } |
||
| 869 | |||
| 870 | /** |
||
| 871 | * Get the value to replace a few standard shortcodes |
||
| 872 | * |
||
| 873 | * @since 2.0 |
||
| 874 | * @return string |
||
| 875 | */ |
||
| 876 | public static function dynamic_default_values( $tag, $atts = array(), $return_array = false ) { |
||
| 877 | $new_value = ''; |
||
| 878 | switch ( $tag ) { |
||
| 879 | case 'admin_email': |
||
| 880 | $new_value = get_option('admin_email'); |
||
| 881 | break; |
||
| 882 | case 'siteurl': |
||
| 883 | $new_value = FrmAppHelper::site_url(); |
||
| 884 | break; |
||
| 885 | case 'frmurl': |
||
| 886 | $new_value = FrmAppHelper::plugin_url(); |
||
| 887 | break; |
||
| 888 | case 'sitename': |
||
| 889 | $new_value = FrmAppHelper::site_name(); |
||
| 890 | break; |
||
| 891 | case 'get': |
||
| 892 | $new_value = self::process_get_shortcode( $atts, $return_array ); |
||
| 893 | break; |
||
| 894 | } |
||
| 895 | |||
| 896 | return $new_value; |
||
| 897 | } |
||
| 898 | |||
| 899 | /** |
||
| 900 | * Process the [get] shortcode |
||
| 901 | * |
||
| 902 | * @since 2.0 |
||
| 903 | * @return string|array |
||
| 904 | */ |
||
| 905 | public static function process_get_shortcode( $atts, $return_array = false ) { |
||
| 906 | if ( ! isset($atts['param']) ) { |
||
| 907 | return ''; |
||
| 908 | } |
||
| 909 | |||
| 910 | if ( strpos($atts['param'], '[') ) { |
||
| 911 | $atts['param'] = str_replace('[', '[', $atts['param']); |
||
| 912 | $atts['param'] = str_replace(']', ']', $atts['param']); |
||
| 913 | } |
||
| 914 | |||
| 915 | $new_value = FrmAppHelper::get_param( $atts['param'], '', 'get', 'sanitize_text_field' ); |
||
| 916 | $new_value = FrmAppHelper::get_query_var( $new_value, $atts['param'] ); |
||
| 917 | |||
| 918 | if ( $new_value == '' ) { |
||
| 919 | if ( ! isset($atts['prev_val']) ) { |
||
| 920 | $atts['prev_val'] = ''; |
||
| 921 | } |
||
| 922 | |||
| 923 | $new_value = isset($atts['default']) ? $atts['default'] : $atts['prev_val']; |
||
| 924 | } |
||
| 925 | |||
| 926 | if ( is_array($new_value) && ! $return_array ) { |
||
| 927 | $new_value = implode(', ', $new_value); |
||
| 928 | } |
||
| 929 | |||
| 930 | return $new_value; |
||
| 931 | } |
||
| 932 | |||
| 933 | public static function get_display_value( $replace_with, $field, $atts = array() ) { |
||
| 934 | $sep = isset( $atts['sep'] ) ? $atts['sep'] : ', '; |
||
| 935 | |||
| 936 | $replace_with = apply_filters( 'frm_get_' . $field->type . '_display_value', $replace_with, $field, $atts ); |
||
| 937 | $replace_with = apply_filters( 'frm_get_display_value', $replace_with, $field, $atts ); |
||
| 938 | |||
| 939 | if ( $field->type == 'textarea' || $field->type == 'rte' ) { |
||
| 940 | $autop = isset($atts['wpautop']) ? $atts['wpautop'] : true; |
||
| 941 | if ( apply_filters('frm_use_wpautop', $autop) ) { |
||
| 942 | if ( is_array($replace_with) ) { |
||
| 943 | $replace_with = implode("\n", $replace_with); |
||
| 944 | } |
||
| 945 | $replace_with = wpautop($replace_with); |
||
| 946 | } |
||
| 947 | unset( $autop ); |
||
| 948 | View Code Duplication | } else if ( is_array( $replace_with ) ) { |
|
| 949 | if ( isset( $atts['show'] ) && $atts['show'] && isset( $replace_with[ $atts['show'] ] ) ) { |
||
| 950 | $replace_with = $replace_with[ $atts['show'] ]; |
||
| 951 | } else { |
||
| 952 | $replace_with = implode( $sep, $replace_with ); |
||
| 953 | } |
||
| 954 | } |
||
| 955 | |||
| 956 | return $replace_with; |
||
| 957 | } |
||
| 958 | |||
| 959 | public static function get_field_types( $type ) { |
||
| 960 | $single_input = array( 'text', 'textarea', 'rte', 'number', 'email', 'url', 'image', 'file', 'date', 'phone', 'hidden', 'time', 'user_id', 'tag', 'password' ); |
||
| 961 | $multiple_input = array( 'radio', 'checkbox', 'select', 'scale', 'lookup' ); |
||
| 962 | $other_type = array( 'html', 'break' ); |
||
| 963 | |||
| 964 | $field_selection = array_merge( FrmField::pro_field_selection(), FrmField::field_selection() ); |
||
| 965 | |||
| 966 | $field_types = array(); |
||
| 967 | if ( in_array($type, $single_input) ) { |
||
| 968 | self::field_types_for_input( $single_input, $field_selection, $field_types ); |
||
| 969 | } else if ( in_array($type, $multiple_input) ) { |
||
| 970 | self::field_types_for_input( $multiple_input, $field_selection, $field_types ); |
||
| 971 | } else if ( in_array($type, $other_type) ) { |
||
| 972 | self::field_types_for_input( $other_type, $field_selection, $field_types ); |
||
| 973 | } else if ( isset( $field_selection[ $type ] ) ) { |
||
| 974 | $field_types[ $type ] = $field_selection[ $type ]; |
||
| 975 | } |
||
| 976 | |||
| 977 | $field_types = apply_filters( 'frm_switch_field_types', $field_types, compact( 'type' ) ); |
||
| 978 | return $field_types; |
||
| 979 | } |
||
| 980 | |||
| 981 | private static function field_types_for_input( $inputs, $fields, &$field_types ) { |
||
| 982 | foreach ( $inputs as $input ) { |
||
| 983 | $field_types[ $input ] = $fields[ $input ]; |
||
| 984 | unset($input); |
||
| 985 | } |
||
| 986 | } |
||
| 987 | |||
| 988 | /** |
||
| 989 | * Check if current field option is an "other" option |
||
| 990 | * |
||
| 991 | * @since 2.0.6 |
||
| 992 | * |
||
| 993 | * @param string $opt_key |
||
| 994 | * @return boolean Returns true if current field option is an "Other" option |
||
| 995 | */ |
||
| 996 | public static function is_other_opt( $opt_key ) { |
||
| 997 | return $opt_key && strpos( $opt_key, 'other_' ) === 0; |
||
| 998 | } |
||
| 999 | |||
| 1000 | /** |
||
| 1001 | * Get value that belongs in "Other" text box |
||
| 1002 | * |
||
| 1003 | * @since 2.0.6 |
||
| 1004 | * |
||
| 1005 | * @param array $args |
||
| 1006 | */ |
||
| 1007 | public static function get_other_val( $args ) { |
||
| 1008 | $defaults = array( |
||
| 1009 | 'opt_key' => 0, |
||
| 1010 | 'field' => array(), |
||
| 1011 | 'parent' => false, |
||
| 1012 | 'pointer' => false, |
||
| 1013 | ); |
||
| 1014 | $args = wp_parse_args( $args, $defaults ); |
||
| 1015 | |||
| 1016 | $opt_key = $args['opt_key']; |
||
| 1017 | $field = $args['field']; |
||
| 1018 | $parent = $args['parent']; |
||
| 1019 | $pointer = $args['pointer']; |
||
| 1020 | $other_val = ''; |
||
| 1021 | |||
| 1022 | // If option is an "other" option and there is a value set for this field, |
||
| 1023 | // check if the value belongs in the current "Other" option text field |
||
| 1024 | if ( ! FrmFieldsHelper::is_other_opt( $opt_key ) || ! FrmField::is_option_true( $field, 'value' ) ) { |
||
| 1025 | return $other_val; |
||
| 1026 | } |
||
| 1027 | |||
| 1028 | // Check posted vals before checking saved values |
||
| 1029 | |||
| 1030 | // For fields inside repeating sections - note, don't check if $pointer is true because it will often be zero |
||
| 1031 | if ( $parent && isset( $_POST['item_meta'][ $parent ][ $pointer ]['other'][ $field['id'] ] ) ) { |
||
| 1032 | if ( FrmField::is_field_with_multiple_values( $field ) ) { |
||
| 1033 | $other_val = isset( $_POST['item_meta'][ $parent ][ $pointer ]['other'][ $field['id'] ][ $opt_key ] ) ? sanitize_text_field( $_POST['item_meta'][ $parent ][ $pointer ]['other'][ $field['id'] ][ $opt_key ] ) : ''; |
||
| 1034 | } else { |
||
| 1035 | $other_val = sanitize_text_field( $_POST['item_meta'][ $parent ][ $pointer ]['other'][ $field['id'] ] ); |
||
| 1036 | } |
||
| 1037 | return $other_val; |
||
| 1038 | |||
| 1039 | } else if ( isset( $field['id'] ) && isset( $_POST['item_meta']['other'][ $field['id'] ] ) ) { |
||
| 1040 | // For normal fields |
||
| 1041 | |||
| 1042 | if ( FrmField::is_field_with_multiple_values( $field ) ) { |
||
| 1043 | $other_val = isset( $_POST['item_meta']['other'][ $field['id'] ][ $opt_key ] ) ? sanitize_text_field( $_POST['item_meta']['other'][ $field['id'] ][ $opt_key ] ) : ''; |
||
| 1044 | } else { |
||
| 1045 | $other_val = sanitize_text_field( $_POST['item_meta']['other'][ $field['id'] ] ); |
||
| 1046 | } |
||
| 1047 | return $other_val; |
||
| 1048 | } |
||
| 1049 | |||
| 1050 | // For checkboxes |
||
| 1051 | if ( $field['type'] == 'checkbox' && is_array( $field['value'] ) ) { |
||
| 1052 | // Check if there is an "other" val in saved value and make sure the |
||
| 1053 | // "other" val is not equal to the Other checkbox option |
||
| 1054 | if ( isset( $field['value'][ $opt_key ] ) && $field['options'][ $opt_key ] != $field['value'][ $opt_key ] ) { |
||
| 1055 | $other_val = $field['value'][ $opt_key ]; |
||
| 1056 | } |
||
| 1057 | } else { |
||
| 1058 | /** |
||
| 1059 | * For radio buttons and dropdowns |
||
| 1060 | * Check if saved value equals any of the options. If not, set it as the other value. |
||
| 1061 | */ |
||
| 1062 | foreach ( $field['options'] as $opt_key => $opt_val ) { |
||
| 1063 | $temp_val = is_array( $opt_val ) ? $opt_val['value'] : $opt_val; |
||
| 1064 | // Multi-select dropdowns - key is not preserved |
||
| 1065 | if ( is_array( $field['value'] ) ) { |
||
| 1066 | $o_key = array_search( $temp_val, $field['value'] ); |
||
| 1067 | if ( isset( $field['value'][ $o_key ] ) ) { |
||
| 1068 | unset( $field['value'][ $o_key ], $o_key ); |
||
| 1069 | } |
||
| 1070 | } else if ( $temp_val == $field['value'] ) { |
||
| 1071 | // For radio and regular dropdowns |
||
| 1072 | return ''; |
||
| 1073 | } else { |
||
| 1074 | $other_val = $field['value']; |
||
| 1075 | } |
||
| 1076 | unset( $opt_key, $opt_val, $temp_val ); |
||
| 1077 | } |
||
| 1078 | // For multi-select dropdowns only |
||
| 1079 | if ( is_array( $field['value'] ) && ! empty( $field['value'] ) ) { |
||
| 1080 | $other_val = reset( $field['value'] ); |
||
| 1081 | } |
||
| 1082 | } |
||
| 1083 | |||
| 1084 | return $other_val; |
||
| 1085 | } |
||
| 1086 | |||
| 1087 | /** |
||
| 1088 | * Check if there is a saved value for the "Other" text field. If so, set it as the $other_val. |
||
| 1089 | * Intended for front-end use |
||
| 1090 | * |
||
| 1091 | * @since 2.0.6 |
||
| 1092 | * |
||
| 1093 | * @param array $args should include field, opt_key and field name |
||
| 1094 | * @param boolean $other_opt |
||
| 1095 | * @param string $checked |
||
| 1096 | * @return string $other_val |
||
| 1097 | */ |
||
| 1098 | public static function prepare_other_input( $args, &$other_opt, &$checked ) { |
||
| 1099 | //Check if this is an "Other" option |
||
| 1100 | if ( ! self::is_other_opt( $args['opt_key'] ) ) { |
||
| 1101 | return; |
||
| 1102 | } |
||
| 1103 | |||
| 1104 | $other_opt = true; |
||
| 1105 | $other_args = array(); |
||
| 1106 | |||
| 1107 | self::set_other_name( $args, $other_args ); |
||
| 1108 | self::set_other_value( $args, $other_args ); |
||
| 1109 | |||
| 1110 | if ( $other_args['value'] ) { |
||
| 1111 | $checked = 'checked="checked" '; |
||
| 1112 | } |
||
| 1113 | |||
| 1114 | return $other_args; |
||
| 1115 | } |
||
| 1116 | |||
| 1117 | /** |
||
| 1118 | * @param array $args |
||
| 1119 | * @param array $other_args |
||
| 1120 | * @since 2.0.6 |
||
| 1121 | */ |
||
| 1122 | private static function set_other_name( $args, &$other_args ) { |
||
| 1123 | //Set up name for other field |
||
| 1124 | $other_args['name'] = str_replace( '[]', '', $args['field_name'] ); |
||
| 1125 | $other_args['name'] = preg_replace('/\[' . $args['field']['id'] . '\]$/', '', $other_args['name']); |
||
| 1126 | $other_args['name'] = $other_args['name'] . '[other]' . '[' . $args['field']['id'] . ']'; |
||
| 1127 | |||
| 1128 | //Converts item_meta[field_id] => item_meta[other][field_id] and |
||
| 1129 | //item_meta[parent][0][field_id] => item_meta[parent][0][other][field_id] |
||
| 1130 | if ( FrmField::is_field_with_multiple_values( $args['field'] ) ) { |
||
| 1131 | $other_args['name'] .= '[' . $args['opt_key'] . ']'; |
||
| 1132 | } |
||
| 1133 | } |
||
| 1134 | |||
| 1135 | /** |
||
| 1136 | * Find the parent and pointer, and get text for "other" text field |
||
| 1137 | * @param array $args |
||
| 1138 | * @param array $other_args |
||
| 1139 | * |
||
| 1140 | * @since 2.0.6 |
||
| 1141 | */ |
||
| 1142 | private static function set_other_value( $args, &$other_args ) { |
||
| 1143 | $parent = ''; |
||
| 1144 | $pointer = ''; |
||
| 1145 | |||
| 1146 | // Check for parent ID and pointer |
||
| 1147 | $temp_array = explode( '[', $args['field_name'] ); |
||
| 1148 | |||
| 1149 | // Count should only be greater than 3 if inside of a repeating section |
||
| 1150 | if ( count( $temp_array ) > 3 ) { |
||
| 1151 | $parent = str_replace( ']', '', $temp_array[1] ); |
||
| 1152 | $pointer = str_replace( ']', '', $temp_array[2]); |
||
| 1153 | } |
||
| 1154 | |||
| 1155 | // Get text for "other" text field |
||
| 1156 | $other_args['value'] = self::get_other_val( array( |
||
| 1157 | 'opt_key' => $args['opt_key'], |
||
| 1158 | 'field' => $args['field'], |
||
| 1159 | 'parent' => $parent, |
||
| 1160 | 'pointer' => $pointer, |
||
| 1161 | ) ); |
||
| 1162 | } |
||
| 1163 | |||
| 1164 | /** |
||
| 1165 | * If this field includes an other option, show it |
||
| 1166 | * @param $args array |
||
| 1167 | * @since 2.0.6 |
||
| 1168 | */ |
||
| 1169 | public static function include_other_input( $args ) { |
||
| 1170 | if ( ! $args['other_opt'] ) { |
||
| 1171 | return; |
||
| 1172 | } |
||
| 1173 | |||
| 1174 | $classes = array( 'frm_other_input' ); |
||
| 1175 | if ( ! $args['checked'] || trim( $args['checked'] ) == '' ) { |
||
| 1176 | // hide the field if the other option is not selected |
||
| 1177 | $classes[] = 'frm_pos_none'; |
||
| 1178 | } |
||
| 1179 | if ( $args['field']['type'] == 'select' && $args['field']['multiple'] ) { |
||
| 1180 | $classes[] = 'frm_other_full'; |
||
| 1181 | } |
||
| 1182 | |||
| 1183 | // Set up HTML ID for Other field |
||
| 1184 | $other_id = self::get_other_field_html_id( $args['field']['type'], $args['html_id'], $args['opt_key'] ); |
||
| 1185 | |||
| 1186 | ?><input type="text" id="<?php echo esc_attr( $other_id ) ?>" class="<?php echo sanitize_text_field( implode( ' ', $classes ) ) ?>" <?php echo ( $args['read_only'] ? ' readonly="readonly" disabled="disabled"' : '' ); ?> name="<?php echo esc_attr( $args['name'] ) ?>" value="<?php echo esc_attr( $args['value'] ); ?>" /><?php |
||
| 1187 | } |
||
| 1188 | |||
| 1189 | /** |
||
| 1190 | * Get the HTML id for an "Other" text field |
||
| 1191 | * Note: This does not affect fields in repeating sections |
||
| 1192 | * |
||
| 1193 | * @since 2.0.08 |
||
| 1194 | * @param string $type - field type |
||
| 1195 | * @param string $html_id |
||
| 1196 | * @param string|boolean $opt_key |
||
| 1197 | * @return string $other_id |
||
| 1198 | */ |
||
| 1199 | public static function get_other_field_html_id( $type, $html_id, $opt_key = false ) { |
||
| 1215 | |||
| 1216 | public static function clear_on_focus_html( $field, $display, $id = '' ) { |
||
| 1217 | if ( $display['clear_on_focus'] ) { |
||
| 1218 | $has_default_value = ! empty( $field['default_value'] ); |
||
| 1219 | echo '<span id="frm_clear_on_focus_' . esc_attr( $field['id'] . $id ) . '" class="frm-show-click">'; |
||
| 1220 | |||
| 1221 | if ( $display['default_blank'] ) { |
||
| 1222 | self::show_default_blank_js( $field['default_blank'], $has_default_value ); |
||
| 1223 | echo '<input type="hidden" name="field_options[default_blank_' . esc_attr( $field['id'] ) . ']" value="' . esc_attr( $field['default_blank'] ) . '" />'; |
||
| 1224 | } |
||
| 1225 | |||
| 1226 | self::show_onfocus_js( $field['clear_on_focus'], $has_default_value ); |
||
| 1227 | echo '<input type="hidden" name="field_options[clear_on_focus_' . esc_attr( $field['id'] ) . ']" value="' . esc_attr( $field['clear_on_focus'] ) . '" />'; |
||
| 1228 | |||
| 1229 | echo '</span>'; |
||
| 1230 | } |
||
| 1231 | } |
||
| 1232 | |||
| 1233 | View Code Duplication | public static function show_onfocus_js( $is_selected, $has_default_value = true ) { |
|
| 1234 | $atts = array( |
||
| 1235 | 'icon' => 'frm_reload_icon', |
||
| 1236 | 'message' => $is_selected ? __( 'Clear default value when typing', 'formidable' ) : __( 'Do not clear default value when typing', 'formidable' ), |
||
| 1237 | 'is_selected' => $is_selected, |
||
| 1238 | 'has_default' => $has_default_value, |
||
| 1239 | ); |
||
| 1240 | self::show_icon_link_js( $atts ); |
||
| 1241 | } |
||
| 1242 | |||
| 1243 | View Code Duplication | public static function show_default_blank_js( $is_selected, $has_default_value = true ) { |
|
| 1244 | $atts = array( |
||
| 1245 | 'icon' => 'frm_error_icon', |
||
| 1246 | 'message' => $is_selected ? __( 'Default value will NOT pass form validation', 'formidable' ) : __( 'Default value will pass form validation', 'formidable' ), |
||
| 1247 | 'is_selected' => $is_selected, |
||
| 1248 | 'has_default' => $has_default_value, |
||
| 1249 | ); |
||
| 1250 | self::show_icon_link_js( $atts ); |
||
| 1251 | } |
||
| 1252 | |||
| 1253 | public static function show_icon_link_js( $atts ) { |
||
| 1254 | $atts['icon'] .= $atts['is_selected'] ? ' ' : ' frm_inactive_icon '; |
||
| 1255 | if ( isset( $atts['has_default'] ) && ! $atts['has_default'] ) { |
||
| 1256 | $atts['icon'] .= 'frm_hidden '; |
||
| 1257 | } |
||
| 1258 | ?><a href="javascript:void(0)" class="frm_bstooltip <?php echo esc_attr( $atts['icon'] ); ?>frm_default_val_icons frm_action_icon frm_icon_font" title="<?php echo esc_attr( $atts['message'] ); ?>"></a><?php |
||
| 1260 | |||
| 1261 | public static function switch_field_ids( $val ) { |
||
| 1293 | |||
| 1294 | public static function get_us_states() { |
||
| 1349 | |||
| 1350 | public static function get_countries() { |
||
| 1354 | |||
| 1355 | public static function get_bulk_prefilled_opts( array &$prepop ) { |
||
| 1408 | |||
| 1409 | /** |
||
| 1410 | * Display a field value selector |
||
| 1411 | * |
||
| 1412 | * @since 2.03.05 |
||
| 1413 | * |
||
| 1414 | * @param int $selector_field_id |
||
| 1415 | * @param array $selector_args |
||
| 1416 | */ |
||
| 1417 | public static function display_field_value_selector( $selector_field_id, $selector_args ) { |
||
| 1421 | |||
| 1422 | /** |
||
| 1423 | * Convert a field object to a flat array |
||
| 1424 | * |
||
| 1425 | * @since 2.03.05 |
||
| 1426 | * |
||
| 1427 | * @param object $field |
||
| 1428 | * |
||
| 1429 | * @return array |
||
| 1430 | */ |
||
| 1431 | public static function convert_field_object_to_flat_array( $field ) { |
||
| 1437 | |||
| 1438 | public static function field_selection() { |
||
| 1442 | |||
| 1443 | public static function pro_field_selection() { |
||
| 1447 | |||
| 1448 | public static function is_no_save_field( $type ) { |
||
| 1452 | |||
| 1453 | public static function no_save_fields() { |
||
| 1457 | |||
| 1458 | public static function is_multiple_select( $field ) { |
||
| 1462 | |||
| 1463 | public static function is_field_with_multiple_values( $field ) { |
||
| 1467 | |||
| 1468 | public static function is_required_field( $field ) { |
||
| 1472 | |||
| 1473 | public static function maybe_get_field( &$field ) { |
||
| 1477 | |||
| 1478 | public static function dropdown_categories( $args ) { |
||
| 1490 | } |
||
| 1491 |