get_var( $wpdb->prepare( " SELECT rate_limit_expiry FROM {$wpdb->prefix}wc_rate_limits WHERE rate_limit_key = %s ", $action_id ) ); self::set_cache( $action_id, $next_try_allowed_at ); } // No record of action running, so action is allowed to run. if ( null === $next_try_allowed_at ) { return false; } // Before the next run is allowed, retry forbidden. if ( time() <= $next_try_allowed_at ) { return true; } // After the next run is allowed, retry allowed. return false; } /** * Sets the rate limit delay in seconds for action with identifier $id. * * @param string $action_id Identifier of the action. * @param int $delay Delay in seconds. * @return bool True if the option setting was successful, false otherwise. */ public static function set_rate_limit( $action_id, $delay ) { global $wpdb; $next_try_allowed_at = time() + $delay; $result = $wpdb->replace( $wpdb->prefix . 'wc_rate_limits', array( 'rate_limit_key' => $action_id, 'rate_limit_expiry' => $next_try_allowed_at, ), array( '%s', '%d' ) ); self::set_cache( $action_id, $next_try_allowed_at ); return false !== $result; } /** * Cleanup expired rate limits from the database and clear caches. */ public static function cleanup() { global $wpdb; $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}wc_rate_limits WHERE rate_limit_expiry < %d", time() ) ); if ( class_exists( 'WC_Cache_Helper' ) ) { WC_Cache_Helper::invalidate_cache_group( self::CACHE_GROUP ); } } } WC_Rate_Limiter::init();