Make WordPress Core


Ignore:
Timestamp:
09/15/2022 10:26:28 PM (3 years ago)
Author:
hellofromTonya
Message:

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.

File:
1 edited

Legend:

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

    r54104 r54184  
    553553
    554554    $theme          = $terms[0]->name;
    555     $has_theme_file = wp_get_theme()->get_stylesheet() === $theme &&
    556         null !== _get_block_template_file( $post->post_type, $post->post_name );
     555    $template_file = _get_block_template_file( $post->post_type, $post->post_name );
     556    $has_theme_file = wp_get_theme()->get_stylesheet() === $theme && null !== $template_file;
    557557
    558558    $origin = get_post_meta( $post->ID, 'origin', true );
     
    574574    $template->author         = $post->post_author;
    575575
     576    if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) {
     577        $template->post_types = $template_file['postTypes'];
     578    }
     579
    576580    if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $template->slug ] ) ) {
    577581        $template->is_custom = false;
     
    673677
    674678        if ( $post_type && ! $template->is_custom ) {
     679            continue;
     680        }
     681
     682        if ( $post_type &&
     683            isset( $template->post_types ) &&
     684            ! in_array( $post_type, $template->post_types, true )
     685        ) {
    675686            continue;
    676687        }
Note: See TracChangeset for help on using the changeset viewer.