Automattic\WooCommerce\StoreApi
Authentication::apply_rate_limiting
Applies Rate Limiting to the request, and passes through any errors from other authentication methods used before this one.
Method of the class: Authentication{}
Hooks from the method
Returns
\WP_Error|null|true|false.
Usage
// protected - for code of main (parent) or child class $result = $this->apply_rate_limiting( $result );
- $result(WP_Error|mixed) (required)
- Error from another authentication handler, null if we should handle it, or another value if not.
Authentication::apply_rate_limiting() Authentication::apply rate limiting code WC 10.8.1
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;
}