From 5d371bc7a24a2da6f3a9ba1b2120c8b4c075c5cc Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Mon, 19 Jun 2023 15:18:34 +0100 Subject: [PATCH] Improve logic in `render_block_core_template_part`. (#50636) * Improve logic in `render_block_core_template_part`. * Use a foreach loop. * Apply suggestions from code review Co-authored-by: George Mamadashvili --------- Co-authored-by: George Mamadashvili --- .../block-library/src/template-part/index.php | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/packages/block-library/src/template-part/index.php b/packages/block-library/src/template-part/index.php index d3de7d0b3afbd5..84e3a985ec4410 100644 --- a/packages/block-library/src/template-part/index.php +++ b/packages/block-library/src/template-part/index.php @@ -18,11 +18,12 @@ function render_block_core_template_part( $attributes ) { $template_part_id = null; $content = null; $area = WP_TEMPLATE_PART_AREA_UNCATEGORIZED; + $stylesheet = get_stylesheet(); if ( isset( $attributes['slug'] ) && isset( $attributes['theme'] ) && - get_stylesheet() === $attributes['theme'] + $stylesheet === $attributes['theme'] ) { $template_part_id = $attributes['theme'] . '//' . $attributes['slug']; $template_part_query = new WP_Query( @@ -65,16 +66,22 @@ function render_block_core_template_part( $attributes ) { } else { // Else, if the template part was provided by the active theme, // render the corresponding file content. - $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 - ? _inject_theme_attribute_in_block_template_content( $content ) - : ''; + if ( 0 === validate_file( $attributes['slug'] ) ) { + $themes = array( $stylesheet ); + $template = get_template(); + if ( $stylesheet !== $template ) { + $themes[] = $template; + } + + foreach ( $themes as $theme ) { + $theme_folders = get_block_theme_folders( $theme ); + $template_part_file_path = get_theme_file_path( '/' . $theme_folders['wp_template_part'] . '/' . $attributes['slug'] . '.html' ); + if ( file_exists( $template_part_file_path ) ) { + $content = (string) file_get_contents( $template_part_file_path ); + $content = '' !== $content ? _inject_theme_attribute_in_block_template_content( $content ) : ''; + break; + } + } } if ( '' !== $content && null !== $content ) {