| 1 |
<?php |
| 2 |
/** |
| 3 |
* Local, per-authenticated-user import rate limiter (FR-015). |
| 4 |
* |
| 5 |
* A small, self-contained seed — not the "shared infrastructure" the spec's |
| 6 |
* Clarifications describe, because no such shared layer exists anywhere in |
| 7 |
* this codebase yet (research.md §5, spec.md Assumptions). Isolated behind |
| 8 |
* this one class so it can be swapped for real shared infrastructure later |
| 9 |
* without touching any ability. |
| 10 |
* |
| 11 |
* @package Templately\Modules\McpCore\Support |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace Templately\Modules\McpCore\Support; |
| 15 |
|
| 16 |
use WP_Error; |
| 17 |
|
| 18 |
class ImportRateLimiter { |
| 19 |
|
| 20 |
const MAX_CALLS = 10; |
| 21 |
const WINDOW_SECONDS = 60; |
| 22 |
|
| 23 |
/** |
| 24 |
* @return WP_Error|null Null if the call may proceed. |
| 25 |
*/ |
| 26 |
public static function check(): ?WP_Error { |
| 27 |
$key = 'templately_mcp_import_rl_' . get_current_user_id(); |
| 28 |
$count = (int) get_transient( $key ); |
| 29 |
|
| 30 |
if ( $count >= self::MAX_CALLS ) { |
| 31 |
return new WP_Error( |
| 32 |
'rate_limited', |
| 33 |
__( 'Too many import requests. Please wait a moment and try again.', 'templately' ) |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
set_transient( $key, $count + 1, self::WINDOW_SECONDS ); |
| 38 |
|
| 39 |
return null; |
| 40 |
} |
| 41 |
} |
| 42 |
|