Make WordPress Core


Ignore:
Timestamp:
02/21/2024 07:24:12 PM (10 months ago)
Author:
joemcgill
Message:

Themes: Use original template paths when switching blogs.

This fixes a bug introduced by [57129] and [56635] in which deprecating the previous TEMPLATEPATH and STYLESHEETPATH constants in favor of get_template_directory() and get_stylesheet_directory() functions caused the active theme template path to change when using switch_to_blog().

This introduces a new function, wp_set_template_globals(), which is called during the bootstrap process to store the template paths to new globals values $wp_template_path and $wp_stylesheet_path. This restores behavior to how things worked prior to [56635] but retains the ability for template values to be reset for better testability.

Related #18298, #60025.

Props joemcgill, flixos90, mukesh27, swissspidy, manfcarlo, metropolis_john, jeremyfelt.
Fixes #60290.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/theme.php

    r57244 r57685  
    13061306        $this->assertSame( $new_root . '/test-parent', $path2, 'The new template path is not correct' );
    13071307    }
     1308
     1309    /**
     1310     * Tests that switch_to_blog() uses the original template path.
     1311     *
     1312     * @ticket 60290
     1313     *
     1314     * @group ms-required
     1315     *
     1316     * @covers ::locate_template
     1317     */
     1318    public function test_switch_to_blog_uses_original_template_path() {
     1319        $old_theme     = wp_get_theme();
     1320        $template_path = locate_template( 'index.php' );
     1321
     1322        $blog_id = self::factory()->blog->create();
     1323        switch_to_blog( $blog_id );
     1324
     1325        switch_theme( 'block-theme' );
     1326        $new_template_path = locate_template( 'index.php' );
     1327
     1328        // Cleanup.
     1329        restore_current_blog();
     1330        switch_theme( $old_theme->get_stylesheet() );
     1331
     1332        $this->assertSame( $template_path, $new_template_path, 'Switching blogs switches the template path' );
     1333    }
    13081334}
Note: See TracChangeset for help on using the changeset viewer.