key (first_name, city…) => locale entries. */ private static function locale_rules(): array { // The classic checkout keeps billing and shipping apart (apply()); locales would join them. if ( ! PageMode::uses_block() ) { return array(); } $rules = self::rules(); $out = array(); foreach ( array( 'first_name', 'last_name', 'company', 'address_1', 'address_2', 'city', 'state', 'postcode', 'phone' ) as $key ) { $rule = $rules[ 'shipping_' . $key ] ?? ( $rules[ 'billing_' . $key ] ?? null ); if ( ! is_array( $rule ) ) { continue; } $entry = array(); if ( 'no' === ( $rule['show'] ?? 'yes' ) ) { $entry['hidden'] = true; $entry['required'] = false; } elseif ( in_array( $rule['required'] ?? 'default', array( 'yes', 'no' ), true ) ) { $entry['required'] = 'yes' === $rule['required']; } if ( '' !== (string) ( $rule['label'] ?? '' ) ) { $entry['label'] = (string) $rule['label']; } if ( isset( $rule['priority'] ) && '' !== (string) $rule['priority'] ) { $entry['priority'] = (int) $rule['priority']; } if ( $entry ) { $out[ $key ] = $entry; } } return $out; } /** * Applies the address rules to the default locale. * * @param array $locale Default locale. * @return array */ public static function locale_default( $locale ) { if ( ! is_array( $locale ) ) { return $locale; } foreach ( self::locale_rules() as $key => $entry ) { $locale[ $key ] = array_merge( (array) ( $locale[ $key ] ?? array() ), $entry ); } return $locale; } /** * And to every country's locale, so a country's own settings do not undo them. * * @param array $locales Locales by country. * @return array */ public static function locales( $locales ) { if ( ! is_array( $locales ) ) { return $locales; } $rules = self::locale_rules(); if ( ! $rules ) { return $locales; } foreach ( $locales as $country => $fields ) { foreach ( $rules as $key => $entry ) { $locales[ $country ][ $key ] = array_merge( (array) ( $fields[ $key ] ?? array() ), $entry ); } } return $locales; } /** * The editable fields: group => key => [label, priority] as WooCommerce has them. * * @return array> */ public static function editable(): array { $address = function_exists( 'WC' ) && WC()->countries ? (array) WC()->countries->get_default_address_fields() : array(); $info = static function ( string $key ) use ( $address ): array { $text = (string) ( $address[ $key ]['label'] ?? '' ); if ( '' === $text && 'address_2' === $key ) { $text = __( 'Apartment, suite, floor', 'aiolos-checkout-for-woocommerce' ); } return array( 'label' => '' !== $text ? $text : $key, 'priority' => (int) ( $address[ $key ]['priority'] ?? 0 ), ); }; $keys = array( 'first_name', 'last_name', 'company', 'country', 'address_1', 'address_2', 'postcode', 'city', 'state' ); $groups = array( 'billing' => array(), 'shipping' => array(), 'order' => array( 'order_comments' => array( 'label' => __( 'Order notes', 'aiolos-checkout-for-woocommerce' ), 'priority' => 0, ), ), ); foreach ( $keys as $key ) { $groups['billing'][ 'billing_' . $key ] = $info( $key ); $groups['shipping'][ 'shipping_' . $key ] = $info( $key ); } $groups['billing']['billing_phone'] = array( 'label' => __( 'Phone', 'woocommerce' ), // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch -- WooCommerce's own label. 'priority' => 100, ); $groups['billing']['billing_email'] = array( 'label' => __( 'Email address', 'woocommerce' ), // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch -- WooCommerce's own label. 'priority' => 110, ); return $groups; } /** * Group labels. * * @return array */ public static function group_labels(): array { return array( 'billing' => __( 'Billing and contact', 'aiolos-checkout-for-woocommerce' ), 'shipping' => __( 'Shipping address', 'aiolos-checkout-for-woocommerce' ), 'order' => __( 'Order', 'aiolos-checkout-for-woocommerce' ), ); } /** * Whether a field may be hidden or made optional. * * @param string $key Field key. */ public static function is_locked( string $key ): bool { return in_array( $key, self::LOCKED, true ); } /** * Saved rules: key => [show, required, label, placeholder, priority]. * * @return array */ public static function rules(): array { $rules = Settings::get( self::OPTION_KEY ); return is_array( $rules ) ? $rules : array(); } /** * Applies the rules. * * @param array $fields Checkout fields by group. * @return array */ public static function apply( $fields ) { if ( ! is_array( $fields ) ) { return $fields; } $rules = self::rules(); if ( ! $rules ) { return $fields; } foreach ( $fields as $group => $group_fields ) { if ( ! is_array( $group_fields ) ) { continue; } foreach ( $group_fields as $key => $field ) { if ( ! isset( $rules[ $key ] ) || ! is_array( $field ) ) { continue; } $rule = $rules[ $key ]; if ( 'no' === ( $rule['show'] ?? 'yes' ) && ! self::is_locked( $key ) ) { unset( $fields[ $group ][ $key ] ); continue; } $required = (string) ( $rule['required'] ?? 'default' ); if ( 'yes' === $required ) { $field['required'] = true; } elseif ( 'no' === $required && ! self::is_locked( $key ) ) { $field['required'] = false; } if ( '' !== (string) ( $rule['label'] ?? '' ) ) { $field['label'] = (string) $rule['label']; // WooCommerce hides some labels (address line 2) for screen readers only. $field['label_class'] = array_values( array_diff( (array) ( $field['label_class'] ?? array() ), array( 'screen-reader-text' ) ) ); } if ( '' !== (string) ( $rule['placeholder'] ?? '' ) ) { $field['placeholder'] = (string) $rule['placeholder']; } if ( isset( $rule['priority'] ) && '' !== (string) $rule['priority'] ) { $field['priority'] = (int) $rule['priority']; } $fields[ $group ][ $key ] = $field; } } // WooCommerce sorts each group by priority after this filter. return $fields; } /** * The editor (a "json" settings field). * * @param string $name Input name. * @param mixed $value Saved rules. */ public static function render( string $name, $value ): void { $value = is_array( $value ) ? $value : array(); echo '

' . esc_html__( 'Empty boxes keep WooCommerce\'s own text. Email and country are always shown and required: WooCommerce needs them for the order email, shipping and taxes. Some countries rename or require the state and postcode fields themselves.', 'aiolos-checkout-for-woocommerce' ) . '

'; echo '

' . esc_html__( 'If customers see WooCommerce\'s Checkout block instead of Aiolos Checkout, "Show" and "Required" apply there too (one setting for both addresses); the block keeps its own labels, placeholders and order.', 'aiolos-checkout-for-woocommerce' ) . '

'; foreach ( self::editable() as $group => $keys ) { $labels = self::group_labels(); ?>

$info ) : $label = $info['label']; $rule = (array) ( $value[ $key ] ?? array() ); $base = $name . '[' . $key . ']'; $locked = self::is_locked( $key ); ?>

aria-label="">
( self::is_locked( $key ) || 'no' !== ( $row['show'] ?? 'yes' ) ) ? 'yes' : 'no', 'required' => self::is_locked( $key ) ? 'default' : ( in_array( $row['required'] ?? '', array( 'yes', 'no' ), true ) ? $row['required'] : 'default' ), 'label' => sanitize_text_field( $row['label'] ?? '' ), 'placeholder' => sanitize_text_field( $row['placeholder'] ?? '' ), 'priority' => is_numeric( $row['priority'] ?? '' ) ? max( 0, min( 999, (int) $row['priority'] ) ) : '', ); if ( 'yes' !== $rule['show'] || 'default' !== $rule['required'] || '' !== $rule['label'] || '' !== $rule['placeholder'] || '' !== $rule['priority'] ) { $clean[ $key ] = $rule; } } } return $clean; } }