AbstractWpRestController
in package
implements
RestControllerInterface
Base for WordPress REST controllers: instance-based with a {@see RequestAuthenticatorInterface} injected for its permission callbacks (replacing the old static auth middleware), plus the sanitize seam readers, required-field validation and the `route()` registration helper.
Table of Contents
Interfaces
Properties
Methods
- __construct() : mixed
- adminPermissionCheck() : true|WP_Error
- Permission callback for admin-only routes.
- permissionCheck() : true|WP_Error
- Permission callback for authenticated routes.
- publicPermissionCheck() : true
- Permission callback for public routes (no auth required).
- getBody() : array<string|int, mixed>
- Parse JSON body from request.
- getUser() : WP_User|null
- Get the authenticated user from the request, or null when the request is not authenticated (including a resolved user whose ID is not positive).
- route() : void
- Register a REST route helper.
- sanitizedEmail() : string
- Read an email scalar from the request, sanitized through the Sanitize seam. Returns the default when the value is missing or not a valid email.
- sanitizedHtml() : string
- Read an HTML scalar from the request, filtered through the Sanitize seam against the WordPress post-content allowlist (`wp_kses_post`).
- sanitizedKey() : string
- Read a key/slug scalar from the request, sanitized through the Sanitize seam (lowercase; alphanumerics, dashes, underscores only).
- sanitizedText() : string
- Read a single-line scalar from the request, sanitized through the Sanitize seam (strips tags + control whitespace). Use this for inbound text fields instead of reading `get_param()` raw at the controller boundary.
- sanitizedTextarea() : string
- Read a multi-line text scalar (e.g. a textarea body) from the request, sanitized through the Sanitize seam, preserving newlines.
- validateRequired() : WP_REST_Response|null
- Validate required fields are present in data.
- resolveAuthenticatedUser() : WP_Error|WP_User
- Resolve the request user and reject anything that is not a logged-in account. WordPress represents "not logged in" as a WP_User with ID 0, and an authenticator (for example a token one that falls back to the session) may hand that object back, so an ID that is not positive is treated as unauthenticated (401), never as a user.
Properties
$authenticator read-only
protected
RequestAuthenticatorInterface
$authenticator
Methods
__construct()
public
__construct(RequestAuthenticatorInterface $authenticator) : mixed
Parameters
- $authenticator : RequestAuthenticatorInterface
adminPermissionCheck()
Permission callback for admin-only routes.
public
adminPermissionCheck(WP_REST_Request $request) : true|WP_Error
Parameters
- $request : WP_REST_Request
Return values
true|WP_ErrorpermissionCheck()
Permission callback for authenticated routes.
public
permissionCheck(WP_REST_Request $request) : true|WP_Error
Parameters
- $request : WP_REST_Request
Return values
true|WP_ErrorpublicPermissionCheck()
Permission callback for public routes (no auth required).
public
publicPermissionCheck(WP_REST_Request $request) : true
Parameters
- $request : WP_REST_Request
Return values
truegetBody()
Parse JSON body from request.
protected
getBody(WP_REST_Request $request) : array<string|int, mixed>
Parameters
- $request : WP_REST_Request
Return values
array<string|int, mixed>getUser()
Get the authenticated user from the request, or null when the request is not authenticated (including a resolved user whose ID is not positive).
protected
getUser(WP_REST_Request $request) : WP_User|null
Parameters
- $request : WP_REST_Request
Return values
WP_User|nullroute()
Register a REST route helper.
protected
route(string $namespace, string $path, string $method, callable $callback[, callable|null $permission = null ]) : void
Parameters
- $namespace : string
- $path : string
- $method : string
- $callback : callable
- $permission : callable|null = null
sanitizedEmail()
Read an email scalar from the request, sanitized through the Sanitize seam. Returns the default when the value is missing or not a valid email.
protected
sanitizedEmail(WP_REST_Request $request, string $key[, string $default = '' ]) : string
Parameters
- $request : WP_REST_Request
- $key : string
- $default : string = ''
Return values
stringsanitizedHtml()
Read an HTML scalar from the request, filtered through the Sanitize seam against the WordPress post-content allowlist (`wp_kses_post`).
protected
sanitizedHtml(WP_REST_Request $request, string $key[, string $default = '' ]) : string
Parameters
- $request : WP_REST_Request
- $key : string
- $default : string = ''
Return values
stringsanitizedKey()
Read a key/slug scalar from the request, sanitized through the Sanitize seam (lowercase; alphanumerics, dashes, underscores only).
protected
sanitizedKey(WP_REST_Request $request, string $key[, string $default = '' ]) : string
Parameters
- $request : WP_REST_Request
- $key : string
- $default : string = ''
Return values
stringsanitizedText()
Read a single-line scalar from the request, sanitized through the Sanitize seam (strips tags + control whitespace). Use this for inbound text fields instead of reading `get_param()` raw at the controller boundary.
protected
sanitizedText(WP_REST_Request $request, string $key[, string $default = '' ]) : string
Parameters
- $request : WP_REST_Request
- $key : string
- $default : string = ''
Return values
stringsanitizedTextarea()
Read a multi-line text scalar (e.g. a textarea body) from the request, sanitized through the Sanitize seam, preserving newlines.
protected
sanitizedTextarea(WP_REST_Request $request, string $key[, string $default = '' ]) : string
Parameters
- $request : WP_REST_Request
- $key : string
- $default : string = ''
Return values
stringvalidateRequired()
Validate required fields are present in data.
protected
validateRequired(array<string|int, mixed> $data, array<string|int, mixed> $fields) : WP_REST_Response|null
Parameters
- $data : array<string|int, mixed>
- $fields : array<string|int, mixed>
Return values
WP_REST_Response|nullresolveAuthenticatedUser()
Resolve the request user and reject anything that is not a logged-in account. WordPress represents "not logged in" as a WP_User with ID 0, and an authenticator (for example a token one that falls back to the session) may hand that object back, so an ID that is not positive is treated as unauthenticated (401), never as a user.
private
resolveAuthenticatedUser(WP_REST_Request $request) : WP_Error|WP_User
Parameters
- $request : WP_REST_Request