>> */ private static array $bundled = array(); /** * Countries this request's `add_states()` actually filled. * * @var array */ private static array $filled = array(); /** * Hook everything. * * @return void */ public function register(): void { add_filter( 'woocommerce_states', array( $this, 'add_states' ) ); add_filter( 'woocommerce_get_country_locale', array( $this, 'show_state_field' ), 20 ); } /** * The bundled lists: country code => state code => name. * * @return array> */ public static function bundled(): array { $locale = determine_locale(); if ( ! isset( self::$bundled[ $locale ] ) ) { self::$bundled[ $locale ] = (array) include __DIR__ . '/states-data.php'; } return self::$bundled[ $locale ]; } /** * Whether the feature is on. * * @return bool */ public static function is_enabled(): bool { return (bool) aiou_get_setting( Module::TAB, Module::SECTION, 'enabled', 0 ); } /** * Fill the countries WooCommerce has no states for. * * @param mixed $states Country code => state code => name. * @return mixed */ public function add_states( $states ) { if ( ! is_array( $states ) || ! self::is_enabled() ) { return $states; } foreach ( self::bundled() as $cc => $list ) { if ( ! empty( $states[ $cc ] ) ) { continue; } /** * Filters the states Free adds for one country. * * @param array $list State code => name. * @param string $cc Country code. */ $list = (array) apply_filters( 'aiou_country_states_bundled', $list, $cc ); if ( $list ) { $states[ $cc ] = $list; self::$filled[ $cc ] = true; } } return $states; } /** * Show the state field for the countries filled above (WooCommerce * hides it for some of them, e.g. Afghanistan and France), and make it * required or optional as configured. * * @param mixed $locale Country code => field overrides. * @return mixed */ public function show_state_field( $locale ) { if ( ! is_array( $locale ) || ! self::is_enabled() || ! function_exists( 'WC' ) || ! WC()->countries ) { return $locale; } // Builds the states (and so `self::$filled`) if nothing has yet. $states = WC()->countries->get_states(); $required = (bool) aiou_get_setting( Module::TAB, Module::SECTION, 'required', 0 ); foreach ( array_keys( self::$filled ) as $cc ) { // Something later (e.g. the Woo Add-on) may have emptied it. if ( empty( $states[ $cc ] ) ) { continue; } // WooCommerce deliberately shows some state fields as optional // (e.g. the UK's County); "required" must not override that. $optional = self::core_shows_optional( $locale[ $cc ]['state'] ?? array() ); $locale[ $cc ]['state']['hidden'] = false; $locale[ $cc ]['state']['required'] = $required && ! $optional; } return $locale; } /** * Whether WooCommerce's locale shows a country's state field, but as * optional: `required` is explicitly false and the field isn't hidden. * * @param mixed $state The country's `state` locale entry. * @return bool */ public static function core_shows_optional( $state ): bool { return is_array( $state ) && array_key_exists( 'required', $state ) && false === $state['required'] && empty( $state['hidden'] ); } }