Completed
Push — master ( feb49b...2e7061 )
by Stephanie
06:17 queued 03:03
created

FrmFieldsHelper::single_input_fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 17
rs 9.7
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
		$values = self::get_default_field( $type );
15
16
		global $wpdb;
17
		$field_count = FrmDb::get_var( 'frm_fields', array( 'form_id' => $form_id ), 'field_order', array( 'order_by' => 'field_order DESC' ) );
18
19
		$values['field_key']   = FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_fields', 'field_key' );
20
		$values['form_id']     = $form_id;
21
		$values['field_order'] = $field_count + 1;
22
23
		$values['field_options']['custom_html'] = self::get_default_html( $type );
24
25
		if ( isset( $setting ) && ! empty( $setting ) ) {
26
			if ( in_array( $type, array( 'data', 'lookup' ) ) ) {
27
				$values['field_options']['data_type'] = $setting;
28
			} else {
29
				$values['field_options'][ $setting ] = 1;
30
			}
31
		}
32
33
		return $values;
34
	}
35
36
	public static function get_html_id( $field, $plus = '' ) {
37
		return apply_filters( 'frm_field_html_id', 'field_' . $field['field_key'] . $plus, $field );
38
	}
39
40
	public static function setup_edit_vars( $field, $doing_ajax = false ) {
41
		$values = self::field_object_to_array( $field );
42
43
		return apply_filters( 'frm_setup_edit_field_vars', $values, array( 'doing_ajax' => $doing_ajax ) );
44
	}
45
46
	public static function field_object_to_array( $field ) {
47
		$values = (array) $field;
48
49
		self::fill_field_array( $field, $values );
50
51
		$values['custom_html'] = ( isset( $field->field_options['custom_html'] ) ) ? $field->field_options['custom_html'] : self::get_default_html( $field->type );
52
53
		return $values;
54
	}
55
56
	private static function fill_field_array( $field, array &$field_array ) {
57
		$field_array['options'] = $field->options;
58
		$field_array['value']   = $field->default_value;
59
60
		self::prepare_edit_front_field( $field_array, $field );
61
62
		$field_array = array_merge( (array) $field->field_options, $field_array );
63
	}
64
65
	/**
66
	 * Prepare field while creating a new entry
67
	 *
68
	 * @since 3.0
69
	 */
70
	public static function prepare_new_front_field( &$field_array, $field, $args = array() ) {
71
		$args['action'] = 'new';
72
		self::prepare_front_field( $field_array, $field, $args );
73
	}
74
75
	/**
76
	 * Prepare field while editing an entry
77
	 *
78
	 * @since 3.0
79
	 */
80
	public static function prepare_edit_front_field( &$field_array, $field, $entry_id = 0, $args = array() ) {
81
		$args['entry_id'] = $entry_id;
82
		$args['action']   = 'edit';
83
		self::prepare_front_field( $field_array, $field, $args );
84
	}
85
86
	/**
87
	 * Prepare field while creating a new entry
88
	 *
89
	 * @since 3.0
90
	 */
91
	private static function prepare_front_field( &$field_array, $field, $args ) {
92
		self::fill_default_field_opts( $field, $field_array );
93
		self::fill_cleared_strings( $field, $field_array );
94
95
		// Track the original field's type
96
		$field_array['original_type'] = isset( $field->field_options['original_type'] ) ? $field->field_options['original_type'] : $field->type;
97
98
		self::prepare_field_options_for_display( $field_array, $field, $args );
99
100
		if ( $args['action'] == 'edit' ) {
101
			$field_array = apply_filters( 'frm_setup_edit_fields_vars', $field_array, $field, $args['entry_id'], $args );
102
		} else {
103
			$field_array = apply_filters( 'frm_setup_new_fields_vars', $field_array, $field, $args );
104
		}
105
	}
106
107
	/**
108
	 * @since 3.0
109
	 *
110
	 * @param string $type
111
	 *
112
	 * @return array
113
	 */
114
	public static function get_default_field_options( $type ) {
115
		$field_type = FrmFieldFactory::get_field_type( $type );
116
117
		return $field_type->get_default_field_options();
118
	}
119
120
	/**
121
	 * @since 3.0
122
	 *
123
	 * @param object $field
124
	 * @param array $values
125
	 */
126
	private static function fill_default_field_opts( $field, array &$values ) {
127
		$check_post = FrmAppHelper::is_admin() && $_POST && isset( $_POST['field_options'] );
128
129
		$defaults = self::get_default_field_options_from_field( $field, $values );
130
		if ( ! $check_post ) {
131
			$defaults['required_indicator'] = '';
132
			$defaults['original_type']      = $field->type;
133
		}
134
135
		foreach ( $defaults as $opt => $default ) {
136
			$values[ $opt ] = isset( $field->field_options[ $opt ] ) ? $field->field_options[ $opt ] : $default;
137
138
			if ( $check_post ) {
139
				self::get_posted_field_setting( $opt . '_' . $field->id, $values[ $opt ] );
140
			}
141
142
			unset( $opt, $default );
143
		}
144
	}
145
146
	/**
147
	 * Fill the required message, invalid message,
148
	 * and refill the HTML when cleared
149
	 *
150
	 * @since 3.0
151
	 *
152
	 * @param object $field
153
	 * @param array $field_array
154
	 */
155
	private static function fill_cleared_strings( $field, array &$field_array ) {
156
		$frm_settings = FrmAppHelper::get_settings();
157
158
		if ( '' == $field_array['blank'] && '1' === $field_array['required'] ) {
159
			$field_array['blank'] = $frm_settings->blank_msg;
160
		}
161
162
		if ( '' == $field_array['invalid'] ) {
163
			if ( 'captcha' === $field->type ) {
164
				$field_array['invalid'] = $frm_settings->re_msg;
165
			} else {
166
				/* translators: %s: Field name */
167
				$field_array['invalid'] = sprintf( __( '%s is invalid', 'formidable' ), $field_array['name'] );
168
			}
169
		}
170
171
		if ( '' == $field_array['custom_html'] ) {
172
			$field_array['custom_html'] = self::get_default_html( $field->type );
173
		}
174
	}
175
176
	/**
177
	 * @since 3.0
178
	 *
179
	 * @param string $setting
180
	 * @param mixed $value
181
	 */
182
	private static function get_posted_field_setting( $setting, &$value ) {
183
		if ( ! isset( $_POST['field_options'][ $setting ] ) ) {
184
			return;
185
		}
186
187
		if ( strpos( $setting, 'html' ) !== false ) {
188
			// Strip slashes from HTML but not regex.
189
			$value = maybe_unserialize( wp_unslash( $_POST['field_options'][ $setting ] ) );
190
		} else {
191
			// TODO: Remove stripslashes on output, and use on input only.
192
			$value = maybe_unserialize( $_POST['field_options'][ $setting ] ); // WPCS: sanitization ok.
193
		}
194
	}
195
196
	/**
197
	 * @since 3.0
198
	 *
199
	 * @param object $field
200
	 * @param array $values The field array is needed for hooks
201
	 *
202
	 * @return array
203
	 */
204
	public static function get_default_field_options_from_field( $field, $values = array() ) {
205
		$field_type = self::get_original_field( $field );
206
		$opts       = $field_type->get_default_field_options();
207
208
		$opts = apply_filters( 'frm_default_field_opts', $opts, $values, $field );
209
		$opts = apply_filters( 'frm_default_' . $field->type . '_field_opts', $opts, $values, $field );
210
211
		return $opts;
212
	}
213
214
	/**
215
	 * @since 3.0
216
	 *
217
	 * @param object $field
218
	 *
219
	 * @return array
220
	 */
221
	private static function get_original_field( $field ) {
222
		$original_type = FrmField::get_option( $field, 'original_type' );
223
		if ( ! empty( $original_type ) && $field->type != $original_type ) {
224
			$field->type = $original_type;
225
		}
226
227
		return FrmFieldFactory::get_field_object( $field );
228
	}
229
230
	/**
231
	 * @since 3.0
232
	 *
233
	 * @param array $field_array
234
	 * @param object $field
235
	 * @param array $atts
236
	 */
237
	private static function prepare_field_options_for_display( &$field_array, $field, $atts ) {
238
		$field_obj   = FrmFieldFactory::get_field_object( $field );
239
		$field_array = $field_obj->prepare_front_field( $field_array, $atts );
240
	}
241
242
	/**
243
	 * @since 3.0
244
	 *
245
	 * @param string $type
246
	 *
247
	 * @return array
248
	 */
249
	public static function get_default_field( $type ) {
250
		$field_type = FrmFieldFactory::get_field_type( $type );
251
252
		return $field_type->get_new_field_defaults();
253
	}
254
255
	public static function fill_field( &$values, $field, $form_id, $new_key = '' ) {
256
		global $wpdb;
257
258
		$values['field_key']     = FrmAppHelper::get_unique_key( $new_key, $wpdb->prefix . 'frm_fields', 'field_key' );
259
		$values['form_id']       = $form_id;
260
		$values['options']       = maybe_serialize( $field->options );
261
		$values['default_value'] = maybe_serialize( $field->default_value );
262
263
		foreach ( array( 'name', 'description', 'type', 'field_order', 'field_options', 'required' ) as $col ) {
264
			$values[ $col ] = $field->{$col};
265
		}
266
	}
267
268
	/**
269
	 * @since 2.0
270
	 *
271
	 * @param $field
272
	 * @param $error
273
	 *
274
	 * @return string
275
	 */
276
	public static function get_error_msg( $field, $error ) {
277
		$frm_settings     = FrmAppHelper::get_settings();
278
		$default_settings = $frm_settings->default_options();
279
		$field_name       = is_array( $field ) ? $field['name'] : $field->name;
280
281
		$conf_msg = __( 'The entered values do not match', 'formidable' );
282
		$defaults = array(
283
			'unique_msg' => array(
284
				'full' => $default_settings['unique_msg'],
285
				/* translators: %s: Field name */
286
				'part' => sprintf( __( '%s must be unique', 'formidable' ), $field_name ),
287
			),
288
			'invalid'    => array(
289
				'full' => __( 'This field is invalid', 'formidable' ),
290
				/* translators: %s: Field name */
291
				'part' => sprintf( __( '%s is invalid', 'formidable' ), $field_name ),
292
			),
293
			'blank'      => array(
294
				'full' => $frm_settings->blank_msg,
295
				'part' => $frm_settings->blank_msg,
296
			),
297
			'conf_msg'   => array(
298
				'full' => $conf_msg,
299
				'part' => $conf_msg,
300
			),
301
		);
302
303
		$msg = FrmField::get_option( $field, $error );
304
		$msg = empty( $msg ) ? $defaults[ $error ]['part'] : $msg;
305
		$msg = do_shortcode( $msg );
306
307
		return $msg;
308
	}
309
310
	public static function get_form_fields( $form_id, $error = array() ) {
311
		$fields = FrmField::get_all_for_form( $form_id );
312
313
		return apply_filters( 'frm_get_paged_fields', $fields, $form_id, $error );
314
	}
315
316
	public static function get_default_html( $type = 'text' ) {
317
		$field        = FrmFieldFactory::get_field_type( $type );
318
		$default_html = $field->default_html();
319
320
		// these hooks are here for reverse compatibility since 3.0
321
		if ( ! apply_filters( 'frm_normal_field_type_html', true, $type ) ) {
322
			$default_html = apply_filters( 'frm_other_custom_html', '', $type );
323
		}
324
325
		return apply_filters( 'frm_custom_html', $default_html, $type );
326
	}
327
328
	/**
329
	 * @param array $fields
330
	 * @param array $errors
331
	 * @param object $form
332
	 * @param $form_action
333
	 */
334
	public static function show_fields( $fields, $errors, $form, $form_action ) {
335
		foreach ( $fields as $field ) {
336
			$field_obj = FrmFieldFactory::get_field_type( $field['type'], $field );
337
			$field_obj->show_field( compact( 'errors', 'form', 'form_action' ) );
338
		}
339
	}
340
341
	/**
342
	 * @since 3.0
343
	 *
344
	 * @param array $atts
345
	 * @param string|array $value
346
	 */
347
	public static function run_wpautop( $atts, &$value ) {
348
		$autop = isset( $atts['wpautop'] ) ? $atts['wpautop'] : true;
349
		if ( apply_filters( 'frm_use_wpautop', $autop ) ) {
350
			if ( is_array( $value ) ) {
351
				$value = implode( "\n", $value );
352
			}
353
			$value = wpautop( $value );
354
		}
355
	}
356
357
	/**
358
	 * Get the class to use for the label position
359
	 *
360
	 * @since 2.05
361
	 */
362
	public static function &label_position( $position, $field, $form ) {
363
		if ( $position && $position != '' ) {
364
			if ( $position == 'inside' && ! self::is_placeholder_field_type( $field['type'] ) ) {
365
				$position = 'top';
366
			}
367
368
			return $position;
369
		}
370
371
		$position = FrmStylesController::get_style_val( 'position', $form );
372
		if ( $position == 'none' ) {
373
			$position = 'top';
374
		} elseif ( $position == 'no_label' ) {
375
			$position = 'none';
376
		} elseif ( $position == 'inside' && ! self::is_placeholder_field_type( $field['type'] ) ) {
377
			$position = 'top';
378
		}
379
380
		$position = apply_filters( 'frm_html_label_position', $position, $field, $form );
381
		$position = ( ! empty( $position ) ) ? $position : 'top';
382
383
		return $position;
384
	}
385
386
	/**
387
	 * Check if this field type allows placeholders
388
	 *
389
	 * @since 2.05
390
	 */
391
	public static function is_placeholder_field_type( $type ) {
392
		return ! in_array( $type, array( 'radio', 'checkbox', 'hidden', 'file' ) );
393
	}
394
395
	public static function get_checkbox_id( $field, $opt_key, $type = 'checkbox' ) {
396
		$id = $field['id'];
397
		if ( isset( $field['in_section'] ) && $field['in_section'] && ! FrmAppHelper::is_admin_page( 'formidable' ) ) {
398
			$id .= '-' . $field['in_section'];
399
		}
400
401
		return 'frm_' . $type . '_' . $id . '-' . $opt_key;
402
	}
403
404
	public static function show_single_option( $field ) {
405
		self::hidden_field_option( $field );
406
407
		if ( ! is_array( $field['options'] ) ) {
408
			return;
409
		}
410
411
		$base_name = 'default_value_' . $field['id'];
412
		$html_id    = isset( $field['html_id'] ) ? $field['html_id'] : self::get_html_id( $field );
413
414
		$default_type = self::get_default_value_type( $field );
415
416
		foreach ( $field['options'] as $opt_key => $opt ) {
417
			$field_val = self::get_value_from_array( $opt, $opt_key, $field );
418
			$opt       = self::get_label_from_array( $opt, $opt_key, $field );
419
420
			$field_name = $base_name . ( $default_type === 'checkbox' ? '[' . $opt_key . ']' : '' );
421
422
			$checked = ( isset( $field['default_value'] ) && ( ( ! is_array( $field['default_value'] ) && $field['default_value'] == $field_val ) || ( is_array( $field['default_value'] ) && in_array( $field_val, $field['default_value'] ) ) ) );
423
424
			// If this is an "Other" option, get the HTML for it.
425
			if ( self::is_other_opt( $opt_key ) ) {
426
				if ( FrmAppHelper::pro_is_installed() ) {
427
					require( FrmProAppHelper::plugin_path() . '/classes/views/frmpro-fields/other-option.php' );
428
				}
429
			} else {
430
				require( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/single-option.php' );
431
			}
432
433
			unset( $checked );
434
		}
435
	}
436
437
	/**
438
	 * Include hidden row for javascript to duplicate.
439
	 *
440
	 * @since 4.0
441
	 * @param array $field
442
	 */
443
	private static function hidden_field_option( $field ) {
444
		// Don't duplicate during an ajax add option.
445
		$ajax_action = FrmAppHelper::get_param( 'action', '', 'post', 'sanitize_text_field' );
446
		if ( $ajax_action === 'frm_add_field_option' ) {
447
			return;
448
		}
449
450
		$opt_key    = '000';
451
		$field_val  = __( 'New Option', 'formidable' );
452
		$opt        = __( 'New Option', 'formidable' );
453
		$checked    = false;
454
		$field_name = 'default_value_' . $field['id'];
455
		$html_id    = isset( $field['html_id'] ) ? $field['html_id'] : self::get_html_id( $field );
456
457
		$default_type = self::get_default_value_type( $field );
458
		$field_name  .= ( $default_type === 'checkbox' ? '[' . $opt_key . ']' : '' );
459
460
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/single-option.php' );
461
	}
462
463
	/**
464
	 * @since 4.0
465
	 *
466
	 * @param array $field
467
	 * @return string radio or checkbox
468
	 */
469
	private static function get_default_value_type( $field ) {
470
		$default_type = $field['type'];
471
		if ( $field['type'] === 'select' ) {
472
			$default_type = FrmField::is_multiple_select( $field ) ? 'checkbox' : 'radio';
473
		}
474
		return $default_type;
475
	}
476
477
	public static function get_value_from_array( $opt, $opt_key, $field ) {
478
		$opt = apply_filters( 'frm_field_value_saved', $opt, $opt_key, $field );
479
480
		return FrmFieldsController::check_value( $opt, $opt_key, $field );
481
	}
482
483
	public static function get_label_from_array( $opt, $opt_key, $field ) {
484
		$opt = apply_filters( 'frm_field_label_seen', $opt, $opt_key, $field );
485
486
		return FrmFieldsController::check_label( $opt );
487
	}
488
489
	/**
490
	 * @since 4.0
491
	 */
492
	public static function inline_modal( $args ) {
493
		$defaults = array(
494
			'id'       => '',
495
			'class'    => '',
496
			'show'     => 0,
497
			'callback' => array(),
498
			'args'     => array(),
499
			'title'    => '',
500
		);
501
		$args = array_merge( $defaults, $args );
502
503
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/inline-modal.php' );
504
	}
505
506
	/**
507
	 * @since 4.0
508
	 */
509
	public static function smart_values() {
510
		$continue = apply_filters( 'frm_smart_values_box', true );
511
		if ( $continue === true ) {
512
			$upgrade_link = array(
513
				'medium'  => 'builder',
514
				'content' => 'smart-tags',
515
			);
516
			include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/smart-values.php' );
517
		}
518
	}
519
520
	/**
521
	 * @since 4.0
522
	 */
523
	public static function input_mask() {
524
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/input-mask-info.php' );
525
	}
526
527
	/**
528
	 * @since 4.0
529
	 */
530
	public static function layout_classes() {
531
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/layout-classes.php' );
532
	}
533
534
	/**
535
	 * @param int $tax_id
536
	 *
537
	 * @return string
538
	 */
539
	public static function get_term_link( $tax_id ) {
540
		$tax = get_taxonomy( $tax_id );
541
		if ( ! $tax ) {
542
			return '';
543
		}
544
545
		$link = sprintf(
546
			/* translators: %1$s: Start HTML link, %2$s: Content type label, %3$s: Content type, %4$s: End HTML link */
547
			esc_html__( 'Options are dynamically created from your %1$s%2$s: %3$s%4$s', 'formidable' ),
548
			'<a href="' . esc_url( admin_url( 'edit-tags.php?taxonomy=' . $tax->name ) ) . '" target="_blank">',
549
			esc_html__( 'taxonomy', 'formidable' ),
550
			empty( $tax->labels->name ) ? esc_html__( 'Categories', 'formidable' ) : $tax->labels->name,
551
			'</a>'
552
		);
553
		unset( $tax );
554
555
		return $link;
556
	}
557
558
	public static function value_meets_condition( $observed_value, $cond, $hide_opt ) {
559
		$hide_opt       = self::get_value_for_comparision( $hide_opt );
560
		$observed_value = self::get_value_for_comparision( $observed_value );
561
562
		if ( is_array( $observed_value ) ) {
563
			return self::array_value_condition( $observed_value, $cond, $hide_opt );
564
		}
565
566
		$m = false;
567
		if ( $cond == '==' ) {
568
			$m = $observed_value == $hide_opt;
569
		} elseif ( $cond == '!=' ) {
570
			$m = $observed_value != $hide_opt;
571
		} elseif ( $cond == '>' ) {
572
			$m = $observed_value > $hide_opt;
573
		} elseif ( $cond == '>=' ) {
574
			$m = $observed_value >= $hide_opt;
575
		} elseif ( $cond == '<' ) {
576
			$m = $observed_value < $hide_opt;
577
		} elseif ( $cond == '<=' ) {
578
			$m = $observed_value <= $hide_opt;
579
		} elseif ( $cond == 'LIKE' || $cond == 'not LIKE' ) {
580
			$m = stripos( $observed_value, $hide_opt );
581
			if ( $cond == 'not LIKE' ) {
582
				$m = ( $m === false ) ? true : false;
583
			} else {
584
				$m = ( $m === false ) ? false : true;
585
			}
586
		}
587
588
		return $m;
589
	}
590
591
	/**
592
	 * Trim and sanitize the values
593
	 *
594
	 * @since 2.05
595
	 */
596
	private static function get_value_for_comparision( $value ) {
597
		// Remove white space from hide_opt
598
		if ( ! is_array( $value ) ) {
599
			$value = trim( $value );
600
		}
601
602
		return wp_kses_post( $value );
603
	}
604
605
	public static function array_value_condition( $observed_value, $cond, $hide_opt ) {
606
		$m = false;
607
		if ( $cond == '==' ) {
608
			if ( is_array( $hide_opt ) ) {
609
				$m = array_intersect( $hide_opt, $observed_value );
610
				$m = empty( $m ) ? false : true;
611
			} else {
612
				$m = in_array( $hide_opt, $observed_value );
613
			}
614
		} elseif ( $cond == '!=' ) {
615
			$m = ! in_array( $hide_opt, $observed_value );
616
		} elseif ( $cond == '>' ) {
617
			$min = min( $observed_value );
618
			$m   = $min > $hide_opt;
619
		} elseif ( $cond == '<' ) {
620
			$max = max( $observed_value );
621
			$m   = $max < $hide_opt;
622
		} elseif ( $cond == 'LIKE' || $cond == 'not LIKE' ) {
623
			foreach ( $observed_value as $ob ) {
624
				$m = strpos( $ob, $hide_opt );
625
				if ( $m !== false ) {
626
					$m = true;
627
					break;
628
				}
629
			}
630
631
			if ( $cond == 'not LIKE' ) {
632
				$m = ( $m === false ) ? true : false;
633
			}
634
		}
635
636
		return $m;
637
	}
638
639
	/**
640
	 * Replace a few basic shortcodes and field ids
641
	 *
642
	 * @since 2.0
643
	 * @return string
644
	 */
645
	public static function basic_replace_shortcodes( $value, $form, $entry ) {
646
		if ( strpos( $value, '[sitename]' ) !== false ) {
647
			$new_value = wp_specialchars_decode( FrmAppHelper::site_name(), ENT_QUOTES );
648
			$value     = str_replace( '[sitename]', $new_value, $value );
649
		}
650
651
		$value = apply_filters( 'frm_content', $value, $form, $entry );
652
		$value = do_shortcode( $value );
653
654
		return $value;
655
	}
656
657
	public static function get_shortcodes( $content, $form_id ) {
658
		if ( FrmAppHelper::pro_is_installed() ) {
659
			return FrmProDisplaysHelper::get_shortcodes( $content, $form_id );
660
		}
661
662
		$fields = FrmField::getAll(
663
			array(
664
				'fi.form_id'  => (int) $form_id,
665
				'fi.type not' => FrmField::no_save_fields(),
666
			)
667
		);
668
669
		$tagregexp = self::allowed_shortcodes( $fields );
670
671
		preg_match_all( "/\[(if )?($tagregexp)\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?/s", $content, $matches, PREG_PATTERN_ORDER );
672
673
		return $matches;
674
	}
675
676
	public static function allowed_shortcodes( $fields = array() ) {
677
		$tagregexp = array(
678
			'editlink',
679
			'id',
680
			'key',
681
			'ip',
682
			'siteurl',
683
			'sitename',
684
			'admin_email',
685
			'post[-|_]id',
686
			'created[-|_]at',
687
			'updated[-|_]at',
688
			'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
699
		return $tagregexp;
700
	}
701
702
	public static function replace_content_shortcodes( $content, $entry, $shortcodes ) {
703
		foreach ( $shortcodes[0] as $short_key => $tag ) {
704
			if ( empty( $tag ) ) {
705
				continue;
706
			}
707
708
			$atts = FrmShortcodeHelper::get_shortcode_attribute_array( $shortcodes[3][ $short_key ] );
709
			$tag  = FrmShortcodeHelper::get_shortcode_tag( $shortcodes, $short_key );
710
711
			$atts['entry'] = $entry;
712
			$atts['tag']   = $tag;
713
			$replace_with  = self::get_value_for_shortcode( $atts );
714
715
			if ( $replace_with !== null ) {
716
				self::sanitize_embedded_shortcodes( compact( 'entry' ), $replace_with );
717
				$content = str_replace( $shortcodes[0][ $short_key ], $replace_with, $content );
718
			}
719
720
			unset( $atts, $replace_with );
721
		}
722
723
		return $content;
724
	}
725
726
	/**
727
	 * Prevent shortcodes in fields from being processed
728
	 *
729
	 * @since 3.01.02
730
	 *
731
	 * @param array $atts - includes entry object
732
	 * @param string $value
733
	 */
734
	public static function sanitize_embedded_shortcodes( $atts, &$value ) {
735
		$atts['value']   = $value;
736
		$should_sanitize = apply_filters( 'frm_sanitize_shortcodes', true, $atts );
737
		if ( $should_sanitize ) {
738
			$value = str_replace( '[', '&#91;', $value );
739
		}
740
	}
741
742
	/**
743
	 * @since 3.0
744
	 *
745
	 * @param $atts
746
	 *
747
	 * @return string
748
	 */
749
	private static function get_value_for_shortcode( $atts ) {
750
		$clean_tag = str_replace( '-', '_', $atts['tag'] );
751
752
		$shortcode_values = array(
753
			'id'  => $atts['entry']->id,
754
			'key' => $atts['entry']->item_key,
755
			'ip'  => $atts['entry']->ip,
756
		);
757
758
		$dynamic_default = array( 'admin_email', 'siteurl', 'frmurl', 'sitename', 'get' );
759
760
		if ( isset( $shortcode_values[ $atts['tag'] ] ) ) {
761
			$replace_with = $shortcode_values[ $atts['tag'] ];
762
		} elseif ( in_array( $atts['tag'], $dynamic_default ) ) {
763
			$replace_with = self::dynamic_default_values( $atts['tag'], $atts );
764
		} elseif ( $clean_tag == 'user_agent' ) {
765
			$description  = maybe_unserialize( $atts['entry']->description );
766
			$replace_with = FrmEntriesHelper::get_browser( $description['browser'] );
767
		} elseif ( $clean_tag == 'created_at' || $clean_tag == 'updated_at' ) {
768
			$atts['tag']  = $clean_tag;
769
			$replace_with = self::get_entry_timestamp( $atts );
770
		} elseif ( $clean_tag == 'created_by' || $clean_tag == 'updated_by' ) {
771
			$replace_with = self::get_display_value( $atts['entry']->{$clean_tag}, (object) array( 'type' => 'user_id' ), $atts );
772
		} else {
773
			$replace_with = self::get_field_shortcode_value( $atts );
774
		}
775
776
		return $replace_with;
777
	}
778
779
	/**
780
	 * @since 3.0
781
	 *
782
	 * @param $atts
783
	 *
784
	 * @return string
785
	 */
786
	private static function get_entry_timestamp( $atts ) {
787
		if ( isset( $atts['format'] ) ) {
788
			$time_format = ' ';
789
		} else {
790
			$atts['format'] = get_option( 'date_format' );
791
			$time_format    = '';
792
		}
793
794
		return FrmAppHelper::get_formatted_time( $atts['entry']->{$atts['tag']}, $atts['format'], $time_format );
795
	}
796
797
	/**
798
	 * @since 3.0
799
	 *
800
	 * @param $atts
801
	 *
802
	 * @return null|string
803
	 */
804
	private static function get_field_shortcode_value( $atts ) {
805
		$field = FrmField::getOne( $atts['tag'] );
806
		if ( empty( $field ) ) {
807
			return null;
808
		}
809
810
		if ( isset( $atts['show'] ) && $atts['show'] == 'field_label' ) {
811
			$replace_with = $field->name;
812
		} elseif ( isset( $atts['show'] ) && $atts['show'] == 'description' ) {
813
			$replace_with = $field->description;
814
		} else {
815
			$replace_with = FrmEntryMeta::get_meta_value( $atts['entry'], $field->id );
816
			$string_value = $replace_with;
817
			if ( is_array( $replace_with ) ) {
818
				$sep          = isset( $atts['sep'] ) ? $atts['sep'] : ', ';
819
				$string_value = implode( $sep, $replace_with );
820
			}
821
822
			if ( empty( $string_value ) && $string_value != '0' ) {
823
				$replace_with = '';
824
			} else {
825
				$atts['entry_id']  = $atts['entry']->id;
826
				$atts['entry_key'] = $atts['entry']->item_key;
827
				$replace_with      = self::get_display_value( $replace_with, $field, $atts );
828
			}
829
		}
830
831
		return $replace_with;
832
	}
833
834
	/**
835
	 * Get the value to replace a few standard shortcodes
836
	 *
837
	 * @since 2.0
838
	 * @return string
839
	 */
840
	public static function dynamic_default_values( $tag, $atts = array(), $return_array = false ) {
841
		$new_value = '';
842
		switch ( $tag ) {
843
			case 'admin_email':
844
				$new_value = get_option( 'admin_email' );
845
				break;
846
			case 'siteurl':
847
				$new_value = FrmAppHelper::site_url();
848
				break;
849
			case 'frmurl':
850
				$new_value = FrmAppHelper::plugin_url();
851
				break;
852
			case 'sitename':
853
				$new_value = FrmAppHelper::site_name();
854
				break;
855
			case 'get':
856
				$new_value = self::process_get_shortcode( $atts, $return_array );
857
		}
858
859
		return $new_value;
860
	}
861
862
	/**
863
	 * Process the [get] shortcode
864
	 *
865
	 * @since 2.0
866
	 * @return string|array
867
	 */
868
	public static function process_get_shortcode( $atts, $return_array = false ) {
869
		if ( ! isset( $atts['param'] ) ) {
870
			return '';
871
		}
872
873
		if ( strpos( $atts['param'], '&#91;' ) ) {
874
			$atts['param'] = str_replace( '&#91;', '[', $atts['param'] );
875
			$atts['param'] = str_replace( '&#93;', ']', $atts['param'] );
876
		}
877
878
		$new_value = FrmAppHelper::get_param( $atts['param'], '', 'get', 'sanitize_text_field' );
879
		$new_value = FrmAppHelper::get_query_var( $new_value, $atts['param'] );
880
881
		if ( $new_value == '' ) {
882
			if ( ! isset( $atts['prev_val'] ) ) {
883
				$atts['prev_val'] = '';
884
			}
885
886
			$new_value = isset( $atts['default'] ) ? $atts['default'] : $atts['prev_val'];
887
		}
888
889
		if ( is_array( $new_value ) && ! $return_array ) {
890
			$new_value = implode( ', ', $new_value );
891
		}
892
893
		return $new_value;
894
	}
895
896
	public static function get_display_value( $value, $field, $atts = array() ) {
897
898
		$value = apply_filters( 'frm_get_' . $field->type . '_display_value', $value, $field, $atts );
899
		$value = apply_filters( 'frm_get_display_value', $value, $field, $atts );
900
901
		return self::get_unfiltered_display_value( compact( 'value', 'field', 'atts' ) );
902
	}
903
904
	/**
905
	 * @param $atts array Includes value, field, and atts
906
	 */
907
	public static function get_unfiltered_display_value( $atts ) {
908
		$value = $atts['value'];
909
		$field = $atts['field'];
910
		$atts  = isset( $atts['atts'] ) ? $atts['atts'] : $atts;
911
912
		if ( is_array( $field ) ) {
913
			$field = $field['id'];
914
		}
915
916
		$field_obj = FrmFieldFactory::get_field_object( $field );
917
918
		return $field_obj->get_display_value( $value, $atts );
919
	}
920
921
	/**
922
	 * Get a value from the user profile from the user ID
923
	 *
924
	 * @since 3.0
925
	 */
926
	public static function get_user_display_name( $user_id, $user_info = 'display_name', $args = array() ) {
927
		$defaults = array(
928
			'blank' => false,
929
			'link'  => false,
930
			'size'  => 96,
931
		);
932
933
		$args = wp_parse_args( $args, $defaults );
934
935
		$user = get_userdata( $user_id );
936
		$info = '';
937
938
		if ( $user ) {
939
			if ( $user_info == 'avatar' ) {
940
				$info = get_avatar( $user_id, $args['size'] );
941
			} elseif ( $user_info == 'author_link' ) {
942
				$info = get_author_posts_url( $user_id );
943
			} else {
944
				$info = isset( $user->$user_info ) ? $user->$user_info : '';
945
			}
946
947
			if ( 'display_name' === $user_info && empty( $info ) && ! $args['blank'] ) {
948
				$info = $user->user_login;
949
			}
950
		}
951
952
		if ( $args['link'] ) {
953
			$info = '<a href="' . esc_url( admin_url( 'user-edit.php?user_id=' . $user_id ) ) . '">' . $info . '</a>';
954
		}
955
956
		return $info;
957
	}
958
959
	public static function get_field_types( $type ) {
960
		$single_input   = self::single_input_fields();
961
		$multiple_input = array( 'radio', 'checkbox', 'select', 'scale', 'star', 'lookup' );
962
963
		$field_selection = FrmField::all_field_selection();
964
965
		$field_types = array();
966
		if ( in_array( $type, $single_input ) ) {
967
			self::field_types_for_input( $single_input, $field_selection, $field_types );
968
		} elseif ( in_array( $type, $multiple_input ) ) {
969
			self::field_types_for_input( $multiple_input, $field_selection, $field_types );
970
		} elseif ( isset( $field_selection[ $type ] ) ) {
971
			$field_types[ $type ] = $field_selection[ $type ];
972
		}
973
974
		$field_types = apply_filters( 'frm_switch_field_types', $field_types, compact( 'type' ) );
975
976
		return $field_types;
977
	}
978
979
	/**
980
	 * Get a list of all fields that use a single value input.
981
	 *
982
	 * @since 4.0
983
	 */
984
	public static function single_input_fields() {
985
		$fields = array(
986
			'text',
987
			'textarea',
988
			'rte',
989
			'number',
990
			'email',
991
			'url',
992
			'date',
993
			'phone',
994
			'hidden',
995
			'time',
996
			'tag',
997
			'password',
998
		);
999
		return apply_filters( 'frm_single_input_fields', $fields );
1000
	}
1001
1002
	private static function field_types_for_input( $inputs, $fields, &$field_types ) {
1003
		foreach ( $inputs as $input ) {
1004
			$field_types[ $input ] = $fields[ $input ];
1005
			unset( $input );
1006
		}
1007
	}
1008
1009
	/**
1010
	 * Check if current field option is an "other" option
1011
	 *
1012
	 * @since 2.0.6
1013
	 *
1014
	 * @param string $opt_key
1015
	 *
1016
	 * @return boolean Returns true if current field option is an "Other" option
1017
	 */
1018
	public static function is_other_opt( $opt_key ) {
1019
		return $opt_key && strpos( $opt_key, 'other_' ) === 0;
1020
	}
1021
1022
	/**
1023
	 * Get value that belongs in "Other" text box
1024
	 *
1025
	 * @since 2.0.6
1026
	 *
1027
	 * @param array $args
1028
	 */
1029
	public static function get_other_val( $args ) {
1030
		$defaults = array(
1031
			'opt_key' => 0,
1032
			'field'   => array(),
1033
			'parent'  => false,
1034
			'pointer' => false,
1035
		);
1036
		$args     = wp_parse_args( $args, $defaults );
1037
1038
		$opt_key   = $args['opt_key'];
1039
		$field     = $args['field'];
1040
		$parent    = $args['parent'];
1041
		$pointer   = $args['pointer'];
1042
		$other_val = '';
1043
1044
		// If option is an "other" option and there is a value set for this field,
1045
		// check if the value belongs in the current "Other" option text field
1046
		if ( ! self::is_other_opt( $opt_key ) || ! FrmField::is_option_true( $field, 'value' ) ) {
1047
			return $other_val;
1048
		}
1049
1050
		// Check posted vals before checking saved values
1051
1052
		// For fields inside repeating sections - note, don't check if $pointer is true because it will often be zero
1053
		if ( $parent && isset( $_POST['item_meta'][ $parent ][ $pointer ]['other'][ $field['id'] ] ) ) {
1054
			if ( FrmField::is_field_with_multiple_values( $field ) ) {
1055
				$other_val = isset( $_POST['item_meta'][ $parent ][ $pointer ]['other'][ $field['id'] ][ $opt_key ] ) ? sanitize_text_field( wp_unslash( $_POST['item_meta'][ $parent ][ $pointer ]['other'][ $field['id'] ][ $opt_key ] ) ) : '';
1056 View Code Duplication
			} else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
1057
				$other_val = sanitize_text_field( wp_unslash( $_POST['item_meta'][ $parent ][ $pointer ]['other'][ $field['id'] ] ) );
1058
			}
1059
1060
			return $other_val;
1061
1062
		} elseif ( isset( $field['id'] ) && isset( $_POST['item_meta']['other'][ $field['id'] ] ) ) {
1063
			// For normal fields
1064
1065
			if ( FrmField::is_field_with_multiple_values( $field ) ) {
1066
				$other_val = isset( $_POST['item_meta']['other'][ $field['id'] ][ $opt_key ] ) ? sanitize_text_field( wp_unslash( $_POST['item_meta']['other'][ $field['id'] ][ $opt_key ] ) ) : '';
1067 View Code Duplication
			} else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
1068
				$other_val = sanitize_text_field( wp_unslash( $_POST['item_meta']['other'][ $field['id'] ] ) );
1069
			}
1070
1071
			return $other_val;
1072
		}
1073
1074
		// For checkboxes
1075
		if ( $field['type'] == 'checkbox' && is_array( $field['value'] ) ) {
1076
			// Check if there is an "other" val in saved value and make sure the
1077
			// "other" val is not equal to the Other checkbox option
1078
			if ( isset( $field['value'][ $opt_key ] ) && $field['options'][ $opt_key ] != $field['value'][ $opt_key ] ) {
1079
				$other_val = $field['value'][ $opt_key ];
1080
			}
1081
		} else {
1082
			/**
1083
			 * For radio buttons and dropdowns
1084
			 * Check if saved value equals any of the options. If not, set it as the other value.
1085
			 */
1086
			foreach ( $field['options'] as $opt_key => $opt_val ) {
1087
				$temp_val = is_array( $opt_val ) ? $opt_val['value'] : $opt_val;
1088
				// Multi-select dropdowns - key is not preserved
1089
				if ( is_array( $field['value'] ) ) {
1090
					$o_key = array_search( $temp_val, $field['value'] );
1091
					if ( isset( $field['value'][ $o_key ] ) ) {
1092
						unset( $field['value'][ $o_key ], $o_key );
1093
					}
1094
				} elseif ( $temp_val == $field['value'] ) {
1095
					// For radio and regular dropdowns
1096
					return '';
1097
				} else {
1098
					$other_val = $field['value'];
1099
				}
1100
				unset( $opt_key, $opt_val, $temp_val );
1101
			}
1102
			// For multi-select dropdowns only
1103
			if ( is_array( $field['value'] ) && ! empty( $field['value'] ) ) {
1104
				$other_val = reset( $field['value'] );
1105
			}
1106
		}
1107
1108
		return $other_val;
1109
	}
1110
1111
	/**
1112
	 * Check if there is a saved value for the "Other" text field. If so, set it as the $other_val.
1113
	 * Intended for front-end use
1114
	 *
1115
	 * @since 2.0.6
1116
	 *
1117
	 * @param array $args should include field, opt_key and field name
1118
	 * @param boolean $other_opt
1119
	 * @param string $checked
1120
	 *
1121
	 * @return array $other_args
1122
	 */
1123
	public static function prepare_other_input( $args, &$other_opt, &$checked ) {
1124
		//Check if this is an "Other" option
1125
		if ( ! self::is_other_opt( $args['opt_key'] ) ) {
1126
			return;
1127
		}
1128
1129
		$other_opt  = true;
1130
		$other_args = array();
1131
1132
		self::set_other_name( $args, $other_args );
1133
		self::set_other_value( $args, $other_args );
1134
1135
		if ( $other_args['value'] ) {
1136
			$checked = 'checked="checked" ';
1137
		}
1138
1139
		return $other_args;
1140
	}
1141
1142
	/**
1143
	 * @param array $args
1144
	 * @param array $other_args
1145
	 *
1146
	 * @since 2.0.6
1147
	 */
1148
	private static function set_other_name( $args, &$other_args ) {
1149
		//Set up name for other field
1150
		$other_args['name'] = str_replace( '[]', '', $args['field_name'] );
1151
		$other_args['name'] = preg_replace( '/\[' . $args['field']['id'] . '\]$/', '', $other_args['name'] );
1152
		$other_args['name'] = $other_args['name'] . '[other][' . $args['field']['id'] . ']';
1153
1154
		//Converts item_meta[field_id] => item_meta[other][field_id] and
1155
		//item_meta[parent][0][field_id] => item_meta[parent][0][other][field_id]
1156
		if ( FrmField::is_field_with_multiple_values( $args['field'] ) ) {
1157
			$other_args['name'] .= '[' . $args['opt_key'] . ']';
1158
		}
1159
	}
1160
1161
	/**
1162
	 * Find the parent and pointer, and get text for "other" text field
1163
	 *
1164
	 * @param array $args
1165
	 * @param array $other_args
1166
	 *
1167
	 * @since 2.0.6
1168
	 */
1169
	private static function set_other_value( $args, &$other_args ) {
1170
		$parent  = '';
1171
		$pointer = '';
1172
1173
		// Check for parent ID and pointer
1174
		$temp_array = explode( '[', $args['field_name'] );
1175
1176
		// Count should only be greater than 3 if inside of a repeating section
1177
		if ( count( $temp_array ) > 3 ) {
1178
			$parent  = str_replace( ']', '', $temp_array[1] );
1179
			$pointer = str_replace( ']', '', $temp_array[2] );
1180
		}
1181
1182
		// Get text for "other" text field
1183
		$other_args['value'] = self::get_other_val(
1184
			array(
1185
				'opt_key' => $args['opt_key'],
1186
				'field'   => $args['field'],
1187
				'parent'  => $parent,
1188
				'pointer' => $pointer,
1189
			)
1190
		);
1191
	}
1192
1193
	/**
1194
	 * If this field includes an other option, show it
1195
	 *
1196
	 * @param $args array
1197
	 *
1198
	 * @since 2.0.6
1199
	 */
1200
	public static function include_other_input( $args ) {
1201
		if ( ! $args['other_opt'] ) {
1202
			return;
1203
		}
1204
1205
		$classes = array( 'frm_other_input' );
1206
		if ( ! $args['checked'] || trim( $args['checked'] ) == '' ) {
1207
			// hide the field if the other option is not selected
1208
			$classes[] = 'frm_pos_none';
1209
		}
1210
		if ( $args['field']['type'] == 'select' && $args['field']['multiple'] ) {
1211
			$classes[] = 'frm_other_full';
1212
		}
1213
1214
		// Set up HTML ID for Other field
1215
		$other_id = self::get_other_field_html_id( $args['field']['type'], $args['html_id'], $args['opt_key'] );
1216
1217
		$label = isset( $args['opt_label'] ) ? $args['opt_label'] : $args['field']['name'];
1218
1219
		echo '<label for="' . esc_attr( $other_id ) . '" class="frm_screen_reader frm_hidden">' .
1220
			esc_html( $label ) .
1221
			'</label>' .
1222
			'<input type="text" id="' . esc_attr( $other_id ) . '" class="' . esc_attr( implode( ' ', $classes ) ) . '" ' .
1223
			( $args['read_only'] ? ' readonly="readonly" disabled="disabled"' : '' ) .
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
1224
			' name="' . esc_attr( $args['name'] ) . '" value="' . esc_attr( $args['value'] ) . '" />';
1225
	}
1226
1227
	/**
1228
	 * Get the HTML id for an "Other" text field
1229
	 * Note: This does not affect fields in repeating sections
1230
	 *
1231
	 * @since 2.0.08
1232
	 *
1233
	 * @param string $type - field type
1234
	 * @param string $html_id
1235
	 * @param string|boolean $opt_key
1236
	 *
1237
	 * @return string $other_id
1238
	 */
1239
	public static function get_other_field_html_id( $type, $html_id, $opt_key = false ) {
1240
		$other_id = $html_id;
1241
1242
		// If hidden radio field, add an opt key of 0
1243
		if ( $type == 'radio' && $opt_key === false ) {
1244
			$opt_key = 0;
1245
		}
1246
1247
		if ( $opt_key !== false ) {
1248
			$other_id .= '-' . $opt_key;
1249
		}
1250
1251
		$other_id .= '-otext';
1252
1253
		return $other_id;
1254
	}
1255
1256
	public static function switch_field_ids( $val ) {
1257
		global $frm_duplicate_ids;
1258
		$replace      = array();
1259
		$replace_with = array();
1260
		foreach ( (array) $frm_duplicate_ids as $old => $new ) {
1261
			$replace[]      = '[if ' . $old . ']';
1262
			$replace_with[] = '[if ' . $new . ']';
1263
			$replace[]      = '[if ' . $old . ' ';
1264
			$replace_with[] = '[if ' . $new . ' ';
1265
			$replace[]      = '[/if ' . $old . ']';
1266
			$replace_with[] = '[/if ' . $new . ']';
1267
			$replace[]      = '[foreach ' . $old . ']';
1268
			$replace_with[] = '[foreach ' . $new . ']';
1269
			$replace[]      = '[/foreach ' . $old . ']';
1270
			$replace_with[] = '[/foreach ' . $new . ']';
1271
			$replace[]      = '[' . $old . ']';
1272
			$replace_with[] = '[' . $new . ']';
1273
			$replace[]      = '[' . $old . ' ';
1274
			$replace_with[] = '[' . $new . ' ';
1275
			unset( $old, $new );
1276
		}
1277
		if ( is_array( $val ) ) {
1278
			foreach ( $val as $k => $v ) {
1279
				$val[ $k ] = str_replace( $replace, $replace_with, $v );
1280
				unset( $k, $v );
1281
			}
1282
		} else {
1283
			$val = str_replace( $replace, $replace_with, $val );
1284
		}
1285
1286
		return $val;
1287
	}
1288
1289
	/**
1290
	 * @since 4.0
1291
	 */
1292
	public static function bulk_options_overlay() {
1293
		$prepop = array();
1294
		self::get_bulk_prefilled_opts( $prepop );
1295
1296
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/bulk-options-overlay.php' );
1297
	}
1298
1299
	public static function get_us_states() {
1300
		$states = array(
1301
			'AL' => 'Alabama',
1302
			'AK' => 'Alaska',
1303
			'AR' => 'Arkansas',
1304
			'AZ' => 'Arizona',
1305
			'CA' => 'California',
1306
			'CO' => 'Colorado',
1307
			'CT' => 'Connecticut',
1308
			'DE' => 'Delaware',
1309
			'DC' => 'District of Columbia',
1310
			'FL' => 'Florida',
1311
			'GA' => 'Georgia',
1312
			'HI' => 'Hawaii',
1313
			'ID' => 'Idaho',
1314
			'IL' => 'Illinois',
1315
			'IN' => 'Indiana',
1316
			'IA' => 'Iowa',
1317
			'KS' => 'Kansas',
1318
			'KY' => 'Kentucky',
1319
			'LA' => 'Louisiana',
1320
			'ME' => 'Maine',
1321
			'MD' => 'Maryland',
1322
			'MA' => 'Massachusetts',
1323
			'MI' => 'Michigan',
1324
			'MN' => 'Minnesota',
1325
			'MS' => 'Mississippi',
1326
			'MO' => 'Missouri',
1327
			'MT' => 'Montana',
1328
			'NE' => 'Nebraska',
1329
			'NV' => 'Nevada',
1330
			'NH' => 'New Hampshire',
1331
			'NJ' => 'New Jersey',
1332
			'NM' => 'New Mexico',
1333
			'NY' => 'New York',
1334
			'NC' => 'North Carolina',
1335
			'ND' => 'North Dakota',
1336
			'OH' => 'Ohio',
1337
			'OK' => 'Oklahoma',
1338
			'OR' => 'Oregon',
1339
			'PA' => 'Pennsylvania',
1340
			'RI' => 'Rhode Island',
1341
			'SC' => 'South Carolina',
1342
			'SD' => 'South Dakota',
1343
			'TN' => 'Tennessee',
1344
			'TX' => 'Texas',
1345
			'UT' => 'Utah',
1346
			'VT' => 'Vermont',
1347
			'VA' => 'Virginia',
1348
			'WA' => 'Washington',
1349
			'WV' => 'West Virginia',
1350
			'WI' => 'Wisconsin',
1351
			'WY' => 'Wyoming',
1352
		);
1353
1354
		return apply_filters( 'frm_us_states', $states );
1355
	}
1356
1357
	public static function get_countries() {
1358
		$countries = array(
1359
			__( 'Afghanistan', 'formidable' ),
1360
			__( 'Albania', 'formidable' ),
1361
			__( 'Algeria', 'formidable' ),
1362
			__( 'American Samoa', 'formidable' ),
1363
			__( 'Andorra', 'formidable' ),
1364
			__( 'Angola', 'formidable' ),
1365
			__( 'Anguilla', 'formidable' ),
1366
			__( 'Antarctica', 'formidable' ),
1367
			__( 'Antigua and Barbuda', 'formidable' ),
1368
			__( 'Argentina', 'formidable' ),
1369
			__( 'Armenia', 'formidable' ),
1370
			__( 'Aruba', 'formidable' ),
1371
			__( 'Australia', 'formidable' ),
1372
			__( 'Austria', 'formidable' ),
1373
			__( 'Azerbaijan', 'formidable' ),
1374
			__( 'Bahamas', 'formidable' ),
1375
			__( 'Bahrain', 'formidable' ),
1376
			__( 'Bangladesh', 'formidable' ),
1377
			__( 'Barbados', 'formidable' ),
1378
			__( 'Belarus', 'formidable' ),
1379
			__( 'Belgium', 'formidable' ),
1380
			__( 'Belize', 'formidable' ),
1381
			__( 'Benin', 'formidable' ),
1382
			__( 'Bermuda', 'formidable' ),
1383
			__( 'Bhutan', 'formidable' ),
1384
			__( 'Bolivia', 'formidable' ),
1385
			__( 'Bosnia and Herzegovina', 'formidable' ),
1386
			__( 'Botswana', 'formidable' ),
1387
			__( 'Brazil', 'formidable' ),
1388
			__( 'Brunei', 'formidable' ),
1389
			__( 'Bulgaria', 'formidable' ),
1390
			__( 'Burkina Faso', 'formidable' ),
1391
			__( 'Burundi', 'formidable' ),
1392
			__( 'Cambodia', 'formidable' ),
1393
			__( 'Cameroon', 'formidable' ),
1394
			__( 'Canada', 'formidable' ),
1395
			__( 'Cape Verde', 'formidable' ),
1396
			__( 'Cayman Islands', 'formidable' ),
1397
			__( 'Central African Republic', 'formidable' ),
1398
			__( 'Chad', 'formidable' ),
1399
			__( 'Chile', 'formidable' ),
1400
			__( 'China', 'formidable' ),
1401
			__( 'Colombia', 'formidable' ),
1402
			__( 'Comoros', 'formidable' ),
1403
			__( 'Congo', 'formidable' ),
1404
			__( 'Costa Rica', 'formidable' ),
1405
			__( 'C&ocirc;te d\'Ivoire', 'formidable' ),
1406
			__( 'Croatia', 'formidable' ),
1407
			__( 'Cuba', 'formidable' ),
1408
			__( 'Cyprus', 'formidable' ),
1409
			__( 'Czech Republic', 'formidable' ),
1410
			__( 'Denmark', 'formidable' ),
1411
			__( 'Djibouti', 'formidable' ),
1412
			__( 'Dominica', 'formidable' ),
1413
			__( 'Dominican Republic', 'formidable' ),
1414
			__( 'East Timor', 'formidable' ),
1415
			__( 'Ecuador', 'formidable' ),
1416
			__( 'Egypt', 'formidable' ),
1417
			__( 'El Salvador', 'formidable' ),
1418
			__( 'Equatorial Guinea', 'formidable' ),
1419
			__( 'Eritrea', 'formidable' ),
1420
			__( 'Estonia', 'formidable' ),
1421
			__( 'Ethiopia', 'formidable' ),
1422
			__( 'Fiji', 'formidable' ),
1423
			__( 'Finland', 'formidable' ),
1424
			__( 'France', 'formidable' ),
1425
			__( 'French Guiana', 'formidable' ),
1426
			__( 'French Polynesia', 'formidable' ),
1427
			__( 'Gabon', 'formidable' ),
1428
			__( 'Gambia', 'formidable' ),
1429
			__( 'Georgia', 'formidable' ),
1430
			__( 'Germany', 'formidable' ),
1431
			__( 'Ghana', 'formidable' ),
1432
			__( 'Gibraltar', 'formidable' ),
1433
			__( 'Greece', 'formidable' ),
1434
			__( 'Greenland', 'formidable' ),
1435
			__( 'Grenada', 'formidable' ),
1436
			__( 'Guam', 'formidable' ),
1437
			__( 'Guatemala', 'formidable' ),
1438
			__( 'Guinea', 'formidable' ),
1439
			__( 'Guinea-Bissau', 'formidable' ),
1440
			__( 'Guyana', 'formidable' ),
1441
			__( 'Haiti', 'formidable' ),
1442
			__( 'Honduras', 'formidable' ),
1443
			__( 'Hong Kong', 'formidable' ),
1444
			__( 'Hungary', 'formidable' ),
1445
			__( 'Iceland', 'formidable' ),
1446
			__( 'India', 'formidable' ),
1447
			__( 'Indonesia', 'formidable' ),
1448
			__( 'Iran', 'formidable' ),
1449
			__( 'Iraq', 'formidable' ),
1450
			__( 'Ireland', 'formidable' ),
1451
			__( 'Israel', 'formidable' ),
1452
			__( 'Italy', 'formidable' ),
1453
			__( 'Jamaica', 'formidable' ),
1454
			__( 'Japan', 'formidable' ),
1455
			__( 'Jordan', 'formidable' ),
1456
			__( 'Kazakhstan', 'formidable' ),
1457
			__( 'Kenya', 'formidable' ),
1458
			__( 'Kiribati', 'formidable' ),
1459
			__( 'North Korea', 'formidable' ),
1460
			__( 'South Korea', 'formidable' ),
1461
			__( 'Kuwait', 'formidable' ),
1462
			__( 'Kyrgyzstan', 'formidable' ),
1463
			__( 'Laos', 'formidable' ),
1464
			__( 'Latvia', 'formidable' ),
1465
			__( 'Lebanon', 'formidable' ),
1466
			__( 'Lesotho', 'formidable' ),
1467
			__( 'Liberia', 'formidable' ),
1468
			__( 'Libya', 'formidable' ),
1469
			__( 'Liechtenstein', 'formidable' ),
1470
			__( 'Lithuania', 'formidable' ),
1471
			__( 'Luxembourg', 'formidable' ),
1472
			__( 'Macedonia', 'formidable' ),
1473
			__( 'Madagascar', 'formidable' ),
1474
			__( 'Malawi', 'formidable' ),
1475
			__( 'Malaysia', 'formidable' ),
1476
			__( 'Maldives', 'formidable' ),
1477
			__( 'Mali', 'formidable' ),
1478
			__( 'Malta', 'formidable' ),
1479
			__( 'Marshall Islands', 'formidable' ),
1480
			__( 'Mauritania', 'formidable' ),
1481
			__( 'Mauritius', 'formidable' ),
1482
			__( 'Mexico', 'formidable' ),
1483
			__( 'Micronesia', 'formidable' ),
1484
			__( 'Moldova', 'formidable' ),
1485
			__( 'Monaco', 'formidable' ),
1486
			__( 'Mongolia', 'formidable' ),
1487
			__( 'Montenegro', 'formidable' ),
1488
			__( 'Montserrat', 'formidable' ),
1489
			__( 'Morocco', 'formidable' ),
1490
			__( 'Mozambique', 'formidable' ),
1491
			__( 'Myanmar', 'formidable' ),
1492
			__( 'Namibia', 'formidable' ),
1493
			__( 'Nauru', 'formidable' ),
1494
			__( 'Nepal', 'formidable' ),
1495
			__( 'Netherlands', 'formidable' ),
1496
			__( 'New Zealand', 'formidable' ),
1497
			__( 'Nicaragua', 'formidable' ),
1498
			__( 'Niger', 'formidable' ),
1499
			__( 'Nigeria', 'formidable' ),
1500
			__( 'Norway', 'formidable' ),
1501
			__( 'Northern Mariana Islands', 'formidable' ),
1502
			__( 'Oman', 'formidable' ),
1503
			__( 'Pakistan', 'formidable' ),
1504
			__( 'Palau', 'formidable' ),
1505
			__( 'Palestine', 'formidable' ),
1506
			__( 'Panama', 'formidable' ),
1507
			__( 'Papua New Guinea', 'formidable' ),
1508
			__( 'Paraguay', 'formidable' ),
1509
			__( 'Peru', 'formidable' ),
1510
			__( 'Philippines', 'formidable' ),
1511
			__( 'Poland', 'formidable' ),
1512
			__( 'Portugal', 'formidable' ),
1513
			__( 'Puerto Rico', 'formidable' ),
1514
			__( 'Qatar', 'formidable' ),
1515
			__( 'Romania', 'formidable' ),
1516
			__( 'Russia', 'formidable' ),
1517
			__( 'Rwanda', 'formidable' ),
1518
			__( 'Saint Kitts and Nevis', 'formidable' ),
1519
			__( 'Saint Lucia', 'formidable' ),
1520
			__( 'Saint Vincent and the Grenadines', 'formidable' ),
1521
			__( 'Samoa', 'formidable' ),
1522
			__( 'San Marino', 'formidable' ),
1523
			__( 'Sao Tome and Principe', 'formidable' ),
1524
			__( 'Saudi Arabia', 'formidable' ),
1525
			__( 'Senegal', 'formidable' ),
1526
			__( 'Serbia and Montenegro', 'formidable' ),
1527
			__( 'Seychelles', 'formidable' ),
1528
			__( 'Sierra Leone', 'formidable' ),
1529
			__( 'Singapore', 'formidable' ),
1530
			__( 'Slovakia', 'formidable' ),
1531
			__( 'Slovenia', 'formidable' ),
1532
			__( 'Solomon Islands', 'formidable' ),
1533
			__( 'Somalia', 'formidable' ),
1534
			__( 'South Africa', 'formidable' ),
1535
			__( 'South Sudan', 'formidable' ),
1536
			__( 'Spain', 'formidable' ),
1537
			__( 'Sri Lanka', 'formidable' ),
1538
			__( 'Sudan', 'formidable' ),
1539
			__( 'Suriname', 'formidable' ),
1540
			__( 'Swaziland', 'formidable' ),
1541
			__( 'Sweden', 'formidable' ),
1542
			__( 'Switzerland', 'formidable' ),
1543
			__( 'Syria', 'formidable' ),
1544
			__( 'Taiwan', 'formidable' ),
1545
			__( 'Tajikistan', 'formidable' ),
1546
			__( 'Tanzania', 'formidable' ),
1547
			__( 'Thailand', 'formidable' ),
1548
			__( 'Togo', 'formidable' ),
1549
			__( 'Tonga', 'formidable' ),
1550
			__( 'Trinidad and Tobago', 'formidable' ),
1551
			__( 'Tunisia', 'formidable' ),
1552
			__( 'Turkey', 'formidable' ),
1553
			__( 'Turkmenistan', 'formidable' ),
1554
			__( 'Tuvalu', 'formidable' ),
1555
			__( 'Uganda', 'formidable' ),
1556
			__( 'Ukraine', 'formidable' ),
1557
			__( 'United Arab Emirates', 'formidable' ),
1558
			__( 'United Kingdom', 'formidable' ),
1559
			__( 'United States', 'formidable' ),
1560
			__( 'Uruguay', 'formidable' ),
1561
			__( 'Uzbekistan', 'formidable' ),
1562
			__( 'Vanuatu', 'formidable' ),
1563
			__( 'Vatican City', 'formidable' ),
1564
			__( 'Venezuela', 'formidable' ),
1565
			__( 'Vietnam', 'formidable' ),
1566
			__( 'Virgin Islands, British', 'formidable' ),
1567
			__( 'Virgin Islands, U.S.', 'formidable' ),
1568
			__( 'Yemen', 'formidable' ),
1569
			__( 'Zambia', 'formidable' ),
1570
			__( 'Zimbabwe', 'formidable' ),
1571
		);
1572
1573
		return apply_filters( 'frm_countries', $countries );
1574
	}
1575
1576
	public static function get_bulk_prefilled_opts( array &$prepop ) {
1577
		$prepop[ __( 'Countries', 'formidable' ) ] = self::get_countries();
1578
1579
		$states    = self::get_us_states();
1580
		$state_abv = array_keys( $states );
1581
		sort( $state_abv );
1582
		$prepop[ __( 'U.S. State Abbreviations', 'formidable' ) ] = $state_abv;
1583
1584
		$states = array_values( $states );
1585
		sort( $states );
1586
		$prepop[ __( 'U.S. States', 'formidable' ) ] = $states;
1587
		unset( $state_abv, $states );
1588
1589
		$prepop[ __( 'Age', 'formidable' ) ] = array(
1590
			__( 'Under 18', 'formidable' ),
1591
			__( '18-24', 'formidable' ),
1592
			__( '25-34', 'formidable' ),
1593
			__( '35-44', 'formidable' ),
1594
			__( '45-54', 'formidable' ),
1595
			__( '55-64', 'formidable' ),
1596
			__( '65 or Above', 'formidable' ),
1597
			__( 'Prefer Not to Answer', 'formidable' ),
1598
		);
1599
1600
		$prepop[ __( 'Satisfaction', 'formidable' ) ] = array(
1601
			__( 'Very Satisfied', 'formidable' ),
1602
			__( 'Satisfied', 'formidable' ),
1603
			__( 'Neutral', 'formidable' ),
1604
			__( 'Unsatisfied', 'formidable' ),
1605
			__( 'Very Unsatisfied', 'formidable' ),
1606
			__( 'N/A', 'formidable' ),
1607
		);
1608
1609
		$prepop[ __( 'Importance', 'formidable' ) ] = array(
1610
			__( 'Very Important', 'formidable' ),
1611
			__( 'Important', 'formidable' ),
1612
			__( 'Neutral', 'formidable' ),
1613
			__( 'Somewhat Important', 'formidable' ),
1614
			__( 'Not at all Important', 'formidable' ),
1615
			__( 'N/A', 'formidable' ),
1616
		);
1617
1618
		$prepop[ __( 'Agreement', 'formidable' ) ] = array(
1619
			__( 'Strongly Agree', 'formidable' ),
1620
			__( 'Agree', 'formidable' ),
1621
			__( 'Neutral', 'formidable' ),
1622
			__( 'Disagree', 'formidable' ),
1623
			__( 'Strongly Disagree', 'formidable' ),
1624
			__( 'N/A', 'formidable' ),
1625
		);
1626
1627
		$prepop = apply_filters( 'frm_bulk_field_choices', $prepop );
1628
	}
1629
1630
	/**
1631
	 * Display a field value selector
1632
	 *
1633
	 * @since 2.03.05
1634
	 *
1635
	 * @param int $selector_field_id
1636
	 * @param array $selector_args
1637
	 */
1638
	public static function display_field_value_selector( $selector_field_id, $selector_args ) {
1639
		$field_value_selector = FrmFieldFactory::create_field_value_selector( $selector_field_id, $selector_args );
1640
		$field_value_selector->display();
1641
	}
1642
1643
	/**
1644
	 * Convert a field object to a flat array
1645
	 *
1646
	 * @since 2.03.05
1647
	 *
1648
	 * @param object $field
1649
	 *
1650
	 * @return array
1651
	 */
1652
	public static function convert_field_object_to_flat_array( $field ) {
1653
		$field_options = $field->field_options;
1654
		$field_array   = get_object_vars( $field );
1655
		unset( $field_array['field_options'] );
1656
1657
		return $field_array + $field_options;
1658
	}
1659
1660
	/**
1661
	 * @deprecated 4.0
1662
	 */
1663
	public static function show_icon_link_js( $atts ) {
1664
		_deprecated_function( __METHOD__, '4.0' );
1665
		$atts['icon'] .= $atts['is_selected'] ? ' ' : ' frm_inactive_icon ';
1666
		if ( isset( $atts['has_default'] ) && ! $atts['has_default'] ) {
1667
			$atts['icon'] .= 'frm_hidden ';
1668
		}
1669
		echo '<a href="javascript:void(0)" class="frm_bstooltip ' . esc_attr( $atts['icon'] ) . 'frm_default_val_icons frm_action_icon frm_icon_font" title="' . esc_attr( $atts['message'] ) . '"></a>';
1670
	}
1671
1672
	/**
1673
	 * @deprecated 4.0
1674
	 */
1675
	public static function show_default_blank_js( $is_selected, $has_default_value = true ) {
1676
		_deprecated_function( __METHOD__, '4.0' );
1677
	}
1678
1679
	/**
1680
	 * @deprecated 4.0
1681
	 */
1682
	public static function clear_on_focus_html( $field, $display, $id = '' ) {
1683
		_deprecated_function( __METHOD__, '4.0' );
1684
	}
1685
1686
	/**
1687
	 * @deprecated 4.0
1688
	 */
1689
	public static function show_onfocus_js( $is_selected, $has_default_value = true ) {
1690
		_deprecated_function( __METHOD__, '4.0' );
1691
	}
1692
1693
	/**
1694
	 * @deprecated 3.0
1695
	 * @codeCoverageIgnore
1696
	 */
1697
	public static function display_recaptcha() {
1698
		_deprecated_function( __FUNCTION__, '3.0', 'FrmFieldCaptcha::field_input' );
1699
	}
1700
1701
	/**
1702
	 * @deprecated 3.0
1703
	 * @codeCoverageIgnore
1704
	 */
1705
	public static function remove_inline_conditions( $no_vars, $code, $replace_with, &$html ) {
1706
		FrmDeprecated::remove_inline_conditions( $no_vars, $code, $replace_with, $html );
1707
	}
1708
1709
	/**
1710
	 * @deprecated 3.0
1711
	 * @codeCoverageIgnore
1712
	 */
1713
	public static function get_shortcode_tag( $shortcodes, $short_key, $args ) {
1714
		return FrmDeprecated::get_shortcode_tag( $shortcodes, $short_key, $args );
1715
	}
1716
1717
	/**
1718
	 * @deprecated 3.0
1719
	 * @codeCoverageIgnore
1720
	 *
1721
	 * @param string $html
1722
	 * @param array $field
1723
	 * @param array $errors
1724
	 * @param object $form
1725
	 * @param array $args
1726
	 *
1727
	 * @return string
1728
	 */
1729
	public static function replace_shortcodes( $html, $field, $errors = array(), $form = false, $args = array() ) {
1730
		return FrmDeprecated::replace_shortcodes( $html, $field, $errors, $form, $args );
1731
	}
1732
1733
	/**
1734
	 * @deprecated 3.0
1735
	 * @codeCoverageIgnore
1736
	 */
1737
	public static function get_default_field_opts( $type, $field = null, $limit = false ) {
1738
		return FrmDeprecated::get_default_field_opts( $type, $field, $limit );
1739
	}
1740
1741
	/**
1742
	 * @deprecated 2.02.07
1743
	 * @codeCoverageIgnore
1744
	 */
1745
	public static function dropdown_categories( $args ) {
1746
		return FrmDeprecated::dropdown_categories( $args );
1747
	}
1748
}
1749