Add configurable API rate limits with subtabs in settings (v0.10.0)
- Make rate limiting configurable via WordPress options - Add subtabs to API settings: General, Rate Limits, Endpoints - Add HTTP method badges for endpoint documentation - Update CHANGELOG with rate limiting configuration details Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -22,23 +22,61 @@ final class RateLimiter {
|
||||
private const TRANSIENT_PREFIX = 'wp_bnb_rate_';
|
||||
|
||||
/**
|
||||
* Rate limits per minute by endpoint type.
|
||||
* Default rate limits per minute by endpoint type.
|
||||
*
|
||||
* @var array<string, int>
|
||||
*/
|
||||
private array $limits = array(
|
||||
private const DEFAULT_LIMITS = array(
|
||||
'public' => 60, // Public read endpoints.
|
||||
'availability' => 30, // Availability checks.
|
||||
'booking' => 10, // Booking creation.
|
||||
'admin' => 120, // Admin endpoints.
|
||||
);
|
||||
|
||||
/**
|
||||
* Rate limits per minute by endpoint type.
|
||||
*
|
||||
* @var array<string, int>
|
||||
*/
|
||||
private array $limits;
|
||||
|
||||
/**
|
||||
* Time window in seconds.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private int $window = 60;
|
||||
private int $window;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->load_limits_from_options();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load rate limits from WordPress options.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function load_limits_from_options(): void {
|
||||
$this->limits = array(
|
||||
'public' => (int) get_option( 'wp_bnb_rate_limit_public', self::DEFAULT_LIMITS['public'] ),
|
||||
'availability' => (int) get_option( 'wp_bnb_rate_limit_availability', self::DEFAULT_LIMITS['availability'] ),
|
||||
'booking' => (int) get_option( 'wp_bnb_rate_limit_booking', self::DEFAULT_LIMITS['booking'] ),
|
||||
'admin' => (int) get_option( 'wp_bnb_rate_limit_admin', self::DEFAULT_LIMITS['admin'] ),
|
||||
);
|
||||
$this->window = (int) get_option( 'wp_bnb_rate_limit_window', 60 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default rate limits.
|
||||
*
|
||||
* @return array<string, int>
|
||||
*/
|
||||
public static function get_default_limits(): array {
|
||||
return self::DEFAULT_LIMITS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if request is within rate limit.
|
||||
|
||||
Reference in New Issue
Block a user