name. * * @return array */ public static function all_codes() { if ( ! function_exists( 'WC' ) || ! WC()->countries ) { return array(); } return (array) WC()->countries->get_countries(); } /** * Countries a phone field never accepts. * * @param string $group billing or shipping. * @return array Upper-case codes. */ public static function excluded( $group ) { return array_map( 'strval', (array) Settings::get( ( 'shipping' === $group ? 'shipping' : 'billing' ) . '_exclude' ) ); } /** * Countries a phone field is limited to (excluded removed); null when not limited or when the limit leaves no country. * * @param string $group billing or shipping. * @return array|null Upper-case codes. */ public static function only( $group ) { $group = 'shipping' === $group ? 'shipping' : 'billing'; $source = (string) Settings::get( $group . '_source' ); $list = array(); if ( 'selling' === $source && function_exists( 'WC' ) && WC()->countries ) { $list = array_keys( (array) WC()->countries->get_allowed_countries() ); } elseif ( 'shipping' === $source && function_exists( 'WC' ) && WC()->countries ) { $list = array_keys( (array) WC()->countries->get_shipping_countries() ); } elseif ( 'custom' === $source ) { $list = (array) Settings::get( $group . '_custom' ); } $list = array_values( array_diff( array_map( 'strtoupper', array_map( 'strval', $list ) ), self::excluded( $group ) ) ); return $list ? $list : null; } /** * Whether a field is set to a country list that leaves no country (then only the excluded ones are refused). * * @param string $group billing or shipping. * @return bool */ public static function limit_is_empty( $group ) { $group = 'shipping' === $group ? 'shipping' : 'billing'; return 'all' !== Settings::get( $group . '_source' ) && null === self::only( $group ); } /** * Countries shown in a phone field's list; an empty list means every country. * * @param string $group billing or shipping. * @return array Upper-case codes. */ public static function allowed( $group ) { $only = self::only( $group ); if ( null !== $only ) { return $only; } $exclude = self::excluded( $group ); return $exclude ? array_values( array_diff( array_keys( self::all_codes() ), $exclude ) ) : array(); } /** * Whether a phone field accepts numbers from a country. * * @param string $group billing or shipping. * @param string $country Upper-case code. * @return bool */ public static function accepts( $group, $country ) { $country = strtoupper( (string) $country ); $only = self::only( $group ); if ( null !== $only && ! in_array( $country, $only, true ) ) { return false; } return ! in_array( $country, self::excluded( $group ), true ); } /** * The store's own country. * * @return string Upper-case code or ''. */ public static function store() { if ( ! function_exists( 'WC' ) || ! WC()->countries ) { return ''; } return strtoupper( (string) WC()->countries->get_base_country() ); } }