API / Minn\Rest / TemplatesController

TemplatesController final readonly class

Minn\Rest\TemplatesController · public/minn/src/Minn/Rest/TemplatesController.php · 324 lines

wp/v2/templates and wp/v2/template-parts: the block theme's templates as one list, whether they come from the theme's files, from a row the site saved over one, or from a plugin. Reading needs only edit_posts (the reference lets an editor or author see the layout); changing anything needs edit_theme_options.

Used by 1

Constants and properties

privateCREATE_BODY = array ( 'slug' => array ( 'description' => 'Unique slug identifying the template.', 'type' => 'string', 'minLength' => 1, 'pattern' => '[a-zA-Z0-9_\\%-]+', 'required' => true, ), )

What creating a template must send (the reference's create arguments).

privateLOOKUP = array ( 'slug' => array ( 'description' => 'The slug of the template to get the fallback for', 'type' => 'string', 'required' => true, ), 'is_custom' => array ( 'description' => 'Indicates if a template is custom or part of the template hierarchy', 'type' => 'boolean', 'required' => false, ), 'template_prefix' => array ( 'description' => 'The template prefix for the created template. This is used to extract the main template type, e.g. in `taxonomy-books` extracts the `taxonomy`', 'type' => 'string', 'required' => false, ), )
privateLIST = array ( 'wp_id' => array ( 'description' => 'Limit to the specified post id.', 'type' => 'integer', ), 'area' => array ( 'description' => 'Limit to the specified template part area.', 'type' => 'string', ), 'post_type' => array ( 'description' => 'Post type to get the templates for.', 'type' => 'string', ), )

The list filters both routes take, as the reference declares them.

privateREAD_CAP = 'edit_posts'
privateWRITE_CAP = 'edit_theme_options'
privateREFUSAL = 'Sorry, you are not allowed to access the templates on this site.'
privatereadonly Minn\Theme\TemplateIndex $index

Promoted from the constructor.

privatereadonly Minn\Theme\TemplateWriter $writer

Promoted from the constructor.

privatereadonly Minn\Rest\TemplateObject $object

Promoted from the constructor.

privatereadonly Minn\Rest\Caller $caller

Promoted from the constructor.

Public

new__construct(Minn\Theme\TemplateIndex $index, Minn\Theme\TemplateWriter $writer, Minn\Rest\TemplateObject $object, Minn\Rest\Caller $caller)#
methodtemplates(Minn\Http\Request $request): Minn\Http\ResponseGET /wp/v2/templates (cap edit_posts)#

The templates list.

methodparts(Minn\Http\Request $request): Minn\Http\ResponseGET /wp/v2/template-parts (cap edit_posts)#

The template parts list.

methodlookupTemplate(Minn\Http\Request $request): Minn\Http\ResponseGET /wp/v2/templates/lookup (cap edit_posts)#

The template a slug would use: the first in its hierarchy the theme or the site has.

methodlookupPart(Minn\Http\Request $request): Minn\Http\ResponseGET /wp/v2/template-parts/lookup (cap edit_posts)#

The same lookup under the parts route: it searches templates, not parts, as the reference's does.

methodtemplate(Minn\Http\Request $request, string $id): Minn\Http\ResponseGET /wp/v2/templates/{id:([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+} (cap edit_posts)#

One template.

methodpart(Minn\Http\Request $request, string $id): Minn\Http\ResponseGET /wp/v2/template-parts/{id:([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+} (cap edit_posts)#

One template part.

methodcreateTemplate(Minn\Http\Request $request): Minn\Http\ResponsePOST /wp/v2/templates (cap edit_posts + edit_theme_options)#

Creates a template.

methodcreatePart(Minn\Http\Request $request): Minn\Http\ResponsePOST /wp/v2/template-parts (cap edit_posts + edit_theme_options)#

Creates a template part.

methodsaveTemplate(Minn\Http\Request $request, string $id): Minn\Http\ResponsePOST /wp/v2/templates/{id:([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+} (cap edit_posts + edit_theme_options)PUT /wp/v2/templates/{id:([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+} (cap edit_posts + edit_theme_options)PATCH /wp/v2/templates/{id:([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+} (cap edit_posts + edit_theme_options)#

Saves a template.

methodsavePart(Minn\Http\Request $request, string $id): Minn\Http\ResponsePOST /wp/v2/template-parts/{id:([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+} (cap edit_posts + edit_theme_options)PUT /wp/v2/template-parts/{id:([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+} (cap edit_posts + edit_theme_options)PATCH /wp/v2/template-parts/{id:([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+} (cap edit_posts + edit_theme_options)#

Saves a template part.

methoddeleteTemplate(Minn\Http\Request $request, string $id): Minn\Http\ResponseDELETE /wp/v2/templates/{id:([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+} (cap edit_posts + edit_theme_options)#

Deletes a customised template.

methoddeletePart(Minn\Http\Request $request, string $id): Minn\Http\ResponseDELETE /wp/v2/template-parts/{id:([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+} (cap edit_posts + edit_theme_options)#

Deletes a customised template part.

Internals 13

Private and protected methods: how the class does its work. Not a contract; they change whenever the code is tidied.

privatelisting(Minn\Http\Request $request, string $type): Minn\Http\Response#
privatelisted(Minn\Theme\TemplateRecord $record, array $query, Minn\Theme\TemplateIndex $index): bool#

Whether a record passes the list's filters (probe patch-security): wp_id matches only a stored one with that id, so a theme file (wp_id 0) never does; post_type keeps templates that declare that type; area keeps parts in that area. Each filter applies only where it means something.

$queryarray<string, mixed>
privatelookup(Minn\Http\Request $request, string $as): Minn\Http\Response#

The first template in the slug's hierarchy that exists, shown as the route's own type shows an item (the parts route searches templates too, as the reference's does); the plugins' templates count once plugins are loaded.

privatesingle(Minn\Http\Request $request, string $type, string $id): Minn\Http\Response#
privatesave(Minn\Http\Request $request, string $type, string $id): Minn\Http\Response#
privatecreate(Minn\Http\Request $request, string $type): Minn\Http\Response#

A new template from the collection (probe rest-template-create): its slug sanitised and made unique among the site's own copies of the type (-2, -3...). A slug only a theme file holds makes the site's copy of that file, blank but for what the body sends. Whatever status is asked for, it is published; the answer is in the request's context.

privatefields(array $body): array#

The fields a write sets, those the body sends.

$bodyarray<string, mixed>
@return array<string, string>
privatedelete(Minn\Http\Request $request, string $type, string $id): Minn\Http\Response#

Removing a template means removing the site's own copy of it. Without force that copy goes to the trash, which already hands the theme's file back; with force the row goes for good. A template that exists only as a theme file has nothing to remove.

privatetrashed(Minn\Theme\TemplateRecord $record): Minn\Theme\TemplateRecord#
privaterecord(string $type, string $id): Minn\Theme\TemplateRecord#
privatereadable(): Minn\Theme\TemplateIndex#

Reading the layout is an editor's business, not only an administrator's.

privaterequireWrite(): void#
privatetext(mixed $value): ?string#

A title or content field arrives either bare or as the raw member of its object.