Completed
Push — master ( ac7d8e...ec16eb )
by Stephanie
05:53 queued 02:59
created

FrmFieldsController::duplicate()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 4
nop 0
dl 0
loc 26
rs 8.8571
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 = FrmFieldsHelper::setup_edit_vars( $field_object );
238
			}
239
240
			$li_classes = self::get_classes_for_builder_field( $field, $display, $field_obj );
241
			$li_classes .= ' ui-state-default widgets-holder-wrap';
242
243
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/add_field.php' );
244
		}
245
	}
246
247
	/**
248
	 * @since 3.0
249
	 */
250
	private static function get_classes_for_builder_field( $field, $display, $field_info ) {
251
		$li_classes = $field_info->form_builder_classes( $display['type'] );
252
		if ( ! empty( $field ) ) {
253
			$li_classes = apply_filters( 'frm_build_field_class', $li_classes, $field );
254
		}
255
		return $li_classes;
256
	}
257
258
    public static function destroy() {
259
		FrmAppHelper::permission_check( 'frm_edit_forms' );
260
        check_ajax_referer( 'frm_ajax', 'nonce' );
261
262
		$field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
263
		FrmField::destroy( $field_id );
264
        wp_die();
265
    }
266
267
    /* Field Options */
268
269
    //Add Single Option or Other Option
270
    public static function add_option() {
271
		FrmAppHelper::permission_check( 'frm_edit_forms' );
272
        check_ajax_referer( 'frm_ajax', 'nonce' );
273
274
		$id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
275
		$opt_type = FrmAppHelper::get_post_param( 'opt_type', '', 'sanitize_text_field' );
276
		$opt_key = FrmAppHelper::get_post_param( 'opt_key', 0, 'absint' );
277
278
		$field = FrmField::getOne( $id );
279
280
		if ( 'other' == $opt_type ) {
281
			$opt = __( 'Other', 'formidable' );
282
			$opt_key = 'other_' . $opt_key;
283
		} else {
284
			$opt = __( 'New Option', 'formidable' );
285
		}
286
287
		$field_data = $field;
288
		$field = (array) $field;
289
		$field['separate_value'] = isset( $field_data->field_options['separate_value'] ) ? $field_data->field_options['separate_value'] : 0;
290
		unset( $field_data );
291
		$field['html_name'] = 'item_meta[' . $field['id'] . ']';
292
293
		$field['options'] = array( $opt_key => $opt );
294
		FrmFieldsHelper::show_single_option( $field );
295
296
		wp_die();
297
    }
298
299
    public static function edit_option() {
300
		_deprecated_function( __FUNCTION__, '2.3' );
301
    }
302
303
    public static function delete_option() {
304
		_deprecated_function( __FUNCTION__, '2.3' );
305
    }
306
307
    public static function import_choices() {
308
        FrmAppHelper::permission_check( 'frm_edit_forms', 'hide' );
309
310
		$field_id = absint( $_REQUEST['field_id'] );
311
312
        global $current_screen, $hook_suffix;
313
314
        // Catch plugins that include admin-header.php before admin.php completes.
315
        if ( empty( $current_screen ) && function_exists( 'set_current_screen' ) ) {
316
            $hook_suffix = '';
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
317
        	set_current_screen();
318
        }
319
320
        if ( function_exists( 'register_admin_color_schemes' ) ) {
321
            register_admin_color_schemes();
322
        }
323
324
		$hook_suffix = '';
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
325
		$admin_body_class = '';
326
327
        if ( get_user_setting( 'mfold' ) == 'f' ) {
328
        	$admin_body_class .= ' folded';
329
        }
330
331
        if ( function_exists( 'is_admin_bar_showing' ) && is_admin_bar_showing() ) {
332
        	$admin_body_class .= ' admin-bar';
333
        }
334
335
        if ( is_rtl() ) {
336
        	$admin_body_class .= ' rtl';
337
        }
338
339
        $admin_body_class .= ' admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'fresh' );
340
        $prepop = array();
341
		FrmFieldsHelper::get_bulk_prefilled_opts( $prepop );
342
343
		$field = FrmField::getOne( $field_id );
344
345
        wp_enqueue_script( 'utils' );
346
		wp_enqueue_style( 'formidable-admin', FrmAppHelper::plugin_url() . '/css/frm_admin.css' );
347
        FrmAppHelper::load_admin_wide_js();
348
349
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/import_choices.php' );
350
        wp_die();
351
    }
352
353
	public static function import_options() {
354
		FrmAppHelper::permission_check( 'frm_edit_forms' );
355
		check_ajax_referer( 'frm_ajax', 'nonce' );
356
357
		if ( ! is_admin() || ! current_user_can( 'frm_edit_forms' ) ) {
358
			return;
359
		}
360
361
		$field_id = absint( $_POST['field_id'] );
362
		$field = FrmField::getOne( $field_id );
363
364
		if ( ! in_array( $field->type, array( 'radio', 'checkbox', 'select' ) ) ) {
365
            return;
366
        }
367
368
		$field = FrmFieldsHelper::setup_edit_vars( $field );
369
		$opts = FrmAppHelper::get_param( 'opts', '', 'post', 'wp_kses_post' );
370
		$opts = explode( "\n", rtrim( $opts, "\n" ) );
371
		$opts = array_map( 'trim', $opts );
372
373
		if ( $field['separate_value'] ) {
374
			foreach ( $opts as $opt_key => $opt ) {
375
				if ( strpos( $opt, '|' ) !== false ) {
376
					$vals = explode( '|', $opt );
377
					if ( $vals[0] != $vals[1] ) {
378
						$opts[ $opt_key ] = array(
379
							'label' => trim( $vals[0] ),
380
							'value' => trim( $vals[1] ),
381
						);
382
					}
383
					unset( $vals );
384
				}
385
				unset( $opt_key, $opt );
386
			}
387
		}
388
389
        //Keep other options after bulk update
390
        if ( isset( $field['field_options']['other'] ) && $field['field_options']['other'] == true ) {
391
            $other_array = array();
392
            foreach ( $field['options'] as $opt_key => $opt ) {
393
				if ( FrmFieldsHelper::is_other_opt( $opt_key ) ) {
394
					$other_array[ $opt_key ] = $opt;
395
				}
396
				unset( $opt_key, $opt );
397
			}
398
			if ( ! empty( $other_array ) ) {
399
				$opts = array_merge( $opts, $other_array );
400
			}
401
		}
402
403
        $field['options'] = $opts;
404
405
		FrmFieldsHelper::show_single_option( $field );
406
407
        wp_die();
408
    }
409
410
    public static function update_order() {
411
		FrmAppHelper::permission_check( 'frm_edit_forms' );
412
        check_ajax_referer( 'frm_ajax', 'nonce' );
413
414
		$fields = FrmAppHelper::get_post_param( 'frm_field_id' );
415
		foreach ( (array) $fields as $position => $item ) {
416
			FrmField::update( absint( $item ), array( 'field_order' => absint( $position ) ) );
417
		}
418
        wp_die();
419
    }
420
421
	public static function change_type( $type ) {
422
        $type_switch = array(
423
            'scale'     => 'radio',
424
			'star'      => 'radio',
425
            '10radio'   => 'radio',
426
            'rte'       => 'textarea',
427
            'website'   => 'url',
428
			'image'     => 'url',
429
        );
430
        if ( isset( $type_switch[ $type ] ) ) {
431
            $type = $type_switch[ $type ];
432
        }
433
434
		$pro_fields = FrmField::pro_field_selection();
435
		$types = array_keys( $pro_fields );
436
		if ( in_array( $type, $types ) ) {
437
			$type = 'text';
438
		}
439
440
        return $type;
441
    }
442
443
	/**
444
	 * @param array $settings
445
	 * @param object $field_info
446
	 *
447
	 * @return array
448
	 */
449
	public static function display_field_options( $settings, $field_info = null ) {
450
		if ( $field_info ) {
451
			$settings = $field_info->display_field_settings();
452
			$settings['field_data'] = $field_info->field;
453
		}
454
455
		return apply_filters( 'frm_display_field_options', $settings );
456
    }
457
458
	/**
459
	 * Display the format option
460
	 *
461
	 * @since 3.0
462
	 * @param array $field
463
	 */
464
	public static function show_format_option( $field ) {
465
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/value-format.php' );
466
	}
467
468
    public static function input_html( $field, $echo = true ) {
469
        $class = array(); //$field['type'];
470
		self::add_input_classes( $field, $class );
471
472
		$add_html = array();
473
		self::add_html_size( $field, $add_html );
474
		self::add_html_length( $field, $add_html );
475
		self::add_html_placeholder( $field, $add_html, $class );
476
		self::add_validation_messages( $field, $add_html );
477
478
		$class = apply_filters( 'frm_field_classes', implode( ' ', $class ), $field );
479
480
		FrmFormsHelper::add_html_attr( $class, 'class', $add_html );
481
482
		self::add_shortcodes_to_html( $field, $add_html );
483
		self::add_pattern_attribute( $field, $add_html );
484
485
		$add_html = apply_filters( 'frm_field_extra_html', $add_html, $field );
486
		$add_html = ' ' . implode( ' ', $add_html ) . '  ';
487
488
        if ( $echo ) {
489
            echo $add_html;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$add_html'
Loading history...
490
        }
491
492
        return $add_html;
493
    }
494
495
	private static function add_input_classes( $field, array &$class ) {
496
		if ( isset( $field['input_class'] ) && ! empty( $field['input_class'] ) ) {
497
			$class[] = $field['input_class'];
498
		}
499
500
        if ( $field['type'] == 'hidden' || $field['type'] == 'user_id' ) {
501
            return;
502
        }
503
504
		if ( isset( $field['size'] ) && $field['size'] > 0 ) {
505
			$class[] = 'auto_width';
506
		}
507
    }
508
509
	private static function add_html_size( $field, array &$add_html ) {
510
		if ( ! isset( $field['size'] ) || $field['size'] <= 0 || in_array( $field['type'], array( 'select', 'data', 'time', 'hidden', 'file', 'lookup' ) ) ) {
511
            return;
512
        }
513
514
        if ( FrmAppHelper::is_admin_page('formidable' ) ) {
515
            return;
516
        }
517
518
		if ( is_numeric( $field['size'] ) ) {
519
			$field['size'] .= 'px';
520
		}
521
522
		$important = apply_filters( 'frm_use_important_width', 1, $field );
523
        // Note: This inline styling must stay since we cannot realistically set a class for every possible field size
524
		$add_html['style'] = 'style="width:' . esc_attr( $field['size'] ) . ( $important ? ' !important' : '' ) . '"';
525
526
		self::add_html_cols( $field, $add_html );
527
    }
528
529
	private static function add_html_cols( $field, array &$add_html ) {
530
		if ( ! in_array( $field['type'], array( 'textarea', 'rte' ) ) ) {
531
            return;
532
        }
533
534
        // convert to cols for textareas
535
        $calc = array(
536
            ''      => 9,
537
            'px'    => 9,
538
            'rem'   => 0.444,
539
            'em'    => 0.544,
540
        );
541
542
		// include "col" for valid html
543
		$unit = trim( preg_replace( '/[0-9]+/', '', $field['size'] ) );
544
545
        if ( ! isset( $calc[ $unit ] ) ) {
546
            return;
547
        }
548
549
        $size = (float) str_replace( $unit, '', $field['size'] ) / $calc[ $unit ];
550
551
		$add_html['cols'] = 'cols="' . absint( $size ) . '"';
552
    }
553
554
	private static function add_html_length( $field, array &$add_html ) {
555
        // check for max setting and if this field accepts maxlength
556
		if ( FrmField::is_option_empty( $field, 'max' ) || in_array( $field['type'], array( 'textarea', 'rte', 'hidden', 'file' ) ) ) {
557
            return;
558
        }
559
560
		if ( FrmAppHelper::is_admin_page( 'formidable' ) ) {
561
            // don't load on form builder page
562
            return;
563
        }
564
565
		$add_html['maxlength'] = 'maxlength="' . esc_attr( $field['max'] ) . '"';
566
    }
567
568
	private static function add_html_placeholder( $field, array &$add_html, array &$class ) {
569
		if ( FrmAppHelper::is_admin_page( 'formidable' ) ) {
570
			return;
571
		}
572
573
		if ( $field['default_value'] != '' && ! FrmField::is_option_true( $field, 'clear_on_focus' ) ) {
574
			if ( is_array( $field['default_value'] ) ) {
575
				$field['default_value'] = json_encode( $field['default_value'] );
576
				$add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['default_value'] ) . '"';
577
			} else {
578
				self::add_frmval_to_input( $field, $add_html );
579
			}
580
			$field['default_value'] = '';
581
		}
582
583
		$field['default_value'] = self::prepare_default_value( $field );
584
		if ( $field['default_value'] == '' || is_array( $field['default_value'] ) ) {
585
			// don't include a json placeholder
586
			return;
587
		}
588
589
		$frm_settings = FrmAppHelper::get_settings();
590
591
		if ( $frm_settings->use_html ) {
592
			self::add_placeholder_to_input( $field, $add_html );
593
		} else {
594
			self::add_frmval_to_input( $field, $add_html );
595
596
			$class[] = 'frm_toggle_default';
597
598
			if ( $field['value'] == $field['default_value'] ) {
599
				$class[] = 'frm_default';
600
			}
601
		}
602
	}
603
604
	private static function prepare_default_value( $field ) {
605
		$is_placeholder_field = FrmFieldsHelper::is_placeholder_field_type( $field['type'] );
606
		$is_combo_field = in_array( $field['type'], array( 'address', 'credit_card' ) );
607
608
		$default_value = $field['default_value'];
609
		if ( empty( $default_value ) ) {
610
			if ( $is_placeholder_field && ! $is_combo_field ) {
611
				$default_value = self::get_default_value_from_name( $field );
612
			}
613
		}
614
615
		return $default_value;
616
	}
617
618
	/**
619
	 * If the label position is "inside",
620
	 * get the label to use as the placeholder
621
	 *
622
	 * @since 2.05
623
	 *
624
	 * @param array $field
625
	 *
626
	 * @return string
627
	 */
628
	public static function get_default_value_from_name( $field ) {
629
		$position = FrmField::get_option( $field, 'label' );
630
		if ( $position == 'inside' ) {
631
			$default_value = $field['name'];
632
		} else {
633
			$default_value = '';
634
		}
635
		return $default_value;
636
	}
637
638
	/**
639
	 * use HMTL5 placeholder with js fallback
640
	 *
641
	 * @param array $field
642
	 * @param array $add_html
643
	 */
644
	private static function add_placeholder_to_input( $field, &$add_html ) {
645
		if ( FrmFieldsHelper::is_placeholder_field_type( $field['type'] ) ) {
646
			$add_html['placeholder'] = 'placeholder="' . esc_attr( $field['default_value'] ) . '"';
647
		}
648
	}
649
650
	private static function add_frmval_to_input( $field, &$add_html ) {
651
		if ( $field['default_value'] != '' ) {
652
			$add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['default_value'] ) . '"';
653
		}
654
	}
655
656
	private static function add_validation_messages( $field, array &$add_html ) {
657 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...
658
			$required_message = FrmFieldsHelper::get_error_msg( $field, 'blank' );
659
			$add_html['data-reqmsg'] = 'data-reqmsg="' . esc_attr( $required_message ) . '"';
660
		}
661
662 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...
663
			$invalid_message = FrmFieldsHelper::get_error_msg( $field, 'invalid' );
664
			$add_html['data-invmsg'] = 'data-invmsg="' . esc_attr( $invalid_message ) . '"';
665
		}
666
	}
667
668
    private static function add_shortcodes_to_html( $field, array &$add_html ) {
669
        if ( FrmField::is_option_empty( $field, 'shortcodes' ) ) {
670
            return;
671
        }
672
673
        foreach ( $field['shortcodes'] as $k => $v ) {
674
            if ( 'opt' === $k ) {
675
                continue;
676
            }
677
678
			if ( is_numeric( $k ) && strpos( $v, '=' ) ) {
679
                $add_html[] = $v;
680
            } else if ( ! empty( $k ) && isset( $add_html[ $k ] ) ) {
681
				$add_html[ $k ] = str_replace( $k . '="', $k . '="' . $v, $add_html[ $k ] );
682
            } else {
683
				$add_html[ $k ] = $k . '="' . esc_attr( $v ) . '"';
684
            }
685
686
			unset( $k, $v );
687
        }
688
    }
689
690
	/**
691
	 * Add pattern attribute
692
	 *
693
	 * @since 3.0
694
	 * @param array $field
695
	 * @param array $add_html
696
	 */
697
	private static function add_pattern_attribute( $field, array &$add_html ) {
698
		$has_format = FrmField::is_option_true_in_array( $field, 'format' );
699
		$format_field = FrmField::is_field_type( $field, 'text' );
700
701
		if ( $field['type'] == 'phone' || ( $has_format && $format_field ) ) {
702
			$frm_settings = FrmAppHelper::get_settings();
703
704
			if ( $frm_settings->use_html ) {
705
				$format = FrmEntryValidate::phone_format( $field );
706
				$format = substr( $format, 2, -1 );
707
				$add_html['pattern'] = 'pattern="' . esc_attr( $format ) . '"';
708
			}
709
		}
710
	}
711
712
    public static function check_value( $opt, $opt_key, $field ) {
713
        if ( is_array( $opt ) ) {
714
            if ( FrmField::is_option_true( $field, 'separate_value' ) ) {
715
                $opt = isset( $opt['value'] ) ? $opt['value'] : ( isset( $opt['label'] ) ? $opt['label'] : reset( $opt ) );
716
            } else {
717
                $opt = isset( $opt['label'] ) ? $opt['label'] : reset( $opt );
718
            }
719
        }
720
        return $opt;
721
    }
722
723
	public static function check_label( $opt ) {
724 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...
725
			$opt = ( isset( $opt['label'] ) ? $opt['label'] : reset( $opt ) );
726
		}
727
728
		return $opt;
729
	}
730
}
731