Editor: Fix get_block_templates() to get templates for a post type.

When a post type is passed to `get_block_templates()` in the query, return only the templates that match that post type.

Fixes an issue where:
* when a block template provided by a theme has been edited by the user
* and that template has specific defined `postTypes`
* but after editing, the template was available for all post types.

Follow-up to [52062].

Props aljullu, bernhard-reiter, hellofromTonya.
Fixes #55881.
Built from https://develop.svn.wordpress.org/trunk@54184


git-svn-id: http://core.svn.wordpress.org/trunk@53743 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
hellofromTonya 2022-09-15 22:28:13 +00:00
parent 335203e971
commit 47d61a73ef
2 changed files with 14 additions and 3 deletions

View File

@ -552,8 +552,8 @@ function _build_block_template_result_from_post( $post ) {
}
$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;
$origin = get_post_meta( $post->ID, 'origin', true );
@ -573,6 +573,10 @@ function _build_block_template_result_from_post( $post ) {
$template->is_custom = true;
$template->author = $post->post_author;
if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) {
$template->post_types = $template_file['postTypes'];
}
if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $template->slug ] ) ) {
$template->is_custom = false;
}
@ -675,6 +679,13 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' )
continue;
}
if ( $post_type &&
isset( $template->post_types ) &&
! in_array( $post_type, $template->post_types, true )
) {
continue;
}
$query_result[] = $template;
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.1-alpha-54183';
$wp_version = '6.1-alpha-54184';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.