Revoke a listener’s feed only if they are not a member
An add-on that ends somebody’s access on a deadline (a trial it granted, an import whose people never paid) must not end it for somebody who paid a moment ago. Checking membership first and then calling revoke() leaves that window open; TokenManager::revoke_unless_subscribed() does both as one step.
Test the answer for 'revoked', never for “not subscribed”: every other answer left access where it was, and more may be added. “Member” means any active membership, free or paid, including trialing, payment retrying, paused and comped. A follower is not one; someone who bought episodes outright is switched to a purchaser token and keeps them.
The reason you revoke with must be recoverable, so register it first: if the person pays later, their original feed URL comes back. A reason that is not on the list throws.
To only ask, without revoking, use benecaster_user_has_active_membership(), which returns true, false or null (cannot tell). Never use benecaster_get_user_tier_for_show() for this: it reads the feed token, which can exist before anyone has paid.
Code
<?php
// Register the reason as recoverable: if the person pays later, their
// original feed URL comes back. Required — a non-recoverable reason throws.
add_filter( 'benecaster_token_reactivation_recoverable_reasons', function ( array $reasons ): array {
$reasons[] = 'my_addon_trial_ended';
return $reasons;
} );
add_action( 'benecaster_boot', function ( \Benecaster\Container $container ): void {
add_action( 'my_addon_trial_expiry', function ( int $user_id, int $show_id ) use ( $container ): void {
$result = $container->make( \Benecaster\Token\TokenManager::class )
->revoke_unless_subscribed( $user_id, $show_id, 'my_addon_trial_ended' );
if ( 'revoked' === $result ) {
// Only now has access actually been taken away — email, log, count.
do_action( 'my_addon_trial_ended', $user_id, $show_id );
} elseif ( 'unknown' === $result ) {
// The membership plugin could not answer. Try again tomorrow.
wp_schedule_single_event( time() + DAY_IN_SECONDS, 'my_addon_trial_expiry', [ $user_id, $show_id ] );
}
// 'subscribed', 'converted', 'no_token' (or anything new): nothing was taken.
}, 10, 2 );
} );
Hooks Used
Need this built rather than just documented? See our services →