$allowed, 'remaining' => max(0, $limit - $count), 'retry_after' => $allowed ? 0 : 60 - ($now % 60), ]; } /** * Who a per-person counter is kept for: the user id, or the hashed client address for a * request with no user. Public because Rest\StreamBudget keys its own per-person counter the * same way and the two must agree on what "one person" means — a route a site has opened to * visitors through its capability filter is every visitor to get_current_user_id(), and one * shared bucket there would let one script spend everyone's allowance of either counter. */ public static function subject(int $userId): string { return $userId > 0 ? (string) $userId : 'ip_' . self::client(); } /** * The anonymous bucket subject: wp_hash() of REMOTE_ADDR, or 'unknown' when there is no * address that parses as one. The IPv4 space is small enough to reverse a plain digest by * brute force, which is why it is wp_hash() (an HMAC over the site's salts) and not md5(). */ private static function client(): string { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- wp_magic_quotes() slashes $_GET, $_POST and $_COOKIE, never REMOTE_ADDR, so there is nothing to unslash; FILTER_VALIDATE_IP is the sanitisation and rejects anything that is not a bare address. $ip = filter_var($_SERVER['REMOTE_ADDR'] ?? '', FILTER_VALIDATE_IP); return is_string($ip) ? wp_hash($ip) : 'unknown'; } }