| @@ -6,93 +6,184 @@ | ||
| 6 | 6 | use FluentForm\Framework\Helpers\ArrayHelper; |
| 7 | 7 | |
| 8 | 8 | class Select extends BaseComponent |
| 9 | 9 | { |
| 10 | - /** | |
| 11 | - * Compile and echo the html element | |
| 12 | - * @param array $data [element data] | |
| 13 | - * @param stdClass $form [Form Object] | |
| 14 | - * @return viod | |
| 15 | - */ | |
| 16 | - public function compile($data, $form) | |
| 17 | - { | |
| 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 | + { | |
| 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 | - if (ArrayHelper::get($data['attributes'], 'multiple')) { | |
| 26 | - $data['attributes']['name'] = $data['attributes']['name'].'[]'; | |
| 27 | - wp_enqueue_script('choices'); | |
| 28 | - wp_enqueue_style('ff_choices'); | |
| 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'); | |
| 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 | |
| 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 | + | |
| 36 | 68 | $data['attributes']['data-calc_value'] = 0; |
| 37 | 69 | |
| 38 | - if(!isset($data['attributes']['class'])) { | |
| 70 | + if (! isset($data['attributes']['class'])) { | |
| 39 | 71 | $data['attributes']['class'] = ''; |
| 40 | 72 | } |
| 41 | 73 | |
| 42 | 74 | $data['attributes']['class'] = trim('ff-el-form-control ' . $data['attributes']['class']); |
| 43 | 75 | |
| 44 | - if($tabIndex = \FluentForm\App\Helpers\Helper::getNextTabIndex()) { | |
| 76 | + if ($tabIndex = Helper::getNextTabIndex()) { | |
| 45 | 77 | $data['attributes']['tabindex'] = $tabIndex; |
| 46 | 78 | } |
| 47 | 79 | |
| 48 | 80 | $defaultValues = (array) $this->extractValueFromAttributes($data); |
| 49 | 81 | |
| 50 | - $elMarkup = "<select ".$this->buildAttributes($data['attributes']).">".$this->buildOptions($data, $defaultValues)."</select>"; | |
| 82 | + if ($dynamicValues = $this->extractDynamicValues($data, $form)) { | |
| 83 | + $defaultValues = $dynamicValues; | |
| 84 | + } | |
| 51 | 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 | + | |
| 52 | 98 | $html = $this->buildElementMarkup($elMarkup, $data, $form); |
| 53 | - 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); | |
| 54 | 113 | } |
| 55 | 114 | |
| 56 | - /** | |
| 57 | - * Build options for select | |
| 58 | - * @param array $options | |
| 59 | - * @return string/html [compiled options] | |
| 60 | - */ | |
| 61 | - protected function buildOptions($data, $defaultValues) | |
| 62 | - { | |
| 63 | - if(!$formattedOptions = ArrayHelper::get($data, 'settings.advanced_options')) { | |
| 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')) { | |
| 64 | 125 | $options = ArrayHelper::get($data, 'options', []); |
| 65 | 126 | $formattedOptions = []; |
| 66 | - foreach($options as $value => $label) { | |
| 127 | + foreach ($options as $value => $label) { | |
| 67 | 128 | $formattedOptions[] = [ |
| 68 | - 'label' => $label, | |
| 69 | - 'value' => $value, | |
| 70 | - 'calc_value' => '' | |
| 129 | + 'label' => $label, | |
| 130 | + 'value' => $value, | |
| 131 | + 'calc_value' => '', | |
| 71 | 132 | ]; |
| 72 | 133 | } |
| 73 | 134 | } |
| 74 | 135 | |
| 136 | + if ('yes' == ArrayHelper::get($data, 'settings.randomize_options')) { | |
| 137 | + shuffle($formattedOptions); | |
| 138 | + } | |
| 139 | + | |
| 75 | 140 | $opts = ''; |
| 76 | - if (!empty($data['settings']['placeholder'])) { | |
| 77 | - $opts .= '<option value="">'.$data['settings']['placeholder'].'</option>'; | |
| 78 | - } else if(!empty($data['attributes']['placeholder'])) { | |
| 79 | - $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>'; | |
| 80 | 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 | + ); | |
| 81 | 153 | |
| 82 | - foreach ($formattedOptions as $option) { | |
| 83 | - if(in_array($option['value'], $defaultValues)) { | |
| 84 | - $selected = 'selected'; | |
| 85 | - } else { | |
| 86 | - $selected = ''; | |
| 87 | - } | |
| 154 | + if ($groupOptions) { | |
| 155 | + $opts .= '<optgroup label="' . esc_attr($groupLabel) . '">' . $groupOptions . '</optgroup>'; | |
| 156 | + } | |
| 157 | + continue; | |
| 158 | + } | |
| 88 | 159 | |
| 89 | - $atts = [ | |
| 90 | - 'data-calc_value' => ArrayHelper::get($option, 'calc_value'), | |
| 91 | - 'value' => ArrayHelper::get($option, 'value'), | |
| 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' : '' | |
| 92 | 182 | ]; |
| 183 | + | |
| 184 | + $opts .= '<option '. $this->buildAttributes($atts) . " {$selected}>" . wp_strip_all_tags($option['label']) . '</option>'; | |
| 185 | + } | |
| 93 | 186 | |
| 94 | - $opts .="<option ".$this->buildAttributes($atts)." {$selected}>{$option['label']}</option>"; | |
| 95 | - } | |
| 96 | - return $opts; | |
| 97 | - } | |
| 187 | + return $opts; | |
| 188 | + } | |
| 98 | 189 | } |