Make WordPress Core


Ignore:
Timestamp:
09/19/2023 04:15:52 PM (18 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/src/wp-includes/block-template-utils.php

    r56620 r56621  
    3838 */
    3939function get_block_theme_folders( $theme_stylesheet = null ) {
    40     $theme_name = null === $theme_stylesheet ? get_stylesheet() : $theme_stylesheet;
    41     $root_dir   = get_theme_root( $theme_name );
    42     $theme_dir  = "$root_dir/$theme_name";
    43 
    44     if ( file_exists( $theme_dir . '/block-templates' ) || file_exists( $theme_dir . '/block-template-parts' ) ) {
     40    $theme = wp_get_theme( (string) $theme_stylesheet );
     41    if ( ! $theme->exists() ) {
     42        // Return the default folders if the theme doesn't exist.
    4543        return array(
    46             'wp_template'      => 'block-templates',
    47             'wp_template_part' => 'block-template-parts',
     44            'wp_template'      => 'templates',
     45            'wp_template_part' => 'parts',
    4846        );
    4947    }
    50 
    51     return array(
    52         'wp_template'      => 'templates',
    53         'wp_template_part' => 'parts',
    54     );
     48    return $theme->get_block_template_folders();
    5549}
    5650
Note: See TracChangeset for help on using the changeset viewer.