Add template title and description on 'get_block_template' hook (#44254)

* Simplify add template title and description

* Add template title and description on 'get_block_template' hook. Fixes #42221

* Add tests

* Add changefile(s) from automation for the following project(s): woocommerce-blocks, woocommerce

* Fix test in template parts

* Use BlockTemplateUtils::template_has_title when possible

* Add test also when saving the default WooCommerce template

---------

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Albert Juhé Lluveras 2024-02-06 13:33:38 +01:00 committed by GitHub
parent b4928ed46a
commit fa9d42be4f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 46 additions and 7 deletions

View File

@ -11,6 +11,8 @@ import { CUSTOMIZABLE_WC_TEMPLATES, WC_TEMPLATES_SLUG } from './constants';
CUSTOMIZABLE_WC_TEMPLATES.forEach( ( testData ) => {
const userText = `Hello World in the ${ testData.templateName } template`;
const fallbackTemplateUserText = `Hello World in the fallback ${ testData.templateName } template`;
const templateTypeName =
testData.templateType === 'wp_template' ? 'template' : 'template part';
test.describe( `${ testData.templateName } template`, async () => {
test( 'can be modified and reverted', async ( {
@ -31,6 +33,14 @@ CUSTOMIZABLE_WC_TEMPLATES.forEach( ( testData ) => {
attributes: { content: userText },
} );
await editorUtils.saveTemplate();
// Verify template name didn't change.
// See: https://github.com/woocommerce/woocommerce/issues/42221
await expect(
page.getByRole( 'heading', {
name: `Editing ${ templateTypeName }: ${ testData.templateName }`,
} )
).toBeVisible();
await testData.visitPage( { frontendUtils, page } );
await expect( page.getByText( userText ).first() ).toBeVisible();

View File

@ -15,6 +15,8 @@ CUSTOMIZABLE_WC_TEMPLATES.forEach( ( testData ) => {
}
const userText = `Hello World in the ${ testData.templateName } template`;
const fallbackTemplateUserText = `Hello World in the fallback ${ testData.templateName } template`;
const templateTypeName =
testData.templateType === 'wp_template' ? 'template' : 'template part';
test.describe( `${ testData.templateName } template`, async () => {
test( "theme template has priority over WooCommerce's and can be modified", async ( {
@ -35,6 +37,13 @@ CUSTOMIZABLE_WC_TEMPLATES.forEach( ( testData ) => {
attributes: { content: userText },
} );
await editorUtils.saveTemplate();
// Verify template name didn't change.
// See: https://github.com/woocommerce/woocommerce/issues/42221
await expect(
page.getByRole( 'heading', {
name: `Editing ${ templateTypeName }: ${ testData.templateName }`,
} )
).toBeVisible();
// Verify the template is the one modified by the user.
await testData.visitPage( { frontendUtils, page } );

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Fix the Site Editor showing the template slug instead of the template title when saving a WooCommerce block template customized by the theme.

View File

@ -58,6 +58,7 @@ class BlockTemplatesController {
add_action( 'template_redirect', array( $this, 'render_block_template' ) );
add_filter( 'pre_get_block_template', array( $this, 'get_block_template_fallback' ), 10, 3 );
add_filter( 'pre_get_block_file_template', array( $this, 'get_block_file_template' ), 10, 3 );
add_filter( 'get_block_template', array( $this, 'add_block_template_details' ), 10, 1 );
add_filter( 'get_block_templates', array( $this, 'add_block_templates' ), 10, 3 );
add_filter( 'current_theme_supports-block-templates', array( $this, 'remove_block_template_support_for_shop_page' ) );
add_filter( 'taxonomy_template_hierarchy', array( $this, 'add_archive_product_to_eligible_for_fallback_templates' ), 10, 1 );
@ -346,6 +347,25 @@ class BlockTemplatesController {
return $template;
}
/**
* Add the template title and description to WooCommerce templates.
*
* @param WP_Block_Template|null $block_template The found block template, or null if there isn't one.
* @return WP_Block_Template|null
*/
public function add_block_template_details( $block_template ) {
if ( ! $block_template ) {
return $block_template;
}
if ( ! BlockTemplateUtils::template_has_title( $block_template ) ) {
$block_template->title = BlockTemplateUtils::get_block_template_title( $block_template->slug );
}
if ( ! $block_template->description ) {
$block_template->description = BlockTemplateUtils::get_block_template_description( $block_template->slug );
}
return $block_template;
}
/**
* Add the block template objects to be used.
*
@ -389,9 +409,7 @@ class BlockTemplatesController {
if ( 'custom' !== $template_file->source ) {
$template = BlockTemplateUtils::build_template_result_from_file( $template_file, $template_type );
} else {
$template_file->title = BlockTemplateUtils::get_block_template_title( $template_file->slug );
$template_file->description = BlockTemplateUtils::get_block_template_description( $template_file->slug );
$query_result[] = $template_file;
$query_result[] = $template_file;
continue;
}
@ -456,15 +474,13 @@ class BlockTemplatesController {
}
}
if ( 'theme' === $template->origin && BlockTemplateUtils::template_has_title( $template ) ) {
return $template;
}
if ( $template->title === $template->slug ) {
if ( ! BlockTemplateUtils::template_has_title( $template ) ) {
$template->title = BlockTemplateUtils::get_block_template_title( $template->slug );
}
if ( ! $template->description ) {
$template->description = BlockTemplateUtils::get_block_template_description( $template->slug );
}
return $template;
},
$query_result