PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.12
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.12
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 / SelectCountry.php

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

165 lines 6.1 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 SelectCountry 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 = $this->loadCountries($data);
34 $defaultValues = (array) $this->extractValueFromAttributes($data);
35 $data['attributes']['class'] = trim('ff-el-form-control ' . ArrayHelper::get($data, 'attributes.class', ''));
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 }
43
44 if ($tabIndex = Helper::getNextTabIndex()) {
45 $data['attributes']['tabindex'] = $tabIndex;
46 }
47
48 $placeholder = ArrayHelper::get($data, 'attributes.placeholder');
49
50 $activeList = ArrayHelper::get($data, 'settings.country_list.active_list');
51
52 $ariaRequired = 'false';
53 if (ArrayHelper::get($data, 'settings.validation_rules.required.value')) {
54 $ariaRequired = 'true';
55 }
56
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) {
60 $selectCountries = ArrayHelper::get($data, 'settings.country_list.priority_based', []);
61 $priorityCountries = $this->getSelectedCountries($selectCountries);
62 $primaryListLabel = ArrayHelper::get($data, 'settings.primary_label');
63 $otherListLabel = ArrayHelper::get($data, 'settings.other_label');
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);
70 $elMarkup .= '</optgroup>';
71 } else {
72 $elMarkup .= $this->buildOptions($data['options'], $defaultValues);
73 }
74
75 $elMarkup .= '</select>';
76
77 $html = $this->buildElementMarkup($elMarkup, $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);
92 }
93 /**
94 * Load countt list from file
95 *
96 * @param array $data
97 *
98 * @return array
99 */
100 public function loadCountries($data)
101 {
102 $app = wpFluentForm();
103 $data['options'] = [];
104 $activeList = ArrayHelper::get($data, 'settings.country_list.active_list');
105 $countries = getFluentFormCountryList();
106
107 if ('visible_list' == $activeList) {
108 $selectCountries = ArrayHelper::get($data, 'settings.country_list.' . $activeList, []);
109 foreach ($selectCountries as $value) {
110 $data['options'][$value] = $countries[$value];
111 }
112 } elseif ('hidden_list' == $activeList || 'priority_based' == $activeList) {
113 $data['options'] = $countries;
114 $selectCountries = ArrayHelper::get($data, 'settings.country_list.' . $activeList, []);
115 foreach ($selectCountries as $value) {
116 unset($data['options'][$value]);
117 }
118 } else {
119 $data['options'] = $countries;
120 }
121
122 $selectedCountries = $data['options'];
123 $selectedCountries = array_flip($selectedCountries);
124 ksort($selectedCountries);
125 $selectedCountries = array_flip($selectedCountries);
126 $data['options'] = $selectedCountries;
127
128 return $data;
129 }
130
131 /**
132 * Build options for country list/select
133 *
134 * @param array $options
135 *
136 * @return string/html [compiled options]
137 */
138 protected function buildOptions($options, $defaultValues = [])
139 {
140 $opts = '';
141 foreach ($options as $value => $label) {
142 if (in_array($value, $defaultValues)) {
143 $selected = 'selected';
144 } else {
145 $selected = '';
146 }
147 $opts .= "<option value='" . esc_attr($value) . "' {$selected}>" . esc_attr($label) . '</option>';
148 }
149 return $opts;
150 }
151
152 public function getSelectedCountries($keys = [])
153 {
154 $options = [];
155 $countries = getFluentFormCountryList();
156 foreach ($keys as $value) {
157 $options[$value] = $countries[$value];
158 }
159
160 $options = array_flip($options);
161 ksort($options);
162 return array_flip($options);
163 }
164 }
165