Completed
Pull Request — master (#54)
by Jamie
03:26
created

FrmFieldsHelper::get_form_fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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 ( 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
                '', __( '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' => 'normal', '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 ),
178
		);
179
180
		$msg = FrmField::get_option( $field, $error );
181
		$msg = 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 = array() ) {
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
		self::add_field_div_classes( $field_id, $field, $errors, $html );
282
283
        //replace [entry_key]
284
        $entry_key = FrmAppHelper::simple_get( 'entry', 'sanitize_title' );
285
        $html = str_replace('[entry_key]', $entry_key, $html);
286
287
		if ( $form ) {
288
			$form = (array) $form;
289
290
			//replace [form_key]
291
			$html = str_replace('[form_key]', $form['form_key'], $html);
292
293
			//replace [form_name]
294
			$html = str_replace('[form_name]', $form['name'], $html);
295
		}
296
297
		self::process_wp_shortcodes( $html );
298
299
        //replace [input]
300
        preg_match_all("/\[(input|deletelink)\b(.*?)(?:(\/))?\]/s", $html, $shortcodes, PREG_PATTERN_ORDER);
301
        global $frm_vars;
302
        $frm_settings = FrmAppHelper::get_settings();
303
304
        foreach ( $shortcodes[0] as $short_key => $tag ) {
305
            $atts = FrmShortcodeHelper::get_shortcode_attribute_array( $shortcodes[2][ $short_key ] );
306
			$tag = self::get_shortcode_tag( $shortcodes, $short_key, array( 'conditional' => false, 'conditional_check' => false ) );
307
308
            $replace_with = '';
309
310
            if ( $tag == 'input' ) {
311
                if ( isset($atts['opt']) ) {
312
                    $atts['opt']--;
313
                }
314
315
                $field['input_class'] = isset($atts['class']) ? $atts['class'] : '';
316
                if ( isset($atts['class']) ) {
317
                    unset($atts['class']);
318
                }
319
320
                $field['shortcodes'] = $atts;
321
                ob_start();
322
				include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/input.php' );
323
                $replace_with = ob_get_contents();
324
                ob_end_clean();
325
            } else if ( $tag == 'deletelink' && FrmAppHelper::pro_is_installed() ) {
326
                $replace_with = FrmProEntriesController::entry_delete_link($atts);
327
            }
328
329
            $html = str_replace( $shortcodes[0][ $short_key ], $replace_with, $html );
330
        }
331
332
        $html .= "\n";
333
334
        //Return html if conf_field to prevent loop
335
        if ( isset($field['conf_field']) && $field['conf_field'] == 'stop' ) {
336
            return $html;
337
        }
338
339
        //If field is in repeating section
340
        if ( $args['section_id'] ) {
341
            $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'] ));
342
        } else {
343
            $html = apply_filters('frm_replace_shortcodes', $html, $field, array( 'errors' => $errors, 'form' => $form ));
344
        }
345
346
		self::remove_collapse_shortcode( $html );
347
348
        return $html;
349
    }
350
351
	/**
352
	 * This filters shortcodes in the field HTML
353
	 *
354
	 * @since 2.02.11
355
	 */
356
	private static function process_wp_shortcodes( &$html ) {
357
		if ( apply_filters( 'frm_do_html_shortcodes', true ) ) {
358
			$html = do_shortcode( $html );
359
		}
360
	}
361
362
	/**
363
	 * Add classes to a field div
364
	 *
365
	 * @since 2.02.05
366
	 *
367
	 * @param string $field_id
368
	 * @param array $field
369
	 * @param array $errors
370
	 * @param string $html
371
	 */
372
	private static function add_field_div_classes( $field_id, $field, $errors, &$html ) {
373
		$classes = self::get_field_div_classes( $field_id, $field, $errors, $html );
374
375
		if ( $field['type'] == 'html' && strpos( $html, '[error_class]' ) === false ) {
376
			// there is no error_class shortcode for HTML fields
377
			$html = str_replace( 'class="frm_form_field', 'class="frm_form_field ' . $classes, $html );
378
		}
379
		$html = str_replace( '[error_class]', $classes, $html );
380
	}
381
382
	/**
383
	 * Get the classes for a field div
384
	 *
385
	 * @since 2.02.05
386
	 *
387
	 * @param string $field_id
388
	 * @param array $field
389
	 * @param array $errors
390
	 * @param string $html
391
	 * @return string $classes
392
	 */
393
	private static function get_field_div_classes( $field_id, $field, $errors, $html ) {
394
		// Add error class
395
		$classes = isset( $errors[ 'field' . $field_id ] ) ? ' frm_blank_field' : '';
396
397
		// Add label position class
398
		$classes .= ' frm_' . $field['label'] . '_container';
399
400
		// Add CSS layout classes
401
		if ( ! empty( $field['classes'] ) ) {
402
			if ( ! strpos( $html, 'frm_form_field ') ) {
403
				$classes .= ' frm_form_field';
404
			}
405
			$classes .= ' ' . $field['classes'];
406
		}
407
408
		// Get additional classes
409
		$classes = apply_filters( 'frm_field_div_classes', $classes, $field, array( 'field_id' => $field_id ) );
410
411
		return $classes;
412
	}
413
414
    public static function remove_inline_conditions( $no_vars, $code, $replace_with, &$html ) {
415
        if ( $no_vars ) {
416
			$html = str_replace( '[if ' . $code . ']', '', $html );
417
			$html = str_replace( '[/if ' . $code . ']', '', $html );
418
        } else {
419
			$html = preg_replace( '/(\[if\s+' . $code . '\])(.*?)(\[\/if\s+' . $code . '\])/mis', '', $html );
420
        }
421
422
		$html = str_replace( '[' . $code . ']', $replace_with, $html );
423
    }
424
425
	public static function get_shortcode_tag( $shortcodes, $short_key, $args ) {
426
		$args = wp_parse_args( $args, array( 'conditional' => false, 'conditional_check' => false, 'foreach' => false ) );
427
        if ( ( $args['conditional'] || $args['foreach'] ) && ! $args['conditional_check'] ) {
428
            $args['conditional_check'] = true;
429
        }
430
431
        $prefix = '';
432
        if ( $args['conditional_check'] ) {
433
            if ( $args['conditional'] ) {
434
                $prefix = 'if ';
435
            } else if ( $args['foreach'] ) {
436
                $prefix = 'foreach ';
437
            }
438
        }
439
440
        $with_tags = $args['conditional_check'] ? 3 : 2;
441
        if ( ! empty( $shortcodes[ $with_tags ][ $short_key ] ) ) {
442
            $tag = str_replace( '[' . $prefix, '', $shortcodes[0][ $short_key ] );
443
            $tag = str_replace(']', '', $tag);
444
            $tags = explode(' ', $tag);
445
            if ( is_array($tags) ) {
446
                $tag = $tags[0];
447
            }
448
        } else {
449
            $tag = $shortcodes[ $with_tags - 1 ][ $short_key ];
450
        }
451
452
        return $tag;
453
    }
454
455
	/**
456
	 * Remove [collapse_this] if it's still included after all processing
457
	 * @since 2.0.8
458
	 */
459
	private static function remove_collapse_shortcode( &$html ) {
460
		if ( preg_match( '/\[(collapse_this)\]/s', $html ) ) {
461
			$html = str_replace( '[collapse_this]', '', $html );
462
		}
463
	}
464
465
	public static function get_checkbox_id( $field, $opt_key ) {
466
		$id = $field['id'];
467
		if ( isset( $field['in_section'] ) && $field['in_section'] ) {
468
			$id .= '-' . $field['in_section'];
469
		}
470
		return 'frm_checkbox_' . $id . '-' . $opt_key;
471
	}
472
473
	public static function display_recaptcha( $field ) {
474
		$frm_settings = FrmAppHelper::get_settings();
475
		$lang = apply_filters( 'frm_recaptcha_lang', $frm_settings->re_lang, $field );
476
477
		$class_prefix = '';
478
		$api_js_url = 'https://www.google.com/recaptcha/api.js?';
479
480
		$allow_mutiple = $frm_settings->re_multi;
481
		if ( $allow_mutiple ) {
482
			$api_js_url .= '&onload=frmRecaptcha&render=explicit';
483
			$class_prefix = 'frm-';
484
		}
485
486
		if ( ! empty( $lang ) ) {
487
			$api_js_url .= '&hl=' . $lang;
488
		}
489
		$api_js_url = apply_filters( 'frm_recaptcha_js_url', $api_js_url );
490
491
        wp_register_script( 'recaptcha-api', $api_js_url, '', true );
492
        wp_enqueue_script( 'recaptcha-api' );
493
494
		// for reverse compatability
495
		$field['captcha_size'] = ( $field['captcha_size'] == 'default' ) ? 'normal' : $field['captcha_size'];
496
497
?>
498
<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>
499
<?php
500
    }
501
502
	public static function show_single_option( $field ) {
503
        $field_name = $field['name'];
504
        $html_id = self::get_html_id($field);
505
        foreach ( $field['options'] as $opt_key => $opt ) {
506
            $field_val = apply_filters('frm_field_value_saved', $opt, $opt_key, $field);
507
            $opt = apply_filters('frm_field_label_seen', $opt, $opt_key, $field);
508
509
            // If this is an "Other" option, get the HTML for it
510
			if ( self::is_other_opt( $opt_key ) ) {
511
                // Get string for Other text field, if needed
512
				$other_val = self::get_other_val( compact( 'opt_key', 'field' ) );
513
				require( FrmAppHelper::plugin_path() . '/pro/classes/views/frmpro-fields/other-option.php' );
514
            } else {
515
				require( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/single-option.php' );
516
            }
517
        }
518
    }
519
520
	public static function get_term_link( $tax_id ) {
521
        $tax = get_taxonomy($tax_id);
522
        if ( ! $tax ) {
523
            return;
524
        }
525
526
        $link = sprintf(
527
            __( 'Please add options from the WordPress "%1$s" page', 'formidable' ),
528
			'<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...
529
        );
530
        unset($tax);
531
532
        return $link;
533
    }
534
535
	public static function value_meets_condition( $observed_value, $cond, $hide_opt ) {
536
		// Remove white space from hide_opt
537
		if ( ! is_array( $hide_opt ) ) {
538
			$hide_opt = rtrim( $hide_opt );
539
		}
540
541
		$observed_value = wp_kses_post( $observed_value );
542
		$hide_opt = wp_kses_post( $hide_opt );
543
544
        if ( is_array($observed_value) ) {
545
            return self::array_value_condition($observed_value, $cond, $hide_opt);
546
        }
547
548
        $m = false;
549
        if ( $cond == '==' ) {
550
            $m = $observed_value == $hide_opt;
551
        } else if ( $cond == '!=' ) {
552
            $m = $observed_value != $hide_opt;
553
        } else if ( $cond == '>' ) {
554
            $m = $observed_value > $hide_opt;
555
        } else if ( $cond == '<' ) {
556
            $m = $observed_value < $hide_opt;
557
        } else if ( $cond == 'LIKE' || $cond == 'not LIKE' ) {
558
            $m = stripos($observed_value, $hide_opt);
559
            if ( $cond == 'not LIKE' ) {
560
                $m = ( $m === false ) ? true : false;
561
            } else {
562
                $m = ( $m === false ) ? false : true;
563
            }
564
        }
565
        return $m;
566
    }
567
568
	public static function array_value_condition( $observed_value, $cond, $hide_opt ) {
569
        $m = false;
570
        if ( $cond == '==' ) {
571
            if ( is_array($hide_opt) ) {
572
                $m = array_intersect($hide_opt, $observed_value);
573
                $m = empty($m) ? false : true;
574
            } else {
575
                $m = in_array($hide_opt, $observed_value);
576
            }
577
        } else if ( $cond == '!=' ) {
578
            $m = ! in_array($hide_opt, $observed_value);
579
        } else if ( $cond == '>' ) {
580
            $min = min($observed_value);
581
            $m = $min > $hide_opt;
582
        } else if ( $cond == '<' ) {
583
            $max = max($observed_value);
584
            $m = $max < $hide_opt;
585
        } else if ( $cond == 'LIKE' || $cond == 'not LIKE' ) {
586
            foreach ( $observed_value as $ob ) {
587
                $m = strpos($ob, $hide_opt);
588
                if ( $m !== false ) {
589
                    $m = true;
590
                    break;
591
                }
592
            }
593
594
            if ( $cond == 'not LIKE' ) {
595
                $m = ( $m === false ) ? true : false;
596
            }
597
        }
598
599
        return $m;
600
    }
601
602
    /**
603
     * Replace a few basic shortcodes and field ids
604
     * @since 2.0
605
     * @return string
606
     */
607
	public static function basic_replace_shortcodes( $value, $form, $entry ) {
608
        if ( strpos($value, '[sitename]') !== false ) {
609
            $new_value = wp_specialchars_decode( FrmAppHelper::site_name(), ENT_QUOTES );
610
            $value = str_replace('[sitename]', $new_value, $value);
611
        }
612
613
        $value = apply_filters('frm_content', $value, $form, $entry);
614
        $value = do_shortcode($value);
615
616
        return $value;
617
    }
618
619
	public static function get_shortcodes( $content, $form_id ) {
620
        if ( FrmAppHelper::pro_is_installed() ) {
621
            return FrmProDisplaysHelper::get_shortcodes($content, $form_id);
622
        }
623
624
        $fields = FrmField::getAll( array( 'fi.form_id' => (int) $form_id, 'fi.type not' => FrmField::no_save_fields() ) );
625
626
        $tagregexp = self::allowed_shortcodes($fields);
627
628
        preg_match_all("/\[(if )?($tagregexp)\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?/s", $content, $matches, PREG_PATTERN_ORDER);
629
630
        return $matches;
631
    }
632
633
	public static function allowed_shortcodes( $fields = array() ) {
634
        $tagregexp = array(
635
            'editlink', 'id', 'key', 'ip',
636
            'siteurl', 'sitename', 'admin_email',
637
            'post[-|_]id', 'created[-|_]at', 'updated[-|_]at', 'updated[-|_]by',
638
			'parent[-|_]id',
639
        );
640
641
        foreach ( $fields as $field ) {
642
            $tagregexp[] = $field->id;
643
            $tagregexp[] = $field->field_key;
644
        }
645
646
        $tagregexp = implode('|', $tagregexp);
647
        return $tagregexp;
648
    }
649
650
	public static function replace_content_shortcodes( $content, $entry, $shortcodes ) {
651
        $shortcode_values = array(
652
           'id'     => $entry->id,
653
           'key'    => $entry->item_key,
654
           'ip'     => $entry->ip,
655
        );
656
657
        foreach ( $shortcodes[0] as $short_key => $tag ) {
658
            $atts = FrmShortcodeHelper::get_shortcode_attribute_array( $shortcodes[3][ $short_key ] );
659
660
            if ( ! empty( $shortcodes[3][ $short_key ] ) ) {
661
				$tag = str_replace( array( '[', ']' ), '', $shortcodes[0][ $short_key ] );
662
                $tags = explode(' ', $tag);
663
                if ( is_array($tags) ) {
664
                    $tag = $tags[0];
665
                }
666
            } else {
667
                $tag = $shortcodes[2][ $short_key ];
668
            }
669
670
            switch ( $tag ) {
671
                case 'id':
672
                case 'key':
673
                case 'ip':
674
                    $replace_with = $shortcode_values[ $tag ];
675
                break;
676
677
                case 'user_agent':
678
                case 'user-agent':
679
                    $entry->description = maybe_unserialize($entry->description);
680
					$replace_with = FrmEntryFormat::get_browser( $entry->description['browser'] );
681
                break;
682
683
                case 'created_at':
684
                case 'created-at':
685
                case 'updated_at':
686
                case 'updated-at':
687
                    if ( isset($atts['format']) ) {
688
                        $time_format = ' ';
689
                    } else {
690
                        $atts['format'] = get_option('date_format');
691
                        $time_format = '';
692
                    }
693
694
                    $this_tag = str_replace('-', '_', $tag);
695
                    $replace_with = FrmAppHelper::get_formatted_time($entry->{$this_tag}, $atts['format'], $time_format);
696
                    unset($this_tag);
697
                break;
698
699
                case 'created_by':
700
                case 'created-by':
701
                case 'updated_by':
702
                case 'updated-by':
703
                    $this_tag = str_replace('-', '_', $tag);
704
					$replace_with = self::get_display_value( $entry->{$this_tag}, (object) array( 'type' => 'user_id' ), $atts );
705
                    unset($this_tag);
706
                break;
707
708
                case 'admin_email':
709
                case 'siteurl':
710
                case 'frmurl':
711
                case 'sitename':
712
                case 'get':
713
                    $replace_with = self::dynamic_default_values( $tag, $atts );
714
                break;
715
716
                default:
717
                    $field = FrmField::getOne( $tag );
718
                    if ( ! $field ) {
719
                        break;
720
                    }
721
722
                    $sep = isset($atts['sep']) ? $atts['sep'] : ', ';
723
724
                    $replace_with = FrmEntryMeta::get_meta_value( $entry, $field->id );
725
726
                    $atts['entry_id'] = $entry->id;
727
                    $atts['entry_key'] = $entry->item_key;
728
729
                    if ( isset($atts['show']) && $atts['show'] == 'field_label' ) {
730
                        $replace_with = $field->name;
731
                    } else if ( isset($atts['show']) && $atts['show'] == 'description' ) {
732
                        $replace_with = $field->description;
733
					} else {
734
						$string_value = $replace_with;
735
						if ( is_array( $replace_with ) ) {
736
							$string_value = implode( $sep, $replace_with );
737
						}
738
739
						if ( empty( $string_value ) && $string_value != '0' ) {
740
							$replace_with = '';
741
						} else {
742
							$replace_with = self::get_display_value( $replace_with, $field, $atts );
743
						}
744
					}
745
746
                    unset($field);
747
                break;
748
            }
749
750
            if ( isset($replace_with) ) {
751
                $content = str_replace( $shortcodes[0][ $short_key ], $replace_with, $content );
752
            }
753
754
            unset($atts, $conditional, $replace_with);
755
		}
756
757
		return $content;
758
    }
759
760
    /**
761
     * Get the value to replace a few standard shortcodes
762
     *
763
     * @since 2.0
764
     * @return string
765
     */
766
    public static function dynamic_default_values( $tag, $atts = array(), $return_array = false ) {
767
        $new_value = '';
768
        switch ( $tag ) {
769
            case 'admin_email':
770
                $new_value = get_option('admin_email');
771
                break;
772
            case 'siteurl':
773
                $new_value = FrmAppHelper::site_url();
774
                break;
775
            case 'frmurl':
776
                $new_value = FrmAppHelper::plugin_url();
777
                break;
778
            case 'sitename':
779
                $new_value = FrmAppHelper::site_name();
780
                break;
781
            case 'get':
782
                $new_value = self::process_get_shortcode( $atts, $return_array );
783
                break;
784
        }
785
786
        return $new_value;
787
    }
788
789
    /**
790
     * Process the [get] shortcode
791
     *
792
     * @since 2.0
793
     * @return string|array
794
     */
795
    public static function process_get_shortcode( $atts, $return_array = false ) {
796
        if ( ! isset($atts['param']) ) {
797
            return '';
798
        }
799
800
        if ( strpos($atts['param'], '&#91;') ) {
801
            $atts['param'] = str_replace('&#91;', '[', $atts['param']);
802
            $atts['param'] = str_replace('&#93;', ']', $atts['param']);
803
        }
804
805
        $new_value = FrmAppHelper::get_param($atts['param'], '');
806
        $new_value = FrmAppHelper::get_query_var( $new_value, $atts['param'] );
807
808
        if ( $new_value == '' ) {
809
            if ( ! isset($atts['prev_val']) ) {
810
                $atts['prev_val'] = '';
811
            }
812
813
            $new_value = isset($atts['default']) ? $atts['default'] : $atts['prev_val'];
814
        }
815
816
        if ( is_array($new_value) && ! $return_array ) {
817
            $new_value = implode(', ', $new_value);
818
        }
819
820
        return $new_value;
821
    }
822
823
	public static function get_display_value( $replace_with, $field, $atts = array() ) {
824
		$sep = isset( $atts['sep'] ) ? $atts['sep'] : ', ';
825
826
		$replace_with = apply_filters( 'frm_get_' . $field->type . '_display_value', $replace_with, $field, $atts );
827
		$replace_with = apply_filters( 'frm_get_display_value', $replace_with, $field, $atts );
828
829
        if ( $field->type == 'textarea' || $field->type == 'rte' ) {
830
            $autop = isset($atts['wpautop']) ? $atts['wpautop'] : true;
831
            if ( apply_filters('frm_use_wpautop', $autop) ) {
832
                if ( is_array($replace_with) ) {
833
                    $replace_with = implode("\n", $replace_with);
834
                }
835
                $replace_with = wpautop($replace_with);
836
            }
837
			unset( $autop );
838
		} else if ( is_array( $replace_with ) ) {
839
			$replace_with = implode( $sep, $replace_with );
840
		}
841
842
		return $replace_with;
843
	}
844
845
	public static function get_field_types( $type ) {
846
        $single_input = array(
847
            'text', 'textarea', 'rte', 'number', 'email', 'url',
848
            'image', 'file', 'date', 'phone', 'hidden', 'time',
849
            'user_id', 'tag', 'password',
850
        );
851
		$multiple_input = array( 'radio', 'checkbox', 'select', 'scale', 'lookup' );
852
		$other_type = array( 'html', 'break' );
853
854
		$field_selection = array_merge( FrmField::pro_field_selection(), FrmField::field_selection() );
855
856
        $field_types = array();
857
        if ( in_array($type, $single_input) ) {
858
            self::field_types_for_input( $single_input, $field_selection, $field_types );
859
        } else if ( in_array($type, $multiple_input) ) {
860
            self::field_types_for_input( $multiple_input, $field_selection, $field_types );
861
        } else if ( in_array($type, $other_type) ) {
862
            self::field_types_for_input( $other_type, $field_selection, $field_types );
863
		} else if ( isset( $field_selection[ $type ] ) ) {
864
            $field_types[ $type ] = $field_selection[ $type ];
865
        }
866
867
		$field_types = apply_filters( 'frm_switch_field_types', $field_types, compact( 'type' ) );
868
        return $field_types;
869
    }
870
871
    private static function field_types_for_input( $inputs, $fields, &$field_types ) {
872
        foreach ( $inputs as $input ) {
873
            $field_types[ $input ] = $fields[ $input ];
874
            unset($input);
875
        }
876
    }
877
878
	/**
879
	 * Check if current field option is an "other" option
880
	 *
881
	 * @since 2.0.6
882
	 *
883
	 * @param string $opt_key
884
	 * @return boolean Returns true if current field option is an "Other" option
885
	 */
886
	public static function is_other_opt( $opt_key ) {
887
		return $opt_key && strpos( $opt_key, 'other_' ) === 0;
888
	}
889
890
    /**
891
    * Get value that belongs in "Other" text box
892
    *
893
    * @since 2.0.6
894
    *
895
    * @param array $args
896
    */
897
    public static function get_other_val( $args ) {
898
		$defaults = array(
899
			'opt_key' => 0, 'field' => array(),
900
			'parent' => false, 'pointer' => false,
901
		);
902
		$args = wp_parse_args( $args, $defaults );
903
904
		$opt_key = $args['opt_key'];
905
		$field = $args['field'];
906
		$parent = $args['parent'];
907
		$pointer = $args['pointer'];
908
		$other_val = '';
909
910
		// If option is an "other" option and there is a value set for this field,
911
		// check if the value belongs in the current "Other" option text field
912
		if ( ! FrmFieldsHelper::is_other_opt( $opt_key ) || ! FrmField::is_option_true( $field, 'value' ) ) {
913
			return $other_val;
914
		}
915
916
		// Check posted vals before checking saved values
917
918
		// For fields inside repeating sections - note, don't check if $pointer is true because it will often be zero
919
		if ( $parent && isset( $_POST['item_meta'][ $parent ][ $pointer ]['other'][ $field['id'] ] ) ) {
920
			if ( FrmField::is_field_with_multiple_values( $field ) ) {
921
				$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...
922
			} else {
923
				$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...
924
			}
925
			return $other_val;
926
927
		} else if ( isset( $field['id'] ) && isset( $_POST['item_meta']['other'][ $field['id'] ] ) ) {
928
			// For normal fields
929
930
			if ( FrmField::is_field_with_multiple_values( $field ) ) {
931
				$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...
932
			} else {
933
				$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...
934
			}
935
			return $other_val;
936
		}
937
938
		// For checkboxes
939
		if ( $field['type'] == 'checkbox' && is_array( $field['value'] ) ) {
940
			// Check if there is an "other" val in saved value and make sure the
941
			// "other" val is not equal to the Other checkbox option
942
			if ( isset( $field['value'][ $opt_key ] ) && $field['options'][ $opt_key ] != $field['value'][ $opt_key ] ) {
943
				$other_val = $field['value'][ $opt_key ];
944
			}
945
		} else {
946
			/**
947
			 * For radio buttons and dropdowns
948
			 * Check if saved value equals any of the options. If not, set it as the other value.
949
			 */
950
			foreach ( $field['options'] as $opt_key => $opt_val ) {
951
				$temp_val = is_array( $opt_val ) ? $opt_val['value'] : $opt_val;
952
				// Multi-select dropdowns - key is not preserved
953
				if ( is_array( $field['value'] ) ) {
954
					$o_key = array_search( $temp_val, $field['value'] );
955
					if ( isset( $field['value'][ $o_key ] ) ) {
956
						unset( $field['value'][ $o_key ], $o_key );
957
					}
958
				} else if ( $temp_val == $field['value'] ) {
959
					// For radio and regular dropdowns
960
					return '';
961
				} else {
962
					$other_val = $field['value'];
963
				}
964
				unset( $opt_key, $opt_val, $temp_val );
965
			}
966
			// For multi-select dropdowns only
967
			if ( is_array( $field['value'] ) && ! empty( $field['value'] ) ) {
968
				$other_val = reset( $field['value'] );
969
			}
970
		}
971
972
		return $other_val;
973
    }
974
975
    /**
976
    * Check if there is a saved value for the "Other" text field. If so, set it as the $other_val.
977
    * Intended for front-end use
978
    *
979
    * @since 2.0.6
980
    *
981
    * @param array $args should include field, opt_key and field name
982
    * @param boolean $other_opt
983
    * @param string $checked
984
    * @return string $other_val
985
    */
986
    public static function prepare_other_input( $args, &$other_opt, &$checked ) {
987
		//Check if this is an "Other" option
988
		if ( ! self::is_other_opt( $args['opt_key'] ) ) {
989
			return;
990
		}
991
992
		$other_opt = true;
993
		$other_args = array();
994
995
		self::set_other_name( $args, $other_args );
996
		self::set_other_value( $args, $other_args );
997
998
		if ( $other_args['value'] ) {
999
			$checked = 'checked="checked" ';
1000
		}
1001
1002
        return $other_args;
1003
    }
1004
1005
	/**
1006
	 * @param array $args
1007
	 * @param array $other_args
1008
	 * @since 2.0.6
1009
	 */
1010
	private static function set_other_name( $args, &$other_args ) {
1011
		//Set up name for other field
1012
		$other_args['name'] = str_replace( '[]', '', $args['field_name'] );
1013
		$other_args['name'] = preg_replace('/\[' . $args['field']['id'] . '\]$/', '', $other_args['name']);
1014
		$other_args['name'] = $other_args['name'] . '[other]' . '[' . $args['field']['id'] . ']';
1015
1016
		//Converts item_meta[field_id] => item_meta[other][field_id] and
1017
		//item_meta[parent][0][field_id] => item_meta[parent][0][other][field_id]
1018
		if ( FrmField::is_field_with_multiple_values( $args['field'] ) ) {
1019
			$other_args['name'] .= '[' . $args['opt_key'] . ']';
1020
		}
1021
	}
1022
1023
	/**
1024
	 * Find the parent and pointer, and get text for "other" text field
1025
	 * @param array $args
1026
	 * @param array $other_args
1027
	 *
1028
	 * @since 2.0.6
1029
	 */
1030
	private static function set_other_value( $args, &$other_args ) {
1031
		$parent = '';
1032
		$pointer = '';
1033
1034
		// Check for parent ID and pointer
1035
		$temp_array = explode( '[', $args['field_name'] );
1036
1037
		// Count should only be greater than 3 if inside of a repeating section
1038
		if ( count( $temp_array ) > 3 ) {
1039
			$parent = str_replace( ']', '', $temp_array[1] );
1040
			$pointer = str_replace( ']', '', $temp_array[2]);
1041
		}
1042
1043
		// Get text for "other" text field
1044
		$other_args['value'] = self::get_other_val( array( 'opt_key' => $args['opt_key'], 'field' => $args['field'], 'parent' => $parent, 'pointer' => $pointer ) );
1045
	}
1046
1047
	/**
1048
	 * If this field includes an other option, show it
1049
	 * @param $args array
1050
	 * @since 2.0.6
1051
	 */
1052
	public static function include_other_input( $args ) {
1053
        if ( ! $args['other_opt'] ) {
1054
        	return;
1055
		}
1056
1057
		$classes = array( 'frm_other_input' );
1058
		if ( ! $args['checked'] || trim( $args['checked'] ) == '' ) {
1059
			// hide the field if the other option is not selected
1060
			$classes[] = 'frm_pos_none';
1061
		}
1062
		if ( $args['field']['type'] == 'select' && $args['field']['multiple'] ) {
1063
			$classes[] = 'frm_other_full';
1064
		}
1065
1066
		// Set up HTML ID for Other field
1067
		$other_id = self::get_other_field_html_id( $args['field']['type'], $args['html_id'], $args['opt_key'] );
1068
1069
		?><input type="text" id="<?php echo esc_attr( $other_id ) ?>" class="<?php echo sanitize_text_field( implode( ' ', $classes ) ) ?>" <?php
1070
		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...
1071
		?> name="<?php echo esc_attr( $args['name'] ) ?>" value="<?php echo esc_attr( $args['value'] ); ?>" /><?php
1072
	}
1073
1074
	/**
1075
	* Get the HTML id for an "Other" text field
1076
	* Note: This does not affect fields in repeating sections
1077
	*
1078
	* @since 2.0.08
1079
	* @param string $type - field type
1080
	* @param string $html_id
1081
	* @param string|boolean $opt_key
1082
	* @return string $other_id
1083
	*/
1084
	public static function get_other_field_html_id( $type, $html_id, $opt_key = false ) {
1085
		$other_id = $html_id;
1086
1087
		// If hidden radio field, add an opt key of 0
1088
		if ( $type == 'radio' && $opt_key === false ) {
1089
			$opt_key = 0;
1090
		}
1091
1092
		if ( $opt_key !== false ) {
1093
			$other_id .= '-' . $opt_key;
1094
		}
1095
1096
		$other_id .= '-otext';
1097
1098
		return $other_id;
1099
	}
1100
1101
	public static function clear_on_focus_html( $field, $display, $id = '' ) {
1102
		if ( $display['clear_on_focus'] ) {
1103
			echo '<span id="frm_clear_on_focus_' . esc_attr( $field['id'] . $id ) . '" class="frm-show-click">';
1104
1105
			if ( $display['default_blank'] ) {
1106
				self::show_default_blank_js( $field['default_blank'] );
1107
				echo '<input type="hidden" name="field_options[default_blank_' . esc_attr( $field['id'] ) . ']" value="' . esc_attr( $field['default_blank'] ) . '" />';
1108
			}
1109
1110
			self::show_onfocus_js( $field['clear_on_focus'] );
1111
			echo '<input type="hidden" name="field_options[clear_on_focus_' . esc_attr( $field['id'] ) . ']" value="' . esc_attr( $field['clear_on_focus'] ) . '" />';
1112
1113
			echo '</span>';
1114
		}
1115
	}
1116
1117 View Code Duplication
	public static function show_onfocus_js( $is_selected ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1118
		$atts = array(
1119
			'icon'        => 'frm_reload_icon',
1120
			'message'     => $is_selected ? __( 'Clear default value when typing', 'formidable' ) : __( 'Do not clear default value when typing', 'formidable' ),
1121
			'is_selected' => $is_selected,
1122
		);
1123
		self::show_icon_link_js( $atts );
1124
	}
1125
1126 View Code Duplication
	public static function show_default_blank_js( $is_selected ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1127
		$atts = array(
1128
			'icon'        => 'frm_error_icon',
1129
			'message'     => $is_selected ? __( 'Default value will NOT pass form validation', 'formidable' ) : __( 'Default value will pass form validation', 'formidable' ),
1130
			'is_selected' => $is_selected,
1131
		);
1132
		self::show_icon_link_js( $atts );
1133
	}
1134
1135
	public static function show_icon_link_js( $atts ) {
1136
		$atts['icon'] .= $atts['is_selected'] ? ' ' : ' frm_inactive_icon ';
1137
		?><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
1138
	}
1139
1140
	public static function switch_field_ids( $val ) {
1141
        global $frm_duplicate_ids;
1142
        $replace = array();
1143
        $replace_with = array();
1144
        foreach ( (array) $frm_duplicate_ids as $old => $new ) {
1145
			$replace[] = '[if ' . $old . ']';
1146
			$replace_with[] = '[if ' . $new . ']';
1147
			$replace[] = '[if ' . $old . ' ';
1148
			$replace_with[] = '[if ' . $new . ' ';
1149
			$replace[] = '[/if ' . $old . ']';
1150
			$replace_with[] = '[/if ' . $new . ']';
1151
			$replace[] = '[foreach ' . $old . ']';
1152
			$replace_with[] = '[foreach ' . $new . ']';
1153
			$replace[] = '[/foreach ' . $old . ']';
1154
			$replace_with[] = '[/foreach ' . $new . ']';
1155
			$replace[] = '[' . $old . ']';
1156
			$replace_with[] = '[' . $new . ']';
1157
			$replace[] = '[' . $old . ' ';
1158
			$replace_with[] = '[' . $new . ' ';
1159
            unset($old, $new);
1160
        }
1161
		if ( is_array( $val ) ) {
1162
			foreach ( $val as $k => $v ) {
1163
                $val[ $k ] = str_replace( $replace, $replace_with, $v );
1164
                unset($k, $v);
1165
            }
1166
        } else {
1167
            $val = str_replace($replace, $replace_with, $val);
1168
        }
1169
1170
        return $val;
1171
    }
1172
1173
    public static function get_us_states() {
1174
        return apply_filters( 'frm_us_states', array(
1175
            'AL' => 'Alabama', 'AK' => 'Alaska', 'AR' => 'Arkansas', 'AZ' => 'Arizona',
1176
            'CA' => 'California', 'CO' => 'Colorado', 'CT' => 'Connecticut', 'DE' => 'Delaware',
1177
            'DC' => 'District of Columbia',
1178
            'FL' => 'Florida', 'GA' => 'Georgia', 'HI' => 'Hawaii', 'ID' => 'Idaho',
1179
            'IL' => 'Illinois', 'IN' => 'Indiana', 'IA' => 'Iowa', 'KS' => 'Kansas',
1180
            'KY' => 'Kentucky', 'LA' => 'Louisiana', 'ME' => 'Maine','MD' => 'Maryland',
1181
            'MA' => 'Massachusetts', 'MI' => 'Michigan', 'MN' => 'Minnesota', 'MS' => 'Mississippi',
1182
            'MO' => 'Missouri', 'MT' => 'Montana', 'NE' => 'Nebraska', 'NV' => 'Nevada',
1183
            'NH' => 'New Hampshire', 'NJ' => 'New Jersey', 'NM' => 'New Mexico', 'NY' => 'New York',
1184
            'NC' => 'North Carolina', 'ND' => 'North Dakota', 'OH' => 'Ohio', 'OK' => 'Oklahoma',
1185
            'OR' => 'Oregon', 'PA' => 'Pennsylvania', 'RI' => 'Rhode Island', 'SC' => 'South Carolina',
1186
            'SD' => 'South Dakota', 'TN' => 'Tennessee', 'TX' => 'Texas', 'UT' => 'Utah',
1187
            'VT' => 'Vermont', 'VA' => 'Virginia', 'WA' => 'Washington', 'WV' => 'West Virginia',
1188
            'WI' => 'Wisconsin', 'WY' => 'Wyoming',
1189
        ) );
1190
    }
1191
1192
    public static function get_countries() {
1193
        return apply_filters( 'frm_countries', array(
1194
            __( 'Afghanistan', 'formidable' ), __( 'Albania', 'formidable' ), __( 'Algeria', 'formidable' ),
1195
            __( 'American Samoa', 'formidable' ), __( 'Andorra', 'formidable' ), __( 'Angola', 'formidable' ),
1196
            __( 'Anguilla', 'formidable' ), __( 'Antarctica', 'formidable' ), __( 'Antigua and Barbuda', 'formidable' ),
1197
            __( 'Argentina', 'formidable' ), __( 'Armenia', 'formidable' ), __( 'Aruba', 'formidable' ),
1198
            __( 'Australia', 'formidable' ), __( 'Austria', 'formidable' ), __( 'Azerbaijan', 'formidable' ),
1199
            __( 'Bahamas', 'formidable' ), __( 'Bahrain', 'formidable' ), __( 'Bangladesh', 'formidable' ),
1200
            __( 'Barbados', 'formidable' ), __( 'Belarus', 'formidable' ), __( 'Belgium', 'formidable' ),
1201
            __( 'Belize', 'formidable' ), __( 'Benin', 'formidable' ), __( 'Bermuda', 'formidable' ),
1202
            __( 'Bhutan', 'formidable' ), __( 'Bolivia', 'formidable' ), __( 'Bosnia and Herzegovina', 'formidable' ),
1203
            __( 'Botswana', 'formidable' ), __( 'Brazil', 'formidable' ), __( 'Brunei', 'formidable' ),
1204
            __( 'Bulgaria', 'formidable' ), __( 'Burkina Faso', 'formidable' ), __( 'Burundi', 'formidable' ),
1205
            __( 'Cambodia', 'formidable' ), __( 'Cameroon', 'formidable' ), __( 'Canada', 'formidable' ),
1206
            __( 'Cape Verde', 'formidable' ), __( 'Cayman Islands', 'formidable' ), __( 'Central African Republic', 'formidable' ),
1207
            __( 'Chad', 'formidable' ), __( 'Chile', 'formidable' ), __( 'China', 'formidable' ),
1208
            __( 'Colombia', 'formidable' ), __( 'Comoros', 'formidable' ), __( 'Congo', 'formidable' ),
1209
            __( 'Costa Rica', 'formidable' ), __( 'C&ocirc;te d\'Ivoire', 'formidable' ), __( 'Croatia', 'formidable' ),
1210
            __( 'Cuba', 'formidable' ), __( 'Cyprus', 'formidable' ), __( 'Czech Republic', 'formidable' ),
1211
            __( 'Denmark', 'formidable' ), __( 'Djibouti', 'formidable' ), __( 'Dominica', 'formidable' ),
1212
            __( 'Dominican Republic', 'formidable' ), __( 'East Timor', 'formidable' ), __( 'Ecuador', 'formidable' ),
1213
            __( 'Egypt', 'formidable' ), __( 'El Salvador', 'formidable' ), __( 'Equatorial Guinea', 'formidable' ),
1214
            __( 'Eritrea', 'formidable' ), __( 'Estonia', 'formidable' ), __( 'Ethiopia', 'formidable' ),
1215
            __( 'Fiji', 'formidable' ), __( 'Finland', 'formidable' ), __( 'France', 'formidable' ),
1216
            __( 'French Guiana', 'formidable' ), __( 'French Polynesia', 'formidable' ), __( 'Gabon', 'formidable' ),
1217
            __( 'Gambia', 'formidable' ), __( 'Georgia', 'formidable' ), __( 'Germany', 'formidable' ),
1218
            __( 'Ghana', 'formidable' ), __( 'Gibraltar', 'formidable' ), __( 'Greece', 'formidable' ),
1219
            __( 'Greenland', 'formidable' ), __( 'Grenada', 'formidable' ), __( 'Guam', 'formidable' ),
1220
            __( 'Guatemala', 'formidable' ), __( 'Guinea', 'formidable' ), __( 'Guinea-Bissau', 'formidable' ),
1221
            __( 'Guyana', 'formidable' ), __( 'Haiti', 'formidable' ), __( 'Honduras', 'formidable' ),
1222
            __( 'Hong Kong', 'formidable' ), __( 'Hungary', 'formidable' ), __( 'Iceland', 'formidable' ),
1223
            __( 'India', 'formidable' ), __( 'Indonesia', 'formidable' ), __( 'Iran', 'formidable' ),
1224
            __( 'Iraq', 'formidable' ), __( 'Ireland', 'formidable' ), __( 'Israel', 'formidable' ),
1225
            __( 'Italy', 'formidable' ), __( 'Jamaica', 'formidable' ), __( 'Japan', 'formidable' ),
1226
            __( 'Jordan', 'formidable' ), __( 'Kazakhstan', 'formidable' ), __( 'Kenya', 'formidable' ),
1227
            __( 'Kiribati', 'formidable' ), __( 'North Korea', 'formidable' ), __( 'South Korea', 'formidable' ),
1228
            __( 'Kuwait', 'formidable' ), __( 'Kyrgyzstan', 'formidable' ), __( 'Laos', 'formidable' ),
1229
            __( 'Latvia', 'formidable' ), __( 'Lebanon', 'formidable' ), __( 'Lesotho', 'formidable' ),
1230
            __( 'Liberia', 'formidable' ), __( 'Libya', 'formidable' ), __( 'Liechtenstein', 'formidable' ),
1231
            __( 'Lithuania', 'formidable' ), __( 'Luxembourg', 'formidable' ), __( 'Macedonia', 'formidable' ),
1232
            __( 'Madagascar', 'formidable' ), __( 'Malawi', 'formidable' ), __( 'Malaysia', 'formidable' ),
1233
            __( 'Maldives', 'formidable' ), __( 'Mali', 'formidable' ), __( 'Malta', 'formidable' ),
1234
            __( 'Marshall Islands', 'formidable' ), __( 'Mauritania', 'formidable' ), __( 'Mauritius', 'formidable' ),
1235
            __( 'Mexico', 'formidable' ), __( 'Micronesia', 'formidable' ), __( 'Moldova', 'formidable' ),
1236
            __( 'Monaco', 'formidable' ), __( 'Mongolia', 'formidable' ), __( 'Montenegro', 'formidable' ),
1237
            __( 'Montserrat', 'formidable' ), __( 'Morocco', 'formidable' ), __( 'Mozambique', 'formidable' ),
1238
            __( 'Myanmar', 'formidable' ), __( 'Namibia', 'formidable' ), __( 'Nauru', 'formidable' ),
1239
            __( 'Nepal', 'formidable' ), __( 'Netherlands', 'formidable' ), __( 'New Zealand', 'formidable' ),
1240
            __( 'Nicaragua', 'formidable' ), __( 'Niger', 'formidable' ), __( 'Nigeria', 'formidable' ),
1241
            __( 'Norway', 'formidable' ), __( 'Northern Mariana Islands', 'formidable' ), __( 'Oman', 'formidable' ),
1242
            __( 'Pakistan', 'formidable' ), __( 'Palau', 'formidable' ), __( 'Palestine', 'formidable' ),
1243
            __( 'Panama', 'formidable' ), __( 'Papua New Guinea', 'formidable' ), __( 'Paraguay', 'formidable' ),
1244
            __( 'Peru', 'formidable' ), __( 'Philippines', 'formidable' ), __( 'Poland', 'formidable' ),
1245
            __( 'Portugal', 'formidable' ), __( 'Puerto Rico', 'formidable' ), __( 'Qatar', 'formidable' ),
1246
            __( 'Romania', 'formidable' ), __( 'Russia', 'formidable' ), __( 'Rwanda', 'formidable' ),
1247
            __( 'Saint Kitts and Nevis', 'formidable' ), __( 'Saint Lucia', 'formidable' ),
1248
            __( 'Saint Vincent and the Grenadines', 'formidable' ), __( 'Samoa', 'formidable' ),
1249
            __( 'San Marino', 'formidable' ), __( 'Sao Tome and Principe', 'formidable' ), __( 'Saudi Arabia', 'formidable' ),
1250
            __( 'Senegal', 'formidable' ), __( 'Serbia and Montenegro', 'formidable' ), __( 'Seychelles', 'formidable' ),
1251
            __( 'Sierra Leone', 'formidable' ), __( 'Singapore', 'formidable' ), __( 'Slovakia', 'formidable' ),
1252
            __( 'Slovenia', 'formidable' ), __( 'Solomon Islands', 'formidable' ), __( 'Somalia', 'formidable' ),
1253
            __( 'South Africa', 'formidable' ), __( 'South Sudan', 'formidable' ),
1254
            __( 'Spain', 'formidable' ), __( 'Sri Lanka', 'formidable' ),
1255
            __( 'Sudan', 'formidable' ), __( 'Suriname', 'formidable' ), __( 'Swaziland', 'formidable' ),
1256
            __( 'Sweden', 'formidable' ), __( 'Switzerland', 'formidable' ), __( 'Syria', 'formidable' ),
1257
            __( 'Taiwan', 'formidable' ), __( 'Tajikistan', 'formidable' ), __( 'Tanzania', 'formidable' ),
1258
            __( 'Thailand', 'formidable' ), __( 'Togo', 'formidable' ), __( 'Tonga', 'formidable' ),
1259
            __( 'Trinidad and Tobago', 'formidable' ), __( 'Tunisia', 'formidable' ), __( 'Turkey', 'formidable' ),
1260
            __( 'Turkmenistan', 'formidable' ), __( 'Tuvalu', 'formidable' ), __( 'Uganda', 'formidable' ),
1261
            __( 'Ukraine', 'formidable' ), __( 'United Arab Emirates', 'formidable' ), __( 'United Kingdom', 'formidable' ),
1262
            __( 'United States', 'formidable' ), __( 'Uruguay', 'formidable' ), __( 'Uzbekistan', 'formidable' ),
1263
            __( 'Vanuatu', 'formidable' ), __( 'Vatican City', 'formidable' ), __( 'Venezuela', 'formidable' ),
1264
            __( 'Vietnam', 'formidable' ), __( 'Virgin Islands, British', 'formidable' ),
1265
            __( 'Virgin Islands, U.S.', 'formidable' ), __( 'Yemen', 'formidable' ), __( 'Zambia', 'formidable' ),
1266
            __( 'Zimbabwe', 'formidable' ),
1267
        ) );
1268
    }
1269
1270
	public static function get_bulk_prefilled_opts( array &$prepop ) {
1271
		$prepop[ __( 'Countries', 'formidable' ) ] = FrmFieldsHelper::get_countries();
1272
1273
        $states = FrmFieldsHelper::get_us_states();
1274
        $state_abv = array_keys($states);
1275
        sort($state_abv);
1276
		$prepop[ __( 'U.S. State Abbreviations', 'formidable' ) ] = $state_abv;
1277
1278
        $states = array_values($states);
1279
        sort($states);
1280
		$prepop[ __( 'U.S. States', 'formidable' ) ] = $states;
1281
        unset($state_abv, $states);
1282
1283
		$prepop[ __( 'Age', 'formidable' ) ] = array(
1284
            __( 'Under 18', 'formidable' ), __( '18-24', 'formidable' ), __( '25-34', 'formidable' ),
1285
            __( '35-44', 'formidable' ), __( '45-54', 'formidable' ), __( '55-64', 'formidable' ),
1286
            __( '65 or Above', 'formidable' ), __( 'Prefer Not to Answer', 'formidable' ),
1287
        );
1288
1289
		$prepop[ __( 'Satisfaction', 'formidable' ) ] = array(
1290
            __( 'Very Satisfied', 'formidable' ), __( 'Satisfied', 'formidable' ), __( 'Neutral', 'formidable' ),
1291
            __( 'Unsatisfied', 'formidable' ), __( 'Very Unsatisfied', 'formidable' ), __( 'N/A', 'formidable' ),
1292
        );
1293
1294
		$prepop[ __( 'Importance', 'formidable' ) ] = array(
1295
            __( 'Very Important', 'formidable' ), __( 'Important', 'formidable' ), __( 'Neutral', 'formidable' ),
1296
            __( 'Somewhat Important', 'formidable' ), __( 'Not at all Important', 'formidable' ), __( 'N/A', 'formidable' ),
1297
        );
1298
1299
		$prepop[ __( 'Agreement', 'formidable' ) ] = array(
1300
            __( 'Strongly Agree', 'formidable' ), __( 'Agree', 'formidable' ), __( 'Neutral', 'formidable' ),
1301
            __( 'Disagree', 'formidable' ), __( 'Strongly Disagree', 'formidable' ), __( 'N/A', 'formidable' ),
1302
        );
1303
1304
		$prepop = apply_filters( 'frm_bulk_field_choices', $prepop );
1305
    }
1306
1307
	/**
1308
	 * Display a field value selector
1309
	 *
1310
	 * @since 2.03.05
1311
	 *
1312
	 * @param int $selector_field_id
1313
	 * @param array $selector_args
1314
	 */
1315
    public static function display_field_value_selector( $selector_field_id, $selector_args ) {
1316
	    $field_value_selector = FrmFieldFactory::create_field_value_selector( $selector_field_id, $selector_args );
1317
	    $field_value_selector->display();
1318
    }
1319
1320
	/**
1321
	 * Convert a field object to a flat array
1322
	 *
1323
	 * @since 2.03.05
1324
	 *
1325
	 * @param object $field
1326
	 *
1327
	 * @return array
1328
	 */
1329
	public static function convert_field_object_to_flat_array( $field ) {
1330
		$field_options = $field->field_options;
1331
		$field_array = get_object_vars( $field );
1332
		unset( $field_array['field_options'] );
1333
		return $field_array + $field_options;
1334
	}
1335
1336
	public static function field_selection() {
1337
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::field_selection' );
1338
		return FrmField::field_selection();
1339
	}
1340
1341
	public static function pro_field_selection() {
1342
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::pro_field_selection' );
1343
		return FrmField::pro_field_selection();
1344
	}
1345
1346
	public static function is_no_save_field( $type ) {
1347
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::is_no_save_field' );
1348
		return FrmField::is_no_save_field( $type );
1349
	}
1350
1351
	public static function no_save_fields() {
1352
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::no_save_fields' );
1353
		return FrmField::no_save_fields();
1354
	}
1355
1356
	public static function is_multiple_select( $field ) {
1357
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::is_multiple_select' );
1358
		return FrmField::is_multiple_select( $field );
1359
	}
1360
1361
	public static function is_field_with_multiple_values( $field ) {
1362
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::is_field_with_multiple_values' );
1363
		return FrmField::is_field_with_multiple_values( $field );
1364
	}
1365
1366
	public static function is_required_field( $field ) {
1367
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::is_required' );
1368
		return FrmField::is_required( $field );
1369
	}
1370
1371
    public static function maybe_get_field( &$field ) {
1372
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::maybe_get_field' );
1373
		FrmField::maybe_get_field( $field );
1374
    }
1375
1376
	public static function dropdown_categories( $args ) {
1377
		_deprecated_function( __FUNCTION__, '2.02.07', 'FrmProPost::get_category_dropdown' );
1378
1379
		if ( FrmAppHelper::pro_is_installed() ) {
1380
			$args['location'] = 'front';
1381
			$dropdown = FrmProPost::get_category_dropdown( $args['field'], $args );
1382
		} else {
1383
			$dropdown = '';
1384
		}
1385
1386
		return $dropdown;
1387
	}
1388
}
1389