= $limit ) { // A Retry-After value cannot be zero. return max( 1, ( $bucket + 1 ) * $window - $now ); } set_transient( $key, $count + 1, $window ); return null; } /** * Return the wait time without using a request. * * Authentication checks must not consume the failure limit. * * @since 1.0.0 * * @param string $scope Limit scope. * @param string $identifier Caller identifier. * @param int $limit Requests per window. * @param int $window Window length in seconds. * @param int|null $now Test time, or null for now. * @return int|null Wait time in seconds, or null. */ public function peek( string $scope, string $identifier, int $limit, int $window, ?int $now = null ): ?int { $now = $now ?? time(); $bucket = intdiv( $now, $window ); $key = self::KEY_PREFIX . sha1( $scope . ':' . $identifier . ':' . $window . ':' . $bucket ); if ( (int) get_transient( $key ) >= $limit ) { return max( 1, ( $bucket + 1 ) * $window - $now ); } return null; } }