From 39e1fb0528be62806b91b484f3d886fb9ddc23bb Mon Sep 17 00:00:00 2001 From: Isabel Brison Date: Tue, 1 Aug 2023 02:10:08 +0000 Subject: [PATCH] Editor: fix duplication in templates list. Excludes parent template when a child template is defined during template retrieval. Props oandregal, mukesh27, flixos90, bgardner. See #57756. git-svn-id: https://develop.svn.wordpress.org/trunk@56329 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/block-template-utils.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 9cec417a7a..226b2068da 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -360,6 +360,14 @@ function _get_block_templates_files( $template_type, $query = array() ) { continue; } + /* + * The child theme items (stylesheet) are processed before the parent theme's (template). + * If a child theme defines a template, prevent the parent template from being added to the list as well. + */ + if ( isset( $template_files[ $template_slug ] ) ) { + continue; + } + $new_template_item = array( 'slug' => $template_slug, 'path' => $template_file, @@ -370,7 +378,7 @@ function _get_block_templates_files( $template_type, $query = array() ) { if ( 'wp_template_part' === $template_type ) { $candidate = _add_block_template_part_area_info( $new_template_item ); if ( ! isset( $area ) || ( isset( $area ) && $area === $candidate['area'] ) ) { - $template_files[] = $candidate; + $template_files[ $template_slug ] = $candidate; } } @@ -380,13 +388,13 @@ function _get_block_templates_files( $template_type, $query = array() ) { ! $post_type || ( $post_type && isset( $candidate['postTypes'] ) && in_array( $post_type, $candidate['postTypes'], true ) ) ) { - $template_files[] = $candidate; + $template_files[ $template_slug ] = $candidate; } } } } - return $template_files; + return array_values( $template_files ); } /**