Make WordPress Core

Changeset 57685


Ignore:
Timestamp:
02/21/2024 07:24:12 PM (3 years 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.

Location:
trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/theme.php

    r57644 r57685  
    11611161 * @since 5.2.0
    11621162 *
     1163 * @global string $wp_stylesheet_path Path to current theme's stylesheet directory.
     1164 * @global string $wp_template_path   Path to current theme's template directory.
     1165 *
    11631166 * @param string $theme    Single theme to resume.
    11641167 * @param string $redirect Optional. URL to redirect to. Default empty string.
    … …  
    11671170 */
    11681171function resume_theme( $theme, $redirect = '' ) {
     1172        global $wp_stylesheet_path, $wp_template_path;
     1173
    11691174        list( $extension ) = explode( '/', $theme );
    11701175
    … …  
    11741179         */
    11751180        if ( ! empty( $redirect ) ) {
    1176                 $stylesheet_path = get_stylesheet_directory();
    1177                 $template_path   = get_template_directory();
    1178 
    11791181                $functions_path = '';
    1180                 if ( str_contains( $stylesheet_path, $extension ) ) {
    1181                         $functions_path = $stylesheet_path . '/functions.php';
    1182                 } elseif ( str_contains( $template_path, $extension ) ) {
    1183                         $functions_path = $template_path . '/functions.php';
     1182                if ( str_contains( $wp_stylesheet_path, $extension ) ) {
     1183                        $functions_path = $wp_stylesheet_path . '/functions.php';
     1184                } elseif ( str_contains( $wp_template_path, $extension ) ) {
     1185                        $functions_path = $wp_template_path . '/functions.php';
    11841186                }
    11851187
  • trunk/src/wp-includes/comment-template.php

    r57648 r57685  
    13911391 * @since 1.5.0
    13921392 *
    1393  * @global WP_Query   $wp_query         WordPress Query object.
    1394  * @global WP_Post    $post             Global post object.
    1395  * @global wpdb       $wpdb             WordPress database abstraction object.
     1393 * @global WP_Query   $wp_query           WordPress Query object.
     1394 * @global WP_Post    $post               Global post object.
     1395 * @global wpdb       $wpdb               WordPress database abstraction object.
    13961396 * @global int        $id
    1397  * @global WP_Comment $comment          Global comment object.
     1397 * @global WP_Comment $comment            Global comment object.
    13981398 * @global string     $user_login
    13991399 * @global string     $user_identity
    14001400 * @global bool       $overridden_cpage
    14011401 * @global bool       $withcomments
     1402 * @global string     $wp_stylesheet_path Path to current theme's stylesheet directory.
     1403 * @global string     $wp_template_path   Path to current theme's template directory.
    14021404 *
    14031405 * @param string $file              Optional. The file to load. Default '/comments.php'.
    … …  
    14061408 */
    14071409function comments_template( $file = '/comments.php', $separate_comments = false ) {
    1408         global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_identity, $overridden_cpage;
     1410        global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_identity, $overridden_cpage, $wp_stylesheet_path, $wp_template_path;
    14091411
    14101412        if ( ! ( is_single() || is_page() || $withcomments ) || empty( $post ) ) {
    … …  
    16011603        }
    16021604
    1603         $stylesheet_path = get_stylesheet_directory();
    1604         $template_path   = get_template_directory();
    1605 
    1606         $theme_template = $stylesheet_path . $file;
     1605        $theme_template = trailingslashit( $wp_stylesheet_path ) . $file;
    16071606
    16081607        /**
    … …  
    16171616        if ( file_exists( $include ) ) {
    16181617                require $include;
    1619         } elseif ( file_exists( $template_path . $file ) ) {
    1620                 require $template_path . $file;
     1618        } elseif ( file_exists( trailingslashit( $wp_template_path ) . $file ) ) {
     1619                require trailingslashit( $wp_template_path ) . $file;
    16211620        } else { // Backward compat code will be removed in a future release.
    16221621                require ABSPATH . WPINC . '/theme-compat/comments.php';
  • trunk/src/wp-includes/load.php

    r57608 r57685  
    10471047 * @access private
    10481048 *
    1049  * @global string $pagenow The filename of the current screen.
     1049 * @global string $pagenow            The filename of the current screen.
     1050 * @global string $wp_stylesheet_path Path to current theme's stylesheet directory.
     1051 * @global string $wp_template_path   Path to current theme's template directory.
    10501052 *
    10511053 * @return string[] Array of absolute paths to theme directories.
    10521054 */
    10531055function wp_get_active_and_valid_themes() {
    1054         global $pagenow;
     1056        global $pagenow, $wp_stylesheet_path, $wp_template_path;
    10551057
    10561058        $themes = array();
    … …  
    10601062        }
    10611063
    1062         $stylesheet_path = get_stylesheet_directory();
    1063         $template_path   = get_template_directory();
    1064 
    1065         if ( $template_path !== $stylesheet_path ) {
    1066                 $themes[] = $stylesheet_path;
    1067         }
    1068 
    1069         $themes[] = $template_path;
     1064        if ( is_child_theme() ) {
     1065                $themes[] = $wp_stylesheet_path;
     1066        }
     1067
     1068        $themes[] = $wp_template_path;
    10701069
    10711070        /*
  • trunk/src/wp-includes/template.php

    r57223 r57685  
    681681
    682682/**
     683 * Set up the globals used for template loading.
     684 *
     685 * @since 6.5.0
     686 *
     687 * @global string $wp_stylesheet_path Path to current theme's stylesheet directory.
     688 * @global string $wp_template_path   Path to current theme's template directory.
     689 */
     690function wp_set_template_globals() {
     691        global $wp_stylesheet_path, $wp_template_path;
     692
     693        $wp_stylesheet_path = get_stylesheet_directory();
     694        $wp_template_path   = get_template_directory();
     695}
     696
     697/**
    683698 * Retrieves the name of the highest priority template file that exists.
    684699 *
    … …  
    689704 * @since 2.7.0
    690705 * @since 5.5.0 The `$args` parameter was added.
     706 *
     707 * @global string $wp_stylesheet_path Path to current theme's stylesheet directory.
     708 * @global string $wp_template_path   Path to current theme's template directory.
    691709 *
    692710 * @param string|array $template_names Template file(s) to search for, in order.
    … …  
    699717 */
    700718function locate_template( $template_names, $load = false, $load_once = true, $args = array() ) {
    701         $stylesheet_path = get_stylesheet_directory();
    702         $template_path   = get_template_directory();
    703         $is_child_theme  = $stylesheet_path !== $template_path;
     719        global $wp_stylesheet_path, $wp_template_path;
     720
     721        if ( ! isset( $wp_stylesheet_path ) || ! isset( $wp_template_path ) ) {
     722                wp_set_template_globals();
     723        }
     724
     725        $is_child_theme = is_child_theme();
    704726
    705727        $located = '';
    … …  
    708730                        continue;
    709731                }
    710                 if ( file_exists( $stylesheet_path . '/' . $template_name ) ) {
    711                         $located = $stylesheet_path . '/' . $template_name;
     732                if ( file_exists( $wp_stylesheet_path . '/' . $template_name ) ) {
     733                        $located = $wp_stylesheet_path . '/' . $template_name;
    712734                        break;
    713                 } elseif ( $is_child_theme && file_exists( $template_path . '/' . $template_name ) ) {
    714                         $located = $template_path . '/' . $template_name;
     735                } elseif ( $is_child_theme && file_exists( $wp_template_path . '/' . $template_name ) ) {
     736                        $located = $wp_template_path . '/' . $template_name;
    715737                        break;
    716738                } elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
  • trunk/src/wp-includes/theme.php

    r57608 r57685  
    154154 *
    155155 * @since 3.0.0
     156 * @since 6.5.0 Makes use of global template variables.
     157 *
     158 * @global string $wp_stylesheet_path Path to current theme's stylesheet directory.
     159 * @global string $wp_template_path   Path to current theme's template directory.
    156160 *
    157161 * @return bool True if a child theme is in use, false otherwise.
    158162 */
    159163function is_child_theme() {
    160         return get_template_directory() !== get_stylesheet_directory();
     164        global $wp_stylesheet_path, $wp_template_path;
     165
     166        return $wp_stylesheet_path !== $wp_template_path;
    161167}
    162168
    … …  
    836842
    837843        update_option( 'theme_switched', $old_theme->get_stylesheet() );
     844
     845        /*
     846         * Reset template globals when switching themes outside of a switched blog
     847         * context to ensure templates will be loaded from the new theme.
     848         */
     849        if ( ! is_multisite() || ! ms_is_switched() ) {
     850                wp_set_template_globals();
     851        }
    838852
    839853        // Clear pattern caches.
  • trunk/src/wp-settings.php

    r57661 r57685  
    609609do_action( 'setup_theme' );
    610610
    611 // Define the template related constants.
     611// Define the template related constants and globals.
    612612wp_templating_constants();
     613wp_set_template_globals();
    613614
    614615// Load the default text localization domain.
  • trunk/tests/phpunit/includes/abstract-testcase.php

    r57608 r57685  
    190190                 */
    191191                $GLOBALS['wp_sitemaps'] = null;
     192
     193                // Reset template globals.
     194                $GLOBALS['wp_stylesheet_path'] = null;
     195                $GLOBALS['wp_template_path']   = null;
    192196
    193197                $this->unregister_all_meta_keys();
  • trunk/tests/phpunit/tests/comment/metaCache.php

    r56549 r57685  
    66        protected $i       = 0;
    77        protected $queries = 0;
     8
     9        /**
     10         * Performs setup tasks for every test.
     11         */
     12        public function set_up() {
     13                parent::set_up();
     14                switch_theme( 'default' );
     15        }
    816
    917        /**
  • 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.