C:\xampp\htdocs\landing\wp-content\plugins\wpforms-lite\includes\fields\class-select.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
<?php

/**
 * Dropdown field.
 *
 * @since 1.0.0
 */
class WPForms_Field_Select extends WPForms_Field {

    
/**
     * Choices JS version.
     *
     * @since 1.6.3
     */
    
const CHOICES_VERSION '9.0.1';

    
/**
     * Classic (old) style.
     *
     * @since 1.6.1
     *
     * @var string
     */
    
const STYLE_CLASSIC 'classic';

    
/**
     * Modern style.
     *
     * @since 1.6.1
     *
     * @var string
     */
    
const STYLE_MODERN 'modern';

    
/**
     * Primary class constructor.
     *
     * @since 1.0.0
     */
    
public function init() {

        
// Define field type information.
        
$this->name     esc_html__'Dropdown''wpforms-lite' );
        
$this->type     'select';
        
$this->icon     'fa-caret-square-o-down';
        
$this->order    70;
        
$this->defaults = array(
            
=> array(
                
'label'   => esc_html__'First Choice''wpforms-lite' ),
                
'value'   => '',
                
'default' => '',
            ),
            
=> array(
                
'label'   => esc_html__'Second Choice''wpforms-lite' ),
                
'value'   => '',
                
'default' => '',
            ),
            
=> array(
                
'label'   => esc_html__'Third Choice''wpforms-lite' ),
                
'value'   => '',
                
'default' => '',
            ),
        );

        
// Define additional field properties.
        
add_filter'wpforms_field_properties_' $this->type, array( $this'field_properties' ), 5);

        
// Form frontend CSS enqueues.
        
add_action'wpforms_frontend_css', array( $this'enqueue_frontend_css' ) );

        
// Form frontend JS enqueues.
        
add_action'wpforms_frontend_js', array( $this'enqueue_frontend_js' ) );
    }

    
/**
     * Define additional field properties.
     *
     * @since 1.5.0
     *
     * @param array $properties Field properties.
     * @param array $field      Field settings.
     * @param array $form_data  Form data and settings.
     *
     * @return array
     */
    
public function field_properties$properties$field$form_data ) {

        
// Remove primary input.
        
unset( $properties['inputs']['primary'] );

        
// Define data.
        
$form_id  absint$form_data['id'] );
        
$field_id absint$field['id'] );
        
$choices  $field['choices'];
        
$dynamic  wpforms_get_field_dynamic_choices$field$form_id$form_data );

        if ( 
$dynamic ) {
            
$choices              $dynamic;
            
$field['show_values'] = true;
        }

        
// Set options container (<select>) properties.
        
$properties['input_container'] = array(
            
'class' => array(),
            
'data'  => array(),
            
'id'    => "wpforms-{$form_id}-field_{$field_id}",
            
'attr'  => array(
                
'name' => "wpforms[fields][{$field_id}]",
            ),
        );

        
// Set properties.
        
foreach ( $choices as $key => $choice ) {

            
// Used for dynamic choices.
            
$depth = isset( $choice['depth'] ) ? absint$choice['depth'] ) : 1;

            
$properties['inputs'][ $key ] = array(
                
'container' => array(
                    
'attr'  => array(),
                    
'class' => array( "choice-{$key}""depth-{$depth}),
                    
'data'  => array(),
                    
'id'    => '',
                ),
                
'label'     => array(
                    
'attr'  => array(
                        
'for' => "wpforms-{$form_id}-field_{$field_id}_{$key}",
                    ),
                    
'class' => array( 'wpforms-field-label-inline' ),
                    
'data'  => array(),
                    
'id'    => '',
                    
'text'  => $choice['label'],
                ),
                
'attr'      => array(
                    
'name'  => "wpforms[fields][{$field_id}]",
                    
'value' => isset( $field['show_values'] ) ? $choice['value'] : $choice['label'],
                ),
                
'class'     => array(),
                
'data'      => array(),
                
'id'        => "wpforms-{$form_id}-field_{$field_id}_{$key}",
                
'required'  => ! empty( $field['required'] ) ? 'required' '',
                
'default'   => isset( $choice['default'] ),
            );
        }

        
// Add class that changes the field size.
        
if ( ! empty( $field['size'] ) ) {
            
$properties['input_container']['class'][] = 'wpforms-field-' esc_attr$field['size'] );
        }

        
// Required class for pagebreak validation.
        
if ( ! empty( $field['required'] ) ) {
            
$properties['input_container']['class'][] = 'wpforms-field-required';
        }

        
// Add additional class for container.
        
if (
            ! empty( 
$field['style'] ) &&
            
in_array$field['style'], array( self::STYLE_CLASSICself::STYLE_MODERN ), true )
        ) {
            
$properties['container']['class'][] = "wpforms-field-select-style-{$field['style']}";
        }

        return 
$properties;
    }

    
/**
     * Field options panel inside the builder.
     *
     * @since 1.0.0
     *
     * @param array $field Field settings.
     */
    
public function field_options$field ) {

        
/*
         * Basic field options.
         */

        // Options open markup.
        
$this->field_option(
            
'basic-options',
            
$field,
            array(
                
'markup' => 'open',
            )
        );

        
// Label.
        
$this->field_option'label'$field );

        
// Choices.
        
$this->field_option'choices'$field );

        
// Description.
        
$this->field_option'description'$field );

        
// Required toggle.
        
$this->field_option'required'$field );

        
// Options close markup.
        
$this->field_option(
            
'basic-options',
            
$field,
            array(
                
'markup' => 'close',
            )
        );

        
/*
         * Advanced field options.
         */

        // Options open markup.
        
$this->field_option(
            
'advanced-options',
            
$field,
            array(
                
'markup' => 'open',
            )
        );

        
// Show Values toggle option. This option will only show if already used
        // or if manually enabled by a filter.
        
if ( ! empty( $field['show_values'] ) || wpforms_show_fields_options_setting() ) {
            
$show_values $this->field_element(
                
'checkbox',
                
$field,
                array(
                    
'slug'    => 'show_values',
                    
'value'   => isset( $field['show_values'] ) ? $field['show_values'] : '0',
                    
'desc'    => esc_html__'Show Values''wpforms-lite' ),
                    
'tooltip' => esc_html__'Check this to manually set form field values.''wpforms-lite' ),
                ),
                
false
            
);
            
$this->field_element(
                
'row',
                
$field,
                array(
                    
'slug'    => 'show_values',
                    
'content' => $show_values,
                )
            );
        }

        
// Multiple options selection.
        
$fld $this->field_element(
            
'checkbox',
            
$field,
            array(
                
'slug'    => 'multiple',
                
'value'   => ! empty( $field['multiple'] ),
                
'desc'    => esc_html__'Multiple Options Selection''wpforms-lite' ),
                
'tooltip' => esc_html__'Allow users to select multiple choices in this field.''wpforms-lite' ) . '<br>' .
                            
sprintf(
                                
wp_kses/* translators: %s - URL to WPForms.com doc article. */
                                    
esc_html__'For details, including how this looks and works for your site\'s visitors, please check out <a href="%s" target="_blank" rel="noopener noreferrer">our doc</a>. ''wpforms-lite' ),
                                    [
                                        
'a' => [
                                            
'href'   => [],
                                            
'target' => [],
                                            
'rel'    => [],
                                        ],
                                    ]
                                ),
                                
'https://wpforms.com/docs/how-to-allow-multiple-selections-to-a-dropdown-field-in-wpforms/'
                            
),
            ),
            
false
        
);

        
$this->field_element(
            
'row',
            
$field,
            array(
                
'slug'    => 'multiple',
                
'content' => $fld,
            )
        );

        
// Style.
        
$lbl $this->field_element(
            
'label',
            
$field,
            array(
                
'slug'    => 'style',
                
'value'   => esc_html__'Style''wpforms-lite' ),
                
'tooltip' => esc_html__'Classic style is the default one generated by your browser. Modern has a fresh look and displays all selected options in a single row.''wpforms-lite' ),
            ),
            
false
        
);

        
$fld $this->field_element(
            
'select',
            
$field,
            array(
                
'slug'    => 'style',
                
'value'   => ! empty( $field['style'] ) ? $field['style'] : self::STYLE_CLASSIC,
                
'options' => array(
                    
self::STYLE_CLASSIC => esc_html__'Classic''wpforms-lite' ),
                    
self::STYLE_MODERN  => esc_html__'Modern''wpforms-lite' ),
                ),
            ),
            
false
        
);

        
$this->field_element(
            
'row',
            
$field,
            array(
                
'slug'    => 'style',
                
'content' => $lbl $fld,
            )
        );

        
// Size.
        
$this->field_option'size'$field );

        
// Placeholder.
        
$this->field_option'placeholder'$field );

        
// Hide label.
        
$this->field_option'label_hide'$field );

        
// Custom CSS classes.
        
$this->field_option'css'$field );

        
// Dynamic choice auto-populating toggle.
        
$this->field_option'dynamic_choices'$field );

        
// Dynamic choice source.
        
$this->field_option'dynamic_choices_source'$field );

        
// Options close markup.
        
$this->field_option(
            
'advanced-options',
            
$field,
            array(
                
'markup' => 'close',
            )
        );
    }

    
/**
     * Field preview inside the builder.
     *
     * @since 1.0.0
     * @since 1.6.1 Added a `Modern` style select support.
     *
     * @param array $field Field settings.
     */
    
public function field_preview$field ) {

        
$args = array();

        
// Label.
        
$this->field_preview_option'label'$field );

        
// Prepare arguments.
        
$args['modern'] = false;

        if (
            ! empty( 
$field['style'] ) &&
            
self::STYLE_MODERN === $field['style']
        ) {
            
$args['modern'] = true;
            
$args['class']  = 'choicesjs-select';
        }

        
// Choices.
        
$this->field_preview_option'choices'$field$args );

        
// Description.
        
$this->field_preview_option'description'$field );
    }

    
/**
     * Field display on the form front-end.
     *
     * @since 1.0.0
     * @since 1.5.0 Converted to a new format, where all the data are taken not from $deprecated, but field properties.
     * @since 1.6.1 Added a multiple select support.
     *
     * @param array $field      Field data and settings.
     * @param array $deprecated Deprecated array of field attributes.
     * @param array $form_data  Form data and settings.
     */
    
public function field_display$field$deprecated$form_data ) {

        
$container         $field['properties']['input_container'];
        
$field_placeholder = ! empty( $field['placeholder'] ) ? $field['placeholder'] : '';
        
$is_multiple       = ! empty( $field['multiple'] );
        
$is_modern         = ! empty( $field['style'] ) && self::STYLE_MODERN === $field['style'];
        
$choices           $field['properties']['inputs'];

        if ( ! empty( 
$field['required'] ) ) {
            
$container['attr']['required'] = 'required';
        }

        
// If it's a multiple select.
        
if ( $is_multiple ) {
            
$container['attr']['multiple'] = 'multiple';

            
// Change a name attribute.
            
if ( ! empty( $container['attr']['name'] ) ) {
                
$container['attr']['name'] .= '[]';
            }
        }

        
// Add a class for Choices.js initialization.
        
if ( $is_modern ) {
            
$container['class'][] = 'choicesjs-select';

            
// Add a size-class to data attribute - it is used when Choices.js is initialized.
            
if ( ! empty( $field['size'] ) ) {
                
$container['data']['size-class'] = 'wpforms-field-row wpforms-field-' sanitize_html_class$field['size'] );
            }

            
$container['data']['search-enabled'] = $this->is_choicesjs_search_enabledcount$choices ) );
        }

        
$has_default false;

        
// Check to see if any of the options were selected by default.
        
foreach ( $choices as $choice ) {
            if ( ! empty( 
$choice['default'] ) ) {
                
$has_default true;
                break;
            }
        }

        
// Fake placeholder for Modern style.
        
if ( $is_modern && empty( $field_placeholder ) ) {
            
$first_choices     reset$choices );
            
$field_placeholder $first_choices['label']['text'];
        }

        
// Preselect default if no other choices were marked as default.
        
printf(
            
'<select %s>',
            
wpforms_html_attributes$container['id'], $container['class'], $container['data'], $container['attr'] )
        );

        
// Optional placeholder.
        
if ( ! empty( $field_placeholder ) ) {
            
printf(
                
'<option value="" class="placeholder" disabled %s>%s</option>',
                
selectedfalse$has_default || $is_multiplefalse ),
                
esc_html$field_placeholder )
            );
        }

        
// Build the select options.
        
foreach ( $choices as $key => $choice ) {
            
printf(
                
'<option value="%s" %s>%s</option>',
                
esc_attr$choice['attr']['value'] ),
                
selectedtrue, ! empty( $choice['default'] ), false ),
                
esc_html$choice['label']['text'] )
            );
        }

        echo 
'</select>';
    }

    
/**
     * Format and sanitize field.
     *
     * @since 1.0.2
     * @since 1.6.1 Added a support for multiple values.
     *
     * @param int          $field_id     Field ID.
     * @param string|array $field_submit Submitted field value (selected option).
     * @param array        $form_data    Form data and settings.
     */
    
public function format$field_id$field_submit$form_data ) {

        
$field    $form_data['fields'][ $field_id ];
        
$dynamic  = ! empty( $field['dynamic_choices'] ) ? $field['dynamic_choices'] : false;
        
$multiple = ! empty( $field['multiple'] );
        
$name     sanitize_text_field$field['label'] );
        
$value    = [];

        
// Convert submitted field value to array.
        
if ( ! is_array$field_submit ) ) {
            
$field_submit = array( $field_submit );
        }

        
$value_raw wpforms_sanitize_array_combine$field_submit );

        
$data = array(
            
'name'      => $name,
            
'value'     => '',
            
'value_raw' => $value_raw,
            
'id'        => absint$field_id ),
            
'type'      => $this->type,
        );

        if ( 
'post_type' === $dynamic && ! empty( $field['dynamic_post_type'] ) ) {

            
// Dynamic population is enabled using post type (like for a `Checkboxes` field).
            
$value_raw                 implode','array_map'absint'$field_submit ) );
            
$data['value_raw']         = $value_raw;
            
$data['dynamic']           = 'post_type';
            
$data['dynamic_items']     = $value_raw;
            
$data['dynamic_post_type'] = $field['dynamic_post_type'];
            
$posts                     = array();

            foreach ( 
$field_submit as $id ) {
                
$post get_post$id );

                if ( ! 
is_wp_error$post ) && ! empty( $post ) && $data['dynamic_post_type'] === $post->post_type ) {
                    
$posts[] = esc_html$post->post_title );
                }
            }

            
$data['value'] = ! empty( $posts ) ? wpforms_sanitize_array_combine$posts ) : '';

        } elseif ( 
'taxonomy' === $dynamic && ! empty( $field['dynamic_taxonomy'] ) ) {

            
// Dynamic population is enabled using taxonomy (like for a `Checkboxes` field).
            
$value_raw                implode','array_map'absint'$field_submit ) );
            
$data['value_raw']        = $value_raw;
            
$data['dynamic']          = 'taxonomy';
            
$data['dynamic_items']    = $value_raw;
            
$data['dynamic_taxonomy'] = $field['dynamic_taxonomy'];
            
$terms                    = array();

            foreach ( 
$field_submit as $id ) {
                
$term get_term$id$field['dynamic_taxonomy'] );

                if ( ! 
is_wp_error$term ) && ! empty( $term ) ) {
                    
$terms[] = esc_html$term->name );
                }
            }

            
$data['value'] = ! empty( $terms ) ? wpforms_sanitize_array_combine$terms ) : '';

        } else {

            
// Normal processing, dynamic population is off.

            // If show_values is true, that means values posted are the raw values
            // and not the labels. So we need to get the label values.
            
if ( ! empty( $field['show_values'] ) && '1' == $field['show_values'] ) {

                foreach ( 
$field_submit as $item ) {
                    foreach ( 
$field['choices'] as $choice ) {
                        if ( 
$item === $choice['value'] ) {
                            
$value[] = $choice['label'];
                            break;
                        }
                    }
                }

                
$data['value'] = ! empty( $value ) ? wpforms_sanitize_array_combine$value ) : '';

            } else {
                
$data['value'] = $value_raw;
            }
        }

        
// Backward compatibility: for single dropdown save a string, for multiple - array.
        
if ( ! $multiple && is_array$data ) && ( === count$data ) ) ) {
            
$data reset$data );
        }

        
// Push field details to be saved.
        
wpforms()->process->fields$field_id ] = $data;
    }

    
/**
     * Form frontend CSS enqueues.
     *
     * @since 1.6.1
     *
     * @param array $forms Forms on the current page.
     */
    
public function enqueue_frontend_css$forms ) {

        
$has_modern_select false;

        foreach ( 
$forms as $form ) {
            if ( 
$this->is_field_style$formself::STYLE_MODERN ) ) {
                
$has_modern_select true;

                break;
            }
        }

        if ( 
$has_modern_select || wpforms()->frontend->assets_global() ) {
            
$min = \wpforms_get_min_suffix();

            
wp_enqueue_style(
                
'wpforms-choicesjs',
                
WPFORMS_PLUGIN_URL "assets/css/choices{$min}.css",
                array(),
                
self::CHOICES_VERSION
            
);
        }
    }

    
/**
     * Form frontend JS enqueues.
     *
     * @since 1.6.1
     *
     * @param array $forms Forms on the current page.
     */
    
public function enqueue_frontend_js$forms ) {

        
$has_modern_select false;

        foreach ( 
$forms as $form ) {
            if ( 
$this->is_field_style$formself::STYLE_MODERN ) ) {
                
$has_modern_select true;

                break;
            }
        }

        if ( 
$has_modern_select || wpforms()->frontend->assets_global() ) {
            
$this->enqueue_choicesjs_once$forms );
        }
    }

    
/**
     * Whether the provided form has a dropdown field with a specified style.
     *
     * @since 1.6.1
     *
     * @param array  $form  Form data.
     * @param string $style Desired field style.
     *
     * @return bool
     */
    
protected function is_field_style$form$style ) {

        
$is_field_style false;

        if ( empty( 
$form['fields'] ) ) {

            return 
$is_field_style;
        }

        foreach ( (array) 
$form['fields'] as $field ) {

            if (
                ! empty( 
$field['type'] ) &&
                
$field['type'] === $this->type &&
                ! empty( 
$field['style'] ) &&
                
sanitize_key$style ) === $field['style']
            ) {
                
$is_field_style true;
                break;
            }
        }

        return 
$is_field_style;
    }

    
/**
     * Get field name for ajax error message.
     *
     * @since 1.6.3
     *
     * @param string $name  Field name for error triggered.
     * @param array  $field Field settings.
     * @param array  $props List of properties.
     * @param string $error Error message.
     *
     * @return string
     */
    
public function ajax_error_field_name$name$field$props$error ) {

        if ( ! isset( 
$field['type'] ) || 'select' !== $field['type'] ) {
            return 
$name;
        }
        if ( ! empty( 
$field['multiple'] ) ) {
            
$input = isset( $props['inputs'] ) ? end$props['inputs'] ) : [];

            return isset( 
$input['attr']['name'] ) ? $input['attr']['name'] . '[]' '';
        }

        return 
$name;
    }
}

new 
WPForms_Field_Select();
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586