Skip to content

Commit 9e96b0e

Browse files
noisysocksaslamdoctor
authored andcommitted
Editor: Add plugin template registration API and improve theme overrides for plugin-registered templates
This commit introduces a new API to allow plugins to easily register block templates with `wp_register_block_template()` and the `WP_Block_Templates_Registry` class, addressing the complexity of hooking into multiple filters. It also ensures plugin-registered templates overridden by themes fall back to the plugin-provided title and description when the theme doesn't define them. See WordPress/gutenberg#61577. See WordPress/gutenberg#64610. Fixes #61804. Props aljullu, peterwilsoncc, antonvlasenko, azaozz, youknowriad, noisysocks. git-svn-id: https://develop.svn.wordpress.org/trunk@59073 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 41d6e5e commit 9e96b0e

11 files changed

Lines changed: 763 additions & 19 deletions

src/wp-includes/block-template-utils.php

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,15 @@ function _build_block_template_result_from_file( $template_file, $template_type
592592
$template->is_custom = true;
593593
$template->modified = null;
594594

595+
if ( 'wp_template' === $template_type ) {
596+
$registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $template_file['slug'] );
597+
if ( $registered_template ) {
598+
$template->plugin = $registered_template->plugin;
599+
$template->title = empty( $template->title ) || $template->title === $template->slug ? $registered_template->title : $template->title;
600+
$template->description = empty( $template->description ) ? $registered_template->description : $template->description;
601+
}
602+
}
603+
595604
if ( 'wp_template' === $template_type && isset( $default_template_types[ $template_file['slug'] ] ) ) {
596605
$template->description = $default_template_types[ $template_file['slug'] ]['description'];
597606
$template->title = $default_template_types[ $template_file['slug'] ]['title'];
@@ -1014,6 +1023,19 @@ function _build_block_template_result_from_post( $post ) {
10141023
}
10151024
}
10161025

1026+
if ( 'wp_template' === $post->post_type ) {
1027+
$registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $template->slug );
1028+
if ( $registered_template ) {
1029+
$template->plugin = $registered_template->plugin;
1030+
$template->origin =
1031+
'theme' !== $template->origin && 'theme' !== $template->source ?
1032+
'plugin' :
1033+
$template->origin;
1034+
$template->title = empty( $template->title ) || $template->title === $template->slug ? $registered_template->title : $template->title;
1035+
$template->description = empty( $template->description ) ? $registered_template->description : $template->description;
1036+
}
1037+
}
1038+
10171039
$hooked_blocks = get_hooked_blocks();
10181040
if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
10191041
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
@@ -1157,6 +1179,23 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' )
11571179
foreach ( $template_files as $template_file ) {
11581180
$query_result[] = _build_block_template_result_from_file( $template_file, $template_type );
11591181
}
1182+
1183+
if ( 'wp_template' === $template_type ) {
1184+
// Add templates registered in the template registry. Filtering out the ones which have a theme file.
1185+
$registered_templates = WP_Block_Templates_Registry::get_instance()->get_by_query( $query );
1186+
$matching_registered_templates = array_filter(
1187+
$registered_templates,
1188+
function ( $registered_template ) use ( $template_files ) {
1189+
foreach ( $template_files as $template_file ) {
1190+
if ( $template_file['slug'] === $registered_template->slug ) {
1191+
return false;
1192+
}
1193+
}
1194+
return true;
1195+
}
1196+
);
1197+
$query_result = array_merge( $query_result, $matching_registered_templates );
1198+
}
11601199
}
11611200

11621201
/**
@@ -1287,18 +1326,17 @@ function get_block_file_template( $id, $template_type = 'wp_template' ) {
12871326
}
12881327
list( $theme, $slug ) = $parts;
12891328

1290-
if ( get_stylesheet() !== $theme ) {
1291-
/** This filter is documented in wp-includes/block-template-utils.php */
1292-
return apply_filters( 'get_block_file_template', null, $id, $template_type );
1293-
}
1329+
if ( get_stylesheet() === $theme ) {
1330+
$template_file = _get_block_template_file( $template_type, $slug );
1331+
if ( null !== $template_file ) {
1332+
$block_template = _build_block_template_result_from_file( $template_file, $template_type );
12941333

1295-
$template_file = _get_block_template_file( $template_type, $slug );
1296-
if ( null === $template_file ) {
1297-
/** This filter is documented in wp-includes/block-template-utils.php */
1298-
return apply_filters( 'get_block_file_template', null, $id, $template_type );
1334+
/** This filter is documented in wp-includes/block-template-utils.php */
1335+
return apply_filters( 'get_block_file_template', $block_template, $id, $template_type );
1336+
}
12991337
}
13001338

1301-
$block_template = _build_block_template_result_from_file( $template_file, $template_type );
1339+
$block_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $slug );
13021340

13031341
/**
13041342
* Filters the block template object after it has been (potentially) fetched from the theme file.
@@ -1665,12 +1703,12 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $deprecated
16651703
);
16661704
}
16671705

1668-
$content = get_comment_delimited_block_content(
1706+
$content = get_comment_delimited_block_content(
16691707
'core/template-part',
16701708
$attributes,
16711709
$changes->post_content
16721710
);
1673-
$content = apply_block_hooks_to_content( $content, $template, 'set_ignored_hooked_blocks_metadata' );
1711+
$content = apply_block_hooks_to_content( $content, $template, 'set_ignored_hooked_blocks_metadata' );
16741712
$changes->post_content = remove_serialized_parent_block( $content );
16751713

16761714
$wrapper_block_markup = extract_serialized_parent_block( $content );

src/wp-includes/block-template.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,38 @@ function _resolve_template_for_new_post( $wp_query ) {
358358
$wp_query->set( 'post_status', 'auto-draft' );
359359
}
360360
}
361+
362+
/**
363+
* Register a block template.
364+
*
365+
* @since 6.7.0
366+
*
367+
* @param string $template_name Template name in the form of `plugin_uri//template_name`.
368+
* @param array|string $args {
369+
* @type string $title Optional. Title of the template as it will be shown in the Site Editor
370+
* and other UI elements.
371+
* @type string $description Optional. Description of the template as it will be shown in the Site
372+
* Editor.
373+
* @type string $content Optional. Default content of the template that will be used when the
374+
* template is rendered or edited in the editor.
375+
* @type string[] $post_types Optional. Array of post types to which the template should be available.
376+
* @type string $plugin Optional. Slug of the plugin that registers the template.
377+
* }
378+
* @return WP_Block_Template|WP_Error The registered template object on success, WP_Error object on failure.
379+
*/
380+
function wp_register_block_template( $template_name, $args = array() ) {
381+
return WP_Block_Templates_Registry::get_instance()->register( $template_name, $args );
382+
}
383+
384+
/**
385+
* Unregister a block template.
386+
*
387+
* @since 6.7.0
388+
*
389+
* @param string $template_name Template name in the form of `plugin_uri//template_name`.
390+
* @return WP_Block_Template|WP_Error The unregistered template object on success, WP_Error object on failure or if the
391+
* template doesn't exist.
392+
*/
393+
function wp_unregister_block_template( $template_name ) {
394+
return WP_Block_Templates_Registry::get_instance()->unregister( $template_name );
395+
}

src/wp-includes/class-wp-block-template.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ class WP_Block_Template {
131131
*/
132132
public $author;
133133

134+
/**
135+
* Plugin.
136+
*
137+
* @since 6.7.0
138+
* @var string|null
139+
*/
140+
public $plugin;
141+
134142
/**
135143
* Post types.
136144
*

0 commit comments

Comments
 (0)