From e515987454fa0c104101032b916f045de11417e3 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 21 Oct 2021 15:01:48 +0400 Subject: [PATCH] Move filtering logic inside gutenberg_get_block_templates --- lib/full-site-editing/block-templates.php | 34 +++++++++++++++---- ...ss-gutenberg-rest-templates-controller.php | 16 ++------- lib/full-site-editing/page-templates.php | 10 +----- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/lib/full-site-editing/block-templates.php b/lib/full-site-editing/block-templates.php index 1969bb6da087cf..5d67b13eb4edab 100644 --- a/lib/full-site-editing/block-templates.php +++ b/lib/full-site-editing/block-templates.php @@ -329,9 +329,10 @@ function _gutenberg_build_template_result_from_post( $post ) { * @param array $query { * Optional. Arguments to retrieve templates. * - * @type array $slug__in List of slugs to include. - * @type int $wp_id Post ID of customized template. - * @type string $area A 'wp_template_part_area' taxonomy value to filter by (for wp_template_part template type only). + * @type array $slug__in List of slugs to include. + * @type int $wp_id Post ID of customized template. + * @type string $area A 'wp_template_part_area' taxonomy value to filter by (for wp_template_part template type only). + * @type string $post_type Post type to get the templates for. * } * @param array $template_type wp_template or wp_template_part. * @@ -352,6 +353,7 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t * * @type array $slug__in List of slugs to include. * @type int $wp_id Post ID of customized template. + * @type string $post_type Post type to get the templates for. * } * @param array $template_type wp_template or wp_template_part. */ @@ -360,6 +362,7 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t return $templates; } + $post_type = isset( $query['post_type'] ) ? $query['post_type'] : ''; $wp_query_args = array( 'post_status' => array( 'auto-draft', 'draft', 'publish' ), 'post_type' => $template_type, @@ -399,14 +402,33 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t foreach ( $template_query->posts as $post ) { $template = _gutenberg_build_template_result_from_post( $post ); - if ( ! is_wp_error( $template ) ) { - $query_result[] = $template; + if ( is_wp_error( $template ) ) { + continue; + } + + if ( $post_type && ! $template->is_custom ) { + continue; } + + $query_result[] = $template; } if ( ! isset( $query['wp_id'] ) ) { $template_files = _gutenberg_get_template_files( $template_type ); foreach ( $template_files as $template_file ) { + $template = _gutenberg_build_template_result_from_file( $template_file, $template_type ); + + if ( $post_type && ! $template->is_custom ) { + continue; + } + + if ( $post_type && + isset( $template->post_types ) && + ! in_array( $post_type, $template->post_types, true ) + ) { + continue; + } + $is_not_custom = false === array_search( wp_get_theme()->get_stylesheet() . '//' . $template_file['slug'], array_column( $query_result, 'id' ), @@ -418,7 +440,7 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t ! isset( $query['area'] ) || $template_file['area'] === $query['area']; $should_include = $is_not_custom && $fits_slug_query && $fits_area_query; if ( $should_include ) { - $query_result[] = _gutenberg_build_template_result_from_file( $template_file, $template_type ); + $query_result[] = $template; } } } diff --git a/lib/full-site-editing/class-gutenberg-rest-templates-controller.php b/lib/full-site-editing/class-gutenberg-rest-templates-controller.php index cb413ab2ead210..912f7a5761fe84 100644 --- a/lib/full-site-editing/class-gutenberg-rest-templates-controller.php +++ b/lib/full-site-editing/class-gutenberg-rest-templates-controller.php @@ -141,22 +141,12 @@ public function get_items( $request ) { if ( isset( $request['area'] ) ) { $query['area'] = $request['area']; } + if ( isset( $request['post_type'] ) ) { + $query['post_type'] = $request['post_type']; + } $templates = array(); foreach ( gutenberg_get_block_templates( $query, $this->post_type ) as $template ) { - // Maybe we should do this on `get_block_templates` level based on query? - if ( isset( $request['post_type'] ) && ! $template->is_custom ) { - continue; - } - - if ( - isset( $request['post_type'] ) && - isset( $template->post_types ) && - ! in_array( $request['post_type'], $template->post_types, true ) - ) { - continue; - } - $data = $this->prepare_item_for_response( $template, $request ); $templates[] = $this->prepare_response_for_collection( $data ); } diff --git a/lib/full-site-editing/page-templates.php b/lib/full-site-editing/page-templates.php index c93843747b246d..1e2fc63264b1b2 100644 --- a/lib/full-site-editing/page-templates.php +++ b/lib/full-site-editing/page-templates.php @@ -19,16 +19,8 @@ function gutenberg_load_block_page_templates( $templates, $theme, $post, $post_t return $templates; } - $block_templates = gutenberg_get_block_templates( array(), 'wp_template' ); + $block_templates = gutenberg_get_block_templates( array( 'post_type' => $post_type ), 'wp_template' ); foreach ( $block_templates as $template ) { - if ( ! $template->is_custom ) { - continue; - } - - if ( isset( $template->post_types ) && ! in_array( $post_type, $template->post_types, true ) ) { - continue; - } - $templates[ $template->slug ] = $template->title; }