Make WordPress Core


Ignore:
Timestamp:
09/20/2023 05:25:26 PM (8 months ago)
Author:
flixos90
Message:

Themes: Deprecate usage of TEMPLATEPATH and STYLESHEETPATH constants.

While generally the functions get_template_directory() and get_stylesheet_directory() were long recommended to use to get the parent or child theme directory, the TEMPLATEPATH and STYLESHEETPATH constants were still used in a few places in core, most importantly in template related logic.

The remaining usage was problematic as it prevented testability of certain key components of WordPress core.

This changeset replaces all remaining usage with the corresponding functions and effectively marks these constants as deprecated. It also adds test coverage accordingly and even unlocks some existing, previously commented out test coverage to work as expected.

Performance of the new approach has been benchmarked and shows no notable differences. Yet, given that the current theme directories are not expected to change within a regular WordPress page load, the get_template_directory() and get_stylesheet_directory() functions were amended with in-memory caching of the result, unless one of the defining values is being filtered.

Props thekt12, spacedmonkey, mukesh27, aaroncampbell, scribu, lloydbudd, cais, chipbennett, toscho, omarabid, CrazyJaco, DrewAPicture, obenland, wonderboymusic, nacin, helen, dd32, chriscct7, SergeyBiryukov, swissspidy, joemcgill, flixos90.
Fixes #18298.

File:
1 edited

Legend:

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

    r56414 r56635  
    158158 */
    159159function is_child_theme() {
    160     return ( TEMPLATEPATH !== STYLESHEETPATH );
     160    return get_template_directory() !== get_stylesheet_directory();
    161161}
    162162
     
    188188 *
    189189 * @since 1.5.0
     190 * @since 6.4.0 Memoizes filter execution so that it only runs once for the current theme.
     191 *
     192 * @global string $wp_stylesheet_path Current theme stylesheet directory path.
    190193 *
    191194 * @return string Path to active theme's stylesheet directory.
    192195 */
    193196function get_stylesheet_directory() {
    194     $stylesheet     = get_stylesheet();
    195     $theme_root     = get_theme_root( $stylesheet );
    196     $stylesheet_dir = "$theme_root/$stylesheet";
    197 
    198     /**
    199      * Filters the stylesheet directory path for the active theme.
    200      *
    201      * @since 1.5.0
    202      *
    203      * @param string $stylesheet_dir Absolute path to the active theme.
    204      * @param string $stylesheet     Directory name of the active theme.
    205      * @param string $theme_root     Absolute path to themes directory.
    206      */
    207     return apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root );
     197    global $wp_stylesheet_path;
     198
     199    if ( null === $wp_stylesheet_path ) {
     200        $stylesheet     = get_stylesheet();
     201        $theme_root     = get_theme_root( $stylesheet );
     202        $stylesheet_dir = "$theme_root/$stylesheet";
     203
     204        /**
     205         * Filters the stylesheet directory path for the active theme.
     206         *
     207         * @since 1.5.0
     208         *
     209         * @param string $stylesheet_dir Absolute path to the active theme.
     210         * @param string $stylesheet     Directory name of the active theme.
     211         * @param string $theme_root     Absolute path to themes directory.
     212         */
     213        $stylesheet_dir = apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root );
     214
     215        // If there are filter callbacks, force the logic to execute on every call.
     216        if ( has_filter( 'stylesheet' ) || has_filter( 'theme_root' ) || has_filter( 'stylesheet_directory' ) ) {
     217            return $stylesheet_dir;
     218        }
     219
     220        $wp_stylesheet_path = $stylesheet_dir;
     221    }
     222
     223    return $wp_stylesheet_path;
    208224}
    209225
     
    322338 *
    323339 * @since 1.5.0
     340 * @since 6.4.0 Memoizes filter execution so that it only runs once for the current theme.
     341 *
     342 * @global string $wp_template_path Current theme template directory path.
    324343 *
    325344 * @return string Path to active theme's template directory.
    326345 */
    327346function get_template_directory() {
    328     $template     = get_template();
    329     $theme_root   = get_theme_root( $template );
    330     $template_dir = "$theme_root/$template";
    331 
    332     /**
    333      * Filters the active theme directory path.
    334      *
    335      * @since 1.5.0
    336      *
    337      * @param string $template_dir The path of the active theme directory.
    338      * @param string $template     Directory name of the active theme.
    339      * @param string $theme_root   Absolute path to the themes directory.
    340      */
    341     return apply_filters( 'template_directory', $template_dir, $template, $theme_root );
     347    global $wp_template_path;
     348
     349    if ( null === $wp_template_path ) {
     350        $template     = get_template();
     351        $theme_root   = get_theme_root( $template );
     352        $template_dir = "$theme_root/$template";
     353
     354        /**
     355         * Filters the active theme directory path.
     356         *
     357         * @since 1.5.0
     358         *
     359         * @param string $template_dir The path of the active theme directory.
     360         * @param string $template     Directory name of the active theme.
     361         * @param string $theme_root   Absolute path to the themes directory.
     362         */
     363        $template_dir = apply_filters( 'template_directory', $template_dir, $template, $theme_root );
     364
     365        // If there are filter callbacks, force the logic to execute on every call.
     366        if ( has_filter( 'template' ) || has_filter( 'theme_root' ) || has_filter( 'template_directory' ) ) {
     367            return $template_dir;
     368        }
     369
     370        $wp_template_path = $template_dir;
     371    }
     372
     373    return $wp_template_path;
    342374}
    343375
     
    745777 * @global array                $sidebars_widgets
    746778 * @global array                $wp_registered_sidebars
     779 * @global string               $wp_stylesheet_path
     780 * @global string               $wp_template_path
    747781 *
    748782 * @param string $stylesheet Stylesheet name.
    749783 */
    750784function switch_theme( $stylesheet ) {
    751     global $wp_theme_directories, $wp_customize, $sidebars_widgets, $wp_registered_sidebars;
     785    global $wp_theme_directories, $wp_customize, $sidebars_widgets, $wp_registered_sidebars, $wp_stylesheet_path, $wp_template_path;
    752786
    753787    $requirements = validate_theme_requirements( $stylesheet );
     
    832866
    833867    update_option( 'theme_switched', $old_theme->get_stylesheet() );
     868
     869    /*
     870     * Reset globals to force refresh the next time these directories are
     871     * accessed via `get_stylesheet_directory()` / `get_template_directory()`.
     872     */
     873    $wp_stylesheet_path = null;
     874    $wp_template_path   = null;
    834875
    835876    /**
Note: See TracChangeset for help on using the changeset viewer.