REST API
Overview
The REST API module provides fluent builders for route registration, request argument schemas, resource schemas, and initial-data serialization. It is aimed at WordPress REST integrations that want typed, composable configuration objects.
Types in this module
Lipe\Lib\Rest_Api\Arguments_SchemaLipe\Lib\Rest_Api\Initial_DataLipe\Lib\Rest_Api\Register_Rest_RouteLipe\Lib\Rest_Api\Resource_SchemaLipe\Lib\Rest_Api\Schema\Argument_PropLipe\Lib\Rest_Api\Schema\ArrayTypeLipe\Lib\Rest_Api\Schema\BooleanTypeLipe\Lib\Rest_Api\Schema\IntegerTypeLipe\Lib\Rest_Api\Schema\NullTypeLipe\Lib\Rest_Api\Schema\NumberTypeLipe\Lib\Rest_Api\Schema\ObjectTypeLipe\Lib\Rest_Api\Schema\Prop(trait)Lipe\Lib\Rest_Api\Schema\PropRules(interface)Lipe\Lib\Rest_Api\Schema\Resource_PropLipe\Lib\Rest_Api\Schema\StringTypeLipe\Lib\Rest_Api\Schema\TypeLipe\Lib\Rest_Api\Schema\TypeRules(interface)Lipe\Lib\Rest_Api\Register_Rest_Route\Method
Register_Rest_Route
Fluent wrapper around register_rest_route() that composes one or more HTTP method definitions and an optional schema.
Key public methods
public function __construct(array $do_not_use)public function method(string $methods): Methodpublic function schema(Resource_Schema $schema): staticpublic function get_args(): array
Example
<?php
use Lipe\Lib\Rest_Api\Register_Rest_Route;
use WP_REST_Request;
use WP_REST_Server;
$route = new Register_Rest_Route([]);
$route->method(WP_REST_Server::READABLE)
->callback(function(WP_REST_Request $request): array {
return ['ok' => true];
})
->permission_callback(fn () => current_user_can('read'));
register_rest_route('acme/v1', '/books', $route->get_args());
Method
Represents one method entry inside a REST route.
Key public methods
public function args(Arguments_Schema $args): staticpublic function callback(\Closure $callback): staticpublic function permission_callback(\Closure $callback): staticpublic function methods(string $methods): static
Arguments_Schema
Builds a REST endpoint args array.
Key public methods
public function prop(string $key): Argument_Proppublic function get_args(): array
Resource_Schema
Builds a public resource schema.
Key public methods
public function title(string $title): staticpublic function type(): Typepublic function get_args(): array
Schema property and type builders
Argument_Propaddspublic function default(mixed $default_value): Argument_Prop,public function validate_callback(callable $callback): Argument_Prop, andpublic function sanitize_callback(callable $callback): Argument_Prop.Resource_Propaddspublic function context(array $context): staticandpublic function readonly(bool $is_readonly): static.Propprovides sharedtitle(),description(),required(),type(), andget_args()helpers.Typemaps to concrete schema types throughstring(),array(),object(),number(),integer(),boolean(),null(),one_of(),any_of(), andget_args().StringType,ArrayType,ObjectType,NumberType, andIntegerTypeexpose the specialized validation methods shown in source, such asenum(),pattern(),items(),prop(),minimum(), andmaximum().BooleanTypeandNullTyperepresent leaf schema types.PropRulesandTypeRulesdefine the common contracts.
Initial_Data
Serializes posts, terms, users, comments, and attachments using the shapes provided by the WordPress REST server.
Key public methods
public function is_retrieving(): boolpublic function get_comments_data(array $comments, bool $with_links = false, array|bool $embed = false): arraypublic function get_post_data(?array $posts = null, bool $with_links = false, array|bool $embed = false): arraypublic function get_user_data(array $users, bool $with_links = false, array|bool $embed = false): arraypublic function get_term_data(array $terms, bool $with_links = false, array|bool $embed = false): arraypublic function get_attachments_data(array $attachments, bool $with_links = false, array|bool $embed = false): array