Make WordPress Core


Ignore:
Timestamp:
12/04/2023 07:57:30 PM (3 years ago)
Author:
jorbin
Message:

Themes: Remove memoization from stylesheet and theme directories.

This fixes bugs introduced in [56635] whereby the template or stylesheet path could be memoized incorrectly if get_template_directory() or get_stylesheet_directory() were called before the theme has been fully initialized.

Reviewed by Jorbin.
Merges [57129] to 6.4 branch.

Props partyfrikadelle, coreyw, kdowns, rebasaurus, meta4, flixos90, mukesh27, joemcgill, icaleb.
Fixes #59847.

Location:
branches/6.4
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.4

  • branches/6.4/tests/phpunit/tests/theme.php

    r57010 r57156  
    11801180                }
    11811181        }
     1182
     1183        /**
     1184         * Make sure filters added after the initial call are fired.
     1185         *
     1186         * @ticket 59847
     1187         *
     1188         * @covers ::get_stylesheet_directory
     1189         */
     1190        public function test_get_stylesheet_directory_filters_apply() {
     1191                // Call the function prior to the filter being added.
     1192                get_stylesheet_directory();
     1193
     1194                $expected = 'test_root/dir';
     1195
     1196                // Add the filer.
     1197                add_filter(
     1198                        'stylesheet_directory',
     1199                        function () use ( $expected ) {
     1200                                return $expected;
     1201                        }
     1202                );
     1203
     1204                $this->assertSame( $expected, get_stylesheet_directory() );
     1205        }
     1206
     1207        /**
     1208         * Make sure filters added after the initial call are fired.
     1209         *
     1210         * @ticket 59847
     1211         *
     1212         * @covers ::get_template_directory
     1213         */
     1214        public function test_get_template_directory_filters_apply() {
     1215                // Call the function prior to the filter being added.
     1216                get_template_directory();
     1217
     1218                $expected = 'test_root/dir';
     1219
     1220                // Add the filer.
     1221                add_filter(
     1222                        'template_directory',
     1223                        function () use ( $expected ) {
     1224                                return $expected;
     1225                        }
     1226                );
     1227
     1228                $this->assertSame( $expected, get_template_directory() );
     1229        }
     1230
     1231        /**
     1232         * Make sure get_stylesheet_directory uses the correct path when the root theme dir changes.
     1233         *
     1234         * @ticket 59847
     1235         *
     1236         * @covers ::get_stylesheet_directory
     1237         */
     1238        public function test_get_stylesheet_directory_uses_registered_theme_dir() {
     1239                $old_theme = wp_get_theme();
     1240
     1241                switch_theme( 'test' );
     1242
     1243                $old_root = get_theme_root( 'test' );
     1244                $path1    = get_stylesheet_directory();
     1245
     1246                $new_root = DIR_TESTDATA . '/themedir2';
     1247                register_theme_directory( $new_root );
     1248
     1249                // Mock the stylesheet root option to mimic that the active root has changed.
     1250                add_filter(
     1251                        'pre_option_stylesheet_root',
     1252                        function () use ( $new_root ) {
     1253                                return $new_root;
     1254                        }
     1255                );
     1256
     1257                $path2 = get_stylesheet_directory();
     1258
     1259                // Cleanup.
     1260                switch_theme( $old_theme->get_stylesheet() );
     1261
     1262                $this->assertEquals( $old_root . '/test', $path1, 'The original stylesheet path is not correct' );
     1263                $this->assertEquals( $new_root . '/test', $path2, 'The new stylesheet path is not correct' );
     1264        }
     1265
     1266        /**
     1267         * Make sure get_template_directory uses the correct path when the root theme dir changes.
     1268         *
     1269         * @ticket 59847
     1270         *
     1271         * @covers ::get_template_directory
     1272         */
     1273        public function test_get_template_directory_uses_registered_theme_dir() {
     1274                $old_theme = wp_get_theme();
     1275
     1276                switch_theme( 'test' );
     1277
     1278                // Mock parent theme to be returned as the template.
     1279                add_filter(
     1280                        'pre_option_template',
     1281                        function () {
     1282                                return 'test-parent';
     1283                        }
     1284                );
     1285
     1286                $old_root = get_theme_root( 'test' );
     1287                $path1    = get_template_directory();
     1288
     1289                $new_root = DIR_TESTDATA . '/themedir2';
     1290                register_theme_directory( $new_root );
     1291
     1292                // Mock the template root option to mimic that the active root has changed.
     1293                add_filter(
     1294                        'pre_option_template_root',
     1295                        function () use ( $new_root ) {
     1296                                return $new_root;
     1297                        }
     1298                );
     1299
     1300                $path2 = get_template_directory();
     1301
     1302                // Cleanup.
     1303                switch_theme( $old_theme->get_stylesheet() );
     1304
     1305                $this->assertEquals( $old_root . '/test-parent', $path1, 'The original template path is not correct' );
     1306                $this->assertEquals( $new_root . '/test-parent', $path2, 'The new template path is not correct' );
     1307        }
    11821308}
Note: See TracChangeset for help on using the changeset viewer.