Skip to main content

Register a custom Member Thanks query type

Free Advanced Since v1.0.0

Push a key→object pair onto benecaster_member_thanks_query_types where the value implements BenecasterMemberThanksQueryInterface. Required methods: get_label(): string, get_description(): string, and get_results( int $show_id, int $episode_id, array $args ): array. The results array shape must match the built-in query results shape so the Member Thanks template can render it without changes.

Code

<?php
// In your add-on's boot class:
add_filter( 'benecaster_member_thanks_query_types', function ( array $types ): array {
    $types['top_donors'] = new MyAddon\TopDonorsQuery();
    return $types;
} );

// Implement BenecasterMemberThanksQueryInterface:
class TopDonorsQuery implements \Benecaster\MemberThanks\BenecasterMemberThanksQueryInterface {

    public function get_label(): string {
        return __( 'Top Donors', 'my-addon' );
    }

    public function get_description(): string {
        return __( 'Members who have donated the most in the past 30 days.', 'my-addon' );
    }

    public function get_results( int $show_id, int $episode_id, array $args ): array {
        global $wpdb;

        $count     = (int) ( $args['count'] ?? 5 );
        $tier_slug = $args['tier_slug'] ?? '';

        $sql = $wpdb->prepare(
            "SELECT d.user_id, u.display_name, '' AS tier_slug, '' AS joined_at
             FROM {$wpdb->prefix}benecaster_listener_support_donations d
             JOIN {$wpdb->prefix}users u ON u.ID = d.user_id
             WHERE d.show_id = %d
               AND d.donated_at >= DATE_SUB(NOW(), INTERVAL 30 DAY)
             GROUP BY d.user_id
             ORDER BY SUM(d.amount) DESC
             LIMIT %d",
            $show_id,
            $count > 0 ? $count : PHP_INT_MAX
        );

        return $wpdb->get_results( $sql, ARRAY_A ) ?: [];
    }
}

View on GitHub →

Hooks Used