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/template.php

    r56357 r56635  
    685685 * Retrieves the name of the highest priority template file that exists.
    686686 *
    687  * Searches in the STYLESHEETPATH before TEMPLATEPATH and wp-includes/theme-compat
    688  * so that themes which inherit from a parent theme can just overload one file.
     687 * Searches in the stylesheet directory before the template directory and
     688 * wp-includes/theme-compat so that themes which inherit from a parent theme
     689 * can just overload one file.
    689690 *
    690691 * @since 2.7.0
     
    700701 */
    701702function locate_template( $template_names, $load = false, $load_once = true, $args = array() ) {
     703    $stylesheet_path = get_stylesheet_directory();
     704    $template_path   = get_template_directory();
     705    $is_child_theme  = $stylesheet_path !== $template_path;
     706
    702707    $located = '';
    703708    foreach ( (array) $template_names as $template_name ) {
     
    705710            continue;
    706711        }
    707         if ( file_exists( STYLESHEETPATH . '/' . $template_name ) ) {
    708             $located = STYLESHEETPATH . '/' . $template_name;
     712        if ( file_exists( $stylesheet_path . '/' . $template_name ) ) {
     713            $located = $stylesheet_path . '/' . $template_name;
    709714            break;
    710         } elseif ( is_child_theme() && file_exists( TEMPLATEPATH . '/' . $template_name ) ) {
    711             $located = TEMPLATEPATH . '/' . $template_name;
     715        } elseif ( $is_child_theme && file_exists( $template_path . '/' . $template_name ) ) {
     716            $located = $template_path . '/' . $template_name;
    712717            break;
    713718        } elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
Note: See TracChangeset for help on using the changeset viewer.