Skip to content

Commit

Permalink
Make gutenberg_get_block_template fall back to use the template hiera…
Browse files Browse the repository at this point in the history
…rchy
  • Loading branch information
ockham committed Dec 9, 2021
1 parent bd3bddc commit 1b176c0
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions lib/compat/wordpress-5.9/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,32 +756,17 @@ function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) {
return null;
}
list( $theme, $slug ) = $parts;
$wp_query_args = array(
'post_name__in' => array( $slug ),
'post_type' => $template_type,
'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ),
'posts_per_page' => 1,
'no_found_rows' => true,
'tax_query' => array(
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => $theme,
),
),
);
$template_query = new WP_Query( $wp_query_args );
$posts = $template_query->posts;

if ( count( $posts ) > 0 ) {
$template = _build_block_template_result_from_post( $posts[0] );

if ( ! is_wp_error( $template ) ) {
return $template;
}
$block_template = resolve_block_template( $slug, array(), '' );
if ( ! $block_template ) {
$block_template = resolve_block_template( 'index', array(), '' );
}

$block_template = get_block_file_template( $id, $template_type );
// This might give us a fallback template with a different ID,
// so we have to override it to make sure it's correct.
$block_template->id = $id;
$block_template->slug = $slug;
// TODO: Infer title properly.
$block_template->title = $slug;
// TODO: Set other fields, such as description, etc?
/**
* Filters the queried block template object after it's been fetched.
Expand Down

0 comments on commit 1b176c0

Please sign in to comment.