Make WordPress Core


Ignore:
Timestamp:
10/27/2023 06:34:11 PM (12 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/tests/phpunit/tests/block-template.php

    r57010 r57020  
    2020
    2121    public function tear_down() {
    22         global $_wp_current_template_content;
    23         unset( $_wp_current_template_content );
     22        global $_wp_current_template_id, $_wp_current_template_content;
     23        unset( $_wp_current_template_id, $_wp_current_template_content );
    2424
    2525        parent::tear_down();
     
    194194     *
    195195     * @ticket 58154
     196     * @ticket 59736
    196197     * @covers ::get_the_block_template_html
    197198     */
    198199    public function test_get_the_block_template_html_enforces_singular_query_loop() {
    199         global $_wp_current_template_content, $wp_query, $wp_the_query;
     200        global $_wp_current_template_id, $_wp_current_template_content, $wp_query, $wp_the_query;
    200201
    201202        // Register test block to log `in_the_loop()` results.
     
    208209        $wp_the_query = $wp_query;
    209210
     211        // Force a template ID that is for the current stylesheet.
     212        $_wp_current_template_id = get_stylesheet() . '//single';
    210213        // Use block template that just renders post title and the above test block.
    211214        $_wp_current_template_content = '<!-- wp:post-title /--><!-- wp:test/in-the-loop-logger /-->';
     
    228231     */
    229232    public function test_get_the_block_template_html_does_not_generally_enforce_loop() {
    230         global $_wp_current_template_content, $wp_query, $wp_the_query;
     233        global $_wp_current_template_id, $_wp_current_template_content, $wp_query, $wp_the_query;
    231234
    232235        // Register test block to log `in_the_loop()` results.
     
    248251        );
    249252        $wp_the_query = $wp_query;
     253
     254        // Force a template ID that is for the current stylesheet.
     255        $_wp_current_template_id = get_stylesheet() . '//home';
    250256
    251257        /*
     
    278284
    279285    /**
     286     * Tests that `get_the_block_template_html()` does not start the main query loop when on a template that is not from the current theme.
     287     *
     288     * @ticket 58154
     289     * @ticket 59736
     290     * @covers ::get_the_block_template_html
     291     */
     292    public function test_get_the_block_template_html_skips_singular_query_loop_when_non_theme_template() {
     293        global $_wp_current_template_id, $_wp_current_template_content, $wp_query, $wp_the_query;
     294
     295        // Register test block to log `in_the_loop()` results.
     296        $in_the_loop_logs = array();
     297        $this->register_in_the_loop_logger_block( $in_the_loop_logs );
     298
     299        // Set main query to single post.
     300        $post_id      = self::factory()->post->create( array( 'post_title' => 'A single post' ) );
     301        $wp_query     = new WP_Query( array( 'p' => $post_id ) );
     302        $wp_the_query = $wp_query;
     303
     304        // Force a template ID that is not for the current stylesheet.
     305        $_wp_current_template_id = 'some-plugin-slug//single';
     306        // Use block template that just renders post title and the above test block.
     307        $_wp_current_template_content = '<!-- wp:post-title /--><!-- wp:test/in-the-loop-logger /-->';
     308
     309        $output = get_the_block_template_html();
     310        $this->unregister_in_the_loop_logger_block();
     311        $this->assertSame( array( false ), $in_the_loop_logs, 'Main query loop was triggered despite a custom block template outside the current theme being used' );
     312    }
     313
     314    /**
    280315     * @ticket 58319
    281316     *
Note: See TracChangeset for help on using the changeset viewer.