Completed
Push — master ( 41730f...87f109 )
by Jamie
03:41
created

FrmFieldsController::update_form_id()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 16
rs 9.4285
cc 3
eloc 10
nc 2
nop 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
		$section_id = FrmAppHelper::get_post_param( 'section_id', 0, 'absint' );
62
63
		$field = self::include_new_field( $field_type, $form_id, $section_id );
64
65
        // this hook will allow for multiple fields to be added at once
66
        do_action('frm_after_field_created', $field, $form_id);
67
68
        wp_die();
69
    }
70
71
    /**
72
     * Set up and create a new field
73
     *
74
     * @param string $field_type
75
     * @param integer $form_id
76
     * @param int $section_id
77
     * @return array
78
     */
79
	public static function include_new_field( $field_type, $form_id, $section_id = 0 ) {
80
        $values = array();
81
        if ( FrmAppHelper::pro_is_installed() ) {
82
            $values['post_type'] = FrmProFormsHelper::post_type($form_id);
83
        }
84
85
		$field_values = FrmFieldsHelper::setup_new_vars( $field_type, $form_id );
86
		$field_values['field_options']['in_section'] = $section_id;
87
        $field_values = apply_filters( 'frm_before_field_created', $field_values );
88
89
        $field_id = FrmField::create( $field_values );
90
91
        if ( ! $field_id ) {
92
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by FrmFieldsController::include_new_field of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

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