From 5d3869ffac837556399aae8c8aa33bab5fda1c97 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Fri, 26 Nov 2021 12:08:54 +0000 Subject: [PATCH 1/3] Templates: Search for old template names in the parent theme too --- lib/compat/wordpress-5.9/block-template-utils.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/compat/wordpress-5.9/block-template-utils.php b/lib/compat/wordpress-5.9/block-template-utils.php index 37ae365ccc4f0b..350506f3f77e36 100644 --- a/lib/compat/wordpress-5.9/block-template-utils.php +++ b/lib/compat/wordpress-5.9/block-template-utils.php @@ -36,7 +36,11 @@ function get_block_theme_folders( $theme_stylesheet = null ) { $root_dir = get_theme_root( $theme_name ); $theme_dir = "$root_dir/$theme_name"; - if ( is_readable( $theme_dir . '/block-templates/index.html' ) ) { + $parent_theme_name = get_template(); + $parent_root_dir = get_theme_root( $parent_theme_name ); + $parent_theme_dir = "$parent_root_dir/$parent_theme_name"; + + if ( is_readable( $theme_dir . '/block-templates/index.html' ) || is_readable( $parent_theme_dir . '/block-templates/index.html' ) ) { return array( 'wp_template' => 'block-templates', 'wp_template_part' => 'block-template-parts', From 12b017b342c403244e05344d6774635f735161ba Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Wed, 8 Dec 2021 16:35:55 +0000 Subject: [PATCH 2/3] check for the directory rather than the file --- lib/compat/wordpress-5.9/block-template-utils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-5.9/block-template-utils.php b/lib/compat/wordpress-5.9/block-template-utils.php index 350506f3f77e36..a28be0d799cf89 100644 --- a/lib/compat/wordpress-5.9/block-template-utils.php +++ b/lib/compat/wordpress-5.9/block-template-utils.php @@ -40,7 +40,7 @@ function get_block_theme_folders( $theme_stylesheet = null ) { $parent_root_dir = get_theme_root( $parent_theme_name ); $parent_theme_dir = "$parent_root_dir/$parent_theme_name"; - if ( is_readable( $theme_dir . '/block-templates/index.html' ) || is_readable( $parent_theme_dir . '/block-templates/index.html' ) ) { + if ( is_readable( $theme_dir . '/block-templates/' ) || is_readable( $parent_theme_dir . '/block-templates/' ) ) { return array( 'wp_template' => 'block-templates', 'wp_template_part' => 'block-template-parts', From 004cba5fc79d8f1ec9d9ee8791775baa87d2f247 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Mon, 13 Dec 2021 13:46:18 +0100 Subject: [PATCH 3/3] Fix template part file lookup --- lib/compat/wordpress-5.9/block-template-utils.php | 6 +----- packages/block-library/src/template-part/index.php | 7 +++++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/compat/wordpress-5.9/block-template-utils.php b/lib/compat/wordpress-5.9/block-template-utils.php index a28be0d799cf89..4b693237e5cad4 100644 --- a/lib/compat/wordpress-5.9/block-template-utils.php +++ b/lib/compat/wordpress-5.9/block-template-utils.php @@ -36,11 +36,7 @@ function get_block_theme_folders( $theme_stylesheet = null ) { $root_dir = get_theme_root( $theme_name ); $theme_dir = "$root_dir/$theme_name"; - $parent_theme_name = get_template(); - $parent_root_dir = get_theme_root( $parent_theme_name ); - $parent_theme_dir = "$parent_root_dir/$parent_theme_name"; - - if ( is_readable( $theme_dir . '/block-templates/' ) || is_readable( $parent_theme_dir . '/block-templates/' ) ) { + if ( file_exists( $theme_dir . '/block-templates' ) || file_exists( $theme_dir . '/block-template-parts' ) ) { return array( 'wp_template' => 'block-templates', 'wp_template_part' => 'block-template-parts', diff --git a/packages/block-library/src/template-part/index.php b/packages/block-library/src/template-part/index.php index 1eb8e39d780e39..20bc6900f83bb0 100644 --- a/packages/block-library/src/template-part/index.php +++ b/packages/block-library/src/template-part/index.php @@ -64,8 +64,11 @@ function render_block_core_template_part( $attributes ) { } else { // Else, if the template part was provided by the active theme, // render the corresponding file content. - $theme_folders = get_block_theme_folders(); - $template_part_file_path = get_theme_file_path( '/' . $theme_folders['wp_template_part'] . '/' . $attributes['slug'] . '.html' ); + $parent_theme_folders = get_block_theme_folders( get_template() ); + $child_theme_folders = get_block_theme_folders( get_stylesheet() ); + $child_theme_part_file_path = get_theme_file_path( '/' . $child_theme_folders['wp_template_part'] . '/' . $attributes['slug'] . '.html' ); + $parent_theme_part_file_path = get_theme_file_path( '/' . $parent_theme_folders['wp_template_part'] . '/' . $attributes['slug'] . '.html' ); + $template_part_file_path = 0 === validate_file( $attributes['slug'] ) && file_exists( $child_theme_part_file_path ) ? $child_theme_part_file_path : $parent_theme_part_file_path; if ( 0 === validate_file( $attributes['slug'] ) && file_exists( $template_part_file_path ) ) { $content = file_get_contents( $template_part_file_path ); $content = is_string( $content ) && '' !== $content