limit( self::RATE_LIMIT_PREFIX, $identifier, $limit, 60 ); } /** * Fixed-window transient rate-limit counter shared by the per-minute and * per-hour limits. * * @param string $prefix Transient key prefix. * @param string $identifier Unique identifier (IP or user ID). * @param int $limit Max requests allowed within the window. * @param int $ttl Window length in seconds. * @return bool True if the request is over the limit. */ private function limit( string $prefix, string $identifier, int $limit, int $ttl ): bool { $key = $prefix . md5( $identifier ); // wp_cache_incr is atomic, closing the read-then-set race the transient path below has; without an external cache wp_cache_* is per-request only, so fall back to it. if ( function_exists( 'wp_using_ext_object_cache' ) && wp_using_ext_object_cache() ) { $group = 'aisentic_rate_limit'; // Seed the fixed window on the first hit; a no-op if it already exists. wp_cache_add( $key, 0, $group, $ttl ); $count = wp_cache_incr( $key, 1, $group ); if ( false === $count ) { // Key evicted between add and incr — re-seed this window. wp_cache_set( $key, 1, $group, $ttl ); $count = 1; } return (int) $count > $limit; } // The window end is stored with the count and every write keeps it: // set_transient() restarts the expiry, so writing the full TTL each time // would let a steady visitor keep one window open forever. $now = time(); $window = get_transient( $key ); if ( ! is_array( $window ) || (int) ( $window['until'] ?? 0 ) <= $now ) { set_transient( $key, [ 'n' => 1, 'until' => $now + $ttl ], $ttl ); return false; } if ( (int) $window['n'] >= $limit ) { return true; } set_transient( $key, [ 'n' => (int) $window['n'] + 1, 'until' => (int) $window['until'] ], (int) $window['until'] - $now ); return false; } /** * Get request identifier (IP address or user ID) * * @return string */ public function getRequestIdentifier(): string { if ( is_user_logged_in() ) { return 'user_' . get_current_user_id(); } // Get IP address. $ip = filter_var( wp_unslash( $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0' ), FILTER_VALIDATE_IP ); if ( false === $ip ) { $ip = '0.0.0.0'; } // Check for forwarded IP ONLY behind a trusted proxy (constant defined in wp-config.php). $trusted_proxies = defined( 'AISENTIC_TRUSTED_PROXIES' ) ? array_map( 'trim', explode( ',', AISENTIC_TRUSTED_PROXIES ) ) : []; if ( ! empty( $trusted_proxies ) && in_array( $ip, $trusted_proxies, true ) && ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { $forwarded = sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ); // Each proxy appends the address it saw, so read from the right: the // first address that is not one of our proxies is the client. The // left end is whatever the client chose to send. foreach ( array_reverse( array_map( 'trim', explode( ',', $forwarded ) ) ) as $hop ) { if ( in_array( $hop, $trusted_proxies, true ) ) { continue; } if ( false !== filter_var( $hop, FILTER_VALIDATE_IP ) ) { $ip = $hop; } break; } } return 'ip_' . $ip; } private const HOURLY_RATE_LIMIT_PREFIX = 'aisentic_hourly_rate_limit_'; /** * Check if request exceeds the hourly rate limit * * @param string $identifier Unique identifier (IP or user ID). * @param int $limit Requests per hour limit. * @return bool True if rate limited, false otherwise. */ public function isHourlyRateLimited( string $identifier, int $limit = 20 ): bool { return $this->limit( self::HOURLY_RATE_LIMIT_PREFIX, $identifier, $limit, HOUR_IN_SECONDS ); } /** * Check if action is blacklisted * * @param string $action Action to check. * @param array $blacklist Blacklisted actions. * @return bool */ public function isBlacklisted( string $action, array $blacklist ): bool { return in_array( $action, $blacklist, true ); } }