settings = $settings; } /** * Table name. * * @return string */ public static function table() { global $wpdb; return $wpdb->prefix . 'mybot_ratelimit'; } /** * Increments the counter and reports whether the request is still within the limit. * * @param string $bucket Bucket key, usually a hashed IP address or a hashed user. * @param int|null $limit Maximum number of hits; null reads it from the settings. * @param int|null $window Window in seconds; null reads it from the settings. * @return array{allowed:bool,hits:int,limit:int,retry_after:int} */ public function hit( $bucket, $limit = null, $window = null ) { global $wpdb; $limit = null === $limit ? (int) $this->settings->get( 'rate_limit', 20 ) : (int) $limit; $window = null === $window ? (int) $this->settings->get( 'rate_window', 60 ) : (int) $window; /** * Filters the rate limit before it is applied. * * @param int $limit Maximum number of hits. * @param string $bucket Bucket key. */ $limit = max( 1, (int) apply_filters( 'mybot_rate_limit', $limit, $bucket ) ); $bucket = substr( preg_replace( '/[^a-zA-Z0-9_\-]/', '', (string) $bucket ), 0, 64 ); $window_start = (int) ( floor( time() / $window ) * $window ); $table = self::table(); // A single query: increment when the row exists, insert it when it does not. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->query( $wpdb->prepare( 'INSERT INTO %i (bucket_key, window_start, hits) VALUES (%s, %d, 1) ON DUPLICATE KEY UPDATE hits = hits + 1', $table, $bucket, $window_start ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $hits = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT hits FROM %i WHERE bucket_key = %s AND window_start = %d', $table, $bucket, $window_start ) ); $retry_after = max( 0, ( $window_start + $window ) - time() ); return array( 'allowed' => $hits <= $limit, 'hits' => $hits, 'limit' => $limit, 'retry_after' => $retry_after, ); } /** * Reads the counter without incrementing it. * * @param string $bucket Bucket key. * @param int|null $window Window in seconds. * @return int */ public function peek( $bucket, $window = null ) { global $wpdb; $window = null === $window ? (int) $this->settings->get( 'rate_window', 60 ) : (int) $window; $window_start = (int) ( floor( time() / $window ) * $window ); $table = self::table(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT hits FROM %i WHERE bucket_key = %s AND window_start = %d', $table, $bucket, $window_start ) ); } /** * Deletes expired windows. * * @return int Number of deleted rows. */ public function purge_expired() { global $wpdb; $table = self::table(); $cutoff = time() - max( 3600, (int) $this->settings->get( 'rate_window', 60 ) * 4 ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching return (int) $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE window_start < %d', $table, $cutoff ) ); } }