Completed
Push — master ( e13ecd...6ba0c4 )
by Stephanie
05:29
created

FrmFieldsHelper::get_display_value()   D

Complexity

Conditions 10
Paths 18

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 17
nc 18
nop 3
dl 0
loc 25
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
			if ( empty( $tag ) ) {
659
				continue;
660
			}
661
662
            $atts = FrmShortcodeHelper::get_shortcode_attribute_array( $shortcodes[3][ $short_key ] );
663
664
            if ( ! empty( $shortcodes[3][ $short_key ] ) ) {
665
				$tag = str_replace( array( '[', ']' ), '', $shortcodes[0][ $short_key ] );
666
                $tags = explode(' ', $tag);
667
                if ( is_array($tags) ) {
668
                    $tag = $tags[0];
669
                }
670
            } else {
671
                $tag = $shortcodes[2][ $short_key ];
672
            }
673
674
            switch ( $tag ) {
675
                case 'id':
676
                case 'key':
677
                case 'ip':
678
                    $replace_with = $shortcode_values[ $tag ];
679
                break;
680
681
                case 'user_agent':
682
                case 'user-agent':
683
                    $entry->description = maybe_unserialize($entry->description);
684
					$replace_with = FrmEntryFormat::get_browser( $entry->description['browser'] );
685
                break;
686
687
                case 'created_at':
688
                case 'created-at':
689
                case 'updated_at':
690
                case 'updated-at':
691
                    if ( isset($atts['format']) ) {
692
                        $time_format = ' ';
693
                    } else {
694
                        $atts['format'] = get_option('date_format');
695
                        $time_format = '';
696
                    }
697
698
                    $this_tag = str_replace('-', '_', $tag);
699
                    $replace_with = FrmAppHelper::get_formatted_time($entry->{$this_tag}, $atts['format'], $time_format);
700
                    unset($this_tag);
701
                break;
702
703
                case 'created_by':
704
                case 'created-by':
705
                case 'updated_by':
706
                case 'updated-by':
707
                    $this_tag = str_replace('-', '_', $tag);
708
					$replace_with = self::get_display_value( $entry->{$this_tag}, (object) array( 'type' => 'user_id' ), $atts );
709
                    unset($this_tag);
710
                break;
711
712
                case 'admin_email':
713
                case 'siteurl':
714
                case 'frmurl':
715
                case 'sitename':
716
                case 'get':
717
                    $replace_with = self::dynamic_default_values( $tag, $atts );
718
                break;
719
720
                default:
721
                    $field = FrmField::getOne( $tag );
722
                    if ( ! $field ) {
723
                        break;
724
                    }
725
726
                    $sep = isset($atts['sep']) ? $atts['sep'] : ', ';
727
728
                    $replace_with = FrmEntryMeta::get_meta_value( $entry, $field->id );
729
730
                    $atts['entry_id'] = $entry->id;
731
                    $atts['entry_key'] = $entry->item_key;
732
733
                    if ( isset($atts['show']) && $atts['show'] == 'field_label' ) {
734
                        $replace_with = $field->name;
735
                    } else if ( isset($atts['show']) && $atts['show'] == 'description' ) {
736
                        $replace_with = $field->description;
737
					} else {
738
						$string_value = $replace_with;
739
						if ( is_array( $replace_with ) ) {
740
							$string_value = implode( $sep, $replace_with );
741
						}
742
743
						if ( empty( $string_value ) && $string_value != '0' ) {
744
							$replace_with = '';
745
						} else {
746
							$replace_with = self::get_display_value( $replace_with, $field, $atts );
747
						}
748
					}
749
750
                    unset($field);
751
                break;
752
            }
753
754
            if ( isset($replace_with) ) {
755
                $content = str_replace( $shortcodes[0][ $short_key ], $replace_with, $content );
756
            }
757
758
            unset($atts, $conditional, $replace_with);
759
		}
760
761
		return $content;
762
    }
763
764
    /**
765
     * Get the value to replace a few standard shortcodes
766
     *
767
     * @since 2.0
768
     * @return string
769
     */
770
    public static function dynamic_default_values( $tag, $atts = array(), $return_array = false ) {
771
        $new_value = '';
772
        switch ( $tag ) {
773
            case 'admin_email':
774
                $new_value = get_option('admin_email');
775
                break;
776
            case 'siteurl':
777
                $new_value = FrmAppHelper::site_url();
778
                break;
779
            case 'frmurl':
780
                $new_value = FrmAppHelper::plugin_url();
781
                break;
782
            case 'sitename':
783
                $new_value = FrmAppHelper::site_name();
784
                break;
785
            case 'get':
786
                $new_value = self::process_get_shortcode( $atts, $return_array );
787
                break;
788
        }
789
790
        return $new_value;
791
    }
792
793
    /**
794
     * Process the [get] shortcode
795
     *
796
     * @since 2.0
797
     * @return string|array
798
     */
799
    public static function process_get_shortcode( $atts, $return_array = false ) {
800
        if ( ! isset($atts['param']) ) {
801
            return '';
802
        }
803
804
        if ( strpos($atts['param'], '&#91;') ) {
805
            $atts['param'] = str_replace('&#91;', '[', $atts['param']);
806
            $atts['param'] = str_replace('&#93;', ']', $atts['param']);
807
        }
808
809
        $new_value = FrmAppHelper::get_param($atts['param'], '');
810
        $new_value = FrmAppHelper::get_query_var( $new_value, $atts['param'] );
811
812
        if ( $new_value == '' ) {
813
            if ( ! isset($atts['prev_val']) ) {
814
                $atts['prev_val'] = '';
815
            }
816
817
            $new_value = isset($atts['default']) ? $atts['default'] : $atts['prev_val'];
818
        }
819
820
        if ( is_array($new_value) && ! $return_array ) {
821
            $new_value = implode(', ', $new_value);
822
        }
823
824
        return $new_value;
825
    }
826
827
	public static function get_display_value( $replace_with, $field, $atts = array() ) {
828
		$sep = isset( $atts['sep'] ) ? $atts['sep'] : ', ';
829
830
		$replace_with = apply_filters( 'frm_get_' . $field->type . '_display_value', $replace_with, $field, $atts );
831
		$replace_with = apply_filters( 'frm_get_display_value', $replace_with, $field, $atts );
832
833
        if ( $field->type == 'textarea' || $field->type == 'rte' ) {
834
            $autop = isset($atts['wpautop']) ? $atts['wpautop'] : true;
835
            if ( apply_filters('frm_use_wpautop', $autop) ) {
836
                if ( is_array($replace_with) ) {
837
                    $replace_with = implode("\n", $replace_with);
838
                }
839
                $replace_with = wpautop($replace_with);
840
            }
841
			unset( $autop );
842
		} else if ( is_array( $replace_with ) ) {
843
			if ( $atts['show'] && isset( $replace_with[ $atts['show'] ] ) ) {
844
				$replace_with = $replace_with[ $atts['show'] ];
845
			} else {
846
				$replace_with = implode( $sep, $replace_with );
847
			}
848
		}
849
850
		return $replace_with;
851
	}
852
853
	public static function get_field_types( $type ) {
854
        $single_input = array(
855
            'text', 'textarea', 'rte', 'number', 'email', 'url',
856
            'image', 'file', 'date', 'phone', 'hidden', 'time',
857
            'user_id', 'tag', 'password',
858
        );
859
		$multiple_input = array( 'radio', 'checkbox', 'select', 'scale', 'lookup' );
860
		$other_type = array( 'html', 'break' );
861
862
		$field_selection = array_merge( FrmField::pro_field_selection(), FrmField::field_selection() );
863
864
        $field_types = array();
865
        if ( in_array($type, $single_input) ) {
866
            self::field_types_for_input( $single_input, $field_selection, $field_types );
867
        } else if ( in_array($type, $multiple_input) ) {
868
            self::field_types_for_input( $multiple_input, $field_selection, $field_types );
869
        } else if ( in_array($type, $other_type) ) {
870
            self::field_types_for_input( $other_type, $field_selection, $field_types );
871
		} else if ( isset( $field_selection[ $type ] ) ) {
872
            $field_types[ $type ] = $field_selection[ $type ];
873
        }
874
875
		$field_types = apply_filters( 'frm_switch_field_types', $field_types, compact( 'type' ) );
876
        return $field_types;
877
    }
878
879
    private static function field_types_for_input( $inputs, $fields, &$field_types ) {
880
        foreach ( $inputs as $input ) {
881
            $field_types[ $input ] = $fields[ $input ];
882
            unset($input);
883
        }
884
    }
885
886
	/**
887
	 * Check if current field option is an "other" option
888
	 *
889
	 * @since 2.0.6
890
	 *
891
	 * @param string $opt_key
892
	 * @return boolean Returns true if current field option is an "Other" option
893
	 */
894
	public static function is_other_opt( $opt_key ) {
895
		return $opt_key && strpos( $opt_key, 'other_' ) === 0;
896
	}
897
898
    /**
899
    * Get value that belongs in "Other" text box
900
    *
901
    * @since 2.0.6
902
    *
903
    * @param array $args
904
    */
905
    public static function get_other_val( $args ) {
906
		$defaults = array(
907
			'opt_key' => 0, 'field' => array(),
908
			'parent' => false, 'pointer' => false,
909
		);
910
		$args = wp_parse_args( $args, $defaults );
911
912
		$opt_key = $args['opt_key'];
913
		$field = $args['field'];
914
		$parent = $args['parent'];
915
		$pointer = $args['pointer'];
916
		$other_val = '';
917
918
		// If option is an "other" option and there is a value set for this field,
919
		// check if the value belongs in the current "Other" option text field
920
		if ( ! FrmFieldsHelper::is_other_opt( $opt_key ) || ! FrmField::is_option_true( $field, 'value' ) ) {
921
			return $other_val;
922
		}
923
924
		// Check posted vals before checking saved values
925
926
		// For fields inside repeating sections - note, don't check if $pointer is true because it will often be zero
927
		if ( $parent && isset( $_POST['item_meta'][ $parent ][ $pointer ]['other'][ $field['id'] ] ) ) {
928
			if ( FrmField::is_field_with_multiple_values( $field ) ) {
929
				$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 ] ) : '';
930
			} else {
931
				$other_val = sanitize_text_field( $_POST['item_meta'][ $parent ][ $pointer ]['other'][ $field['id'] ] );
932
			}
933
			return $other_val;
934
935
		} else if ( isset( $field['id'] ) && isset( $_POST['item_meta']['other'][ $field['id'] ] ) ) {
936
			// For normal fields
937
938
			if ( FrmField::is_field_with_multiple_values( $field ) ) {
939
				$other_val = isset( $_POST['item_meta']['other'][ $field['id'] ][ $opt_key ] ) ? sanitize_text_field( $_POST['item_meta']['other'][ $field['id'] ][ $opt_key ] ) : '';
940
			} else {
941
				$other_val = sanitize_text_field( $_POST['item_meta']['other'][ $field['id'] ] );
942
			}
943
			return $other_val;
944
		}
945
946
		// For checkboxes
947
		if ( $field['type'] == 'checkbox' && is_array( $field['value'] ) ) {
948
			// Check if there is an "other" val in saved value and make sure the
949
			// "other" val is not equal to the Other checkbox option
950
			if ( isset( $field['value'][ $opt_key ] ) && $field['options'][ $opt_key ] != $field['value'][ $opt_key ] ) {
951
				$other_val = $field['value'][ $opt_key ];
952
			}
953
		} else {
954
			/**
955
			 * For radio buttons and dropdowns
956
			 * Check if saved value equals any of the options. If not, set it as the other value.
957
			 */
958
			foreach ( $field['options'] as $opt_key => $opt_val ) {
959
				$temp_val = is_array( $opt_val ) ? $opt_val['value'] : $opt_val;
960
				// Multi-select dropdowns - key is not preserved
961
				if ( is_array( $field['value'] ) ) {
962
					$o_key = array_search( $temp_val, $field['value'] );
963
					if ( isset( $field['value'][ $o_key ] ) ) {
964
						unset( $field['value'][ $o_key ], $o_key );
965
					}
966
				} else if ( $temp_val == $field['value'] ) {
967
					// For radio and regular dropdowns
968
					return '';
969
				} else {
970
					$other_val = $field['value'];
971
				}
972
				unset( $opt_key, $opt_val, $temp_val );
973
			}
974
			// For multi-select dropdowns only
975
			if ( is_array( $field['value'] ) && ! empty( $field['value'] ) ) {
976
				$other_val = reset( $field['value'] );
977
			}
978
		}
979
980
		return $other_val;
981
    }
982
983
    /**
984
    * Check if there is a saved value for the "Other" text field. If so, set it as the $other_val.
985
    * Intended for front-end use
986
    *
987
    * @since 2.0.6
988
    *
989
    * @param array $args should include field, opt_key and field name
990
    * @param boolean $other_opt
991
    * @param string $checked
992
    * @return string $other_val
993
    */
994
    public static function prepare_other_input( $args, &$other_opt, &$checked ) {
995
		//Check if this is an "Other" option
996
		if ( ! self::is_other_opt( $args['opt_key'] ) ) {
997
			return;
998
		}
999
1000
		$other_opt = true;
1001
		$other_args = array();
1002
1003
		self::set_other_name( $args, $other_args );
1004
		self::set_other_value( $args, $other_args );
1005
1006
		if ( $other_args['value'] ) {
1007
			$checked = 'checked="checked" ';
1008
		}
1009
1010
        return $other_args;
1011
    }
1012
1013
	/**
1014
	 * @param array $args
1015
	 * @param array $other_args
1016
	 * @since 2.0.6
1017
	 */
1018
	private static function set_other_name( $args, &$other_args ) {
1019
		//Set up name for other field
1020
		$other_args['name'] = str_replace( '[]', '', $args['field_name'] );
1021
		$other_args['name'] = preg_replace('/\[' . $args['field']['id'] . '\]$/', '', $other_args['name']);
1022
		$other_args['name'] = $other_args['name'] . '[other]' . '[' . $args['field']['id'] . ']';
1023
1024
		//Converts item_meta[field_id] => item_meta[other][field_id] and
1025
		//item_meta[parent][0][field_id] => item_meta[parent][0][other][field_id]
1026
		if ( FrmField::is_field_with_multiple_values( $args['field'] ) ) {
1027
			$other_args['name'] .= '[' . $args['opt_key'] . ']';
1028
		}
1029
	}
1030
1031
	/**
1032
	 * Find the parent and pointer, and get text for "other" text field
1033
	 * @param array $args
1034
	 * @param array $other_args
1035
	 *
1036
	 * @since 2.0.6
1037
	 */
1038
	private static function set_other_value( $args, &$other_args ) {
1039
		$parent = '';
1040
		$pointer = '';
1041
1042
		// Check for parent ID and pointer
1043
		$temp_array = explode( '[', $args['field_name'] );
1044
1045
		// Count should only be greater than 3 if inside of a repeating section
1046
		if ( count( $temp_array ) > 3 ) {
1047
			$parent = str_replace( ']', '', $temp_array[1] );
1048
			$pointer = str_replace( ']', '', $temp_array[2]);
1049
		}
1050
1051
		// Get text for "other" text field
1052
		$other_args['value'] = self::get_other_val( array( 'opt_key' => $args['opt_key'], 'field' => $args['field'], 'parent' => $parent, 'pointer' => $pointer ) );
1053
	}
1054
1055
	/**
1056
	 * If this field includes an other option, show it
1057
	 * @param $args array
1058
	 * @since 2.0.6
1059
	 */
1060
	public static function include_other_input( $args ) {
1061
        if ( ! $args['other_opt'] ) {
1062
        	return;
1063
		}
1064
1065
		$classes = array( 'frm_other_input' );
1066
		if ( ! $args['checked'] || trim( $args['checked'] ) == '' ) {
1067
			// hide the field if the other option is not selected
1068
			$classes[] = 'frm_pos_none';
1069
		}
1070
		if ( $args['field']['type'] == 'select' && $args['field']['multiple'] ) {
1071
			$classes[] = 'frm_other_full';
1072
		}
1073
1074
		// Set up HTML ID for Other field
1075
		$other_id = self::get_other_field_html_id( $args['field']['type'], $args['html_id'], $args['opt_key'] );
1076
1077
		?><input type="text" id="<?php echo esc_attr( $other_id ) ?>" class="<?php echo sanitize_text_field( implode( ' ', $classes ) ) ?>" <?php
1078
		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...
1079
		?> name="<?php echo esc_attr( $args['name'] ) ?>" value="<?php echo esc_attr( $args['value'] ); ?>" /><?php
1080
	}
1081
1082
	/**
1083
	* Get the HTML id for an "Other" text field
1084
	* Note: This does not affect fields in repeating sections
1085
	*
1086
	* @since 2.0.08
1087
	* @param string $type - field type
1088
	* @param string $html_id
1089
	* @param string|boolean $opt_key
1090
	* @return string $other_id
1091
	*/
1092
	public static function get_other_field_html_id( $type, $html_id, $opt_key = false ) {
1093
		$other_id = $html_id;
1094
1095
		// If hidden radio field, add an opt key of 0
1096
		if ( $type == 'radio' && $opt_key === false ) {
1097
			$opt_key = 0;
1098
		}
1099
1100
		if ( $opt_key !== false ) {
1101
			$other_id .= '-' . $opt_key;
1102
		}
1103
1104
		$other_id .= '-otext';
1105
1106
		return $other_id;
1107
	}
1108
1109
	public static function clear_on_focus_html( $field, $display, $id = '' ) {
1110
		if ( $display['clear_on_focus'] ) {
1111
			echo '<span id="frm_clear_on_focus_' . esc_attr( $field['id'] . $id ) . '" class="frm-show-click">';
1112
1113
			if ( $display['default_blank'] ) {
1114
				self::show_default_blank_js( $field['default_blank'] );
1115
				echo '<input type="hidden" name="field_options[default_blank_' . esc_attr( $field['id'] ) . ']" value="' . esc_attr( $field['default_blank'] ) . '" />';
1116
			}
1117
1118
			self::show_onfocus_js( $field['clear_on_focus'] );
1119
			echo '<input type="hidden" name="field_options[clear_on_focus_' . esc_attr( $field['id'] ) . ']" value="' . esc_attr( $field['clear_on_focus'] ) . '" />';
1120
1121
			echo '</span>';
1122
		}
1123
	}
1124
1125 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...
1126
		$atts = array(
1127
			'icon'        => 'frm_reload_icon',
1128
			'message'     => $is_selected ? __( 'Clear default value when typing', 'formidable' ) : __( 'Do not clear default value when typing', 'formidable' ),
1129
			'is_selected' => $is_selected,
1130
		);
1131
		self::show_icon_link_js( $atts );
1132
	}
1133
1134 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...
1135
		$atts = array(
1136
			'icon'        => 'frm_error_icon',
1137
			'message'     => $is_selected ? __( 'Default value will NOT pass form validation', 'formidable' ) : __( 'Default value will pass form validation', 'formidable' ),
1138
			'is_selected' => $is_selected,
1139
		);
1140
		self::show_icon_link_js( $atts );
1141
	}
1142
1143
	public static function show_icon_link_js( $atts ) {
1144
		$atts['icon'] .= $atts['is_selected'] ? ' ' : ' frm_inactive_icon ';
1145
		?><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
1146
	}
1147
1148
	public static function switch_field_ids( $val ) {
1149
        global $frm_duplicate_ids;
1150
        $replace = array();
1151
        $replace_with = array();
1152
        foreach ( (array) $frm_duplicate_ids as $old => $new ) {
1153
			$replace[] = '[if ' . $old . ']';
1154
			$replace_with[] = '[if ' . $new . ']';
1155
			$replace[] = '[if ' . $old . ' ';
1156
			$replace_with[] = '[if ' . $new . ' ';
1157
			$replace[] = '[/if ' . $old . ']';
1158
			$replace_with[] = '[/if ' . $new . ']';
1159
			$replace[] = '[foreach ' . $old . ']';
1160
			$replace_with[] = '[foreach ' . $new . ']';
1161
			$replace[] = '[/foreach ' . $old . ']';
1162
			$replace_with[] = '[/foreach ' . $new . ']';
1163
			$replace[] = '[' . $old . ']';
1164
			$replace_with[] = '[' . $new . ']';
1165
			$replace[] = '[' . $old . ' ';
1166
			$replace_with[] = '[' . $new . ' ';
1167
            unset($old, $new);
1168
        }
1169
		if ( is_array( $val ) ) {
1170
			foreach ( $val as $k => $v ) {
1171
                $val[ $k ] = str_replace( $replace, $replace_with, $v );
1172
                unset($k, $v);
1173
            }
1174
        } else {
1175
            $val = str_replace($replace, $replace_with, $val);
1176
        }
1177
1178
        return $val;
1179
    }
1180
1181
    public static function get_us_states() {
1182
        return apply_filters( 'frm_us_states', array(
1183
            'AL' => 'Alabama', 'AK' => 'Alaska', 'AR' => 'Arkansas', 'AZ' => 'Arizona',
1184
            'CA' => 'California', 'CO' => 'Colorado', 'CT' => 'Connecticut', 'DE' => 'Delaware',
1185
            'DC' => 'District of Columbia',
1186
            'FL' => 'Florida', 'GA' => 'Georgia', 'HI' => 'Hawaii', 'ID' => 'Idaho',
1187
            'IL' => 'Illinois', 'IN' => 'Indiana', 'IA' => 'Iowa', 'KS' => 'Kansas',
1188
            'KY' => 'Kentucky', 'LA' => 'Louisiana', 'ME' => 'Maine','MD' => 'Maryland',
1189
            'MA' => 'Massachusetts', 'MI' => 'Michigan', 'MN' => 'Minnesota', 'MS' => 'Mississippi',
1190
            'MO' => 'Missouri', 'MT' => 'Montana', 'NE' => 'Nebraska', 'NV' => 'Nevada',
1191
            'NH' => 'New Hampshire', 'NJ' => 'New Jersey', 'NM' => 'New Mexico', 'NY' => 'New York',
1192
            'NC' => 'North Carolina', 'ND' => 'North Dakota', 'OH' => 'Ohio', 'OK' => 'Oklahoma',
1193
            'OR' => 'Oregon', 'PA' => 'Pennsylvania', 'RI' => 'Rhode Island', 'SC' => 'South Carolina',
1194
            'SD' => 'South Dakota', 'TN' => 'Tennessee', 'TX' => 'Texas', 'UT' => 'Utah',
1195
            'VT' => 'Vermont', 'VA' => 'Virginia', 'WA' => 'Washington', 'WV' => 'West Virginia',
1196
            'WI' => 'Wisconsin', 'WY' => 'Wyoming',
1197
        ) );
1198
    }
1199
1200
    public static function get_countries() {
1201
        return apply_filters( 'frm_countries', array(
1202
            __( 'Afghanistan', 'formidable' ), __( 'Albania', 'formidable' ), __( 'Algeria', 'formidable' ),
1203
            __( 'American Samoa', 'formidable' ), __( 'Andorra', 'formidable' ), __( 'Angola', 'formidable' ),
1204
            __( 'Anguilla', 'formidable' ), __( 'Antarctica', 'formidable' ), __( 'Antigua and Barbuda', 'formidable' ),
1205
            __( 'Argentina', 'formidable' ), __( 'Armenia', 'formidable' ), __( 'Aruba', 'formidable' ),
1206
            __( 'Australia', 'formidable' ), __( 'Austria', 'formidable' ), __( 'Azerbaijan', 'formidable' ),
1207
            __( 'Bahamas', 'formidable' ), __( 'Bahrain', 'formidable' ), __( 'Bangladesh', 'formidable' ),
1208
            __( 'Barbados', 'formidable' ), __( 'Belarus', 'formidable' ), __( 'Belgium', 'formidable' ),
1209
            __( 'Belize', 'formidable' ), __( 'Benin', 'formidable' ), __( 'Bermuda', 'formidable' ),
1210
            __( 'Bhutan', 'formidable' ), __( 'Bolivia', 'formidable' ), __( 'Bosnia and Herzegovina', 'formidable' ),
1211
            __( 'Botswana', 'formidable' ), __( 'Brazil', 'formidable' ), __( 'Brunei', 'formidable' ),
1212
            __( 'Bulgaria', 'formidable' ), __( 'Burkina Faso', 'formidable' ), __( 'Burundi', 'formidable' ),
1213
            __( 'Cambodia', 'formidable' ), __( 'Cameroon', 'formidable' ), __( 'Canada', 'formidable' ),
1214
            __( 'Cape Verde', 'formidable' ), __( 'Cayman Islands', 'formidable' ), __( 'Central African Republic', 'formidable' ),
1215
            __( 'Chad', 'formidable' ), __( 'Chile', 'formidable' ), __( 'China', 'formidable' ),
1216
            __( 'Colombia', 'formidable' ), __( 'Comoros', 'formidable' ), __( 'Congo', 'formidable' ),
1217
            __( 'Costa Rica', 'formidable' ), __( 'C&ocirc;te d\'Ivoire', 'formidable' ), __( 'Croatia', 'formidable' ),
1218
            __( 'Cuba', 'formidable' ), __( 'Cyprus', 'formidable' ), __( 'Czech Republic', 'formidable' ),
1219
            __( 'Denmark', 'formidable' ), __( 'Djibouti', 'formidable' ), __( 'Dominica', 'formidable' ),
1220
            __( 'Dominican Republic', 'formidable' ), __( 'East Timor', 'formidable' ), __( 'Ecuador', 'formidable' ),
1221
            __( 'Egypt', 'formidable' ), __( 'El Salvador', 'formidable' ), __( 'Equatorial Guinea', 'formidable' ),
1222
            __( 'Eritrea', 'formidable' ), __( 'Estonia', 'formidable' ), __( 'Ethiopia', 'formidable' ),
1223
            __( 'Fiji', 'formidable' ), __( 'Finland', 'formidable' ), __( 'France', 'formidable' ),
1224
            __( 'French Guiana', 'formidable' ), __( 'French Polynesia', 'formidable' ), __( 'Gabon', 'formidable' ),
1225
            __( 'Gambia', 'formidable' ), __( 'Georgia', 'formidable' ), __( 'Germany', 'formidable' ),
1226
            __( 'Ghana', 'formidable' ), __( 'Gibraltar', 'formidable' ), __( 'Greece', 'formidable' ),
1227
            __( 'Greenland', 'formidable' ), __( 'Grenada', 'formidable' ), __( 'Guam', 'formidable' ),
1228
            __( 'Guatemala', 'formidable' ), __( 'Guinea', 'formidable' ), __( 'Guinea-Bissau', 'formidable' ),
1229
            __( 'Guyana', 'formidable' ), __( 'Haiti', 'formidable' ), __( 'Honduras', 'formidable' ),
1230
            __( 'Hong Kong', 'formidable' ), __( 'Hungary', 'formidable' ), __( 'Iceland', 'formidable' ),
1231
            __( 'India', 'formidable' ), __( 'Indonesia', 'formidable' ), __( 'Iran', 'formidable' ),
1232
            __( 'Iraq', 'formidable' ), __( 'Ireland', 'formidable' ), __( 'Israel', 'formidable' ),
1233
            __( 'Italy', 'formidable' ), __( 'Jamaica', 'formidable' ), __( 'Japan', 'formidable' ),
1234
            __( 'Jordan', 'formidable' ), __( 'Kazakhstan', 'formidable' ), __( 'Kenya', 'formidable' ),
1235
            __( 'Kiribati', 'formidable' ), __( 'North Korea', 'formidable' ), __( 'South Korea', 'formidable' ),
1236
            __( 'Kuwait', 'formidable' ), __( 'Kyrgyzstan', 'formidable' ), __( 'Laos', 'formidable' ),
1237
            __( 'Latvia', 'formidable' ), __( 'Lebanon', 'formidable' ), __( 'Lesotho', 'formidable' ),
1238
            __( 'Liberia', 'formidable' ), __( 'Libya', 'formidable' ), __( 'Liechtenstein', 'formidable' ),
1239
            __( 'Lithuania', 'formidable' ), __( 'Luxembourg', 'formidable' ), __( 'Macedonia', 'formidable' ),
1240
            __( 'Madagascar', 'formidable' ), __( 'Malawi', 'formidable' ), __( 'Malaysia', 'formidable' ),
1241
            __( 'Maldives', 'formidable' ), __( 'Mali', 'formidable' ), __( 'Malta', 'formidable' ),
1242
            __( 'Marshall Islands', 'formidable' ), __( 'Mauritania', 'formidable' ), __( 'Mauritius', 'formidable' ),
1243
            __( 'Mexico', 'formidable' ), __( 'Micronesia', 'formidable' ), __( 'Moldova', 'formidable' ),
1244
            __( 'Monaco', 'formidable' ), __( 'Mongolia', 'formidable' ), __( 'Montenegro', 'formidable' ),
1245
            __( 'Montserrat', 'formidable' ), __( 'Morocco', 'formidable' ), __( 'Mozambique', 'formidable' ),
1246
            __( 'Myanmar', 'formidable' ), __( 'Namibia', 'formidable' ), __( 'Nauru', 'formidable' ),
1247
            __( 'Nepal', 'formidable' ), __( 'Netherlands', 'formidable' ), __( 'New Zealand', 'formidable' ),
1248
            __( 'Nicaragua', 'formidable' ), __( 'Niger', 'formidable' ), __( 'Nigeria', 'formidable' ),
1249
            __( 'Norway', 'formidable' ), __( 'Northern Mariana Islands', 'formidable' ), __( 'Oman', 'formidable' ),
1250
            __( 'Pakistan', 'formidable' ), __( 'Palau', 'formidable' ), __( 'Palestine', 'formidable' ),
1251
            __( 'Panama', 'formidable' ), __( 'Papua New Guinea', 'formidable' ), __( 'Paraguay', 'formidable' ),
1252
            __( 'Peru', 'formidable' ), __( 'Philippines', 'formidable' ), __( 'Poland', 'formidable' ),
1253
            __( 'Portugal', 'formidable' ), __( 'Puerto Rico', 'formidable' ), __( 'Qatar', 'formidable' ),
1254
            __( 'Romania', 'formidable' ), __( 'Russia', 'formidable' ), __( 'Rwanda', 'formidable' ),
1255
            __( 'Saint Kitts and Nevis', 'formidable' ), __( 'Saint Lucia', 'formidable' ),
1256
            __( 'Saint Vincent and the Grenadines', 'formidable' ), __( 'Samoa', 'formidable' ),
1257
            __( 'San Marino', 'formidable' ), __( 'Sao Tome and Principe', 'formidable' ), __( 'Saudi Arabia', 'formidable' ),
1258
            __( 'Senegal', 'formidable' ), __( 'Serbia and Montenegro', 'formidable' ), __( 'Seychelles', 'formidable' ),
1259
            __( 'Sierra Leone', 'formidable' ), __( 'Singapore', 'formidable' ), __( 'Slovakia', 'formidable' ),
1260
            __( 'Slovenia', 'formidable' ), __( 'Solomon Islands', 'formidable' ), __( 'Somalia', 'formidable' ),
1261
            __( 'South Africa', 'formidable' ), __( 'South Sudan', 'formidable' ),
1262
            __( 'Spain', 'formidable' ), __( 'Sri Lanka', 'formidable' ),
1263
            __( 'Sudan', 'formidable' ), __( 'Suriname', 'formidable' ), __( 'Swaziland', 'formidable' ),
1264
            __( 'Sweden', 'formidable' ), __( 'Switzerland', 'formidable' ), __( 'Syria', 'formidable' ),
1265
            __( 'Taiwan', 'formidable' ), __( 'Tajikistan', 'formidable' ), __( 'Tanzania', 'formidable' ),
1266
            __( 'Thailand', 'formidable' ), __( 'Togo', 'formidable' ), __( 'Tonga', 'formidable' ),
1267
            __( 'Trinidad and Tobago', 'formidable' ), __( 'Tunisia', 'formidable' ), __( 'Turkey', 'formidable' ),
1268
            __( 'Turkmenistan', 'formidable' ), __( 'Tuvalu', 'formidable' ), __( 'Uganda', 'formidable' ),
1269
            __( 'Ukraine', 'formidable' ), __( 'United Arab Emirates', 'formidable' ), __( 'United Kingdom', 'formidable' ),
1270
            __( 'United States', 'formidable' ), __( 'Uruguay', 'formidable' ), __( 'Uzbekistan', 'formidable' ),
1271
            __( 'Vanuatu', 'formidable' ), __( 'Vatican City', 'formidable' ), __( 'Venezuela', 'formidable' ),
1272
            __( 'Vietnam', 'formidable' ), __( 'Virgin Islands, British', 'formidable' ),
1273
            __( 'Virgin Islands, U.S.', 'formidable' ), __( 'Yemen', 'formidable' ), __( 'Zambia', 'formidable' ),
1274
            __( 'Zimbabwe', 'formidable' ),
1275
        ) );
1276
    }
1277
1278
	public static function get_bulk_prefilled_opts( array &$prepop ) {
1279
		$prepop[ __( 'Countries', 'formidable' ) ] = FrmFieldsHelper::get_countries();
1280
1281
        $states = FrmFieldsHelper::get_us_states();
1282
        $state_abv = array_keys($states);
1283
        sort($state_abv);
1284
		$prepop[ __( 'U.S. State Abbreviations', 'formidable' ) ] = $state_abv;
1285
1286
        $states = array_values($states);
1287
        sort($states);
1288
		$prepop[ __( 'U.S. States', 'formidable' ) ] = $states;
1289
        unset($state_abv, $states);
1290
1291
		$prepop[ __( 'Age', 'formidable' ) ] = array(
1292
            __( 'Under 18', 'formidable' ), __( '18-24', 'formidable' ), __( '25-34', 'formidable' ),
1293
            __( '35-44', 'formidable' ), __( '45-54', 'formidable' ), __( '55-64', 'formidable' ),
1294
            __( '65 or Above', 'formidable' ), __( 'Prefer Not to Answer', 'formidable' ),
1295
        );
1296
1297
		$prepop[ __( 'Satisfaction', 'formidable' ) ] = array(
1298
            __( 'Very Satisfied', 'formidable' ), __( 'Satisfied', 'formidable' ), __( 'Neutral', 'formidable' ),
1299
            __( 'Unsatisfied', 'formidable' ), __( 'Very Unsatisfied', 'formidable' ), __( 'N/A', 'formidable' ),
1300
        );
1301
1302
		$prepop[ __( 'Importance', 'formidable' ) ] = array(
1303
            __( 'Very Important', 'formidable' ), __( 'Important', 'formidable' ), __( 'Neutral', 'formidable' ),
1304
            __( 'Somewhat Important', 'formidable' ), __( 'Not at all Important', 'formidable' ), __( 'N/A', 'formidable' ),
1305
        );
1306
1307
		$prepop[ __( 'Agreement', 'formidable' ) ] = array(
1308
            __( 'Strongly Agree', 'formidable' ), __( 'Agree', 'formidable' ), __( 'Neutral', 'formidable' ),
1309
            __( 'Disagree', 'formidable' ), __( 'Strongly Disagree', 'formidable' ), __( 'N/A', 'formidable' ),
1310
        );
1311
1312
		$prepop = apply_filters( 'frm_bulk_field_choices', $prepop );
1313
    }
1314
1315
	/**
1316
	 * Display a field value selector
1317
	 *
1318
	 * @since 2.03.05
1319
	 *
1320
	 * @param int $selector_field_id
1321
	 * @param array $selector_args
1322
	 */
1323
    public static function display_field_value_selector( $selector_field_id, $selector_args ) {
1324
	    $field_value_selector = FrmFieldFactory::create_field_value_selector( $selector_field_id, $selector_args );
1325
	    $field_value_selector->display();
1326
    }
1327
1328
	/**
1329
	 * Convert a field object to a flat array
1330
	 *
1331
	 * @since 2.03.05
1332
	 *
1333
	 * @param object $field
1334
	 *
1335
	 * @return array
1336
	 */
1337
	public static function convert_field_object_to_flat_array( $field ) {
1338
		$field_options = $field->field_options;
1339
		$field_array = get_object_vars( $field );
1340
		unset( $field_array['field_options'] );
1341
		return $field_array + $field_options;
1342
	}
1343
1344
	public static function field_selection() {
1345
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::field_selection' );
1346
		return FrmField::field_selection();
1347
	}
1348
1349
	public static function pro_field_selection() {
1350
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::pro_field_selection' );
1351
		return FrmField::pro_field_selection();
1352
	}
1353
1354
	public static function is_no_save_field( $type ) {
1355
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::is_no_save_field' );
1356
		return FrmField::is_no_save_field( $type );
1357
	}
1358
1359
	public static function no_save_fields() {
1360
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::no_save_fields' );
1361
		return FrmField::no_save_fields();
1362
	}
1363
1364
	public static function is_multiple_select( $field ) {
1365
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::is_multiple_select' );
1366
		return FrmField::is_multiple_select( $field );
1367
	}
1368
1369
	public static function is_field_with_multiple_values( $field ) {
1370
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::is_field_with_multiple_values' );
1371
		return FrmField::is_field_with_multiple_values( $field );
1372
	}
1373
1374
	public static function is_required_field( $field ) {
1375
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::is_required' );
1376
		return FrmField::is_required( $field );
1377
	}
1378
1379
    public static function maybe_get_field( &$field ) {
1380
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmField::maybe_get_field' );
1381
		FrmField::maybe_get_field( $field );
1382
    }
1383
1384
	public static function dropdown_categories( $args ) {
1385
		_deprecated_function( __FUNCTION__, '2.02.07', 'FrmProPost::get_category_dropdown' );
1386
1387
		if ( FrmAppHelper::pro_is_installed() ) {
1388
			$args['location'] = 'front';
1389
			$dropdown = FrmProPost::get_category_dropdown( $args['field'], $args );
1390
		} else {
1391
			$dropdown = '';
1392
		}
1393
1394
		return $dropdown;
1395
	}
1396
}
1397