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

FrmFieldsController::load_field()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 16
nop 0
dl 0
loc 42
rs 8.3146
c 0
b 0
f 0
1
<?php
2
3
class FrmFieldsController {
4
5
	public static function load_field() {
6
		FrmAppHelper::permission_check( 'frm_edit_forms' );
7
		check_ajax_referer( 'frm_ajax', 'nonce' );
8
9
		$fields = isset( $_POST['field'] ) ? wp_unslash( $_POST['field'] ) : array();
10
		if ( empty( $fields ) ) {
11
			wp_die();
12
		}
13
14
		$_GET['page'] = 'formidable';
15
16
		$values     = array(
17
			'id'         => FrmAppHelper::get_post_param( 'form_id', '', 'absint' ),
18
			'doing_ajax' => true,
19
		);
20
		$field_html = array();
21
22
		foreach ( $fields as $field ) {
23
			$field = htmlspecialchars_decode( nl2br( $field ) );
24
			$field = json_decode( $field );
25
			if ( ! isset( $field->id ) || ! is_numeric( $field->id ) ) {
26
				// this field may have already been loaded
27
				continue;
28
			}
29
30
			if ( ! isset( $field->value ) ) {
31
				$field->value = '';
32
			}
33
			$field->field_options = json_decode( json_encode( $field->field_options ), true );
34
			$field->options       = json_decode( json_encode( $field->options ), true );
35
			$field->default_value = json_decode( json_encode( $field->default_value ), true );
36
37
			ob_start();
38
			self::load_single_field( $field, $values );
39
			$field_html[ absint( $field->id ) ] = ob_get_contents();
40
			ob_end_clean();
41
		}
42
43
		echo json_encode( $field_html );
44
45
		wp_die();
46
	}
47
48
	/**
49
	 * Create a new field with ajax
50
	 */
51
	public static function create() {
52
		FrmAppHelper::permission_check( 'frm_edit_forms' );
53
		check_ajax_referer( 'frm_ajax', 'nonce' );
54
55
		$field_type = FrmAppHelper::get_post_param( 'field_type', '', 'sanitize_text_field' );
56
		$form_id    = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
57
58
		$field = self::include_new_field( $field_type, $form_id );
0 ignored issues
show
Bug introduced by
It seems like $field_type defined by \FrmAppHelper::get_post_... 'sanitize_text_field') on line 55 can also be of type array; however, FrmFieldsController::include_new_field() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
59
60
		// this hook will allow for multiple fields to be added at once
61
		do_action( 'frm_after_field_created', $field, $form_id );
62
63
		wp_die();
64
	}
65
66
	/**
67
	 * Set up and create a new field
68
	 *
69
	 * @param string $field_type
70
	 * @param integer $form_id
71
	 *
72
	 * @return array|bool
73
	 */
74
	public static function include_new_field( $field_type, $form_id ) {
75
		$field_values = FrmFieldsHelper::setup_new_vars( $field_type, $form_id );
76
		$field_values = apply_filters( 'frm_before_field_created', $field_values );
77
78
		$field_id = FrmField::create( $field_values );
79
80
		if ( ! $field_id ) {
81
			return false;
82
		}
83
84
		$field = self::get_field_array_from_id( $field_id );
85
86
		$values = array();
87
		if ( FrmAppHelper::pro_is_installed() ) {
88
			$values['post_type'] = FrmProFormsHelper::post_type( $form_id );
89
90
			$parent_form_id = FrmDb::get_var( 'frm_forms', array( 'id' => $form_id ), 'parent_form_id' );
91
			if ( $parent_form_id ) {
92
				$field['parent_form_id'] = $parent_form_id;
93
			}
94
		}
95
96
		self::load_single_field( $field, $values, $form_id );
97
98
		return $field;
99
	}
100
101
	public static function duplicate() {
102
		FrmAppHelper::permission_check( 'frm_edit_forms' );
103
		check_ajax_referer( 'frm_ajax', 'nonce' );
104
105
		$field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
106
		$form_id  = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
107
108
		$copy_field = FrmField::getOne( $field_id );
0 ignored issues
show
Bug introduced by
It seems like $field_id defined by \FrmAppHelper::get_post_...field_id', 0, 'absint') on line 105 can also be of type array; however, FrmField::getOne() does only seem to accept string|integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
109
		if ( ! $copy_field ) {
110
			wp_die();
111
		}
112
113
		do_action( 'frm_duplicate_field', $copy_field, $form_id );
114
		do_action( 'frm_duplicate_field_' . $copy_field->type, $copy_field, $form_id );
115
116
		$values = array(
117
			'id' => $copy_field->id,
118
		);
119
		FrmFieldsHelper::fill_field( $values, $copy_field, $copy_field->form_id );
120
		$values = apply_filters( 'frm_prepare_single_field_for_duplication', $values );
121
122
		$field_id = FrmField::create( $values );
123
		if ( $field_id ) {
124
			self::load_single_field( $field_id, $values );
125
		}
126
127
		wp_die();
128
	}
129
130
	/**
131
	 * @since 3.0
132
	 *
133
	 * @param int $field_id
134
	 *
135
	 * @return array
136
	 */
137
	public static function get_field_array_from_id( $field_id ) {
138
		$field = FrmField::getOne( $field_id );
139
140
		return FrmFieldsHelper::setup_edit_vars( $field );
141
	}
142
143
	/**
144
	 * @since 3.0
145
	 *
146
	 * @param int|array|object $field_object
147
	 * @param array $values
148
	 * @param int $form_id
149
	 */
150
	public static function load_single_field( $field_object, $values, $form_id = 0 ) {
151
		global $frm_vars;
152
		$frm_vars['is_admin'] = true;
153
154
		if ( is_numeric( $field_object ) ) {
155
			$field_object = FrmField::getOne( $field_object );
156
		} elseif ( is_array( $field_object ) ) {
157
			$field        = $field_object;
158
			$field_object = FrmField::getOne( $field['id'] );
159
		}
160
161
		$field_obj = FrmFieldFactory::get_field_factory( $field_object );
162
		$display   = self::display_field_options( array(), $field_obj );
163
164
		$ajax_loading    = isset( $values['ajax_load'] ) && $values['ajax_load'];
165
		$ajax_this_field = isset( $values['count'] ) && $values['count'] > 10 && ! in_array( $field_object->type, array( 'divider', 'end_divider' ) );
166
167
		if ( $ajax_loading && $ajax_this_field ) {
168
			$li_classes = self::get_classes_for_builder_field( array(), $display, $field_obj );
169
			include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/ajax-field-placeholder.php' );
170
		} else {
171
			if ( ! isset( $field ) && is_object( $field_object ) ) {
172
				$field_object->parent_form_id = isset( $values['id'] ) ? $values['id'] : $field_object->form_id;
173
174
				$field = FrmFieldsHelper::setup_edit_vars( $field_object );
175
			}
176
177
			$li_classes = self::get_classes_for_builder_field( $field, $display, $field_obj );
178
			$li_classes .= ' ui-state-default widgets-holder-wrap';
179
180
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/add_field.php' );
181
		}
182
	}
183
184
	/**
185
	 * @since 3.0
186
	 */
187
	private static function get_classes_for_builder_field( $field, $display, $field_info ) {
188
		$li_classes = $field_info->form_builder_classes( $display['type'] );
189
		$classes    = isset( $field['classes'] ) ? $field['classes'] : '';
190
191
		// Exclude alignright for now since we aren't using widths.
192
		$classes    = str_replace( ' frm_alignright ', ' ', $classes );
193
194
		$li_classes .= ' frm_form_field frmstart ' . $classes . ' frmend';
195
		if ( ! empty( $field ) ) {
196
			$li_classes = apply_filters( 'frm_build_field_class', $li_classes, $field );
197
		}
198
199
		return $li_classes;
200
	}
201
202
	public static function destroy() {
203
		FrmAppHelper::permission_check( 'frm_edit_forms' );
204
		check_ajax_referer( 'frm_ajax', 'nonce' );
205
206
		$field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
207
		FrmField::destroy( $field_id );
208
		wp_die();
209
	}
210
211
	/* Field Options */
212
213
	public static function import_options() {
214
		FrmAppHelper::permission_check( 'frm_edit_forms' );
215
		check_ajax_referer( 'frm_ajax', 'nonce' );
216
217
		if ( ! is_admin() || ! current_user_can( 'frm_edit_forms' ) ) {
218
			return;
219
		}
220
221
		$field_id = FrmAppHelper::get_param( 'field_id', '', 'post', 'absint' );
222
		$field    = FrmField::getOne( $field_id );
223
224
		if ( ! in_array( $field->type, array( 'radio', 'checkbox', 'select' ) ) ) {
225
			return;
226
		}
227
228
		$field = FrmFieldsHelper::setup_edit_vars( $field );
229
		$opts  = FrmAppHelper::get_param( 'opts', '', 'post', 'wp_kses_post' );
230
		$opts  = explode( "\n", rtrim( $opts, "\n" ) );
231
		$opts  = array_map( 'trim', $opts );
232
233
		$separate = FrmAppHelper::get_param( 'separate', '', 'post', 'sanitize_text_field' );
234
		$field['separate_value'] = ( $separate === 'true' );
235
236
		if ( $field['separate_value'] ) {
237
			foreach ( $opts as $opt_key => $opt ) {
238
				if ( strpos( $opt, '|' ) !== false ) {
239
					$vals = explode( '|', $opt );
240
					$opts[ $opt_key ] = array(
241
						'label' => trim( $vals[0] ),
242
						'value' => trim( $vals[1] ),
243
					);
244
					unset( $vals );
245
				}
246
				unset( $opt_key, $opt );
247
			}
248
		}
249
250
		// Keep other options after bulk update.
251
		if ( isset( $field['field_options']['other'] ) && $field['field_options']['other'] == true ) {
252
			$other_array = array();
253
			foreach ( $field['options'] as $opt_key => $opt ) {
254
				if ( FrmFieldsHelper::is_other_opt( $opt_key ) ) {
255
					$other_array[ $opt_key ] = $opt;
256
				}
257
				unset( $opt_key, $opt );
258
			}
259
			if ( ! empty( $other_array ) ) {
260
				$opts = array_merge( $opts, $other_array );
261
			}
262
		}
263
264
		$field['options'] = $opts;
265
266
		FrmFieldsHelper::show_single_option( $field );
267
268
		wp_die();
269
	}
270
271
	/**
272
	 * @since 4.0
273
	 *
274
	 * @param array $atts - Includes field array, field_obj, display array, values array.
275
	 */
276
	public static function load_single_field_settings( $atts ) {
277
		$field     = $atts['field'];
278
		$field_obj = $atts['field_obj'];
279
		$values    = $atts['values'];
280
		$display   = $atts['display'];
281
		unset( $atts );
282
283
		if ( ! isset( $field['unique'] ) ) {
284
			$field['unique'] = false;
285
		}
286
287
		if ( ! isset( $field['read_only'] ) ) {
288
			$field['read_only'] = false;
289
		}
290
291
		$field_types         = FrmFieldsHelper::get_field_types( $field['type'] );
292
		$pro_field_selection = FrmField::pro_field_selection();
293
		$all_field_types     = array_merge( $pro_field_selection, FrmField::field_selection() );
294
		$disabled_fields     = FrmAppHelper::pro_is_installed() ? array() : $pro_field_selection;
295
		$frm_settings        = FrmAppHelper::get_settings();
296
297
		if ( ! isset( $all_field_types[ $field['type'] ] ) ) {
298
			// Add fallback for an add-on field type that has been deactivated.
299
			$all_field_types[ $field['type'] ] = array(
300
				'name' => ucfirst( $field['type'] ),
301
				'icon' => 'frm_icon_font frm_pencil_icon',
302
			);
303
		} elseif ( ! is_array( $all_field_types[ $field['type'] ] ) ) {
304
			// Fallback for fields added in a more basic way.
305
			FrmFormsHelper::prepare_field_type( $all_field_types[ $field['type'] ] );
306
		}
307
308
		$type_name = $all_field_types[ $field['type'] ]['name'];
309
		if ( $field['type'] === 'divider' && FrmField::is_option_true( $field, 'repeat' ) ) {
310
			$type_name = $all_field_types['divider|repeat']['name'];
311
		}
312
313
		$display_type = self::displayed_field_type( $field );
314
315
		if ( $display['default'] ) {
316
			$default_value_types = self::default_value_types( $field, compact( 'display' ) );
317
		}
318
319
		if ( $display['clear_on_focus'] && is_array( $field['placeholder'] ) ) {
320
			$field['placeholder'] = implode( $field['placeholder'], ', ' );
321
		}
322
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/settings.php' );
323
	}
324
325
	/**
326
	 * Get the type of field being displayed for lookups and dynamic fields.
327
	 *
328
	 * @since 4.0
329
	 * @return array
330
	 */
331
	private static function displayed_field_type( $field ) {
332
		$display_type = array(
333
			'radio'    => FrmField::is_field_type( $field, 'radio' ),
334
			'checkbox' => FrmField::is_field_type( $field, 'checkbox' ),
335
			'select'   => FrmField::is_field_type( $field, 'select' ),
336
			'lookup'   => FrmField::is_field_type( $field, 'lookup' ),
337
			'data'     => FrmField::is_field_type( $field, 'data' ),
338
		);
339
		return array_filter( $display_type );
340
	}
341
342
	/**
343
	 * Get the list of default value types that can be toggled in the builder.
344
	 *
345
	 * @since 4.0
346
	 * @return array
347
	 */
348
	private static function default_value_types( $field, $atts ) {
349
		$types = array(
350
			'default_value' => array(
351
				'class' => '',
352
				'icon'  => 'frm_icon_font frm_text2_icon',
353
				'title' => __( 'Default Value (Text)', 'formidable' ),
354
				'data'  => array(
355
					'frmshow' => '#default-value-for-',
356
				),
357
			),
358
			'calc' => array(
359
				'class' => 'frm_show_upgrade frm_noallow',
360
				'title' => __( 'Default Value (Calculation)', 'formidable' ),
361
				'icon'  => 'frm_icon_font frm_calculator_icon',
362
				'data'  => array(
363
					'medium'  => 'calculations',
364
					'upgrade' => __( 'Calculator forms', 'formidable' ),
365
				),
366
			),
367
			'get_values_field' => array(
368
				'class' => 'frm_show_upgrade frm_noallow',
369
				'title' => __( 'Default Value (Lookup)', 'formidable' ),
370
				'icon'  => 'frm_icon_font frm_search_icon',
371
				'data'  => array(
372
					'medium'  => 'lookup',
373
					'upgrade' => __( 'Lookup fields', 'formidable' ),
374
				),
375
			),
376
		);
377
378
		$types = apply_filters( 'frm_default_value_types', $types, $atts );
379
380
		if ( FrmAppHelper::pro_is_installed() && ! FrmAppHelper::meets_min_pro_version( '4.0' ) ) {
381
			// Prevent settings from showing in 2 spots.
382
			unset( $types['calc'], $types['get_values_field'] );
383
		}
384
385
		// Set active class.
386
		$settings = array_keys( $types );
387
		$active   = 'default_value';
388
389
		foreach ( $settings as $type ) {
390
			if ( ! empty( $field[ $type ] ) ) {
391
				$active = $type;
392
			}
393
		}
394
395
		$types[ $active ]['class']  .= ' current';
396
		$types[ $active ]['current'] = true;
397
398
		return $types;
399
	}
400
401
	public static function change_type( $type ) {
402
		$type_switch = array(
403
			'scale'   => 'radio',
404
			'star'    => 'radio',
405
			'10radio' => 'radio',
406
			'rte'     => 'textarea',
407
			'website' => 'url',
408
			'image'   => 'url',
409
		);
410
		if ( isset( $type_switch[ $type ] ) ) {
411
			$type = $type_switch[ $type ];
412
		}
413
414
		$pro_fields = FrmField::pro_field_selection();
415
		$types      = array_keys( $pro_fields );
416
		if ( in_array( $type, $types ) ) {
417
			$type = 'text';
418
		}
419
420
		return $type;
421
	}
422
423
	/**
424
	 * @param array $settings
425
	 * @param object $field_info
426
	 *
427
	 * @return array
428
	 */
429
	public static function display_field_options( $settings, $field_info = null ) {
430
		if ( $field_info ) {
431
			$settings               = $field_info->display_field_settings();
432
			$settings['field_data'] = $field_info->field;
433
		}
434
435
		return apply_filters( 'frm_display_field_options', $settings );
436
	}
437
438
	/**
439
	 * Display the format option
440
	 *
441
	 * @since 3.0
442
	 *
443
	 * @param array $field
444
	 */
445
	public static function show_format_option( $field ) {
446
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/value-format.php' );
447
	}
448
449
	public static function input_html( $field, $echo = true ) {
450
		$class = array();
451
		self::add_input_classes( $field, $class );
452
453
		$add_html = array();
454
		self::add_html_size( $field, $add_html );
455
		self::add_html_length( $field, $add_html );
456
		self::add_html_placeholder( $field, $add_html, $class );
457
		self::add_validation_messages( $field, $add_html );
458
459
		$class = apply_filters( 'frm_field_classes', implode( ' ', $class ), $field );
460
461
		FrmFormsHelper::add_html_attr( $class, 'class', $add_html );
462
463
		self::add_shortcodes_to_html( $field, $add_html );
464
		self::add_pattern_attribute( $field, $add_html );
465
466
		$add_html = apply_filters( 'frm_field_extra_html', $add_html, $field );
467
		$add_html = ' ' . implode( ' ', $add_html ) . '  ';
468
469
		if ( $echo ) {
470
			echo $add_html; // WPCS: XSS ok.
471
		}
472
473
		return $add_html;
474
	}
475
476
	private static function add_input_classes( $field, array &$class ) {
477
		if ( isset( $field['input_class'] ) && ! empty( $field['input_class'] ) ) {
478
			$class[] = $field['input_class'];
479
		}
480
481
		if ( $field['type'] == 'hidden' || $field['type'] == 'user_id' ) {
482
			return;
483
		}
484
485
		if ( isset( $field['size'] ) && $field['size'] > 0 ) {
486
			$class[] = 'auto_width';
487
		}
488
	}
489
490
	private static function add_html_size( $field, array &$add_html ) {
491
		$size_fields = array(
492
			'select',
493
			'data',
494
			'time',
495
			'hidden',
496
			'file',
497
			'lookup',
498
		);
499
500
		if ( ! isset( $field['size'] ) || $field['size'] <= 0 || in_array( $field['type'], $size_fields ) ) {
501
			return;
502
		}
503
504
		if ( FrmAppHelper::is_admin_page( 'formidable' ) ) {
505
			return;
506
		}
507
508
		if ( is_numeric( $field['size'] ) ) {
509
			$field['size'] .= 'px';
510
		}
511
512
		$important = apply_filters( 'frm_use_important_width', 1, $field );
513
		// Note: This inline styling must stay since we cannot realistically set a class for every possible field size.
514
		$add_html['style'] = 'style="width:' . esc_attr( $field['size'] ) . ( $important ? ' !important' : '' ) . '"';
515
516
		self::add_html_cols( $field, $add_html );
517
	}
518
519
	private static function add_html_cols( $field, array &$add_html ) {
520
		if ( ! in_array( $field['type'], array( 'textarea', 'rte' ) ) ) {
521
			return;
522
		}
523
524
		// Convert to cols for textareas.
525
		$calc = array(
526
			''    => 9,
527
			'px'  => 9,
528
			'rem' => 0.444,
529
			'em'  => 0.544,
530
		);
531
532
		// include "col" for valid html
533
		$unit = trim( preg_replace( '/[0-9]+/', '', $field['size'] ) );
534
535
		if ( ! isset( $calc[ $unit ] ) ) {
536
			return;
537
		}
538
539
		$size = (float) str_replace( $unit, '', $field['size'] ) / $calc[ $unit ];
540
541
		$add_html['cols'] = 'cols="' . absint( $size ) . '"';
542
	}
543
544
	private static function add_html_length( $field, array &$add_html ) {
545
		// Check for max setting and if this field accepts maxlength.
546
		$fields = array(
547
			'textarea',
548
			'rte',
549
			'hidden',
550
			'file',
551
		);
552
553
		if ( FrmField::is_option_empty( $field, 'max' ) || in_array( $field['type'], $fields ) ) {
554
			return;
555
		}
556
557
		if ( FrmAppHelper::is_admin_page( 'formidable' ) ) {
558
			// Don't load on form builder page.
559
			return;
560
		}
561
562
		$add_html['maxlength'] = 'maxlength="' . esc_attr( $field['max'] ) . '"';
563
	}
564
565
	private static function add_html_placeholder( $field, array &$add_html, array &$class ) {
566
		if ( $field['default_value'] != '' ) {
567
			if ( is_array( $field['default_value'] ) ) {
568
				$add_html['data-frmval'] = 'data-frmval="' . esc_attr( json_encode( $field['default_value'] ) ) . '"';
569
			} else {
570
				self::add_frmval_to_input( $field, $add_html );
571
			}
572
		}
573
574
		$field['placeholder'] = self::prepare_placeholder( $field );
575
		if ( $field['placeholder'] == '' || is_array( $field['placeholder'] ) ) {
576
			// don't include a json placeholder
577
			return;
578
		}
579
580
		$frm_settings = FrmAppHelper::get_settings();
581
582
		if ( $frm_settings->use_html ) {
583
			self::add_placeholder_to_input( $field, $add_html );
584
		} else {
585
			self::add_frmval_to_input( $field, $add_html );
586
587
			$class[] = 'frm_toggle_default';
588
589
			if ( $field['value'] == $field['placeholder'] ) {
590
				$class[] = 'frm_default';
591
			}
592
		}
593
	}
594
595
	private static function prepare_placeholder( $field ) {
596
		$is_placeholder_field = FrmFieldsHelper::is_placeholder_field_type( $field['type'] );
597
		$is_combo_field       = in_array( $field['type'], array( 'address', 'credit_card' ) );
598
599
		$placeholder = $field['placeholder'];
600
		if ( empty( $placeholder ) && $is_placeholder_field && ! $is_combo_field ) {
601
			$placeholder = self::get_default_value_from_name( $field );
602
		}
603
604
		return $placeholder;
605
	}
606
607
	/**
608
	 * If the label position is "inside",
609
	 * get the label to use as the placeholder
610
	 *
611
	 * @since 2.05
612
	 *
613
	 * @param array $field
614
	 *
615
	 * @return string
616
	 */
617
	public static function get_default_value_from_name( $field ) {
618
		$position = FrmField::get_option( $field, 'label' );
619
		if ( $position == 'inside' ) {
620
			$default_value = $field['name'];
621
		} else {
622
			$default_value = '';
623
		}
624
625
		return $default_value;
626
	}
627
628
	/**
629
	 * Use HMTL5 placeholder with js fallback
630
	 *
631
	 * @param array $field
632
	 * @param array $add_html
633
	 */
634
	private static function add_placeholder_to_input( $field, &$add_html ) {
635
		if ( FrmFieldsHelper::is_placeholder_field_type( $field['type'] ) ) {
636
			$add_html['placeholder'] = 'placeholder="' . esc_attr( $field['placeholder'] ) . '"';
637
		}
638
	}
639
640
	private static function add_frmval_to_input( $field, &$add_html ) {
641
		if ( $field['placeholder'] != '' ) {
642
			$add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['placeholder'] ) . '"';
643
644
			if ( 'select' === $field['type'] ) {
645
				$add_html['data-frmplaceholder'] = 'data-frmplaceholder="' . esc_attr( $field['placeholder'] ) . '"';
646
			}
647
		}
648
649
		if ( $field['default_value'] != '' ) {
650
			$add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['default_value'] ) . '"';
651
		}
652
	}
653
654
	private static function add_validation_messages( $field, array &$add_html ) {
655 View Code Duplication
		if ( FrmField::is_required( $field ) ) {
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...
656
			$required_message        = FrmFieldsHelper::get_error_msg( $field, 'blank' );
657
			$add_html['data-reqmsg'] = 'data-reqmsg="' . esc_attr( $required_message ) . '"';
658
			self::maybe_add_html_required( $field, $add_html );
659
		}
660
661 View Code Duplication
		if ( ! FrmField::is_option_empty( $field, 'invalid' ) ) {
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...
662
			$invalid_message         = FrmFieldsHelper::get_error_msg( $field, 'invalid' );
663
			$add_html['data-invmsg'] = 'data-invmsg="' . esc_attr( $invalid_message ) . '"';
664
		}
665
	}
666
667
	/**
668
	 * If 'required' is added to a conditionall hidden field, the form won't
669
	 * submit in many browsers. Check to make sure the javascript to conditionally
670
	 * remove it is present if needed.
671
	 *
672
	 * @since 3.06.01
673
	 * @param array $field
674
	 * @param array $add_html
675
	 */
676
	private static function maybe_add_html_required( $field, array &$add_html ) {
677
		if ( in_array( $field['type'], array( 'radio', 'checkbox', 'file', 'data', 'lookup' ) ) ) {
678
			return;
679
		}
680
681
		$include_html = FrmAppHelper::meets_min_pro_version( '3.06.01' );
682
		if ( $include_html ) {
683
			$add_html['aria-required'] = 'aria-required="true"';
684
		}
685
	}
686
687
	private static function add_shortcodes_to_html( $field, array &$add_html ) {
688
		if ( FrmField::is_option_empty( $field, 'shortcodes' ) ) {
689
			return;
690
		}
691
692
		foreach ( $field['shortcodes'] as $k => $v ) {
693
			if ( 'opt' === $k ) {
694
				continue;
695
			}
696
697
			if ( is_numeric( $k ) && strpos( $v, '=' ) ) {
698
				$add_html[] = $v;
699
			} elseif ( ! empty( $k ) && isset( $add_html[ $k ] ) ) {
700
				$add_html[ $k ] = str_replace( $k . '="', $k . '="' . $v, $add_html[ $k ] );
701
			} else {
702
				$add_html[ $k ] = $k . '="' . esc_attr( $v ) . '"';
703
			}
704
705
			unset( $k, $v );
706
		}
707
	}
708
709
	/**
710
	 * Add pattern attribute
711
	 *
712
	 * @since 3.0
713
	 *
714
	 * @param array $field
715
	 * @param array $add_html
716
	 */
717
	private static function add_pattern_attribute( $field, array &$add_html ) {
718
		$has_format   = FrmField::is_option_true_in_array( $field, 'format' );
719
		$format_field = FrmField::is_field_type( $field, 'text' );
720
721
		if ( $field['type'] == 'phone' || ( $has_format && $format_field ) ) {
722
			$frm_settings = FrmAppHelper::get_settings();
723
724
			if ( $frm_settings->use_html ) {
725
				$format = FrmEntryValidate::phone_format( $field );
726
				$format = substr( $format, 2, - 1 );
727
728
				$add_html['pattern'] = 'pattern="' . esc_attr( $format ) . '"';
729
			}
730
		}
731
	}
732
733
	public static function check_value( $opt, $opt_key, $field ) {
734
		if ( is_array( $opt ) ) {
735
			if ( FrmField::is_option_true( $field, 'separate_value' ) ) {
736
				$opt = isset( $opt['value'] ) ? $opt['value'] : ( isset( $opt['label'] ) ? $opt['label'] : reset( $opt ) );
737
			} else {
738
				$opt = isset( $opt['label'] ) ? $opt['label'] : reset( $opt );
739
			}
740
		}
741
742
		return $opt;
743
	}
744
745
	public static function check_label( $opt ) {
746 View Code Duplication
		if ( is_array( $opt ) ) {
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...
747
			$opt = ( isset( $opt['label'] ) ? $opt['label'] : reset( $opt ) );
748
		}
749
750
		return $opt;
751
	}
752
753
	/**
754
	 * @deprecated 4.0
755
	 */
756
	public static function update_ajax_option() {
757
		_deprecated_function( __METHOD__, '4.0' );
758
		FrmAppHelper::permission_check( 'frm_edit_forms' );
759
		check_ajax_referer( 'frm_ajax', 'nonce' );
760
761
		$field_id = FrmAppHelper::get_post_param( 'field', 0, 'absint' );
762
		if ( ! $field_id ) {
763
			wp_die();
764
		}
765
766
		$field = FrmField::getOne( $field_id );
0 ignored issues
show
Bug introduced by
It seems like $field_id defined by \FrmAppHelper::get_post_...m('field', 0, 'absint') on line 761 can also be of type array; however, FrmField::getOne() does only seem to accept string|integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
767
768
		if ( isset( $_POST['separate_value'] ) ) {
769
			$new_val = FrmField::is_option_true( $field, 'separate_value' ) ? 0 : 1;
770
771
			$field->field_options['separate_value'] = $new_val;
772
			unset( $new_val );
773
		}
774
775
		FrmField::update(
776
			$field_id,
777
			array(
778
				'field_options' => $field->field_options,
779
				'form_id'       => $field->form_id,
780
			)
781
		);
782
		wp_die();
783
	}
784
785
	/**
786
	 * @deprecated 4.0
787
	 */
788
	public static function import_choices() {
789
		_deprecated_function( __METHOD__, '4.0' );
790
		wp_die();
791
	}
792
793
	/**
794
	 * Add Single Option or Other Option.
795
	 *
796
	 * @deprecated 4.0 Moved to Pro for Other option only.
797
	 */
798
	public static function add_option() {
799
		_deprecated_function( __METHOD__, '4.0', 'FrmProFormsController::add_other_option' );
800
		if ( is_callable( 'FrmProFormsController::add_other_option' ) ) {
801
			FrmProFormsController::add_other_option();
802
		}
803
	}
804
805
	/**
806
	 * @deprecated 4.0
807
	 */
808
	public static function update_order() {
809
		FrmDeprecated::update_order();
810
	}
811
812
	/**
813
	 * @deprecated 3.0
814
	 * @codeCoverageIgnore
815
	 */
816
	public static function edit_name( $field = 'name', $id = '' ) {
817
		FrmDeprecated::edit_name( $field, $id );
818
	}
819
820
	/**
821
	 * @deprecated 3.0
822
	 * @codeCoverageIgnore
823
	 *
824
	 * @param int $field_id
825
	 * @param array $values
826
	 * @param int $form_id
827
	 *
828
	 * @return array
829
	 */
830
	public static function include_single_field( $field_id, $values, $form_id = 0 ) {
831
		return FrmDeprecated::include_single_field( $field_id, $values, $form_id );
832
	}
833
834
	/**
835
	 * @deprecated 2.3
836
	 * @codeCoverageIgnore
837
	 */
838
	public static function edit_option() {
839
		FrmDeprecated::deprecated( __METHOD__, '2.3' );
840
	}
841
842
	/**
843
	 * @deprecated 2.3
844
	 * @codeCoverageIgnore
845
	 */
846
	public static function delete_option() {
847
		FrmDeprecated::deprecated( __METHOD__, '2.3' );
848
	}
849
}
850