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.

Built from https://develop.svn.wordpress.org/trunk@52247


git-svn-id: http://core.svn.wordpress.org/trunk@51839 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
youknowriad 2021-11-25 10:59:04 +00:00
parent 322b60f600
commit 5d73860e5c
3 changed files with 37 additions and 14 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

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.9-alpha-52246';
$wp_version = '5.9-alpha-52247';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.