renderer (callable), or true when only presence was * declared, for every workspace the Pro add-on registered at a compatible * contract version. * * @var array */ private static $workspaces = array(); /** @var array Workspace key => tab label the add-on offers. */ private static $labels = array(); /** * Register the add-on's renderer for a workspace. Also marks it present. * * Call this at require time in the module that owns the workspace, not * gated on entitlement state: presence and entitlement are different * questions, and the renderer handed over here is where the second one is * answered. * * @param string $key Workspace key: metadata, schema or local. * @param callable $render Renders the workspace (or its locked state). Takes no arguments. * @param int $contract_version The CONTRACT_VERSION the caller was built against. * @return bool True if the registration was accepted. */ public static function register_workspace( $key, $render, $contract_version = self::CONTRACT_VERSION ) { $key = (string) $key; if ( '' === $key || (int) $contract_version !== self::CONTRACT_VERSION || ! is_callable( $render ) ) { return false; } self::$workspaces[ $key ] = $render; return true; } /** * Declare that the Pro add-on provides a workspace, without a renderer. * Kept for callers that only need presence; register_workspace() is the * normal path. * * @param string $key Workspace key. * @param int $contract_version The CONTRACT_VERSION the caller was built against. * @return bool True if the registration was accepted. */ public static function mark_present( $key, $contract_version = self::CONTRACT_VERSION ) { $key = (string) $key; if ( '' === $key || (int) $contract_version !== self::CONTRACT_VERSION ) { return false; } if ( ! isset( self::$workspaces[ $key ] ) ) { self::$workspaces[ $key ] = true; } return true; } /** * Whether the Pro add-on has registered $key at a compatible contract * version. This says nothing about entitlement state. * * @param string $key Workspace key. * @return bool */ public static function is_available( $key ) { return ! empty( self::$workspaces[ (string) $key ] ); } /** * Register the tab label to show for $key once the add-on is present, so * a tab never shows real add-on content under core's own absent-state name. * * @param string $key Workspace key. * @param string $label Label to show once the add-on is present. * @param int $contract_version The CONTRACT_VERSION the caller was built against. * @return bool True if accepted. */ public static function register_label( $key, $label, $contract_version = self::CONTRACT_VERSION ) { $key = (string) $key; if ( '' === $key || (int) $contract_version !== self::CONTRACT_VERSION || '' === (string) $label ) { return false; } self::$labels[ $key ] = (string) $label; return true; } /** @param string $key Workspace key. @param string $default Fallback. @return string */ public static function label( $key, $default ) { $key = (string) $key; return isset( self::$labels[ $key ] ) && '' !== self::$labels[ $key ] ? self::$labels[ $key ] : (string) $default; } /** * Render the add-on's workspace for $key, if one is registered. * * @param string $key Workspace key. * @return bool True if a registered renderer ran; false if the caller * should show its own absent-state presentation instead. */ public static function render( $key ) { $key = (string) $key; if ( empty( self::$workspaces[ $key ] ) || ! is_callable( self::$workspaces[ $key ] ) ) { return false; } call_user_func( self::$workspaces[ $key ] ); return true; } /** * Test-only: clear registrations between fixtures in the same process. * * @return void */ public static function reset_for_tests() { self::$workspaces = array(); self::$labels = array(); } }