Completed
Push — master ( 1e23f1...3c8881 )
by Stephanie
03:12
created

FrmFieldsHelper::get_default_field_opts()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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