Skip to content

Commit

Permalink
Make plugin-registered templates overriden by themes to fall back to …
Browse files Browse the repository at this point in the history
…plugin-registered title and description (WordPress#64610)

* Make plugin-registered templates overriden by themes to fall back to plugin-registered title and description

* Update backport changelog file
  • Loading branch information
Aljullu authored and bph committed Aug 31, 2024
1 parent 37a47b0 commit 8886585
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions backport-changelog/6.7/7125.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
https://github.com/WordPress/wordpress-develop/pull/7125

* https://github.com/WordPress/gutenberg/pull/61577
* https://github.com/WordPress/gutenberg/pull/64610
14 changes: 10 additions & 4 deletions lib/compat/wordpress-6.7/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ function _gutenberg_add_block_templates_from_registry( $query_result, $query, $t
foreach ( $query_result as $key => $value ) {
$registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $query_result[ $key ]->slug );
if ( $registered_template ) {
$query_result[ $key ]->plugin = $registered_template->plugin;
$query_result[ $key ]->origin =
$query_result[ $key ]->plugin = $registered_template->plugin;
$query_result[ $key ]->origin =
'theme' !== $query_result[ $key ]->origin && 'theme' !== $query_result[ $key ]->source ?
'plugin' :
$query_result[ $key ]->origin;
$query_result[ $key ]->title =
empty( $query_result[ $key ]->title ) || $query_result[ $key ]->title === $query_result[ $key ]->slug ?
$registered_template->title : $query_result[ $key ]->title;
$query_result[ $key ]->description = empty( $query_result[ $key ]->description ) ? $registered_template->description : $query_result[ $key ]->description;
}
}

Expand Down Expand Up @@ -70,11 +74,13 @@ function _gutenberg_add_block_template_plugin_attribute( $block_template ) {
if ( $block_template ) {
$registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $block_template->slug );
if ( $registered_template ) {
$block_template->plugin = $registered_template->plugin;
$block_template->origin =
$block_template->plugin = $registered_template->plugin;
$block_template->origin =
'theme' !== $block_template->origin && 'theme' !== $block_template->source ?
'plugin' :
$block_template->origin;
$block_template->title = empty( $block_template->title ) || $block_template->title === $block_template->slug ? $registered_template->title : $block_template->title;
$block_template->description = empty( $block_template->description ) ? $registered_template->description : $block_template->description;
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/e2e/specs/site-editor/template-registration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ test.describe( 'Block template registration', () => {
await expect(
page.getByText( 'Custom Template (overridden by the theme)' )
).toBeHidden();
// Verify the template description fall backs to the plugin registered description.
await expect(
page.getByText(
'A custom template registered by a plugin and overridden by a theme.'
)
).toBeVisible();
// Verify the theme template shows the theme name as the author.
await expect( page.getByText( 'AuthorEmptytheme' ) ).toBeVisible();
} );
Expand Down

0 comments on commit 8886585

Please sign in to comment.