<!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>[52062] trunk: Editor: Add block theme infrastructure</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/52062">52062</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/52062","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>2021-11-08 23:09:53 +0000 (Mon, 08 Nov 2021)</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 block theme infrastructure
Adds the required infrastructure to render block-based themes. This is sourced
from the Gutenberg plugin.
Fixes <a href="https://core.trac.wordpress.org/ticket/54335">#54335</a>.
Props bernhard-reiter, youknowriad, ntsekouras, hellofromtonya.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesblocktemplateutilsphp">trunk/src/wp-includes/block-template-utils.php</a></li>
<li><a href="#trunksrcwpincludesblocksindexphp">trunk/src/wp-includes/blocks/index.php</a></li>
<li><a href="#trunksrcwpincludesblocksphp">trunk/src/wp-includes/blocks.php</a></li>
<li><a href="#trunksrcwpincludesclasswpblocktemplatephp">trunk/src/wp-includes/class-wp-block-template.php</a></li>
<li><a href="#trunksrcwpincludesclasswpthemejsonphp">trunk/src/wp-includes/class-wp-theme-json.php</a></li>
<li><a href="#trunksrcwpincludesdefaultfiltersphp">trunk/src/wp-includes/default-filters.php</a></li>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.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="#trunksrcwpincludestaxonomyphp">trunk/src/wp-includes/taxonomy.php</a></li>
<li><a href="#trunksrcwpincludesthemetemplatesphp">trunk/src/wp-includes/theme-templates.php</a></li>
<li><a href="#trunktestsphpunitincludesfunctionsphp">trunk/tests/phpunit/includes/functions.php</a></li>
<li><a href="#trunktestsphpunittestsblocktemplateutilsphp">trunk/tests/phpunit/tests/block-template-utils.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestschemasetupphp">trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php</a></li>
<li><a href="#trunktestsqunitfixtureswpapigeneratedjs">trunk/tests/qunit/fixtures/wp-api-generated.js</a></li>
<li><a href="#trunktoolswebpackblocksjs">trunk/tools/webpack/blocks.js</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li>trunk/src/wp-includes/blocks/template-part/</li>
<li><a href="#trunksrcwpincludesblockstemplatepartblockjson">trunk/src/wp-includes/blocks/template-part/block.json</a></li>
<li><a href="#trunksrcwpincludesblockstemplatepartphp">trunk/src/wp-includes/blocks/template-part.php</a></li>
<li>trunk/tests/phpunit/data/templates/</li>
<li><a href="#trunktestsphpunitdatatemplatestemplatehtml">trunk/tests/phpunit/data/templates/template.html</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 2021-11-08 23:05:50 UTC (rev 52061)
+++ trunk/src/wp-includes/block-template-utils.php 2021-11-08 23:09:53 UTC (rev 52062)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,23 +1,495 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Utilities used to fetch and create templates.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Utilities used to fetch and create templates and template parts.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @package WordPress
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.8.0
</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">+// Define constants for supported wp_template_part_area taxonomy.
+if ( ! defined( 'WP_TEMPLATE_PART_AREA_HEADER' ) ) {
+ define( 'WP_TEMPLATE_PART_AREA_HEADER', 'header' );
+}
+if ( ! defined( 'WP_TEMPLATE_PART_AREA_FOOTER' ) ) {
+ define( 'WP_TEMPLATE_PART_AREA_FOOTER', 'footer' );
+}
+if ( ! defined( 'WP_TEMPLATE_PART_AREA_SIDEBAR' ) ) {
+ define( 'WP_TEMPLATE_PART_AREA_SIDEBAR', 'sidebar' );
+}
+if ( ! defined( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED' ) ) {
+ define( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED', 'uncategorized' );
+}
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Returns a filtered list of allowed area values for template parts.
+ *
+ * @since 5.9.0
+ *
+ * @return array The supported template part area values.
+ */
+function get_allowed_block_template_part_areas() {
+ $default_area_definitions = array(
+ array(
+ 'area' => WP_TEMPLATE_PART_AREA_UNCATEGORIZED,
+ 'label' => __( 'General' ),
+ 'description' => __(
+ 'General templates often perform a specific role like displaying post content, and are not tied to any particular area.'
+ ),
+ 'icon' => 'layout',
+ 'area_tag' => 'div',
+ ),
+ array(
+ 'area' => WP_TEMPLATE_PART_AREA_HEADER,
+ 'label' => __( 'Header' ),
+ 'description' => __(
+ 'The Header template defines a page area that typically contains a title, logo, and main navigation.'
+ ),
+ 'icon' => 'header',
+ 'area_tag' => 'header',
+ ),
+ array(
+ 'area' => WP_TEMPLATE_PART_AREA_FOOTER,
+ 'label' => __( 'Footer' ),
+ 'description' => __(
+ 'The Footer template defines a page area that typically contains site credits, social links, or any other combination of blocks.'
+ ),
+ 'icon' => 'footer',
+ 'area_tag' => 'footer',
+ ),
+ );
+
+ /**
+ * Filters the list of allowed template part area values.
+ *
+ * @since 5.9.0
+ *
+ * @param array $default_areas An array of supported area objects.
+ */
+ return apply_filters( 'default_wp_template_part_areas', $default_area_definitions );
+}
+
+
+/**
+ * Returns a filtered list of default template types, containing their
+ * localized titles and descriptions.
+ *
+ * @since 5.9.0
+ *
+ * @return array The default template types.
+ */
+function get_default_block_template_types() {
+ $default_template_types = array(
+ 'index' => array(
+ 'title' => _x( 'Index', 'Template name' ),
+ 'description' => __( 'The default template used when no other template is available. This is a required template in WordPress.' ),
+ ),
+ 'home' => array(
+ 'title' => _x( 'Home', 'Template name' ),
+ 'description' => __( 'Template used for the main page that displays blog posts. This is the front page by default in WordPress. If a static front page is set, this is the template used for the page that contains the latest blog posts.' ),
+ ),
+ 'front-page' => array(
+ 'title' => _x( 'Front Page', 'Template name' ),
+ 'description' => __( 'Template used to render the front page of the site, whether it displays blog posts or a static page. The front page template takes precedence over the "Home" template.' ),
+ ),
+ 'singular' => array(
+ 'title' => _x( 'Singular', 'Template name' ),
+ 'description' => __( 'Template used for displaying single views of the content. This template is a fallback for the Single, Post, and Page templates, which take precedence when they exist.' ),
+ ),
+ 'single' => array(
+ 'title' => _x( 'Single Post', 'Template name' ),
+ 'description' => __( 'Template used to display a single blog post.' ),
+ ),
+ 'page' => array(
+ 'title' => _x( 'Page', 'Template name' ),
+ 'description' => __( 'Template used to display individual pages.' ),
+ ),
+ 'archive' => array(
+ 'title' => _x( 'Archive', 'Template name' ),
+ 'description' => __( 'The archive template displays multiple entries at once. It is used as a fallback for the Category, Author, and Date templates, which take precedence when they are available.' ),
+ ),
+ 'author' => array(
+ 'title' => _x( 'Author', 'Template name' ),
+ 'description' => __( 'Archive template used to display a list of posts from a single author.' ),
+ ),
+ 'category' => array(
+ 'title' => _x( 'Category', 'Template name' ),
+ 'description' => __( 'Archive template used to display a list of posts from the same category.' ),
+ ),
+ 'taxonomy' => array(
+ 'title' => _x( 'Taxonomy', 'Template name' ),
+ 'description' => __( 'Archive template used to display a list of posts from the same taxonomy.' ),
+ ),
+ 'date' => array(
+ 'title' => _x( 'Date', 'Template name' ),
+ 'description' => __( 'Archive template used to display a list of posts from a specific date.' ),
+ ),
+ 'tag' => array(
+ 'title' => _x( 'Tag', 'Template name' ),
+ 'description' => __( 'Archive template used to display a list of posts with a given tag.' ),
+ ),
+ 'attachment' => array(
+ 'title' => __( 'Media' ),
+ 'description' => __( 'Template used to display individual media items or attachments.' ),
+ ),
+ 'search' => array(
+ 'title' => _x( 'Search', 'Template name' ),
+ 'description' => __( 'Template used to display search results.' ),
+ ),
+ 'privacy-policy' => array(
+ 'title' => __( 'Privacy Policy' ),
+ 'description' => '',
+ ),
+ '404' => array(
+ 'title' => _x( '404', 'Template name' ),
+ 'description' => __( 'Template shown when no content is found.' ),
+ ),
+ );
+
+ /**
+ * Filters the list of template types.
+ *
+ * @since 5.9.0
+ *
+ * @param array $default_template_types An array of template types, formatted as [ slug => [ title, description ] ].
+ */
+ return apply_filters( 'default_template_types', $default_template_types );
+}
+
+/**
+ * Checks whether the input 'area' is a supported value.
+ * Returns the input if supported, otherwise returns the 'uncategorized' value.
+ *
+ * @access private
+ * @since 5.9.0
+ *
+ * @param string $type Template part area name.
+ *
+ * @return string Input if supported, else the uncategorized value.
+ */
+function _filter_block_template_part_area( $type ) {
+ $allowed_areas = array_map(
+ static function ( $item ) {
+ return $item['area'];
+ },
+ get_allowed_block_template_part_areas()
+ );
+ if ( in_array( $type, $allowed_areas, true ) ) {
+ return $type;
+ }
+
+ $warning_message = sprintf(
+ /* translators: %1$s: Template area type, %2$s: the uncategorized template area value. */
+ __( '"%1$s" is not a supported wp_template_part area value and has been added as "%2$s".' ),
+ $type,
+ WP_TEMPLATE_PART_AREA_UNCATEGORIZED
+ );
+ trigger_error( $warning_message, E_USER_NOTICE );
+ return WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
+}
+
+/**
+ * Finds all nested template part file paths in a theme's directory.
+ *
+ * @access private
+ * @since 5.9.0
+ *
+ * @param string $base_directory The theme's file path.
+ * @return array $path_list A list of paths to all template part files.
+ */
+function _get_block_templates_paths( $base_directory ) {
+ $path_list = array();
+ if ( file_exists( $base_directory ) ) {
+ $nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) );
+ $nested_html_files = new RegexIterator( $nested_files, '/^.+\.html$/i', RecursiveRegexIterator::GET_MATCH );
+ foreach ( $nested_html_files as $path => $file ) {
+ $path_list[] = $path;
+ }
+ }
+ return $path_list;
+}
+
+/**
+ * Retrieves the template file from the theme for a given slug.
+ *
+ * @access private
+ * @since 5.9.0
+ *
+ * @param string $template_type wp_template or wp_template_part.
+ * @param string $slug template slug.
+ *
+ * @return array|null Template.
+ */
+function _get_block_template_file( $template_type, $slug ) {
+ if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) {
+ return null;
+ }
+
+ $template_base_paths = array(
+ 'wp_template' => 'block-templates',
+ 'wp_template_part' => 'block-template-parts',
+ );
+ $themes = array(
+ get_stylesheet() => get_stylesheet_directory(),
+ get_template() => get_template_directory(),
+ );
+ foreach ( $themes as $theme_slug => $theme_dir ) {
+ $file_path = $theme_dir . '/' . $template_base_paths[ $template_type ] . '/' . $slug . '.html';
+ if ( file_exists( $file_path ) ) {
+ $new_template_item = array(
+ 'slug' => $slug,
+ 'path' => $file_path,
+ 'theme' => $theme_slug,
+ 'type' => $template_type,
+ );
+
+ if ( 'wp_template_part' === $template_type ) {
+ return _add_block_template_part_area_info( $new_template_item );
+ }
+
+ if ( 'wp_template' === $template_type ) {
+ return _add_block_template_info( $new_template_item );
+ }
+
+ return $new_template_item;
+ }
+ }
+
+ return null;
+}
+
+/**
+ * Retrieves the template files from the theme.
+ *
+ * @access private
+ * @since 5.9.0
+ *
+ * @param string $template_type wp_template or wp_template_part.
+ *
+ * @return array Template.
+ */
+function _get_block_templates_files( $template_type ) {
+ if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) {
+ return null;
+ }
+
+ $template_base_paths = array(
+ 'wp_template' => 'block-templates',
+ 'wp_template_part' => 'block-template-parts',
+ );
+ $themes = array(
+ get_stylesheet() => get_stylesheet_directory(),
+ get_template() => get_template_directory(),
+ );
+
+ $template_files = array();
+ foreach ( $themes as $theme_slug => $theme_dir ) {
+ $theme_template_files = _get_block_templates_paths( $theme_dir . '/' . $template_base_paths[ $template_type ] );
+ foreach ( $theme_template_files as $template_file ) {
+ $template_base_path = $template_base_paths[ $template_type ];
+ $template_slug = substr(
+ $template_file,
+ // Starting position of slug.
+ strpos( $template_file, $template_base_path . DIRECTORY_SEPARATOR ) + 1 + strlen( $template_base_path ),
+ // Subtract ending '.html'.
+ -5
+ );
+ $new_template_item = array(
+ 'slug' => $template_slug,
+ 'path' => $template_file,
+ 'theme' => $theme_slug,
+ 'type' => $template_type,
+ );
+
+ if ( 'wp_template_part' === $template_type ) {
+ $template_files[] = _add_block_template_part_area_info( $new_template_item );
+ }
+
+ if ( 'wp_template' === $template_type ) {
+ $template_files[] = _add_block_template_info( $new_template_item );
+ }
+ }
+ }
+
+ return $template_files;
+}
+
+/**
+ * Attempts to add custom template information to the template item.
+ *
+ * @access private
+ * @since 5.9.0
+ *
+ * @param array $template_item Template to add information to (requires 'slug' field).
+ * @return array Template
+ */
+function _add_block_template_info( $template_item ) {
+ if ( ! WP_Theme_JSON_Resolver::theme_has_support() ) {
+ return $template_item;
+ }
+
+ $theme_data = WP_Theme_JSON_Resolver::get_theme_data()->get_custom_templates();
+ if ( isset( $theme_data[ $template_item['slug'] ] ) ) {
+ $template_item['title'] = $theme_data[ $template_item['slug'] ]['title'];
+ $template_item['postTypes'] = $theme_data[ $template_item['slug'] ]['postTypes'];
+ }
+
+ return $template_item;
+}
+
+/**
+ * Attempts to add the template part's area information to the input template.
+ *
+ * @access private
+ * @since 5.9.0
+ *
+ * @param array $template_info Template to add information to (requires 'type' and 'slug' fields).
+ *
+ * @return array Template.
+ */
+function _add_block_template_part_area_info( $template_info ) {
+ if ( WP_Theme_JSON_Resolver::theme_has_support() ) {
+ $theme_data = WP_Theme_JSON_Resolver::get_theme_data()->get_template_parts();
+ }
+
+ if ( isset( $theme_data[ $template_info['slug'] ]['area'] ) ) {
+ $template_info['title'] = $theme_data[ $template_info['slug'] ]['title'];
+ $template_info['area'] = _filter_block_template_part_area( $theme_data[ $template_info['slug'] ]['area'] );
+ } else {
+ $template_info['area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
+ }
+
+ return $template_info;
+}
+
+/**
+ * Returns an array containing the references of
+ * the passed blocks and their inner blocks.
+ *
+ * @access private
+ * @since 5.9.0
+ *
+ * @param array $blocks array of blocks.
+ *
+ * @return array block references to the passed blocks and their inner blocks.
+ */
+function _flatten_blocks( &$blocks ) {
+ $all_blocks = array();
+ $queue = array();
+ foreach ( $blocks as &$block ) {
+ $queue[] = &$block;
+ }
+
+ while ( count( $queue ) > 0 ) {
+ $block = &$queue[0];
+ array_shift( $queue );
+ $all_blocks[] = &$block;
+
+ if ( ! empty( $block['innerBlocks'] ) ) {
+ foreach ( $block['innerBlocks'] as &$inner_block ) {
+ $queue[] = &$inner_block;
+ }
+ }
+ }
+
+ return $all_blocks;
+}
+
+/**
+ * Parses wp_template content and injects the current theme's
+ * stylesheet as a theme attribute into each wp_template_part
+ *
+ * @access private
+ * @since 5.9.0
+ *
+ * @param string $template_content serialized wp_template content.
+ *
+ * @return string Updated wp_template content.
+ */
+function _inject_theme_attribute_in_block_template_content( $template_content ) {
+ $has_updated_content = false;
+ $new_content = '';
+ $template_blocks = parse_blocks( $template_content );
+
+ $blocks = _flatten_blocks( $template_blocks );
+ foreach ( $blocks as &$block ) {
+ if (
+ 'core/template-part' === $block['blockName'] &&
+ ! isset( $block['attrs']['theme'] )
+ ) {
+ $block['attrs']['theme'] = wp_get_theme()->get_stylesheet();
+ $has_updated_content = true;
+ }
+ }
+
+ if ( $has_updated_content ) {
+ foreach ( $template_blocks as &$block ) {
+ $new_content .= serialize_block( $block );
+ }
+
+ return $new_content;
+ }
+
+ return $template_content;
+}
+
+/**
+ * Build a unified template object based on a theme file.
+ *
+ * @access private
+ * @since 5.9.0
+ *
+ * @param array $template_file Theme file.
+ * @param array $template_type wp_template or wp_template_part.
+ *
+ * @return WP_Block_Template Template.
+ */
+function _build_block_template_result_from_file( $template_file, $template_type ) {
+ $default_template_types = get_default_block_template_types();
+ $template_content = file_get_contents( $template_file['path'] );
+ $theme = wp_get_theme()->get_stylesheet();
+
+ $template = new WP_Block_Template();
+ $template->id = $theme . '//' . $template_file['slug'];
+ $template->theme = $theme;
+ $template->content = _inject_theme_attribute_in_block_template_content( $template_content );
+ $template->slug = $template_file['slug'];
+ $template->source = 'theme';
+ $template->type = $template_type;
+ $template->title = ! empty( $template_file['title'] ) ? $template_file['title'] : $template_file['slug'];
+ $template->status = 'publish';
+ $template->has_theme_file = true;
+ $template->is_custom = true;
+
+ if ( 'wp_template' === $template_type && isset( $default_template_types[ $template_file['slug'] ] ) ) {
+ $template->description = $default_template_types[ $template_file['slug'] ]['description'];
+ $template->title = $default_template_types[ $template_file['slug'] ]['title'];
+ $template->is_custom = false;
+ }
+
+ if ( 'wp_template' === $template_type && isset( $template_file['postTypes'] ) ) {
+ $template->post_types = $template_file['postTypes'];
+ }
+
+ if ( 'wp_template_part' === $template_type && isset( $template_file['area'] ) ) {
+ $template->area = $template_file['area'];
+ }
+
+ return $template;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Build a unified template object based a post Object.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @access private
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 5.8.0
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.9.0
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_Post $post Template post.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @return WP_Block_Template|WP_Error Template.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function _build_template_result_from_post( $post ) {
- $terms = get_the_terms( $post, 'wp_theme' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function _build_block_template_result_from_post( $post ) {
+ $default_template_types = get_default_block_template_types();
+ $terms = get_the_terms( $post, 'wp_theme' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $terms ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $terms;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -27,7 +499,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) );
</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">- $theme = $terms[0]->name;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $theme = $terms[0]->name;
+ $has_theme_file = wp_get_theme()->get_stylesheet() === $theme &&
+ null !== _get_block_template_file( $post->post_type, $post->post_name );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $template = new WP_Block_Template();
</span><span class="cx" style="display: block; padding: 0 10px"> $template->wp_id = $post->ID;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -40,8 +514,20 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $template->description = $post->post_excerpt;
</span><span class="cx" style="display: block; padding: 0 10px"> $template->title = $post->post_title;
</span><span class="cx" style="display: block; padding: 0 10px"> $template->status = $post->post_status;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $template->has_theme_file = false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $template->has_theme_file = $has_theme_file;
+ $template->is_custom = true;
</ins><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 && isset( $default_template_types[ $template->slug ] ) ) {
+ $template->is_custom = false;
+ }
+
+ if ( 'wp_template_part' === $post->post_type ) {
+ $type_terms = get_the_terms( $post, 'wp_template_part_area' );
+ if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) {
+ $template->area = $type_terms[0]->name;
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> return $template;
</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">@@ -53,13 +539,40 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $query {
</span><span class="cx" style="display: block; padding: 0 10px"> * Optional. Arguments to retrieve templates.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @type array $slug__in List of slugs to include.
- * @type int $wp_id Post ID of customized template.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @type array $slug__in List of slugs to include.
+ * @type int $wp_id Post ID of customized template.
+ * @type string $area A 'wp_template_part_area' taxonomy value to filter by (for wp_template_part template type only).
+ * @type string $post_type Post type to get the templates for.
</ins><span class="cx" style="display: block; padding: 0 10px"> * }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $template_type Optional. The template type (post type). Default 'wp_template'.
- * @return WP_Block_Template[] Block template objects.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param array $template_type wp_template or wp_template_part.
+ *
+ * @return array Templates.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function get_block_templates( $query = array(), $template_type = 'wp_template' ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Filters the block templates array before the query takes place.
+ *
+ * Return a non-null value to bypass the WordPress queries.
+ *
+ * @since 5.9
+ *
+ * @param WP_Block_Template[]|null $block_templates Return an array of block templates to short-circuit the default query,
+ * or null to allow WP to run it's normal queries.
+ * @param array $query {
+ * Optional. Arguments to retrieve templates.
+ *
+ * @type array $slug__in List of slugs to include.
+ * @type int $wp_id Post ID of customized template.
+ * @type string $post_type Post type to get the templates for.
+ * }
+ * @param array $template_type wp_template or wp_template_part.
+ */
+ $templates = apply_filters( 'pre_get_block_templates', null, $query, $template_type );
+ if ( ! is_null( $templates ) ) {
+ return $templates;
+ }
+
+ $post_type = isset( $query['post_type'] ) ? $query['post_type'] : '';
</ins><span class="cx" style="display: block; padding: 0 10px"> $wp_query_args = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'post_status' => array( 'auto-draft', 'draft', 'publish' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'post_type' => $template_type,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -74,11 +587,20 @@
</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_part' === $template_type && isset( $query['area'] ) ) {
+ $wp_query_args['tax_query'][] = array(
+ 'taxonomy' => 'wp_template_part_area',
+ 'field' => 'name',
+ 'terms' => $query['area'],
+ );
+ $wp_query_args['tax_query']['relation'] = 'AND';
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $query['slug__in'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_query_args['post_name__in'] = $query['slug__in'];
</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">- // This is only needed for the regular templates CPT listing and editor.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // This is only needed for the regular templates/template parts CPT listing and editor.
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $query['wp_id'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_query_args['p'] = $query['wp_id'];
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -88,14 +610,66 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $template_query = new WP_Query( $wp_query_args );
</span><span class="cx" style="display: block; padding: 0 10px"> $query_result = array();
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $template_query->posts as $post ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $template = _build_template_result_from_post( $post );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $template = _build_block_template_result_from_post( $post );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! is_wp_error( $template ) ) {
- $query_result[] = $template;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( is_wp_error( $template ) ) {
+ continue;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ if ( $post_type && ! $template->is_custom ) {
+ continue;
+ }
+
+ $query_result[] = $template;
</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">- return $query_result;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! isset( $query['wp_id'] ) ) {
+ $template_files = _get_block_templates_files( $template_type );
+ foreach ( $template_files as $template_file ) {
+ $template = _build_block_template_result_from_file( $template_file, $template_type );
+
+ if ( $post_type && ! $template->is_custom ) {
+ continue;
+ }
+
+ if ( $post_type &&
+ isset( $template->post_types ) &&
+ ! in_array( $post_type, $template->post_types, true )
+ ) {
+ continue;
+ }
+
+ $is_not_custom = false === array_search(
+ wp_get_theme()->get_stylesheet() . '//' . $template_file['slug'],
+ array_column( $query_result, 'id' ),
+ true
+ );
+ $fits_slug_query =
+ ! isset( $query['slug__in'] ) || in_array( $template_file['slug'], $query['slug__in'], true );
+ $fits_area_query =
+ ! isset( $query['area'] ) || $template_file['area'] === $query['area'];
+ $should_include = $is_not_custom && $fits_slug_query && $fits_area_query;
+ if ( $should_include ) {
+ $query_result[] = $template;
+ }
+ }
+ }
+
+ /**
+ * Filters the array of queried block templates array after they've been fetched.
+ *
+ * @since 5.9
+ *
+ * @param WP_Block_Template[] $query_result Array of found block templates.
+ * @param array $query {
+ * Optional. Arguments to retrieve templates.
+ *
+ * @type array $slug__in List of slugs to include.
+ * @type int $wp_id Post ID of customized template.
+ * }
+ * @param array $template_type wp_template or wp_template_part.
+ */
+ return apply_filters( 'get_block_templates', $query_result, $query, $template_type );
</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">@@ -104,10 +678,29 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.8.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $id Template unique identifier (example: theme_slug//template_slug).
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $template_type Optional. The template type (post type). Default 'wp_template'.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param array $template_type Optional. Template type: `'wp_template'` or '`wp_template_part'`.
+ * Default `'wp_template'`.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return WP_Block_Template|null Template.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function get_block_template( $id, $template_type = 'wp_template' ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Filters the block templates array before the query takes place.
+ *
+ * Return a non-null value to bypass the WordPress queries.
+ *
+ * @since 5.9.0
+ *
+ * @param WP_Block_Template|null $block_template Return block template object to short-circuit the default query,
+ * or null to allow WP to run its normal queries.
+ * @param string $id Template unique identifier (example: theme_slug//template_slug).
+ * @param array $template_type Template type: `'wp_template'` or '`wp_template_part'`.
+ */
+ $block_template = apply_filters( 'pre_get_block_template', null, $id, $template_type );
+ if ( ! is_null( $block_template ) ) {
+ return $block_template;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $parts = explode( '//', $id, 2 );
</span><span class="cx" style="display: block; padding: 0 10px"> if ( count( $parts ) < 2 ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return null;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -131,7 +724,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $posts = $template_query->posts;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( count( $posts ) > 0 ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $template = _build_template_result_from_post( $posts[0] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $template = _build_block_template_result_from_post( $posts[0] );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! is_wp_error( $template ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $template;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -138,5 +731,109 @@
</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">- return null;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $block_template = get_block_file_template( $id, $template_type );
+
+ /**
+ * Filters the array of queried block templates array after they've been fetched.
+ *
+ * @since 5.9
+ *
+ * @param WP_Block_Template $block_template The found block template.
+ * @param string $id Template unique identifier (example: theme_slug//template_slug).
+ * @param array $template_type Template type: `'wp_template'` or '`wp_template_part'`.
+ */
+ return apply_filters( 'get_block_template', $block_template, $id, $template_type );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Retrieves a single unified template object using its id.
+ *
+ * @since 5.9.0
+ *
+ * @param string $id Template unique identifier (example: theme_slug//template_slug).
+ * @param array $template_type Optional. Template type: `'wp_template'` or '`wp_template_part'`.
+ * Default `'wp_template'`.
+ */
+function get_block_file_template( $id, $template_type = 'wp_template' ) {
+ /**
+ * Filters the block templates array before the query takes place.
+ *
+ * Return a non-null value to bypass the WordPress queries.
+ *
+ *
+ * @since 5.9.0
+ *
+ * @param WP_Block_Template|null $block_template Return block template object to short-circuit the default query,
+ * or null to allow WP to run its normal queries.
+ * @param string $id Template unique identifier (example: theme_slug//template_slug).
+ * @param array $template_type Template type: `'wp_template'` or '`wp_template_part'`.
+ */
+ $block_template = apply_filters( 'pre_get_block_file_template', null, $id, $template_type );
+ if ( ! is_null( $block_template ) ) {
+ return $block_template;
+ }
+
+ $parts = explode( '//', $id, 2 );
+ if ( count( $parts ) < 2 ) {
+ /** This filter is documented at the end of this function */
+ return apply_filters( 'get_block_file_template', null, $id, $template_type );
+ }
+ list( $theme, $slug ) = $parts;
+
+ if ( wp_get_theme()->get_stylesheet() !== $theme ) {
+ /** This filter is documented at the end of this function */
+ return apply_filters( 'get_block_file_template', null, $id, $template_type );
+ }
+
+ $template_file = _get_block_template_file( $template_type, $slug );
+ if ( null === $template_file ) {
+ /** This filter is documented at the end of this function */
+ return apply_filters( 'get_block_file_template', null, $id, $template_type );
+ }
+
+ $block_template = _build_block_template_result_from_file( $template_file, $template_type );
+
+ /**
+ * Filters the array of queried block templates array after they've been fetched.
+ *
+ * @since 5.9.0
+ *
+ * @param WP_Block_Template $block_template The found block template.
+ * @param string $id Template unique identifier (example: theme_slug//template_slug).
+ * @param array $template_type Template type: `'wp_template'` or '`wp_template_part'`.
+ */
+ return apply_filters( 'get_block_file_template', $block_template, $id, $template_type );
+}
+
+/**
+ * Print a template-part.
+ *
+ * @since 5.9.0
+ *
+ * @param string $part The template-part to print. Use "header" or "footer".
+ */
+function block_template_part( $part ) {
+ $template_part = get_block_template( get_stylesheet() . '//' . $part, 'wp_template_part' );
+ if ( ! $template_part || empty( $template_part->content ) ) {
+ return;
+ }
+ echo do_blocks( $template_part->content );
+}
+
+/**
+ * Print the header template-part.
+ *
+ * @since 5.9.0
+ */
+function block_header_area() {
+ block_template_part( 'header' );
+}
+
+/**
+ * Print the footer template-part.
+ *
+ * @since 5.9.0
+ */
+function block_footer_area() {
+ block_template_part( 'footer' );
+}
</ins></span></pre></div>
<a id="trunksrcwpincludesblocksindexphp"></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/blocks/index.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/blocks/index.php 2021-11-08 23:05:50 UTC (rev 52061)
+++ trunk/src/wp-includes/blocks/index.php 2021-11-08 23:09:53 UTC (rev 52062)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -37,6 +37,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/blocks/site-title.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/blocks/social-link.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/blocks/tag-cloud.php';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require ABSPATH . WPINC . '/blocks/template-part.php';
</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"> * Registers core block types using metadata files.
</span></span></pre></div>
<a id="trunksrcwpincludesblockstemplatepartblockjson"></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/blocks/template-part/block.json</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/blocks/template-part/block.json (rev 0)
+++ trunk/src/wp-includes/blocks/template-part/block.json 2021-11-08 23:09:53 UTC (rev 52062)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,35 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+{
+ "apiVersion": 2,
+ "name": "core/template-part",
+ "title": "Template Part",
+ "category": "theme",
+ "description": "Edit the different global regions of your site, like the header, footer, sidebar, or create your own.",
+ "textdomain": "default",
+ "attributes": {
+ "slug": {
+ "type": "string"
+ },
+ "theme": {
+ "type": "string"
+ },
+ "tagName": {
+ "type": "string"
+ },
+ "area": {
+ "type": "string"
+ }
+ },
+ "supports": {
+ "align": true,
+ "html": false,
+ "color": {
+ "gradients": true,
+ "link": true
+ },
+ "spacing": {
+ "padding": true
+ },
+ "__experimentalLayout": true
+ },
+ "editorStyle": "wp-block-template-part-editor"
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/src/wp-includes/blocks/template-part/block.json
</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="trunksrcwpincludesblockstemplatepartphp"></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/blocks/template-part.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/blocks/template-part.php (rev 0)
+++ trunk/src/wp-includes/blocks/template-part.php 2021-11-08 23:09:53 UTC (rev 52062)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,155 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Server-side rendering of the `core/template-part` block.
+ *
+ * @package WordPress
+ */
+
+/**
+ * Renders the `core/template-part` block on the server.
+ *
+ * @param array $attributes The block attributes.
+ *
+ * @return string The render.
+ */
+function render_block_core_template_part( $attributes ) {
+ static $seen_ids = array();
+
+ $template_part_id = null;
+ $content = null;
+ $area = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
+
+ if (
+ isset( $attributes['slug'] ) &&
+ isset( $attributes['theme'] ) &&
+ wp_get_theme()->get_stylesheet() === $attributes['theme']
+ ) {
+ $template_part_id = $attributes['theme'] . '//' . $attributes['slug'];
+ $template_part_query = new WP_Query(
+ array(
+ 'post_type' => 'wp_template_part',
+ 'post_status' => 'publish',
+ 'post_name__in' => array( $attributes['slug'] ),
+ 'tax_query' => array(
+ array(
+ 'taxonomy' => 'wp_theme',
+ 'field' => 'slug',
+ 'terms' => $attributes['theme'],
+ ),
+ ),
+ 'posts_per_page' => 1,
+ 'no_found_rows' => true,
+ )
+ );
+ $template_part_post = $template_part_query->have_posts() ? $template_part_query->next_post() : null;
+ if ( $template_part_post ) {
+ // A published post might already exist if this template part was customized elsewhere
+ // or if it's part of a customized template.
+ $content = $template_part_post->post_content;
+ $area_terms = get_the_terms( $template_part_post, 'wp_template_part_area' );
+ if ( ! is_wp_error( $area_terms ) && false !== $area_terms ) {
+ $area = $area_terms[0]->name;
+ }
+ } else {
+ // Else, if the template part was provided by the active theme,
+ // render the corresponding file content.
+ $template_part_file_path = get_theme_file_path( '/block-template-parts/' . $attributes['slug'] . '.html' );
+ if ( 0 === validate_file( $attributes['slug'] ) && file_exists( $template_part_file_path ) ) {
+ $content = _inject_theme_attribute_in_block_template_content( file_get_contents( $template_part_file_path ) );
+ }
+ }
+ }
+
+ if ( is_null( $content ) && is_user_logged_in() ) {
+ if ( ! isset( $attributes['slug'] ) ) {
+ // If there is no slug this is a placeholder and we dont want to return any message.
+ return;
+ }
+ return sprintf(
+ /* translators: %s: Template part slug. */
+ __( 'Template part has been deleted or is unavailable: %s' ),
+ $attributes['slug']
+ );
+ }
+
+ if ( isset( $seen_ids[ $template_part_id ] ) ) {
+ // WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent
+ // is set in `wp_debug_mode()`.
+ $is_debug = defined( 'WP_DEBUG' ) && WP_DEBUG &&
+ defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY;
+
+ return $is_debug ?
+ // translators: Visible only in the front end, this warning takes the place of a faulty block.
+ __( '[block rendering halted]' ) :
+ '';
+ }
+
+ // Run through the actions that are typically taken on the_content.
+ $seen_ids[ $template_part_id ] = true;
+ $content = do_blocks( $content );
+ unset( $seen_ids[ $template_part_id ] );
+ $content = wptexturize( $content );
+ $content = convert_smilies( $content );
+ $content = shortcode_unautop( $content );
+ $content = wp_filter_content_tags( $content );
+ $content = do_shortcode( $content );
+
+ // Handle embeds for block template parts.
+ global $wp_embed;
+ $content = $wp_embed->autoembed( $content );
+
+ if ( empty( $attributes['tagName'] ) ) {
+ $defined_areas = get_allowed_block_template_part_areas();
+ $area_tag = 'div';
+ foreach ( $defined_areas as $defined_area ) {
+ if ( $defined_area['area'] === $area && isset( $defined_area['area_tag'] ) ) {
+ $area_tag = $defined_area['area_tag'];
+ }
+ }
+ $html_tag = $area_tag;
+ } else {
+ $html_tag = esc_attr( $attributes['tagName'] );
+ }
+ $wrapper_attributes = get_block_wrapper_attributes();
+
+ return "<$html_tag $wrapper_attributes>" . str_replace( ']]>', ']]>', $content ) . "</$html_tag>";
+}
+
+/**
+ * Returns an array of variation objects for the template part block.
+ *
+ * @return array Array containing the block variation objects.
+ */
+function build_template_part_block_variations() {
+ $variations = array();
+ $defined_areas = get_allowed_block_template_part_areas();
+ foreach ( $defined_areas as $area ) {
+ if ( 'uncategorized' !== $area['area'] ) {
+ $variations[] = array(
+ 'name' => $area['area'],
+ 'title' => $area['label'],
+ 'description' => $area['description'],
+ 'attributes' => array(
+ 'area' => $area['area'],
+ ),
+ 'scope' => array( 'inserter' ),
+ 'icon' => $area['icon'],
+ );
+ }
+ }
+ return $variations;
+}
+
+/**
+ * Registers the `core/template-part` block on the server.
+ */
+function register_block_core_template_part() {
+ register_block_type_from_metadata(
+ __DIR__ . '/template-part',
+ array(
+ 'render_callback' => 'render_block_core_template_part',
+ 'variations' => build_template_part_block_variations(),
+ )
+ );
+}
+add_action( 'init', 'register_block_core_template_part' );
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/src/wp-includes/blocks/template-part.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="trunksrcwpincludesblocksphp"></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/blocks.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/blocks.php 2021-11-08 23:05:50 UTC (rev 52061)
+++ trunk/src/wp-includes/blocks.php 2021-11-08 23:09:53 UTC (rev 52062)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1154,6 +1154,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * It's used in QueryPaginationNext and QueryPaginationPrevious blocks.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.9.0
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param WP_Block $block Block instance.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param boolean $is_next Flag for hanlding `next/previous` blocks.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span></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 2021-11-08 23:05:50 UTC (rev 52061)
+++ trunk/src/wp-includes/class-wp-block-template.php 2021-11-08 23:09:53 UTC (rev 52062)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -100,4 +100,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @var boolean
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public $has_theme_file;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * Whether a template is a custom template.
+ *
+ * @since 5.9.0
+ *
+ * @var boolean
+ */
+ public $is_custom = true;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpthemejsonphp"></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-theme-json.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-theme-json.php 2021-11-08 23:05:50 UTC (rev 52061)
+++ trunk/src/wp-includes/class-wp-theme-json.php 2021-11-08 23:09:53 UTC (rev 52062)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -601,7 +601,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function get_custom_templates() {
</span><span class="cx" style="display: block; padding: 0 10px"> $custom_templates = array();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! isset( $this->theme_json['customTemplates'] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! isset( $this->theme_json['customTemplates'] ) || ! is_array( $this->theme_json['customTemplates'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return $custom_templates;
</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">@@ -625,7 +625,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function get_template_parts() {
</span><span class="cx" style="display: block; padding: 0 10px"> $template_parts = array();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! isset( $this->theme_json['templateParts'] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! isset( $this->theme_json['templateParts'] ) || ! is_array( $this->theme_json['templateParts'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return $template_parts;
</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="trunksrcwpincludesdefaultfiltersphp"></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/default-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/default-filters.php 2021-11-08 23:05:50 UTC (rev 52061)
+++ trunk/src/wp-includes/default-filters.php 2021-11-08 23:09:53 UTC (rev 52062)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -664,6 +664,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Block Templates CPT and Rendering
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'render_block_context', '_block_template_render_without_post_block_context' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'pre_wp_unique_post_slug', 'wp_filter_wp_template_unique_post_slug', 10, 5 );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+add_action( 'save_post_wp_template_part', 'wp_set_unique_slug_on_create_template_part' );
</ins><span class="cx" style="display: block; padding: 0 10px"> add_action( 'wp_footer', 'the_block_template_skip_link' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'setup_theme', 'wp_enable_block_templates' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcwpincludespostphp"></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/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/post.php 2021-11-08 23:05:50 UTC (rev 52061)
+++ trunk/src/wp-includes/post.php 2021-11-08 23:09:53 UTC (rev 52062)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -366,6 +366,64 @@
</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"> register_post_type(
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'wp_template_part',
+ array(
+ 'labels' => array(
+ 'name' => __( 'Template Parts' ),
+ 'singular_name' => __( 'Template Part' ),
+ 'add_new' => _x( 'Add New', 'Template Part' ),
+ 'add_new_item' => __( 'Add New Template Part' ),
+ 'new_item' => __( 'New Template Part' ),
+ 'edit_item' => __( 'Edit Template Part' ),
+ 'view_item' => __( 'View Template Part' ),
+ 'all_items' => __( 'All Template Parts' ),
+ 'search_items' => __( 'Search Template Parts' ),
+ 'parent_item_colon' => __( 'Parent Template Part:' ),
+ 'not_found' => __( 'No template parts found.' ),
+ 'not_found_in_trash' => __( 'No template parts found in Trash.' ),
+ 'archives' => __( 'Template part archives' ),
+ 'insert_into_item' => __( 'Insert into template part' ),
+ 'uploaded_to_this_item' => __( 'Uploaded to this template part' ),
+ 'filter_items_list' => __( 'Filter template parts list' ),
+ 'items_list_navigation' => __( 'Template parts list navigation' ),
+ 'items_list' => __( 'Template parts list' ),
+ ),
+ 'description' => __( 'Template parts to include in your templates.' ),
+ 'public' => false,
+ '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
+ 'has_archive' => false,
+ 'show_ui' => false,
+ 'show_in_menu' => false,
+ 'show_in_rest' => true,
+ 'rewrite' => false,
+ 'rest_base' => 'template-parts',
+ 'rest_controller_class' => 'WP_REST_Templates_Controller',
+ 'map_meta_cap' => true,
+ 'capabilities' => array(
+ 'create_posts' => 'edit_theme_options',
+ 'delete_posts' => 'edit_theme_options',
+ 'delete_others_posts' => 'edit_theme_options',
+ 'delete_private_posts' => 'edit_theme_options',
+ 'delete_published_posts' => 'edit_theme_options',
+ 'edit_posts' => 'edit_theme_options',
+ 'edit_others_posts' => 'edit_theme_options',
+ 'edit_private_posts' => 'edit_theme_options',
+ 'edit_published_posts' => 'edit_theme_options',
+ 'publish_posts' => 'edit_theme_options',
+ 'read' => 'edit_theme_options',
+ 'read_private_posts' => 'edit_theme_options',
+ ),
+ 'supports' => array(
+ 'title',
+ 'slug',
+ 'excerpt',
+ 'editor',
+ 'revisions',
+ ),
+ )
+ );
+
+ register_post_type(
</ins><span class="cx" style="display: block; padding: 0 10px"> 'wp_global_styles',
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'label' => __( 'Global Styles', 'gutenberg' ),
</span></span></pre></div>
<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 2021-11-08 23:05:50 UTC (rev 52061)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php 2021-11-08 23:09:53 UTC (rev 52062)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -156,6 +156,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $request['area'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $query['area'] = $request['area'];
</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 ( isset( $request['post_type'] ) ) {
+ $query['post_type'] = $request['post_type'];
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $templates = array();
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( get_block_templates( $query, $this->post_type ) as $template ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -187,7 +190,11 @@
</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">- $template = get_block_template( $request['id'], $this->post_type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( isset( $request['source'] ) && 'theme' === $request['source'] ) {
+ $template = get_block_file_template( $request['id'], $this->post_type );
+ } else {
+ $template = get_block_template( $request['id'], $this->post_type );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $template ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.' ), array( 'status' => 404 ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -222,6 +229,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.' ), array( 'status' => 404 ) );
</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 ( isset( $request['source'] ) && 'theme' === $request['source'] ) {
+ wp_delete_post( $template->wp_id, true );
+ return $this->prepare_item_for_response( get_block_file_template( $request['id'], $this->post_type ), $request );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $changes = $this->prepare_item_for_database( $request );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( 'custom' === $template->source ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -395,6 +407,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $changes->post_excerpt = $template->description;
</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_part' === $this->post_type ) {
+ if ( isset( $request['area'] ) ) {
+ $changes->tax_input['wp_template_part_area'] = _filter_block_template_part_area( $request['area'] );
+ } elseif ( null !== $template && 'custom' !== $template->source && $template->area ) {
+ $changes->tax_input['wp_template_part_area'] = _filter_block_template_part_area( $template->area );
+ } elseif ( ! $template->area ) {
+ $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> return $changes;
</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">@@ -502,16 +524,25 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Retrieves the query params for the posts collection.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.8.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.9.0 Added `'area'` and `'post_type'`.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @return array Collection parameters.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function get_collection_params() {
</span><span class="cx" style="display: block; padding: 0 10px"> return array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'context' => $this->get_context_param(),
- 'wp_id' => array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'context' => $this->get_context_param(),
+ 'wp_id' => array(
</ins><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Limit to the specified post id.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'integer',
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ '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',
+ ),
</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">@@ -519,6 +550,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Retrieves the block type' schema, conforming to JSON Schema.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.8.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.9.0 Added `'area'`.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @return array Item schema data.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -596,6 +628,14 @@
</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_part' === $this->post_type ) {
+ $schema['properties']['area'] = array(
+ 'description' => __( 'Where the template part is intended for use (header, footer, etc.)' ),
+ 'type' => 'string',
+ 'context' => array( 'embed', 'view', 'edit' ),
+ );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->schema = $schema;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return $this->add_additional_fields_schema( $this->schema );
</span></span></pre></div>
<a id="trunksrcwpincludestaxonomyphp"></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/taxonomy.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/taxonomy.php 2021-11-08 23:05:50 UTC (rev 52061)
+++ trunk/src/wp-includes/taxonomy.php 2021-11-08 23:09:53 UTC (rev 52062)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -18,6 +18,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * avoid registering rewrite rules before the {@see 'init'} action.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.8.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.9.0 Added `'wp_template_part_area'` taxonomy.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -175,7 +176,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> register_taxonomy(
</span><span class="cx" style="display: block; padding: 0 10px"> 'wp_theme',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- array( 'wp_template', 'wp_global_styles' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ array( 'wp_template', 'wp_template_part', 'wp_global_styles' ),
</ins><span class="cx" style="display: block; padding: 0 10px"> array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'public' => false,
</span><span class="cx" style="display: block; padding: 0 10px"> 'hierarchical' => false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -191,6 +192,25 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'show_in_rest' => false,
</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_taxonomy(
+ 'wp_template_part_area',
+ array( 'wp_template_part' ),
+ array(
+ 'public' => false,
+ 'hierarchical' => false,
+ 'labels' => array(
+ 'name' => __( 'Template Part Areas' ),
+ 'singular_name' => __( 'Template Part Area' ),
+ ),
+ 'query_var' => false,
+ 'rewrite' => false,
+ 'show_ui' => false,
+ '_builtin' => true,
+ 'show_in_nav_menus' => false,
+ 'show_in_rest' => false,
+ )
+ );
</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="trunksrcwpincludesthemetemplatesphp"></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/theme-templates.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/theme-templates.php 2021-11-08 23:05:50 UTC (rev 52061)
+++ trunk/src/wp-includes/theme-templates.php 2021-11-08 23:09:53 UTC (rev 52062)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,6 +1,36 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</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">+ * Sets a custom slug when creating auto-draft template parts.
+ *
+ * This is only needed for auto-drafts created by the regular WP editor.
+ * If this page is to be removed, this won't be necessary.
+ *
+ * @since 5.9.0
+ *
+ */
+function wp_set_unique_slug_on_create_template_part( $post_id ) {
+ $post = get_post( $post_id );
+ if ( 'auto-draft' !== $post->post_status ) {
+ return;
+ }
+
+ if ( ! $post->post_name ) {
+ wp_update_post(
+ array(
+ 'ID' => $post_id,
+ 'post_name' => 'custom_slug_' . uniqid(),
+ )
+ );
+ }
+
+ $terms = get_the_terms( $post_id, 'wp_theme' );
+ if ( ! is_array( $terms ) || ! count( $terms ) ) {
+ wp_set_post_terms( $post_id, wp_get_theme()->get_stylesheet(), 'wp_theme' );
+ }
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Generates a unique slug for templates.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @access private
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -14,7 +44,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return string The original, desired slug.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_filter_wp_template_unique_post_slug( $override_slug, $slug, $post_ID, $post_status, $post_type ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( 'wp_template' !== $post_type ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( 'wp_template' !== $post_type && 'wp_template_part' !== $post_type ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return $override_slug;
</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="trunktestsphpunitdatatemplatestemplatehtml"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/data/templates/template.html</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/data/templates/template.html (rev 0)
+++ trunk/tests/phpunit/data/templates/template.html 2021-11-08 23:09:53 UTC (rev 52062)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,3 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<!-- wp:paragraph -->
+<p>Just a paragraph</p>
+<!-- /wp:paragraph -->
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of file
</span><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/data/templates/template.html
</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="trunktestsphpunitincludesfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/includes/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/functions.php 2021-11-08 23:05:50 UTC (rev 52061)
+++ trunk/tests/phpunit/includes/functions.php 2021-11-08 23:09:53 UTC (rev 52062)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -337,5 +337,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> remove_action( 'init', 'register_block_core_tag_cloud' );
</span><span class="cx" style="display: block; padding: 0 10px"> remove_action( 'init', 'register_core_block_types_from_metadata' );
</span><span class="cx" style="display: block; padding: 0 10px"> remove_action( 'init', 'register_block_core_legacy_widget' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ remove_action( 'init', 'register_block_core_template_part' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> tests_add_filter( 'init', '_unhook_block_registration', 1000 );
</span></span></pre></div>
<a id="trunktestsphpunittestsblocktemplateutilsphp"></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-utils.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/block-template-utils.php 2021-11-08 23:05:50 UTC (rev 52061)
+++ trunk/tests/phpunit/tests/block-template-utils.php 2021-11-08 23:09:53 UTC (rev 52062)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6,12 +6,34 @@
</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">- * Tests for the Block Template Loader abstraction layer.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Tests for the Block Templates abstraction layer.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> class Block_Template_Utils_Test extends WP_UnitTestCase {
</span><span class="cx" style="display: block; padding: 0 10px"> private static $post;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private static $template_part_post;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> public static function wpSetUpBeforeClass() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // We may need a block theme.
+ // switch_theme( 'tt1-blocks' );
+
+ // Set up a template post corresponding to a different theme.
+ // We do this to ensure resolution and slug creation works as expected,
+ // even with another post of that same name present for another theme.
+ $args = array(
+ 'post_type' => 'wp_template',
+ 'post_name' => 'my_template',
+ 'post_title' => 'My Template',
+ 'post_content' => 'Content',
+ 'post_excerpt' => 'Description of my template',
+ 'tax_input' => array(
+ 'wp_theme' => array(
+ 'this-theme-should-not-resolve',
+ ),
+ ),
+ );
+ self::$post = self::factory()->post->create_and_get( $args );
+ wp_set_post_terms( self::$post->ID, 'this-theme-should-not-resolve', 'wp_theme' );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Set up template post.
</span><span class="cx" style="display: block; padding: 0 10px"> $args = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'post_type' => 'wp_template',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -27,6 +49,26 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> self::$post = self::factory()->post->create_and_get( $args );
</span><span class="cx" style="display: block; padding: 0 10px"> wp_set_post_terms( self::$post->ID, get_stylesheet(), 'wp_theme' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // Set up template part post.
+ $template_part_args = array(
+ 'post_type' => 'wp_template_part',
+ 'post_name' => 'my_template_part',
+ 'post_title' => 'My Template Part',
+ 'post_content' => 'Content',
+ 'post_excerpt' => 'Description of my template part',
+ 'tax_input' => array(
+ 'wp_theme' => array(
+ get_stylesheet(),
+ ),
+ 'wp_template_part_area' => array(
+ WP_TEMPLATE_PART_AREA_HEADER,
+ ),
+ ),
+ );
+ self::$template_part_post = self::factory()->post->create_and_get( $template_part_args );
+ wp_set_post_terms( self::$template_part_post->ID, WP_TEMPLATE_PART_AREA_HEADER, 'wp_template_part_area' );
+ wp_set_post_terms( self::$template_part_post->ID, get_stylesheet(), 'wp_theme' );
</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"> public static function wpTearDownAfterClass() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -34,38 +76,172 @@
</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"> public function test_build_template_result_from_post() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $template = _build_template_result_from_post(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $template = _build_block_template_result_from_post(
</ins><span class="cx" style="display: block; padding: 0 10px"> self::$post,
</span><span class="cx" style="display: block; padding: 0 10px"> 'wp_template'
</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"> $this->assertNotWPError( $template );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( get_stylesheet() . '//my_template', $template->id );
- $this->assertSame( get_stylesheet(), $template->theme );
- $this->assertSame( 'my_template', $template->slug );
- $this->assertSame( 'publish', $template->status );
- $this->assertSame( 'custom', $template->source );
- $this->assertSame( 'My Template', $template->title );
- $this->assertSame( 'Description of my template', $template->description );
- $this->assertSame( 'wp_template', $template->type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( get_stylesheet() . '//my_template', $template->id );
+ $this->assertEquals( get_stylesheet(), $template->theme );
+ $this->assertEquals( 'my_template', $template->slug );
+ $this->assertEquals( 'publish', $template->status );
+ $this->assertEquals( 'custom', $template->source );
+ $this->assertEquals( 'My Template', $template->title );
+ $this->assertEquals( 'Description of my template', $template->description );
+ $this->assertEquals( 'wp_template', $template->type );
+
+ // Test template parts.
+ $template_part = _build_block_template_result_from_post(
+ self::$template_part_post,
+ 'wp_template_part'
+ );
+ $this->assertNotWPError( $template_part );
+ $this->assertEquals( get_stylesheet() . '//my_template_part', $template_part->id );
+ $this->assertEquals( get_stylesheet(), $template_part->theme );
+ $this->assertEquals( 'my_template_part', $template_part->slug );
+ $this->assertEquals( 'publish', $template_part->status );
+ $this->assertEquals( 'custom', $template_part->source );
+ $this->assertEquals( 'My Template Part', $template_part->title );
+ $this->assertEquals( 'Description of my template part', $template_part->description );
+ $this->assertEquals( 'wp_template_part', $template_part->type );
+ $this->assertEquals( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area );
</ins><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">+ function test_build_block_template_result_from_file() {
+ $template = _build_block_template_result_from_file(
+ array(
+ 'slug' => 'single',
+ 'path' => __DIR__ . '/../data/templates/template.html',
+ ),
+ 'wp_template'
+ );
+
+ $this->assertEquals( get_stylesheet() . '//single', $template->id );
+ $this->assertEquals( get_stylesheet(), $template->theme );
+ $this->assertEquals( 'single', $template->slug );
+ $this->assertEquals( 'publish', $template->status );
+ $this->assertEquals( 'theme', $template->source );
+ $this->assertEquals( 'Single Post', $template->title );
+ $this->assertEquals( 'Template used to display a single blog post.', $template->description );
+ $this->assertEquals( 'wp_template', $template->type );
+
+ // Test template parts.
+ $template_part = _build_block_template_result_from_file(
+ array(
+ 'slug' => 'header',
+ 'path' => __DIR__ . '/../data/templates/template.html',
+ 'area' => WP_TEMPLATE_PART_AREA_HEADER,
+ ),
+ 'wp_template_part'
+ );
+ $this->assertEquals( get_stylesheet() . '//header', $template_part->id );
+ $this->assertEquals( get_stylesheet(), $template_part->theme );
+ $this->assertEquals( 'header', $template_part->slug );
+ $this->assertEquals( 'publish', $template_part->status );
+ $this->assertEquals( 'theme', $template_part->source );
+ $this->assertEquals( 'header', $template_part->title );
+ $this->assertEquals( '', $template_part->description );
+ $this->assertEquals( 'wp_template_part', $template_part->type );
+ $this->assertEquals( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area );
+ }
+
+ function test_inject_theme_attribute_in_block_template_content() {
+ $theme = get_stylesheet();
+ $content_without_theme_attribute = '<!-- wp:template-part {"slug":"header","align":"full", "tagName":"header","className":"site-header"} /-->';
+ $template_content = _inject_theme_attribute_in_block_template_content(
+ $content_without_theme_attribute,
+ $theme
+ );
+ $expected = sprintf(
+ '<!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header","theme":"%s"} /-->',
+ get_stylesheet()
+ );
+ $this->assertEquals( $expected, $template_content );
+
+ $content_without_theme_attribute_nested = '<!-- wp:group --><!-- wp:template-part {"slug":"header","align":"full", "tagName":"header","className":"site-header"} /--><!-- /wp:group -->';
+ $template_content = _inject_theme_attribute_in_block_template_content(
+ $content_without_theme_attribute_nested,
+ $theme
+ );
+ $expected = sprintf(
+ '<!-- wp:group --><!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header","theme":"%s"} /--><!-- /wp:group -->',
+ get_stylesheet()
+ );
+ $this->assertEquals( $expected, $template_content );
+
+ // Does not inject theme when there is an existing theme attribute.
+ $content_with_existing_theme_attribute = '<!-- wp:template-part {"slug":"header","theme":"fake-theme","align":"full", "tagName":"header","className":"site-header"} /-->';
+ $template_content = _inject_theme_attribute_in_block_template_content(
+ $content_with_existing_theme_attribute,
+ $theme
+ );
+ $this->assertEquals( $content_with_existing_theme_attribute, $template_content );
+
+ // Does not inject theme when there is no template part.
+ $content_with_no_template_part = '<!-- wp:post-content /-->';
+ $template_content = _inject_theme_attribute_in_block_template_content(
+ $content_with_no_template_part,
+ $theme
+ );
+ $this->assertEquals( $content_with_no_template_part, $template_content );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Should retrieve the template from the theme files.
+ */
+ function test_get_block_template_from_file() {
+ $this->markTestIncomplete();
+ // Requires switching to a block theme.
+ /* $id = get_stylesheet() . '//' . 'index';
+ $template = get_block_template( $id, 'wp_template' );
+ $this->assertEquals( $id, $template->id );
+ $this->assertEquals( get_stylesheet(), $template->theme );
+ $this->assertEquals( 'index', $template->slug );
+ $this->assertEquals( 'publish', $template->status );
+ $this->assertEquals( 'theme', $template->source );
+ $this->assertEquals( 'wp_template', $template->type );
+
+ // Test template parts.
+ $id = get_stylesheet() . '//' . 'header';
+ $template = get_block_template( $id, 'wp_template_part' );
+ $this->assertEquals( $id, $template->id );
+ $this->assertEquals( get_stylesheet(), $template->theme );
+ $this->assertEquals( 'header', $template->slug );
+ $this->assertEquals( 'publish', $template->status );
+ $this->assertEquals( 'theme', $template->source );
+ $this->assertEquals( 'wp_template_part', $template->type );
+ $this->assertEquals( WP_TEMPLATE_PART_AREA_HEADER, $template->area );
+ */
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Should retrieve the template from the CPT.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_get_block_template_from_post() {
</span><span class="cx" style="display: block; padding: 0 10px"> $id = get_stylesheet() . '//' . 'my_template';
</span><span class="cx" style="display: block; padding: 0 10px"> $template = get_block_template( $id, 'wp_template' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( $id, $template->id );
- $this->assertSame( get_stylesheet(), $template->theme );
- $this->assertSame( 'my_template', $template->slug );
- $this->assertSame( 'publish', $template->status );
- $this->assertSame( 'custom', $template->source );
- $this->assertSame( 'wp_template', $template->type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( $id, $template->id );
+ $this->assertEquals( get_stylesheet(), $template->theme );
+ $this->assertEquals( 'my_template', $template->slug );
+ $this->assertEquals( 'publish', $template->status );
+ $this->assertEquals( 'custom', $template->source );
+ $this->assertEquals( 'wp_template', $template->type );
+
+ // Test template parts.
+ $id = get_stylesheet() . '//' . 'my_template_part';
+ $template = get_block_template( $id, 'wp_template_part' );
+ $this->assertEquals( $id, $template->id );
+ $this->assertEquals( get_stylesheet(), $template->theme );
+ $this->assertEquals( 'my_template_part', $template->slug );
+ $this->assertEquals( 'publish', $template->status );
+ $this->assertEquals( 'custom', $template->source );
+ $this->assertEquals( 'wp_template_part', $template->type );
+ $this->assertEquals( WP_TEMPLATE_PART_AREA_HEADER, $template->area );
</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><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Should retrieve block templates.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Should retrieve block templates (file and CPT)
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_get_block_templates() {
</span><span class="cx" style="display: block; padding: 0 10px"> function get_template_ids( $templates ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -84,14 +260,53 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Avoid testing the entire array because the theme might add/remove templates.
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertContains( get_stylesheet() . '//' . 'my_template', $template_ids );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // The result might change in a block theme.
+ // $this->assertContains( get_stylesheet() . '//' . 'index', $template_ids );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Filter by slug.
</span><span class="cx" style="display: block; padding: 0 10px"> $templates = get_block_templates( array( 'slug__in' => array( 'my_template' ) ), 'wp_template' );
</span><span class="cx" style="display: block; padding: 0 10px"> $template_ids = get_template_ids( $templates );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( array( get_stylesheet() . '//' . 'my_template' ), $template_ids );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( array( get_stylesheet() . '//' . 'my_template' ), $template_ids );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Filter by CPT ID.
</span><span class="cx" style="display: block; padding: 0 10px"> $templates = get_block_templates( array( 'wp_id' => self::$post->ID ), 'wp_template' );
</span><span class="cx" style="display: block; padding: 0 10px"> $template_ids = get_template_ids( $templates );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( array( get_stylesheet() . '//' . 'my_template' ), $template_ids );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( array( get_stylesheet() . '//' . 'my_template' ), $template_ids );
+
+ // Filter template part by area.
+ // Requires a block theme.
+ /*$templates = get_block_templates( array( 'area' => WP_TEMPLATE_PART_AREA_HEADER ), 'wp_template_part' );
+ $template_ids = get_template_ids( $templates );
+ $this->assertEquals(
+ array(
+ get_stylesheet() . '//' . 'my_template_part',
+ get_stylesheet() . '//' . 'header',
+ ),
+ $template_ids
+ );
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * Should flatten nested blocks
+ */
+ function test_flatten_blocks() {
+ $content_template_part_inside_group = '<!-- wp:group --><!-- wp:template-part {"slug":"header"} /--><!-- /wp:group -->';
+ $blocks = parse_blocks( $content_template_part_inside_group );
+ $actual = _flatten_blocks( $blocks );
+ $expected = array( $blocks[0], $blocks[0]['innerBlocks'][0] );
+ $this->assertEquals( $expected, $actual );
+
+ $content_template_part_inside_group_inside_group = '<!-- wp:group --><!-- wp:group --><!-- wp:template-part {"slug":"header"} /--><!-- /wp:group --><!-- /wp:group -->';
+ $blocks = parse_blocks( $content_template_part_inside_group_inside_group );
+ $actual = _flatten_blocks( $blocks );
+ $expected = array( $blocks[0], $blocks[0]['innerBlocks'][0], $blocks[0]['innerBlocks'][0]['innerBlocks'][0] );
+ $this->assertEquals( $expected, $actual );
+
+ $content_without_inner_blocks = '<!-- wp:group /-->';
+ $blocks = parse_blocks( $content_without_inner_blocks );
+ $actual = _flatten_blocks( $blocks );
+ $expected = array( $blocks[0] );
+ $this->assertEquals( $expected, $actual );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestschemasetupphp"></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/rest-schema-setup.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php 2021-11-08 23:05:50 UTC (rev 52061)
+++ trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php 2021-11-08 23:09:53 UTC (rev 52062)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -132,6 +132,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/block-types/(?P<namespace>[a-zA-Z0-9_-]+)',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/block-types/(?P<namespace>[a-zA-Z0-9_-]+)/(?P<name>[a-zA-Z0-9_-]+)',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/settings',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ '/wp/v2/template-parts',
+ '/wp/v2/template-parts/(?P<id>[\/\w-]+)',
+ '/wp/v2/template-parts/(?P<id>[\d]+)/autosaves',
+ '/wp/v2/template-parts/(?P<parent>[\d]+)/autosaves/(?P<id>[\d]+)',
+ '/wp/v2/template-parts/(?P<parent>[\d]+)/revisions',
+ '/wp/v2/template-parts/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)',
</ins><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/templates',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/templates/(?P<id>[\/\w-]+)',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/templates/(?P<id>[\d]+)/autosaves',
</span></span></pre></div>
<a id="trunktestsqunitfixtureswpapigeneratedjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/qunit/fixtures/wp-api-generated.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/qunit/fixtures/wp-api-generated.js 2021-11-08 23:05:50 UTC (rev 52061)
+++ trunk/tests/qunit/fixtures/wp-api-generated.js 2021-11-08 23:09:53 UTC (rev 52062)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4164,6 +4164,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> "description": "Limit to the specified post id.",
</span><span class="cx" style="display: block; padding: 0 10px"> "type": "integer",
</span><span class="cx" style="display: block; padding: 0 10px"> "required": false
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ },
+ "area": {
+ "description": "Limit to the specified template part area.",
+ "type": "string",
+ "required": false
+ },
+ "post_type": {
+ "description": "Post type to get the templates for.",
+ "type": "string",
+ "required": false
</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">@@ -4585,6 +4595,478 @@
</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">+ "/wp/v2/template-parts": {
+ "namespace": "wp/v2",
+ "methods": [
+ "GET",
+ "POST"
+ ],
+ "endpoints": [
+ {
+ "methods": [
+ "GET"
+ ],
+ "args": {
+ "context": {
+ "description": "Scope under which the request is made; determines fields present in response.",
+ "type": "string",
+ "enum": [
+ "view",
+ "embed",
+ "edit"
+ ],
+ "required": false
+ },
+ "wp_id": {
+ "description": "Limit to the specified post id.",
+ "type": "integer",
+ "required": false
+ },
+ "area": {
+ "description": "Limit to the specified template part area.",
+ "type": "string",
+ "required": false
+ },
+ "post_type": {
+ "description": "Post type to get the templates for.",
+ "type": "string",
+ "required": false
+ }
+ }
+ },
+ {
+ "methods": [
+ "POST"
+ ],
+ "args": {
+ "slug": {
+ "description": "Unique slug identifying the template.",
+ "type": "string",
+ "minLength": 1,
+ "pattern": "[a-zA-Z_\\-]+",
+ "required": true
+ },
+ "theme": {
+ "description": "Theme identifier for the template.",
+ "type": "string",
+ "required": false
+ },
+ "content": {
+ "default": "",
+ "description": "Content of template.",
+ "type": [
+ "object",
+ "string"
+ ],
+ "required": false
+ },
+ "title": {
+ "default": "",
+ "description": "Title of template.",
+ "type": [
+ "object",
+ "string"
+ ],
+ "required": false
+ },
+ "description": {
+ "default": "",
+ "description": "Description of template.",
+ "type": "string",
+ "required": false
+ },
+ "status": {
+ "default": "publish",
+ "description": "Status of template.",
+ "type": "string",
+ "required": false
+ },
+ "area": {
+ "description": "Where the template part is intended for use (header, footer, etc.)",
+ "type": "string",
+ "required": false
+ }
+ }
+ }
+ ],
+ "_links": {
+ "self": [
+ {
+ "href": "http://example.org/index.php?rest_route=/wp/v2/template-parts"
+ }
+ ]
+ }
+ },
+ "/wp/v2/template-parts/(?P<id>[\\/\\w-]+)": {
+ "namespace": "wp/v2",
+ "methods": [
+ "GET",
+ "POST",
+ "PUT",
+ "PATCH",
+ "DELETE"
+ ],
+ "endpoints": [
+ {
+ "methods": [
+ "GET"
+ ],
+ "args": {
+ "id": {
+ "description": "The id of a template",
+ "type": "string",
+ "required": false
+ }
+ }
+ },
+ {
+ "methods": [
+ "POST",
+ "PUT",
+ "PATCH"
+ ],
+ "args": {
+ "slug": {
+ "description": "Unique slug identifying the template.",
+ "type": "string",
+ "minLength": 1,
+ "pattern": "[a-zA-Z_\\-]+",
+ "required": false
+ },
+ "theme": {
+ "description": "Theme identifier for the template.",
+ "type": "string",
+ "required": false
+ },
+ "content": {
+ "description": "Content of template.",
+ "type": [
+ "object",
+ "string"
+ ],
+ "required": false
+ },
+ "title": {
+ "description": "Title of template.",
+ "type": [
+ "object",
+ "string"
+ ],
+ "required": false
+ },
+ "description": {
+ "description": "Description of template.",
+ "type": "string",
+ "required": false
+ },
+ "status": {
+ "description": "Status of template.",
+ "type": "string",
+ "required": false
+ },
+ "area": {
+ "description": "Where the template part is intended for use (header, footer, etc.)",
+ "type": "string",
+ "required": false
+ }
+ }
+ },
+ {
+ "methods": [
+ "DELETE"
+ ],
+ "args": {
+ "force": {
+ "type": "boolean",
+ "default": false,
+ "description": "Whether to bypass Trash and force deletion.",
+ "required": false
+ }
+ }
+ }
+ ]
+ },
+ "/wp/v2/template-parts/(?P<parent>[\\d]+)/revisions": {
+ "namespace": "wp/v2",
+ "methods": [
+ "GET"
+ ],
+ "endpoints": [
+ {
+ "methods": [
+ "GET"
+ ],
+ "args": {
+ "parent": {
+ "description": "The ID for the parent of the revision.",
+ "type": "integer",
+ "required": false
+ },
+ "context": {
+ "description": "Scope under which the request is made; determines fields present in response.",
+ "type": "string",
+ "enum": [
+ "view",
+ "embed",
+ "edit"
+ ],
+ "default": "view",
+ "required": false
+ },
+ "page": {
+ "description": "Current page of the collection.",
+ "type": "integer",
+ "default": 1,
+ "minimum": 1,
+ "required": false
+ },
+ "per_page": {
+ "description": "Maximum number of items to be returned in result set.",
+ "type": "integer",
+ "minimum": 1,
+ "maximum": 100,
+ "required": false
+ },
+ "search": {
+ "description": "Limit results to those matching a string.",
+ "type": "string",
+ "required": false
+ },
+ "exclude": {
+ "description": "Ensure result set excludes specific IDs.",
+ "type": "array",
+ "items": {
+ "type": "integer"
+ },
+ "default": [],
+ "required": false
+ },
+ "include": {
+ "description": "Limit result set to specific IDs.",
+ "type": "array",
+ "items": {
+ "type": "integer"
+ },
+ "default": [],
+ "required": false
+ },
+ "offset": {
+ "description": "Offset the result set by a specific number of items.",
+ "type": "integer",
+ "required": false
+ },
+ "order": {
+ "description": "Order sort attribute ascending or descending.",
+ "type": "string",
+ "default": "desc",
+ "enum": [
+ "asc",
+ "desc"
+ ],
+ "required": false
+ },
+ "orderby": {
+ "description": "Sort collection by object attribute.",
+ "type": "string",
+ "default": "date",
+ "enum": [
+ "date",
+ "id",
+ "include",
+ "relevance",
+ "slug",
+ "include_slugs",
+ "title"
+ ],
+ "required": false
+ }
+ }
+ }
+ ]
+ },
+ "/wp/v2/template-parts/(?P<parent>[\\d]+)/revisions/(?P<id>[\\d]+)": {
+ "namespace": "wp/v2",
+ "methods": [
+ "GET",
+ "DELETE"
+ ],
+ "endpoints": [
+ {
+ "methods": [
+ "GET"
+ ],
+ "args": {
+ "parent": {
+ "description": "The ID for the parent of the revision.",
+ "type": "integer",
+ "required": false
+ },
+ "id": {
+ "description": "Unique identifier for the revision.",
+ "type": "integer",
+ "required": false
+ },
+ "context": {
+ "description": "Scope under which the request is made; determines fields present in response.",
+ "type": "string",
+ "enum": [
+ "view",
+ "embed",
+ "edit"
+ ],
+ "default": "view",
+ "required": false
+ }
+ }
+ },
+ {
+ "methods": [
+ "DELETE"
+ ],
+ "args": {
+ "parent": {
+ "description": "The ID for the parent of the revision.",
+ "type": "integer",
+ "required": false
+ },
+ "id": {
+ "description": "Unique identifier for the revision.",
+ "type": "integer",
+ "required": false
+ },
+ "force": {
+ "type": "boolean",
+ "default": false,
+ "description": "Required to be true, as revisions do not support trashing.",
+ "required": false
+ }
+ }
+ }
+ ]
+ },
+ "/wp/v2/template-parts/(?P<id>[\\d]+)/autosaves": {
+ "namespace": "wp/v2",
+ "methods": [
+ "GET",
+ "POST"
+ ],
+ "endpoints": [
+ {
+ "methods": [
+ "GET"
+ ],
+ "args": {
+ "parent": {
+ "description": "The ID for the parent of the autosave.",
+ "type": "integer",
+ "required": false
+ },
+ "context": {
+ "description": "Scope under which the request is made; determines fields present in response.",
+ "type": "string",
+ "enum": [
+ "view",
+ "embed",
+ "edit"
+ ],
+ "default": "view",
+ "required": false
+ }
+ }
+ },
+ {
+ "methods": [
+ "POST"
+ ],
+ "args": {
+ "parent": {
+ "description": "The ID for the parent of the autosave.",
+ "type": "integer",
+ "required": false
+ },
+ "slug": {
+ "description": "Unique slug identifying the template.",
+ "type": "string",
+ "minLength": 1,
+ "pattern": "[a-zA-Z_\\-]+",
+ "required": false
+ },
+ "theme": {
+ "description": "Theme identifier for the template.",
+ "type": "string",
+ "required": false
+ },
+ "content": {
+ "description": "Content of template.",
+ "type": [
+ "object",
+ "string"
+ ],
+ "required": false
+ },
+ "title": {
+ "description": "Title of template.",
+ "type": [
+ "object",
+ "string"
+ ],
+ "required": false
+ },
+ "description": {
+ "description": "Description of template.",
+ "type": "string",
+ "required": false
+ },
+ "status": {
+ "description": "Status of template.",
+ "type": "string",
+ "required": false
+ },
+ "area": {
+ "description": "Where the template part is intended for use (header, footer, etc.)",
+ "type": "string",
+ "required": false
+ }
+ }
+ }
+ ]
+ },
+ "/wp/v2/template-parts/(?P<parent>[\\d]+)/autosaves/(?P<id>[\\d]+)": {
+ "namespace": "wp/v2",
+ "methods": [
+ "GET"
+ ],
+ "endpoints": [
+ {
+ "methods": [
+ "GET"
+ ],
+ "args": {
+ "parent": {
+ "description": "The ID for the parent of the autosave.",
+ "type": "integer",
+ "required": false
+ },
+ "id": {
+ "description": "The ID for the autosave.",
+ "type": "integer",
+ "required": false
+ },
+ "context": {
+ "description": "Scope under which the request is made; determines fields present in response.",
+ "type": "string",
+ "enum": [
+ "view",
+ "embed",
+ "edit"
+ ],
+ "default": "view",
+ "required": false
+ }
+ }
+ }
+ ]
+ },
</ins><span class="cx" style="display: block; padding: 0 10px"> "/wp/v2/types": {
</span><span class="cx" style="display: block; padding: 0 10px"> "namespace": "wp/v2",
</span><span class="cx" style="display: block; padding: 0 10px"> "methods": [
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -8610,6 +9092,34 @@
</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">+ },
+ "wp_template_part": {
+ "description": "Template parts to include in your templates.",
+ "hierarchical": false,
+ "name": "Template Parts",
+ "slug": "wp_template_part",
+ "taxonomies": [],
+ "rest_base": "template-parts",
+ "rest_namespace": "wp/v2",
+ "_links": {
+ "collection": [
+ {
+ "href": "http://example.org/index.php?rest_route=/wp/v2/types"
+ }
+ ],
+ "wp:items": [
+ {
+ "href": "http://example.org/index.php?rest_route=/wp/v2/template-parts"
+ }
+ ],
+ "curies": [
+ {
+ "name": "wp",
+ "href": "https://api.w.org/{rel}",
+ "templated": true
+ }
+ ]
+ }
</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="trunktoolswebpackblocksjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tools/webpack/blocks.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tools/webpack/blocks.js 2021-11-08 23:05:50 UTC (rev 52061)
+++ trunk/tools/webpack/blocks.js 2021-11-08 23:09:53 UTC (rev 52062)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -53,6 +53,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'site-title',
</span><span class="cx" style="display: block; padding: 0 10px"> 'social-link',
</span><span class="cx" style="display: block; padding: 0 10px"> 'tag-cloud',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'template-part',
</ins><span class="cx" style="display: block; padding: 0 10px"> ];
</span><span class="cx" style="display: block; padding: 0 10px"> const blockFolders = [
</span><span class="cx" style="display: block; padding: 0 10px"> 'audio',
</span></span></pre>
</div>
</div>
</body>
</html>