Make WordPress Core


Ignore:
Timestamp:
09/20/2023 05:25:26 PM (13 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/tests/phpunit/tests/template.php

    r56547 r56635  
    628628            ),
    629629        );
     630    }
     631
     632    /**
     633     * Tests that `locate_template()` uses the current theme even after switching the theme.
     634     *
     635     * @ticket 18298
     636     *
     637     * @covers ::locate_template
     638     */
     639    public function test_locate_template_uses_current_theme() {
     640        $themes = wp_get_themes();
     641
     642        // Look for parent themes with an index.php template.
     643        $relevant_themes = array();
     644        foreach ( $themes as $theme ) {
     645            if ( $theme->get_stylesheet() !== $theme->get_template() ) {
     646                continue;
     647            }
     648            $php_templates = $theme['Template Files'];
     649            if ( ! isset( $php_templates['index.php'] ) ) {
     650                continue;
     651            }
     652            $relevant_themes[] = $theme;
     653        }
     654        if ( count( $relevant_themes ) < 2 ) {
     655            $this->markTestSkipped( 'Test requires at least two parent themes with an index.php template.' );
     656        }
     657
     658        $template_names = array( 'index.php' );
     659
     660        $old_theme = $relevant_themes[0];
     661        $new_theme = $relevant_themes[1];
     662
     663        switch_theme( $old_theme->get_stylesheet() );
     664        $this->assertSame( $old_theme->get_stylesheet_directory() . '/index.php', locate_template( $template_names ), 'Incorrect index template found in initial theme.' );
     665
     666        switch_theme( $new_theme->get_stylesheet() );
     667        $this->assertSame( $new_theme->get_stylesheet_directory() . '/index.php', locate_template( $template_names ), 'Incorrect index template found in theme after switch.' );
    630668    }
    631669
Note: See TracChangeset for help on using the changeset viewer.