Make WordPress Core


Ignore:
Timestamp:
09/19/2023 04:15:52 PM (12 months ago)
Author:
spacedmonkey
Message:

Themes: Improve performance of get_block_theme_folders function

This commit enhances the performance of the get_block_theme_folders function by introducing a new method called get_block_template_folders within the WP_Theme class. Previously, this function suffered from poor performance due to repeated file lookups using file_exists. The new method implements basic caching, storing the result in the theme's cache, similar to how block themes are cached in the block_theme property (see [55236]).

Additionally, this change improves error handling by checking if a theme exists before attempting to look up the file. It also enhances test coverage.

Props spacedmonkey, thekt12, swissspidy, flixos90, costdev, mukesh27.
Fixes #58319.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/block-template.php

    r56559 r56621  
    277277
    278278    /**
     279     * @ticket 58319
     280     *
     281     * @covers ::get_block_theme_folders
     282     *
     283     * @dataProvider data_get_block_theme_folders
     284     *
     285     * @param string   $theme    The theme's stylesheet.
     286     * @param string[] $expected The expected associative array of block theme folders.
     287     */
     288    public function test_get_block_theme_folders( $theme, $expected ) {
     289        $wp_theme = wp_get_theme( $theme );
     290        $wp_theme->cache_delete(); // Clear cache.
     291
     292        $this->assertSame( $expected, get_block_theme_folders( $theme ), 'Incorrect block theme folders were retrieved.' );
     293        $reflection = new ReflectionMethod( $wp_theme, 'cache_get' );
     294        $reflection->setAccessible( true );
     295        $theme_cache  = $reflection->invoke( $wp_theme, 'theme' );
     296        $cached_value = $theme_cache['block_template_folders'];
     297        $reflection->setAccessible( false );
     298
     299        $this->assertSame( $expected, $cached_value, 'The cached value is incorrect.' );
     300    }
     301
     302    /**
     303     * Data provider.
     304     *
     305     * @return array[]
     306     */
     307    public function data_get_block_theme_folders() {
     308        return array(
     309            'block-theme'                       => array(
     310                'block-theme',
     311                array(
     312                    'wp_template'      => 'templates',
     313                    'wp_template_part' => 'parts',
     314                ),
     315            ),
     316            'block-theme-deprecated-path'       => array(
     317                'block-theme-deprecated-path',
     318                array(
     319                    'wp_template'      => 'block-templates',
     320                    'wp_template_part' => 'block-template-parts',
     321                ),
     322            ),
     323            'block-theme-child'                 => array(
     324                'block-theme-child',
     325                array(
     326                    'wp_template'      => 'templates',
     327                    'wp_template_part' => 'parts',
     328                ),
     329            ),
     330            'block-theme-child-deprecated-path' => array(
     331                'block-theme-child-deprecated-path',
     332                array(
     333                    'wp_template'      => 'block-templates',
     334                    'wp_template_part' => 'block-template-parts',
     335                ),
     336            ),
     337            'this-is-an-invalid-theme'          => array(
     338                'this-is-an-invalid-theme',
     339                array(
     340                    'wp_template'      => 'templates',
     341                    'wp_template_part' => 'parts',
     342                ),
     343            ),
     344            'null'                              => array(
     345                null,
     346                array(
     347                    'wp_template'      => 'templates',
     348                    'wp_template_part' => 'parts',
     349                ),
     350            ),
     351        );
     352    }
     353
     354    /**
    279355     * Registers a test block to log `in_the_loop()` results.
    280356     *
Note: See TracChangeset for help on using the changeset viewer.