Automattic\WooCommerce\Admin\API\RateLimits

QRLoginRateLimits::consumepublic staticWC 1.0

Consume one request from a bucket.

Method of the class: QRLoginRateLimits{}

No Hooks.

Returns

bool. True if the request is within the bucket limit.

Usage

$result = QRLoginRateLimits::consume( $bucket, $identifier ): bool;
$bucket(string) (required)
Bucket name.
$identifier(string) (required)
Bucket identifier.

QRLoginRateLimits::consume() code WC 11.0.1

public static function consume( string $bucket, string $identifier ): bool {
	global $wpdb;

	$options = self::get_bucket_options( $bucket );
	if ( null === $options ) {
		return false;
	}

	$time              = time();
	$limit             = max( 1, (int) $options['limit'] );
	$rate_limit_expiry = $time + (int) $options['seconds'];
	$action_id         = self::get_action_id( $bucket, $identifier );

	$result = $wpdb->query(
		$wpdb->prepare(
			"INSERT INTO {$wpdb->prefix}wc_rate_limits
				(`rate_limit_key`, `rate_limit_expiry`, `rate_limit_remaining`)
			VALUES
				(%s, %d, %d)
			ON DUPLICATE KEY UPDATE
				`rate_limit_id` = IF(
					`rate_limit_expiry` < %d OR `rate_limit_remaining` > 0,
					LAST_INSERT_ID(`rate_limit_id`),
					LAST_INSERT_ID(0) + `rate_limit_id`
				),
				`rate_limit_remaining` = IF(
					`rate_limit_expiry` < %d,
					VALUES(`rate_limit_remaining`),
					IF(`rate_limit_remaining` > 0, `rate_limit_remaining` - 1, 0)
				),
				`rate_limit_expiry` = IF(`rate_limit_expiry` < %d, VALUES(`rate_limit_expiry`), `rate_limit_expiry`);
			",
			$action_id,
			$rate_limit_expiry,
			$limit - 1,
			$time,
			$time,
			$time
		)
	);

	if ( false === $result ) {
		return false;
	}

	return (int) $wpdb->get_var( 'SELECT LAST_INSERT_ID()' ) > 0;
}