From a1d4276ba97405818578e5be0ac2f072d9bd2dc3 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 25 Aug 2022 09:44:19 +0400 Subject: [PATCH] Block Templates: Add the custom templates info for the template posts --- lib/compat/wordpress-6.1/block-template-utils.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.1/block-template-utils.php b/lib/compat/wordpress-6.1/block-template-utils.php index 03b93c83937467..ed1f105c0e5762 100644 --- a/lib/compat/wordpress-6.1/block-template-utils.php +++ b/lib/compat/wordpress-6.1/block-template-utils.php @@ -121,6 +121,13 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t continue; } + if ( $post_type && + isset( $template->post_types ) && + ! in_array( $post_type, $template->post_types, true ) + ) { + continue; + } + $query_result[] = $template; } if ( ! isset( $query['wp_id'] ) ) { @@ -264,8 +271,8 @@ function gutenberg_build_block_template_result_from_post( $post ) { $is_wp_suggestion = get_post_meta( $post->ID, 'is_wp_suggestion', true ); $theme = $terms[0]->name; - $has_theme_file = wp_get_theme()->get_stylesheet() === $theme && - null !== _get_block_template_file( $post->post_type, $post->post_name ); + $template_file = _get_block_template_file( $post->post_type, $post->post_name ); + $has_theme_file = wp_get_theme()->get_stylesheet() === $theme && null !== $template_file; $template = new WP_Block_Template(); $template->wp_id = $post->ID; @@ -288,6 +295,10 @@ function gutenberg_build_block_template_result_from_post( $post ) { $template->is_custom = false; } + if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) { + $template->post_types = $template_file['postTypes']; + } + if ( 'wp_template_part' === $post->post_type ) { $type_terms = get_the_terms( $post, 'wp_template_part_area' ); if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) {