WpRestKernel
in package
Dispatch kernel for attribute-routed WordPress REST controllers.
The counterpart of the admin WpHttpKernel for the REST surface. WP
splits a route into a permission_callback (a boolean gate) and a callback
(the handler); this kernel supplies both from the controller's PHP 8
attributes:
- authorize() reads
#[Auth](method wins over class; absent = login required, a secure default), resolves the user through the injected RequestAuthenticatorInterface (the session/token seam the imperative controllers use) and requires every capability the attribute lists of that user, withuser_can(). - handle() composes the route-middleware chain declared with
#[Middleware](the org-scope/RBAC and rate-limit gate) around the action, resolves the controller from the container, injects the method arguments (the request plus named path parameters) and returns its WP_REST_Response, preserving the product's response envelope.
Route middleware speaks RestRouteMiddlewareInterface (WP-REST-native), not the framework's HttpFoundation contract, so a denial stays inside the REST envelope; a controller wiring an entry that does not implement it fails loud at dispatch (a wiring bug, not a request error).
Table of Contents
Properties
- $authenticator : RequestAuthenticatorInterface
- $container : ContainerInterface
Methods
- __construct() : mixed
- authorize() : true|WP_Error
- Permission callback: enforce the route's `#[Auth]` requirement.
- handle() : WP_REST_Response
- Route callback: run the `#[Middleware]` chain around the action.
- authFor() : Auth
- The effective `#[Auth]` for an action: method-level wins over class-level; an absent attribute defaults to "login required".
- buildDefinition() : CapabilityDefinitionInterface|null
- capabilityOf() : string|null
- The WordPress capability a requirement stands for: its reference key, else the reference of its definition object, else the reference of its definition class, built from the container (or with a zero-argument `new` when not registered). Null when none of them yields a non-empty key.
- denyOnFailure() : WP_Error
- Deny a request whose authorization subject could not be resolved, and log why. Fails closed: the throw becomes a 403, never a 500. A logger that throws in turn is ignored, so logging cannot reopen the 500.
- forbidden() : WP_Error
- instantiateWithoutArguments() : object|null
- invokeAction() : WP_REST_Response
- Resolve the controller, invoke the action and envelope its result — the innermost link the middleware chain wraps.
- objectIdOf() : int|null
- The meta-capability object id of a requirement: its `instanceid` option when set, else the attribute's `instanceId`. Zero means "no object".
- resolveArguments() : array<int, mixed>
- Resolve an action's arguments: the request object by type, otherwise the matching request parameter by name (falling back to the declared default).
- resolveRouteMiddleware() : array<int, RestRouteMiddlewareInterface>
- Resolve the ordered route-middleware chain declared via `#[Middleware]`.
- runRouteMiddleware() : WP_REST_Response
- Compose the route-middleware chain around the action, outermost first.
- userMeets() : bool
- Whether the user holds the capability a requirement stands for, with the meta-capability object id when there is one. Fails closed: a requirement with no resolvable capability, or with an invalid object id, is not met.
Properties
$authenticator
private
RequestAuthenticatorInterface
$authenticator
$container
private
ContainerInterface
$container
Methods
__construct()
public
__construct(ContainerInterface $container, RequestAuthenticatorInterface $authenticator) : mixed
Parameters
- $container : ContainerInterface
- $authenticator : RequestAuthenticatorInterface
authorize()
Permission callback: enforce the route's `#[Auth]` requirement.
public
authorize(string $controllerClass, string $action, WP_REST_Request $request) : true|WP_Error
OPTIONS(CORS preflight) passes.login: falsepasses.- The authenticator resolves the user: a
WP_Erroris returned as is, no user, or a user with an ID that is not positive (WordPress's logged-out user is ID 0), gives401 unauthorized. - Every requirement of the attribute (its
capabilitiesand itsrequirements) must passuser_can()for the resolved user, or the result is403 forbidden. The resolved user may be a token user, so the check never reads the session user (current_user_can()). A requirement whose capability cannot be resolved is denied, never skipped. An attribute with no capability means login only. - The object id of a meta capability is the requirement's
instanceidoption, else the attribute'sinstanceId. Aninstanceidoption that is not a positive integer, or a negativeinstanceId, is denied: an explicit but invalid object id never degrades to a check without one. AninstanceIdof 0 (the default) means "no object": the capability is checked as a primitive one, and core meta capabilities (edit_postand the like) then map todo_not_allowinmap_meta_cap(), so they are denied. - A
Throwablewhile the attribute or a capability is resolved, or whileuser_can()runs, is logged and denied with403 forbidden; it never leaves the permission callback as a 500.
The host of a CapabilityReference is not read: the framework
defines no host convention, so the key goes to user_can() whatever the
host says. A key no role holds is denied;
a multisite super admin passes any key except do_not_allow, which
grants nothing a super admin does not already hold.
Parameters
- $controllerClass : string
- $action : string
- $request : WP_REST_Request
Return values
true|WP_Errorhandle()
Route callback: run the `#[Middleware]` chain around the action.
public
handle(string $controllerClass, string $action, WP_REST_Request $request) : WP_REST_Response
The chain is resolved before the try/catch so a middleware that violates RestRouteMiddlewareInterface fails loud (a wiring bug surfaced at dispatch, mirroring how the framework kernel treats route middleware). Any throw from the chain or the action is caught and enveloped as a 500, so a request-time failure never breaks the REST response envelope.
Parameters
- $controllerClass : string
- $action : string
- $request : WP_REST_Request
Return values
WP_REST_ResponseauthFor()
The effective `#[Auth]` for an action: method-level wins over class-level; an absent attribute defaults to "login required".
private
authFor(string $controllerClass, string $action) : Auth
Parameters
- $controllerClass : string
- $action : string
Return values
AuthbuildDefinition()
private
buildDefinition(string|null $definitionClass) : CapabilityDefinitionInterface|null
Parameters
- $definitionClass : string|null
Return values
CapabilityDefinitionInterface|nullcapabilityOf()
The WordPress capability a requirement stands for: its reference key, else the reference of its definition object, else the reference of its definition class, built from the container (or with a zero-argument `new` when not registered). Null when none of them yields a non-empty key.
private
capabilityOf(CapabilityRequirement $requirement) : string|null
Parameters
- $requirement : CapabilityRequirement
Return values
string|nulldenyOnFailure()
Deny a request whose authorization subject could not be resolved, and log why. Fails closed: the throw becomes a 403, never a 500. A logger that throws in turn is ignored, so logging cannot reopen the 500.
private
denyOnFailure(Throwable $throwable, string $controllerClass, string $action) : WP_Error
Parameters
- $throwable : Throwable
- $controllerClass : string
- $action : string
Return values
WP_Errorforbidden()
private
forbidden() : WP_Error
Return values
WP_ErrorinstantiateWithoutArguments()
private
instantiateWithoutArguments(CapabilityDefinitionInterface> $class) : object|null
Parameters
- $class : CapabilityDefinitionInterface>
Return values
object|nullinvokeAction()
Resolve the controller, invoke the action and envelope its result — the innermost link the middleware chain wraps.
private
invokeAction(string $controllerClass, string $action, WP_REST_Request $request) : WP_REST_Response
Parameters
- $controllerClass : string
- $action : string
- $request : WP_REST_Request
Return values
WP_REST_ResponseobjectIdOf()
The meta-capability object id of a requirement: its `instanceid` option when set, else the attribute's `instanceId`. Zero means "no object".
private
objectIdOf(CapabilityRequirement $requirement, int $instanceId) : int|null
Null means an explicit but invalid id (an option that is not a positive integer, or a negative attribute id), which the caller denies.
Parameters
- $requirement : CapabilityRequirement
- $instanceId : int
Return values
int|nullresolveArguments()
Resolve an action's arguments: the request object by type, otherwise the matching request parameter by name (falling back to the declared default).
private
resolveArguments(ReflectionMethod $reflection, WP_REST_Request $request) : array<int, mixed>
Parameters
- $reflection : ReflectionMethod
- $request : WP_REST_Request
Return values
array<int, mixed>resolveRouteMiddleware()
Resolve the ordered route-middleware chain declared via `#[Middleware]`.
private
resolveRouteMiddleware(class-string $controllerClass, string $action) : array<int, RestRouteMiddlewareInterface>
Class-level declarations come first (outermost), then method-level, each in
declaration order; the attribute is repeatable so several accumulate. Each
entry is fetched from the container — falling back to a zero-argument new
when unregistered — and must implement RestRouteMiddlewareInterface.
Parameters
- $controllerClass : class-string
- $action : string
Return values
array<int, RestRouteMiddlewareInterface>runRouteMiddleware()
Compose the route-middleware chain around the action, outermost first.
private
runRouteMiddleware(array<int, RestRouteMiddlewareInterface> $middlewares, WP_REST_Request $request, callable(WP_REST_Request): WP_REST_Response $action) : WP_REST_Response
Parameters
- $middlewares : array<int, RestRouteMiddlewareInterface>
- $request : WP_REST_Request
- $action : callable(WP_REST_Request): WP_REST_Response
Return values
WP_REST_ResponseuserMeets()
Whether the user holds the capability a requirement stands for, with the meta-capability object id when there is one. Fails closed: a requirement with no resolvable capability, or with an invalid object id, is not met.
private
userMeets(WP_User $user, CapabilityRequirement $requirement, int $instanceId) : bool
Parameters
- $user : WP_User
- $requirement : CapabilityRequirement
- $instanceId : int