← All changes
|
app/Services/FormBuilder/Components/SelectCountry.php
+76
-30
3.6.51
→
6.2.14
View file →
| @@ -1,9 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentForm\App\Services\FormBuilder\Components; |
| 4 | 4 | |
| 5 | -use FluentForm\App; | |
| 6 | 5 | use FluentForm\App\Helpers\Helper; |
| 7 | 6 | use FluentForm\Framework\Helpers\ArrayHelper; |
| 8 | 7 | |
| 9 | 8 | class SelectCountry extends BaseComponent |
| @@ -9,23 +8,41 @@ | ||
| 9 | 8 | class SelectCountry extends BaseComponent |
| 10 | 9 | { |
| 11 | 10 | /** |
| 12 | 11 | * Compile and echo the html element |
| 13 | - * @param array $data [element data] | |
| 14 | - * @param stdClass $form [Form Object] | |
| 15 | - * @return viod | |
| 12 | + * | |
| 13 | + * @param array $data [element data] | |
| 14 | + * @param \stdClass $form [Form Object] | |
| 15 | + * | |
| 16 | + * @return void | |
| 16 | 17 | */ |
| 17 | 18 | public function compile($data, $form) |
| 18 | 19 | { |
| 19 | 20 | $elementName = $data['element']; |
| 20 | - $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); | |
| 21 | 32 | |
| 22 | 33 | $data = $this->loadCountries($data); |
| 23 | - $defaultValues = (array)$this->extractValueFromAttributes($data); | |
| 24 | - $data['attributes']['class'] = trim('ff-el-form-control ' . $data['attributes']['class']); | |
| 34 | + $defaultValues = (array) $this->extractValueFromAttributes($data); | |
| 35 | + $data['attributes']['class'] = trim('ff-el-form-control ' . ArrayHelper::get($data, 'attributes.class', '')); | |
| 25 | 36 | $data['attributes']['id'] = $this->makeElementId($data, $form); |
| 37 | + $isSearchable = ArrayHelper::get($data, 'settings.enable_select_2'); | |
| 38 | + if ('yes' == $isSearchable) { | |
| 39 | + wp_enqueue_script('choices'); | |
| 40 | + wp_enqueue_style('ff_choices'); | |
| 41 | + $data['attributes']['class'] .= ' ff_has_multi_select'; | |
| 42 | + } | |
| 26 | 43 | |
| 27 | - if ($tabIndex = \FluentForm\App\Helpers\Helper::getNextTabIndex()) { | |
| 44 | + if ($tabIndex = Helper::getNextTabIndex()) { | |
| 28 | 45 | $data['attributes']['tabindex'] = $tabIndex; |
| 29 | 46 | } |
| 30 | 47 | |
| 31 | 48 | $placeholder = ArrayHelper::get($data, 'attributes.placeholder'); |
| @@ -31,48 +48,69 @@ | ||
| 31 | 48 | $placeholder = ArrayHelper::get($data, 'attributes.placeholder'); |
| 32 | 49 | |
| 33 | 50 | $activeList = ArrayHelper::get($data, 'settings.country_list.active_list'); |
| 34 | 51 | |
| 35 | - $elMarkup = "<select " . $this->buildAttributes($data['attributes']) . "><option value=''>".$placeholder."</option>"; | |
| 52 | + $ariaRequired = 'false'; | |
| 53 | + if (ArrayHelper::get($data, 'settings.validation_rules.required.value')) { | |
| 54 | + $ariaRequired = 'true'; | |
| 55 | + } | |
| 36 | 56 | |
| 37 | - if ($activeList == 'priority_based') { | |
| 57 | + $elMarkup = '<select ' . $this->buildAttributes($data['attributes']) . "aria-invalid='false' aria-required=$ariaRequired><option value=''>" . wp_strip_all_tags($placeholder) . '</option>'; | |
| 58 | + | |
| 59 | + if ('priority_based' == $activeList) { | |
| 38 | 60 | $selectCountries = ArrayHelper::get($data, 'settings.country_list.priority_based', []); |
| 39 | 61 | $priorityCountries = $this->getSelectedCountries($selectCountries); |
| 40 | 62 | $primaryListLabel = ArrayHelper::get($data, 'settings.primary_label'); |
| 41 | 63 | $otherListLabel = ArrayHelper::get($data, 'settings.other_label'); |
| 42 | - $elMarkup .= '<optgroup label="'.$primaryListLabel.'">'; | |
| 43 | - $elMarkup .= $this->buildOptions($priorityCountries, $defaultValues); | |
| 44 | - $elMarkup .= '</optgroup><optgroup label="'.$otherListLabel.'">'; | |
| 45 | - $elMarkup .= $this->buildOptions($data['options'], $defaultValues); | |
| 64 | + // SECURITY (FINDING-12): esc_attr (not just strip_all_tags, which leaves quotes) the | |
| 65 | + // optgroup labels before interpolating them into the double-quoted label attribute. | |
| 66 | + $elMarkup .= '<optgroup label="' . esc_attr($primaryListLabel) . '">'; | |
| 67 | + $elMarkup .= $this->buildOptions($priorityCountries, $defaultValues); | |
| 68 | + $elMarkup .= '</optgroup><optgroup label="' . esc_attr($otherListLabel) . '">'; | |
| 69 | + $elMarkup .= $this->buildOptions($data['options'], $defaultValues); | |
| 46 | 70 | $elMarkup .= '</optgroup>'; |
| 47 | 71 | } else { |
| 48 | - $elMarkup .= $this->buildOptions($data['options'], $defaultValues); | |
| 72 | + $elMarkup .= $this->buildOptions($data['options'], $defaultValues); | |
| 49 | 73 | } |
| 50 | 74 | |
| 51 | - $elMarkup .= "</select>"; | |
| 75 | + $elMarkup .= '</select>'; | |
| 52 | 76 | |
| 53 | 77 | $html = $this->buildElementMarkup($elMarkup, $data, $form); |
| 54 | - echo apply_filters('fluenform_rendering_field_html_' . $elementName, $html, $data, $form); | |
| 78 | + | |
| 79 | + $html = apply_filters_deprecated( | |
| 80 | + 'fluentform_rendering_field_html_' . $elementName, | |
| 81 | + [ | |
| 82 | + $html, | |
| 83 | + $data, | |
| 84 | + $form | |
| 85 | + ], | |
| 86 | + FLUENTFORM_FRAMEWORK_UPGRADE, | |
| 87 | + 'fluentform/rendering_field_html_' . $elementName, | |
| 88 | + 'Use fluentform/rendering_field_html_' . $elementName . ' instead of fluentform_rendering_field_html_' . $elementName | |
| 89 | + ); | |
| 90 | + | |
| 91 | + $this->printContent('fluentform/rendering_field_html_' . $elementName, $html, $data, $form); | |
| 55 | 92 | } |
| 56 | - | |
| 57 | 93 | /** |
| 58 | 94 | * Load countt list from file |
| 95 | + * | |
| 59 | 96 | * @param array $data |
| 97 | + * | |
| 60 | 98 | * @return array |
| 61 | 99 | */ |
| 62 | - protected function loadCountries($data) | |
| 100 | + public function loadCountries($data) | |
| 63 | 101 | { |
| 64 | - $app = App::make(); | |
| 65 | - $data['options'] = array(); | |
| 102 | + $app = wpFluentForm(); | |
| 103 | + $data['options'] = []; | |
| 66 | 104 | $activeList = ArrayHelper::get($data, 'settings.country_list.active_list'); |
| 67 | - $countries = $app->load($app->appPath('Services/FormBuilder/CountryNames.php')); | |
| 105 | + $countries = getFluentFormCountryList(); | |
| 68 | 106 | |
| 69 | - if ($activeList == 'visible_list') { | |
| 107 | + if ('visible_list' == $activeList) { | |
| 70 | 108 | $selectCountries = ArrayHelper::get($data, 'settings.country_list.' . $activeList, []); |
| 71 | 109 | foreach ($selectCountries as $value) { |
| 72 | 110 | $data['options'][$value] = $countries[$value]; |
| 73 | 111 | } |
| 74 | - } elseif ($activeList == 'hidden_list' || $activeList == 'priority_based') { | |
| 112 | + } elseif ('hidden_list' == $activeList || 'priority_based' == $activeList) { | |
| 75 | 113 | $data['options'] = $countries; |
| 76 | 114 | $selectCountries = ArrayHelper::get($data, 'settings.country_list.' . $activeList, []); |
| 77 | 115 | foreach ($selectCountries as $value) { |
| 78 | 116 | unset($data['options'][$value]); |
| @@ -80,14 +118,22 @@ | ||
| 80 | 118 | } else { |
| 81 | 119 | $data['options'] = $countries; |
| 82 | 120 | } |
| 83 | 121 | |
| 122 | + $selectedCountries = $data['options']; | |
| 123 | + $selectedCountries = array_flip($selectedCountries); | |
| 124 | + ksort($selectedCountries); | |
| 125 | + $selectedCountries = array_flip($selectedCountries); | |
| 126 | + $data['options'] = $selectedCountries; | |
| 127 | + | |
| 84 | 128 | return $data; |
| 85 | 129 | } |
| 86 | 130 | |
| 87 | 131 | /** |
| 88 | 132 | * Build options for country list/select |
| 133 | + * | |
| 89 | 134 | * @param array $options |
| 135 | + * | |
| 90 | 136 | * @return string/html [compiled options] |
| 91 | 137 | */ |
| 92 | 138 | protected function buildOptions($options, $defaultValues = []) |
| 93 | 139 | { |
| @@ -97,22 +143,22 @@ | ||
| 97 | 143 | $selected = 'selected'; |
| 98 | 144 | } else { |
| 99 | 145 | $selected = ''; |
| 100 | 146 | } |
| 101 | - $opts .= "<option value='{$value}' {$selected}>{$label}</option>"; | |
| 147 | + $opts .= "<option value='" . esc_attr($value) . "' {$selected}>" . esc_attr($label) . '</option>'; | |
| 102 | 148 | } |
| 103 | 149 | return $opts; |
| 104 | 150 | } |
| 105 | 151 | |
| 106 | - protected function getSelectedCountries($keys = []) | |
| 152 | + public function getSelectedCountries($keys = []) | |
| 107 | 153 | { |
| 108 | - $app = App::make(); | |
| 109 | 154 | $options = []; |
| 110 | - $countries = $app->load($app->appPath('Services/FormBuilder/CountryNames.php')); | |
| 155 | + $countries = getFluentFormCountryList(); | |
| 111 | 156 | foreach ($keys as $value) { |
| 112 | 157 | $options[$value] = $countries[$value]; |
| 113 | 158 | } |
| 114 | 159 | |
| 115 | - return $options; | |
| 160 | + $options = array_flip($options); | |
| 161 | + ksort($options); | |
| 162 | + return array_flip($options); | |
| 116 | 163 | } |
| 117 | - | |
| 118 | 164 | } |