Completed
Push — master ( abd0cd...4ef3e4 )
by Stephanie
03:27
created

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