Skip to main content

BridgeManager

Benecaster\Bridge\BridgeManager

Responsible for bridge detection, activation, and lookup. BridgeManager knows which membership plugins are installed and active, which bridge is configured for each show, and how to retrieve a bridge instance on demand.

Core and add-ons never instantiate bridges directly — they ask BridgeManager to hand them the right bridge for a given show. This ensures the fallback to NullBridge is automatic and consistent.

Getting the Instance

BridgeManager is available through the Benecaster service container. Access it via benecaster_boot:

add_action( 'benecaster_boot', function ( \Benecaster\Container $container ) {
    $manager = $container->make( \Benecaster\Bridge\BridgeManager::class );
} );

Do not instantiate BridgeManager directly. It is registered as a singleton in the container — you will receive the same instance throughout the request.

Methods

get_available_bridges(): array

Returns all bridges whose underlying plugin is currently active on the site.

Used by the bridge picker UI in Settings to show only the plugins the site actually has installed. Detection uses class_exists() for class-identified plugins (MemberPress, WooCommerce Subscriptions) and defined() for constant-identified plugins (Paid Memberships Pro, Restrict Content Pro). WooCommerce Memberships is no longer a supported bridge and is not detected.

Returns: array<int, array{slug: string, name: string}>

$available = $manager->get_available_bridges();
// Example:
// [
//   ['slug' => 'memberpress', 'name' => 'MemberPress'],
//   ['slug' => 'woocommerce-subscriptions', 'name' => 'WooCommerce Subscriptions'],
// ]

get_active_bridge( int $show_id ): BridgeInterface

Returns the configured bridge for a show.

Reads the _benecaster_show_active_bridge post meta on the show post to find the stored slug, then returns the corresponding bridge instance. If no bridge has been configured, or if the stored slug does not match any known bridge, returns a NullBridge instance.

The return type is always a valid BridgeInterface — callers do not need to check for null. Any code that calls bridge methods can do so unconditionally.

Parameters:

Parameter Type Description
$show_id int Benecaster show post ID

Returns: BridgeInterface — configured bridge, or NullBridge if none is set

$bridge = $manager->get_active_bridge( $show_id );
$tier   = $bridge->get_user_tier( $user_id, $show_id );
// $tier is null if NullBridge is active or user is not subscribed

set_active_bridge( int $show_id, string $slug ): bool

Persists the active bridge slug for a show.

Stores the slug in _benecaster_show_active_bridge post meta. On the next request, get_active_bridge() returns the corresponding bridge. Returns false for unrecognised slugs — the meta is not updated if the slug doesn’t match a registered bridge.

Pass an empty string to clear the active bridge. After clearing, get_active_bridge() returns NullBridge.

Parameters:

Parameter Type Description
$show_id int Benecaster show post ID
$slug string Bridge slug, or empty string to clear

Returns: booltrue on success, false for unrecognised slugs

$success = $manager->set_active_bridge( $show_id, 'memberpress' );
// Returns false if 'memberpress' bridge is not registered

get_bridge_by_slug( string $slug ): BridgeInterface

Returns a bridge instance by slug, regardless of show context.

Used internally by SubscriptionListener to register event callbacks on all active bridges at boot time. May also be useful in custom code that needs a specific bridge regardless of which show is being processed.

Returns NullBridge for unknown or unregistered slugs.

Parameters:

Parameter Type Description
$slug string Bridge slug

Returns: BridgeInterface — matching bridge, or NullBridge for unknown slugs

Detection Mechanism

get_available_bridges() determines which bridges are available by checking whether each bridge’s detection condition is met at request time:

Bridge Detection
MemberPress class_exists('MeprUser')
WooCommerce Subscriptions class_exists('WC_Subscriptions')
Paid Memberships Pro defined('PMPRO_VERSION')
Restrict Content Pro defined('RCP_PLUGIN_VERSION')

Detection is evaluated on every request — if a plugin is deactivated, its bridge is no longer in the available list on the next request. The set_active_bridge() stored slug is not automatically cleared if the underlying plugin is deactivated; get_active_bridge() falls back to NullBridge in that case.