Enumerate WP user IDs eligible for a broadcast audience, with follower support
Benecaster resolves a broadcast audience slug to a list of eligible WordPress user IDs. Built-in slugs: paying (paying subscribers only), followers (direct followers only), all (every active token holder). Add-ons register custom audience slugs by hooking benecaster_broadcast_audience_user_ids. Unknown built-in slugs resolve to an empty array before the filter fires, so an add-on can fully define a custom audience. Check opt-out status before handing the list to an email queue or ESP API.
Code
<?php
$repo = \Benecaster\Plugin::instance()->make( \Benecaster\Token\TokenRepository::class );
$ids = $repo->find_audience_user_ids( $show_id, 'paying' );
foreach ( $ids as $user_id ) {
$user = get_userdata( $user_id );
// Hand off to the email queue, an ESP API call, an export sheet, etc.
}
// Register a custom per-tier audience slug.
add_filter( 'benecaster_broadcast_audience_user_ids', function (
array $user_ids,
string $audience,
int $show_id
): array {
if ( 0 !== strpos( $audience, 'tier:' ) ) {
return $user_ids;
}
$tier_slug = substr( $audience, 5 );
global $wpdb;
$rows = $wpdb->get_col( $wpdb->prepare(
"SELECT DISTINCT user_id FROM {$wpdb->prefix}benecaster_tokens
WHERE show_id = %d AND status = 'active' AND tier_slug = %s",
$show_id,
$tier_slug
) );
return array_map( 'intval', (array) $rows );
}, 10, 3 );
Hooks Used
-
benecaster_broadcast_audience_user_ids