Completed
Push — master ( a0de24...442934 )
by Stephanie
04:02
created

FrmFieldsController::display_field_options()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 29
Code Lines 26

Duplication

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