Completed
Pull Request — master (#132)
by Stephanie
02:31
created

FrmFieldsController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
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
		// Javascript may be included in some field settings.
10
		// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
11
		$fields = isset( $_POST['field'] ) ? wp_unslash( $_POST['field'] ) : array();
12
		if ( empty( $fields ) ) {
13
			wp_die();
14
		}
15
16
		$_GET['page'] = 'formidable';
17
18
		$values     = array(
19
			'id'         => FrmAppHelper::get_post_param( 'form_id', '', 'absint' ),
20
			'doing_ajax' => true,
21
		);
22
		$field_html = array();
23
24
		foreach ( $fields as $field ) {
25
			$field = htmlspecialchars_decode( nl2br( $field ) );
26
			$field = json_decode( $field );
27
			if ( ! isset( $field->id ) || ! is_numeric( $field->id ) ) {
28
				// this field may have already been loaded
29
				continue;
30
			}
31
32
			if ( ! isset( $field->value ) ) {
33
				$field->value = '';
34
			}
35
			$field->field_options = json_decode( json_encode( $field->field_options ), true );
36
			$field->options       = json_decode( json_encode( $field->options ), true );
37
			$field->default_value = json_decode( json_encode( $field->default_value ), true );
38
39
			ob_start();
40
			self::load_single_field( $field, $values );
41
			$field_html[ absint( $field->id ) ] = ob_get_contents();
42
			ob_end_clean();
43
		}
44
45
		echo json_encode( $field_html );
46
47
		wp_die();
48
	}
49
50
	/**
51
	 * Create a new field with ajax
52
	 */
53
	public static function create() {
54
		FrmAppHelper::permission_check( 'frm_edit_forms' );
55
		check_ajax_referer( 'frm_ajax', 'nonce' );
56
57
		$field_type = FrmAppHelper::get_post_param( 'field_type', '', 'sanitize_text_field' );
58
		$form_id    = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
59
60
		do_action( 'frm_before_create_field', $field_type, $form_id );
61
62
		$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 57 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...
63
64
		// this hook will allow for multiple fields to be added at once
65
		do_action( 'frm_after_field_created', $field, $form_id );
66
67
		wp_die();
68
	}
69
70
	/**
71
	 * Set up and create a new field
72
	 *
73
	 * @param string $field_type
74
	 * @param integer $form_id
75
	 *
76
	 * @return array|bool
77
	 */
78
	public static function include_new_field( $field_type, $form_id ) {
79
		$field_values = FrmFieldsHelper::setup_new_vars( $field_type, $form_id );
80
		$field_values = apply_filters( 'frm_before_field_created', $field_values );
81
82
		$field_id = FrmField::create( $field_values );
83
84
		if ( ! $field_id ) {
85
			return false;
86
		}
87
88
		$field = self::get_field_array_from_id( $field_id );
89
90
		$values = array();
91
		if ( FrmAppHelper::pro_is_installed() ) {
92
			$values['post_type'] = FrmProFormsHelper::post_type( $form_id );
93
94
			$parent_form_id = FrmDb::get_var( 'frm_forms', array( 'id' => $form_id ), 'parent_form_id' );
95
			if ( $parent_form_id ) {
96
				$field['parent_form_id'] = $parent_form_id;
97
			}
98
		}
99
100
		self::load_single_field( $field, $values, $form_id );
101
102
		return $field;
103
	}
104
105
	public static function duplicate() {
106
		FrmAppHelper::permission_check( 'frm_edit_forms' );
107
		check_ajax_referer( 'frm_ajax', 'nonce' );
108
109
		$field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
110
		$form_id  = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
111
112
		$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 109 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...
113
		if ( ! $copy_field ) {
114
			wp_die();
115
		}
116
117
		do_action( 'frm_duplicate_field', $copy_field, $form_id );
118
		do_action( 'frm_duplicate_field_' . $copy_field->type, $copy_field, $form_id );
119
120
		$values = array(
121
			'id' => $copy_field->id,
122
		);
123
		FrmFieldsHelper::fill_field( $values, $copy_field, $copy_field->form_id );
124
		$values = apply_filters( 'frm_prepare_single_field_for_duplication', $values );
125
126
		$field_id = FrmField::create( $values );
127
		if ( $field_id ) {
128
			self::load_single_field( $field_id, $values );
129
		}
130
131
		wp_die();
132
	}
133
134
	/**
135
	 * @since 3.0
136
	 *
137
	 * @param int $field_id
138
	 *
139
	 * @return array
140
	 */
141
	public static function get_field_array_from_id( $field_id ) {
142
		$field = FrmField::getOne( $field_id );
143
144
		return FrmFieldsHelper::setup_edit_vars( $field );
145
	}
146
147
	/**
148
	 * @since 3.0
149
	 *
150
	 * @param int|array|object $field_object
151
	 * @param array $values
152
	 * @param int $form_id
153
	 */
154
	public static function load_single_field( $field_object, $values, $form_id = 0 ) {
155
		global $frm_vars;
156
		$frm_vars['is_admin'] = true;
157
158
		if ( is_numeric( $field_object ) ) {
159
			$field_object = FrmField::getOne( $field_object );
160
		} elseif ( is_array( $field_object ) ) {
161
			$field        = $field_object;
162
			$field_object = FrmField::getOne( $field['id'] );
163
		}
164
165
		$field_obj = FrmFieldFactory::get_field_factory( $field_object );
166
		$display   = self::display_field_options( array(), $field_obj );
167
168
		$ajax_loading    = isset( $values['ajax_load'] ) && $values['ajax_load'];
169
		$ajax_this_field = isset( $values['count'] ) && $values['count'] > 10 && ! in_array( $field_object->type, array( 'divider', 'end_divider' ) );
170
171
		if ( $ajax_loading && $ajax_this_field ) {
172
			$li_classes = self::get_classes_for_builder_field( array(), $display, $field_obj );
173
			include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/ajax-field-placeholder.php' );
174
		} else {
175
			if ( ! isset( $field ) && is_object( $field_object ) ) {
176
				$field_object->parent_form_id = isset( $values['id'] ) ? $values['id'] : $field_object->form_id;
177
178
				$field = FrmFieldsHelper::setup_edit_vars( $field_object );
179
			}
180
181
			$li_classes = self::get_classes_for_builder_field( $field, $display, $field_obj );
182
			$li_classes .= ' ui-state-default widgets-holder-wrap';
183
184
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/add_field.php' );
185
		}
186
	}
187
188
	/**
189
	 * @since 3.0
190
	 */
191
	private static function get_classes_for_builder_field( $field, $display, $field_info ) {
192
		$li_classes = $field_info->form_builder_classes( $display['type'] );
193
		$classes    = isset( $field['classes'] ) ? $field['classes'] : '';
194
195
		// Exclude alignright for now since we aren't using widths.
196
		$classes    = str_replace( ' frm_alignright ', ' ', $classes );
197
		if ( trim( $classes ) === 'frm_alignright' ) {
198
			$classes = '';
199
		}
200
201
		$li_classes .= ' frm_form_field frmstart ' . $classes . ' frmend';
202
		if ( ! empty( $field ) ) {
203
			$li_classes = apply_filters( 'frm_build_field_class', $li_classes, $field );
204
		}
205
206
		return $li_classes;
207
	}
208
209
	public static function destroy() {
210
		FrmAppHelper::permission_check( 'frm_edit_forms' );
211
		check_ajax_referer( 'frm_ajax', 'nonce' );
212
213
		$field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
214
		FrmField::destroy( $field_id );
215
		wp_die();
216
	}
217
218
	/* Field Options */
219
220
	public static function import_options() {
221
		FrmAppHelper::permission_check( 'frm_edit_forms' );
222
		check_ajax_referer( 'frm_ajax', 'nonce' );
223
224
		if ( ! is_admin() || ! current_user_can( 'frm_edit_forms' ) ) {
225
			return;
226
		}
227
228
		$field_id = FrmAppHelper::get_param( 'field_id', '', 'post', 'absint' );
229
		$field    = FrmField::getOne( $field_id );
230
231
		if ( ! in_array( $field->type, array( 'radio', 'checkbox', 'select' ) ) ) {
232
			return;
233
		}
234
235
		$field = FrmFieldsHelper::setup_edit_vars( $field );
236
		$opts  = FrmAppHelper::get_param( 'opts', '', 'post', 'wp_kses_post' );
237
		$opts  = explode( "\n", rtrim( $opts, "\n" ) );
238
		$opts  = array_map( 'trim', $opts );
239
240
		$separate = FrmAppHelper::get_param( 'separate', '', 'post', 'sanitize_text_field' );
241
		$field['separate_value'] = ( $separate === 'true' );
242
243
		if ( $field['separate_value'] ) {
244
			foreach ( $opts as $opt_key => $opt ) {
245
				if ( strpos( $opt, '|' ) !== false ) {
246
					$vals = explode( '|', $opt );
247
					$opts[ $opt_key ] = array(
248
						'label' => trim( $vals[0] ),
249
						'value' => trim( $vals[1] ),
250
					);
251
					unset( $vals );
252
				}
253
				unset( $opt_key, $opt );
254
			}
255
		}
256
257
		// Keep other options after bulk update.
258
		if ( isset( $field['field_options']['other'] ) && $field['field_options']['other'] == true ) {
259
			$other_array = array();
260
			foreach ( $field['options'] as $opt_key => $opt ) {
261
				if ( FrmFieldsHelper::is_other_opt( $opt_key ) ) {
262
					$other_array[ $opt_key ] = $opt;
263
				}
264
				unset( $opt_key, $opt );
265
			}
266
			if ( ! empty( $other_array ) ) {
267
				$opts = array_merge( $opts, $other_array );
268
			}
269
		}
270
271
		$field['options'] = $opts;
272
273
		FrmFieldsHelper::show_single_option( $field );
274
275
		wp_die();
276
	}
277
278
	/**
279
	 * @since 4.0
280
	 *
281
	 * @param array $atts - Includes field array, field_obj, display array, values array.
282
	 */
283
	public static function load_single_field_settings( $atts ) {
284
		$field     = $atts['field'];
285
		$field_obj = $atts['field_obj'];
286
		$values    = $atts['values'];
287
		$display   = $atts['display'];
288
		unset( $atts );
289
290
		if ( ! isset( $field['unique'] ) ) {
291
			$field['unique'] = false;
292
		}
293
294
		if ( ! isset( $field['read_only'] ) ) {
295
			$field['read_only'] = false;
296
		}
297
298
		$field_types         = FrmFieldsHelper::get_field_types( $field['type'] );
299
		$pro_field_selection = FrmField::pro_field_selection();
300
		$all_field_types     = array_merge( $pro_field_selection, FrmField::field_selection() );
301
		$disabled_fields     = FrmAppHelper::pro_is_installed() ? array() : $pro_field_selection;
302
		$frm_settings        = FrmAppHelper::get_settings();
303
304
		if ( ! isset( $all_field_types[ $field['type'] ] ) ) {
305
			// Add fallback for an add-on field type that has been deactivated.
306
			$all_field_types[ $field['type'] ] = array(
307
				'name' => ucfirst( $field['type'] ),
308
				'icon' => 'frm_icon_font frm_pencil_icon',
309
			);
310
		} elseif ( ! is_array( $all_field_types[ $field['type'] ] ) ) {
311
			// Fallback for fields added in a more basic way.
312
			FrmFormsHelper::prepare_field_type( $all_field_types[ $field['type'] ] );
313
		}
314
315
		$type_name = $all_field_types[ $field['type'] ]['name'];
316
		if ( $field['type'] === 'divider' && FrmField::is_option_true( $field, 'repeat' ) ) {
317
			$type_name = $all_field_types['divider|repeat']['name'];
318
		}
319
320
		if ( $display['default'] ) {
321
			$default_value_types = self::default_value_types( $field, compact( 'display' ) );
322
		}
323
324
		if ( $display['clear_on_focus'] && is_array( $field['placeholder'] ) ) {
325
			$field['placeholder'] = implode( $field['placeholder'], ', ' );
326
		}
327
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/settings.php' );
328
	}
329
330
	/**
331
	 * Get the list of default value types that can be toggled in the builder.
332
	 *
333
	 * @since 4.0
334
	 * @return array
335
	 */
336
	private static function default_value_types( $field, $atts ) {
337
		$types = array(
338
			'default_value' => array(
339
				'class' => '',
340
				'icon'  => 'frm_icon_font frm_text2_icon',
341
				'title' => __( 'Default Value (Text)', 'formidable' ),
342
				'data'  => array(
343
					'frmshow' => '#default-value-for-',
344
				),
345
			),
346
			'calc' => array(
347
				'class' => 'frm_show_upgrade frm_noallow',
348
				'title' => __( 'Default Value (Calculation)', 'formidable' ),
349
				'icon'  => 'frm_icon_font frm_calculator_icon',
350
				'data'  => array(
351
					'medium'  => 'calculations',
352
					'upgrade' => __( 'Calculator forms', 'formidable' ),
353
				),
354
			),
355
			'get_values_field' => array(
356
				'class' => 'frm_show_upgrade frm_noallow',
357
				'title' => __( 'Default Value (Lookup)', 'formidable' ),
358
				'icon'  => 'frm_icon_font frm_search_icon',
359
				'data'  => array(
360
					'medium'  => 'lookup',
361
					'upgrade' => __( 'Lookup fields', 'formidable' ),
362
				),
363
			),
364
		);
365
366
		$types = apply_filters( 'frm_default_value_types', $types, $atts );
367
368
		if ( FrmAppHelper::pro_is_installed() && ! FrmAppHelper::meets_min_pro_version( '4.0' ) ) {
369
			// Prevent settings from showing in 2 spots.
370
			unset( $types['calc'], $types['get_values_field'] );
371
		}
372
373
		// Set active class.
374
		$settings = array_keys( $types );
375
		$active   = 'default_value';
376
377
		foreach ( $settings as $type ) {
378
			if ( ! empty( $field[ $type ] ) ) {
379
				$active = $type;
380
			}
381
		}
382
383
		$types[ $active ]['class']  .= ' current';
384
		$types[ $active ]['current'] = true;
385
386
		return $types;
387
	}
388
389
	public static function change_type( $type ) {
390
		$type_switch = array(
391
			'scale'   => 'radio',
392
			'star'    => 'radio',
393
			'10radio' => 'radio',
394
			'rte'     => 'textarea',
395
			'website' => 'url',
396
			'image'   => 'url',
397
		);
398
		if ( isset( $type_switch[ $type ] ) ) {
399
			$type = $type_switch[ $type ];
400
		}
401
402
		$pro_fields = FrmField::pro_field_selection();
403
		$types      = array_keys( $pro_fields );
404
		if ( in_array( $type, $types ) ) {
405
			$type = 'text';
406
		}
407
408
		return $type;
409
	}
410
411
	/**
412
	 * @param array $settings
413
	 * @param object $field_info
414
	 *
415
	 * @return array
416
	 */
417
	public static function display_field_options( $settings, $field_info = null ) {
418
		if ( $field_info ) {
419
			$settings               = $field_info->display_field_settings();
420
			$settings['field_data'] = $field_info->field;
421
		}
422
423
		return apply_filters( 'frm_display_field_options', $settings );
424
	}
425
426
	/**
427
	 * Display the format option
428
	 *
429
	 * @since 3.0
430
	 *
431
	 * @param array $field
432
	 */
433
	public static function show_format_option( $field ) {
434
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/value-format.php' );
435
	}
436
437
	public static function input_html( $field, $echo = true ) {
438
		$class = array();
439
		self::add_input_classes( $field, $class );
440
441
		$add_html = array();
442
		self::add_html_size( $field, $add_html );
443
		self::add_html_length( $field, $add_html );
444
		self::add_html_placeholder( $field, $add_html, $class );
445
		self::add_validation_messages( $field, $add_html );
446
447
		$class = apply_filters( 'frm_field_classes', implode( ' ', $class ), $field );
448
449
		FrmFormsHelper::add_html_attr( $class, 'class', $add_html );
450
451
		self::add_shortcodes_to_html( $field, $add_html );
452
		self::add_pattern_attribute( $field, $add_html );
453
454
		$add_html = apply_filters( 'frm_field_extra_html', $add_html, $field );
455
		$add_html = ' ' . implode( ' ', $add_html ) . '  ';
456
457
		if ( $echo ) {
458
			echo $add_html; // WPCS: XSS ok.
459
		}
460
461
		return $add_html;
462
	}
463
464
	private static function add_input_classes( $field, array &$class ) {
465
		if ( isset( $field['input_class'] ) && ! empty( $field['input_class'] ) ) {
466
			$class[] = $field['input_class'];
467
		}
468
469
		if ( $field['type'] == 'hidden' || $field['type'] == 'user_id' ) {
470
			return;
471
		}
472
473
		if ( isset( $field['size'] ) && $field['size'] > 0 ) {
474
			$class[] = 'auto_width';
475
		}
476
	}
477
478
	private static function add_html_size( $field, array &$add_html ) {
479
		$size_fields = array(
480
			'select',
481
			'data',
482
			'time',
483
			'hidden',
484
			'file',
485
			'lookup',
486
		);
487
488
		if ( ! isset( $field['size'] ) || $field['size'] <= 0 || in_array( $field['type'], $size_fields ) ) {
489
			return;
490
		}
491
492
		if ( FrmAppHelper::is_admin_page( 'formidable' ) ) {
493
			return;
494
		}
495
496
		if ( is_numeric( $field['size'] ) ) {
497
			$field['size'] .= 'px';
498
		}
499
500
		$important = apply_filters( 'frm_use_important_width', 1, $field );
501
		// Note: This inline styling must stay since we cannot realistically set a class for every possible field size.
502
		$add_html['style'] = 'style="width:' . esc_attr( $field['size'] ) . ( $important ? ' !important' : '' ) . '"';
503
504
		self::add_html_cols( $field, $add_html );
505
	}
506
507
	private static function add_html_cols( $field, array &$add_html ) {
508
		if ( ! in_array( $field['type'], array( 'textarea', 'rte' ) ) ) {
509
			return;
510
		}
511
512
		// Convert to cols for textareas.
513
		$calc = array(
514
			''    => 9,
515
			'px'  => 9,
516
			'rem' => 0.444,
517
			'em'  => 0.544,
518
		);
519
520
		// include "col" for valid html
521
		$unit = trim( preg_replace( '/[0-9]+/', '', $field['size'] ) );
522
523
		if ( ! isset( $calc[ $unit ] ) ) {
524
			return;
525
		}
526
527
		$size = (float) str_replace( $unit, '', $field['size'] ) / $calc[ $unit ];
528
529
		$add_html['cols'] = 'cols="' . absint( $size ) . '"';
530
	}
531
532
	private static function add_html_length( $field, array &$add_html ) {
533
		// Check for max setting and if this field accepts maxlength.
534
		$fields = array(
535
			'textarea',
536
			'rte',
537
			'hidden',
538
			'file',
539
		);
540
541
		if ( FrmField::is_option_empty( $field, 'max' ) || in_array( $field['type'], $fields ) ) {
542
			return;
543
		}
544
545
		if ( FrmAppHelper::is_admin_page( 'formidable' ) ) {
546
			// Don't load on form builder page.
547
			return;
548
		}
549
550
		$add_html['maxlength'] = 'maxlength="' . esc_attr( $field['max'] ) . '"';
551
	}
552
553
	private static function add_html_placeholder( $field, array &$add_html, array &$class ) {
554
		if ( $field['default_value'] != '' ) {
555
			if ( is_array( $field['default_value'] ) ) {
556
				$add_html['data-frmval'] = 'data-frmval="' . esc_attr( json_encode( $field['default_value'] ) ) . '"';
557
			} else {
558
				self::add_frmval_to_input( $field, $add_html );
559
			}
560
		}
561
562
		$field['placeholder'] = self::prepare_placeholder( $field );
563
		if ( $field['placeholder'] == '' || is_array( $field['placeholder'] ) ) {
564
			// don't include a json placeholder
565
			return;
566
		}
567
568
		$frm_settings = FrmAppHelper::get_settings();
569
570
		if ( $frm_settings->use_html ) {
571
			self::add_placeholder_to_input( $field, $add_html );
572
		} else {
573
			self::add_frmval_to_input( $field, $add_html );
574
575
			$class[] = 'frm_toggle_default';
576
577
			if ( $field['value'] == $field['placeholder'] ) {
578
				$class[] = 'frm_default';
579
			}
580
		}
581
	}
582
583
	private static function prepare_placeholder( $field ) {
584
		$is_placeholder_field = FrmFieldsHelper::is_placeholder_field_type( $field['type'] );
585
		$is_combo_field       = in_array( $field['type'], array( 'address', 'credit_card' ) );
586
587
		$placeholder = $field['placeholder'];
588
		if ( empty( $placeholder ) && $is_placeholder_field && ! $is_combo_field ) {
589
			$placeholder = self::get_default_value_from_name( $field );
590
		}
591
592
		return $placeholder;
593
	}
594
595
	/**
596
	 * If the label position is "inside",
597
	 * get the label to use as the placeholder
598
	 *
599
	 * @since 2.05
600
	 *
601
	 * @param array $field
602
	 *
603
	 * @return string
604
	 */
605
	public static function get_default_value_from_name( $field ) {
606
		$position = FrmField::get_option( $field, 'label' );
607
		if ( $position == 'inside' ) {
608
			$default_value = $field['name'];
609
		} else {
610
			$default_value = '';
611
		}
612
613
		return $default_value;
614
	}
615
616
	/**
617
	 * Maybe add a blank placeholder option before any options
618
	 * in a dropdown.
619
	 *
620
	 * @since 4.04
621
	 * @return bool True if placeholder was added.
622
	 */
623
	public static function add_placeholder_to_select( $field ) {
624
		$placeholder = FrmField::get_option( $field, 'placeholder' );
625
		if ( empty( $placeholder ) ) {
626
			$placeholder = self::get_default_value_from_name( $field );
627
		}
628
629
		if ( $placeholder !== '' ) {
630
			?>
631
			<option value="">
632
				<?php echo esc_html( FrmField::get_option( $field, 'autocom' ) ? '' : $placeholder ); ?>
633
			</option>
634
			<?php
635
			return true;
636
		}
637
638
		return false;
639
	}
640
641
	/**
642
	 * Use HMTL5 placeholder with js fallback
643
	 *
644
	 * @param array $field
645
	 * @param array $add_html
646
	 */
647
	private static function add_placeholder_to_input( $field, &$add_html ) {
648
		if ( FrmFieldsHelper::is_placeholder_field_type( $field['type'] ) ) {
649
			$add_html['placeholder'] = 'placeholder="' . esc_attr( $field['placeholder'] ) . '"';
650
		}
651
	}
652
653
	private static function add_frmval_to_input( $field, &$add_html ) {
654
		if ( $field['placeholder'] != '' ) {
655
			$add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['placeholder'] ) . '"';
656
657
			if ( 'select' === $field['type'] ) {
658
				$add_html['data-frmplaceholder'] = 'data-frmplaceholder="' . esc_attr( $field['placeholder'] ) . '"';
659
			}
660
		}
661
662
		if ( $field['default_value'] != '' ) {
663
			$add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['default_value'] ) . '"';
664
		}
665
	}
666
667
	private static function add_validation_messages( $field, array &$add_html ) {
668 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...
669
			$required_message        = FrmFieldsHelper::get_error_msg( $field, 'blank' );
670
			$add_html['data-reqmsg'] = 'data-reqmsg="' . esc_attr( $required_message ) . '"';
671
			self::maybe_add_html_required( $field, $add_html );
672
		}
673
674 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...
675
			$invalid_message         = FrmFieldsHelper::get_error_msg( $field, 'invalid' );
676
			$add_html['data-invmsg'] = 'data-invmsg="' . esc_attr( $invalid_message ) . '"';
677
		}
678
	}
679
680
	/**
681
	 * If 'required' is added to a conditionall hidden field, the form won't
682
	 * submit in many browsers. Check to make sure the javascript to conditionally
683
	 * remove it is present if needed.
684
	 *
685
	 * @since 3.06.01
686
	 * @param array $field
687
	 * @param array $add_html
688
	 */
689
	private static function maybe_add_html_required( $field, array &$add_html ) {
690
		if ( in_array( $field['type'], array( 'radio', 'checkbox', 'file', 'data', 'lookup' ) ) ) {
691
			return;
692
		}
693
694
		$include_html = FrmAppHelper::meets_min_pro_version( '3.06.01' );
695
		if ( $include_html ) {
696
			$add_html['aria-required'] = 'aria-required="true"';
697
		}
698
	}
699
700
	private static function add_shortcodes_to_html( $field, array &$add_html ) {
701
		if ( FrmField::is_option_empty( $field, 'shortcodes' ) ) {
702
			return;
703
		}
704
705
		foreach ( $field['shortcodes'] as $k => $v ) {
706
			if ( 'opt' === $k ) {
707
				continue;
708
			}
709
710
			if ( is_numeric( $k ) && strpos( $v, '=' ) ) {
711
				$add_html[] = $v;
712
			} elseif ( ! empty( $k ) && isset( $add_html[ $k ] ) ) {
713
				$add_html[ $k ] = str_replace( $k . '="', $k . '="' . $v, $add_html[ $k ] );
714
			} else {
715
				$add_html[ $k ] = $k . '="' . esc_attr( $v ) . '"';
716
			}
717
718
			unset( $k, $v );
719
		}
720
	}
721
722
	/**
723
	 * Add pattern attribute
724
	 *
725
	 * @since 3.0
726
	 *
727
	 * @param array $field
728
	 * @param array $add_html
729
	 */
730
	private static function add_pattern_attribute( $field, array &$add_html ) {
731
		$has_format   = FrmField::is_option_true_in_array( $field, 'format' );
732
		$format_field = FrmField::is_field_type( $field, 'text' );
733
734
		if ( $field['type'] == 'phone' || ( $has_format && $format_field ) ) {
735
			$frm_settings = FrmAppHelper::get_settings();
736
737
			if ( $frm_settings->use_html ) {
738
				$format = FrmEntryValidate::phone_format( $field );
739
				$format = substr( $format, 2, - 1 );
740
741
				$add_html['pattern'] = 'pattern="' . esc_attr( $format ) . '"';
742
			}
743
		}
744
	}
745
746
	public static function check_value( $opt, $opt_key, $field ) {
747
		if ( is_array( $opt ) ) {
748
			if ( FrmField::is_option_true( $field, 'separate_value' ) ) {
749
				$opt = isset( $opt['value'] ) ? $opt['value'] : ( isset( $opt['label'] ) ? $opt['label'] : reset( $opt ) );
750
			} else {
751
				$opt = isset( $opt['label'] ) ? $opt['label'] : reset( $opt );
752
			}
753
		}
754
755
		return $opt;
756
	}
757
758
	public static function check_label( $opt ) {
759 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...
760
			$opt = ( isset( $opt['label'] ) ? $opt['label'] : reset( $opt ) );
761
		}
762
763
		return $opt;
764
	}
765
766
	/**
767
	 * @deprecated 4.0
768
	 */
769
	public static function update_ajax_option() {
770
		_deprecated_function( __METHOD__, '4.0' );
771
		FrmAppHelper::permission_check( 'frm_edit_forms' );
772
		check_ajax_referer( 'frm_ajax', 'nonce' );
773
774
		$field_id = FrmAppHelper::get_post_param( 'field', 0, 'absint' );
775
		if ( ! $field_id ) {
776
			wp_die();
777
		}
778
779
		$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 774 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...
780
781
		if ( isset( $_POST['separate_value'] ) ) {
782
			$new_val = FrmField::is_option_true( $field, 'separate_value' ) ? 0 : 1;
783
784
			$field->field_options['separate_value'] = $new_val;
785
			unset( $new_val );
786
		}
787
788
		FrmField::update(
789
			$field_id,
790
			array(
791
				'field_options' => $field->field_options,
792
				'form_id'       => $field->form_id,
793
			)
794
		);
795
		wp_die();
796
	}
797
798
	/**
799
	 * @deprecated 4.0
800
	 */
801
	public static function import_choices() {
802
		_deprecated_function( __METHOD__, '4.0' );
803
		wp_die();
804
	}
805
806
	/**
807
	 * Add Single Option or Other Option.
808
	 *
809
	 * @deprecated 4.0 Moved to Pro for Other option only.
810
	 */
811
	public static function add_option() {
812
		_deprecated_function( __METHOD__, '4.0', 'FrmProFormsController::add_other_option' );
813
		if ( is_callable( 'FrmProFormsController::add_other_option' ) ) {
814
			FrmProFormsController::add_other_option();
815
		}
816
	}
817
818
	/**
819
	 * @deprecated 4.0
820
	 */
821
	public static function update_order() {
822
		FrmDeprecated::update_order();
823
	}
824
825
	/**
826
	 * @deprecated 3.0
827
	 * @codeCoverageIgnore
828
	 */
829
	public static function edit_name( $field = 'name', $id = '' ) {
830
		FrmDeprecated::edit_name( $field, $id );
831
	}
832
833
	/**
834
	 * @deprecated 3.0
835
	 * @codeCoverageIgnore
836
	 *
837
	 * @param int $field_id
838
	 * @param array $values
839
	 * @param int $form_id
840
	 *
841
	 * @return array
842
	 */
843
	public static function include_single_field( $field_id, $values, $form_id = 0 ) {
844
		return FrmDeprecated::include_single_field( $field_id, $values, $form_id );
845
	}
846
847
	/**
848
	 * @deprecated 2.3
849
	 * @codeCoverageIgnore
850
	 */
851
	public static function edit_option() {
852
		FrmDeprecated::deprecated( __METHOD__, '2.3' );
853
	}
854
855
	/**
856
	 * @deprecated 2.3
857
	 * @codeCoverageIgnore
858
	 */
859
	public static function delete_option() {
860
		FrmDeprecated::deprecated( __METHOD__, '2.3' );
861
	}
862
}
863