Простой вариант.
<?php
session_start(); // в апи не должно быть сессии. Но вордпресс так не думает.
$hour = date('Y-m-d H');
$minute = floor(date('i') / 10);
$key = $hour . '_' . $minute;
$counts = $_SESSION['throttle'][ $ip ] ?? []; // получаем сохраненное
$count = $counts[ $key ] ?? 0; // берем по временной марке "сейчас"
$count++; // увеличиваем на единицу
$counts = []; // чистим старые временные марки
$counts[ $key ] = $count; // сохраняем новую
$_SESSION['throttle'][ $ip ] = $counts; // закидываем обратно в ящик
// проверяем, достигнут ли лимит
if ($count >= $limit) {
throw new \RuntimeException('Throttling limit exceeded');
}
Более правильный вариант, особенно в апи:
composer require symfony/cache
<?php
require_once __DIR__ . '/vendor/autoload.php';
$cache = new FilesystemCache('throttle', null, __DIR__ . '/cache');
$hour = date('Y-m-d H');
$minute = floor(date('i') / 10);
$key = $hour . '_' . $minute;
$cacheItem = $cache->getItem($ip);
$counts = $cacheItem->isHit() ? $cacheItem->get() : [];
$count = $counts[ $key ] ?? 0;
$count++;
$counts = [];
$counts[ $key ] = $count;
$cacheItem->save($counts);
if ($count >= $limit) {
throw new \RuntimeException('Throttling limit exceeded');
}