Automattic\WooCommerce\StoreApi\Schemas\V1

AbstractAddressSchema::sanitize_callback()publicWC 1.0

Sanitize and format the given address object.

Method of the class: AbstractAddressSchema{}

No Hooks.

Return

Array.

Usage

$AbstractAddressSchema = new AbstractAddressSchema();
$AbstractAddressSchema->sanitize_callback( $address, $request, $param );
$address(array) (required)
Value being sanitized.
$request(\WP_REST_Request) (required)
The Request.
$param(string) (required)
The param being sanitized.

AbstractAddressSchema::sanitize_callback() code WC 9.3.3

public function sanitize_callback( $address, $request, $param ) {
	$validation_util   = new ValidationUtils();
	$sanitization_util = new SanitizationUtils();
	$address           = (array) $address;
	$schema            = $this->get_properties();
	// omit all keys from address that are not in the schema. This should account for email.
	$address = array_intersect_key( $address, $schema );
	$address = array_reduce(
		array_keys( $address ),
		function ( $carry, $key ) use ( $address, $validation_util, $schema ) {
			switch ( $key ) {
				case 'country':
					$carry[ $key ] = wc_strtoupper( sanitize_text_field( wp_unslash( $address[ $key ] ) ) );
					break;
				case 'state':
					$carry[ $key ] = $validation_util->format_state( sanitize_text_field( wp_unslash( $address[ $key ] ) ), $address['country'] );
					break;
				case 'postcode':
					$carry[ $key ] = $address['postcode'] ? wc_format_postcode( sanitize_text_field( wp_unslash( $address['postcode'] ) ), $address['country'] ) : '';
					break;
				default:
					$carry[ $key ] = rest_sanitize_value_from_schema( wp_unslash( $address[ $key ] ), $schema[ $key ], $key );
					break;
			}
			if ( $this->additional_fields_controller->is_field( $key ) ) {
				$carry[ $key ] = $this->additional_fields_controller->sanitize_field( $key, $carry[ $key ] );
			}
			return $carry;
		},
		[]
	);

	return $sanitization_util->wp_kses_array( $address );
}