Completed
Push — master ( facbef...904b92 )
by Stephanie
03:55
created

FrmFieldsController::add_validation_messages()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 12

Duplication

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