React to a Plan Change Without Diffing It Yourself
benecaster_license_validated fires on every daily check and hands over the whole response, so an add-on that only cares when the plan changed would have to store its own previous value and compare. benecaster_license_plan_changed does that for you and fires only on a real transition. $from and $to are plan slugs, and $show_uuid names a show on the license that changed.
An empty $to means the license lapsed, was revoked, or was never valid: clear anything you lit up on the way in. An empty $from with a plan in $to is the first successful check on a fresh install, which is the license becoming active.
Key what you store on the show, not the site, and read the plan back with benecaster_get_license_plan( $show_uuid ). Every show on a site shares one license, but keying by show keeps your add-on’s state with the show it belongs to.
What it deliberately does not do. It fires only when the cached plan changes, so an unreachable license server, which leaves the cache alone, produces nothing. A hook that fired on every network failure would report a lapsed license every time the connection blipped. Nor does it fire twice for one change, because the next check sees no difference.
Do not use it to decide whether a downgrade is available: downgrade_eligible is on the license check’s response and is cached locally, so read that. This hook answers “what changed”, not “what is allowed next”. It also cannot suppress or customise a plan-change email. Those are sent by benecaster.com before your site hears about the change.
Code
<?php
add_action( 'benecaster_license_plan_changed', function ( string $from, string $to, string $show_uuid ): void {
// Licence lapsed, was revoked, or was never valid: clear anything
// you lit up on the way in. $to is '' in all three cases.
if ( '' === $to ) {
delete_transient( 'my_addon_upgrade_cta_' . $show_uuid );
return;
}
// '' => 'starter' is the first successful validation on a fresh
// install, i.e. the licence becoming active.
if ( '' === $from ) {
my_addon_on_licence_activated( $to, $show_uuid );
return;
}
my_addon_on_plan_moved( $from, $to, $show_uuid );
}, 10, 3 );
Hooks Used
Need this built rather than just documented? See our services →