PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.14
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.14
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 6.2.14, at app/Services/FormBuilder/Components/Select.php

190 lines 7.0 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_deprecated(
22 'fluentform_rendering_field_data_' . $elementName,
23 [
24 $data,
25 $form
26 ],
27 FLUENTFORM_FRAMEWORK_UPGRADE,
28 'fluentform/rendering_field_data_' . $elementName,
29 'Use fluentform/rendering_field_data_' . $elementName . ' instead of fluentform_rendering_field_data_' . $elementName
30 );
31 $data = apply_filters('fluentform/rendering_field_data_' . $elementName, $data, $form);
32
33 $data['attributes']['id'] = $this->makeElementId($data, $form);
34
35 $isMulti = 'yes' == ArrayHelper::get($data, 'settings.enable_select_2');
36
37 if (ArrayHelper::get($data['attributes'], 'multiple')) {
38 $data['attributes']['name'] = $data['attributes']['name'] . '[]';
39 wp_enqueue_script('choices');
40 wp_enqueue_style('ff_choices');
41 $data['attributes']['class'] .= ' ff_has_multi_select';
42 } elseif ($isMulti) {
43 wp_enqueue_script('choices');
44 wp_enqueue_style('ff_choices');
45 $data['attributes']['class'] .= ' ff_has_multi_select';
46 }
47
48 $maxSelection = Helper::resolveMaxSelection($data);
49
50 if ($maxSelection) {
51 $data['attributes']['data-max_selected_options'] = $maxSelection;
52
53 // Choices.js blocks the pick and draws its own notice, so that notice
54 // is the only wording a visitor ever sees for this limit. Only a
55 // message the site owner actually wrote overrides it — Choices' own
56 // string is translated and carries the count, so a field left on the
57 // global default is better served by it.
58 $rules = ArrayHelper::get($data, 'settings.validation_rules', []);
59 $customMessage = ArrayHelper::isTrue($rules, 'max_selection.global')
60 ? ''
61 : ArrayHelper::get($rules, 'max_selection.message', '');
62
63 if ($customMessage) {
64 $data['attributes']['data-max_selection_message'] = $customMessage;
65 }
66 }
67
68 $data['attributes']['data-calc_value'] = 0;
69
70 if (! isset($data['attributes']['class'])) {
71 $data['attributes']['class'] = '';
72 }
73
74 $data['attributes']['class'] = trim('ff-el-form-control ' . $data['attributes']['class']);
75
76 if ($tabIndex = Helper::getNextTabIndex()) {
77 $data['attributes']['tabindex'] = $tabIndex;
78 }
79
80 $defaultValues = (array) $this->extractValueFromAttributes($data);
81
82 if ($dynamicValues = $this->extractDynamicValues($data, $form)) {
83 $defaultValues = $dynamicValues;
84 }
85
86 $atts = $this->buildAttributes($data['attributes']);
87 $options = $this->buildOptions($data, $defaultValues);
88
89 $ariaRequired = 'false';
90 if (ArrayHelper::get($data, 'settings.validation_rules.required.value')) {
91 $ariaRequired = 'true';
92 }
93
94 $ariaLabelledBy = esc_attr('label_' . ArrayHelper::get($data, 'attributes.id'));
95
96 $elMarkup = '<select ' . $atts . ' aria-invalid="false" aria-required="' . $ariaRequired . '" aria-labelledby="' . $ariaLabelledBy .'"'.'>' . $options . '</select>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $atts, $options are escaped before being passed in.
97
98 $html = $this->buildElementMarkup($elMarkup, $data, $form);
99
100 $html = apply_filters_deprecated(
101 'fluentform_rendering_field_html_' . $elementName,
102 [
103 $html,
104 $data,
105 $form
106 ],
107 FLUENTFORM_FRAMEWORK_UPGRADE,
108 'fluentform/rendering_field_html_' . $elementName,
109 'Use fluentform/rendering_field_html_' . $elementName . ' instead of fluentform_rendering_field_html_' . $elementName
110 );
111
112 $this->printContent('fluentform/rendering_field_html_' . $elementName, $html, $data, $form);
113 }
114
115 /**
116 * Build options for select
117 *
118 * @param array $options
119 *
120 * @return string/html [compiled options]
121 */
122 protected function buildOptions($data, $defaultValues)
123 {
124 if (! $formattedOptions = ArrayHelper::get($data, 'settings.advanced_options')) {
125 $options = ArrayHelper::get($data, 'options', []);
126 $formattedOptions = [];
127 foreach ($options as $value => $label) {
128 $formattedOptions[] = [
129 'label' => $label,
130 'value' => $value,
131 'calc_value' => '',
132 ];
133 }
134 }
135
136 if ('yes' == ArrayHelper::get($data, 'settings.randomize_options')) {
137 shuffle($formattedOptions);
138 }
139
140 $opts = '';
141 if (! empty($data['settings']['placeholder'])) {
142 $opts .= '<option value="">' . wp_strip_all_tags($data['settings']['placeholder']) . '</option>';
143 } elseif (! empty($data['attributes']['placeholder'])) {
144 $opts .= '<option value="">' . wp_strip_all_tags($data['attributes']['placeholder']) . '</option>';
145 }
146 foreach ($formattedOptions as $option) {
147 if (Helper::isOptionGroup($option)) {
148 $groupLabel = wp_strip_all_tags(ArrayHelper::get($option, 'label', ''));
149 $groupOptions = $this->buildOptionMarkup(
150 ArrayHelper::get($option, 'options', []),
151 $defaultValues
152 );
153
154 if ($groupOptions) {
155 $opts .= '<optgroup label="' . esc_attr($groupLabel) . '">' . $groupOptions . '</optgroup>';
156 }
157 continue;
158 }
159
160 $opts .= $this->buildOptionMarkup([$option], $defaultValues);
161 }
162
163 return $opts;
164 }
165
166 protected function buildOptionMarkup($formattedOptions, $defaultValues)
167 {
168 $opts = '';
169
170 foreach ($formattedOptions as $option) {
171 if (in_array($option['value'], $defaultValues)) {
172 $selected = 'selected';
173 } else {
174 $selected = '';
175 }
176
177 $atts = [
178 'data-calc_value' => ArrayHelper::get($option, 'calc_value'),
179 'data-custom-properties' => ArrayHelper::get($option, 'calc_value'),
180 'value' => ArrayHelper::get($option, 'value'),
181 'disabled' => ArrayHelper::get($option, 'disabled') ? 'disabled' : ''
182 ];
183
184 $opts .= '<option '. $this->buildAttributes($atts) . " {$selected}>" . wp_strip_all_tags($option['label']) . '</option>';
185 }
186
187 return $opts;
188 }
189 }
190