← All changes
|
modules/checkout/classes/class-cartflows-checkout-fields.php
+88
-6
3.2.0
→
3.3.0
View file →
| @@ -279,8 +279,81 @@ | ||
| 279 | 279 | } |
| 280 | 280 | |
| 281 | 281 | |
| 282 | 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 | + /** | |
| 283 | 356 | * Prepare country locale. |
| 284 | 357 | * |
| 285 | 358 | * @param array $fields country locale fields. |
| 286 | 359 | * @return array |
| @@ -323,12 +396,16 @@ | ||
| 323 | 396 | } |
| 324 | 397 | |
| 325 | 398 | if ( 'modern-label' === $fields_skins ) { |
| 326 | 399 | |
| 400 | + $is_instant = Cartflows_Helper::is_instant_layout_enabled(); | |
| 401 | + | |
| 327 | 402 | foreach ( $fields as $key => $props ) { |
| 328 | 403 | |
| 329 | - // Add label as placeholder if the placeholder value is empty. | |
| 330 | - if ( empty( $fields[ $key ]['placeholder'] ) && ! empty( $fields[ $key ]['label'] ) ) { | |
| 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'] ) ) { | |
| 331 | 408 | $fields[ $key ]['placeholder'] = $fields[ $key ]['label']; |
| 332 | 409 | } |
| 333 | 410 | |
| 334 | 411 | if ( isset( $fields[ $key ]['label_class'] ) ) { |
| @@ -335,9 +412,9 @@ | ||
| 335 | 412 | $fields[ $key ]['label_class'] = array_diff( $fields[ $key ]['label_class'], array( 'screen-reader-text' ) ); |
| 336 | 413 | } |
| 337 | 414 | |
| 338 | 415 | // 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'] ) ) { | |
| 416 | + if ( ! $is_instant && ! empty( $fields[ $key ]['placeholder'] ) && ( isset( $fields[ $key ]['required'] ) && $fields[ $key ]['required'] ) ) { | |
| 340 | 417 | $fields[ $key ]['placeholder'] = $fields[ $key ]['placeholder'] . ' *'; |
| 341 | 418 | } |
| 342 | 419 | } |
| 343 | 420 | } |
| @@ -586,8 +663,10 @@ | ||
| 586 | 663 | ); |
| 587 | 664 | |
| 588 | 665 | if ( 'modern-label' === $fields_skins ) { |
| 589 | 666 | |
| 667 | + $is_instant = Cartflows_Helper::is_instant_layout_enabled(); | |
| 668 | + | |
| 590 | 669 | foreach ( $field_types as $type ) { |
| 591 | 670 | |
| 592 | 671 | if ( isset( $fields[ $type ] ) && is_array( $fields[ $type ] ) ) { |
| 593 | 672 | |
| @@ -594,10 +673,13 @@ | ||
| 594 | 673 | foreach ( $fields[ $type ] as $key => $field ) { |
| 595 | 674 | if ( ! empty( $fields[ $type ][ $key ]['type'] ) && 'file' === $fields[ $type ][ $key ]['type'] ) { |
| 596 | 675 | continue; |
| 597 | 676 | } |
| 598 | - // Add label as placeholder if the placeholder value is empty. | |
| 599 | - if ( empty( $fields[ $type ][ $key ]['placeholder'] ) ) { | |
| 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'] ) ) { | |
| 600 | 682 | $fields[ $type ][ $key ]['placeholder'] = $fields[ $type ][ $key ]['label']; |
| 601 | 683 | } |
| 602 | 684 | |
| 603 | 685 | // Remove screen-reader-text class from labels. |
| @@ -605,9 +687,9 @@ | ||
| 605 | 687 | $fields[ $type ][ $key ]['label_class'] = array_diff( $fields[ $type ][ $key ]['label_class'], array( 'screen-reader-text' ) ); |
| 606 | 688 | } |
| 607 | 689 | |
| 608 | 690 | // 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'] ) ) { | |
| 691 | + if ( ! $is_instant && ! empty( $fields[ $type ][ $key ]['placeholder'] ) && ( isset( $fields[ $type ][ $key ]['required'] ) && $fields[ $type ][ $key ]['required'] ) ) { | |
| 610 | 692 | $fields[ $type ][ $key ]['placeholder'] = $fields[ $type ][ $key ]['placeholder'] . ' *'; |
| 611 | 693 | } |
| 612 | 694 | } |
| 613 | 695 | } |