| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cartflows Checkout Field. |
| 4 |
* |
| 5 |
* @package cartflows |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class Cartflows_Checkout_Fields. |
| 14 |
*/ |
| 15 |
class Cartflows_Checkout_Fields { |
| 16 |
|
| 17 |
/** |
| 18 |
* Member Variable |
| 19 |
* |
| 20 |
* @var instance |
| 21 |
*/ |
| 22 |
private static $instance; |
| 23 |
|
| 24 |
/** |
| 25 |
* Checkout ID |
| 26 |
* |
| 27 |
* @var checkout_id |
| 28 |
*/ |
| 29 |
public static $cached_checkout_id = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* Initiator |
| 33 |
*/ |
| 34 |
public static function get_instance() { |
| 35 |
if ( ! isset( self::$instance ) ) { |
| 36 |
self::$instance = new self(); |
| 37 |
} |
| 38 |
return self::$instance; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Constructor |
| 43 |
*/ |
| 44 |
public function __construct() { |
| 45 |
// Register field filters directly instead of deferring to the wp/init hooks. |
| 46 |
// WooCommerce caches get_checkout_fields() on first call, which can occur before those hooks fire. |
| 47 |
$this->checkout_field_actions(); |
| 48 |
|
| 49 |
add_action( 'wp_enqueue_scripts', array( $this, 'add_frontend_scripts' ) ); |
| 50 |
|
| 51 |
add_filter( 'cartflows_show_coupon_field', array( $this, 'show_hide_coupon_field_on_checkout' ), 10, 2 ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Add frontend scripts. |
| 56 |
*/ |
| 57 |
public function add_frontend_scripts() { |
| 58 |
|
| 59 |
if ( ! _is_wcf_checkout_type() ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
global $post; |
| 64 |
|
| 65 |
$checkout_id = $post->ID; |
| 66 |
|
| 67 |
$this->add_frontend_localize_optimized_scripts( $checkout_id ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Add localize variables. |
| 72 |
* |
| 73 |
* @param int $checkout_id Checkout id. |
| 74 |
*/ |
| 75 |
public function add_frontend_localize_optimized_scripts( $checkout_id ) { |
| 76 |
|
| 77 |
$is_custom_fields_enabled = get_post_meta( $checkout_id, 'wcf-custom-checkout-fields', true ); |
| 78 |
|
| 79 |
$localize = array(); |
| 80 |
|
| 81 |
if ( 'yes' === $is_custom_fields_enabled ) { |
| 82 |
|
| 83 |
$get_ordered_billing_fields = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf_field_order_billing' ); |
| 84 |
$get_ordered_shipping_fields = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf_field_order_shipping' ); |
| 85 |
|
| 86 |
if ( ! is_array( $get_ordered_billing_fields ) ) { |
| 87 |
$get_ordered_billing_fields = array(); |
| 88 |
} |
| 89 |
|
| 90 |
if ( ! is_array( $get_ordered_shipping_fields ) ) { |
| 91 |
$get_ordered_shipping_fields = array(); |
| 92 |
} |
| 93 |
|
| 94 |
$order_checkout_fields = array_merge( $get_ordered_billing_fields, $get_ordered_shipping_fields ); |
| 95 |
|
| 96 |
foreach ( $order_checkout_fields as $field_key => $order_checkout_field ) { |
| 97 |
|
| 98 |
$is_enabled = isset( $order_checkout_field['enabled'] ) ? $order_checkout_field['enabled'] : false; |
| 99 |
$is_required = isset( $order_checkout_field['required'] ) ? $order_checkout_field['required'] : false; |
| 100 |
$is_optimized = isset( $order_checkout_field['optimized'] ) ? $order_checkout_field['optimized'] : false; |
| 101 |
|
| 102 |
$localize[ $field_key . '_field' ] = array( |
| 103 |
'is_optimized' => ( $is_enabled && ! $is_required && $is_optimized ), |
| 104 |
|
| 105 |
/* Translators: %1$s & %2$s is replaced with Field Name */ |
| 106 |
'field_label' => sprintf( __( '%1$s Add %2$s', 'cartflows' ), '<div class="dashicons dashicons-arrow-right"></div>', esc_html( $order_checkout_field['label'] ) ), |
| 107 |
); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
$show_coupon_field = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf-show-coupon-field' ); |
| 112 |
$optimize_coupon_field = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf-optimize-coupon-field' ); |
| 113 |
|
| 114 |
$localize['wcf_custom_coupon_field'] = array( |
| 115 |
'is_optimized' => ( 'yes' === $show_coupon_field && 'yes' === $optimize_coupon_field ? true : false ), |
| 116 |
/* Translators: %s is replaced with Field Icon */ |
| 117 |
'field_label' => sprintf( __( '%s Have a coupon?', 'cartflows' ), '<div class="dashicons dashicons-arrow-right"></div>' ), |
| 118 |
); |
| 119 |
|
| 120 |
$show_order_field = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf-checkout-additional-fields' ); |
| 121 |
$optimize_order_field = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf-optimize-order-note-field' ); |
| 122 |
|
| 123 |
$localize['order_comments_field'] = array( |
| 124 |
'is_optimized' => ( 'yes' === $show_order_field && 'yes' === $optimize_order_field ? true : false ), |
| 125 |
/* Translators: %s is replaced with Field Icon */ |
| 126 |
'field_label' => sprintf( __( '%s Add Order Notes', 'cartflows' ), '<div class="dashicons dashicons-arrow-right"></div>' ), |
| 127 |
); |
| 128 |
|
| 129 |
$localize = apply_filters( 'cartflows_checkout_optimized_fields', $localize, $checkout_id ); |
| 130 |
|
| 131 |
$localize_script = '<!-- script to print the admin localized variables -->'; |
| 132 |
$localize_script .= '<script type="text/javascript">'; |
| 133 |
$localize_script .= 'var cartflows_checkout_optimized_fields = ' . wp_json_encode( $localize ) . ';'; |
| 134 |
$localize_script .= '</script>'; |
| 135 |
|
| 136 |
echo $localize_script; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Trigger actions. |
| 141 |
*/ |
| 142 |
public function checkout_field_actions() { |
| 143 |
|
| 144 |
add_filter( 'woocommerce_checkout_fields', array( $this, 'add_three_column_layout_fields' ) ); |
| 145 |
add_filter( 'woocommerce_checkout_fields', array( $this, 'label_skins_fields_customization' ), 1000 ); |
| 146 |
|
| 147 |
add_filter( 'woocommerce_checkout_fields', array( $this, 'additional_fields_customization' ), 1000 ); |
| 148 |
|
| 149 |
add_filter( 'woocommerce_billing_fields', array( $this, 'billing_fields_customization' ), 1000, 2 ); |
| 150 |
add_filter( 'woocommerce_shipping_fields', array( $this, 'shipping_fields_customization' ), 1000, 2 ); |
| 151 |
|
| 152 |
add_filter( 'woocommerce_get_country_locale_default', array( $this, 'prepare_country_locale' ) ); |
| 153 |
|
| 154 |
add_filter( 'woocommerce_default_address_fields', array( $this, 'woo_default_address_fields' ), 1000 ); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Prepare default country locale. |
| 159 |
* |
| 160 |
* @param array $fields country locale fields. |
| 161 |
* @return array |
| 162 |
*/ |
| 163 |
public function woo_default_address_fields( $fields ) { |
| 164 |
|
| 165 |
$checkout_id = $this->get_conditional_checkout_id(); |
| 166 |
|
| 167 |
if ( ! $checkout_id ) { |
| 168 |
|
| 169 |
return $fields; |
| 170 |
} |
| 171 |
|
| 172 |
$is_custom_fields_enabled = get_post_meta( $checkout_id, 'wcf-custom-checkout-fields', true ); |
| 173 |
|
| 174 |
if ( 'yes' === $is_custom_fields_enabled ) { |
| 175 |
|
| 176 |
$sname = apply_filters( 'wcf_address_field_override_with', 'billing' ); |
| 177 |
|
| 178 |
if ( 'billing' === $sname || 'shipping' === $sname ) { |
| 179 |
|
| 180 |
$address_fields = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf_field_order_billing' ); |
| 181 |
|
| 182 |
if ( '' === $address_fields ) { |
| 183 |
|
| 184 |
$address_fields = Cartflows_Helper::get_checkout_fields( $sname, $checkout_id ); |
| 185 |
} |
| 186 |
|
| 187 |
if ( is_array( $address_fields ) && ! empty( $address_fields ) && ! empty( $fields ) ) { |
| 188 |
$override_required = apply_filters( 'wcf_address_field_override_required', true ); |
| 189 |
|
| 190 |
foreach ( $fields as $name => $field ) { |
| 191 |
$fname = $sname . '_' . $name; |
| 192 |
|
| 193 |
if ( $this->is_locale_field( $fname ) && $override_required ) { |
| 194 |
$custom_field = isset( $address_fields[ $fname ] ) ? $address_fields[ $fname ] : false; |
| 195 |
|
| 196 |
if ( $custom_field && ! ( isset( $custom_field['enabled'] ) && false == $custom_field['enabled'] ) ) { |
| 197 |
$fields[ $name ]['required'] = isset( $custom_field['required'] ) && $custom_field['required'] ? true : false; |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
return $fields; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
/** |
| 210 |
* Set locale fields. |
| 211 |
* |
| 212 |
* @param string $field_name field name. |
| 213 |
* @return bool |
| 214 |
*/ |
| 215 |
public function is_locale_field( $field_name ) { |
| 216 |
if ( ! empty( $field_name ) && in_array( |
| 217 |
$field_name, |
| 218 |
array( |
| 219 |
'billing_country', |
| 220 |
'billing_address_1', |
| 221 |
'billing_address_2', |
| 222 |
'billing_state', |
| 223 |
'billing_postcode', |
| 224 |
'billing_city', |
| 225 |
'shipping_country', |
| 226 |
'shipping_address_1', |
| 227 |
'shipping_address_2', |
| 228 |
'shipping_state', |
| 229 |
'shipping_postcode', |
| 230 |
'shipping_city', |
| 231 |
), |
| 232 |
true |
| 233 |
) |
| 234 |
) { |
| 235 |
return true; |
| 236 |
} |
| 237 |
return false; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Additional fields customization. |
| 242 |
* |
| 243 |
* @param array $fields fields. |
| 244 |
* |
| 245 |
* @return array fields |
| 246 |
*/ |
| 247 |
public function additional_fields_customization( $fields ) { |
| 248 |
|
| 249 |
$checkout_id = $this->get_conditional_checkout_id(); |
| 250 |
|
| 251 |
if ( ! $checkout_id ) { |
| 252 |
return $fields; |
| 253 |
} |
| 254 |
|
| 255 |
$show_shipto_diff_addr = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf-shipto-diff-addr-fields' ); |
| 256 |
$show_order_field = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf-checkout-additional-fields' ); |
| 257 |
|
| 258 |
if ( isset( $fields['order']['order_comments'] ) ) { |
| 259 |
|
| 260 |
if ( 'no' === $show_order_field ) { |
| 261 |
unset( $fields['order']['order_comments'] ); |
| 262 |
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' ); |
| 263 |
} else { |
| 264 |
|
| 265 |
$is_order_note_optimized = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf-optimize-order-note-field' ); |
| 266 |
|
| 267 |
if ( 'yes' === $is_order_note_optimized ) { |
| 268 |
$fields['order']['order_comments']['class'][] = 'wcf-hide-field'; |
| 269 |
add_filter( 'cartflows_show_additional_field', '__return_true' ); |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
if ( 'no' === $show_shipto_diff_addr ) { |
| 275 |
add_filter( 'woocommerce_cart_needs_shipping_address', '__return_false' ); |
| 276 |
} |
| 277 |
|
| 278 |
return $fields; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
/** |
| 283 |
* Example values shown in Instant Checkout's fields, keyed by field name without its address prefix. |
| 284 |
* Only fields whose format is worth demonstrating appear here; the rest are left empty. |
| 285 |
* |
| 286 |
* @since x.x.x |
| 287 |
* @return array<string, string> |
| 288 |
*/ |
| 289 |
public function get_instant_checkout_placeholders() { |
| 290 |
|
| 291 |
return apply_filters( |
| 292 |
'cartflows_instant_checkout_field_placeholders', |
| 293 |
array( |
| 294 |
'email' => __( '[email protected]', 'cartflows' ), |
| 295 |
'first_name' => __( 'Jane', 'cartflows' ), |
| 296 |
'last_name' => __( 'Doe', 'cartflows' ), |
| 297 |
'phone' => __( '+1 415 555 0142', 'cartflows' ), |
| 298 |
) |
| 299 |
); |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Whether a placeholder says nothing the visible label does not already say. |
| 304 |
* |
| 305 |
* @since x.x.x |
| 306 |
* @param string $placeholder placeholder text. |
| 307 |
* @param string $label label text. |
| 308 |
* @return bool |
| 309 |
*/ |
| 310 |
public function is_placeholder_echoing_label( $placeholder, $label ) { |
| 311 |
|
| 312 |
if ( '' === $placeholder || '' === $label ) { |
| 313 |
return false; |
| 314 |
} |
| 315 |
|
| 316 |
// Woo marks the label alone with "(optional)" or an asterisk, so compare what is left of the name. |
| 317 |
$strip = static function ( $text ) { |
| 318 |
$text = str_replace( array( ' ', '*', '*' ), ' ', wp_strip_all_tags( (string) $text ) ); |
| 319 |
$text = preg_replace( '/\(\s*optional\s*\)/i', '', $text ); |
| 320 |
return strtolower( trim( (string) preg_replace( '/\s+/', ' ', (string) $text ) ) ); |
| 321 |
}; |
| 322 |
|
| 323 |
return $strip( $placeholder ) === $strip( $label ); |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Placeholder for one Instant Checkout field: a merchant-set placeholder wins, an example |
| 328 |
* fills a blank or echoing one, and the label itself is the last resort so the box is never empty. |
| 329 |
* |
| 330 |
* @since x.x.x |
| 331 |
* @param string $key field key, with or without a billing/shipping prefix. |
| 332 |
* @param array<string,mixed> $field field definition. |
| 333 |
* @return string |
| 334 |
*/ |
| 335 |
public function get_instant_checkout_placeholder( $key, $field ) { |
| 336 |
|
| 337 |
$examples = $this->get_instant_checkout_placeholders(); |
| 338 |
$name = (string) preg_replace( '/^(?:billing|shipping)_/', '', (string) $key ); |
| 339 |
$placeholder = isset( $field['placeholder'] ) && is_string( $field['placeholder'] ) ? $field['placeholder'] : ''; |
| 340 |
$label = isset( $field['label'] ) && is_string( $field['label'] ) ? $field['label'] : ''; |
| 341 |
$is_echo = $this->is_placeholder_echoing_label( $placeholder, $label ); |
| 342 |
|
| 343 |
if ( isset( $examples[ $name ] ) && ( '' === $placeholder || $is_echo ) ) { |
| 344 |
return $examples[ $name ]; |
| 345 |
} |
| 346 |
|
| 347 |
if ( '' !== $placeholder && ! $is_echo ) { |
| 348 |
return $placeholder; |
| 349 |
} |
| 350 |
|
| 351 |
// The modern-label skin's floating label is invisible until the field has a value, so it can never be the only visible name. |
| 352 |
return $label; |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* Prepare country locale. |
| 357 |
* |
| 358 |
* @param array $fields country locale fields. |
| 359 |
* @return array |
| 360 |
*/ |
| 361 |
public function prepare_country_locale( $fields ) { |
| 362 |
|
| 363 |
$checkout_id = $this->get_conditional_checkout_id(); |
| 364 |
|
| 365 |
if ( ! $checkout_id ) { |
| 366 |
|
| 367 |
return $fields; |
| 368 |
} |
| 369 |
|
| 370 |
if ( is_array( $fields ) ) { |
| 371 |
|
| 372 |
$is_custom_fields_enabled = get_post_meta( $checkout_id, 'wcf-custom-checkout-fields', true ); |
| 373 |
$override_placeholder = apply_filters( 'wcf_field_override_placeholder', true ); |
| 374 |
$override_label = apply_filters( 'wcf_field_override_label', true ); |
| 375 |
|
| 376 |
if ( 'yes' === $is_custom_fields_enabled ) { |
| 377 |
foreach ( $fields as $key => $props ) { |
| 378 |
if ( isset( $props['priority'] ) ) { |
| 379 |
unset( $fields[ $key ]['priority'] ); |
| 380 |
} |
| 381 |
|
| 382 |
if ( $override_placeholder && isset( $props['placeholder'] ) ) { |
| 383 |
unset( $fields[ $key ]['placeholder'] ); |
| 384 |
} |
| 385 |
if ( $override_label && isset( $props['label'] ) ) { |
| 386 |
unset( $fields[ $key ]['label'] ); |
| 387 |
} |
| 388 |
} |
| 389 |
} else { |
| 390 |
|
| 391 |
$fields_skins = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf-fields-skins' ); |
| 392 |
|
| 393 |
// Change the field style to modern labels for older users who are using style one i:e floating labels. |
| 394 |
if ( 'style-one' === $fields_skins ) { |
| 395 |
$fields_skins = 'modern-label'; |
| 396 |
} |
| 397 |
|
| 398 |
if ( 'modern-label' === $fields_skins ) { |
| 399 |
|
| 400 |
$is_instant = Cartflows_Helper::is_instant_layout_enabled(); |
| 401 |
|
| 402 |
foreach ( $fields as $key => $props ) { |
| 403 |
|
| 404 |
// Instant Checkout shows the label already, so the box carries an example or nothing. |
| 405 |
if ( $is_instant ) { |
| 406 |
$fields[ $key ]['placeholder'] = $this->get_instant_checkout_placeholder( $key, $fields[ $key ] ); |
| 407 |
} elseif ( empty( $fields[ $key ]['placeholder'] ) && ! empty( $fields[ $key ]['label'] ) ) { |
| 408 |
$fields[ $key ]['placeholder'] = $fields[ $key ]['label']; |
| 409 |
} |
| 410 |
|
| 411 |
if ( isset( $fields[ $key ]['label_class'] ) ) { |
| 412 |
$fields[ $key ]['label_class'] = array_diff( $fields[ $key ]['label_class'], array( 'screen-reader-text' ) ); |
| 413 |
} |
| 414 |
|
| 415 |
// Add Asterisk mark to the placeholder text if the field is required. |
| 416 |
if ( ! $is_instant && ! empty( $fields[ $key ]['placeholder'] ) && ( isset( $fields[ $key ]['required'] ) && $fields[ $key ]['required'] ) ) { |
| 417 |
$fields[ $key ]['placeholder'] = $fields[ $key ]['placeholder'] . ' *'; |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
} |
| 423 |
return $fields; |
| 424 |
} |
| 425 |
|
| 426 |
/** |
| 427 |
* Billing field customization. |
| 428 |
* |
| 429 |
* @param array $fields fields data. |
| 430 |
* @param string $country country name. |
| 431 |
* @return array |
| 432 |
*/ |
| 433 |
public function billing_fields_customization( $fields, $country ) { |
| 434 |
|
| 435 |
$checkout_id = $this->get_conditional_checkout_id(); |
| 436 |
|
| 437 |
if ( ! $checkout_id ) { |
| 438 |
|
| 439 |
return $fields; |
| 440 |
} |
| 441 |
|
| 442 |
if ( ! _is_wcf_meta_custom_checkout( $checkout_id ) || is_wc_endpoint_url( 'edit-address' ) ) { |
| 443 |
return $fields; |
| 444 |
} |
| 445 |
|
| 446 |
$saved_fields = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf_field_order_billing' ); |
| 447 |
|
| 448 |
if ( '' === $saved_fields ) { |
| 449 |
$saved_fields = Cartflows_Helper::get_checkout_fields( 'billing', $checkout_id ); |
| 450 |
} |
| 451 |
|
| 452 |
return $this->prepare_address_fields( $saved_fields, $country, $checkout_id, $fields, 'billing' ); |
| 453 |
} |
| 454 |
|
| 455 |
/** |
| 456 |
* Shipping field customization. |
| 457 |
* |
| 458 |
* @param array $fields fields data. |
| 459 |
* @param string $country country name. |
| 460 |
* @return array |
| 461 |
*/ |
| 462 |
public function shipping_fields_customization( $fields, $country ) { |
| 463 |
|
| 464 |
$checkout_id = $this->get_conditional_checkout_id(); |
| 465 |
|
| 466 |
if ( ! $checkout_id ) { |
| 467 |
return $fields; |
| 468 |
} |
| 469 |
|
| 470 |
if ( ! _is_wcf_meta_custom_checkout( $checkout_id ) || is_wc_endpoint_url( 'edit-address' ) ) { |
| 471 |
return $fields; |
| 472 |
} |
| 473 |
|
| 474 |
$saved_fields = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf_field_order_shipping' ); |
| 475 |
|
| 476 |
if ( '' === $saved_fields ) { |
| 477 |
$saved_fields = Cartflows_Helper::get_checkout_fields( 'shipping', $checkout_id ); |
| 478 |
} |
| 479 |
|
| 480 |
return $this->prepare_address_fields( $saved_fields, $country, $checkout_id, $fields, 'shipping' ); |
| 481 |
} |
| 482 |
|
| 483 |
/** |
| 484 |
* Prepare address fields. |
| 485 |
* |
| 486 |
* @param array $fieldset fieldset data. |
| 487 |
* @param string $country country name. |
| 488 |
* @param int $checkout_id checkout ID. |
| 489 |
* @param bool $original_fieldset is original fieldset. |
| 490 |
* @param string $type address type. |
| 491 |
* |
| 492 |
* @return array |
| 493 |
*/ |
| 494 |
public function prepare_address_fields( $fieldset, $country, $checkout_id, $original_fieldset = false, $type = 'billing' ) { |
| 495 |
|
| 496 |
if ( is_array( $fieldset ) && ! empty( $fieldset ) ) { |
| 497 |
$priority = 0; |
| 498 |
|
| 499 |
$locale = WC()->countries->get_country_locale(); |
| 500 |
|
| 501 |
if ( isset( $locale[ $country ] ) && is_array( $locale[ $country ] ) ) { |
| 502 |
foreach ( $locale[ $country ] as $key => $value ) { |
| 503 |
if ( is_array( $value ) && isset( $fieldset[ $type . '_' . $key ] ) ) { |
| 504 |
if ( isset( $value['required'] ) ) { |
| 505 |
$fieldset[ $type . '_' . $key ]['required'] = $value['required']; |
| 506 |
} |
| 507 |
if ( ! empty( $country ) && 'state' === $key ) { |
| 508 |
$fieldset[ $type . '_' . $key ]['country'] = $country; |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
$all_original_fields = array_merge( $original_fieldset, $fieldset ); |
| 515 |
$original_fieldset = $this->prepare_checkout_fields_lite( $fieldset, $all_original_fields, $checkout_id ); |
| 516 |
|
| 517 |
if ( ! empty( $original_fieldset ) ) { |
| 518 |
foreach ( $original_fieldset as $fieldset_key => $fieldset_value ) { |
| 519 |
if ( ! isset( $fieldset_value['priority'] ) ) { |
| 520 |
$new_priority = $priority + 10; |
| 521 |
$original_fieldset[ $fieldset_key ]['priority'] = $new_priority; |
| 522 |
$priority = $new_priority; |
| 523 |
} else { |
| 524 |
$priority = $fieldset_value['priority']; |
| 525 |
} |
| 526 |
|
| 527 |
if ( isset( $fieldset_value['optimized'] ) && $fieldset_value['optimized'] && ! $fieldset_value['required'] ) { |
| 528 |
$original_fieldset[ $fieldset_key ]['class'][] = 'wcf-hide-field'; |
| 529 |
} |
| 530 |
|
| 531 |
if ( isset( $fieldset_value['type'] ) && 'radio' === $fieldset_value['type'] ) { |
| 532 |
$original_fieldset[ $fieldset_key ]['label_class'][] = 'input-radio'; |
| 533 |
$original_fieldset[ $fieldset_key ]['class'][] = 'wcf-input-radio-field-wrapper'; |
| 534 |
} |
| 535 |
} |
| 536 |
} |
| 537 |
} |
| 538 |
return $original_fieldset; |
| 539 |
} |
| 540 |
|
| 541 |
/** |
| 542 |
* Prepare checkout fields. |
| 543 |
* |
| 544 |
* @param array $fields fields data. |
| 545 |
* @param bool $original_fields is original fields. |
| 546 |
* @param int $checkout_id checkout ID. |
| 547 |
* @return array |
| 548 |
*/ |
| 549 |
public function prepare_checkout_fields_lite( $fields, $original_fields, $checkout_id ) { |
| 550 |
|
| 551 |
if ( is_array( $fields ) && ! empty( $fields ) ) { |
| 552 |
|
| 553 |
$get_ordered_billing_fields = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf_field_order_billing' ); |
| 554 |
$get_ordered_shipping_fields = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf_field_order_shipping' ); |
| 555 |
|
| 556 |
/* Check for array before merged. */ |
| 557 |
$order_checkout_fields = array_merge( $get_ordered_billing_fields, $get_ordered_shipping_fields ); |
| 558 |
|
| 559 |
foreach ( $fields as $name => $field ) { |
| 560 |
|
| 561 |
// Backword compatibility with field enabled. |
| 562 |
if ( isset( $order_checkout_fields[ $name ]['enabled'] ) ) { |
| 563 |
$is_enabled = $order_checkout_fields[ $name ]['enabled']; |
| 564 |
} else { |
| 565 |
$is_enabled = get_post_meta( $checkout_id, 'wcf-' . $name, true ); |
| 566 |
$is_enabled = 'yes' === $is_enabled ? true : false; |
| 567 |
} |
| 568 |
|
| 569 |
// Backword compatibility with field width. |
| 570 |
if ( isset( $order_checkout_fields[ $name ]['width'] ) ) { |
| 571 |
$field_widths = $order_checkout_fields[ $name ]['width']; |
| 572 |
} else { |
| 573 |
$field_widths = get_post_meta( $checkout_id, 'wcf-field-width_' . $name, true ); |
| 574 |
} |
| 575 |
|
| 576 |
// Set/Unset field if checked/unchecked. |
| 577 |
if ( ! $is_enabled ) { |
| 578 |
unset( $original_fields[ $name ] ); |
| 579 |
unset( $fields[ $name ] ); |
| 580 |
} else { |
| 581 |
if ( ! isset( $original_fields[ $name ] ) ) { |
| 582 |
$original_fields[ $name ] = $field; |
| 583 |
} |
| 584 |
|
| 585 |
$field_widths = apply_filters( 'cartflows_checkout_fields_width', $field_widths, $name ); |
| 586 |
|
| 587 |
// Add Custom class if set. |
| 588 |
if ( '' !== $field_widths ) { |
| 589 |
|
| 590 |
$original_fields[ $name ]['class'][] = 'wcf-column-' . $field_widths; |
| 591 |
|
| 592 |
} |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
return $original_fields; |
| 598 |
} |
| 599 |
|
| 600 |
/** |
| 601 |
* Get checkout id based on condition. |
| 602 |
* |
| 603 |
* @return int|bool |
| 604 |
*/ |
| 605 |
public function get_conditional_checkout_id() { |
| 606 |
//phpcs:disable WordPress.Security.NonceVerification |
| 607 |
if ( empty( self::$cached_checkout_id ) ) { |
| 608 |
|
| 609 |
self::$cached_checkout_id = false; |
| 610 |
|
| 611 |
if ( _is_wcf_checkout_type() ) { |
| 612 |
|
| 613 |
global $post; |
| 614 |
|
| 615 |
self::$cached_checkout_id = $post->ID; |
| 616 |
} elseif ( wp_doing_ajax() ) { |
| 617 |
|
| 618 |
if ( isset( $_GET['wcf_checkout_id'] ) ) { |
| 619 |
|
| 620 |
self::$cached_checkout_id = intval( wp_unslash( $_GET['wcf_checkout_id'] ) ); |
| 621 |
} elseif ( isset( $_POST['id'] ) ) { |
| 622 |
|
| 623 |
$is_checkout = wcf()->utils->check_is_checkout_page( intval( wp_unslash( $_POST['id'] ) ) ); |
| 624 |
|
| 625 |
if ( $is_checkout ) { |
| 626 |
self::$cached_checkout_id = intval( wp_unslash( $_POST['id'] ) ); |
| 627 |
} |
| 628 |
} |
| 629 |
} |
| 630 |
} |
| 631 |
//phpcs:enable WordPress.Security.NonceVerification |
| 632 |
|
| 633 |
return self::$cached_checkout_id; |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
/** |
| 638 |
* Change order comments placeholder and label, and set billing phone number to not required. |
| 639 |
* |
| 640 |
* @param array $fields checkout fields. |
| 641 |
* @return array |
| 642 |
*/ |
| 643 |
public function label_skins_fields_customization( $fields ) { |
| 644 |
|
| 645 |
$checkout_id = $this->get_conditional_checkout_id(); |
| 646 |
|
| 647 |
if ( ! $checkout_id ) { |
| 648 |
return $fields; |
| 649 |
} |
| 650 |
|
| 651 |
$fields_skins = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf-fields-skins' ); |
| 652 |
|
| 653 |
// Change the field style to modern labels for older users who are using style one i:e floating labels. |
| 654 |
if ( 'style-one' === $fields_skins ) { |
| 655 |
$fields_skins = 'modern-label'; |
| 656 |
} |
| 657 |
|
| 658 |
$field_types = array( |
| 659 |
'billing', |
| 660 |
'shipping', |
| 661 |
'account', |
| 662 |
'order', |
| 663 |
); |
| 664 |
|
| 665 |
if ( 'modern-label' === $fields_skins ) { |
| 666 |
|
| 667 |
$is_instant = Cartflows_Helper::is_instant_layout_enabled(); |
| 668 |
|
| 669 |
foreach ( $field_types as $type ) { |
| 670 |
|
| 671 |
if ( isset( $fields[ $type ] ) && is_array( $fields[ $type ] ) ) { |
| 672 |
|
| 673 |
foreach ( $fields[ $type ] as $key => $field ) { |
| 674 |
if ( ! empty( $fields[ $type ][ $key ]['type'] ) && 'file' === $fields[ $type ][ $key ]['type'] ) { |
| 675 |
continue; |
| 676 |
} |
| 677 |
|
| 678 |
// Instant Checkout shows the label already, so the box carries an example or nothing. |
| 679 |
if ( $is_instant ) { |
| 680 |
$fields[ $type ][ $key ]['placeholder'] = $this->get_instant_checkout_placeholder( $key, $fields[ $type ][ $key ] ); |
| 681 |
} elseif ( empty( $fields[ $type ][ $key ]['placeholder'] ) ) { |
| 682 |
$fields[ $type ][ $key ]['placeholder'] = $fields[ $type ][ $key ]['label']; |
| 683 |
} |
| 684 |
|
| 685 |
// Remove screen-reader-text class from labels. |
| 686 |
if ( ! empty( $fields[ $type ][ $key ]['label_class'] ) && is_array( $fields[ $type ][ $key ]['label_class'] ) ) { |
| 687 |
$fields[ $type ][ $key ]['label_class'] = array_diff( $fields[ $type ][ $key ]['label_class'], array( 'screen-reader-text' ) ); |
| 688 |
} |
| 689 |
|
| 690 |
// Add Asterisk mark to the placeholder text if the field is required. |
| 691 |
if ( ! $is_instant && ! empty( $fields[ $type ][ $key ]['placeholder'] ) && ( isset( $fields[ $type ][ $key ]['required'] ) && $fields[ $type ][ $key ]['required'] ) ) { |
| 692 |
$fields[ $type ][ $key ]['placeholder'] = $fields[ $type ][ $key ]['placeholder'] . ' *'; |
| 693 |
} |
| 694 |
} |
| 695 |
} |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
return $fields; |
| 700 |
} |
| 701 |
|
| 702 |
/** |
| 703 |
* Add custom class to the fields to change the UI to three column. |
| 704 |
* |
| 705 |
* @param array $fields fields. |
| 706 |
* |
| 707 |
* @return array |
| 708 |
*/ |
| 709 |
public function add_three_column_layout_fields( $fields ) { |
| 710 |
|
| 711 |
$checkout_id = $this->get_conditional_checkout_id(); |
| 712 |
|
| 713 |
if ( ! $checkout_id ) { |
| 714 |
return $fields; |
| 715 |
} |
| 716 |
|
| 717 |
if ( _is_wcf_meta_custom_checkout( $checkout_id ) ) { |
| 718 |
|
| 719 |
// Remove screen-reader-text class from the label even if the field style is set to Default. |
| 720 |
if ( isset( $fields['billing']['billing_address_2'] ) ) { |
| 721 |
|
| 722 |
if ( isset( $fields['billing']['billing_address_2']['label_class'] ) ) { |
| 723 |
$fields['billing']['billing_address_2']['label_class'] = array_diff( $fields['billing']['billing_address_2']['label_class'], array( 'screen-reader-text' ) ); |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
if ( isset( $fields['shipping']['shipping_address_2'] ) ) { |
| 728 |
|
| 729 |
if ( isset( $fields['shipping']['shipping_address_2']['label_class'] ) ) { |
| 730 |
$fields['shipping']['shipping_address_2']['label_class'] = array_diff( $fields['shipping']['shipping_address_2']['label_class'], array( 'screen-reader-text' ) ); |
| 731 |
} |
| 732 |
} |
| 733 |
// Remove screen-reader-text class from the label even if the field style is set to Default. |
| 734 |
|
| 735 |
return $fields; |
| 736 |
} |
| 737 |
|
| 738 |
if ( empty( $fields['billing']['billing_address_2'] ) ) { |
| 739 |
|
| 740 |
if ( isset( $fields['billing']['billing_address_1'] ) && is_array( $fields['billing']['billing_address_1'] ) ) { |
| 741 |
$fields['billing']['billing_address_1']['class'][] = 'form-row-full'; |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
if ( ! empty( $fields['billing']['billing_company'] ) ) { |
| 746 |
|
| 747 |
if ( isset( $fields['billing']['billing_company'] ) && is_array( $fields['billing']['billing_company'] ) ) { |
| 748 |
$fields['billing']['billing_company']['class'][] = 'form-row-full'; |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
if ( ! empty( $fields['shipping']['shipping_company'] ) ) { |
| 753 |
|
| 754 |
if ( isset( $fields['shipping']['shipping_company'] ) && is_array( $fields['shipping']['shipping_company'] ) ) { |
| 755 |
$fields['shipping']['shipping_company']['class'][] = 'form-row-full'; |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
if ( ! empty( $fields['billing']['billing_country'] ) ) { |
| 760 |
|
| 761 |
if ( isset( $fields['billing']['billing_country'] ) && is_array( $fields['billing']['billing_country'] ) ) { |
| 762 |
$fields['billing']['billing_country']['class'][] = 'form-row-full'; |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
if ( ! empty( $fields['shipping']['shipping_country'] ) ) { |
| 767 |
|
| 768 |
if ( isset( $fields['shipping']['shipping_country'] ) && is_array( $fields['shipping']['shipping_country'] ) ) { |
| 769 |
$fields['shipping']['shipping_country']['class'][] = 'form-row-full'; |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
if ( ! empty( $fields['billing']['billing_phone'] ) ) { |
| 774 |
|
| 775 |
if ( isset( $fields['billing']['billing_phone'] ) && is_array( $fields['billing']['billing_phone'] ) ) { |
| 776 |
$fields['billing']['billing_phone']['class'][] = 'form-row-full'; |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
if ( ! empty( $fields['billing']['billing_email'] ) ) { |
| 781 |
|
| 782 |
if ( isset( $fields['billing']['billing_email'] ) && is_array( $fields['billing']['billing_email'] ) ) { |
| 783 |
$fields['billing']['billing_email']['class'][] = 'form-row-full'; |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
if ( empty( $fields['shipping']['shipping_address_2'] ) ) { |
| 788 |
|
| 789 |
if ( isset( $fields['shipping']['shipping_address_1'] ) && is_array( $fields['shipping']['shipping_address_1'] ) ) { |
| 790 |
$fields['shipping']['shipping_address_1']['class'][] = 'form-row-full'; |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
/* Billing address classes for 50% width */ |
| 795 |
if ( |
| 796 |
isset( $fields['billing']['billing_address_1'] ) && |
| 797 |
isset( $fields['billing']['billing_address_2'] ) |
| 798 |
) { |
| 799 |
$fields['billing']['billing_address_1']['class'][] = 'wcf-column-50'; |
| 800 |
$fields['billing']['billing_address_2']['class'][] = 'wcf-column-50'; |
| 801 |
|
| 802 |
if ( isset( $fields['billing']['billing_address_2']['label_class'] ) ) { |
| 803 |
$fields['billing']['billing_address_2']['label_class'] = array_diff( $fields['billing']['billing_address_2']['label_class'], array( 'screen-reader-text' ) ); |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
/* Shipping address classes for 50% width */ |
| 808 |
if ( |
| 809 |
isset( $fields['shipping']['shipping_address_1'] ) && |
| 810 |
isset( $fields['shipping']['shipping_address_2'] ) |
| 811 |
) { |
| 812 |
$fields['shipping']['shipping_address_1']['class'][] = 'wcf-column-50'; |
| 813 |
$fields['shipping']['shipping_address_2']['class'][] = 'wcf-column-50'; |
| 814 |
|
| 815 |
if ( isset( $fields['shipping']['shipping_address_2']['label_class'] ) ) { |
| 816 |
$fields['shipping']['shipping_address_2']['label_class'] = array_diff( $fields['shipping']['shipping_address_2']['label_class'], array( 'screen-reader-text' ) ); |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
/* Billing address classes for 33% width */ |
| 821 |
if ( |
| 822 |
isset( $fields['billing']['billing_city'] ) && |
| 823 |
isset( $fields['billing']['billing_state'] ) && isset( $fields['billing']['billing_postcode'] ) |
| 824 |
) { |
| 825 |
|
| 826 |
$fields['billing']['billing_city']['class'][] = 'wcf-column-33'; |
| 827 |
$fields['billing']['billing_state']['class'][] = 'wcf-column-33'; |
| 828 |
$fields['billing']['billing_postcode']['class'][] = 'wcf-column-33'; |
| 829 |
} |
| 830 |
|
| 831 |
/* Billing address classes for 33% width */ |
| 832 |
if ( |
| 833 |
isset( $fields['shipping']['shipping_city'] ) && |
| 834 |
isset( $fields['shipping']['shipping_state'] ) && isset( $fields['shipping']['shipping_postcode'] ) |
| 835 |
) { |
| 836 |
|
| 837 |
$fields['shipping']['shipping_city']['class'][] = 'wcf-column-33'; |
| 838 |
$fields['shipping']['shipping_state']['class'][] = 'wcf-column-33'; |
| 839 |
$fields['shipping']['shipping_postcode']['class'][] = 'wcf-column-33'; |
| 840 |
} |
| 841 |
|
| 842 |
return $fields; |
| 843 |
} |
| 844 |
|
| 845 |
/** |
| 846 |
* Optimize coupon field. |
| 847 |
* |
| 848 |
* @param array $coupon_field coupon fields. |
| 849 |
* @return mixed |
| 850 |
*/ |
| 851 |
public function optimize_coupon_field( $coupon_field ) { |
| 852 |
|
| 853 |
$coupon_field['class'] = 'wcf-hide-field'; |
| 854 |
|
| 855 |
return $coupon_field; |
| 856 |
} |
| 857 |
|
| 858 |
/** |
| 859 |
* Show/Hide coupon field on checkout page |
| 860 |
* |
| 861 |
* @param bool $is_field true. |
| 862 |
* |
| 863 |
* @return bool |
| 864 |
*/ |
| 865 |
public function show_hide_coupon_field_on_checkout( $is_field ) { |
| 866 |
|
| 867 |
$checkout_id = $this->get_conditional_checkout_id(); |
| 868 |
|
| 869 |
if ( ! $checkout_id ) { |
| 870 |
return $is_field; |
| 871 |
} |
| 872 |
|
| 873 |
$show_coupon_field = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf-show-coupon-field' ); |
| 874 |
$is_field = false; |
| 875 |
|
| 876 |
if ( 'yes' === $show_coupon_field ) { |
| 877 |
|
| 878 |
$is_field = true; |
| 879 |
|
| 880 |
$optimize_coupon_field = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf-optimize-coupon-field' ); |
| 881 |
|
| 882 |
if ( 'yes' === $optimize_coupon_field ) { |
| 883 |
|
| 884 |
add_filter( 'cartflows_coupon_field_options', array( $this, 'optimize_coupon_field' ), 10 ); |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
return $is_field; |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
/** |
| 893 |
* Prepare if class 'Cartflows_Checkout_Fields' exist. |
| 894 |
* Kicking this off by calling 'get_instance()' method |
| 895 |
*/ |
| 896 |
Cartflows_Checkout_Fields::get_instance(); |
| 897 |
|