From 239a6c33ec5fef9416655c36225b064b987c31cc Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 21 Nov 2022 16:48:33 +0000 Subject: [PATCH] Site Editor: Show correct theme per template or template part. Child themes inherit templates and template parts from the parent theme. In Site Editor, the "Added by" column for a template defaults to displaying the child theme, even though it is inherited from the parent, creating confusion as to where the actual templates are located. This commit ensures that the parent theme is correctly displayed in that scenario. Follow-up to [51003], [52062]. Props ptahdunbar, WoutPitje, petaryoast, costdev, poena, audrasjb, SergeyBiryukov. Fixes #55437. git-svn-id: https://develop.svn.wordpress.org/trunk@54860 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/block-template-utils.php | 2 +- tests/phpunit/tests/block-template-utils.php | 42 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index b1a3172fbb..f4958050c6 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -503,7 +503,7 @@ function _build_block_template_result_from_file( $template_file, $template_type $template = new WP_Block_Template(); $template->id = $theme . '//' . $template_file['slug']; - $template->theme = $theme; + $template->theme = ! empty( $template_file['theme'] ) ? $template_file['theme'] : $theme; $template->content = _inject_theme_attribute_in_block_template_content( $template_content ); $template->slug = $template_file['slug']; $template->source = 'theme'; diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 440dbc15d9..a77f7e9d81 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -123,6 +123,25 @@ class Tests_Block_Template_Utils extends WP_UnitTestCase { $this->assertSame( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area ); } + /** + * Tests that _build_block_template_result_from_post() returns the correct theme + * for the template when a child theme is active. + * + * @ticket 55437 + * + * @covers ::_build_block_template_result_from_post + */ + function test_build_block_template_result_from_post_with_child_theme() { + switch_theme( 'block-theme-child' ); + + $template = _build_block_template_result_from_post( + self::$template_post, + 'wp_template' + ); + + $this->assertSame( self::TEST_THEME, $template->theme ); + } + function test_build_block_template_result_from_file() { $template = _build_block_template_result_from_file( array( @@ -161,6 +180,29 @@ class Tests_Block_Template_Utils extends WP_UnitTestCase { $this->assertSame( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area ); } + /** + * Tests that _build_block_template_result_from_file() returns the correct theme + * for the template when a child theme is active. + * + * @ticket 55437 + * + * @covers ::_build_block_template_result_from_file + */ + function test_build_block_template_result_from_file_with_child_theme() { + switch_theme( 'block-theme-child' ); + + $template = _build_block_template_result_from_file( + array( + 'slug' => 'single', + 'path' => __DIR__ . '/../data/templates/template.html', + 'theme' => self::TEST_THEME, + ), + 'wp_template' + ); + + $this->assertSame( self::TEST_THEME, $template->theme ); + } + function test_inject_theme_attribute_in_block_template_content() { $theme = get_stylesheet(); $content_without_theme_attribute = '';