Completed
Push — master ( ce427e...4339a7 )
by Jamie
06:16
created

FrmFieldsController::add_validation_messages()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 18
Code Lines 12

Duplication

Lines 8
Ratio 44.44 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 8
nop 2
dl 8
loc 18
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'];
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
10
        if ( empty( $fields ) ) {
11
            wp_die();
12
        }
13
14
        $_GET['page'] = 'formidable';
15
        $fields = stripslashes_deep( $fields );
16
17
        $ajax = true;
18
		$values = array( 'id' => FrmAppHelper::get_post_param( 'form_id', '', 'absint' ) );
19
        $path = FrmAppHelper::plugin_path();
20
        $field_html = array();
21
22
        foreach ( $fields as $field ) {
23
            $field = htmlspecialchars_decode( nl2br( $field ) );
24
            $field = json_decode( $field, true );
25
            if ( ! isset( $field['id'] ) ) {
26
                // this field may have already been loaded
27
                continue;
28
            }
29
30
            $field_id = absint( $field['id'] );
31
32
            if ( ! isset( $field['value'] ) ) {
33
                $field['value'] = '';
34
            }
35
36
			$field_name = 'item_meta[' . $field_id . ']';
37
            $html_id = FrmFieldsHelper::get_html_id($field);
38
39
            ob_start();
40
			include( $path . '/classes/views/frm-forms/add_field.php' );
41
            $field_html[ $field_id ] = ob_get_contents();
42
            ob_end_clean();
43
        }
44
45
        unset($path);
46
47
        echo json_encode($field_html);
48
49
        wp_die();
50
    }
51
52
	/**
53
	 * Create a new field with ajax
54
	 */
55
    public static function create() {
56
		FrmAppHelper::permission_check('frm_edit_forms');
57
        check_ajax_referer( 'frm_ajax', 'nonce' );
58
59
		$field_type = FrmAppHelper::get_post_param( 'field_type', '', 'sanitize_text_field' );
60
		$form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
61
62
		$field = self::include_new_field( $field_type, $form_id );
63
64
        // this hook will allow for multiple fields to be added at once
65
        do_action('frm_after_field_created', $field, $form_id);
66
67
        wp_die();
68
    }
69
70
    /**
71
     * Set up and create a new field
72
     *
73
     * @param string $field_type
74
     * @param integer $form_id
75
     * @return array|bool
76
     */
77
	public static function include_new_field( $field_type, $form_id ) {
78
        $values = array();
79
        if ( FrmAppHelper::pro_is_installed() ) {
80
            $values['post_type'] = FrmProFormsHelper::post_type($form_id);
81
        }
82
83
		$field_values = FrmFieldsHelper::setup_new_vars( $field_type, $form_id );
84
        $field_values = apply_filters( 'frm_before_field_created', $field_values );
85
86
        $field_id = FrmField::create( $field_values );
87
88
        if ( ! $field_id ) {
89
            return false;
90
        }
91
92
        $field = self::include_single_field($field_id, $values, $form_id);
93
94
        return $field;
95
    }
96
97
	public static function edit_name( $field = 'name', $id = '' ) {
98
		FrmAppHelper::permission_check('frm_edit_forms');
99
        check_ajax_referer( 'frm_ajax', 'nonce' );
100
101
        if ( empty($field) ) {
102
            $field = 'name';
103
        }
104
105
        if ( empty($id) ) {
106
			$id = FrmAppHelper::get_post_param( 'element_id', '', 'sanitize_title' );
107
			$id = str_replace( 'field_label_', '', $id );
108
        }
109
110
		$value = FrmAppHelper::get_post_param( 'update_value', '', 'wp_kses_post' );
111
		$value = trim( $value );
112
        if ( trim(strip_tags($value)) == '' ) {
113
            // set blank value if there is no content
114
            $value = '';
115
        }
116
117
		FrmField::update( $id, array( $field => $value ) );
118
119
		do_action( 'frm_after_update_field_' . $field, compact( 'id', 'value' ) );
120
121
		echo stripslashes( wp_kses_post( $value ) );
1 ignored issue
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'stripslashes'
Loading history...
122
        wp_die();
123
    }
124
125
    public static function update_ajax_option() {
126
		FrmAppHelper::permission_check('frm_edit_forms');
127
        check_ajax_referer( 'frm_ajax', 'nonce' );
128
129
		$field_id = FrmAppHelper::get_post_param( 'field', 0, 'absint' );
130
		if ( ! $field_id ) {
131
			wp_die();
132
		}
133
134
		$field = FrmField::getOne( $field_id );
135
136
		if ( isset( $_POST['separate_value'] ) ) {
137
			$new_val = FrmField::is_option_true( $field, 'separate_value' ) ? 0 : 1;
138
			$field->field_options['separate_value'] = $new_val;
139
			unset($new_val);
140
		}
141
142
        FrmField::update( $field_id, array(
143
            'field_options' => $field->field_options,
144
			'form_id'		=> $field->form_id,
145
        ) );
146
        wp_die();
147
    }
148
149
    public static function duplicate() {
150
		FrmAppHelper::permission_check('frm_edit_forms');
151
        check_ajax_referer( 'frm_ajax', 'nonce' );
152
153
        global $wpdb;
154
155
		$field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
156
		$form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
157
158
		$copy_field = FrmField::getOne( $field_id );
159
        if ( ! $copy_field ) {
160
            wp_die();
161
        }
162
163
		do_action( 'frm_duplicate_field', $copy_field, $form_id );
164
		do_action( 'frm_duplicate_field_' . $copy_field->type, $copy_field, $form_id );
165
166
		$values = array();
167
        FrmFieldsHelper::fill_field( $values, $copy_field, $form_id );
168
169
		$field_count = FrmDb::get_count( $wpdb->prefix . 'frm_fields fi LEFT JOIN ' . $wpdb->prefix . 'frm_forms fr ON (fi.form_id = fr.id)', array( 'or' => 1, 'fr.id' => $form_id, 'fr.parent_form_id' => $form_id ) );
170
171
        $values['field_order'] = $field_count + 1;
172
173
	    $values = apply_filters( 'frm_prepare_single_field_for_duplication', $values );
174
175
        if ( ! $field_id = FrmField::create($values) ) {
176
            wp_die();
177
        }
178
179
        self::include_single_field($field_id, $values);
180
181
        wp_die();
182
    }
183
184
    /**
185
     * Load a single field in the form builder along with all needed variables
186
     */
187
    public static function include_single_field( $field_id, $values, $form_id = 0 ) {
188
        $field = FrmFieldsHelper::setup_edit_vars(FrmField::getOne($field_id));
189
		$field_name = 'item_meta[' . $field_id . ']';
190
        $html_id = FrmFieldsHelper::get_html_id($field);
191
        $id = $form_id ? $form_id : $field['form_id'];
192
        if ( $field['type'] == 'html' ) {
193
            $field['stop_filter'] = true;
194
        }
195
196
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/add_field.php' );
197
198
        return $field;
199
    }
200
201
    public static function destroy() {
202
		FrmAppHelper::permission_check('frm_edit_forms');
203
        check_ajax_referer( 'frm_ajax', 'nonce' );
204
205
		$field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
206
		FrmField::destroy( $field_id );
207
        wp_die();
208
    }
209
210
    /* Field Options */
211
212
    //Add Single Option or Other Option
213
    public static function add_option() {
214
		FrmAppHelper::permission_check('frm_edit_forms');
215
        check_ajax_referer( 'frm_ajax', 'nonce' );
216
217
		$id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
218
		$opt_type = FrmAppHelper::get_post_param( 'opt_type', '', 'sanitize_text_field' );
219
220
        //Get the field
221
        $field = FrmField::getOne($id);
222
223
		if ( ! empty( $field->options ) ) {
224
			$keys = array_keys( $field->options );
225
            $last = str_replace( 'other_', '', end( $keys ) );
226
        } else {
227
            $last = 0;
228
        }
229
        $opt_key = $last + 1;
230
231
        if ( 'other' == $opt_type ) {
232
			$opt = esc_html__( 'Other', 'formidable' );
233
            $other_val = '';
234
            $opt_key = 'other_' . $opt_key;
235
236
            //Update value of "other" in DB
237
            $field_options = maybe_unserialize( $field->field_options );
238
            $field_options['other'] = 1;
239
            FrmField::update( $id, array( 'field_options' => maybe_serialize( $field_options ) ) );
240
        } else {
241
			$first_opt = reset( $field->options );
242
			$next_opt = count( $field->options );
243
            if ( $first_opt != '' ) {
244
                $next_opt++;
245
            }
246
			$opt = esc_html__( 'Option', 'formidable' ) . ' ' . $next_opt;
247
            unset($next_opt);
248
        }
249
        $field_val = $opt;
250
		$field->options[ $opt_key ] = $opt;
251
252
        //Update options in DB
253
		FrmField::update( $id, array( 'options' => $field->options ) );
254
255
        $field_data = $field;
256
        $field = array(
257
            'type'  => $field_data->type,
258
            'id'    => $id,
259
            'separate_value' => isset($field_data->field_options['separate_value']) ? $field_data->field_options['separate_value'] : 0,
260
            'form_id' => $field_data->form_id,
261
            'field_key' => $field_data->field_key,
262
        );
263
264
		$field_name = 'item_meta[' . $id . ']';
265
        $html_id = FrmFieldsHelper::get_html_id($field);
266
        $checked = '';
267
268
        if ( 'other' == $opt_type ) {
269
			require( FrmAppHelper::plugin_path() . '/pro/classes/views/frmpro-fields/other-option.php' );
270
        } else {
271
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/single-option.php' );
272
        }
273
        wp_die();
274
    }
275
276
    public static function edit_option() {
277
		FrmAppHelper::permission_check('frm_edit_forms');
278
        check_ajax_referer( 'frm_ajax', 'nonce' );
279
280
		$element_id = FrmAppHelper::get_post_param( 'element_id', '', 'sanitize_title' );
281
		$ids = explode( '-', $element_id );
282
		$id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
283
284
		$orig_update_value = $update_value = trim( FrmAppHelper::get_post_param( 'update_value', '', 'wp_kses_post' ) );
285
		if ( strpos( $element_id, 'key_' ) ) {
286
            $new_value = $update_value;
287
        } else {
288
            $new_label = $update_value;
289
        }
290
291
        $field = FrmField::getOne($id);
292
        $separate_values = FrmField::is_option_true( $field, 'separate_value' );
293
294
        $this_opt_id = end($ids);
295
		$this_opt = (array) $field->options[ $this_opt_id ];
296
		$other_opt = ( $this_opt_id && strpos( $this_opt_id, 'other') !== false );
297
298
        $label = isset($this_opt['label']) ? $this_opt['label'] : reset($this_opt);
299
        $value = isset($this_opt['value']) ? $this_opt['value'] : '';
300
301
        if ( ! isset( $new_label ) ) {
302
            $new_label = $label;
303
        }
304
305
        if ( isset($new_value) || isset($value) ) {
306
            $update_value = isset($new_value) ? $new_value : $value;
307
        }
308
309
		if ( $update_value != $new_label && $other_opt === false && $separate_values ) {
310
			$field->options[ $this_opt_id ] = array( 'value' => $update_value, 'label' => $new_label );
311
        } else {
312
			$field->options[ $this_opt_id ] = $orig_update_value;
313
        }
314
315
		FrmField::update( $field->id, array( 'options' => $field->options ) );
316
		echo ( $orig_update_value == '' ) ? esc_html__( '(Blank)', 'formidable' ) : stripslashes( $orig_update_value );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
317
        wp_die();
318
    }
319
320
    public static function delete_option() {
321
		FrmAppHelper::permission_check('frm_edit_forms');
322
        check_ajax_referer( 'frm_ajax', 'nonce' );
323
324
		$field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
325
		$field = FrmField::getOne( $field_id );
326
		// Opt key will NOT be numeric for "Other" options
327
		$opt_key = FrmAppHelper::get_post_param( 'opt_key', 0, 'sanitize_title' );
328
329
		$options = $field->options;
330
        unset( $options[ $opt_key ] );
331
        $response = array( 'other' => true );
332
333
        //If the deleted option is an "other" option
334
		if ( FrmFieldsHelper::is_other_opt( $opt_key ) ) {
335
            //Assume all other options are gone, unless proven otherwise
336
            $other = false;
337
338
            //Check if all other options are really gone
339
            foreach ( $options as $o_key => $o_val ) {
340
                //If there is still an other option in the field, set other to true
341
				if ( FrmFieldsHelper::is_other_opt( $o_key ) ) {
342
                    $other = true;
343
                    break;
344
                }
345
                unset( $o_key, $o_val );
346
            }
347
348
            //If all other options are gone
349
            if ( false === $other ) {
350
                $field_options = maybe_unserialize( $field->field_options );
351
                $field_options['other'] = 0;
352
				FrmField::update( $field_id, array( 'field_options' => maybe_serialize( $field_options ) ) );
353
                $response = array( 'other' => false );
354
            }
355
        }
356
        echo json_encode( $response );
357
358
		FrmField::update( $field_id, array( 'options' => maybe_serialize( $options ) ) );
359
360
        wp_die();
361
    }
362
363
    public static function import_choices() {
364
        FrmAppHelper::permission_check( 'frm_edit_forms', 'hide' );
365
366
		$field_id = absint( $_REQUEST['field_id'] );
1 ignored issue
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_REQUEST
Loading history...
367
368
        global $current_screen, $hook_suffix;
369
370
        // Catch plugins that include admin-header.php before admin.php completes.
371
        if ( empty( $current_screen ) && function_exists( 'set_current_screen' ) ) {
372
            $hook_suffix = '';
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
373
        	set_current_screen();
374
        }
375
376
        if ( function_exists( 'register_admin_color_schemes' ) ) {
377
            register_admin_color_schemes();
378
        }
379
380
        $hook_suffix = $admin_body_class = '';
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
381
382
        if ( get_user_setting( 'mfold' ) == 'f' ) {
383
        	$admin_body_class .= ' folded';
384
        }
385
386
        if ( function_exists( 'is_admin_bar_showing' ) && is_admin_bar_showing() ) {
387
        	$admin_body_class .= ' admin-bar';
388
        }
389
390
        if ( is_rtl() ) {
391
        	$admin_body_class .= ' rtl';
392
        }
393
394
        $admin_body_class .= ' admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'fresh' );
395
        $prepop = array();
396
        FrmFieldsHelper::get_bulk_prefilled_opts($prepop);
397
398
        $field = FrmField::getOne($field_id);
399
400
        wp_enqueue_script( 'utils' );
401
		wp_enqueue_style( 'formidable-admin', FrmAppHelper::plugin_url() . '/css/frm_admin.css' );
402
        FrmAppHelper::load_admin_wide_js();
403
404
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/import_choices.php' );
405
        wp_die();
406
    }
407
408
    public static function import_options() {
409
		FrmAppHelper::permission_check('frm_edit_forms');
410
        check_ajax_referer( 'frm_ajax', 'nonce' );
411
412
        if ( ! is_admin() || ! current_user_can('frm_edit_forms') ) {
413
            return;
414
        }
415
416
		$field_id = absint( $_POST['field_id'] );
1 ignored issue
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
417
        $field = FrmField::getOne($field_id);
418
419
		if ( ! in_array( $field->type, array( 'radio', 'checkbox', 'select' ) ) ) {
420
            return;
421
        }
422
423
        $field = FrmFieldsHelper::setup_edit_vars($field);
424
		$opts = FrmAppHelper::get_param( 'opts', '', 'post', 'wp_kses_post' );
425
		$opts = explode( "\n", rtrim( $opts, "\n" ) );
426
		$opts = array_map( 'trim', $opts );
427
428
        if ( $field['separate_value'] ) {
429
            foreach ( $opts as $opt_key => $opt ) {
430
                if ( strpos($opt, '|') !== false ) {
431
                    $vals = explode('|', $opt);
432
                    if ( $vals[0] != $vals[1] ) {
433
                        $opts[ $opt_key ] = array( 'label' => trim( $vals[0] ), 'value' => trim( $vals[1] ) );
434
                    }
435
                    unset($vals);
436
                }
437
                unset($opt_key, $opt);
438
            }
439
        }
440
441
        //Keep other options after bulk update
442
        if ( isset( $field['field_options']['other'] ) && $field['field_options']['other'] == true ) {
443
            $other_array = array();
444
            foreach ( $field['options'] as $opt_key => $opt ) {
445
                if ( $opt_key && strpos( $opt_key, 'other' ) !== false ) {
446
                    $other_array[ $opt_key ] = $opt;
447
                }
448
                unset($opt_key, $opt);
449
            }
450
            if ( ! empty($other_array) ) {
451
                $opts = array_merge( $opts, $other_array);
452
            }
453
        }
454
455
        FrmField::update( $field_id, array( 'options' => maybe_serialize( $opts ) ) );
456
457
        $field['options'] = $opts;
458
        $field_name = $field['name'];
459
460
        // Get html_id which will be used in single-option.php
461
        $html_id = FrmFieldsHelper::get_html_id( $field );
462
463
        if ( $field['type'] == 'radio' || $field['type'] == 'checkbox' ) {
464
			require( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/radio.php' );
465
        } else {
466
            FrmFieldsHelper::show_single_option($field);
467
        }
468
469
        wp_die();
470
    }
471
472
    public static function update_order() {
473
		FrmAppHelper::permission_check('frm_edit_forms');
474
        check_ajax_referer( 'frm_ajax', 'nonce' );
475
476
		$fields = FrmAppHelper::get_post_param( 'frm_field_id' );
477
		foreach ( (array) $fields as $position => $item ) {
478
			FrmField::update( absint( $item ), array( 'field_order' => absint( $position ) ) );
479
		}
480
        wp_die();
481
    }
482
483
	public static function change_type( $type ) {
484
        $type_switch = array(
485
            'scale'     => 'radio',
486
            '10radio'   => 'radio',
487
            'rte'       => 'textarea',
488
            'website'   => 'url',
489
        );
490
        if ( isset( $type_switch[ $type ] ) ) {
491
            $type = $type_switch[ $type ];
492
        }
493
494
		$frm_field_selection = FrmField::field_selection();
495
        $types = array_keys($frm_field_selection);
496
        if ( ! in_array($type, $types) && $type != 'captcha' ) {
497
            $type = 'text';
498
        }
499
500
        return $type;
501
    }
502
503
	public static function display_field_options( $display ) {
504
		switch ( $display['type'] ) {
505
            case 'captcha':
506
                $display['required'] = false;
507
                $display['invalid'] = true;
508
                $display['default_blank'] = false;
509
				$display['captcha_size'] = true;
510
            break;
511
            case 'radio':
512
                $display['default_blank'] = false;
513
            break;
514
            case 'text':
515
            case 'textarea':
516
                $display['size'] = true;
517
                $display['clear_on_focus'] = true;
518
            break;
519
            case 'select':
520
                $display['size'] = true;
521
            break;
522
            case 'url':
523
            case 'website':
524
            case 'email':
525
                $display['size'] = true;
526
                $display['clear_on_focus'] = true;
527
                $display['invalid'] = true;
528
        }
529
530
        return $display;
531
    }
532
533
    public static function input_html( $field, $echo = true ) {
534
        $class = array(); //$field['type'];
535
        self::add_input_classes($field, $class);
536
537
        $add_html = array();
538
        self::add_html_size($field, $add_html);
539
        self::add_html_length($field, $add_html);
540
        self::add_html_placeholder($field, $add_html, $class);
541
		self::add_validation_messages( $field, $add_html );
542
543
        $class = apply_filters('frm_field_classes', implode(' ', $class), $field);
544
545
		FrmFormsHelper::add_html_attr( $class, 'class', $add_html );
546
547
        self::add_shortcodes_to_html($field, $add_html);
548
549
		$add_html = apply_filters( 'frm_field_extra_html', $add_html, $field );
550
		$add_html = ' ' . implode( ' ', $add_html ) . '  ';
551
552
        if ( $echo ) {
553
            echo $add_html;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$add_html'
Loading history...
554
        }
555
556
        return $add_html;
557
    }
558
559
	private static function add_input_classes( $field, array &$class ) {
560
        if ( isset($field['input_class']) && ! empty($field['input_class']) ) {
561
            $class[] = $field['input_class'];
562
        }
563
564
        if ( $field['type'] == 'hidden' || $field['type'] == 'user_id' ) {
565
            return;
566
        }
567
568
        global $frm_vars;
569
		if ( is_admin() && ! FrmAppHelper::is_preview_page() && ! in_array( $field['type'], array( 'scale', 'radio', 'checkbox', 'data', 'lookup' ) ) ) {
570
			// Add the dyn_default_value class to some field inputs on form builder page
571
            $class[] = 'dyn_default_value';
572
        }
573
574
        if ( isset($field['size']) && $field['size'] > 0 ) {
575
            $class[] = 'auto_width';
576
        }
577
    }
578
579
	private static function add_html_size( $field, array &$add_html ) {
580
		if ( ! isset( $field['size'] ) || $field['size'] <= 0 || in_array( $field['type'], array( 'select', 'data', 'time', 'hidden', 'file', 'lookup' ) ) ) {
581
            return;
582
        }
583
584
        if ( FrmAppHelper::is_admin_page('formidable' ) ) {
585
            return;
586
        }
587
588
        if ( is_numeric($field['size']) ) {
589
            $field['size'] .= 'px';
590
        }
591
592
        $important = apply_filters('frm_use_important_width', 1, $field);
593
        // Note: This inline styling must stay since we cannot realistically set a class for every possible field size
594
		$add_html['style'] = 'style="width:' . esc_attr( $field['size'] ) . ( $important ? ' !important' : '' ) . '"';
595
596
        self::add_html_cols($field, $add_html);
597
    }
598
599
	private static function add_html_cols( $field, array &$add_html ) {
600
		if ( ! in_array( $field['type'], array( 'textarea', 'rte' ) ) ) {
601
            return;
602
        }
603
604
        // convert to cols for textareas
605
        $calc = array(
606
            ''      => 9,
607
            'px'    => 9,
608
            'rem'   => 0.444,
609
            'em'    => 0.544,
610
        );
611
612
        // include "col" for valid html
613
        $unit = trim(preg_replace('/[0-9]+/', '', $field['size']));
614
615
        if ( ! isset( $calc[ $unit ] ) ) {
616
            return;
617
        }
618
619
        $size = (float) str_replace( $unit, '', $field['size'] ) / $calc[ $unit ];
620
621
		$add_html['cols'] = 'cols="' . absint( $size ) . '"';
622
    }
623
624
	private static function add_html_length( $field, array &$add_html ) {
625
        // check for max setting and if this field accepts maxlength
626
		if ( FrmField::is_option_empty( $field, 'max' ) || in_array( $field['type'], array( 'textarea', 'rte', 'hidden', 'file' ) ) ) {
627
            return;
628
        }
629
630
        if ( FrmAppHelper::is_admin_page('formidable' ) ) {
631
            // don't load on form builder page
632
            return;
633
        }
634
635
		$add_html['maxlength'] = 'maxlength="' . esc_attr( $field['max'] ) . '"';
636
    }
637
638
	private static function add_html_placeholder( $field, array &$add_html, array &$class ) {
639
		if ( empty( $field['default_value'] ) || FrmAppHelper::is_admin_page( 'formidable' ) ) {
640
			return;
641
		}
642
643
		$default_value_array = is_array( $field['default_value'] );
644
        if ( ! FrmField::is_option_true( $field, 'clear_on_focus' ) ) {
645
			if ( $default_value_array ) {
646
				$field['default_value'] = json_encode( $field['default_value'] );
647
			}
648
			$add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['default_value'] ) . '"';
649
            return;
650
        }
651
652
		if ( $default_value_array ) {
653
			// don't include a json placeholder
654
			return;
655
		}
656
657
        $frm_settings = FrmAppHelper::get_settings();
658
659
		if ( $frm_settings->use_html && ! in_array( $field['type'], array( 'select', 'radio', 'checkbox', 'hidden' ) ) ) {
660
            // use HMTL5 placeholder with js fallback
661
			$add_html['placeholder'] = 'placeholder="' . esc_attr( $field['default_value'] ) . '"';
662
            wp_enqueue_script('jquery-placeholder');
663
        } else if ( ! $frm_settings->use_html ) {
664
			$val = str_replace( array( "\r\n", "\n" ), '\r', addslashes( str_replace( '&#039;', "'", esc_attr( $field['default_value'] ) ) ) );
665
			$add_html['data-frmval'] = 'data-frmval="' . esc_attr( $val ) . '"';
666
            $class[] = 'frm_toggle_default';
667
668
            if ( $field['value'] == $field['default_value'] ) {
669
                $class[] = 'frm_default';
670
            }
671
        }
672
    }
673
674
	private static function add_validation_messages( $field, array &$add_html ) {
675 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...
676
			$required_message = FrmFieldsHelper::get_error_msg( $field, 'blank' );
677
			$add_html['data-reqmsg'] = 'data-reqmsg="' . esc_attr( $required_message ) . '"';
678
		}
679
680 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...
681
			$invalid_message = FrmFieldsHelper::get_error_msg( $field, 'invalid' );
682
			$add_html['data-invmsg'] = 'data-invmsg="' . esc_attr( $invalid_message ) . '"';
683
		}
684
685
		if ( $field['type'] == 'phone' || ( $field['type'] == 'text' && FrmField::is_option_true_in_array( $field, 'format' ) ) ) {
686
			$format = FrmEntryValidate::phone_format( $field );
687
			$format = substr( $format, 2, -1 );
688
			$key = 'pattern';
689
			$add_html[ $key ] = $key . '="' . esc_attr( $format ) . '"';
690
		}
691
	}
692
693
    private static function add_shortcodes_to_html( $field, array &$add_html ) {
694
        if ( FrmField::is_option_empty( $field, 'shortcodes' ) ) {
695
            return;
696
        }
697
698
        foreach ( $field['shortcodes'] as $k => $v ) {
699
            if ( 'opt' === $k ) {
700
                continue;
701
            }
702
703
            if ( is_numeric($k) && strpos($v, '=') ) {
704
                $add_html[] = $v;
705
            } else if ( ! empty( $k ) && isset( $add_html[ $k ] ) ) {
706
				$add_html[ $k ] = str_replace( $k . '="', $k . '="' . $v, $add_html[ $k ] );
707
            } else {
708
				$add_html[ $k ] = $k . '="' . esc_attr( $v ) . '"';
709
            }
710
711
            unset($k, $v);
712
        }
713
    }
714
715
    public static function check_value( $opt, $opt_key, $field ) {
1 ignored issue
show
Unused Code introduced by
The parameter $opt_key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
716
        if ( is_array( $opt ) ) {
717
            if ( FrmField::is_option_true( $field, 'separate_value' ) ) {
718
                $opt = isset( $opt['value'] ) ? $opt['value'] : ( isset( $opt['label'] ) ? $opt['label'] : reset( $opt ) );
719
            } else {
720
                $opt = isset( $opt['label'] ) ? $opt['label'] : reset( $opt );
721
            }
722
        }
723
        return $opt;
724
    }
725
726
	public static function check_label( $opt ) {
727 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...
728
            $opt = (isset($opt['label']) ? $opt['label'] : reset($opt));
729
        }
730
731
        return $opt;
732
    }
733
}
734