Messages keyed by FIELD_BILLING / FIELD_SHIPPING. */ public static function all_for_block_checkout(): array { $messages = array(); foreach ( array( self::FIELD_BILLING, self::FIELD_SHIPPING ) as $field ) { $field_messages = array(); foreach ( self::ERROR_CODES as $error_code ) { $field_messages[] = self::for_block_checkout( $error_code, $field ); } $messages[ $field ] = array_values( array_unique( $field_messages ) ); } return $messages; } /** * Get the message for block checkout (Store API) responses. * * Block checkout shows errors in a generic banner, so the message names the field. * * @param string|null $error_code One of the ValidationResult::ERROR_* constants. * @param string $field FIELD_BILLING or FIELD_SHIPPING. * @return string The translated message. */ public static function for_block_checkout( ?string $error_code, string $field ): string { $field_label = self::FIELD_SHIPPING === $field ? __( 'Shipping phone', 'shift64-phone-validation' ) : __( 'Billing phone', 'shift64-phone-validation' ); if ( ValidationResult::ERROR_MISSING_INTERNATIONAL_PREFIX === $error_code ) { /* translators: %s: field label (Billing phone or Shipping phone) */ $message = sprintf( __( '%s must contain country prefix.', 'shift64-phone-validation' ), $field_label ); } else { /* translators: %s: field label (Billing phone or Shipping phone) */ $message = sprintf( __( '%s is not a valid phone number.', 'shift64-phone-validation' ), $field_label ); } return self::filter( $message, $error_code, $field, self::CONTEXT_BLOCK ); } /** * Let themes reword a customer-facing message. * * @param string $message Default translated message. * @param string|null $error_code One of the ValidationResult::ERROR_* constants. * @param string $field FIELD_BILLING or FIELD_SHIPPING. * @param string $context CONTEXT_CLASSIC or CONTEXT_BLOCK. * @return string */ private static function filter( string $message, ?string $error_code, string $field, string $context ): string { /** * Filters the validation message shown to the customer. * * @param string $message Default translated message. * @param string|null $error_code Machine-readable reason, see ValidationResult::ERROR_*. * @param string $field 'billing' or 'shipping'. * @param string $context 'classic' or 'block'. */ $filtered = apply_filters( 'shift64_phone_validation_error_message', $message, $error_code, $field, $context ); return is_string( $filtered ) && '' !== $filtered ? $filtered : $message; } }