InertiaAdapter
in package
Per-component Inertia adapter for the WordPress admin SPA pipeline.
Thin WordPress plumbing over the framework's Inertia v3 wire: the whole
protocol — closure/optional()/defer()/merge() prop resolution, partial
reloads (only/except), deferred/merge/match metadata, asset-version skew
(409 + X-Inertia-Location), X-Inertia-Reset and entity normalization — is
delegated to InertiaResponse through
InertiaFactory::render(). This adapter contributes only the parts that
are genuinely WordPress: the CSRF nonce shared as csrf_token, the
component-namespaced mount node ({component}-app), the wp-admin HTML shell,
the per-component asset version, the SPA asset enqueue, and the XHR detection
helper the admin router intercepts on.
Instance-scoped: each host plugin builds one adapter from its own HostComponentContextInterface (resolved through the plugin's own DI container), so two plugins living in the same request never share shared-props, mount id, CSRF nonce action, or asset version. The framework wire it delegates to is configured (version + HTML bootstrap) per render(), which is safe because wp-admin serves exactly one component per request — the framework statics are the documented adapter boot seam.
Request/response boundaries follow the CsrfGuard split: the framework wire
reads the request superglobals at its own boundary, while every response
side-effect (status, headers, body, redirect, termination) goes through the
injected ResponseEmitterInterface. This keeps the JSON/redirect/exit
paths that used to be untestable inline header/echo/exit assertable.
Table of Contents
Properties
- $context : HostComponentContextInterface
- $emitter : ResponseEmitterInterface
- $sharedProps : array<string, mixed>
Methods
- __construct() : mixed
- enqueueAssets() : void
- Register + enqueue the SPA bundle (and optional stylesheet) for this host component, cache-busted by the component's asset version.
- isInertiaRequest() : bool
- Whether the current request is an Inertia XHR. Pure: takes the server array (read once at the boundary) instead of touching `$_SERVER` directly.
- location() : never
- mountId() : string
- The DOM id the SPA mounts on, namespaced by the host component so two plugins never collide on one `#app` node. The frontend entry must mount on exactly this id.
- nonceAction() : string
- The WordPress nonce action this component's admin SPA uses for CSRF, derived from the component so each plugin owns an isolated nonce.
- render() : void
- Render an Inertia page for this component, emitting the response through the injected emitter.
- share() : void
- configureFrameworkWire() : void
- Point the framework's Inertia wire at THIS component before delegating: its own asset version (each plugin cache-busts independently) and a wp-admin HTML bootstrap that mounts the SPA on the component-namespaced node with the page payload in `data-page`.
- emit() : void
- Emit a framework HttpFoundation response through the WordPress emitter seam (status, headers preserving their original case, body). Headers keep their case so an Inertia client reads `X-Inertia`, `Vary`, etc. exactly.
- getVersion() : string
- htmlShell() : string
- The wp-admin mount node the framework's HTML branch writes on a first visit: the component-namespaced div carrying the page payload in `data-page`.
- resolveProps() : array<string, mixed>
- Build the props handed to the framework wire: shared props overlaid by the per-render local props, plus the auto-shared WP nonce.
Properties
$context read-only
private
HostComponentContextInterface
$context
$emitter read-only
private
ResponseEmitterInterface
$emitter
= new PhpSapiEmitter()
$sharedProps
private
array<string, mixed>
$sharedProps
= []
Methods
__construct()
public
__construct(HostComponentContextInterface $context[, ResponseEmitterInterface $emitter = new PhpSapiEmitter() ]) : mixed
Parameters
- $context : HostComponentContextInterface
- $emitter : ResponseEmitterInterface = new PhpSapiEmitter()
enqueueAssets()
Register + enqueue the SPA bundle (and optional stylesheet) for this host component, cache-busted by the component's asset version.
public
enqueueAssets(string $handle, string $scriptSrc[, string|null $styleSrc = null ][, array<string|int, mixed> $scriptDeps = [] ]) : void
Host-neutral by design: the host plugin supplies the asset URLs — only it
knows its own plugins_url() — while the adapter contributes the
WordPress enqueue wiring and the version. This is deliberately independent
of render() (which emits only the mount node); call it from the
host's admin_enqueue_scripts / wp_enqueue_scripts hook.
Parameters
- $handle : string
- $scriptSrc : string
- $styleSrc : string|null = null
- $scriptDeps : array<string|int, mixed> = []
isInertiaRequest()
Whether the current request is an Inertia XHR. Pure: takes the server array (read once at the boundary) instead of touching `$_SERVER` directly.
public
static isInertiaRequest(array<string, mixed> $server) : bool
Parameters
- $server : array<string, mixed>
Return values
boollocation()
public
location(string $url) : never
Parameters
- $url : string
Return values
nevermountId()
The DOM id the SPA mounts on, namespaced by the host component so two plugins never collide on one `#app` node. The frontend entry must mount on exactly this id.
public
mountId() : string
Return values
stringnonceAction()
The WordPress nonce action this component's admin SPA uses for CSRF, derived from the component so each plugin owns an isolated nonce.
public
nonceAction() : string
Return values
stringrender()
Render an Inertia page for this component, emitting the response through the injected emitter.
public
render(string $component[, array<string, mixed> $props = [] ]) : void
Builds the props (shared + local + auto csrf_token) and hands them to the
framework wire, which decides between the JSON (Inertia XHR) and HTML shell
(first visit) branches and applies every v3 rule. The Inertia XHR branch
pre-empts the wp-admin HTML shell and therefore terminates; a full visit
writes the mount node and returns so WordPress finishes the admin page.
Parameters
- $component : string
- $props : array<string, mixed> = []
share()
public
share(string $key, mixed $value) : void
Parameters
- $key : string
- $value : mixed
configureFrameworkWire()
Point the framework's Inertia wire at THIS component before delegating: its own asset version (each plugin cache-busts independently) and a wp-admin HTML bootstrap that mounts the SPA on the component-namespaced node with the page payload in `data-page`.
private
configureFrameworkWire() : void
Set per render because wp-admin serves one component per request, so there is never a second component's render in flight to clobber; the framework's static version manager and HTML bootstrap are the documented adapter boot seam for exactly this.
emit()
Emit a framework HttpFoundation response through the WordPress emitter seam (status, headers preserving their original case, body). Headers keep their case so an Inertia client reads `X-Inertia`, `Vary`, etc. exactly.
private
emit(Response $response) : void
Parameters
- $response : Response
getVersion()
private
getVersion() : string
Return values
stringhtmlShell()
The wp-admin mount node the framework's HTML branch writes on a first visit: the component-namespaced div carrying the page payload in `data-page`.
private
htmlShell(string $json) : string
Escapes exactly once at this output boundary, through the Escape seam. The JSON is the framework's page payload — it is NOT pre-escaped, so this attr() call is the single (and only) HTML-attribute escaping layer over the data-page value. Do not add another.
Parameters
- $json : string
Return values
stringresolveProps()
Build the props handed to the framework wire: shared props overlaid by the per-render local props, plus the auto-shared WP nonce.
private
resolveProps(array<string, mixed> $props) : array<string, mixed>
The nonce is exposed as csrf_token so the SPA can echo it back via the
X-WP-Nonce header that CsrfGuard verifies on mutating requests; an
explicit share/prop of the same key wins. Everything else (closures,
optional()/defer()/merge() wrappers, and any contract envelope) is
forwarded verbatim — resolution, partial-reload filtering and normalization
are the framework wire's responsibility, and a legacy contract is passed
through unchanged so the frontend rejects it rather than being silently
rewritten here.
Parameters
- $props : array<string, mixed>