Skip to main content

TenureBadgeCalculator

\Benecaster\Membership\TenureBadgeCalculator

Class Free

Works out how long a subscriber has been a member and which milestone label that earns. Tenure is counted from the creation time of the subscriber’s private-feed token, which is UTC and is the date Benecaster first issued that subscriber a feed. A join date entered by hand or carried in by an import counts exactly like any other.

The default milestones are 1 month (“New Member”, which covers months 1 to 5), 6 months, 1 year, 2 years, 5 years and 10 years. The subscriber gets the highest milestone their whole months of tenure have reached; under one month there is no milestone and the label is empty. Change the milestone map with the benecaster_tenure_badge_milestones filter (months to label) and the final label with benecaster_tenure_badge_label. Both are fired from this class.

TenureBadgeAppender and the [benecaster_member_since] shortcode are its callers. Add-ons can also use it when they need a tenure figure outside the badge pipeline, for example to gate something on how long someone has subscribed.

Constructor Dependencies

Type Description
\Benecaster\Token\TokenRepository Looks up the subscriber's token for a show, or all their active tokens for the site-wide figure.

Methods

Method Visibility Since Description
calculate( int $user_id, int $show_id ): ?array Public — Returns the tenure snapshot for one subscriber on one show, or null when either ID is not positive or the subscriber has no token for that show. The snapshot is an array with since (the join date as Y-m-d), months (whole calendar months since joining, rounded down), milestone_months (the threshold reached, or 0 below the first), label (the milestone label after the benecaster_tenure_badge_label filter, or an empty string) and token_type (subscriber or follower, taken from the token the tenure was read from and used to classify the chip, never to gate it). The result, including a null result, is cached per user, show and calendar day.
calculate_for_user( int $user_id ): ?array Public — Returns the tenure snapshot for a subscriber with no show in scope, measured from the earliest creation time among their active tokens across all shows, or null when the ID is not positive or they have no active token. Same array shape as calculate(); the benecaster_tenure_badge_label filter receives the ID of the show that contributed that earliest token. Cached per user per calendar day, separately from the per-show results.

Constants

Name Value Description
CACHE_GROUP 'benecaster_tenure' The object-cache group holding computed snapshots, with a one-day lifetime. Keys include the current UTC date, so results roll over at midnight UTC without an explicit flush.

Hooks Fired

Notes

Snapshots are cached for a day, so changing a milestone map, a label, a translation or a token's join date is not reflected on a site with a persistent object cache until the cache rolls over or the benecaster_tenure group is flushed. Without a persistent object cache the cache lasts one request. A calculate() result is per show, so it will not match calculate_for_user() for a subscriber who joined several shows at different times. A filtered milestone map is normalised: keys below 1 and empty or non-string labels are dropped, and an empty result falls back to the defaults, so a filter cannot remove every milestone.