Automattic\WooCommerce\StoreApi\Schemas\V1

AbstractSchema::get_recursive_validate_callback()protectedWC 1.0

Gets a function that validates recursively.

Метод класса: AbstractSchema{}

Хуков нет.

Возвращает

function. Anonymous validation callback.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_recursive_validate_callback( $properties );
$properties(массив) (обязательный)
Schema property data.

Код AbstractSchema::get_recursive_validate_callback() WC 8.7.0

protected function get_recursive_validate_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 ) {
		foreach ( $properties as $property_key => $property_value ) {
			$current_value = isset( $values[ $property_key ] ) ? $values[ $property_key ] : null;

			$property_type = is_array( $property_value['type'] ) ? $property_value['type'] : [ $property_value['type'] ];
			if ( empty( $current_value ) && in_array( 'null', $property_type, true ) ) {
				// If the value is null and the schema allows null, we can skip validation for children.
				continue;
			}

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

			if ( ! $result || is_wp_error( $result ) ) {
				// If schema validation fails, we return here as we don't need to validate any deeper.
				return $result;
			}

			if ( isset( $property_value['properties'] ) ) {
				$validate_callback = $this->get_recursive_validate_callback( $property_value['properties'] );
				$result            = $validate_callback( $current_value, $request, $param . ' > ' . $property_key );

				if ( ! $result || is_wp_error( $result ) ) {
					// If schema validation fails, we return here as we don't need to validate any deeper.
					return $result;
				}
			}
		}

		return true;
	};
}