Automattic\WooCommerce\Internal\RestApi\Routes\V4\Settings\General

Controller::validate_country_or_state_codeprivateWC 1.0

Validate country or state code.

Method of the class: Controller{}

No Hooks.

Returns

true|false. Valid or not valid.

Usage

// private - for code of main (parent) class only
$result = $this->validate_country_or_state_code( $country_or_state );
$country_or_state(string) (required)
Country or state code.

Controller::validate_country_or_state_code() code WC 10.4.3

private function validate_country_or_state_code( $country_or_state ) {
	list( $country, $state ) = array_pad( explode( ':', (string) $country_or_state, 2 ), 2, '' );
	if ( '' === $country ) {
		return false;
	}
	$country_codes = array_keys( WC()->countries->get_countries() );
	if ( ! in_array( $country, $country_codes, true ) ) {
		return false;
	}
	if ( '' === $state ) {
		return true;
	}
	$states_for_country = WC()->countries->get_states( $country );
	if ( empty( $states_for_country ) ) {
		return false;
	}
	return isset( $states_for_country[ $state ] );
}