Skip to main content

NullBridge

Benecaster\Bridge\NullBridge

A safe, inert implementation of BridgeInterface returned by BridgeManager when no subscription plugin is configured or when an unknown slug is requested. All query methods return empty values. All event registration methods do nothing.

What NullBridge Returns

Method Return value
get_plugin_slug() 'null'
get_user_tier() null
is_user_active() false
get_all_tiers() []
on_subscription_activated() no-op
on_subscription_cancelled() no-op
on_subscription_changed() no-op
on_subscription_renewed() no-op
on_payment_failed() no-op

When BridgeManager Returns NullBridge

BridgeManager::get_active_bridge() returns a NullBridge instance in two situations:

  • No bridge configured — the show has no _benecaster_show_active_bridge post meta set, or it was explicitly cleared by passing an empty string to set_active_bridge().
  • Unknown slug — the stored slug doesn’t match any registered bridge. This can happen if a bridge plugin is deactivated after its slug was saved, or if a custom bridge is no longer registered.

BridgeManager::get_bridge_by_slug() also returns NullBridge for any unrecognised slug.

Why It Exists

Without the null object pattern, every piece of code that retrieves a bridge would need a guard:

$bridge = $manager->get_active_bridge( $show_id );
if ( $bridge !== null ) {
    $tier = $bridge->get_user_tier( $user_id, $show_id );
}

With NullBridge, the guard is unnecessary:

$bridge = $manager->get_active_bridge( $show_id );
$tier   = $bridge->get_user_tier( $user_id, $show_id ); // null — NullBridge returns null safely

The result is the same either way — $tier is null when no bridge is configured — but the calling code is simpler and less prone to being written incorrectly. NullBridge ensures that premium features degrade gracefully on free installs or unconfigured sites without defensive coding throughout core.

Using NullBridge in Your Own Code

You don’t instantiate NullBridge directly. Rely on BridgeManager to return it when appropriate. If you need to check whether a real bridge is configured, compare the slug:

$bridge = $manager->get_active_bridge( $show_id );

if ( $bridge->get_plugin_slug() === 'null' ) {
    // No bridge configured for this show.
}

Alternatively, check get_available_bridges() to see whether any bridge is available site-wide, or read the _benecaster_show_active_bridge post meta directly if you only need to know whether a slug has been stored.