Make WordPress Core


Ignore:
Timestamp:
10/27/2023 06:34:11 PM (7 months ago)
Author:
flixos90
Message:

Themes: Skip wrapping block template for singular content with a main query loop when the template was injected from outside the current theme.

As a follow up to [56507], this fixes a bug that could occur for instance when plugins hijack the block template detection process to inject their own block template with entirely custom logic.

Props afragen, hellofromTonya, costdev, mukesh27, huzaifaalmesbah, flixos90.
Merges [57019] to the 6.4 branch.
Fixes #59736.
See #58154.

Location:
branches/6.4
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.4

  • branches/6.4/src/wp-includes/block-template.php

    r56507 r57020  
    209209 * @since 5.8.0
    210210 *
     211 * @global string   $_wp_current_template_id
    211212 * @global string   $_wp_current_template_content
    212213 * @global WP_Embed $wp_embed
     
    216217 */
    217218function get_the_block_template_html() {
    218     global $_wp_current_template_content, $wp_embed, $wp_query;
     219    global $_wp_current_template_id, $_wp_current_template_content, $wp_embed, $wp_query;
    219220
    220221    if ( ! $_wp_current_template_content ) {
     
    243244     * loop, it would not cause errors since it would use a cloned instance and go through the same loop of a single
    244245     * post, within the actual main query loop.
     246     *
     247     * This special logic should be skipped if the current template does not come from the current theme, in which case
     248     * it has been injected by a plugin by hijacking the block template loader mechanism. In that case, entirely custom
     249     * logic may be applied which is unpredictable and therefore safer to omit this special handling on.
    245250     */
    246     if ( is_singular() && 1 === $wp_query->post_count && have_posts() ) {
     251    if (
     252        $_wp_current_template_id &&
     253        str_starts_with( $_wp_current_template_id, get_stylesheet() . '//' ) &&
     254        is_singular() &&
     255        1 === $wp_query->post_count &&
     256        have_posts()
     257    ) {
    247258        while ( have_posts() ) {
    248259            the_post();
Note: See TracChangeset for help on using the changeset viewer.