Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor: Fix block template files query for a post-type #6468

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ function _get_block_templates_files( $template_type, $query = array() ) {
return null;
}

$default_template_types = array();
if ( 'wp_template' === $template_type ) {
$default_template_types = get_default_block_template_types();
}

// Prepare metadata from $query.
$slugs_to_include = isset( $query['slug__in'] ) ? $query['slug__in'] : array();
$slugs_to_skip = isset( $query['slug__not_in'] ) ? $query['slug__not_in'] : array();
Expand Down Expand Up @@ -383,12 +388,19 @@ function _get_block_templates_files( $template_type, $query = array() ) {

if ( 'wp_template' === $template_type ) {
$candidate = _add_block_template_info( $new_template_item );
$is_custom = ! isset( $default_template_types[ $candidate['slug'] ] );

if (
! $post_type ||
( $post_type && isset( $candidate['postTypes'] ) && in_array( $post_type, $candidate['postTypes'], true ) )
) {
$template_files[ $template_slug ] = $candidate;
}

// The custom templates with no associated post types are available for all post types.
if ( $post_type && ! isset( $candidate['postTypes'] ) && $is_custom ) {
$template_files[ $template_slug ] = $candidate;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:paragraph -->
<p>Custom Hero template</p>
<!-- /wp:paragraph -->
1 change: 1 addition & 0 deletions tests/phpunit/tests/block-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ public function test_get_block_templates_paths_dir_exists() {
// Templates in the current theme.
$templates = array(
'parts/small-header.html',
'templates/custom-hero-template.html',
'templates/custom-single-post-template.html',
'templates/index.html',
'templates/page-home.html',
Expand Down
6 changes: 6 additions & 0 deletions tests/phpunit/tests/blocks/getBlockTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ public function data_get_block_templates_returns_unique_entities() {
/**
* @dataProvider data_get_block_templates_should_respect_posttypes_property
* @ticket 55881
* @ticket 61110
*
* @param string $post_type Post type for query.
* @param array $expected Expected template IDs.
Expand All @@ -204,19 +205,24 @@ public function test_get_block_templates_should_respect_posttypes_property( $pos
/**
* Data provider.
*
* The `custom-hero-template` is intentionally omitted from the theme.json's `customTemplates`.
* See: https://core.trac.wordpress.org/ticket/61110.
*
* @return array
*/
public function data_get_block_templates_should_respect_posttypes_property() {
return array(
'post' => array(
'post_type' => 'post',
'expected' => array(
'block-theme//custom-hero-template',
'block-theme//custom-single-post-template',
),
),
'page' => array(
'post_type' => 'page',
'expected' => array(
'block-theme//custom-hero-template',
'block-theme//page-home',
),
),
Expand Down
Loading