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.
Constants and properties
| private | CREATE_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). |
| private | LOOKUP = 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, ), ) | |
| private | LIST = 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. |
| private | READ_CAP = 'edit_posts' | |
| private | WRITE_CAP = 'edit_theme_options' | |
| private | REFUSAL = 'Sorry, you are not allowed to access the templates on this site.' | |
| private | readonly Minn\Theme\TemplateIndex $index | Promoted from the constructor. |
| private | readonly Minn\Theme\TemplateWriter $writer | Promoted from the constructor. |
| private | readonly Minn\Rest\TemplateObject $object | Promoted from the constructor. |
| private | readonly 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)# | |
| method | templates(Minn\Http\Request $request): Minn\Http\ResponseGET /wp/v2/templates (cap edit_posts)# | The templates list. |
| method | parts(Minn\Http\Request $request): Minn\Http\ResponseGET /wp/v2/template-parts (cap edit_posts)# | The template parts list. |
| method | lookupTemplate(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. |
| method | lookupPart(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. |
| method | template(Minn\Http\Request $request, string $id): Minn\Http\ResponseGET /wp/v2/templates/{id:([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+} (cap edit_posts)# | One template. |
| method | part(Minn\Http\Request $request, string $id): Minn\Http\ResponseGET /wp/v2/template-parts/{id:([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+} (cap edit_posts)# | One template part. |
| method | createTemplate(Minn\Http\Request $request): Minn\Http\ResponsePOST /wp/v2/templates (cap edit_posts + edit_theme_options)# | Creates a template. |
| method | createPart(Minn\Http\Request $request): Minn\Http\ResponsePOST /wp/v2/template-parts (cap edit_posts + edit_theme_options)# | Creates a template part. |
| method | saveTemplate(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. |
| method | savePart(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. |
| method | deleteTemplate(Minn\Http\Request $request, string $id): Minn\Http\ResponseDELETE /wp/v2/templates/{id:([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+} (cap edit_posts + edit_theme_options)# | Deletes a customised template. |
| method | deletePart(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.
| private | listing(Minn\Http\Request $request, string $type): Minn\Http\Response# | ||||
| private | listed(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.
| |||
| private | lookup(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. | |||
| private | single(Minn\Http\Request $request, string $type, string $id): Minn\Http\Response# | ||||
| private | save(Minn\Http\Request $request, string $type, string $id): Minn\Http\Response# | ||||
| private | create(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. | |||
| private | fields(array $body): array# | The fields a write sets, those the body sends.
@return array<string, string> | |||
| private | delete(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. | |||
| private | trashed(Minn\Theme\TemplateRecord $record): Minn\Theme\TemplateRecord# | ||||
| private | record(string $type, string $id): Minn\Theme\TemplateRecord# | ||||
| private | readable(): Minn\Theme\TemplateIndex# | Reading the layout is an editor's business, not only an administrator's. | |||
| private | requireWrite(): void# | ||||
| private | text(mixed $value): ?string# | A title or content field arrives either bare or as the raw member of its object. |
Nothing matches.