Automattic\WooCommerce\StoreApi

Authentication::apply_rate_limitingprotectedWC 1.0

Applies Rate Limiting to the request, and passes through any errors from other authentication methods used before this one.

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

Хуки из метода

Возвращает

\WP_Error|null|true|false.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->apply_rate_limiting( $result );
$result(WP_Error|разное) (обязательный)
Error from another authentication handler, null if we should handle it, or another value if not.

Код Authentication::apply_rate_limiting() WC 9.9.5

protected function apply_rate_limiting( $result ) {
	$rate_limiting_options = RateLimits::get_options();

	if ( $rate_limiting_options->enabled ) {
		$action_id = 'store_api_request_' . self::get_rate_limiting_id( $rate_limiting_options->proxy_support );

		$retry  = RateLimits::is_exceeded_retry_after( $action_id );
		$server = rest_get_server();
		$server->send_header( 'RateLimit-Limit', $rate_limiting_options->limit );

		if ( false !== $retry ) {
			$server->send_header( 'RateLimit-Remaining', 0 );
			$server->send_header( 'RateLimit-Retry-After', $retry );
			$server->send_header( 'RateLimit-Reset', time() + $retry );

			/**
			 * Fires when the rate limit is exceeded.
			 *
			 * @param string $ip_address The IP address of the request.
			 * @param string $action_id  The grouping identifier to the request.
			 *
			 * @since 8.9.0
			 * @since 9.8.0 Added $action_id parameter.
			 */
			do_action(
				'woocommerce_store_api_rate_limit_exceeded',
				self::get_ip_address( $rate_limiting_options->proxy_support ),
				$action_id
			);

			return new \WP_Error(
				'rate_limit_exceeded',
				sprintf(
					'Too many requests. Please wait %d seconds before trying again.',
					$retry
				),
				array( 'status' => 400 )
			);
		}

		$rate_limit = RateLimits::update_rate_limit( $action_id );
		$server->send_header( 'RateLimit-Remaining', $rate_limit->remaining );
		$server->send_header( 'RateLimit-Reset', $rate_limit->reset );
	}

	return $result;
}