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
This commit is contained in:
Riad Benguella
2021-11-25 10:57:19 +00:00
parent 90cdaabd2f
commit b90fc1b6dd
7 changed files with 40 additions and 17 deletions

View File

@@ -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 ];

View File

@@ -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' ) );
}

View File

@@ -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 );
}
/**