| 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 |
$default_value = ''; |
| 69 |
if ( array_key_exists( 'default', $args ) ) { |
| 70 |
$default_value = $args['default']; |
| 71 |
} |
| 72 |
|
| 73 |
if ( 'shipping_country' === $key || 'billing_country' === $key ) { |
| 74 |
$this->woocommerce_field_country( $key, $default_value ); |
| 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, $default_value ); |
| 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 foreach ( $args['options'] as $value => $label ) : ?> |
| 111 |
<?php |
| 112 |
if ( $value === $default_value ) { |
| 113 |
echo sprintf( |
| 114 |
/** Translators: 1:value 2: label */ |
| 115 |
'<option selected value="%1$s">%2$s</option>', |
| 116 |
esc_attr( $value ), |
| 117 |
esc_html( $label ) |
| 118 |
); |
| 119 |
|
| 120 |
continue; |
| 121 |
} |
| 122 |
|
| 123 |
echo sprintf( |
| 124 |
/** Translators: 1:value 2: label */ |
| 125 |
'<option value="%1$s">%2$s</option>', |
| 126 |
esc_attr( $value ), |
| 127 |
esc_html( $label ) |
| 128 |
); |
| 129 |
?> |
| 130 |
<?php endforeach; ?> |
| 131 |
</select> |
| 132 |
</span> |
| 133 |
</p> |
| 134 |
<?php |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Customize woocommerce country field. |
| 139 |
* |
| 140 |
* @param string $key field key. |
| 141 |
* @param string $default field default value. |
| 142 |
* @since 1.1.0 |
| 143 |
* @return void |
| 144 |
*/ |
| 145 |
private function woocommerce_field_country( $key, $default ) { |
| 146 |
?> |
| 147 |
<p id="<?php echo esc_attr( $key ); ?>_field"> |
| 148 |
<span class="woocommerce-input-wrapper"> |
| 149 |
<?php |
| 150 |
$countries = WC()->countries->get_shipping_countries(); |
| 151 |
|
| 152 |
if ( 'billing_country' === $key ) { |
| 153 |
$countries = WC()->countries->get_allowed_countries(); |
| 154 |
} |
| 155 |
|
| 156 |
$field = '<select name="' . esc_attr( $key ) . '" id="' . $key . '" class="country_to_state" ><option value="">' . esc_html__( 'Select a country…', 'sellkit' ) . '</option>'; |
| 157 |
|
| 158 |
foreach ( $countries as $country_key => $country_label ) { |
| 159 |
$selected = ''; |
| 160 |
|
| 161 |
if ( $default === $country_key ) { |
| 162 |
$selected = 'selected="selected"'; |
| 163 |
} |
| 164 |
|
| 165 |
$field .= '<option ' . $selected . ' value="' . esc_attr( $country_key ) . '" >' . esc_html( $country_label ) . '</option>'; |
| 166 |
} |
| 167 |
|
| 168 |
$field .= '</select>'; |
| 169 |
$field .= '<noscript><input type="submit" name="woocommerce_checkout_update_totals" value="' . esc_attr__( 'Update country', 'sellkit' ) . '" /></noscript>'; |
| 170 |
|
| 171 |
$allowed_html = [ |
| 172 |
'select' => [ |
| 173 |
'name' => true, |
| 174 |
'id' => true, |
| 175 |
'class' => true, |
| 176 |
], |
| 177 |
'option' => [ |
| 178 |
'value' => true, |
| 179 |
'selected' => true, |
| 180 |
], |
| 181 |
'noscript' => [], |
| 182 |
'input' => [ |
| 183 |
'type' => true, |
| 184 |
'name' => true, |
| 185 |
'value' => true, |
| 186 |
], |
| 187 |
]; |
| 188 |
|
| 189 |
echo wp_kses( $field, $allowed_html ); |
| 190 |
?> |
| 191 |
</span> |
| 192 |
</p> |
| 193 |
<?php |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Customize woocommerce state field. |
| 198 |
* |
| 199 |
* @param string $key field key. |
| 200 |
* @param array $args field options. |
| 201 |
* @param array $states country states. |
| 202 |
* @param string $default_value field default value. |
| 203 |
* @since 1.1.0 |
| 204 |
* @return void |
| 205 |
*/ |
| 206 |
private function woocommerce_field_state( $key, $args, $states, $default_value ) { |
| 207 |
$placeholder = $this->placeholder_required_value( $args ); |
| 208 |
|
| 209 |
?> |
| 210 |
<p id="<?php echo esc_attr( $key ); ?>_field"> |
| 211 |
<span class="woocommerce-input-wrapper"> |
| 212 |
<?php |
| 213 |
if ( ! is_array( $states ) || empty( $states ) ) { |
| 214 |
$state = filter_input( INPUT_COOKIE, 'sellkit-checkout-billing-state' ); |
| 215 |
|
| 216 |
if ( 'shipping_state' === $key ) { |
| 217 |
$state = filter_input( INPUT_COOKIE, 'sellkit-checkout-shipping-state' ); |
| 218 |
} |
| 219 |
|
| 220 |
if ( empty( $state ) ) { |
| 221 |
$state = ''; |
| 222 |
} |
| 223 |
|
| 224 |
echo sprintf( |
| 225 |
/* translators: 1: placeholder 2: name 3: id 4: default value */ |
| 226 |
'<input type="text" placeholder="%1$s" name="%2$s" id="%3$s" value="%4$s">', |
| 227 |
esc_attr( $placeholder ), |
| 228 |
esc_attr__( 'State', 'sellkit' ), |
| 229 |
esc_attr( 'sellkit-' . $key ), |
| 230 |
esc_attr( $state ) |
| 231 |
); |
| 232 |
|
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
$field = '<select id="sellkit-' . esc_attr( $key ) . '" name="' . esc_attr( $key ) . '" placeholder="' . esc_attr( $args['label'] ) . '"> |
| 237 |
<option value="">' . esc_html__( 'Select a state…', 'sellkit' ) . '</option>'; |
| 238 |
|
| 239 |
foreach ( $states as $state_key => $state_label ) { |
| 240 |
$selected = ''; |
| 241 |
|
| 242 |
if ( $default_value === $state_key ) { |
| 243 |
$selected = 'selected'; |
| 244 |
} |
| 245 |
$field .= '<option ' . $selected . ' value="' . esc_attr( $state_key ) . '" >' . esc_html( $state_label ) . '</option>'; |
| 246 |
} |
| 247 |
|
| 248 |
$field .= '</select>'; |
| 249 |
|
| 250 |
$allowed_html = [ |
| 251 |
'select' => [ |
| 252 |
'id' => true, |
| 253 |
'name' => true, |
| 254 |
'placeholder' => true, |
| 255 |
], |
| 256 |
'option' => [ |
| 257 |
'value' => true, |
| 258 |
'selected' => true, |
| 259 |
], |
| 260 |
]; |
| 261 |
|
| 262 |
echo wp_kses( $field, $allowed_html ); |
| 263 |
?> |
| 264 |
</span> |
| 265 |
</p> |
| 266 |
<?php |
| 267 |
} |
| 268 |
} |
| 269 |
|