<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[59073] trunk: Editor: Add plugin template registration API and improve theme overrides for plugin-registered templates</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/59073">59073</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/59073","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>noisysocks</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2024-09-20 02:05:50 +0000 (Fri, 20 Sep 2024)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Editor: Add plugin template registration API and improve theme overrides for plugin-registered templates
This commit introduces a new API to allow plugins to easily register block
templates with `wp_register_block_template()` and the
`WP_Block_Templates_Registry` class, addressing the complexity of hooking into
multiple filters. It also ensures plugin-registered templates overridden by
themes fall back to the plugin-provided title and description when the theme
doesn't define them.
See https://github.com/WordPress/gutenberg/pull/61577.
See https://github.com/WordPress/gutenberg/pull/64610.
Fixes <a href="https://core.trac.wordpress.org/ticket/61804">#61804</a>.
Props aljullu, peterwilsoncc, antonvlasenko, azaozz, youknowriad, noisysocks.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesblocktemplateutilsphp">trunk/src/wp-includes/block-template-utils.php</a></li>
<li><a href="#trunksrcwpincludesblocktemplatephp">trunk/src/wp-includes/block-template.php</a></li>
<li><a href="#trunksrcwpincludesclasswpblocktemplatephp">trunk/src/wp-includes/class-wp-block-template.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswpresttemplatescontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php</a></li>
<li><a href="#trunksrcwpsettingsphp">trunk/src/wp-settings.php</a></li>
<li><a href="#trunktestsphpunittestsblocktemplatephp">trunk/tests/phpunit/tests/block-template.php</a></li>
<li><a href="#trunktestsphpunittestsrestapiwpRestTemplateAutosavesControllerphp">trunk/tests/phpunit/tests/rest-api/wpRestTemplateAutosavesController.php</a></li>
<li><a href="#trunktestsphpunittestsrestapiwpRestTemplateRevisionsControllerphp">trunk/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php</a></li>
<li><a href="#trunktestsphpunittestsrestapiwpRestTemplatesControllerphp">trunk/tests/phpunit/tests/rest-api/wpRestTemplatesController.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpblocktemplatesregistryphp">trunk/src/wp-includes/class-wp-block-templates-registry.php</a></li>
<li><a href="#trunktestsphpunittestsblocktemplatesWpBlockTemplatesRegistryphp">trunk/tests/phpunit/tests/block-templates/WpBlockTemplatesRegistry.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesblocktemplateutilsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/block-template-utils.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/block-template-utils.php 2024-09-20 01:53:52 UTC (rev 59072)
+++ trunk/src/wp-includes/block-template-utils.php 2024-09-20 02:05:50 UTC (rev 59073)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -592,6 +592,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $template->is_custom = true;
</span><span class="cx" style="display: block; padding: 0 10px"> $template->modified = null;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( 'wp_template' === $template_type ) {
+ $registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $template_file['slug'] );
+ if ( $registered_template ) {
+ $template->plugin = $registered_template->plugin;
+ $template->title = empty( $template->title ) || $template->title === $template->slug ? $registered_template->title : $template->title;
+ $template->description = empty( $template->description ) ? $registered_template->description : $template->description;
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( 'wp_template' === $template_type && isset( $default_template_types[ $template_file['slug'] ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $template->description = $default_template_types[ $template_file['slug'] ]['description'];
</span><span class="cx" style="display: block; padding: 0 10px"> $template->title = $default_template_types[ $template_file['slug'] ]['title'];
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1014,6 +1023,19 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( 'wp_template' === $post->post_type ) {
+ $registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $template->slug );
+ if ( $registered_template ) {
+ $template->plugin = $registered_template->plugin;
+ $template->origin =
+ 'theme' !== $template->origin && 'theme' !== $template->source ?
+ 'plugin' :
+ $template->origin;
+ $template->title = empty( $template->title ) || $template->title === $template->slug ? $registered_template->title : $template->title;
+ $template->description = empty( $template->description ) ? $registered_template->description : $template->description;
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $hooked_blocks = get_hooked_blocks();
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1157,6 +1179,23 @@
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $template_files as $template_file ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $query_result[] = _build_block_template_result_from_file( $template_file, $template_type );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ if ( 'wp_template' === $template_type ) {
+ // Add templates registered in the template registry. Filtering out the ones which have a theme file.
+ $registered_templates = WP_Block_Templates_Registry::get_instance()->get_by_query( $query );
+ $matching_registered_templates = array_filter(
+ $registered_templates,
+ function ( $registered_template ) use ( $template_files ) {
+ foreach ( $template_files as $template_file ) {
+ if ( $template_file['slug'] === $registered_template->slug ) {
+ return false;
+ }
+ }
+ return true;
+ }
+ );
+ $query_result = array_merge( $query_result, $matching_registered_templates );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1287,18 +1326,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> list( $theme, $slug ) = $parts;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( get_stylesheet() !== $theme ) {
- /** This filter is documented in wp-includes/block-template-utils.php */
- return apply_filters( 'get_block_file_template', null, $id, $template_type );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( get_stylesheet() === $theme ) {
+ $template_file = _get_block_template_file( $template_type, $slug );
+ if ( null !== $template_file ) {
+ $block_template = _build_block_template_result_from_file( $template_file, $template_type );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $template_file = _get_block_template_file( $template_type, $slug );
- if ( null === $template_file ) {
- /** This filter is documented in wp-includes/block-template-utils.php */
- return apply_filters( 'get_block_file_template', null, $id, $template_type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /** This filter is documented in wp-includes/block-template-utils.php */
+ return apply_filters( 'get_block_file_template', $block_template, $id, $template_type );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $block_template = _build_block_template_result_from_file( $template_file, $template_type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $block_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $slug );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filters the block template object after it has been (potentially) fetched from the theme file.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1665,12 +1703,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $content = get_comment_delimited_block_content(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $content = get_comment_delimited_block_content(
</ins><span class="cx" style="display: block; padding: 0 10px"> 'core/template-part',
</span><span class="cx" style="display: block; padding: 0 10px"> $attributes,
</span><span class="cx" style="display: block; padding: 0 10px"> $changes->post_content
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $content = apply_block_hooks_to_content( $content, $template, 'set_ignored_hooked_blocks_metadata' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $content = apply_block_hooks_to_content( $content, $template, 'set_ignored_hooked_blocks_metadata' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $changes->post_content = remove_serialized_parent_block( $content );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $wrapper_block_markup = extract_serialized_parent_block( $content );
</span></span></pre></div>
<a id="trunksrcwpincludesblocktemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/block-template.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/block-template.php 2024-09-20 01:53:52 UTC (rev 59072)
+++ trunk/src/wp-includes/block-template.php 2024-09-20 02:05:50 UTC (rev 59073)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -358,3 +358,38 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_query->set( 'post_status', 'auto-draft' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Register a block template.
+ *
+ * @since 6.7.0
+ *
+ * @param string $template_name Template name in the form of `plugin_uri//template_name`.
+ * @param array|string $args {
+ * @type string $title Optional. Title of the template as it will be shown in the Site Editor
+ * and other UI elements.
+ * @type string $description Optional. Description of the template as it will be shown in the Site
+ * Editor.
+ * @type string $content Optional. Default content of the template that will be used when the
+ * template is rendered or edited in the editor.
+ * @type string[] $post_types Optional. Array of post types to which the template should be available.
+ * @type string $plugin Optional. Slug of the plugin that registers the template.
+ * }
+ * @return WP_Block_Template|WP_Error The registered template object on success, WP_Error object on failure.
+ */
+function wp_register_block_template( $template_name, $args = array() ) {
+ return WP_Block_Templates_Registry::get_instance()->register( $template_name, $args );
+}
+
+/**
+ * Unregister a block template.
+ *
+ * @since 6.7.0
+ *
+ * @param string $template_name Template name in the form of `plugin_uri//template_name`.
+ * @return WP_Block_Template|WP_Error The unregistered template object on success, WP_Error object on failure or if the
+ * template doesn't exist.
+ */
+function wp_unregister_block_template( $template_name ) {
+ return WP_Block_Templates_Registry::get_instance()->unregister( $template_name );
+}
</ins></span></pre></div>
<a id="trunksrcwpincludesclasswpblocktemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-block-template.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-block-template.php 2024-09-20 01:53:52 UTC (rev 59072)
+++ trunk/src/wp-includes/class-wp-block-template.php 2024-09-20 02:05:50 UTC (rev 59073)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -132,6 +132,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public $author;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Plugin.
+ *
+ * @since 6.7.0
+ * @var string|null
+ */
+ public $plugin;
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Post types.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.9.0
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpblocktemplatesregistryphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/wp-includes/class-wp-block-templates-registry.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-block-templates-registry.php (rev 0)
+++ trunk/src/wp-includes/class-wp-block-templates-registry.php 2024-09-20 02:05:50 UTC (rev 59073)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,256 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Templates registry functions.
+ *
+ * @package WordPress
+ * @since 6.7.0
+ */
+
+/**
+ * Core class used for interacting with templates.
+ *
+ * @since 6.7.0
+ */
+final class WP_Block_Templates_Registry {
+ /**
+ * Registered templates, as `$name => $instance` pairs.
+ *
+ * @since 6.7.0
+ * @var WP_Block_Template[] $registered_block_templates Registered templates.
+ */
+ private $registered_templates = array();
+
+ /**
+ * Container for the main instance of the class.
+ *
+ * @since 6.7.0
+ * @var WP_Block_Templates_Registry|null
+ */
+ private static $instance = null;
+
+ /**
+ * Registers a template.
+ *
+ * @since 6.7.0
+ *
+ * @param string $template_name Template name including namespace.
+ * @param array $args Optional. Array of template arguments.
+ * @return WP_Block_Template|WP_Error The registered template on success, or WP_Error on failure.
+ */
+ public function register( $template_name, $args = array() ) {
+
+ $template = null;
+
+ $error_message = '';
+ $error_code = '';
+
+ if ( ! is_string( $template_name ) ) {
+ $error_message = __( 'Template names must be strings.' );
+ $error_code = 'template_name_no_string';
+ } elseif ( preg_match( '/[A-Z]+/', $template_name ) ) {
+ $error_message = __( 'Template names must not contain uppercase characters.' );
+ $error_code = 'template_name_no_uppercase';
+ } elseif ( ! preg_match( '/^[a-z0-9-]+\/\/[a-z0-9-]+$/', $template_name ) ) {
+ $error_message = __( 'Template names must contain a namespace prefix. Example: my-plugin//my-custom-template' );
+ $error_code = 'template_no_prefix';
+ } elseif ( $this->is_registered( $template_name ) ) {
+ /* translators: %s: Template name. */
+ $error_message = sprintf( __( 'Template "%s" is already registered.' ), $template_name );
+ $error_code = 'template_already_registered';
+ }
+
+ if ( $error_message ) {
+ _doing_it_wrong(
+ __METHOD__,
+ $error_message,
+ '6.7.0'
+ );
+ return new WP_Error( $error_code, $error_message );
+ }
+
+ if ( ! $template ) {
+ $theme_name = get_stylesheet();
+ list( $plugin, $slug ) = explode( '//', $template_name );
+ $default_template_types = get_default_block_template_types();
+
+ $template = new WP_Block_Template();
+ $template->id = $theme_name . '//' . $slug;
+ $template->theme = $theme_name;
+ $template->plugin = $plugin;
+ $template->author = null;
+ $template->content = isset( $args['content'] ) ? $args['content'] : '';
+ $template->source = 'plugin';
+ $template->slug = $slug;
+ $template->type = 'wp_template';
+ $template->title = isset( $args['title'] ) ? $args['title'] : $template_name;
+ $template->description = isset( $args['description'] ) ? $args['description'] : '';
+ $template->status = 'publish';
+ $template->origin = 'plugin';
+ $template->is_custom = ! isset( $default_template_types[ $template_name ] );
+ $template->post_types = isset( $args['post_types'] ) ? $args['post_types'] : array();
+ }
+
+ $this->registered_templates[ $template_name ] = $template;
+
+ return $template;
+ }
+
+ /**
+ * Retrieves all registered templates.
+ *
+ * @since 6.7.0
+ *
+ * @return WP_Block_Template[] Associative array of `$template_name => $template` pairs.
+ */
+ public function get_all_registered() {
+ return $this->registered_templates;
+ }
+
+ /**
+ * Retrieves a registered template by its name.
+ *
+ * @since 6.7.0
+ *
+ * @param string $template_name Template name including namespace.
+ * @return WP_Block_Template|null The registered template, or null if it is not registered.
+ */
+ public function get_registered( $template_name ) {
+ if ( ! $this->is_registered( $template_name ) ) {
+ return null;
+ }
+
+ return $this->registered_templates[ $template_name ];
+ }
+
+ /**
+ * Retrieves a registered template by its slug.
+ *
+ * @since 6.7.0
+ *
+ * @param string $template_slug Slug of the template.
+ * @return WP_Block_Template|null The registered template, or null if it is not registered.
+ */
+ public function get_by_slug( $template_slug ) {
+ $all_templates = $this->get_all_registered();
+
+ if ( ! $all_templates ) {
+ return null;
+ }
+
+ foreach ( $all_templates as $template ) {
+ if ( $template->slug === $template_slug ) {
+ return $template;
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Retrieves registered templates matching a query.
+ *
+ * @since 6.7.0
+ *
+ * @param array $query {
+ * Arguments to retrieve templates. Optional, empty by default.
+ *
+ * @type string[] $slug__in List of slugs to include.
+ * @type string[] $slug__not_in List of slugs to skip.
+ * @type string $post_type Post type to get the templates for.
+ * }
+ * @return WP_Block_Template[] Associative array of `$template_name => $template` pairs.
+ */
+ public function get_by_query( $query = array() ) {
+ $all_templates = $this->get_all_registered();
+
+ if ( ! $all_templates ) {
+ return array();
+ }
+
+ $query = wp_parse_args(
+ $query,
+ array(
+ 'slug__in' => array(),
+ 'slug__not_in' => array(),
+ 'post_type' => '',
+ )
+ );
+ $slugs_to_include = $query['slug__in'];
+ $slugs_to_skip = $query['slug__not_in'];
+ $post_type = $query['post_type'];
+
+ $matching_templates = array();
+ foreach ( $all_templates as $template_name => $template ) {
+ if ( $slugs_to_include && ! in_array( $template->slug, $slugs_to_include, true ) ) {
+ continue;
+ }
+
+ if ( $slugs_to_skip && in_array( $template->slug, $slugs_to_skip, true ) ) {
+ continue;
+ }
+
+ if ( $post_type && ! in_array( $post_type, $template->post_types, true ) ) {
+ continue;
+ }
+
+ $matching_templates[ $template_name ] = $template;
+ }
+
+ return $matching_templates;
+ }
+
+ /**
+ * Checks if a template is registered.
+ *
+ * @since 6.7.0
+ *
+ * @param string $template_name Template name.
+ * @return bool True if the template is registered, false otherwise.
+ */
+ public function is_registered( $template_name ) {
+ return isset( $this->registered_templates[ $template_name ] );
+ }
+
+ /**
+ * Unregisters a template.
+ *
+ * @since 6.7.0
+ *
+ * @param string $template_name Template name including namespace.
+ * @return WP_Block_Template|WP_Error The unregistered template on success, or WP_Error on failure.
+ */
+ public function unregister( $template_name ) {
+ if ( ! $this->is_registered( $template_name ) ) {
+ _doing_it_wrong(
+ __METHOD__,
+ /* translators: %s: Template name. */
+ sprintf( __( 'Template "%s" is not registered.' ), $template_name ),
+ '6.7.0'
+ );
+ /* translators: %s: Template name. */
+ return new WP_Error( 'template_not_registered', __( 'Template "%s" is not registered.' ) );
+ }
+
+ $unregistered_template = $this->registered_templates[ $template_name ];
+ unset( $this->registered_templates[ $template_name ] );
+
+ return $unregistered_template;
+ }
+
+ /**
+ * Utility method to retrieve the main instance of the class.
+ *
+ * The instance will be created if it does not exist yet.
+ *
+ * @since 6.7.0
+ *
+ * @return WP_Block_Templates_Registry The main instance.
+ */
+ public static function get_instance() {
+ if ( null === self::$instance ) {
+ self::$instance = new self();
+ }
+
+ return self::$instance;
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/src/wp-includes/class-wp-block-templates-registry.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunksrcwpincludesrestapiendpointsclasswpresttemplatescontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php 2024-09-20 01:53:52 UTC (rev 59072)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php 2024-09-20 02:05:50 UTC (rev 59073)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -326,7 +326,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return WP_REST_Response|WP_Error
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function get_item( $request ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $request['source'] ) && 'theme' === $request['source'] ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( isset( $request['source'] ) && ( 'theme' === $request['source'] || 'plugin' === $request['source'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $template = get_block_file_template( $request['id'], $this->post_type );
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> $template = get_block_template( $request['id'], $this->post_type );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -776,6 +776,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $data['original_source'] = self::get_wp_templates_original_source_field( $template );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'plugin', $fields ) ) {
+ $registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $template->slug );
+ if ( $registered_template ) {
+ $data['plugin'] = $registered_template->plugin;
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
</span><span class="cx" style="display: block; padding: 0 10px"> $data = $this->add_additional_fields_to_object( $data, $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $data = $this->filter_response_by_context( $data, $context );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -831,7 +838,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Added by plugin.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $template_object->has_theme_file && 'plugin' === $template_object->origin ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( 'plugin' === $template_object->origin ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return 'plugin';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -865,9 +872,41 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $theme_name = wp_get_theme( $template_object->theme )->get( 'Name' );
</span><span class="cx" style="display: block; padding: 0 10px"> return empty( $theme_name ) ? $template_object->theme : $theme_name;
</span><span class="cx" style="display: block; padding: 0 10px"> case 'plugin':
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $plugins = get_plugins();
- $plugin = $plugins[ plugin_basename( sanitize_text_field( $template_object->theme . '.php' ) ) ];
- return empty( $plugin['Name'] ) ? $template_object->theme : $plugin['Name'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! function_exists( 'get_plugins' ) || ! function_exists( 'get_plugin_data' ) ) {
+ require_once ABSPATH . 'wp-admin/includes/plugin.php';
+ }
+ if ( isset( $template_object->plugin ) ) {
+ $plugins = wp_get_active_and_valid_plugins();
+
+ foreach ( $plugins as $plugin_file ) {
+ $plugin_basename = plugin_basename( $plugin_file );
+ // Split basename by '/' to get the plugin slug.
+ list( $plugin_slug, ) = explode( '/', $plugin_basename );
+
+ if ( $plugin_slug === $template_object->plugin ) {
+ $plugin_data = get_plugin_data( $plugin_file );
+
+ if ( ! empty( $plugin_data['Name'] ) ) {
+ return $plugin_data['Name'];
+ }
+
+ break;
+ }
+ }
+ }
+
+ /*
+ * Fall back to the theme name if the plugin is not defined. That's needed to keep backwards
+ * compatibility with templates that were registered before the plugin attribute was added.
+ */
+ $plugins = get_plugins();
+ $plugin_basename = plugin_basename( sanitize_text_field( $template_object->theme . '.php' ) );
+ if ( isset( $plugins[ $plugin_basename ] ) && isset( $plugins[ $plugin_basename ]['Name'] ) ) {
+ return $plugins[ $plugin_basename ]['Name'];
+ }
+ return isset( $template_object->plugin ) ?
+ $template_object->plugin :
+ $template_object->theme;
</ins><span class="cx" style="display: block; padding: 0 10px"> case 'site':
</span><span class="cx" style="display: block; padding: 0 10px"> return get_bloginfo( 'name' );
</span><span class="cx" style="display: block; padding: 0 10px"> case 'user':
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1134,6 +1173,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'context' => array( 'embed', 'view', 'edit' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'readonly' => true,
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $schema['properties']['plugin'] = array(
+ 'type' => 'string',
+ 'description' => __( 'Plugin that registered the template.' ),
+ 'readonly' => true,
+ 'context' => array( 'view', 'edit', 'embed' ),
+ );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( 'wp_template_part' === $this->post_type ) {
</span></span></pre></div>
<a id="trunksrcwpsettingsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-settings.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-settings.php 2024-09-20 01:53:52 UTC (rev 59072)
+++ trunk/src/wp-settings.php 2024-09-20 02:05:50 UTC (rev 59073)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -192,6 +192,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/class-wp-duotone.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/global-styles-and-settings.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/class-wp-block-template.php';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require ABSPATH . WPINC . '/class-wp-block-templates-registry.php';
</ins><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/block-template-utils.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/block-template.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/theme-templates.php';
</span></span></pre></div>
<a id="trunktestsphpunittestsblocktemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/block-template.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/block-template.php 2024-09-20 01:53:52 UTC (rev 59072)
+++ trunk/tests/phpunit/tests/block-template.php 2024-09-20 02:05:50 UTC (rev 59073)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -434,6 +434,47 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Tests that get_block_templates() returns plugin-registered templates.
+ *
+ * @ticket 61804
+ *
+ * @covers ::get_block_templates
+ */
+ public function test_get_block_templates_from_registry() {
+ $template_name = 'test-plugin//test-template';
+
+ wp_register_block_template( $template_name );
+
+ $templates = get_block_templates();
+
+ $this->assertArrayHasKey( $template_name, $templates );
+
+ wp_unregister_block_template( $template_name );
+ }
+
+ /**
+ * Tests that get_block_template() returns plugin-registered templates.
+ *
+ * @ticket 61804
+ *
+ * @covers ::get_block_template
+ */
+ public function test_get_block_template_from_registry() {
+ $template_name = 'test-plugin//test-template';
+ $args = array(
+ 'title' => 'Test Template',
+ );
+
+ wp_register_block_template( $template_name, $args );
+
+ $template = get_block_template( 'block-theme//test-template' );
+
+ $this->assertSame( 'Test Template', $template->title );
+
+ wp_unregister_block_template( $template_name );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Registers a test block to log `in_the_loop()` results.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $in_the_loop_logs Array to log function results in. Passed by reference.
</span></span></pre></div>
<a id="trunktestsphpunittestsblocktemplatesWpBlockTemplatesRegistryphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/block-templates/WpBlockTemplatesRegistry.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/block-templates/WpBlockTemplatesRegistry.php (rev 0)
+++ trunk/tests/phpunit/tests/block-templates/WpBlockTemplatesRegistry.php 2024-09-20 02:05:50 UTC (rev 59073)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,270 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Test WP_Block_Templates_Registry class.
+ *
+ * @coversDefaultClass WP_Block_Templates_Registry
+ */
+class Tests_Block_Templates_wpBlockTemplatesRegistry extends WP_UnitTestCase {
+
+ /**
+ * @var WP_Block_Templates_Registry
+ */
+ protected static $registry;
+
+ public static function set_up_before_class() {
+ parent::set_up_before_class();
+
+ self::$registry = WP_Block_Templates_Registry::get_instance();
+ }
+
+ /**
+ * Tests that register() returns the registered template.
+ *
+ * @ticket 61804
+ *
+ * @covers ::register
+ */
+ public function test_register_template() {
+ // Register a valid template.
+ $template_name = 'test-plugin//test-template';
+ $template = self::$registry->register( $template_name );
+
+ $this->assertSame( $template->slug, 'test-template' );
+
+ self::$registry->unregister( $template_name );
+ }
+
+ /**
+ * Tests that register() returns an error if template name is not a string.
+ *
+ * @ticket 61804
+ *
+ * @covers ::register
+ */
+ public function test_register_template_invalid_name() {
+ // Try to register a template with invalid name (non-string).
+ $template_name = array( 'invalid-template-name' );
+
+ $this->setExpectedIncorrectUsage( 'WP_Block_Templates_Registry::register' );
+ $result = self::$registry->register( $template_name );
+
+ $this->assertWPError( $result, 'Template registration is expected to trigger an error.' );
+ $this->assertSame( 'template_name_no_string', $result->get_error_code(), 'Error code mismatch.' );
+ $this->assertSame( 'Template names must be strings.', $result->get_error_message(), 'Error message mismatch.' );
+ }
+
+ /**
+ * Tests that register() returns an error if template name contains
+ * uppercase characters.
+ *
+ * @ticket 61804
+ *
+ * @covers ::register
+ */
+ public function test_register_template_invalid_name_uppercase() {
+ // Try to register a template with uppercase characters in the name.
+ $template_name = 'test-plugin//Invalid-Template-Name';
+
+ $this->setExpectedIncorrectUsage( 'WP_Block_Templates_Registry::register' );
+ $result = self::$registry->register( $template_name );
+
+ $this->assertWPError( $result, 'Template registration is expected to trigger an error.' );
+ $this->assertSame( 'template_name_no_uppercase', $result->get_error_code(), 'Error code mismatch.' );
+ $this->assertSame( 'Template names must not contain uppercase characters.', $result->get_error_message(), 'Error message mismatch.' );
+ }
+
+ /**
+ * Tests that register() returns an error if template name has no prefix.
+ *
+ * @ticket 61804
+ *
+ * @covers ::register
+ */
+ public function test_register_template_no_prefix() {
+ // Try to register a template without a namespace.
+ $this->setExpectedIncorrectUsage( 'WP_Block_Templates_Registry::register' );
+ $result = self::$registry->register( 'template-no-plugin', array() );
+
+ $this->assertWPError( $result, 'Template registration is expected to trigger an error.' );
+ $this->assertSame( 'template_no_prefix', $result->get_error_code(), 'Error code mismatch.' );
+ $this->assertSame( 'Template names must contain a namespace prefix. Example: my-plugin//my-custom-template', $result->get_error_message(), 'Error message mismatch.' );
+ }
+
+ /**
+ * Tests that register() returns an error if template already exists.
+ *
+ * @ticket 61804
+ *
+ * @covers ::register
+ */
+ public function test_register_template_already_exists() {
+ // Register the template for the first time.
+ $template_name = 'test-plugin//duplicate-template';
+ self::$registry->register( $template_name );
+
+ // Try to register the same template again.
+ $this->setExpectedIncorrectUsage( 'WP_Block_Templates_Registry::register' );
+ $result = self::$registry->register( $template_name );
+
+ $this->assertWPError( $result, 'Template registration is expected to trigger an error.' );
+ $this->assertSame( 'template_already_registered', $result->get_error_code(), 'Error code mismatch.' );
+ $this->assertStringContainsString( 'Template "test-plugin//duplicate-template" is already registered.', $result->get_error_message(), 'Error message mismatch.' );
+
+ self::$registry->unregister( $template_name );
+ }
+
+ /**
+ * Tests that get_all_registered() returns all registered templates.
+ *
+ * @ticket 61804
+ *
+ * @covers ::get_all_registered
+ */
+ public function test_get_all_registered() {
+ $template_name_1 = 'test-plugin//template-1';
+ $template_name_2 = 'test-plugin//template-2';
+ self::$registry->register( $template_name_1 );
+ self::$registry->register( $template_name_2 );
+
+ $all_templates = self::$registry->get_all_registered();
+
+ $this->assertIsArray( $all_templates, 'Registered templates should be an array.' );
+ $this->assertCount( 2, $all_templates, 'Registered templates should contain 2 items.' );
+ $this->assertArrayHasKey( 'test-plugin//template-1', $all_templates, 'Registered templates should contain "test-plugin//template-1".' );
+ $this->assertArrayHasKey( 'test-plugin//template-2', $all_templates, 'Registered templates should contain "test-plugin//template-2".' );
+
+ self::$registry->unregister( $template_name_1 );
+ self::$registry->unregister( $template_name_2 );
+ }
+
+ /**
+ * Tests that get_registered() returns the correct registered template.
+ *
+ * @ticket 61804
+ *
+ * @covers ::get_registered
+ */
+ public function test_get_registered() {
+ $template_name = 'test-plugin//registered-template';
+ $args = array(
+ 'content' => 'Template content',
+ 'title' => 'Registered Template',
+ 'description' => 'Description of registered template',
+ 'post_types' => array( 'post', 'page' ),
+ );
+ self::$registry->register( $template_name, $args );
+
+ $registered_template = self::$registry->get_registered( $template_name );
+
+ $this->assertSame( 'default', $registered_template->theme, 'Template theme mismatch.' );
+ $this->assertSame( 'registered-template', $registered_template->slug, 'Template slug mismatch.' );
+ $this->assertSame( 'default//registered-template', $registered_template->id, 'Template ID mismatch.' );
+ $this->assertSame( 'Registered Template', $registered_template->title, 'Template title mismatch.' );
+ $this->assertSame( 'Template content', $registered_template->content, 'Template content mismatch.' );
+ $this->assertSame( 'Description of registered template', $registered_template->description, 'Template description mismatch.' );
+ $this->assertSame( 'plugin', $registered_template->source, "Template source should be 'plugin'." );
+ $this->assertSame( 'plugin', $registered_template->origin, "Template origin should be 'plugin'." );
+ $this->assertSameSets( array( 'post', 'page' ), $registered_template->post_types, 'Template post types mismatch.' );
+ $this->assertSame( 'test-plugin', $registered_template->plugin, 'Plugin name mismatch.' );
+
+ self::$registry->unregister( $template_name );
+ }
+
+ /**
+ * Tests that get_by_slug() returns the correct template by slug.
+ *
+ * @ticket 61804
+ *
+ * @covers ::get_by_slug
+ */
+ public function test_get_by_slug() {
+ $slug = 'slug-template';
+ $template_name = 'test-plugin//' . $slug;
+ $args = array(
+ 'content' => 'Template content',
+ 'title' => 'Slug Template',
+ );
+ self::$registry->register( $template_name, $args );
+
+ $registered_template = self::$registry->get_by_slug( $slug );
+
+ $this->assertNotNull( $registered_template, 'Registered template should not be null.' );
+ $this->assertSame( $slug, $registered_template->slug, 'Template slug mismatch.' );
+
+ self::$registry->unregister( $template_name );
+ }
+
+ /**
+ * Tests that get_by_query() returns the correct templates based on the query.
+ *
+ * @ticket 61804
+ *
+ * @covers ::get_by_query
+ */
+ public function test_get_by_query() {
+ $template_name_1 = 'test-plugin//query-template-1';
+ $template_name_2 = 'test-plugin//query-template-2';
+ $args_1 = array(
+ 'content' => 'Template content 1',
+ 'title' => 'Query Template 1',
+ );
+ $args_2 = array(
+ 'content' => 'Template content 2',
+ 'title' => 'Query Template 2',
+ );
+ self::$registry->register( $template_name_1, $args_1 );
+ self::$registry->register( $template_name_2, $args_2 );
+
+ $query = array(
+ 'slug__in' => array( 'query-template-1' ),
+ );
+ $results = self::$registry->get_by_query( $query );
+
+ $this->assertCount( 1, $results, 'Query result should contain 1 item.' );
+ $this->assertArrayHasKey( $template_name_1, $results, 'Query result should contain "test-plugin//query-template-1".' );
+
+ self::$registry->unregister( $template_name_1 );
+ self::$registry->unregister( $template_name_2 );
+ }
+
+ /**
+ * Tests that is_registered() correctly identifies registered templates.
+ *
+ * @ticket 61804
+ *
+ * @covers ::is_registered
+ */
+ public function test_is_registered() {
+ $template_name = 'test-plugin//is-registered-template';
+ $args = array(
+ 'content' => 'Template content',
+ 'title' => 'Is Registered Template',
+ );
+ self::$registry->register( $template_name, $args );
+
+ $this->assertTrue( self::$registry->is_registered( $template_name ) );
+
+ self::$registry->unregister( $template_name );
+ }
+
+ /**
+ * Tests that unregister() correctly unregisters a registered template.
+ *
+ * @ticket 61804
+ *
+ * @covers ::unregister
+ */
+ public function test_unregister() {
+ $template_name = 'test-plugin//unregister-template';
+ $args = array(
+ 'content' => 'Template content',
+ 'title' => 'Unregister Template',
+ );
+ $template = self::$registry->register( $template_name, $args );
+
+ $unregistered_template = self::$registry->unregister( $template_name );
+
+ $this->assertEquals( $template, $unregistered_template, 'Unregistered template should be the same as the registered one.' );
+ $this->assertFalse( self::$registry->is_registered( $template_name ), 'Template should not be registered after unregistering.' );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/block-templates/WpBlockTemplatesRegistry.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestsrestapiwpRestTemplateAutosavesControllerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/wpRestTemplateAutosavesController.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/wpRestTemplateAutosavesController.php 2024-09-20 01:53:52 UTC (rev 59072)
+++ trunk/tests/phpunit/tests/rest-api/wpRestTemplateAutosavesController.php 2024-09-20 02:05:50 UTC (rev 59073)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -310,7 +310,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $properties = $data['schema']['properties'];
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertCount( 18, $properties );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertCount( 19, $properties );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'id', $properties, 'ID key should exist in properties.' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'slug', $properties, 'Slug key should exist in properties.' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'theme', $properties, 'Theme key should exist in properties.' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -328,6 +328,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'parent', $properties, 'Parent key should exist in properties.' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'author_text', $properties, 'author_text key should exist in properties.' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'original_source', $properties, 'original_source key should exist in properties.' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertArrayHasKey( 'plugin', $properties, 'plugin key should exist in properties.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapiwpRestTemplateRevisionsControllerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php 2024-09-20 01:53:52 UTC (rev 59072)
+++ trunk/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php 2024-09-20 02:05:50 UTC (rev 59073)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -449,7 +449,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $data = $response->get_data();
</span><span class="cx" style="display: block; padding: 0 10px"> $properties = $data['schema']['properties'];
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertCount( 18, $properties );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertCount( 19, $properties );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'id', $properties, 'ID key should exist in properties.' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'slug', $properties, 'Slug key should exist in properties.' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'theme', $properties, 'Theme key should exist in properties.' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -467,6 +467,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'parent', $properties, 'Parent key should exist in properties.' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'author_text', $properties, 'author_text key should exist in properties.' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'original_source', $properties, 'original_source key should exist in properties.' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertArrayHasKey( 'plugin', $properties, 'plugin key should exist in properties.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapiwpRestTemplatesControllerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/wpRestTemplatesController.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/wpRestTemplatesController.php 2024-09-20 01:53:52 UTC (rev 59072)
+++ trunk/tests/phpunit/tests/rest-api/wpRestTemplatesController.php 2024-09-20 02:05:50 UTC (rev 59073)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -529,6 +529,53 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Tests that get_item() returns plugin-registered templates.
+ *
+ * @ticket 61804
+ *
+ * @covers WP_REST_Templates_Controller::get_item
+ */
+ public function test_get_item_from_registry() {
+ wp_set_current_user( self::$admin_id );
+
+ $template_name = 'test-plugin//test-template';
+ $args = array(
+ 'content' => 'Template content',
+ 'title' => 'Test Template',
+ 'description' => 'Description of test template',
+ 'post_types' => array( 'post', 'page' ),
+ );
+
+ wp_register_block_template( $template_name, $args );
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/templates/test-plugin//test-template' );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->assertNotWPError( $response, "Fetching a registered template shouldn't cause an error." );
+
+ $data = $response->get_data();
+
+ $this->assertSame( 'default//test-template', $data['id'], 'Template ID mismatch.' );
+ $this->assertSame( 'default', $data['theme'], 'Template theme mismatch.' );
+ $this->assertSame( 'Template content', $data['content']['raw'], 'Template content mismatch.' );
+ $this->assertSame( 'test-template', $data['slug'], 'Template slug mismatch.' );
+ $this->assertSame( 'plugin', $data['source'], "Template source should be 'plugin'." );
+ $this->assertSame( 'plugin', $data['origin'], "Template origin should be 'plugin'." );
+ $this->assertSame( 'test-plugin', $data['author_text'], 'Template author text mismatch.' );
+ $this->assertSame( 'Description of test template', $data['description'], 'Template description mismatch.' );
+ $this->assertSame( 'Test Template', $data['title']['rendered'], 'Template title mismatch.' );
+ $this->assertSame( 'test-plugin', $data['plugin'], 'Plugin name mismatch.' );
+
+ wp_unregister_block_template( $template_name );
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/templates/test-plugin//test-template' );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->assertNotWPError( $response, "Fetching an unregistered template shouldn't cause an error." );
+ $this->assertSame( 404, $response->get_status(), 'Fetching an unregistered template should return 404.' );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 54507
</span><span class="cx" style="display: block; padding: 0 10px"> * @dataProvider data_sanitize_template_id
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -863,7 +910,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $data = $response->get_data();
</span><span class="cx" style="display: block; padding: 0 10px"> $properties = $data['schema']['properties'];
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertCount( 17, $properties );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertCount( 18, $properties );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'id', $properties );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'description', $properties );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'slug', $properties );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -882,6 +929,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'modified', $properties );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'author_text', $properties );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'original_source', $properties );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertArrayHasKey( 'plugin', $properties );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> protected function find_and_normalize_template_by_id( $templates, $id ) {
</span></span></pre>
</div>
</div>
</body>
</html>