PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.18
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.18
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Services / FormBuilder / Components / Select.php

Select.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 4.3.18, at app/Services/FormBuilder/Components/Select.php

121 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Services\FormBuilder\Components;
4
5 use FluentForm\App\Helpers\Helper;
6 use FluentForm\Framework\Helpers\ArrayHelper;
7
8 class Select extends BaseComponent
9 {
10 /**
11 * Compile and echo the html element
12 *
13 * @param array $data [element data]
14 * @param \stdClass $form [Form Object]
15 *
16 * @return void
17 */
18 public function compile($data, $form)
19 {
20 $elementName = $data['element'];
21 $data = apply_filters('fluentform_rendering_field_data_' . $elementName, $data, $form);
22
23 $data['attributes']['id'] = $this->makeElementId($data, $form);
24
25 $isMulti = 'yes' == ArrayHelper::get($data, 'settings.enable_select_2');
26
27 if (ArrayHelper::get($data['attributes'], 'multiple')) {
28 $data['attributes']['name'] = $data['attributes']['name'] . '[]';
29 wp_enqueue_script('choices');
30 wp_enqueue_style('ff_choices');
31 $data['attributes']['class'] .= ' ff_has_multi_select';
32 } elseif ($isMulti) {
33 wp_enqueue_script('choices');
34 wp_enqueue_style('ff_choices');
35 $data['attributes']['class'] .= ' ff_has_multi_select';
36 }
37
38 if ($maxSelection = ArrayHelper::get($data, 'settings.max_selection')) {
39 $data['attributes']['data-max_selected_options'] = $maxSelection;
40 }
41
42 $data['attributes']['data-calc_value'] = 0;
43
44 if (! isset($data['attributes']['class'])) {
45 $data['attributes']['class'] = '';
46 }
47
48 $data['attributes']['class'] = trim('ff-el-form-control ' . $data['attributes']['class']);
49
50 if ($tabIndex = Helper::getNextTabIndex()) {
51 $data['attributes']['tabindex'] = $tabIndex;
52 }
53
54 $defaultValues = (array) $this->extractValueFromAttributes($data);
55
56 if ($dynamicValues = $this->extractDynamicValues($data, $form)) {
57 $defaultValues = $dynamicValues;
58 }
59
60 $atts = $this->buildAttributes($data['attributes']);
61 $options = $this->buildOptions($data, $defaultValues);
62
63 $elMarkup = '<select ' . $atts . '>' . $options . '</select>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $atts, $options are escaped before being passed in.
64
65 $html = $this->buildElementMarkup($elMarkup, $data, $form);
66
67 $this->printContent('fluentform_rendering_field_html_' . $elementName, $html, $data, $form);
68 }
69
70 /**
71 * Build options for select
72 *
73 * @param array $options
74 *
75 * @return string/html [compiled options]
76 */
77 protected function buildOptions($data, $defaultValues)
78 {
79 if (! $formattedOptions = ArrayHelper::get($data, 'settings.advanced_options')) {
80 $options = ArrayHelper::get($data, 'options', []);
81 $formattedOptions = [];
82 foreach ($options as $value => $label) {
83 $formattedOptions[] = [
84 'label' => $label,
85 'value' => $value,
86 'calc_value' => '',
87 ];
88 }
89 }
90
91 if ('yes' == ArrayHelper::get($data, 'settings.randomize_options')) {
92 shuffle($formattedOptions);
93 }
94
95 $opts = '';
96 if (! empty($data['settings']['placeholder'])) {
97 $opts .= '<option value="">' . wp_strip_all_tags($data['settings']['placeholder']) . '</option>';
98 } elseif (! empty($data['attributes']['placeholder'])) {
99 $opts .= '<option value="">' . wp_strip_all_tags($data['attributes']['placeholder']) . '</option>';
100 }
101
102 foreach ($formattedOptions as $option) {
103 if (in_array($option['value'], $defaultValues)) {
104 $selected = 'selected';
105 } else {
106 $selected = '';
107 }
108
109 $atts = [
110 'data-calc_value' => ArrayHelper::get($option, 'calc_value'),
111 'data-custom-properties' => ArrayHelper::get($option, 'calc_value'),
112 'value' => ArrayHelper::get($option, 'value'),
113 ];
114
115 $opts .= '<option ' . $this->buildAttributes($atts) . " {$selected}>" . wp_strip_all_tags($option['label']) . '</option>';
116 }
117
118 return $opts;
119 }
120 }
121