PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 2.3.0
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v2.3.0
2.8.0 2.7.0 2.6.0 trunk 1.1.0 1.1.4 1.2.1 1.2.2 1.2.3 1.2.5 1.2.9 1.3.1 1.3.2 1.5.0 1.5.1 1.5.4 1.5.7 1.5.8 1.5.9 1.6.2 1.6.5 1.6.8 1.7.2 1.7.4 1.7.5 All 44 releases
sellkit / includes / elementor / modules / checkout / fields / select.php

select.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster 2.3.0, at includes/elementor/modules/checkout/fields/select.php

265 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 Sellkit\Elementor\Modules\Checkout\Fields;
4
5 defined( 'ABSPATH' ) || die();
6
7 use Sellkit\Elementor\Modules\Checkout\Fields\Base;
8
9 /**
10 * Class select.
11 *
12 * @since 1.1.0
13 * @SuppressWarnings(PHPMD.NPathComplexity)
14 */
15 class Select extends Base {
16 /**
17 * Type of field.
18 *
19 * @return string
20 * @since 1.1.0
21 */
22 public function type() {
23 return 'select';
24 }
25
26 /**
27 * Adds additional class for fields if required.
28 *
29 * @param array $args checkout fields options.
30 * @param string $key field key.
31 * @return array
32 * @since 1.1.0
33 */
34 protected function additional_class( $args, $key ) {
35 $args['class'][] = 'country-state';
36
37 $country_key = 'sellkit-checkout-shipping-cc';
38
39 if ( 'billing_state' === $key ) {
40 $country_key = 'sellkit-checkout-billing-cc';
41 }
42
43 $country_code = sellkit_htmlspecialchars( INPUT_COOKIE, $country_key );
44
45 $states = WC()->countries->get_states( $country_code );
46
47 if ( ( 'billing_state' === $key || 'shipping_state' === $key ) && ( ! is_array( $states ) || empty( $states ) ) ) {
48 return $args;
49 }
50
51 $args['class'][] = 'sellkit-checkout-field-select';
52
53 return $args;
54 }
55
56 /**
57 * Customized html per field.
58 *
59 * @param string $field field html string.
60 * @param array $args checkout fields options.
61 * @param string $key key of field.
62 * @return void
63 * @since 1.1.0
64 */
65 public function field( $field, $args, $key ) {
66 echo '<span class="sellkit-select-appearance"><svg xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 448 512"><path d="M201.4 342.6c12.5 12.5 32.8 12.5 45.3 0l160-160c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L224 274.7 86.6 137.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3l160 160z"/></svg></span>';
67
68 if ( 'shipping_country' === $key || 'billing_country' === $key ) {
69 $default = '';
70 if ( array_key_exists( 'default', $args ) ) {
71 $default = $args['default'];
72 }
73
74 $this->woocommerce_field_country( $key, $default );
75
76 return;
77 }
78
79 if ( 'shipping_state' === $key || 'billing_state' === $key ) {
80 $country_code = filter_input( INPUT_COOKIE, 'sellkit-checkout-shipping-cc' );
81
82 if ( 'billing_state' === $key ) {
83 $country_code = filter_input( INPUT_COOKIE, 'sellkit-checkout-billing-cc' );
84 }
85
86 if ( empty( $country_code ) ) {
87 $country_code = 'US';
88 }
89
90 $states = WC()->countries->get_states( $country_code );
91
92 $this->woocommerce_field_state( $key, $args, $states );
93
94 return;
95 }
96
97 $placeholder = $this->placeholder_required_value( $args );
98
99 ?>
100 <p id="<?php echo esc_attr( $key ); ?>_field">
101 <span class="woocommerce-input-wrapper">
102 <select
103 name="<?php echo esc_attr( $key ); ?>"
104 id="<?php echo esc_attr( $key ); ?>"
105 class=""
106 autocomplete="country"
107 data-placeholder="<?php echo esc_attr( $placeholder ); ?>"
108 data-label="<?php echo ( array_key_exists( 'label', $args ) ) ? esc_attr( $args['label'] ) : ''; ?>"
109 >
110 <?php
111 $default_value = ( array_key_exists( 'default', $args ) ) ? $args['default'] : '';
112 ?>
113 <?php foreach ( $args['options'] as $value => $label ) : ?>
114 <?php
115 if ( $value === $default_value ) {
116 echo sprintf(
117 /** Translators: 1:value 2: label */
118 '<option selected value="%1$s">%2$s</option>',
119 esc_attr( $value ),
120 esc_html( $label )
121 );
122
123 continue;
124 }
125
126 echo sprintf(
127 /** Translators: 1:value 2: label */
128 '<option value="%1$s">%2$s</option>',
129 esc_attr( $value ),
130 esc_html( $label )
131 );
132 ?>
133 <?php endforeach; ?>
134 </select>
135 </span>
136 </p>
137 <?php
138 }
139
140 /**
141 * Customize woocommerce country field.
142 *
143 * @param string $key field key.
144 * @param string $default field default value.
145 * @since 1.1.0
146 * @return void
147 */
148 private function woocommerce_field_country( $key, $default ) {
149 ?>
150 <p id="<?php echo esc_attr( $key ); ?>_field">
151 <span class="woocommerce-input-wrapper">
152 <?php
153 $countries = WC()->countries->get_shipping_countries();
154
155 if ( 'billing_country' === $key ) {
156 $countries = WC()->countries->get_allowed_countries();
157 }
158
159 $field = '<select name="' . esc_attr( $key ) . '" id="' . $key . '" class="country_to_state" ><option value="">' . esc_html__( 'Select a country…', 'sellkit' ) . '</option>';
160
161 foreach ( $countries as $country_key => $country_label ) {
162 $selected = '';
163
164 if ( $default === $country_key ) {
165 $selected = 'selected="selected"';
166 }
167
168 $field .= '<option ' . $selected . ' value="' . esc_attr( $country_key ) . '" >' . esc_html( $country_label ) . '</option>';
169 }
170
171 $field .= '</select>';
172 $field .= '<noscript><input type="submit" name="woocommerce_checkout_update_totals" value="' . esc_attr__( 'Update country', 'sellkit' ) . '" /></noscript>';
173
174 $allowed_html = [
175 'select' => [
176 'name' => true,
177 'id' => true,
178 'class' => true,
179 ],
180 'option' => [
181 'value' => true,
182 'selected' => true,
183 ],
184 'noscript' => [],
185 'input' => [
186 'type' => true,
187 'name' => true,
188 'value' => true,
189 ],
190 ];
191
192 echo wp_kses( $field, $allowed_html );
193 ?>
194 </span>
195 </p>
196 <?php
197 }
198
199 /**
200 * Customize woocommerce state field.
201 *
202 * @param string $key field key.
203 * @param array $args field options.
204 * @param array $states country states.
205 * @since 1.1.0
206 * @return void
207 */
208 private function woocommerce_field_state( $key, $args, $states ) {
209 $placeholder = $this->placeholder_required_value( $args );
210
211 ?>
212 <p id="<?php echo esc_attr( $key ); ?>_field">
213 <span class="woocommerce-input-wrapper">
214 <?php
215 if ( ! is_array( $states ) || empty( $states ) ) {
216 $state = filter_input( INPUT_COOKIE, 'sellkit-checkout-billing-state' );
217
218 if ( 'shipping_state' === $key ) {
219 $state = filter_input( INPUT_COOKIE, 'sellkit-checkout-shipping-state' );
220 }
221
222 if ( empty( $state ) ) {
223 $state = '';
224 }
225
226 echo sprintf(
227 /* translators: 1: placeholder 2: name 3: id 4: default value */
228 '<input type="text" placeholder="%1$s" name="%2$s" id="%3$s" value="%4$s">',
229 esc_attr( $placeholder ),
230 esc_attr__( 'State', 'sellkit' ),
231 esc_attr( 'sellkit-' . $key ),
232 esc_attr( $state )
233 );
234
235 return;
236 }
237
238 $field = '<select id="sellkit-' . esc_attr( $key ) . '" name="' . esc_attr( $key ) . '" placeholder="' . esc_attr( $args['label'] ) . '">
239 <option value="">' . esc_html__( 'Select a state…', 'sellkit' ) . '</option>';
240
241 foreach ( $states as $state_key => $state_label ) {
242 $field .= '<option value="' . esc_attr( $state_key ) . '" >' . esc_html( $state_label ) . '</option>';
243 }
244
245 $field .= '</select>';
246
247 $allowed_html = [
248 'select' => [
249 'id' => true,
250 'name' => true,
251 'placeholder' => true,
252 ],
253 'option' => [
254 'value' => true,
255 ],
256 ];
257
258 echo wp_kses( $field, $allowed_html );
259 ?>
260 </span>
261 </p>
262 <?php
263 }
264 }
265