Automattic\WooCommerce\StoreApi\Schemas\V1

AbstractAddressSchema::validate_callback()publicWC 1.0

Validate the given address object.

{} Это метод класса: AbstractAddressSchema{}

Хуков нет.

Возвращает

true|\WP_Error.

Использование

$AbstractAddressSchema = new AbstractAddressSchema();
$AbstractAddressSchema->validate_callback( $address, $request, $param );
$address(массив) (обязательный)
Value being sanitized.
$request(\WP_REST_Request) (обязательный)
The Request.
$param(строка) (обязательный)
The param being sanitized.

Заметки

Код AbstractAddressSchema::validate_callback() WC 6.7.0

public function validate_callback( $address, $request, $param ) {
	$errors  = new \WP_Error();
	$address = $this->sanitize_callback( $address, $request, $param );

	if ( empty( $address['country'] ) ) {
		$errors->add(
			'missing_country',
			__( 'Country is required', 'woocommerce' )
		);
		return $errors;
	}

	if ( ! in_array( $address['country'], array_keys( wc()->countries->get_countries() ), true ) ) {
		$errors->add(
			'invalid_country',
			sprintf(
				/* translators: %s valid country codes */
				__( 'Invalid country code provided. Must be one of: %s', 'woocommerce' ),
				implode( ', ', array_keys( wc()->countries->get_countries() ) )
			)
		);
		return $errors;
	}

	if ( ! empty( $address['state'] ) && ! $this->validate_state( $address['state'], $address['country'] ) ) {
		$errors->add(
			'invalid_state',
			sprintf(
				/* translators: %1$s given state, %2$s valid states */
				__( 'The provided state (%1$s) is not valid. Must be one of: %2$s', 'woocommerce' ),
				esc_html( $address['state'] ),
				implode( ', ', array_keys( $this->get_states_for_country( $address['country'] ) ) )
			)
		);
	}

	if ( ! empty( $address['postcode'] ) && ! \WC_Validation::is_postcode( $address['postcode'], $address['country'] ) ) {
		$errors->add(
			'invalid_postcode',
			__( 'The provided postcode / ZIP is not valid', 'woocommerce' )
		);
	}

	if ( ! empty( $address['phone'] ) && ! \WC_Validation::is_phone( $address['phone'] ) ) {
		$errors->add(
			'invalid_phone',
			__( 'The provided phone number is not valid', 'woocommerce' )
		);
	}

	return $errors->has_errors( $errors ) ? $errors : true;
}