Make WordPress Core

Changeset 58323


Ignore:
Timestamp:
06/04/2024 08:13:01 AM (7 weeks ago)
Author:
ellatrix
Message:

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

See https://github.com/WordPress/gutenberg/pull/61244.
See https://github.com/WordPress/wordpress-develop/pull/6468.

Fixes #61110.

Props mamaduka, mukesh27, grantmkin, vcanales, ellatrix, oandregal.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-template-utils.php

    r58291 r58323  
    361361    if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) {
    362362        return null;
     363    }
     364
     365    $default_template_types = array();
     366    if ( 'wp_template' === $template_type ) {
     367        $default_template_types = get_default_block_template_types();
    363368    }
    364369
     
    426431            if ( 'wp_template' === $template_type ) {
    427432                $candidate = _add_block_template_info( $new_template_item );
     433                $is_custom = ! isset( $default_template_types[ $candidate['slug'] ] );
     434
    428435                if (
    429436                    ! $post_type ||
    430437                    ( $post_type && isset( $candidate['postTypes'] ) && in_array( $post_type, $candidate['postTypes'], true ) )
    431438                ) {
     439                    $template_files[ $template_slug ] = $candidate;
     440                }
     441
     442                // The custom templates with no associated post types are available for all post types.
     443                if ( $post_type && ! isset( $candidate['postTypes'] ) && $is_custom ) {
    432444                    $template_files[ $template_slug ] = $candidate;
    433445                }
  • trunk/tests/phpunit/tests/block-template.php

    r57215 r58323  
    400400        $templates = array(
    401401            'parts/small-header.html',
     402            'templates/custom-hero-template.html',
    402403            'templates/custom-single-post-template.html',
    403404            'templates/index.html',
  • trunk/tests/phpunit/tests/blocks/getBlockTemplates.php

    r56971 r58323  
    189189     * @dataProvider data_get_block_templates_should_respect_posttypes_property
    190190     * @ticket 55881
     191     * @ticket 61110
    191192     *
    192193     * @param string $post_type Post type for query.
     
    204205    /**
    205206     * Data provider.
     207     *
     208     * The `custom-hero-template` is intentionally omitted from the theme.json's `customTemplates`.
     209     * See: https://core.trac.wordpress.org/ticket/61110.
    206210     *
    207211     * @return array
     
    212216                'post_type' => 'post',
    213217                'expected'  => array(
     218                    'block-theme//custom-hero-template',
    214219                    'block-theme//custom-single-post-template',
    215220                ),
     
    218223                'post_type' => 'page',
    219224                'expected'  => array(
     225                    'block-theme//custom-hero-template',
    220226                    'block-theme//page-home',
    221227                ),
Note: See TracChangeset for help on using the changeset viewer.