PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / trunk
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster vtrunk
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 1.7.9 1.8.1 All 42 releases
sellkit / includes / block-editor / blocks / checkout / form-fields / select.php

select.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster trunk, at includes/block-editor/blocks/checkout/form-fields/select.php

235 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Sellkit\Blocks\Checkout\Fields;
4
5 defined( 'ABSPATH' ) || die();
6
7 use Sellkit\Blocks\Checkout\Fields\Base;
8
9 /**
10 * Class select.
11 *
12 * @since 2.3.0
13 * @SuppressWarnings(PHPMD.NPathComplexity)
14 */
15 class Select extends Base {
16 /**
17 * Type of field.
18 *
19 * @return string
20 * @since 2.3.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 2.3.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 2.3.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 autocomplete="country"
106 data-placeholder="<?php echo esc_attr( $placeholder ); ?>"
107 data-label="<?php echo ( array_key_exists( 'label', $args ) ) ? esc_attr( $args['label'] ) : ''; ?>"
108 >
109 <?php
110 $default_value = ( array_key_exists( 'default', $args ) ) ? $args['default'] : '';
111 ?>
112 <?php foreach ( $args['options'] as $value => $label ) : ?>
113 <?php
114 if ( $value === $default_value ) {
115 echo sprintf(
116 /** Translators: 1:value 2: label */
117 '<option selected value="%1$s">%2$s</option>',
118 esc_attr( $value ),
119 esc_html( $label )
120 );
121
122 continue;
123 }
124
125 echo sprintf(
126 /** Translators: 1:value 2: label */
127 '<option value="%1$s">%2$s</option>',
128 esc_attr( $value ),
129 esc_html( $label )
130 );
131 ?>
132 <?php endforeach; ?>
133 </select>
134 </span>
135 </p>
136 <?php
137 }
138
139 /**
140 * Customize woocommerce country field.
141 *
142 * @param string $key field key.
143 * @param string $default field default value.
144 * @since 2.3.0
145 * @return void
146 */
147 private function woocommerce_field_country( $key, $default ) {
148 ?>
149 <p id="<?php echo esc_attr( $key ); ?>_field">
150 <span class="woocommerce-input-wrapper">
151 <?php
152 $countries = WC()->countries->get_shipping_countries();
153
154 if ( 'billing_country' === $key ) {
155 $countries = WC()->countries->get_allowed_countries();
156 }
157
158 $field = '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $key ) . '" class="country_to_state" ><option value="">' . esc_html__( 'Select a country…', 'sellkit' ) . '</option>';
159
160 foreach ( $countries as $country_key => $country_label ) {
161 $selected = '';
162
163 if ( $default === $country_key ) {
164 $selected = 'selected="selected"';
165 }
166
167 $field .= '<option ' . esc_attr( $selected ) . ' value="' . esc_attr( $country_key ) . '" >' . esc_html( $country_label ) . '</option>';
168 }
169
170 $field .= '</select>';
171 $field .= '<noscript><input type="submit" name="woocommerce_checkout_update_totals" value="' . esc_attr__( 'Update country', 'sellkit' ) . '" /></noscript>';
172
173 echo $field; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
174 ?>
175 </span>
176 </p>
177 <?php
178 }
179
180 /**
181 * Customize woocommerce state field.
182 *
183 * @param string $key field key.
184 * @param array $args field options.
185 * @param array $states country states.
186 * @since 2.3.0
187 * @return void
188 */
189 private function woocommerce_field_state( $key, $args, $states ) {
190 $placeholder = $this->placeholder_required_value( $args );
191
192 ?>
193 <p id="<?php echo esc_attr( $key ); ?>_field">
194 <span class="woocommerce-input-wrapper">
195 <?php
196 if ( ! is_array( $states ) || empty( $states ) ) {
197 $state = filter_input( INPUT_COOKIE, 'sellkit-checkout-billing-state' );
198
199 if ( 'shipping_state' === $key ) {
200 $state = filter_input( INPUT_COOKIE, 'sellkit-checkout-shipping-state' );
201 }
202
203 if ( empty( $state ) ) {
204 $state = '';
205 }
206
207 echo sprintf(
208 /* translators: 1: placeholder 2: name 3: id 4: default value */
209 '<input type="text" placeholder="%1$s" name="%2$s" id="%3$s" value="%4$s">',
210 esc_attr( $placeholder ),
211 esc_attr( $key ),
212 esc_attr( 'sellkit-' . $key ),
213 esc_attr( $state )
214 );
215
216 return;
217 }
218
219 $field = '<select id="sellkit-' . esc_attr( $key ) . '" name="' . esc_attr( $key ) . '" placeholder="' . esc_attr( $args['label'] ) . '">
220 <option value="">' . esc_html__( 'Select a state…', 'sellkit' ) . '</option>';
221
222 foreach ( $states as $state_key => $state_label ) {
223 $field .= '<option value="' . esc_attr( $state_key ) . '" >' . esc_html( $state_label ) . '</option>';
224 }
225
226 $field .= '</select>';
227
228 echo $field; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
229 ?>
230 </span>
231 </p>
232 <?php
233 }
234 }
235