Skip to content

Commit

Permalink
Move filtering logic inside gutenberg_get_block_templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Oct 21, 2021
1 parent c6e6b73 commit 22a4f61
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
34 changes: 28 additions & 6 deletions lib/full-site-editing/block-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
*/
Expand All @@ -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,
Expand Down Expand Up @@ -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' ),
Expand All @@ -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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
10 changes: 1 addition & 9 deletions lib/full-site-editing/page-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 22a4f61

Please sign in to comment.