| @@ -8,20 +8,32 @@ | ||
| 8 | 8 | class Select extends BaseComponent |
| 9 | 9 | { |
| 10 | 10 | /** |
| 11 | 11 | * Compile and echo the html element |
| 12 | - * @param array $data [element data] | |
| 13 | - * @param stdClass $form [Form Object] | |
| 14 | - * @return viod | |
| 12 | + * | |
| 13 | + * @param array $data [element data] | |
| 14 | + * @param \stdClass $form [Form Object] | |
| 15 | + * | |
| 16 | + * @return void | |
| 15 | 17 | */ |
| 16 | 18 | public function compile($data, $form) |
| 17 | 19 | { |
| 18 | 20 | $elementName = $data['element']; |
| 19 | - $data = apply_filters('fluenform_rendering_field_data_' . $elementName, $data, $form); | |
| 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); | |
| 20 | 32 | |
| 21 | 33 | $data['attributes']['id'] = $this->makeElementId($data, $form); |
| 22 | 34 | |
| 23 | - $isMulti = ArrayHelper::get($data, 'settings.enable_select_2') == 'yes'; | |
| 35 | + $isMulti = 'yes' == ArrayHelper::get($data, 'settings.enable_select_2'); | |
| 24 | 36 | |
| 25 | 37 | if (ArrayHelper::get($data['attributes'], 'multiple')) { |
| 26 | 38 | $data['attributes']['name'] = $data['attributes']['name'] . '[]'; |
| 27 | 39 | wp_enqueue_script('choices'); |
| @@ -26,50 +38,91 @@ | ||
| 26 | 38 | $data['attributes']['name'] = $data['attributes']['name'] . '[]'; |
| 27 | 39 | wp_enqueue_script('choices'); |
| 28 | 40 | wp_enqueue_style('ff_choices'); |
| 29 | 41 | $data['attributes']['class'] .= ' ff_has_multi_select'; |
| 30 | - } else if ($isMulti) { | |
| 42 | + } elseif ($isMulti) { | |
| 31 | 43 | wp_enqueue_script('choices'); |
| 32 | 44 | wp_enqueue_style('ff_choices'); |
| 33 | 45 | $data['attributes']['class'] .= ' ff_has_multi_select'; |
| 34 | 46 | } |
| 35 | 47 | |
| 36 | - if ($maxSelection = ArrayHelper::get($data, 'settings.max_selection')) { | |
| 48 | + $maxSelection = Helper::resolveMaxSelection($data); | |
| 49 | + | |
| 50 | + if ($maxSelection) { | |
| 37 | 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 | + } | |
| 38 | 66 | } |
| 39 | 67 | |
| 40 | 68 | $data['attributes']['data-calc_value'] = 0; |
| 41 | 69 | |
| 42 | - if (!isset($data['attributes']['class'])) { | |
| 70 | + if (! isset($data['attributes']['class'])) { | |
| 43 | 71 | $data['attributes']['class'] = ''; |
| 44 | 72 | } |
| 45 | 73 | |
| 46 | 74 | $data['attributes']['class'] = trim('ff-el-form-control ' . $data['attributes']['class']); |
| 47 | 75 | |
| 48 | - if ($tabIndex = \FluentForm\App\Helpers\Helper::getNextTabIndex()) { | |
| 76 | + if ($tabIndex = Helper::getNextTabIndex()) { | |
| 49 | 77 | $data['attributes']['tabindex'] = $tabIndex; |
| 50 | 78 | } |
| 51 | 79 | |
| 52 | - $defaultValues = (array)$this->extractValueFromAttributes($data); | |
| 80 | + $defaultValues = (array) $this->extractValueFromAttributes($data); | |
| 53 | 81 | |
| 54 | 82 | if ($dynamicValues = $this->extractDynamicValues($data, $form)) { |
| 55 | 83 | $defaultValues = $dynamicValues; |
| 56 | 84 | } |
| 57 | 85 | |
| 58 | - $elMarkup = "<select " . $this->buildAttributes($data['attributes']) . ">" . $this->buildOptions($data, $defaultValues) . "</select>"; | |
| 86 | + $atts = $this->buildAttributes($data['attributes']); | |
| 87 | + $options = $this->buildOptions($data, $defaultValues); | |
| 59 | 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 | + | |
| 60 | 98 | $html = $this->buildElementMarkup($elMarkup, $data, $form); |
| 61 | - echo apply_filters('fluenform_rendering_field_html_' . $elementName, $html, $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); | |
| 62 | 113 | } |
| 63 | 114 | |
| 64 | 115 | /** |
| 65 | 116 | * Build options for select |
| 117 | + * | |
| 66 | 118 | * @param array $options |
| 119 | + * | |
| 67 | 120 | * @return string/html [compiled options] |
| 68 | 121 | */ |
| 69 | 122 | protected function buildOptions($data, $defaultValues) |
| 70 | 123 | { |
| 71 | - if (!$formattedOptions = ArrayHelper::get($data, 'settings.advanced_options')) { | |
| 124 | + if (! $formattedOptions = ArrayHelper::get($data, 'settings.advanced_options')) { | |
| 72 | 125 | $options = ArrayHelper::get($data, 'options', []); |
| 73 | 126 | $formattedOptions = []; |
| 74 | 127 | foreach ($options as $value => $label) { |
| 75 | 128 | $formattedOptions[] = [ |
| @@ -74,24 +127,47 @@ | ||
| 74 | 127 | foreach ($options as $value => $label) { |
| 75 | 128 | $formattedOptions[] = [ |
| 76 | 129 | 'label' => $label, |
| 77 | 130 | 'value' => $value, |
| 78 | - 'calc_value' => '' | |
| 131 | + 'calc_value' => '', | |
| 79 | 132 | ]; |
| 80 | 133 | } |
| 81 | 134 | } |
| 82 | 135 | |
| 83 | - if(ArrayHelper::get($data, 'settings.randomize_options') == 'yes') { | |
| 136 | + if ('yes' == ArrayHelper::get($data, 'settings.randomize_options')) { | |
| 84 | 137 | shuffle($formattedOptions); |
| 85 | 138 | } |
| 86 | 139 | |
| 87 | 140 | $opts = ''; |
| 88 | - if (!empty($data['settings']['placeholder'])) { | |
| 89 | - $opts .= '<option value="">' . $data['settings']['placeholder'] . '</option>'; | |
| 90 | - } else if (!empty($data['attributes']['placeholder'])) { | |
| 91 | - $opts .= '<option value="">' . $data['attributes']['placeholder'] . '</option>'; | |
| 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>'; | |
| 92 | 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 | + ); | |
| 93 | 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 | + | |
| 94 | 170 | foreach ($formattedOptions as $option) { |
| 95 | 171 | if (in_array($option['value'], $defaultValues)) { |
| 96 | 172 | $selected = 'selected'; |
| 97 | 173 | } else { |
| @@ -96,16 +172,17 @@ | ||
| 96 | 172 | $selected = 'selected'; |
| 97 | 173 | } else { |
| 98 | 174 | $selected = ''; |
| 99 | 175 | } |
| 100 | - | |
| 176 | + | |
| 101 | 177 | $atts = [ |
| 102 | 178 | 'data-calc_value' => ArrayHelper::get($option, 'calc_value'), |
| 103 | 179 | 'data-custom-properties' => ArrayHelper::get($option, 'calc_value'), |
| 104 | 180 | 'value' => ArrayHelper::get($option, 'value'), |
| 181 | + 'disabled' => ArrayHelper::get($option, 'disabled') ? 'disabled' : '' | |
| 105 | 182 | ]; |
| 106 | - | |
| 107 | - $opts .= "<option " . $this->buildAttributes($atts) . " {$selected}>{$option['label']}</option>"; | |
| 183 | + | |
| 184 | + $opts .= '<option '. $this->buildAttributes($atts) . " {$selected}>" . wp_strip_all_tags($option['label']) . '</option>'; | |
| 108 | 185 | } |
| 109 | 186 | |
| 110 | 187 | return $opts; |
| 111 | 188 | } |