Completed
Push — master ( 3d899c...530106 )
by Stephanie
04:51
created

FrmFieldsHelper   F

Complexity

Total Complexity 286

Size/Duplication

Total Lines 1413
Duplicated Lines 1.13 %

Coupling/Cohesion

Components 4
Dependencies 8

Importance

Changes 9
Bugs 0 Features 6
Metric Value
wmc 286
c 9
b 0
f 6
lcom 4
cbo 8
dl 16
loc 1413
rs 0.6314

How to fix   Duplicated Code    Complexity   

Duplicated Code

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 Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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
2
if ( ! defined('ABSPATH') ) {
3
	die( 'You are not allowed to call this page directly.' );
4
}
5
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 ( 'data' == $type ) {
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
                '', __( 'Option 1', 'formidable' ),
48
            ) );
49
        } else if ( $type == 'textarea' ) {
50
            $values['field_options']['max'] = '5';
51
        } else if ( $type == 'captcha' ) {
52
            $frm_settings = FrmAppHelper::get_settings();
53
            $values['invalid'] = $frm_settings->re_msg;
54
        } else if ( 'url' == $type ) {
55
            $values['name'] = __( 'Website', 'formidable' );
56
        }
57
58
		$fields = FrmField::field_selection();
59
        $fields = array_merge($fields, FrmField::pro_field_selection());
60
61
        if ( isset( $fields[ $type ] ) ) {
62
            $values['name'] = is_array( $fields[ $type ] ) ? $fields[ $type ]['name'] : $fields[ $type ];
63
        }
64
65
        unset($fields);
66
67
        return $values;
68
    }
69
70
	public static function get_html_id( $field, $plus = '' ) {
71
		return apply_filters( 'frm_field_html_id', 'field_' . $field['field_key'] . $plus, $field );
72
    }
73
74
    public static function setup_edit_vars( $record, $doing_ajax = false ) {
75
		$values = array( 'id' => $record->id, 'form_id' => $record->form_id );
76
		$defaults = array(
77
			'name'          => $record->name,
78
			'description'   => $record->description,
79
			'field_key'     => $record->field_key,
80
			'type'          => $record->type,
81
			'default_value' => $record->default_value,
82
			'field_order'   => $record->field_order,
83
			'required'      => $record->required,
84
		);
85
86
		if ( $doing_ajax ) {
87
            $values = $values + $defaults;
88
            $values['form_name'] = '';
89
		} else {
90
			foreach ( $defaults as $var => $default ) {
91
                $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'htmlspecialchars' );
92
                unset($var, $default);
93
            }
94
95
			$values['form_name'] = $record->form_id ? FrmForm::getName( $record->form_id ) : '';
96
        }
97
98
		unset( $defaults );
99
100
        $values['options'] = $record->options;
101
        $values['field_options'] = $record->field_options;
102
103
        $defaults = self::get_default_field_opts($values['type'], $record, true);
104
105
		if ( $values['type'] == 'captcha' ) {
106
            $frm_settings = FrmAppHelper::get_settings();
107
            $defaults['invalid'] = $frm_settings->re_msg;
108
        }
109
110
		foreach ( $defaults as $opt => $default ) {
111
            $values[ $opt ] = isset( $record->field_options[ $opt ] ) ? $record->field_options[ $opt ] : $default;
112
            unset($opt, $default);
113
        }
114
115
        $values['custom_html'] = (isset($record->field_options['custom_html'])) ? $record->field_options['custom_html'] : self::get_default_html($record->type);
116
117
		return apply_filters( 'frm_setup_edit_field_vars', $values, array( 'doing_ajax' => $doing_ajax ) );
118
    }
119
120
    public static function get_default_field_opts( $type, $field, $limit = false ) {
121
        $field_options = array(
122
            'size' => '', 'max' => '', 'label' => '', 'blank' => '',
123
            'required_indicator' => '*', 'invalid' => '', 'separate_value' => 0,
124
            'clear_on_focus' => 0, 'default_blank' => 0, 'classes' => '',
125
			'custom_html' => '', 'captcha_size' => 'default', 'captcha_theme' => 'light',
126
        );
127
128
		if ( $limit ) {
129
            return $field_options;
130
		}
131
132
        global $wpdb;
133
134
        $form_id = (is_numeric($field)) ? $field : $field->form_id;
135
136
		$key = is_numeric( $field ) ? FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_fields', 'field_key' ) : $field->field_key;
137
138
        $field_count = FrmDb::get_var( 'frm_fields', array( 'form_id' => $form_id ), 'field_order', array( 'order_by' => 'field_order DESC' ) );
139
140
        $frm_settings = FrmAppHelper::get_settings();
141
        return array(
142
            'name' => __( 'Untitled', 'formidable' ), 'description' => '',
143
			'field_key' => $key, 'type' => $type, 'options' => '', 'default_value' => '',
144
			'field_order' => $field_count + 1, 'required' => false,
145
            'blank' => $frm_settings->blank_msg, 'unique_msg' => $frm_settings->unique_msg,
146
            'invalid' => __( 'This field is invalid', 'formidable' ), 'form_id' => $form_id,
147
			'field_options' => $field_options,
148
        );
149
    }
150
151
    public static function fill_field( &$values, $field, $form_id, $new_key = '' ) {
152
        global $wpdb;
153
154
		$values['field_key'] = FrmAppHelper::get_unique_key( $new_key, $wpdb->prefix . 'frm_fields', 'field_key' );
155
        $values['form_id'] = $form_id;
156
        $values['options'] = maybe_serialize($field->options);
157
        $values['default_value'] = maybe_serialize($field->default_value);
158
159
        foreach ( array( 'name', 'description', 'type', 'field_order', 'field_options', 'required' ) as $col ) {
160
            $values[ $col ] = $field->{$col};
161
        }
162
    }
163
164
    /**
165
     * @since 2.0
166
     */
167
	public static function get_error_msg( $field, $error ) {
168
		$frm_settings = FrmAppHelper::get_settings();
169
		$default_settings = $frm_settings->default_options();
170
		$field_name = is_array( $field ) ? $field['name'] : $field->name;
171
172
		$conf_msg = __( 'The entered values do not match', 'formidable' );
173
		$defaults = array(
174
			'unique_msg' => array( 'full' => $default_settings['unique_msg'], 'part' => sprintf( __('%s must be unique', 'formidable' ), $field_name ) ),
175
			'invalid'   => array( 'full' => __( 'This field is invalid', 'formidable' ), 'part' => sprintf( __('%s is invalid', 'formidable' ), $field_name ) ),
176
			'blank'     => array( 'full' => $frm_settings->blank_msg, 'part' => $frm_settings->blank_msg ),
177
			'conf_msg'  => array( 'full' => $conf_msg, 'part' => $conf_msg );
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ';', expecting ')'
Loading history...
178
		);
179
180
		$msg = FrmField::get_option( $field, $error );
181
		$msg = ( $msg == $defaults[ $error ]['full'] || empty( $msg ) ) ? $defaults[ $error ]['part'] : $msg;
182
		$msg = do_shortcode( $msg );
183
		return $msg;
184
	}
185
186
    public static function get_form_fields( $form_id, $error = false ) {
187
        $fields = FrmField::get_all_for_form($form_id);
188
        $fields = apply_filters('frm_get_paged_fields', $fields, $form_id, $error);
189
        return $fields;
190
    }
191
192
	public static function get_default_html( $type = 'text' ) {
193
		if ( apply_filters( 'frm_normal_field_type_html', true, $type ) ) {
194
			$input = ( in_array( $type, array( 'radio', 'checkbox', 'data' ) ) ) ? '<div class="frm_opt_container">[input]</div>' : '[input]';
195
            $for = '';
196
			if ( ! in_array( $type, array( 'radio', 'checkbox', 'data', 'scale' ) ) ) {
197
                $for = 'for="field_[key]"';
198
            }
199
200
            $default_html = <<<DEFAULT_HTML
201
<div id="frm_field_[id]_container" class="frm_form_field form-field [required_class][error_class]">
202
    <label $for class="frm_primary_label">[field_name]
203
        <span class="frm_required">[required_label]</span>
204
    </label>
205
    $input
206
    [if description]<div class="frm_description">[description]</div>[/if description]
207
    [if error]<div class="frm_error">[error]</div>[/if error]
208
</div>
209
DEFAULT_HTML;
210
        } else {
211
			$default_html = apply_filters('frm_other_custom_html', '', $type);
212
        }
213
214
        return apply_filters('frm_custom_html', $default_html, $type);
215
    }
216
217
	public static function replace_shortcodes( $html, $field, $errors = array(), $form = false, $args = array() ) {
218
        $html = apply_filters('frm_before_replace_shortcodes', $html, $field, $errors, $form);
219
220
        $defaults = array(
221
			'field_name'    => 'item_meta[' . $field['id'] . ']',
222
			'field_id'      => $field['id'],
223
            'field_plus_id' => '',
224
            'section_id'    => '',
225
        );
226
        $args = wp_parse_args($args, $defaults);
227
        $field_name = $args['field_name'];
228
        $field_id = $args['field_id'];
229
        $html_id = self::get_html_id($field, $args['field_plus_id']);
230
231
        if ( FrmField::is_multiple_select($field) ) {
232
            $field_name .= '[]';
233
        }
234
235
        //replace [id]
236
        $html = str_replace('[id]', $field_id, $html);
237
238
        // Remove the for attribute for captcha
239
        if ( $field['type'] == 'captcha' ) {
240
            $html = str_replace(' for="field_[key]"', '', $html);
241
        }
242
243
        // set the label for
244
        $html = str_replace('field_[key]', $html_id, $html);
245
246
        //replace [key]
247
        $html = str_replace('[key]', $field['field_key'], $html);
248
249
        //replace [description] and [required_label] and [error]
250
		$required = FrmField::is_required( $field ) ? $field['required_indicator'] : '';
251
        if ( ! is_array( $errors ) ) {
252
            $errors = array();
253
        }
254
		$error = isset( $errors[ 'field' . $field_id ] ) ? $errors[ 'field' . $field_id ] : false;
255
256
        //If field type is section heading, add class so a bottom margin can be added to either the h3 or description
257
        if ( $field['type'] == 'divider' ) {
258
            if ( FrmField::is_option_true( $field, 'description' ) ) {
259
                $html = str_replace( 'frm_description', 'frm_description frm_section_spacing', $html );
260
            } else {
261
                $html = str_replace('[label_position]', '[label_position] frm_section_spacing', $html);
262
            }
263
        }
264
265
		foreach ( array( 'description' => $field['description'], 'required_label' => $required, 'error' => $error ) as $code => $value ) {
266
            self::remove_inline_conditions( ( $value && $value != '' ), $code, $value, $html );
267
        }
268
269
        //replace [required_class]
270
		$required_class = FrmField::is_required( $field ) ? ' frm_required_field' : '';
271
        $html = str_replace('[required_class]', $required_class, $html);
272
273
        //replace [label_position]
274
        $field['label'] = apply_filters('frm_html_label_position', $field['label'], $field, $form);
275
        $field['label'] = ( $field['label'] && $field['label'] != '' ) ? $field['label'] : 'top';
276
		$html = str_replace( '[label_position]', ( ( in_array( $field['type'], array( 'divider', 'end_divider', 'break' ) ) ) ? $field['label'] : ' frm_primary_label' ), $html );
277
278
        //replace [field_name]
279
        $html = str_replace('[field_name]', $field['name'], $html);
280
281
        //replace [error_class]
282
		$error_class = isset( $errors[ 'field' . $field_id ] ) ? ' frm_blank_field' : '';
283
		self::get_more_field_classes( $error_class, $field, $field_id, $html );
284
		if ( $field['type'] == 'html' && strpos( $html, '[error_class]' ) === false ) {
285
			// there is no error_class shortcode to use for addign fields
286
			$html = str_replace( 'class="frm_form_field', 'class="frm_form_field ' . $error_class, $html );
287
		}
288
        $html = str_replace('[error_class]', $error_class, $html);
289
290
        //replace [entry_key]
291
        $entry_key = FrmAppHelper::simple_get( 'entry', 'sanitize_title' );
292
        $html = str_replace('[entry_key]', $entry_key, $html);
293
294
        //replace [input]
295
        preg_match_all("/\[(input|deletelink)\b(.*?)(?:(\/))?\]/s", $html, $shortcodes, PREG_PATTERN_ORDER);
296
        global $frm_vars;
297
        $frm_settings = FrmAppHelper::get_settings();
298
299
        foreach ( $shortcodes[0] as $short_key => $tag ) {
300
            $atts = shortcode_parse_atts( $shortcodes[2][ $short_key ] );
301
			$tag = self::get_shortcode_tag( $shortcodes, $short_key, array( 'conditional' => false, 'conditional_check' => false ) );
302
303
            $replace_with = '';
304
305
            if ( $tag == 'input' ) {
306
                if ( isset($atts['opt']) ) {
307
                    $atts['opt']--;
308
                }
309
310
                $field['input_class'] = isset($atts['class']) ? $atts['class'] : '';
311
                if ( isset($atts['class']) ) {
312
                    unset($atts['class']);
313
                }
314
315
                $field['shortcodes'] = $atts;
316
                ob_start();
317
				include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/input.php' );
318
                $replace_with = ob_get_contents();
319
                ob_end_clean();
320
            } else if ( $tag == 'deletelink' && FrmAppHelper::pro_is_installed() ) {
321
                $replace_with = FrmProEntriesController::entry_delete_link($atts);
322
            }
323
324
            $html = str_replace( $shortcodes[0][ $short_key ], $replace_with, $html );
325
        }
326
327
		if ( $form ) {
328
            $form = (array) $form;
329
330
            //replace [form_key]
331
            $html = str_replace('[form_key]', $form['form_key'], $html);
332
333
            //replace [form_name]
334
            $html = str_replace('[form_name]', $form['name'], $html);
335
        }
336
        $html .= "\n";
337
338
        //Return html if conf_field to prevent loop
339
        if ( isset($field['conf_field']) && $field['conf_field'] == 'stop' ) {
340
            return $html;
341
        }
342
343
        //If field is in repeating section
344
        if ( $args['section_id'] ) {
345
            $html = apply_filters('frm_replace_shortcodes', $html, $field, array( 'errors' => $errors, 'form' => $form, 'field_name' => $field_name, 'field_id' => $field_id, 'field_plus_id' => $args['field_plus_id'], 'section_id' => $args['section_id'] ));
346
        } else {
347
            $html = apply_filters('frm_replace_shortcodes', $html, $field, array( 'errors' => $errors, 'form' => $form ));
348
        }
349
350
		self::remove_collapse_shortcode( $html );
351
352
		if ( apply_filters( 'frm_do_html_shortcodes', true ) ) {
353
			$html = do_shortcode( $html );
354
		}
355
356
        return $html;
357
    }
358
359
	/**
360
	* Add more classes to certain fields (like confirmation fields, other fields, repeating fields, etc.)
361
	*
362
	* @since 2.0
363
	* @param $error_class string, pass by reference
364
	* @param $field array
365
	* @param $field_id int
366
	* @param $html string
367
	*/
368
	private static function get_more_field_classes( &$error_class, $field, $field_id, $html ) {
369
		$error_class .= ' frm_' . $field['label'] . '_container';
370
		if ( $field['id'] != $field_id ) {
371
			// add a class for repeating/embedded fields
372
			$error_class .= ' frm_field_' . $field['id'] . '_container';
373
		}
374
375
		// Add class to embedded form field
376
		if ( $field['type'] == 'form' ) {
377
			$error_class .= ' frm_embed_form_container';
378
		}
379
380
		// Add class to HTML field
381
		if ( $field['type'] == 'html' ) {
382
			$error_class .= ' frm_html_container';
383
		}
384
385
		//Add classes to inline confirmation field (if it doesn't already have classes set)
386
		if ( isset( $field['conf_field'] ) && $field['conf_field'] == 'inline' && ! $field['classes'] ) {
387
			$error_class .= ' frm_first frm_half';
388
		}
389
390
		//Add class if field includes other option
391
		if ( isset( $field['other'] ) && true == $field['other'] ) {
392
			$error_class .= ' frm_other_container';
393
		}
394
395
		// Add class to Dynamic fields
396
		if ( $field['type'] == 'data' ) {
397
			$error_class .= ' frm_dynamic_' . $field['data_type'] . '_container';
398
		}
399
400
		// Add class to inline Scale field
401
		if ( $field['type'] == 'scale' && $field['label'] == 'inline' ) {
402
			$error_class .= ' frm_scale_container';
403
		}
404
405
		// If this is a Section
406
		if ( $field['type'] == 'divider' ) {
407
408
			// If the top margin needs to be removed from a section heading
409
			if ( $field['label'] == 'none' ) {
410
				$error_class .= ' frm_hide_section';
411
			}
412
413
			// If this is a repeating section that should be hidden with exclude_fields or fields shortcode, hide it
414
			if ( $field['repeat'] ) {
415
				global $frm_vars;
416
				if ( isset( $frm_vars['show_fields'] ) && ! empty( $frm_vars['show_fields'] ) && ! in_array( $field['id'], $frm_vars['show_fields'] ) && ! in_array( $field['field_key'], $frm_vars['show_fields'] ) ) {
417
					$error_class .= ' frm_hidden';
418
				}
419
			}
420
		}
421
422
		//insert custom CSS classes
423
		if ( ! empty( $field['classes'] ) ) {
424
			if ( ! strpos( $html, 'frm_form_field ') ) {
425
				$error_class .= ' frm_form_field';
426
			}
427
			$error_class .= ' ' . $field['classes'];
428
		}
429
	}
430
431
    public static function remove_inline_conditions( $no_vars, $code, $replace_with, &$html ) {
432
        if ( $no_vars ) {
433
			$html = str_replace( '[if ' . $code . ']', '', $html );
434
			$html = str_replace( '[/if ' . $code . ']', '', $html );
435
        } else {
436
			$html = preg_replace( '/(\[if\s+' . $code . '\])(.*?)(\[\/if\s+' . $code . '\])/mis', '', $html );
437
        }
438
439
		$html = str_replace( '[' . $code . ']', $replace_with, $html );
440
    }
441
442
	public static function get_shortcode_tag( $shortcodes, $short_key, $args ) {
443
		$args = wp_parse_args( $args, array( 'conditional' => false, 'conditional_check' => false, 'foreach' => false ) );
444
        if ( ( $args['conditional'] || $args['foreach'] ) && ! $args['conditional_check'] ) {
445
            $args['conditional_check'] = true;
446
        }
447
448
        $prefix = '';
449
        if ( $args['conditional_check'] ) {
450
            if ( $args['conditional'] ) {
451
                $prefix = 'if ';
452
            } else if ( $args['foreach'] ) {
453
                $prefix = 'foreach ';
454
            }
455
        }
456
457
        $with_tags = $args['conditional_check'] ? 3 : 2;
458
        if ( ! empty( $shortcodes[ $with_tags ][ $short_key ] ) ) {
459
            $tag = str_replace( '[' . $prefix, '', $shortcodes[0][ $short_key ] );
460
            $tag = str_replace(']', '', $tag);
461
            $tags = explode(' ', $tag);
462
            if ( is_array($tags) ) {
463
                $tag = $tags[0];
464
            }
465
        } else {
466
            $tag = $shortcodes[ $with_tags - 1 ][ $short_key ];
467
        }
468
469
        return $tag;
470
    }
471
472
	/**
473
	 * Remove [collapse_this] if it's still included after all processing
474
	 * @since 2.0.8
475
	 */
476
	private static function remove_collapse_shortcode( &$html ) {
477
		if ( preg_match( '/\[(collapse_this)\]/s', $html ) ) {
478
			$html = str_replace( '[collapse_this]', '', $html );
479
		}
480
	}
481
482
	public static function display_recaptcha( $field ) {
483
		$frm_settings = FrmAppHelper::get_settings();
484
		$lang = apply_filters( 'frm_recaptcha_lang', $frm_settings->re_lang, $field );
485
486
		$class_prefix = '';
487
		$api_js_url = 'https://www.google.com/recaptcha/api.js?';
488
489
		$allow_mutiple = $frm_settings->re_multi;
490
		if ( $allow_mutiple ) {
491
			$api_js_url .= '&onload=frmRecaptcha&render=explicit';
492
			$class_prefix = 'frm-';
493
		}
494
495
        if ( $lang != 'en' ) {
496
			$api_js_url .= '&hl=' . $lang;
497
        }
498
		$api_js_url = apply_filters( 'frm_recaptcha_js_url', $api_js_url );
499
500
        wp_register_script( 'recaptcha-api', $api_js_url, '', true );
501
        wp_enqueue_script( 'recaptcha-api' );
502
503
?>
504
<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'] ) ?>"></div>
505
<?php
506
    }
507
508
	public static function show_single_option( $field ) {
509
        $field_name = $field['name'];
510
        $html_id = self::get_html_id($field);
511
        foreach ( $field['options'] as $opt_key => $opt ) {
512
            $field_val = apply_filters('frm_field_value_saved', $opt, $opt_key, $field);
513
            $opt = apply_filters('frm_field_label_seen', $opt, $opt_key, $field);
514
515
            // If this is an "Other" option, get the HTML for it
516
			if ( self::is_other_opt( $opt_key ) ) {
517
                // Get string for Other text field, if needed
518
				$other_val = self::get_other_val( compact( 'opt_key', 'field' ) );
519
				require( FrmAppHelper::plugin_path() . '/pro/classes/views/frmpro-fields/other-option.php' );
520
            } else {
521
				require( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/single-option.php' );
522
            }
523
        }
524
    }
525
526
	public static function dropdown_categories( $args ) {
527
		$defaults = array( 'field' => false, 'name' => false, 'show_option_all' => ' ' );
528
        $args = wp_parse_args($args, $defaults);
529
530
        if ( ! $args['field'] ) {
531
            return;
532
        }
533
534
        if ( ! $args['name'] ) {
535
			$args['name'] = 'item_meta[' . $args['field']['id'] . ']';
536
        }
537
538
        $id = self::get_html_id($args['field']);
539
        $class = $args['field']['type'];
540
541
        $exclude = (is_array($args['field']['exclude_cat'])) ? implode(',', $args['field']['exclude_cat']) : $args['field']['exclude_cat'];
542
        $exclude = apply_filters('frm_exclude_cats', $exclude, $args['field']);
543
544
        if ( is_array($args['field']['value']) ) {
545
            if ( ! empty($exclude) ) {
546
                $args['field']['value'] = array_diff($args['field']['value'], explode(',', $exclude));
547
            }
548
            $selected = reset($args['field']['value']);
549
        } else {
550
            $selected = $args['field']['value'];
551
        }
552
553
        $tax_atts = array(
554
            'show_option_all' => $args['show_option_all'], 'hierarchical' => 1, 'name' => $args['name'],
555
            'id' => $id, 'exclude' => $exclude, 'class' => $class, 'selected' => $selected,
556
            'hide_empty' => false, 'echo' => 0, 'orderby' => 'name',
557
        );
558
559
        $tax_atts = apply_filters('frm_dropdown_cat', $tax_atts, $args['field']);
560
561
        if ( FrmAppHelper::pro_is_installed() ) {
562
            $post_type = FrmProFormsHelper::post_type($args['field']['form_id']);
563
            $tax_atts['taxonomy'] = FrmProAppHelper::get_custom_taxonomy($post_type, $args['field']);
564
            if ( ! $tax_atts['taxonomy'] ) {
565
                return;
566
            }
567
568
            // If field type is dropdown (not Dynamic), exclude children when parent is excluded
569
            if ( $args['field']['type'] != 'data' && is_taxonomy_hierarchical($tax_atts['taxonomy']) ) {
570
                $tax_atts['exclude_tree'] = $exclude;
571
            }
572
        }
573
574
        $dropdown = wp_dropdown_categories($tax_atts);
575
576
        $add_html = FrmFieldsController::input_html($args['field'], false);
577
578
        if ( FrmAppHelper::pro_is_installed() ) {
579
            $add_html .= FrmProFieldsController::input_html($args['field'], false);
580
        }
581
582
		$dropdown = str_replace( "<select name='" . esc_attr( $args['name'] ) . "' id='" . esc_attr( $id ) . "' class='" . esc_attr( $class ) . "'", "<select name='" . esc_attr( $args['name'] ) . "' id='" . esc_attr( $id ) . "' " . $add_html, $dropdown );
583
584
        if ( is_array($args['field']['value']) ) {
585
            $skip = true;
586
            foreach ( $args['field']['value'] as $v ) {
587
				if ( $skip ) {
588
                    $skip = false;
589
                    continue;
590
                }
591
				$dropdown = str_replace(' value="' . esc_attr( $v ) . '"', ' value="' . esc_attr( $v ) . '" selected="selected"', $dropdown );
592
                unset($v);
593
            }
594
        }
595
596
        return $dropdown;
597
    }
598
599
	public static function get_term_link( $tax_id ) {
600
        $tax = get_taxonomy($tax_id);
601
        if ( ! $tax ) {
602
            return;
603
        }
604
605
        $link = sprintf(
606
            __( 'Please add options from the WordPress "%1$s" page', 'formidable' ),
607
			'<a href="' . esc_url( admin_url( 'edit-tags.php?taxonomy=' . $tax->name ) ) . '" target="_blank">' . ( empty( $tax->labels->name ) ? __( 'Categories' ) : $tax->labels->name ) . '</a>'
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
608
        );
609
        unset($tax);
610
611
        return $link;
612
    }
613
614
	public static function value_meets_condition( $observed_value, $cond, $hide_opt ) {
615
		// Remove white space from hide_opt
616
		if ( ! is_array( $hide_opt ) ) {
617
			$hide_opt = rtrim( $hide_opt );
618
		}
619
620
        if ( is_array($observed_value) ) {
621
            return self::array_value_condition($observed_value, $cond, $hide_opt);
622
        }
623
624
        $m = false;
625
        if ( $cond == '==' ) {
626
            $m = $observed_value == $hide_opt;
627
        } else if ( $cond == '!=' ) {
628
            $m = $observed_value != $hide_opt;
629
        } else if ( $cond == '>' ) {
630
            $m = $observed_value > $hide_opt;
631
        } else if ( $cond == '<' ) {
632
            $m = $observed_value < $hide_opt;
633
        } else if ( $cond == 'LIKE' || $cond == 'not LIKE' ) {
634
            $m = stripos($observed_value, $hide_opt);
635
            if ( $cond == 'not LIKE' ) {
636
                $m = ( $m === false ) ? true : false;
637
            } else {
638
                $m = ( $m === false ) ? false : true;
639
            }
640
        }
641
        return $m;
642
    }
643
644
	public static function array_value_condition( $observed_value, $cond, $hide_opt ) {
645
        $m = false;
646
        if ( $cond == '==' ) {
647
            if ( is_array($hide_opt) ) {
648
                $m = array_intersect($hide_opt, $observed_value);
649
                $m = empty($m) ? false : true;
650
            } else {
651
                $m = in_array($hide_opt, $observed_value);
652
            }
653
        } else if ( $cond == '!=' ) {
654
            $m = ! in_array($hide_opt, $observed_value);
655
        } else if ( $cond == '>' ) {
656
            $min = min($observed_value);
657
            $m = $min > $hide_opt;
658
        } else if ( $cond == '<' ) {
659
            $max = max($observed_value);
660
            $m = $max < $hide_opt;
661
        } else if ( $cond == 'LIKE' || $cond == 'not LIKE' ) {
662
            foreach ( $observed_value as $ob ) {
663
                $m = strpos($ob, $hide_opt);
664
                if ( $m !== false ) {
665
                    $m = true;
666
                    break;
667
                }
668
            }
669
670
            if ( $cond == 'not LIKE' ) {
671
                $m = ( $m === false ) ? true : false;
672
            }
673
        }
674
675
        return $m;
676
    }
677
678
    /**
679
     * Replace a few basic shortcodes and field ids
680
     * @since 2.0
681
     * @return string
682
     */
683
	public static function basic_replace_shortcodes( $value, $form, $entry ) {
684
        if ( strpos($value, '[sitename]') !== false ) {
685
            $new_value = wp_specialchars_decode( FrmAppHelper::site_name(), ENT_QUOTES );
686
            $value = str_replace('[sitename]', $new_value, $value);
687
        }
688
689
        $value = apply_filters('frm_content', $value, $form, $entry);
690
        $value = do_shortcode($value);
691
692
        return $value;
693
    }
694
695
	public static function get_shortcodes( $content, $form_id ) {
696
        if ( FrmAppHelper::pro_is_installed() ) {
697
            return FrmProDisplaysHelper::get_shortcodes($content, $form_id);
698
        }
699
700
        $fields = FrmField::getAll( array( 'fi.form_id' => (int) $form_id, 'fi.type not' => FrmField::no_save_fields() ) );
701
702
        $tagregexp = self::allowed_shortcodes($fields);
703
704
        preg_match_all("/\[(if )?($tagregexp)\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?/s", $content, $matches, PREG_PATTERN_ORDER);
705
706
        return $matches;
707
    }
708
709
	public static function allowed_shortcodes( $fields = array() ) {
710
        $tagregexp = array(
711
            'editlink', 'id', 'key', 'ip',
712
            'siteurl', 'sitename', 'admin_email',
713
            'post[-|_]id', 'created[-|_]at', 'updated[-|_]at', 'updated[-|_]by',
714
			'parent[-|_]id',
715
        );
716
717
        foreach ( $fields as $field ) {
718
            $tagregexp[] = $field->id;
719
            $tagregexp[] = $field->field_key;
720
        }
721
722
        $tagregexp = implode('|', $tagregexp);
723
        return $tagregexp;
724
    }
725
726
	public static function replace_content_shortcodes( $content, $entry, $shortcodes ) {
727
        $shortcode_values = array(
728
           'id'     => $entry->id,
729
           'key'    => $entry->item_key,
730
           'ip'     => $entry->ip,
731
        );
732
733
        foreach ( $shortcodes[0] as $short_key => $tag ) {
734
            $atts = shortcode_parse_atts( $shortcodes[3][ $short_key ] );
735
736
            if ( ! empty( $shortcodes[3][ $short_key ] ) ) {
737
				$tag = str_replace( array( '[', ']' ), '', $shortcodes[0][ $short_key ] );
738
                $tags = explode(' ', $tag);
739
                if ( is_array($tags) ) {
740
                    $tag = $tags[0];
741
                }
742
            } else {
743
                $tag = $shortcodes[2][ $short_key ];
744
            }
745
746
            switch ( $tag ) {
747
                case 'id':
748
                case 'key':
749
                case 'ip':
750
                    $replace_with = $shortcode_values[ $tag ];
751
                break;
752
753
                case 'user_agent':
754
                case 'user-agent':
755
                    $entry->description = maybe_unserialize($entry->description);
756
					$replace_with = FrmEntryFormat::get_browser( $entry->description['browser'] );
757
                break;
758
759
                case 'created_at':
760
                case 'created-at':
761
                case 'updated_at':
762
                case 'updated-at':
763
                    if ( isset($atts['format']) ) {
764
                        $time_format = ' ';
765
                    } else {
766
                        $atts['format'] = get_option('date_format');
767
                        $time_format = '';
768
                    }
769
770
                    $this_tag = str_replace('-', '_', $tag);
771
                    $replace_with = FrmAppHelper::get_formatted_time($entry->{$this_tag}, $atts['format'], $time_format);
772
                    unset($this_tag);
773
                break;
774
775
                case 'created_by':
776
                case 'created-by':
777
                case 'updated_by':
778
                case 'updated-by':
779
                    $this_tag = str_replace('-', '_', $tag);
780
					$replace_with = self::get_display_value( $entry->{$this_tag}, (object) array( 'type' => 'user_id' ), $atts );
781
                    unset($this_tag);
782
                break;
783
784
                case 'admin_email':
785
                case 'siteurl':
786
                case 'frmurl':
787
                case 'sitename':
788
                case 'get':
789
                    $replace_with = self::dynamic_default_values( $tag, $atts );
790
                break;
791
792
                default:
793
                    $field = FrmField::getOne( $tag );
794
                    if ( ! $field ) {
795
                        break;
796
                    }
797
798
                    $sep = isset($atts['sep']) ? $atts['sep'] : ', ';
799
800
                    $replace_with = FrmEntryMeta::get_meta_value( $entry, $field->id );
801
802
                    $atts['entry_id'] = $entry->id;
803
                    $atts['entry_key'] = $entry->item_key;
804
805
                    if ( isset($atts['show']) && $atts['show'] == 'field_label' ) {
806
                        $replace_with = $field->name;
807
                    } else if ( isset($atts['show']) && $atts['show'] == 'description' ) {
808
                        $replace_with = $field->description;
809
					} else {
810
						$string_value = $replace_with;
811
						if ( is_array( $replace_with ) ) {
812
							$string_value = implode( $sep, $replace_with );
813
						}
814
815
						if ( empty( $string_value ) && $string_value != '0' ) {
816
							$replace_with = '';
817
						} else {
818
							$replace_with = self::get_display_value( $replace_with, $field, $atts );
819
						}
820
					}
821
822
                    unset($field);
823
                break;
824
            }
825
826
            if ( isset($replace_with) ) {
827
                $content = str_replace( $shortcodes[0][ $short_key ], $replace_with, $content );
828
            }
829
830
            unset($atts, $conditional, $replace_with);
831
		}
832
833
		return $content;
834
    }
835
836
    /**
837
     * Get the value to replace a few standard shortcodes
838
     *
839
     * @since 2.0
840
     * @return string
841
     */
842
    public static function dynamic_default_values( $tag, $atts = array(), $return_array = false ) {
843
        $new_value = '';
844
        switch ( $tag ) {
845
            case 'admin_email':
846
                $new_value = get_option('admin_email');
847
                break;
848
            case 'siteurl':
849
                $new_value = FrmAppHelper::site_url();
850
                break;
851
            case 'frmurl':
852
                $new_value = FrmAppHelper::plugin_url();
853
                break;
854
            case 'sitename':
855
                $new_value = FrmAppHelper::site_name();
856
                break;
857
            case 'get':
858
                $new_value = self::process_get_shortcode( $atts, $return_array );
859
                break;
860
        }
861
862
        return $new_value;
863
    }
864
865
    /**
866
     * Process the [get] shortcode
867
     *
868
     * @since 2.0
869
     * @return string|array
870
     */
871
    public static function process_get_shortcode( $atts, $return_array = false ) {
872
        if ( ! isset($atts['param']) ) {
873
            return '';
874
        }
875
876
        if ( strpos($atts['param'], '&#91;') ) {
877
            $atts['param'] = str_replace('&#91;', '[', $atts['param']);
878
            $atts['param'] = str_replace('&#93;', ']', $atts['param']);
879
        }
880
881
        $new_value = FrmAppHelper::get_param($atts['param'], '');
882
        $new_value = FrmAppHelper::get_query_var( $new_value, $atts['param'] );
883
884
        if ( $new_value == '' ) {
885
            if ( ! isset($atts['prev_val']) ) {
886
                $atts['prev_val'] = '';
887
            }
888
889
            $new_value = isset($atts['default']) ? $atts['default'] : $atts['prev_val'];
890
        }
891
892
        if ( is_array($new_value) && ! $return_array ) {
893
            $new_value = implode(', ', $new_value);
894
        }
895
896
        return $new_value;
897
    }
898
899
	public static function get_display_value( $replace_with, $field, $atts = array() ) {
900
		$atts['sep'] = isset( $atts['sep'] ) ? $atts['sep'] : ', ';
901
902
		$replace_with = apply_filters( 'frm_get_' . $field->type . '_display_value', $replace_with, $field, $atts );
903
		$replace_with = apply_filters( 'frm_get_display_value', $replace_with, $field, $atts );
904
905
        if ( $field->type == 'textarea' || $field->type == 'rte' ) {
906
            $autop = isset($atts['wpautop']) ? $atts['wpautop'] : true;
907
            if ( apply_filters('frm_use_wpautop', $autop) ) {
908
                if ( is_array($replace_with) ) {
909
                    $replace_with = implode("\n", $replace_with);
910
                }
911
                $replace_with = wpautop($replace_with);
912
            }
913
			unset( $autop );
914
		} else if ( is_array( $replace_with ) ) {
915
			$replace_with = implode( $atts['sep'], $replace_with );
916
		}
917
918
		return $replace_with;
919
	}
920
921
	public static function get_field_types( $type ) {
922
        $single_input = array(
923
            'text', 'textarea', 'rte', 'number', 'email', 'url',
924
            'image', 'file', 'date', 'phone', 'hidden', 'time',
925
            'user_id', 'tag', 'password',
926
        );
927
		$multiple_input = array( 'radio', 'checkbox', 'select', 'scale' );
928
		$other_type = array( 'divider', 'html', 'break' );
929
930
		$field_selection = array_merge( FrmField::pro_field_selection(), FrmField::field_selection() );
931
932
        $field_types = array();
933
        if ( in_array($type, $single_input) ) {
934
            self::field_types_for_input( $single_input, $field_selection, $field_types );
935
        } else if ( in_array($type, $multiple_input) ) {
936
            self::field_types_for_input( $multiple_input, $field_selection, $field_types );
937
        } else if ( in_array($type, $other_type) ) {
938
            self::field_types_for_input( $other_type, $field_selection, $field_types );
939
		} else if ( isset( $field_selection[ $type ] ) ) {
940
            $field_types[ $type ] = $field_selection[ $type ];
941
        }
942
943
        return $field_types;
944
    }
945
946
    private static function field_types_for_input( $inputs, $fields, &$field_types ) {
947
        foreach ( $inputs as $input ) {
948
            $field_types[ $input ] = $fields[ $input ];
949
            unset($input);
950
        }
951
    }
952
953
    /**
954
    * Check if current field option is an "other" option
955
    *
956
    * @since 2.0.6
957
    *
958
    * @param string $opt_key
959
    * @return boolean Returns true if current field option is an "Other" option
960
    */
961
    public static function is_other_opt( $opt_key ) {
962
        return $opt_key && strpos( $opt_key, 'other' ) !== false;
963
    }
964
965
    /**
966
    * Get value that belongs in "Other" text box
967
    *
968
    * @since 2.0.6
969
    *
970
    * @param array $args
971
    */
972
    public static function get_other_val( $args ) {
973
		$defaults = array(
974
			'opt_key' => 0, 'field' => array(),
975
			'parent' => false, 'pointer' => false,
976
		);
977
		$args = wp_parse_args( $args, $defaults );
978
979
		$opt_key = $args['opt_key'];
980
		$field = $args['field'];
981
		$parent = $args['parent'];
982
		$pointer = $args['pointer'];
983
		$other_val = '';
984
985
		// If option is an "other" option and there is a value set for this field,
986
		// check if the value belongs in the current "Other" option text field
987
		if ( ! FrmFieldsHelper::is_other_opt( $opt_key ) || ! FrmField::is_option_true( $field, 'value' ) ) {
988
			return $other_val;
989
		}
990
991
		// Check posted vals before checking saved values
992
993
		// For fields inside repeating sections - note, don't check if $pointer is true because it will often be zero
994
		if ( $parent && isset( $_POST['item_meta'][ $parent ][ $pointer ]['other'][ $field['id'] ] ) ) {
995
			if ( FrmField::is_field_with_multiple_values( $field ) ) {
996
				$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 ] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
997
			} else {
998
				$other_val = sanitize_text_field( $_POST['item_meta'][ $parent ][ $pointer ]['other'][ $field['id'] ] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
999
			}
1000
			return $other_val;
1001
1002
		} else if ( isset( $field['id'] ) && isset( $_POST['item_meta']['other'][ $field['id'] ] ) ) {
1003
			// For normal fields
1004
1005
			if ( FrmField::is_field_with_multiple_values( $field ) ) {
1006
				$other_val = isset( $_POST['item_meta']['other'][ $field['id'] ][ $opt_key ] ) ? sanitize_text_field( $_POST['item_meta']['other'][ $field['id'] ][ $opt_key ] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
1007
			} else {
1008
				$other_val = sanitize_text_field( $_POST['item_meta']['other'][ $field['id'] ] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
1009
			}
1010
			return $other_val;
1011
		}
1012
1013
		// For checkboxes
1014
		if ( $field['type'] == 'checkbox' && is_array( $field['value'] ) ) {
1015
			// Check if there is an "other" val in saved value and make sure the
1016
			// "other" val is not equal to the Other checkbox option
1017
			if ( isset( $field['value'][ $opt_key ] ) && $field['options'][ $opt_key ] != $field['value'][ $opt_key ] ) {
1018
				$other_val = $field['value'][ $opt_key ];
1019
			}
1020
		} else {
1021
			/**
1022
			 * For radio buttons and dropdowns
1023
			 * Check if saved value equals any of the options. If not, set it as the other value.
1024
			 */
1025
			foreach ( $field['options'] as $opt_key => $opt_val ) {
1026
				$temp_val = is_array( $opt_val ) ? $opt_val['value'] : $opt_val;
1027
				// Multi-select dropdowns - key is not preserved
1028
				if ( is_array( $field['value'] ) ) {
1029
					$o_key = array_search( $temp_val, $field['value'] );
1030
					if ( isset( $field['value'][ $o_key ] ) ) {
1031
						unset( $field['value'][ $o_key ], $o_key );
1032
					}
1033
				} else if ( $temp_val == $field['value'] ) {
1034
					// For radio and regular dropdowns
1035
					return '';
1036
				} else {
1037
					$other_val = $field['value'];
1038
				}
1039
				unset( $opt_key, $opt_val, $temp_val );
1040
			}
1041
			// For multi-select dropdowns only
1042
			if ( is_array( $field['value'] ) && ! empty( $field['value'] ) ) {
1043
				$other_val = reset( $field['value'] );
1044
			}
1045
		}
1046
1047
		return $other_val;
1048
    }
1049
1050
    /**
1051
    * Check if there is a saved value for the "Other" text field. If so, set it as the $other_val.
1052
    * Intended for front-end use
1053
    *
1054
    * @since 2.0.6
1055
    *
1056
    * @param array $args should include field, opt_key and field name
1057
    * @param boolean $other_opt
1058
    * @param string $checked
1059
    * @return string $other_val
1060
    */
1061
    public static function prepare_other_input( $args, &$other_opt, &$checked ) {
1062
		//Check if this is an "Other" option
1063
		if ( ! self::is_other_opt( $args['opt_key'] ) ) {
1064
			return;
1065
		}
1066
1067
		$other_opt = true;
1068
		$other_args = array();
1069
1070
		self::set_other_name( $args, $other_args );
1071
		self::set_other_value( $args, $other_args );
1072
1073
		if ( $other_args['value'] ) {
1074
			$checked = 'checked="checked" ';
1075
		}
1076
1077
        return $other_args;
1078
    }
1079
1080
	/**
1081
	 * @param array $args
1082
	 * @param array $other_args
1083
	 * @since 2.0.6
1084
	 */
1085
	private static function set_other_name( $args, &$other_args ) {
1086
		//Set up name for other field
1087
		$other_args['name'] = str_replace( '[]', '', $args['field_name'] );
1088
		$other_args['name'] = preg_replace('/\[' . $args['field']['id'] . '\]$/', '', $other_args['name']);
1089
		$other_args['name'] = $other_args['name'] . '[other]' . '[' . $args['field']['id'] . ']';
1090
1091
		//Converts item_meta[field_id] => item_meta[other][field_id] and
1092
		//item_meta[parent][0][field_id] => item_meta[parent][0][other][field_id]
1093
		if ( FrmField::is_field_with_multiple_values( $args['field'] ) ) {
1094
			$other_args['name'] .= '[' . $args['opt_key'] . ']';
1095
		}
1096
	}
1097
1098
	/**
1099
	 * Find the parent and pointer, and get text for "other" text field
1100
	 * @param array $args
1101
	 * @param array $other_args
1102
	 *
1103
	 * @since 2.0.6
1104
	 */
1105
	private static function set_other_value( $args, &$other_args ) {
1106
		$parent = $pointer = '';
1107
1108
		// Check for parent ID and pointer
1109
		$temp_array = explode( '[', $args['field_name'] );
1110
1111
		// Count should only be greater than 3 if inside of a repeating section
1112
		if ( count( $temp_array ) > 3 ) {
1113
			$parent = str_replace( ']', '', $temp_array[1] );
1114
			$pointer = str_replace( ']', '', $temp_array[2]);
1115
		}
1116
1117
		// Get text for "other" text field
1118
		$other_args['value'] = self::get_other_val( array( 'opt_key' => $args['opt_key'], 'field' => $args['field'], 'parent' => $parent, 'pointer' => $pointer ) );
1119
	}
1120
1121
	/**
1122
	 * If this field includes an other option, show it
1123
	 * @param $args array
1124
	 * @since 2.0.6
1125
	 */
1126
	public static function include_other_input( $args ) {
1127
        if ( ! $args['other_opt'] ) {
1128
        	return;
1129
		}
1130
1131
		$classes = array( 'frm_other_input' );
1132
		if ( ! $args['checked'] || trim( $args['checked'] ) == '' ) {
1133
			// hide the field if the other option is not selected
1134
			$classes[] = 'frm_pos_none';
1135
		}
1136
		if ( $args['field']['type'] == 'select' && $args['field']['multiple'] ) {
1137
			$classes[] = 'frm_other_full';
1138
		}
1139
1140
		// Set up HTML ID for Other field
1141
		$other_id = self::get_other_field_html_id( $args['field']['type'], $args['html_id'], $args['opt_key'] );
1142
1143
		?><input type="text" id="<?php echo esc_attr( $other_id ) ?>" class="<?php echo sanitize_text_field( implode( ' ', $classes ) ) ?>" <?php
1144
		echo ( $args['read_only'] ? ' readonly="readonly" disabled="disabled"' : '' );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
1145
		?> name="<?php echo esc_attr( $args['name'] ) ?>" value="<?php echo esc_attr( $args['value'] ); ?>" /><?php
1146
	}
1147
1148
	/**
1149
	* Get the HTML id for an "Other" text field
1150
	* Note: This does not affect fields in repeating sections
1151
	*
1152
	* @since 2.0.08
1153
	* @param string $type - field type
1154
	* @param string $html_id
1155
	* @param string|boolean $opt_key
1156
	* @return string $other_id
1157
	*/
1158
	public static function get_other_field_html_id( $type, $html_id, $opt_key = false ) {
1159
		$other_id = $html_id;
1160
1161
		// If hidden radio field, add an opt key of 0
1162
		if ( $type == 'radio' && $opt_key === false ) {
1163
			$opt_key = 0;
1164
		}
1165
1166
		if ( $opt_key !== false ) {
1167
			$other_id .= '-' . $opt_key;
1168
		}
1169
1170
		$other_id .= '-otext';
1171
1172
		return $other_id;
1173
	}
1174
1175
	public static function clear_on_focus_html( $field, $display, $id = '' ) {
1176
		if ( $display['clear_on_focus'] ) {
1177
			echo '<span id="frm_clear_on_focus_' . esc_attr( $field['id'] . $id ) . '" class="frm-show-click">';
1178
1179
			if ( $display['default_blank'] ) {
1180
				self::show_default_blank_js( $field['default_blank'] );
1181
				echo '<input type="hidden" name="field_options[default_blank_' . esc_attr( $field['id'] ) . ']" value="' . esc_attr( $field['default_blank'] ) . '" />';
1182
			}
1183
1184
			self::show_onfocus_js( $field['clear_on_focus'] );
1185
			echo '<input type="hidden" name="field_options[clear_on_focus_' . esc_attr( $field['id'] ) . ']" value="' . esc_attr( $field['clear_on_focus'] ) . '" />';
1186
1187
			echo '</span>';
1188
		}
1189
	}
1190
1191
	public static function show_onfocus_js( $is_selected ) {
1192
		$atts = array(
1193
			'icon'        => 'frm_reload_icon',
1194
			'message'     => $is_selected ? __( 'Clear default value when typing', 'formidable' ) : __( 'Do not clear default value when typing', 'formidable' ),
1195
			'is_selected' => $is_selected,
1196
		);
1197
		self::show_icon_link_js( $atts );
1198
	}
1199
1200
	public static function show_default_blank_js( $is_selected ) {
1201
		$atts = array(
1202
			'icon'        => 'frm_error_icon',
1203
			'message'     => $is_selected ? __( 'Default value will NOT pass form validation', 'formidable' ) : __( 'Default value will pass form validation', 'formidable' ),
1204
			'is_selected' => $is_selected,
1205
		);
1206
		self::show_icon_link_js( $atts );
1207
	}
1208
1209
	public static function show_icon_link_js( $atts ) {
1210
		$atts['icon'] .= $atts['is_selected'] ? ' ' : ' frm_inactive_icon ';
1211
		?><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
1212
	}
1213
1214
	public static function switch_field_ids( $val ) {
1215
        global $frm_duplicate_ids;
1216
        $replace = array();
1217
        $replace_with = array();
1218
        foreach ( (array) $frm_duplicate_ids as $old => $new ) {
1219
			$replace[] = '[if ' . $old . ']';
1220
			$replace_with[] = '[if ' . $new . ']';
1221
			$replace[] = '[if ' . $old . ' ';
1222
			$replace_with[] = '[if ' . $new . ' ';
1223
			$replace[] = '[/if ' . $old . ']';
1224
			$replace_with[] = '[/if ' . $new . ']';
1225
			$replace[] = '[foreach ' . $old . ']';
1226
			$replace_with[] = '[foreach ' . $new . ']';
1227
			$replace[] = '[/foreach ' . $old . ']';
1228
			$replace_with[] = '[/foreach ' . $new . ']';
1229
			$replace[] = '[' . $old . ']';
1230
			$replace_with[] = '[' . $new . ']';
1231
			$replace[] = '[' . $old . ' ';
1232
			$replace_with[] = '[' . $new . ' ';
1233
            unset($old, $new);
1234
        }
1235
		if ( is_array( $val ) ) {
1236
			foreach ( $val as $k => $v ) {
1237
                $val[ $k ] = str_replace( $replace, $replace_with, $v );
1238
                unset($k, $v);
1239
            }
1240
        } else {
1241
            $val = str_replace($replace, $replace_with, $val);
1242
        }
1243
1244
        return $val;
1245
    }
1246
1247
    public static function get_us_states() {
1248
        return apply_filters( 'frm_us_states', array(
1249
            'AL' => 'Alabama', 'AK' => 'Alaska', 'AR' => 'Arkansas', 'AZ' => 'Arizona',
1250
            'CA' => 'California', 'CO' => 'Colorado', 'CT' => 'Connecticut', 'DE' => 'Delaware',
1251
            'DC' => 'District of Columbia',
1252
            'FL' => 'Florida', 'GA' => 'Georgia', 'HI' => 'Hawaii', 'ID' => 'Idaho',
1253
            'IL' => 'Illinois', 'IN' => 'Indiana', 'IA' => 'Iowa', 'KS' => 'Kansas',
1254
            'KY' => 'Kentucky', 'LA' => 'Louisiana', 'ME' => 'Maine','MD' => 'Maryland',
1255
            'MA' => 'Massachusetts', 'MI' => 'Michigan', 'MN' => 'Minnesota', 'MS' => 'Mississippi',
1256
            'MO' => 'Missouri', 'MT' => 'Montana', 'NE' => 'Nebraska', 'NV' => 'Nevada',
1257
            'NH' => 'New Hampshire', 'NJ' => 'New Jersey', 'NM' => 'New Mexico', 'NY' => 'New York',
1258
            'NC' => 'North Carolina', 'ND' => 'North Dakota', 'OH' => 'Ohio', 'OK' => 'Oklahoma',
1259
            'OR' => 'Oregon', 'PA' => 'Pennsylvania', 'RI' => 'Rhode Island', 'SC' => 'South Carolina',
1260
            'SD' => 'South Dakota', 'TN' => 'Tennessee', 'TX' => 'Texas', 'UT' => 'Utah',
1261
            'VT' => 'Vermont', 'VA' => 'Virginia', 'WA' => 'Washington', 'WV' => 'West Virginia',
1262
            'WI' => 'Wisconsin', 'WY' => 'Wyoming',
1263
        ) );
1264
    }
1265
1266
    public static function get_countries() {
1267
        return apply_filters( 'frm_countries', array(
1268
            __( 'Afghanistan', 'formidable' ), __( 'Albania', 'formidable' ), __( 'Algeria', 'formidable' ),
1269
            __( 'American Samoa', 'formidable' ), __( 'Andorra', 'formidable' ), __( 'Angola', 'formidable' ),
1270
            __( 'Anguilla', 'formidable' ), __( 'Antarctica', 'formidable' ), __( 'Antigua and Barbuda', 'formidable' ),
1271
            __( 'Argentina', 'formidable' ), __( 'Armenia', 'formidable' ), __( 'Aruba', 'formidable' ),
1272
            __( 'Australia', 'formidable' ), __( 'Austria', 'formidable' ), __( 'Azerbaijan', 'formidable' ),
1273
            __( 'Bahamas', 'formidable' ), __( 'Bahrain', 'formidable' ), __( 'Bangladesh', 'formidable' ),
1274
            __( 'Barbados', 'formidable' ), __( 'Belarus', 'formidable' ), __( 'Belgium', 'formidable' ),
1275
            __( 'Belize', 'formidable' ), __( 'Benin', 'formidable' ), __( 'Bermuda', 'formidable' ),
1276
            __( 'Bhutan', 'formidable' ), __( 'Bolivia', 'formidable' ), __( 'Bosnia and Herzegovina', 'formidable' ),
1277
            __( 'Botswana', 'formidable' ), __( 'Brazil', 'formidable' ), __( 'Brunei', 'formidable' ),
1278
            __( 'Bulgaria', 'formidable' ), __( 'Burkina Faso', 'formidable' ), __( 'Burundi', 'formidable' ),
1279
            __( 'Cambodia', 'formidable' ), __( 'Cameroon', 'formidable' ), __( 'Canada', 'formidable' ),
1280
            __( 'Cape Verde', 'formidable' ), __( 'Cayman Islands', 'formidable' ), __( 'Central African Republic', 'formidable' ),
1281
            __( 'Chad', 'formidable' ), __( 'Chile', 'formidable' ), __( 'China', 'formidable' ),
1282
            __( 'Colombia', 'formidable' ), __( 'Comoros', 'formidable' ), __( 'Congo', 'formidable' ),
1283
            __( 'Costa Rica', 'formidable' ), __( 'C&ocirc;te d\'Ivoire', 'formidable' ), __( 'Croatia', 'formidable' ),
1284
            __( 'Cuba', 'formidable' ), __( 'Cyprus', 'formidable' ), __( 'Czech Republic', 'formidable' ),
1285
            __( 'Denmark', 'formidable' ), __( 'Djibouti', 'formidable' ), __( 'Dominica', 'formidable' ),
1286
            __( 'Dominican Republic', 'formidable' ), __( 'East Timor', 'formidable' ), __( 'Ecuador', 'formidable' ),
1287
            __( 'Egypt', 'formidable' ), __( 'El Salvador', 'formidable' ), __( 'Equatorial Guinea', 'formidable' ),
1288
            __( 'Eritrea', 'formidable' ), __( 'Estonia', 'formidable' ), __( 'Ethiopia', 'formidable' ),
1289
            __( 'Fiji', 'formidable' ), __( 'Finland', 'formidable' ), __( 'France', 'formidable' ),
1290
            __( 'French Guiana', 'formidable' ), __( 'French Polynesia', 'formidable' ), __( 'Gabon', 'formidable' ),
1291
            __( 'Gambia', 'formidable' ), __( 'Georgia', 'formidable' ), __( 'Germany', 'formidable' ),
1292
            __( 'Ghana', 'formidable' ), __( 'Gibraltar', 'formidable' ), __( 'Greece', 'formidable' ),
1293
            __( 'Greenland', 'formidable' ), __( 'Grenada', 'formidable' ), __( 'Guam', 'formidable' ),
1294
            __( 'Guatemala', 'formidable' ), __( 'Guinea', 'formidable' ), __( 'Guinea-Bissau', 'formidable' ),
1295
            __( 'Guyana', 'formidable' ), __( 'Haiti', 'formidable' ), __( 'Honduras', 'formidable' ),
1296
            __( 'Hong Kong', 'formidable' ), __( 'Hungary', 'formidable' ), __( 'Iceland', 'formidable' ),
1297
            __( 'India', 'formidable' ), __( 'Indonesia', 'formidable' ), __( 'Iran', 'formidable' ),
1298
            __( 'Iraq', 'formidable' ), __( 'Ireland', 'formidable' ), __( 'Israel', 'formidable' ),
1299
            __( 'Italy', 'formidable' ), __( 'Jamaica', 'formidable' ), __( 'Japan', 'formidable' ),
1300
            __( 'Jordan', 'formidable' ), __( 'Kazakhstan', 'formidable' ), __( 'Kenya', 'formidable' ),
1301
            __( 'Kiribati', 'formidable' ), __( 'North Korea', 'formidable' ), __( 'South Korea', 'formidable' ),
1302
            __( 'Kuwait', 'formidable' ), __( 'Kyrgyzstan', 'formidable' ), __( 'Laos', 'formidable' ),
1303
            __( 'Latvia', 'formidable' ), __( 'Lebanon', 'formidable' ), __( 'Lesotho', 'formidable' ),
1304
            __( 'Liberia', 'formidable' ), __( 'Libya', 'formidable' ), __( 'Liechtenstein', 'formidable' ),
1305
            __( 'Lithuania', 'formidable' ), __( 'Luxembourg', 'formidable' ), __( 'Macedonia', 'formidable' ),
1306
            __( 'Madagascar', 'formidable' ), __( 'Malawi', 'formidable' ), __( 'Malaysia', 'formidable' ),
1307
            __( 'Maldives', 'formidable' ), __( 'Mali', 'formidable' ), __( 'Malta', 'formidable' ),
1308
            __( 'Marshall Islands', 'formidable' ), __( 'Mauritania', 'formidable' ), __( 'Mauritius', 'formidable' ),
1309
            __( 'Mexico', 'formidable' ), __( 'Micronesia', 'formidable' ), __( 'Moldova', 'formidable' ),
1310
            __( 'Monaco', 'formidable' ), __( 'Mongolia', 'formidable' ), __( 'Montenegro', 'formidable' ),
1311
            __( 'Montserrat', 'formidable' ), __( 'Morocco', 'formidable' ), __( 'Mozambique', 'formidable' ),
1312
            __( 'Myanmar', 'formidable' ), __( 'Namibia', 'formidable' ), __( 'Nauru', 'formidable' ),
1313
            __( 'Nepal', 'formidable' ), __( 'Netherlands', 'formidable' ), __( 'New Zealand', 'formidable' ),
1314
            __( 'Nicaragua', 'formidable' ), __( 'Niger', 'formidable' ), __( 'Nigeria', 'formidable' ),
1315
            __( 'Norway', 'formidable' ), __( 'Northern Mariana Islands', 'formidable' ), __( 'Oman', 'formidable' ),
1316
            __( 'Pakistan', 'formidable' ), __( 'Palau', 'formidable' ), __( 'Palestine', 'formidable' ),
1317
            __( 'Panama', 'formidable' ), __( 'Papua New Guinea', 'formidable' ), __( 'Paraguay', 'formidable' ),
1318
            __( 'Peru', 'formidable' ), __( 'Philippines', 'formidable' ), __( 'Poland', 'formidable' ),
1319
            __( 'Portugal', 'formidable' ), __( 'Puerto Rico', 'formidable' ), __( 'Qatar', 'formidable' ),
1320
            __( 'Romania', 'formidable' ), __( 'Russia', 'formidable' ), __( 'Rwanda', 'formidable' ),
1321
            __( 'Saint Kitts and Nevis', 'formidable' ), __( 'Saint Lucia', 'formidable' ),
1322
            __( 'Saint Vincent and the Grenadines', 'formidable' ), __( 'Samoa', 'formidable' ),
1323
            __( 'San Marino', 'formidable' ), __( 'Sao Tome and Principe', 'formidable' ), __( 'Saudi Arabia', 'formidable' ),
1324
            __( 'Senegal', 'formidable' ), __( 'Serbia and Montenegro', 'formidable' ), __( 'Seychelles', 'formidable' ),
1325
            __( 'Sierra Leone', 'formidable' ), __( 'Singapore', 'formidable' ), __( 'Slovakia', 'formidable' ),
1326
            __( 'Slovenia', 'formidable' ), __( 'Solomon Islands', 'formidable' ), __( 'Somalia', 'formidable' ),
1327
            __( 'South Africa', 'formidable' ), __( 'South Sudan', 'formidable' ),
1328
            __( 'Spain', 'formidable' ), __( 'Sri Lanka', 'formidable' ),
1329
            __( 'Sudan', 'formidable' ), __( 'Suriname', 'formidable' ), __( 'Swaziland', 'formidable' ),
1330
            __( 'Sweden', 'formidable' ), __( 'Switzerland', 'formidable' ), __( 'Syria', 'formidable' ),
1331
            __( 'Taiwan', 'formidable' ), __( 'Tajikistan', 'formidable' ), __( 'Tanzania', 'formidable' ),
1332
            __( 'Thailand', 'formidable' ), __( 'Togo', 'formidable' ), __( 'Tonga', 'formidable' ),
1333
            __( 'Trinidad and Tobago', 'formidable' ), __( 'Tunisia', 'formidable' ), __( 'Turkey', 'formidable' ),
1334
            __( 'Turkmenistan', 'formidable' ), __( 'Tuvalu', 'formidable' ), __( 'Uganda', 'formidable' ),
1335
            __( 'Ukraine', 'formidable' ), __( 'United Arab Emirates', 'formidable' ), __( 'United Kingdom', 'formidable' ),
1336
            __( 'United States', 'formidable' ), __( 'Uruguay', 'formidable' ), __( 'Uzbekistan', 'formidable' ),
1337
            __( 'Vanuatu', 'formidable' ), __( 'Vatican City', 'formidable' ), __( 'Venezuela', 'formidable' ),
1338
            __( 'Vietnam', 'formidable' ), __( 'Virgin Islands, British', 'formidable' ),
1339
            __( 'Virgin Islands, U.S.', 'formidable' ), __( 'Yemen', 'formidable' ), __( 'Zambia', 'formidable' ),
1340
            __( 'Zimbabwe', 'formidable' ),
1341
        ) );
1342
    }
1343
1344
	public static function get_bulk_prefilled_opts( array &$prepop ) {
1345
		$prepop[ __( 'Countries', 'formidable' ) ] = FrmFieldsHelper::get_countries();
1346
1347
        $states = FrmFieldsHelper::get_us_states();
1348
        $state_abv = array_keys($states);
1349
        sort($state_abv);
1350
		$prepop[ __( 'U.S. State Abbreviations', 'formidable' ) ] = $state_abv;
1351
1352
        $states = array_values($states);
1353
        sort($states);
1354
		$prepop[ __( 'U.S. States', 'formidable' ) ] = $states;
1355
        unset($state_abv, $states);
1356
1357
		$prepop[ __( 'Age', 'formidable' ) ] = array(
1358
            __( 'Under 18', 'formidable' ), __( '18-24', 'formidable' ), __( '25-34', 'formidable' ),
1359
            __( '35-44', 'formidable' ), __( '45-54', 'formidable' ), __( '55-64', 'formidable' ),
1360
            __( '65 or Above', 'formidable' ), __( 'Prefer Not to Answer', 'formidable' ),
1361
        );
1362
1363
		$prepop[ __( 'Satisfaction', 'formidable' ) ] = array(
1364
            __( 'Very Satisfied', 'formidable' ), __( 'Satisfied', 'formidable' ), __( 'Neutral', 'formidable' ),
1365
            __( 'Unsatisfied', 'formidable' ), __( 'Very Unsatisfied', 'formidable' ), __( 'N/A', 'formidable' ),
1366
        );
1367
1368
		$prepop[ __( 'Importance', 'formidable' ) ] = array(
1369
            __( 'Very Important', 'formidable' ), __( 'Important', 'formidable' ), __( 'Neutral', 'formidable' ),
1370
            __( 'Somewhat Important', 'formidable' ), __( 'Not at all Important', 'formidable' ), __( 'N/A', 'formidable' ),
1371
        );
1372
1373
		$prepop[ __( 'Agreement', 'formidable' ) ] = array(
1374
            __( 'Strongly Agree', 'formidable' ), __( 'Agree', 'formidable' ), __( 'Neutral', 'formidable' ),
1375
            __( 'Disagree', 'formidable' ), __( 'Strongly Disagree', 'formidable' ), __( 'N/A', 'formidable' ),
1376
        );
1377
1378
		$prepop = apply_filters( 'frm_bulk_field_choices', $prepop );
1379
    }
1380
1381
	public static function field_selection() {
1382
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::field_selection' );
1383
		return FrmField::field_selection();
1384
	}
1385
1386
	public static function pro_field_selection() {
1387
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::pro_field_selection' );
1388
		return FrmField::pro_field_selection();
1389
	}
1390
1391
	public static function is_no_save_field( $type ) {
1392
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::is_no_save_field' );
1393
		return FrmField::is_no_save_field( $type );
1394
	}
1395
1396
	public static function no_save_fields() {
1397
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::no_save_fields' );
1398
		return FrmField::no_save_fields();
1399
	}
1400
1401
	public static function is_multiple_select( $field ) {
1402
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::is_multiple_select' );
1403
		return FrmField::is_multiple_select( $field );
1404
	}
1405
1406
	public static function is_field_with_multiple_values( $field ) {
1407
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::is_field_with_multiple_values' );
1408
		return FrmField::is_field_with_multiple_values( $field );
1409
	}
1410
1411
	public static function is_required_field( $field ) {
1412
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::is_required' );
1413
		return FrmField::is_required( $field );
1414
	}
1415
1416
    public static function maybe_get_field( &$field ) {
1417
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::maybe_get_field' );
1418
		FrmField::maybe_get_field( $field );
1419
    }
1420
}
1421