Addresses: the saved address line has the number too. add_filter( 'woocommerce_process_myaccount_field_billing_address_1', array( self::class, 'join_account_address' ) ); add_filter( 'woocommerce_process_myaccount_field_shipping_address_1', array( self::class, 'join_account_address' ) ); } /** * The street and the number as one address line, in the country's order; the street alone when * there is no number or it is already in it. * * @param string $street Street. * @param string $number House number. * @param string $country Country code. */ public static function joined( string $street, string $number, string $country ): string { $street = trim( $street ); $number = trim( $number ); // Already written into the street box (autofill, a customer who typed both there). if ( '' === $number || '' === $street || preg_match( '/(^|\s)' . preg_quote( $number, '/' ) . '(\s|,|$)/u', $street ) ) { return $street; } return in_array( strtoupper( $country ), self::number_first_countries(), true ) ? $number . ' ' . $street : $street . ' ' . $number; } /** * The address line saved from My Account, with the house number typed next to it. * * @param mixed $value The address line (sanitized by WooCommerce). * @return mixed */ public static function join_account_address( $value ) { if ( ! is_string( $value ) || 'off' === self::split_mode() ) { return $value; } $type = 0 === strpos( (string) current_filter(), 'woocommerce_process_myaccount_field_shipping' ) ? 'shipping' : 'billing'; // phpcs:disable WordPress.Security.NonceVerification.Missing -- WooCommerce checked the form's nonce before this filter. $number = isset( $_POST[ $type . '_house_number' ] ) && is_string( $_POST[ $type . '_house_number' ] ) ? sanitize_text_field( wp_unslash( $_POST[ $type . '_house_number' ] ) ) : ''; $country = isset( $_POST[ $type . '_country' ] ) && is_string( $_POST[ $type . '_country' ] ) ? sanitize_text_field( wp_unslash( $_POST[ $type . '_country' ] ) ) : ''; // phpcs:enable return self::joined( $value, $number, $country ); } /** * Countries whose addresses put the postcode before the city. * * @return string[] */ public static function postcode_first_countries(): array { /** * Filters the countries whose checkout shows the postcode before the city. * * @since 1.6.0 * * @param string[] $countries Country codes. */ return (array) apply_filters( 'aiolos_postcode_first_countries', array( 'AT', 'BE', 'BG', 'CH', 'CY', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FR', 'GR', 'HR', 'HU', 'IS', 'IT', 'LI', 'LT', 'LU', 'MT', 'NL', 'NO', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK' ) ); } /** * Countries where the house number follows the street ("auto" of the setting). * * @return string[] */ public static function split_countries(): array { /** * Filters the countries that get a separate house number field when "House number in its * own field" is on "Where the number follows the street". * * @since 1.6.0 * * @param string[] $countries Country codes. */ return (array) apply_filters( 'aiolos_split_address_countries', array( 'AT', 'BE', 'CH', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'GR', 'HR', 'HU', 'IS', 'IT', 'LI', 'LT', 'LV', 'NL', 'NO', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK' ) ); } /** * Countries that write the number before the street ("12 Main Street"), for joining the two. * * @return string[] */ public static function number_first_countries(): array { /** * Filters the countries that write the house number before the street. * * @since 1.6.0 * * @param string[] $countries Country codes. */ return (array) apply_filters( 'aiolos_house_number_first_countries', array( 'AU', 'CA', 'FR', 'GB', 'IE', 'LU', 'MC', 'NZ', 'US' ) ); } /** * The house number setting: off, auto or always. */ public static function split_mode(): string { $mode = (string) Settings::get( 'split_address', 'auto' ); return in_array( $mode, array( 'off', 'auto', 'always' ), true ) ? $mode : 'auto'; } /** * Adds the house number field next to the street (both addresses, My Account included). * * @param array $fields Default address fields. * @return array */ public static function default_fields( $fields ) { if ( ! is_array( $fields ) || 'off' === self::split_mode() || ! isset( $fields['address_1'] ) || ( is_admin() && ! wp_doing_ajax() ) ) { return $fields; } $fields['address_1']['class'] = array_values( array_unique( array_merge( (array) ( $fields['address_1']['class'] ?? array() ), array( 'acw-street' ) ) ) ); $fields['house_number'] = array( 'label' => __( 'House number', 'aiolos-checkout-for-woocommerce' ), 'required' => false, 'class' => array( 'form-row-last', 'acw-house-number' ), 'priority' => (int) ( $fields['address_1']['priority'] ?? 50 ) + 2, 'autocomplete' => 'off', ); if ( 'always' === self::split_mode() ) { $fields['address_1']['label'] = __( 'Street', 'aiolos-checkout-for-woocommerce' ); $fields['address_1']['placeholder'] = __( 'Street name', 'aiolos-checkout-for-woocommerce' ); } return $fields; } /** * The house number rows, for WooCommerce's address script (it applies each country's locale). * * @param array $selectors Field key => CSS selector. * @return array */ public static function field_selectors( $selectors ) { if ( is_array( $selectors ) && 'off' !== self::split_mode() ) { $selectors['house_number'] = '#billing_house_number_field, #shipping_house_number_field'; } return $selectors; } /** * Default locale: the house number is hidden unless the country asks for it ("auto"). * * @param array $locale Default locale. * @return array */ public static function locale_default( $locale ) { if ( is_array( $locale ) && 'auto' === self::split_mode() ) { $locale['house_number'] = array( 'hidden' => true, 'required' => false, ); } return $locale; } /** * Per country: postcode before the city where it is written so; the house number field where the * number follows the street. * * @param array $locales Country => field => settings. * @return array */ public static function locales( $locales ) { if ( ! is_array( $locales ) ) { return $locales; } foreach ( self::postcode_first_countries() as $country ) { if ( is_string( $country ) && ! isset( $locales[ $country ]['postcode']['priority'] ) ) { $locales[ $country ]['postcode']['priority'] = 65; } } if ( 'auto' === self::split_mode() ) { foreach ( self::split_countries() as $country ) { if ( ! is_string( $country ) || ! empty( $locales[ $country ]['address_1']['hidden'] ) ) { continue; } $locales[ $country ]['house_number'] = array( 'hidden' => false, 'required' => false, ); $locales[ $country ]['address_1'] = array_merge( (array) ( $locales[ $country ]['address_1'] ?? array() ), array( 'label' => __( 'Street', 'aiolos-checkout-for-woocommerce' ), 'placeholder' => __( 'Street name', 'aiolos-checkout-for-woocommerce' ), ) ); } } return $locales; } /** * Joins the house number into the address line, in the country's order ("Hauptstraße 5", * "5 Main Street"); the number also stays on the order as its own field. * * @param array $data Posted data. * @return array */ public static function join_house_number( $data ) { if ( ! is_array( $data ) || 'off' === self::split_mode() ) { return $data; } foreach ( array( 'billing', 'shipping' ) as $type ) { $number = isset( $data[ $type . '_house_number' ] ) && is_scalar( $data[ $type . '_house_number' ] ) ? trim( (string) $data[ $type . '_house_number' ] ) : ''; $street = isset( $data[ $type . '_address_1' ] ) && is_scalar( $data[ $type . '_address_1' ] ) ? trim( (string) $data[ $type . '_address_1' ] ) : ''; if ( '' === $number || '' === $street ) { continue; } $country = isset( $data[ $type . '_country' ] ) && is_string( $data[ $type . '_country' ] ) ? $data[ $type . '_country' ] : ''; $data[ $type . '_address_1' ] = self::joined( $street, $number, $country ); } return $data; } }