Skip to content

Commit

Permalink
Improve logic in render_block_core_template_part. (#50636)
Browse files Browse the repository at this point in the history
* Improve logic in `render_block_core_template_part`.

* Use a foreach loop.

* Apply suggestions from code review

Co-authored-by: George Mamadashvili <georgemamadashvili@gmail.com>

---------

Co-authored-by: George Mamadashvili <georgemamadashvili@gmail.com>
  • Loading branch information
spacedmonkey and Mamaduka authored Jun 19, 2023
1 parent ce4dd61 commit 5d371bc
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions packages/block-library/src/template-part/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 ) {
Expand Down

0 comments on commit 5d371bc

Please sign in to comment.