generateTransientKey( $rule_id, $ip_address ); // Get current count from transient. $current_count = get_transient( $transient_key ); if ( false === $current_count ) { // No existing transient, start with count 1. $current_count = 0; } // Check if we've reached the limit. if ( $current_count >= self::MAX_INCIDENTS_PER_WINDOW ) { return false; } // Increment count and update transient. $new_count = $current_count + 1; set_transient( $transient_key, $new_count, self::TIME_WINDOW ); return true; } /** * Generate a unique transient key for the rule and IP combination. * * @param string $rule_id The rule ID. * @param string $ip_address The IP address. * * @return string The transient key. */ private function generateTransientKey( $rule_id, $ip_address ) { // Hash the IP address for brevity and to avoid special characters. $ip_hash = md5( $ip_address ); // nosemgrep: weak-crypto -- used for cache key, not security. return self::TRANSIENT_PREFIX . $rule_id . '_' . $ip_hash; } /** * Get current incident count for a rule and IP combination. * * This method is primarily for testing and debugging purposes. * * @param string $rule_id The rule ID. * @param string $ip_address The IP address. * * @return int Current incident count. */ public function getCurrentCount( $rule_id, $ip_address ) { $transient_key = $this->generateTransientKey( $rule_id, $ip_address ); $count = get_transient( $transient_key ); return false === $count ? 0 : $count; } /** * Clear rate limit data for a specific rule and IP combination. * * This method is primarily for testing purposes. * * @param string $rule_id The rule ID. * @param string $ip_address The IP address. * * @return bool True if transient was deleted, false otherwise. */ public function clearRateLimit( $rule_id, $ip_address ) { $transient_key = $this->generateTransientKey( $rule_id, $ip_address ); return delete_transient( $transient_key ); } }