Automattic\WooCommerce\StoreApi\Schemas\V1

AbstractSchema::get_recursive_sanitize_callback()protectedWC 1.0

Gets a function that sanitizes recursively.

Method of the class: AbstractSchema{}

No Hooks.

Return

function. Anonymous validation callback.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_recursive_sanitize_callback( $properties );
$properties(array) (required)
Schema property data.

AbstractSchema::get_recursive_sanitize_callback() code WC 8.7.0

protected function get_recursive_sanitize_callback( $properties ) {
	/**
	 * Validate a request argument based on details registered to the route.
	 *
	 * @param mixed            $values
	 * @param \WP_REST_Request $request
	 * @param string           $param
	 * @return true|\WP_Error
	 */
	return function ( $values, $request, $param ) use ( $properties ) {
		$sanitized_values = [];

		foreach ( $properties as $property_key => $property_value ) {
			$current_value = isset( $values[ $property_key ] ) ? $values[ $property_key ] : null;

			if ( isset( $property_value['arg_options']['sanitize_callback'] ) ) {
				$callback      = $property_value['arg_options']['sanitize_callback'];
				$current_value = is_callable( $callback ) ? $callback( $current_value, $request, $param ) : $current_value;
			} else {
				$current_value = rest_sanitize_value_from_schema( $current_value, $property_value, $param . ' > ' . $property_key );
			}

			// If sanitization failed, return the WP_Error object straight away.
			if ( is_wp_error( $current_value ) ) {
				return $current_value;
			}

			if ( isset( $property_value['properties'] ) ) {
				$sanitize_callback                 = $this->get_recursive_sanitize_callback( $property_value['properties'] );
				$sanitized_values[ $property_key ] = $sanitize_callback( $current_value, $request, $param . ' > ' . $property_key );
			} else {
				$sanitized_values[ $property_key ] = $current_value;
			}
		}

		return $sanitized_values;
	};
}