Completed
Push — master ( 981450...e356d7 )
by Stephanie
03:03
created

FrmFieldsController::add_placeholder_to_input()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
725
	}
726
727
	/**
728
	 * @deprecated 3.0
729
	 * @codeCoverageIgnore
730
	 *
731
	 * @param int $field_id
732
	 * @param array $values
733
	 * @param int $form_id
734
	 *
735
	 * @return array
736
	 */
737
	public static function include_single_field( $field_id, $values, $form_id = 0 ) {
738
		return FrmDeprecated::include_single_field( $field_id, $values, $form_id );
1 ignored issue
show
Deprecated Code introduced by
The method FrmDeprecated::include_single_field() has been deprecated with message: 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
739
	}
740
741
	/**
742
	 * @deprecated 2.3
743
	 * @codeCoverageIgnore
744
	 */
745
	public static function edit_option() {
746
		FrmDeprecated::deprecated( __METHOD__, '2.3' );
1 ignored issue
show
Deprecated Code introduced by
The method FrmDeprecated::deprecated() has been deprecated with message: 2.3

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
747
	}
748
749
	/**
750
	 * @deprecated 2.3
751
	 * @codeCoverageIgnore
752
	 */
753
	public static function delete_option() {
754
		FrmDeprecated::deprecated( __METHOD__, '2.3' );
1 ignored issue
show
Deprecated Code introduced by
The method FrmDeprecated::deprecated() has been deprecated with message: 2.3

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
755
	}
756
}
757