Completed
Push — master ( 99c6b4...69e680 )
by Stephanie
02:55
created

FrmFieldsController::show_format_option()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
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
        $fields = $_POST['field'];
10
        if ( empty( $fields ) ) {
11
            wp_die();
12
        }
13
14
        $_GET['page'] = 'formidable';
15
        $fields = stripslashes_deep( $fields );
16
17
		$values = array(
18
			'id' => FrmAppHelper::get_post_param( 'form_id', '', 'absint' ),
19
			'doing_ajax' => true,
20
		);
21
        $field_html = array();
22
23
        foreach ( $fields as $field ) {
24
			$field = htmlspecialchars_decode( nl2br( $field ) );
25
			$field = json_decode( $field );
26
			if ( ! isset( $field->id ) || ! is_numeric( $field->id ) ) {
27
				// this field may have already been loaded
28
				continue;
29
			}
30
31
			if ( ! isset( $field->value ) ) {
32
				$field->value = '';
33
			}
34
			$field->field_options = json_decode( json_encode( $field->field_options ), true );
35
			$field->options = json_decode( json_encode( $field->options ), 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
     * @return array|bool
72
     */
73
	public static function include_new_field( $field_type, $form_id ) {
74
		$values = array();
75
		if ( FrmAppHelper::pro_is_installed() ) {
76
			$values['post_type'] = FrmProFormsHelper::post_type( $form_id );
77
		}
78
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
		self::load_single_field( $field, $values, $form_id );
90
91
        return $field;
92
    }
93
94
	public static function edit_name( $field = 'name', $id = '' ) {
95
		_deprecated_function( __FUNCTION__, '3.0' );
96
97
		FrmAppHelper::permission_check( 'frm_edit_forms' );
98
		check_ajax_referer( 'frm_ajax', 'nonce' );
99
100
		if ( empty( $field ) ) {
101
			$field = 'name';
102
		}
103
104
        if ( empty( $id ) ) {
105
			$id = FrmAppHelper::get_post_param( 'element_id', '', 'sanitize_title' );
106
			$id = str_replace( 'field_label_', '', $id );
107
        }
108
109
		$value = FrmAppHelper::get_post_param( 'update_value', '', 'wp_kses_post' );
110
		$value = trim( $value );
111
        if ( trim( strip_tags( $value ) ) === '' ) {
112
            // set blank value if there is no content
113
            $value = '';
114
        }
115
116
		FrmField::update( $id, array( $field => $value ) );
117
118
		do_action( 'frm_after_update_field_' . $field, compact( 'id', 'value' ) );
119
120
		echo stripslashes( wp_kses_post( $value ) );
121
        wp_die();
122
    }
123
124
    public static function update_ajax_option() {
125
		FrmAppHelper::permission_check( 'frm_edit_forms' );
126
        check_ajax_referer( 'frm_ajax', 'nonce' );
127
128
		$field_id = FrmAppHelper::get_post_param( 'field', 0, 'absint' );
129
		if ( ! $field_id ) {
130
			wp_die();
131
		}
132
133
		$field = FrmField::getOne( $field_id );
134
135
		if ( isset( $_POST['separate_value'] ) ) {
136
			$new_val = FrmField::is_option_true( $field, 'separate_value' ) ? 0 : 1;
137
			$field->field_options['separate_value'] = $new_val;
138
			unset( $new_val );
139
		}
140
141
        FrmField::update( $field_id, array(
142
            'field_options' => $field->field_options,
143
			'form_id'       => $field->form_id,
144
        ) );
145
        wp_die();
146
    }
147
148
    public static function duplicate() {
149
		FrmAppHelper::permission_check( 'frm_edit_forms' );
150
        check_ajax_referer( 'frm_ajax', 'nonce' );
151
152
		$field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
153
		$form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
154
155
		$copy_field = FrmField::getOne( $field_id );
156
        if ( ! $copy_field ) {
157
            wp_die();
158
        }
159
160
		do_action( 'frm_duplicate_field', $copy_field, $form_id );
161
		do_action( 'frm_duplicate_field_' . $copy_field->type, $copy_field, $form_id );
162
163
		$values = array();
164
		FrmFieldsHelper::fill_field( $values, $copy_field, $copy_field->form_id );
165
		$values = apply_filters( 'frm_prepare_single_field_for_duplication', $values );
166
167
		$field_id = FrmField::create( $values );
168
		if ( $field_id ) {
169
			self::load_single_field( $field_id, $values );
170
		}
171
172
        wp_die();
173
    }
174
175
	/**
176
	 * Load a single field in the form builder along with all needed variables
177
	 *
178
	 * @param int $field_id
179
	 * @param array $values
180
	 * @param int $form_id
181
	 *
182
	 * @return array
183
	 */
184
	public static function include_single_field( $field_id, $values, $form_id = 0 ) {
185
		_deprecated_function( __FUNCTION__, '3.0', 'FrmFieldsController::load_single_field' );
186
187
		$field = FrmFieldsHelper::setup_edit_vars( FrmField::getOne( $field_id ) );
188
		self::load_single_field( $field, $values, $form_id );
189
190
		return $field;
191
	}
192
193
	/**
194
	 * @since 3.0
195
	 *
196
	 * @param int $field_id
197
	 *
198
	 * @return array
199
	 */
200
	public static function get_field_array_from_id( $field_id ) {
201
		$field = FrmField::getOne( $field_id );
202
		return FrmFieldsHelper::setup_edit_vars( $field );
203
	}
204
205
	/**
206
	 * @since 3.0
207
	 *
208
	 * @param int|array|object $field_object
209
	 * @param array $values
210
	 * @param int $form_id
211
	 */
212
	public static function load_single_field( $field_object, $values, $form_id = 0 ) {
213
		if ( is_numeric( $field_object ) ) {
214
			$field_object = FrmField::getOne( $field_object );
215
		} elseif ( is_array( $field_object ) ) {
216
			$field = $field_object;
217
			$field_object = FrmField::getOne( $field['id'] );
218
		}
219
220
		$field_obj = FrmFieldFactory::get_field_factory( $field_object );
221
		$display = self::display_field_options( array(), $field_obj );
222
223
		$ajax_loading = isset( $values['ajax_load'] ) && $values['ajax_load'];
224
		$ajax_this_field = isset( $values['count'] ) && $values['count'] > 10 && ! in_array( $field_object->type, array( 'divider', 'end_divider' ) );
225
226
		if ( $ajax_loading && $ajax_this_field ) {
227
			$li_classes = self::get_classes_for_builder_field( array(), $display, $field_obj );
228
			include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/ajax-field-placeholder.php' );
229
		} else {
230
			$frm_settings = FrmAppHelper::get_settings();
231
232
			$pro_field_selection = FrmField::pro_field_selection();
233
			$frm_all_field_selection = array_merge( FrmField::field_selection(), $pro_field_selection );
234
			$disabled_fields = FrmAppHelper::pro_is_installed() ? array() : $pro_field_selection;
235
236
			if ( ! isset( $field ) && is_object( $field_object ) ) {
237
				$field_object->parent_form_id = isset( $values['id'] ) ? $values['id'] : $field_object->form_id;
238
				$field = FrmFieldsHelper::setup_edit_vars( $field_object );
239
			}
240
241
			$li_classes = self::get_classes_for_builder_field( $field, $display, $field_obj );
242
			$li_classes .= ' ui-state-default widgets-holder-wrap';
243
244
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/add_field.php' );
245
		}
246
	}
247
248
	/**
249
	 * @since 3.0
250
	 */
251
	private static function get_classes_for_builder_field( $field, $display, $field_info ) {
252
		$li_classes = $field_info->form_builder_classes( $display['type'] );
253
		if ( ! empty( $field ) ) {
254
			$li_classes = apply_filters( 'frm_build_field_class', $li_classes, $field );
255
		}
256
		return $li_classes;
257
	}
258
259
    public static function destroy() {
260
		FrmAppHelper::permission_check( 'frm_edit_forms' );
261
        check_ajax_referer( 'frm_ajax', 'nonce' );
262
263
		$field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
264
		FrmField::destroy( $field_id );
265
        wp_die();
266
    }
267
268
    /* Field Options */
269
270
    //Add Single Option or Other Option
271
    public static function add_option() {
272
		FrmAppHelper::permission_check( 'frm_edit_forms' );
273
        check_ajax_referer( 'frm_ajax', 'nonce' );
274
275
		$id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
276
		$opt_type = FrmAppHelper::get_post_param( 'opt_type', '', 'sanitize_text_field' );
277
		$opt_key = FrmAppHelper::get_post_param( 'opt_key', 0, 'absint' );
278
279
		$field = FrmField::getOne( $id );
280
281
		if ( 'other' == $opt_type ) {
282
			$opt = __( 'Other', 'formidable' );
283
			$opt_key = 'other_' . $opt_key;
284
		} else {
285
			$opt = __( 'New Option', 'formidable' );
286
		}
287
288
		$field_data = $field;
289
		$field = (array) $field;
290
		$field['separate_value'] = isset( $field_data->field_options['separate_value'] ) ? $field_data->field_options['separate_value'] : 0;
291
		unset( $field_data );
292
		$field['html_name'] = 'item_meta[' . $field['id'] . ']';
293
294
		$field['options'] = array( $opt_key => $opt );
295
		FrmFieldsHelper::show_single_option( $field );
296
297
		wp_die();
298
    }
299
300
    public static function edit_option() {
301
		_deprecated_function( __FUNCTION__, '2.3' );
302
    }
303
304
    public static function delete_option() {
305
		_deprecated_function( __FUNCTION__, '2.3' );
306
    }
307
308
    public static function import_choices() {
309
        FrmAppHelper::permission_check( 'frm_edit_forms', 'hide' );
310
311
		$field_id = absint( $_REQUEST['field_id'] );
312
313
        global $current_screen, $hook_suffix;
314
315
        // Catch plugins that include admin-header.php before admin.php completes.
316
        if ( empty( $current_screen ) && function_exists( 'set_current_screen' ) ) {
317
            $hook_suffix = '';
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
318
        	set_current_screen();
319
        }
320
321
        if ( function_exists( 'register_admin_color_schemes' ) ) {
322
            register_admin_color_schemes();
323
        }
324
325
		$hook_suffix = '';
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
326
		$admin_body_class = '';
327
328
        if ( get_user_setting( 'mfold' ) == 'f' ) {
329
        	$admin_body_class .= ' folded';
330
        }
331
332
        if ( function_exists( 'is_admin_bar_showing' ) && is_admin_bar_showing() ) {
333
        	$admin_body_class .= ' admin-bar';
334
        }
335
336
        if ( is_rtl() ) {
337
        	$admin_body_class .= ' rtl';
338
        }
339
340
        $admin_body_class .= ' admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'fresh' );
341
        $prepop = array();
342
		FrmFieldsHelper::get_bulk_prefilled_opts( $prepop );
343
344
		$field = FrmField::getOne( $field_id );
345
346
        wp_enqueue_script( 'utils' );
347
		wp_enqueue_style( 'formidable-admin', FrmAppHelper::plugin_url() . '/css/frm_admin.css' );
348
        FrmAppHelper::load_admin_wide_js();
349
350
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/import_choices.php' );
351
        wp_die();
352
    }
353
354
	public static function import_options() {
355
		FrmAppHelper::permission_check( 'frm_edit_forms' );
356
		check_ajax_referer( 'frm_ajax', 'nonce' );
357
358
		if ( ! is_admin() || ! current_user_can( 'frm_edit_forms' ) ) {
359
			return;
360
		}
361
362
		$field_id = absint( $_POST['field_id'] );
363
		$field = FrmField::getOne( $field_id );
364
365
		if ( ! in_array( $field->type, array( 'radio', 'checkbox', 'select' ) ) ) {
366
            return;
367
        }
368
369
		$field = FrmFieldsHelper::setup_edit_vars( $field );
370
		$opts = FrmAppHelper::get_param( 'opts', '', 'post', 'wp_kses_post' );
371
		$opts = explode( "\n", rtrim( $opts, "\n" ) );
372
		$opts = array_map( 'trim', $opts );
373
374
		if ( $field['separate_value'] ) {
375
			foreach ( $opts as $opt_key => $opt ) {
376
				if ( strpos( $opt, '|' ) !== false ) {
377
					$vals = explode( '|', $opt );
378
					if ( $vals[0] != $vals[1] ) {
379
						$opts[ $opt_key ] = array(
380
							'label' => trim( $vals[0] ),
381
							'value' => trim( $vals[1] ),
382
						);
383
					}
384
					unset( $vals );
385
				}
386
				unset( $opt_key, $opt );
387
			}
388
		}
389
390
        //Keep other options after bulk update
391
        if ( isset( $field['field_options']['other'] ) && $field['field_options']['other'] == true ) {
392
            $other_array = array();
393
            foreach ( $field['options'] as $opt_key => $opt ) {
394
				if ( FrmFieldsHelper::is_other_opt( $opt_key ) ) {
395
					$other_array[ $opt_key ] = $opt;
396
				}
397
				unset( $opt_key, $opt );
398
			}
399
			if ( ! empty( $other_array ) ) {
400
				$opts = array_merge( $opts, $other_array );
401
			}
402
		}
403
404
        $field['options'] = $opts;
405
406
		FrmFieldsHelper::show_single_option( $field );
407
408
        wp_die();
409
    }
410
411
    public static function update_order() {
412
		FrmAppHelper::permission_check( 'frm_edit_forms' );
413
        check_ajax_referer( 'frm_ajax', 'nonce' );
414
415
		$fields = FrmAppHelper::get_post_param( 'frm_field_id' );
416
		foreach ( (array) $fields as $position => $item ) {
417
			FrmField::update( absint( $item ), array( 'field_order' => absint( $position ) ) );
418
		}
419
        wp_die();
420
    }
421
422
	public static function change_type( $type ) {
423
        $type_switch = array(
424
            'scale'     => 'radio',
425
			'star'      => 'radio',
426
            '10radio'   => 'radio',
427
            'rte'       => 'textarea',
428
            'website'   => 'url',
429
			'image'     => 'url',
430
        );
431
        if ( isset( $type_switch[ $type ] ) ) {
432
            $type = $type_switch[ $type ];
433
        }
434
435
		$pro_fields = FrmField::pro_field_selection();
436
		$types = array_keys( $pro_fields );
437
		if ( in_array( $type, $types ) ) {
438
			$type = 'text';
439
		}
440
441
        return $type;
442
    }
443
444
	/**
445
	 * @param array $settings
446
	 * @param object $field_info
447
	 *
448
	 * @return array
449
	 */
450
	public static function display_field_options( $settings, $field_info = null ) {
451
		if ( $field_info ) {
452
			$settings = $field_info->display_field_settings();
453
			$settings['field_data'] = $field_info->field;
454
		}
455
456
		return apply_filters( 'frm_display_field_options', $settings );
457
    }
458
459
	/**
460
	 * Display the format option
461
	 *
462
	 * @since 3.0
463
	 * @param array $field
464
	 */
465
	public static function show_format_option( $field ) {
466
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/value-format.php' );
467
	}
468
469
    public static function input_html( $field, $echo = true ) {
470
        $class = array(); //$field['type'];
471
		self::add_input_classes( $field, $class );
472
473
		$add_html = array();
474
		self::add_html_size( $field, $add_html );
475
		self::add_html_length( $field, $add_html );
476
		self::add_html_placeholder( $field, $add_html, $class );
477
		self::add_validation_messages( $field, $add_html );
478
479
		$class = apply_filters( 'frm_field_classes', implode( ' ', $class ), $field );
480
481
		FrmFormsHelper::add_html_attr( $class, 'class', $add_html );
482
483
		self::add_shortcodes_to_html( $field, $add_html );
484
		self::add_pattern_attribute( $field, $add_html );
485
486
		$add_html = apply_filters( 'frm_field_extra_html', $add_html, $field );
487
		$add_html = ' ' . implode( ' ', $add_html ) . '  ';
488
489
        if ( $echo ) {
490
            echo $add_html;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$add_html'
Loading history...
491
        }
492
493
        return $add_html;
494
    }
495
496
	private static function add_input_classes( $field, array &$class ) {
497
		if ( isset( $field['input_class'] ) && ! empty( $field['input_class'] ) ) {
498
			$class[] = $field['input_class'];
499
		}
500
501
        if ( $field['type'] == 'hidden' || $field['type'] == 'user_id' ) {
502
            return;
503
        }
504
505
		if ( isset( $field['size'] ) && $field['size'] > 0 ) {
506
			$class[] = 'auto_width';
507
		}
508
    }
509
510
	private static function add_html_size( $field, array &$add_html ) {
511
		if ( ! isset( $field['size'] ) || $field['size'] <= 0 || in_array( $field['type'], array( 'select', 'data', 'time', 'hidden', 'file', 'lookup' ) ) ) {
512
            return;
513
        }
514
515
        if ( FrmAppHelper::is_admin_page('formidable' ) ) {
516
            return;
517
        }
518
519
		if ( is_numeric( $field['size'] ) ) {
520
			$field['size'] .= 'px';
521
		}
522
523
		$important = apply_filters( 'frm_use_important_width', 1, $field );
524
        // Note: This inline styling must stay since we cannot realistically set a class for every possible field size
525
		$add_html['style'] = 'style="width:' . esc_attr( $field['size'] ) . ( $important ? ' !important' : '' ) . '"';
526
527
		self::add_html_cols( $field, $add_html );
528
    }
529
530
	private static function add_html_cols( $field, array &$add_html ) {
531
		if ( ! in_array( $field['type'], array( 'textarea', 'rte' ) ) ) {
532
            return;
533
        }
534
535
        // convert to cols for textareas
536
        $calc = array(
537
            ''      => 9,
538
            'px'    => 9,
539
            'rem'   => 0.444,
540
            'em'    => 0.544,
541
        );
542
543
		// include "col" for valid html
544
		$unit = trim( preg_replace( '/[0-9]+/', '', $field['size'] ) );
545
546
        if ( ! isset( $calc[ $unit ] ) ) {
547
            return;
548
        }
549
550
        $size = (float) str_replace( $unit, '', $field['size'] ) / $calc[ $unit ];
551
552
		$add_html['cols'] = 'cols="' . absint( $size ) . '"';
553
    }
554
555
	private static function add_html_length( $field, array &$add_html ) {
556
        // check for max setting and if this field accepts maxlength
557
		if ( FrmField::is_option_empty( $field, 'max' ) || in_array( $field['type'], array( 'textarea', 'rte', 'hidden', 'file' ) ) ) {
558
            return;
559
        }
560
561
		if ( FrmAppHelper::is_admin_page( 'formidable' ) ) {
562
            // don't load on form builder page
563
            return;
564
        }
565
566
		$add_html['maxlength'] = 'maxlength="' . esc_attr( $field['max'] ) . '"';
567
    }
568
569
	private static function add_html_placeholder( $field, array &$add_html, array &$class ) {
570
		if ( FrmAppHelper::is_admin_page( 'formidable' ) ) {
571
			return;
572
		}
573
574
		if ( $field['default_value'] != '' && ! FrmField::is_option_true( $field, 'clear_on_focus' ) ) {
575
			if ( is_array( $field['default_value'] ) ) {
576
				$field['default_value'] = json_encode( $field['default_value'] );
577
				$add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['default_value'] ) . '"';
578
			} else {
579
				self::add_frmval_to_input( $field, $add_html );
580
			}
581
			$field['default_value'] = '';
582
		}
583
584
		$field['default_value'] = self::prepare_default_value( $field );
585
		if ( $field['default_value'] == '' || is_array( $field['default_value'] ) ) {
586
			// don't include a json placeholder
587
			return;
588
		}
589
590
		$frm_settings = FrmAppHelper::get_settings();
591
592
		if ( $frm_settings->use_html ) {
593
			self::add_placeholder_to_input( $field, $add_html );
594
		} else {
595
			self::add_frmval_to_input( $field, $add_html );
596
597
			$class[] = 'frm_toggle_default';
598
599
			if ( $field['value'] == $field['default_value'] ) {
600
				$class[] = 'frm_default';
601
			}
602
		}
603
	}
604
605
	private static function prepare_default_value( $field ) {
606
		$is_placeholder_field = FrmFieldsHelper::is_placeholder_field_type( $field['type'] );
607
		$is_combo_field = in_array( $field['type'], array( 'address', 'credit_card' ) );
608
609
		$default_value = $field['default_value'];
610
		if ( empty( $default_value ) ) {
611
			if ( $is_placeholder_field && ! $is_combo_field ) {
612
				$default_value = self::get_default_value_from_name( $field );
613
			}
614
		}
615
616
		return $default_value;
617
	}
618
619
	/**
620
	 * If the label position is "inside",
621
	 * get the label to use as the placeholder
622
	 *
623
	 * @since 2.05
624
	 *
625
	 * @param array $field
626
	 *
627
	 * @return string
628
	 */
629
	public static function get_default_value_from_name( $field ) {
630
		$position = FrmField::get_option( $field, 'label' );
631
		if ( $position == 'inside' ) {
632
			$default_value = $field['name'];
633
		} else {
634
			$default_value = '';
635
		}
636
		return $default_value;
637
	}
638
639
	/**
640
	 * use HMTL5 placeholder with js fallback
641
	 *
642
	 * @param array $field
643
	 * @param array $add_html
644
	 */
645
	private static function add_placeholder_to_input( $field, &$add_html ) {
646
		if ( FrmFieldsHelper::is_placeholder_field_type( $field['type'] ) ) {
647
			$add_html['placeholder'] = 'placeholder="' . esc_attr( $field['default_value'] ) . '"';
648
		}
649
	}
650
651
	private static function add_frmval_to_input( $field, &$add_html ) {
652
		if ( $field['default_value'] != '' ) {
653
			$add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['default_value'] ) . '"';
654
		}
655
	}
656
657
	private static function add_validation_messages( $field, array &$add_html ) {
658 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...
659
			$required_message = FrmFieldsHelper::get_error_msg( $field, 'blank' );
660
			$add_html['data-reqmsg'] = 'data-reqmsg="' . esc_attr( $required_message ) . '"';
661
		}
662
663 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...
664
			$invalid_message = FrmFieldsHelper::get_error_msg( $field, 'invalid' );
665
			$add_html['data-invmsg'] = 'data-invmsg="' . esc_attr( $invalid_message ) . '"';
666
		}
667
	}
668
669
    private static function add_shortcodes_to_html( $field, array &$add_html ) {
670
        if ( FrmField::is_option_empty( $field, 'shortcodes' ) ) {
671
            return;
672
        }
673
674
        foreach ( $field['shortcodes'] as $k => $v ) {
675
            if ( 'opt' === $k ) {
676
                continue;
677
            }
678
679
			if ( is_numeric( $k ) && strpos( $v, '=' ) ) {
680
                $add_html[] = $v;
681
            } else if ( ! empty( $k ) && isset( $add_html[ $k ] ) ) {
682
				$add_html[ $k ] = str_replace( $k . '="', $k . '="' . $v, $add_html[ $k ] );
683
            } else {
684
				$add_html[ $k ] = $k . '="' . esc_attr( $v ) . '"';
685
            }
686
687
			unset( $k, $v );
688
        }
689
    }
690
691
	/**
692
	 * Add pattern attribute
693
	 *
694
	 * @since 3.0
695
	 * @param array $field
696
	 * @param array $add_html
697
	 */
698
	private static function add_pattern_attribute( $field, array &$add_html ) {
699
		$has_format = FrmField::is_option_true_in_array( $field, 'format' );
700
		$format_field = FrmField::is_field_type( $field, 'text' );
701
702
		if ( $field['type'] == 'phone' || ( $has_format && $format_field ) ) {
703
			$frm_settings = FrmAppHelper::get_settings();
704
705
			if ( $frm_settings->use_html ) {
706
				$format = FrmEntryValidate::phone_format( $field );
707
				$format = substr( $format, 2, -1 );
708
				$add_html['pattern'] = 'pattern="' . esc_attr( $format ) . '"';
709
			}
710
		}
711
	}
712
713
    public static function check_value( $opt, $opt_key, $field ) {
714
        if ( is_array( $opt ) ) {
715
            if ( FrmField::is_option_true( $field, 'separate_value' ) ) {
716
                $opt = isset( $opt['value'] ) ? $opt['value'] : ( isset( $opt['label'] ) ? $opt['label'] : reset( $opt ) );
717
            } else {
718
                $opt = isset( $opt['label'] ) ? $opt['label'] : reset( $opt );
719
            }
720
        }
721
        return $opt;
722
    }
723
724
	public static function check_label( $opt ) {
725 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...
726
			$opt = ( isset( $opt['label'] ) ? $opt['label'] : reset( $opt ) );
727
		}
728
729
		return $opt;
730
	}
731
}
732