From b90fc1b6dda3bfff668ad07e25da545c3eb10423 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Thu, 25 Nov 2021 10:57:19 +0000 Subject: [PATCH] Themes: Update the base folders for templates and template parts in block themes. Block Themes should now use the following folders: - templates instead of block-templates - parts instead of block-template-parts Existing themes and folders will continue to work without impact. Props bernhard-reiter. Fixes #54493. git-svn-id: https://develop.svn.wordpress.org/trunk@52247 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/block-template-utils.php | 46 ++++++++++++++----- src/wp-includes/theme.php | 3 +- .../page-1.html | 0 .../{block-templates => templates}/index.html | 0 .../page-home.html | 0 .../{block-templates => templates}/page.html | 0 tests/phpunit/tests/block-template.php | 8 ++-- 7 files changed, 40 insertions(+), 17 deletions(-) rename tests/phpunit/data/themedir1/block-theme-child/{block-templates => templates}/page-1.html (100%) rename tests/phpunit/data/themedir1/block-theme/{block-templates => templates}/index.html (100%) rename tests/phpunit/data/themedir1/block-theme/{block-templates => templates}/page-home.html (100%) rename tests/phpunit/data/themedir1/block-theme/{block-templates => templates}/page.html (100%) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 9b248c1453..6475ac36f2 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -20,6 +20,35 @@ if ( ! defined( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED' ) ) { define( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED', 'uncategorized' ); } +/** + * For backward compatibility reasons, + * block themes might be using block-templates or block-template-parts, + * this function ensures we fallback to these folders properly. + * + * @since 5.9.0 + * + * @param string $theme_stylesheet The stylesheet. Default is to leverage the main theme root. + * + * @return array Folder names used by block themes. + */ +function get_block_theme_folders( $theme_stylesheet = null ) { + $theme_name = null === $theme_stylesheet ? get_stylesheet() : $theme_stylesheet; + $root_dir = get_theme_root( $theme_name ); + $theme_dir = "$root_dir/$theme_name"; + + if ( is_readable( $theme_dir . '/block-templates/index.html' ) ) { + return array( + 'wp_template' => 'block-templates', + 'wp_template_part' => 'block-template-parts', + ); + } + + return array( + 'wp_template' => 'templates', + 'wp_template_part' => 'parts', + ); +} + /** * Returns a filtered list of allowed area values for template parts. * @@ -224,16 +253,13 @@ function _get_block_template_file( $template_type, $slug ) { return null; } - $template_base_paths = array( - 'wp_template' => 'block-templates', - 'wp_template_part' => 'block-template-parts', - ); - $themes = array( + $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'; + $template_base_paths = get_block_theme_folders( $theme_slug ); + $file_path = $theme_dir . '/' . $template_base_paths[ $template_type ] . '/' . $slug . '.html'; if ( file_exists( $file_path ) ) { $new_template_item = array( 'slug' => $slug, @@ -272,17 +298,13 @@ function _get_block_templates_files( $template_type ) { return null; } - $template_base_paths = array( - 'wp_template' => 'block-templates', - 'wp_template_part' => 'block-template-parts', - ); - $themes = array( + $themes = array( get_stylesheet() => get_stylesheet_directory(), get_template() => get_template_directory(), ); - $template_files = array(); foreach ( $themes as $theme_slug => $theme_dir ) { + $template_base_paths = get_block_theme_folders( $theme_slug ); $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 ]; diff --git a/src/wp-includes/theme.php b/src/wp-includes/theme.php index 838fe23596..9d43d1b2a2 100644 --- a/src/wp-includes/theme.php +++ b/src/wp-includes/theme.php @@ -4084,5 +4084,6 @@ function create_initial_theme_features() { * @return boolean Whether the current theme is a block-based theme or not. */ function wp_is_block_template_theme() { - return is_readable( get_theme_file_path( '/block-templates/index.html' ) ); + return is_readable( get_theme_file_path( '/block-templates/index.html' ) ) || + is_readable( get_theme_file_path( '/templates/index.html' ) ); } diff --git a/tests/phpunit/data/themedir1/block-theme-child/block-templates/page-1.html b/tests/phpunit/data/themedir1/block-theme-child/templates/page-1.html similarity index 100% rename from tests/phpunit/data/themedir1/block-theme-child/block-templates/page-1.html rename to tests/phpunit/data/themedir1/block-theme-child/templates/page-1.html diff --git a/tests/phpunit/data/themedir1/block-theme/block-templates/index.html b/tests/phpunit/data/themedir1/block-theme/templates/index.html similarity index 100% rename from tests/phpunit/data/themedir1/block-theme/block-templates/index.html rename to tests/phpunit/data/themedir1/block-theme/templates/index.html diff --git a/tests/phpunit/data/themedir1/block-theme/block-templates/page-home.html b/tests/phpunit/data/themedir1/block-theme/templates/page-home.html similarity index 100% rename from tests/phpunit/data/themedir1/block-theme/block-templates/page-home.html rename to tests/phpunit/data/themedir1/block-theme/templates/page-home.html diff --git a/tests/phpunit/data/themedir1/block-theme/block-templates/page.html b/tests/phpunit/data/themedir1/block-theme/templates/page.html similarity index 100% rename from tests/phpunit/data/themedir1/block-theme/block-templates/page.html rename to tests/phpunit/data/themedir1/block-theme/templates/page.html diff --git a/tests/phpunit/tests/block-template.php b/tests/phpunit/tests/block-template.php index e97b7be17f..29a69b15da 100644 --- a/tests/phpunit/tests/block-template.php +++ b/tests/phpunit/tests/block-template.php @@ -37,7 +37,7 @@ class Block_Template_Test extends WP_UnitTestCase { ); $resolved_template_path = locate_block_template( get_stylesheet_directory() . '/page-home.php', $type, $templates ); $this->assertEquals( self::$template_canvas_path, $resolved_template_path ); - $this->assertStringEqualsFile( get_stylesheet_directory() . '/block-templates/page-home.html', $_wp_current_template_content ); + $this->assertStringEqualsFile( get_stylesheet_directory() . '/templates/page-home.html', $_wp_current_template_content ); } function test_page_block_template_takes_precedence() { @@ -50,7 +50,7 @@ class Block_Template_Test extends WP_UnitTestCase { ); $resolved_template_path = locate_block_template( get_stylesheet_directory() . '/page.php', $type, $templates ); $this->assertEquals( self::$template_canvas_path, $resolved_template_path ); - $this->assertStringEqualsFile( get_stylesheet_directory() . '/block-templates/page.html', $_wp_current_template_content ); + $this->assertStringEqualsFile( get_stylesheet_directory() . '/templates/page.html', $_wp_current_template_content ); } function test_block_template_takes_precedence_over_equally_specific_php_template() { @@ -61,7 +61,7 @@ class Block_Template_Test extends WP_UnitTestCase { ); $resolved_template_path = locate_block_template( get_stylesheet_directory() . '/index.php', $type, $templates ); $this->assertEquals( self::$template_canvas_path, $resolved_template_path ); - $this->assertStringEqualsFile( get_stylesheet_directory() . '/block-templates/index.html', $_wp_current_template_content ); + $this->assertStringEqualsFile( get_stylesheet_directory() . '/templates/index.html', $_wp_current_template_content ); } /** @@ -128,7 +128,7 @@ class Block_Template_Test extends WP_UnitTestCase { ); $resolved_template_path = locate_block_template( $parent_theme_page_template_path, $type, $templates ); $this->assertEquals( self::$template_canvas_path, $resolved_template_path ); - $this->assertStringEqualsFile( get_stylesheet_directory() . '/block-templates/page-1.html', $_wp_current_template_content ); + $this->assertStringEqualsFile( get_stylesheet_directory() . '/templates/page-1.html', $_wp_current_template_content ); } /**