Skip to main content

Rate-limit unsubscribe endpoint to prevent abuse

Free Intermediate Since v1.0.0

The email unsubscribe endpoint is public by design, but a bad actor with a list of subscriber IDs could use it to mass-unsubscribe your audience. Return false from benecaster_email_unsubscribe_allowed when the request IP exceeds a rate limit, giving legitimate subscribers immediate unsubscription while throttling abuse.

Code

<?php
add_filter(
    'benecaster_email_unsubscribe_allowed',
    function ( bool $allowed, ?int $user_id, int $show_id, string $type, string $token ): bool {
        if ( ! $allowed ) {
            return false; // already blocked upstream
        }

        // Allow at most 5 unsubscribe attempts per hour per token.
        // Uses a WordPress transient as a lightweight counter.
        if ( '' !== $token ) {
            $key     = 'bc_unsub_rate_' . substr( $token, 0, 16 );
            $count   = (int) get_transient( $key );
            if ( $count >= 5 ) {
                return false; // rate-limited — do not record opt-out
            }
            set_transient( $key, $count + 1, HOUR_IN_SECONDS );
        }

        return true;
    },
    10, 5
);

View on GitHub →

Hooks Used