| 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 |
* Prepare country locale. |
| 284 |
* |
| 285 |
* @param array $fields country locale fields. |
| 286 |
* @return array |
| 287 |
*/ |
| 288 |
public function prepare_country_locale( $fields ) { |
| 289 |
|
| 290 |
$checkout_id = $this->get_conditional_checkout_id(); |
| 291 |
|
| 292 |
if ( ! $checkout_id ) { |
| 293 |
|
| 294 |
return $fields; |
| 295 |
} |
| 296 |
|
| 297 |
if ( is_array( $fields ) ) { |
| 298 |
|
| 299 |
$is_custom_fields_enabled = get_post_meta( $checkout_id, 'wcf-custom-checkout-fields', true ); |
| 300 |
$override_placeholder = apply_filters( 'wcf_field_override_placeholder', true ); |
| 301 |
$override_label = apply_filters( 'wcf_field_override_label', true ); |
| 302 |
|
| 303 |
if ( 'yes' === $is_custom_fields_enabled ) { |
| 304 |
foreach ( $fields as $key => $props ) { |
| 305 |
if ( isset( $props['priority'] ) ) { |
| 306 |
unset( $fields[ $key ]['priority'] ); |
| 307 |
} |
| 308 |
|
| 309 |
if ( $override_placeholder && isset( $props['placeholder'] ) ) { |
| 310 |
unset( $fields[ $key ]['placeholder'] ); |
| 311 |
} |
| 312 |
if ( $override_label && isset( $props['label'] ) ) { |
| 313 |
unset( $fields[ $key ]['label'] ); |
| 314 |
} |
| 315 |
} |
| 316 |
} else { |
| 317 |
|
| 318 |
$fields_skins = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf-fields-skins' ); |
| 319 |
|
| 320 |
// Change the field style to modern labels for older users who are using style one i:e floating labels. |
| 321 |
if ( 'style-one' === $fields_skins ) { |
| 322 |
$fields_skins = 'modern-label'; |
| 323 |
} |
| 324 |
|
| 325 |
if ( 'modern-label' === $fields_skins ) { |
| 326 |
|
| 327 |
foreach ( $fields as $key => $props ) { |
| 328 |
|
| 329 |
// Add label as placeholder if the placeholder value is empty. |
| 330 |
if ( empty( $fields[ $key ]['placeholder'] ) && ! empty( $fields[ $key ]['label'] ) ) { |
| 331 |
$fields[ $key ]['placeholder'] = $fields[ $key ]['label']; |
| 332 |
} |
| 333 |
|
| 334 |
if ( isset( $fields[ $key ]['label_class'] ) ) { |
| 335 |
$fields[ $key ]['label_class'] = array_diff( $fields[ $key ]['label_class'], array( 'screen-reader-text' ) ); |
| 336 |
} |
| 337 |
|
| 338 |
// Add Asterisk mark to the placeholder text if the field is required. |
| 339 |
if ( ! empty( $fields[ $key ]['placeholder'] ) && ( isset( $fields[ $key ]['required'] ) && $fields[ $key ]['required'] ) ) { |
| 340 |
$fields[ $key ]['placeholder'] = $fields[ $key ]['placeholder'] . ' *'; |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
return $fields; |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Billing field customization. |
| 351 |
* |
| 352 |
* @param array $fields fields data. |
| 353 |
* @param string $country country name. |
| 354 |
* @return array |
| 355 |
*/ |
| 356 |
public function billing_fields_customization( $fields, $country ) { |
| 357 |
|
| 358 |
$checkout_id = $this->get_conditional_checkout_id(); |
| 359 |
|
| 360 |
if ( ! $checkout_id ) { |
| 361 |
|
| 362 |
return $fields; |
| 363 |
} |
| 364 |
|
| 365 |
if ( ! _is_wcf_meta_custom_checkout( $checkout_id ) || is_wc_endpoint_url( 'edit-address' ) ) { |
| 366 |
return $fields; |
| 367 |
} |
| 368 |
|
| 369 |
$saved_fields = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf_field_order_billing' ); |
| 370 |
|
| 371 |
if ( '' === $saved_fields ) { |
| 372 |
$saved_fields = Cartflows_Helper::get_checkout_fields( 'billing', $checkout_id ); |
| 373 |
} |
| 374 |
|
| 375 |
return $this->prepare_address_fields( $saved_fields, $country, $checkout_id, $fields, 'billing' ); |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* Shipping field customization. |
| 380 |
* |
| 381 |
* @param array $fields fields data. |
| 382 |
* @param string $country country name. |
| 383 |
* @return array |
| 384 |
*/ |
| 385 |
public function shipping_fields_customization( $fields, $country ) { |
| 386 |
|
| 387 |
$checkout_id = $this->get_conditional_checkout_id(); |
| 388 |
|
| 389 |
if ( ! $checkout_id ) { |
| 390 |
return $fields; |
| 391 |
} |
| 392 |
|
| 393 |
if ( ! _is_wcf_meta_custom_checkout( $checkout_id ) || is_wc_endpoint_url( 'edit-address' ) ) { |
| 394 |
return $fields; |
| 395 |
} |
| 396 |
|
| 397 |
$saved_fields = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf_field_order_shipping' ); |
| 398 |
|
| 399 |
if ( '' === $saved_fields ) { |
| 400 |
$saved_fields = Cartflows_Helper::get_checkout_fields( 'shipping', $checkout_id ); |
| 401 |
} |
| 402 |
|
| 403 |
return $this->prepare_address_fields( $saved_fields, $country, $checkout_id, $fields, 'shipping' ); |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Prepare address fields. |
| 408 |
* |
| 409 |
* @param array $fieldset fieldset data. |
| 410 |
* @param string $country country name. |
| 411 |
* @param int $checkout_id checkout ID. |
| 412 |
* @param bool $original_fieldset is original fieldset. |
| 413 |
* @param string $type address type. |
| 414 |
* |
| 415 |
* @return array |
| 416 |
*/ |
| 417 |
public function prepare_address_fields( $fieldset, $country, $checkout_id, $original_fieldset = false, $type = 'billing' ) { |
| 418 |
|
| 419 |
if ( is_array( $fieldset ) && ! empty( $fieldset ) ) { |
| 420 |
$priority = 0; |
| 421 |
|
| 422 |
$locale = WC()->countries->get_country_locale(); |
| 423 |
|
| 424 |
if ( isset( $locale[ $country ] ) && is_array( $locale[ $country ] ) ) { |
| 425 |
foreach ( $locale[ $country ] as $key => $value ) { |
| 426 |
if ( is_array( $value ) && isset( $fieldset[ $type . '_' . $key ] ) ) { |
| 427 |
if ( isset( $value['required'] ) ) { |
| 428 |
$fieldset[ $type . '_' . $key ]['required'] = $value['required']; |
| 429 |
} |
| 430 |
if ( ! empty( $country ) && 'state' === $key ) { |
| 431 |
$fieldset[ $type . '_' . $key ]['country'] = $country; |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
$all_original_fields = array_merge( $original_fieldset, $fieldset ); |
| 438 |
$original_fieldset = $this->prepare_checkout_fields_lite( $fieldset, $all_original_fields, $checkout_id ); |
| 439 |
|
| 440 |
if ( ! empty( $original_fieldset ) ) { |
| 441 |
foreach ( $original_fieldset as $fieldset_key => $fieldset_value ) { |
| 442 |
if ( ! isset( $fieldset_value['priority'] ) ) { |
| 443 |
$new_priority = $priority + 10; |
| 444 |
$original_fieldset[ $fieldset_key ]['priority'] = $new_priority; |
| 445 |
$priority = $new_priority; |
| 446 |
} else { |
| 447 |
$priority = $fieldset_value['priority']; |
| 448 |
} |
| 449 |
|
| 450 |
if ( isset( $fieldset_value['optimized'] ) && $fieldset_value['optimized'] && ! $fieldset_value['required'] ) { |
| 451 |
$original_fieldset[ $fieldset_key ]['class'][] = 'wcf-hide-field'; |
| 452 |
} |
| 453 |
|
| 454 |
if ( isset( $fieldset_value['type'] ) && 'radio' === $fieldset_value['type'] ) { |
| 455 |
$original_fieldset[ $fieldset_key ]['label_class'][] = 'input-radio'; |
| 456 |
$original_fieldset[ $fieldset_key ]['class'][] = 'wcf-input-radio-field-wrapper'; |
| 457 |
} |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
return $original_fieldset; |
| 462 |
} |
| 463 |
|
| 464 |
/** |
| 465 |
* Prepare checkout fields. |
| 466 |
* |
| 467 |
* @param array $fields fields data. |
| 468 |
* @param bool $original_fields is original fields. |
| 469 |
* @param int $checkout_id checkout ID. |
| 470 |
* @return array |
| 471 |
*/ |
| 472 |
public function prepare_checkout_fields_lite( $fields, $original_fields, $checkout_id ) { |
| 473 |
|
| 474 |
if ( is_array( $fields ) && ! empty( $fields ) ) { |
| 475 |
|
| 476 |
$get_ordered_billing_fields = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf_field_order_billing' ); |
| 477 |
$get_ordered_shipping_fields = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf_field_order_shipping' ); |
| 478 |
|
| 479 |
/* Check for array before merged. */ |
| 480 |
$order_checkout_fields = array_merge( $get_ordered_billing_fields, $get_ordered_shipping_fields ); |
| 481 |
|
| 482 |
foreach ( $fields as $name => $field ) { |
| 483 |
|
| 484 |
// Backword compatibility with field enabled. |
| 485 |
if ( isset( $order_checkout_fields[ $name ]['enabled'] ) ) { |
| 486 |
$is_enabled = $order_checkout_fields[ $name ]['enabled']; |
| 487 |
} else { |
| 488 |
$is_enabled = get_post_meta( $checkout_id, 'wcf-' . $name, true ); |
| 489 |
$is_enabled = 'yes' === $is_enabled ? true : false; |
| 490 |
} |
| 491 |
|
| 492 |
// Backword compatibility with field width. |
| 493 |
if ( isset( $order_checkout_fields[ $name ]['width'] ) ) { |
| 494 |
$field_widths = $order_checkout_fields[ $name ]['width']; |
| 495 |
} else { |
| 496 |
$field_widths = get_post_meta( $checkout_id, 'wcf-field-width_' . $name, true ); |
| 497 |
} |
| 498 |
|
| 499 |
// Set/Unset field if checked/unchecked. |
| 500 |
if ( ! $is_enabled ) { |
| 501 |
unset( $original_fields[ $name ] ); |
| 502 |
unset( $fields[ $name ] ); |
| 503 |
} else { |
| 504 |
if ( ! isset( $original_fields[ $name ] ) ) { |
| 505 |
$original_fields[ $name ] = $field; |
| 506 |
} |
| 507 |
|
| 508 |
$field_widths = apply_filters( 'cartflows_checkout_fields_width', $field_widths, $name ); |
| 509 |
|
| 510 |
// Add Custom class if set. |
| 511 |
if ( '' !== $field_widths ) { |
| 512 |
|
| 513 |
$original_fields[ $name ]['class'][] = 'wcf-column-' . $field_widths; |
| 514 |
|
| 515 |
} |
| 516 |
} |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
return $original_fields; |
| 521 |
} |
| 522 |
|
| 523 |
/** |
| 524 |
* Get checkout id based on condition. |
| 525 |
* |
| 526 |
* @return int|bool |
| 527 |
*/ |
| 528 |
public function get_conditional_checkout_id() { |
| 529 |
//phpcs:disable WordPress.Security.NonceVerification |
| 530 |
if ( empty( self::$cached_checkout_id ) ) { |
| 531 |
|
| 532 |
self::$cached_checkout_id = false; |
| 533 |
|
| 534 |
if ( _is_wcf_checkout_type() ) { |
| 535 |
|
| 536 |
global $post; |
| 537 |
|
| 538 |
self::$cached_checkout_id = $post->ID; |
| 539 |
} elseif ( wp_doing_ajax() ) { |
| 540 |
|
| 541 |
if ( isset( $_GET['wcf_checkout_id'] ) ) { |
| 542 |
|
| 543 |
self::$cached_checkout_id = intval( wp_unslash( $_GET['wcf_checkout_id'] ) ); |
| 544 |
} elseif ( isset( $_POST['id'] ) ) { |
| 545 |
|
| 546 |
$is_checkout = wcf()->utils->check_is_checkout_page( intval( wp_unslash( $_POST['id'] ) ) ); |
| 547 |
|
| 548 |
if ( $is_checkout ) { |
| 549 |
self::$cached_checkout_id = intval( wp_unslash( $_POST['id'] ) ); |
| 550 |
} |
| 551 |
} |
| 552 |
} |
| 553 |
} |
| 554 |
//phpcs:enable WordPress.Security.NonceVerification |
| 555 |
|
| 556 |
return self::$cached_checkout_id; |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
/** |
| 561 |
* Change order comments placeholder and label, and set billing phone number to not required. |
| 562 |
* |
| 563 |
* @param array $fields checkout fields. |
| 564 |
* @return array |
| 565 |
*/ |
| 566 |
public function label_skins_fields_customization( $fields ) { |
| 567 |
|
| 568 |
$checkout_id = $this->get_conditional_checkout_id(); |
| 569 |
|
| 570 |
if ( ! $checkout_id ) { |
| 571 |
return $fields; |
| 572 |
} |
| 573 |
|
| 574 |
$fields_skins = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf-fields-skins' ); |
| 575 |
|
| 576 |
// Change the field style to modern labels for older users who are using style one i:e floating labels. |
| 577 |
if ( 'style-one' === $fields_skins ) { |
| 578 |
$fields_skins = 'modern-label'; |
| 579 |
} |
| 580 |
|
| 581 |
$field_types = array( |
| 582 |
'billing', |
| 583 |
'shipping', |
| 584 |
'account', |
| 585 |
'order', |
| 586 |
); |
| 587 |
|
| 588 |
if ( 'modern-label' === $fields_skins ) { |
| 589 |
|
| 590 |
foreach ( $field_types as $type ) { |
| 591 |
|
| 592 |
if ( isset( $fields[ $type ] ) && is_array( $fields[ $type ] ) ) { |
| 593 |
|
| 594 |
foreach ( $fields[ $type ] as $key => $field ) { |
| 595 |
if ( ! empty( $fields[ $type ][ $key ]['type'] ) && 'file' === $fields[ $type ][ $key ]['type'] ) { |
| 596 |
continue; |
| 597 |
} |
| 598 |
// Add label as placeholder if the placeholder value is empty. |
| 599 |
if ( empty( $fields[ $type ][ $key ]['placeholder'] ) ) { |
| 600 |
$fields[ $type ][ $key ]['placeholder'] = $fields[ $type ][ $key ]['label']; |
| 601 |
} |
| 602 |
|
| 603 |
// Remove screen-reader-text class from labels. |
| 604 |
if ( ! empty( $fields[ $type ][ $key ]['label_class'] ) && is_array( $fields[ $type ][ $key ]['label_class'] ) ) { |
| 605 |
$fields[ $type ][ $key ]['label_class'] = array_diff( $fields[ $type ][ $key ]['label_class'], array( 'screen-reader-text' ) ); |
| 606 |
} |
| 607 |
|
| 608 |
// Add Asterisk mark to the placeholder text if the field is required. |
| 609 |
if ( ! empty( $fields[ $type ][ $key ]['placeholder'] ) && ( isset( $fields[ $type ][ $key ]['required'] ) && $fields[ $type ][ $key ]['required'] ) ) { |
| 610 |
$fields[ $type ][ $key ]['placeholder'] = $fields[ $type ][ $key ]['placeholder'] . ' *'; |
| 611 |
} |
| 612 |
} |
| 613 |
} |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
return $fields; |
| 618 |
} |
| 619 |
|
| 620 |
/** |
| 621 |
* Add custom class to the fields to change the UI to three column. |
| 622 |
* |
| 623 |
* @param array $fields fields. |
| 624 |
* |
| 625 |
* @return array |
| 626 |
*/ |
| 627 |
public function add_three_column_layout_fields( $fields ) { |
| 628 |
|
| 629 |
$checkout_id = $this->get_conditional_checkout_id(); |
| 630 |
|
| 631 |
if ( ! $checkout_id ) { |
| 632 |
return $fields; |
| 633 |
} |
| 634 |
|
| 635 |
if ( _is_wcf_meta_custom_checkout( $checkout_id ) ) { |
| 636 |
|
| 637 |
// Remove screen-reader-text class from the label even if the field style is set to Default. |
| 638 |
if ( isset( $fields['billing']['billing_address_2'] ) ) { |
| 639 |
|
| 640 |
if ( isset( $fields['billing']['billing_address_2']['label_class'] ) ) { |
| 641 |
$fields['billing']['billing_address_2']['label_class'] = array_diff( $fields['billing']['billing_address_2']['label_class'], array( 'screen-reader-text' ) ); |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
if ( isset( $fields['shipping']['shipping_address_2'] ) ) { |
| 646 |
|
| 647 |
if ( isset( $fields['shipping']['shipping_address_2']['label_class'] ) ) { |
| 648 |
$fields['shipping']['shipping_address_2']['label_class'] = array_diff( $fields['shipping']['shipping_address_2']['label_class'], array( 'screen-reader-text' ) ); |
| 649 |
} |
| 650 |
} |
| 651 |
// Remove screen-reader-text class from the label even if the field style is set to Default. |
| 652 |
|
| 653 |
return $fields; |
| 654 |
} |
| 655 |
|
| 656 |
if ( empty( $fields['billing']['billing_address_2'] ) ) { |
| 657 |
|
| 658 |
if ( isset( $fields['billing']['billing_address_1'] ) && is_array( $fields['billing']['billing_address_1'] ) ) { |
| 659 |
$fields['billing']['billing_address_1']['class'][] = 'form-row-full'; |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
if ( ! empty( $fields['billing']['billing_company'] ) ) { |
| 664 |
|
| 665 |
if ( isset( $fields['billing']['billing_company'] ) && is_array( $fields['billing']['billing_company'] ) ) { |
| 666 |
$fields['billing']['billing_company']['class'][] = 'form-row-full'; |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
if ( ! empty( $fields['shipping']['shipping_company'] ) ) { |
| 671 |
|
| 672 |
if ( isset( $fields['shipping']['shipping_company'] ) && is_array( $fields['shipping']['shipping_company'] ) ) { |
| 673 |
$fields['shipping']['shipping_company']['class'][] = 'form-row-full'; |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
if ( ! empty( $fields['billing']['billing_country'] ) ) { |
| 678 |
|
| 679 |
if ( isset( $fields['billing']['billing_country'] ) && is_array( $fields['billing']['billing_country'] ) ) { |
| 680 |
$fields['billing']['billing_country']['class'][] = 'form-row-full'; |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
if ( ! empty( $fields['shipping']['shipping_country'] ) ) { |
| 685 |
|
| 686 |
if ( isset( $fields['shipping']['shipping_country'] ) && is_array( $fields['shipping']['shipping_country'] ) ) { |
| 687 |
$fields['shipping']['shipping_country']['class'][] = 'form-row-full'; |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
if ( ! empty( $fields['billing']['billing_phone'] ) ) { |
| 692 |
|
| 693 |
if ( isset( $fields['billing']['billing_phone'] ) && is_array( $fields['billing']['billing_phone'] ) ) { |
| 694 |
$fields['billing']['billing_phone']['class'][] = 'form-row-full'; |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
if ( ! empty( $fields['billing']['billing_email'] ) ) { |
| 699 |
|
| 700 |
if ( isset( $fields['billing']['billing_email'] ) && is_array( $fields['billing']['billing_email'] ) ) { |
| 701 |
$fields['billing']['billing_email']['class'][] = 'form-row-full'; |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
if ( empty( $fields['shipping']['shipping_address_2'] ) ) { |
| 706 |
|
| 707 |
if ( isset( $fields['shipping']['shipping_address_1'] ) && is_array( $fields['shipping']['shipping_address_1'] ) ) { |
| 708 |
$fields['shipping']['shipping_address_1']['class'][] = 'form-row-full'; |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
/* Billing address classes for 50% width */ |
| 713 |
if ( |
| 714 |
isset( $fields['billing']['billing_address_1'] ) && |
| 715 |
isset( $fields['billing']['billing_address_2'] ) |
| 716 |
) { |
| 717 |
$fields['billing']['billing_address_1']['class'][] = 'wcf-column-50'; |
| 718 |
$fields['billing']['billing_address_2']['class'][] = 'wcf-column-50'; |
| 719 |
|
| 720 |
if ( isset( $fields['billing']['billing_address_2']['label_class'] ) ) { |
| 721 |
$fields['billing']['billing_address_2']['label_class'] = array_diff( $fields['billing']['billing_address_2']['label_class'], array( 'screen-reader-text' ) ); |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
/* Shipping address classes for 50% width */ |
| 726 |
if ( |
| 727 |
isset( $fields['shipping']['shipping_address_1'] ) && |
| 728 |
isset( $fields['shipping']['shipping_address_2'] ) |
| 729 |
) { |
| 730 |
$fields['shipping']['shipping_address_1']['class'][] = 'wcf-column-50'; |
| 731 |
$fields['shipping']['shipping_address_2']['class'][] = 'wcf-column-50'; |
| 732 |
|
| 733 |
if ( isset( $fields['shipping']['shipping_address_2']['label_class'] ) ) { |
| 734 |
$fields['shipping']['shipping_address_2']['label_class'] = array_diff( $fields['shipping']['shipping_address_2']['label_class'], array( 'screen-reader-text' ) ); |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
/* Billing address classes for 33% width */ |
| 739 |
if ( |
| 740 |
isset( $fields['billing']['billing_city'] ) && |
| 741 |
isset( $fields['billing']['billing_state'] ) && isset( $fields['billing']['billing_postcode'] ) |
| 742 |
) { |
| 743 |
|
| 744 |
$fields['billing']['billing_city']['class'][] = 'wcf-column-33'; |
| 745 |
$fields['billing']['billing_state']['class'][] = 'wcf-column-33'; |
| 746 |
$fields['billing']['billing_postcode']['class'][] = 'wcf-column-33'; |
| 747 |
} |
| 748 |
|
| 749 |
/* Billing address classes for 33% width */ |
| 750 |
if ( |
| 751 |
isset( $fields['shipping']['shipping_city'] ) && |
| 752 |
isset( $fields['shipping']['shipping_state'] ) && isset( $fields['shipping']['shipping_postcode'] ) |
| 753 |
) { |
| 754 |
|
| 755 |
$fields['shipping']['shipping_city']['class'][] = 'wcf-column-33'; |
| 756 |
$fields['shipping']['shipping_state']['class'][] = 'wcf-column-33'; |
| 757 |
$fields['shipping']['shipping_postcode']['class'][] = 'wcf-column-33'; |
| 758 |
} |
| 759 |
|
| 760 |
return $fields; |
| 761 |
} |
| 762 |
|
| 763 |
/** |
| 764 |
* Optimize coupon field. |
| 765 |
* |
| 766 |
* @param array $coupon_field coupon fields. |
| 767 |
* @return mixed |
| 768 |
*/ |
| 769 |
public function optimize_coupon_field( $coupon_field ) { |
| 770 |
|
| 771 |
$coupon_field['class'] = 'wcf-hide-field'; |
| 772 |
|
| 773 |
return $coupon_field; |
| 774 |
} |
| 775 |
|
| 776 |
/** |
| 777 |
* Show/Hide coupon field on checkout page |
| 778 |
* |
| 779 |
* @param bool $is_field true. |
| 780 |
* |
| 781 |
* @return bool |
| 782 |
*/ |
| 783 |
public function show_hide_coupon_field_on_checkout( $is_field ) { |
| 784 |
|
| 785 |
$checkout_id = $this->get_conditional_checkout_id(); |
| 786 |
|
| 787 |
if ( ! $checkout_id ) { |
| 788 |
return $is_field; |
| 789 |
} |
| 790 |
|
| 791 |
$show_coupon_field = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf-show-coupon-field' ); |
| 792 |
$is_field = false; |
| 793 |
|
| 794 |
if ( 'yes' === $show_coupon_field ) { |
| 795 |
|
| 796 |
$is_field = true; |
| 797 |
|
| 798 |
$optimize_coupon_field = wcf()->options->get_checkout_meta_value( $checkout_id, 'wcf-optimize-coupon-field' ); |
| 799 |
|
| 800 |
if ( 'yes' === $optimize_coupon_field ) { |
| 801 |
|
| 802 |
add_filter( 'cartflows_coupon_field_options', array( $this, 'optimize_coupon_field' ), 10 ); |
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
return $is_field; |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
/** |
| 811 |
* Prepare if class 'Cartflows_Checkout_Fields' exist. |
| 812 |
* Kicking this off by calling 'get_instance()' method |
| 813 |
*/ |
| 814 |
Cartflows_Checkout_Fields::get_instance(); |
| 815 |
|