Make WordPress Core


Ignore:
Timestamp:
12/20/2023 08:00:04 PM (2 years ago)
Author:
joemcgill
Message:

Themes: Improve the performance of _get_block_templates_paths.

This avoids redundant recursive lookups for block template paths in the same base directory by implementing a static cache. It also replaces an potentially expensive file_exists call in favor of doing recursive iteration of files inside a try/catch block.

Props thekt12, spacedmonkey, flixos90, mukesh27, joemcgill.
Fixes #58196.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-template-utils.php

    r57118 r57215  
    225225 */
    226226function _get_block_templates_paths( $base_directory ) {
     227    static $template_path_list = array();
     228    if ( isset( $template_path_list[ $base_directory ] ) ) {
     229        return $template_path_list[ $base_directory ];
     230    }
    227231    $path_list = array();
    228     if ( file_exists( $base_directory ) ) {
     232    try {
    229233        $nested_files      = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) );
    230234        $nested_html_files = new RegexIterator( $nested_files, '/^.+\.html$/i', RecursiveRegexIterator::GET_MATCH );
     
    232236            $path_list[] = $path;
    233237        }
    234     }
     238    } catch ( Exception $e ) {
     239        // Do nothing.
     240    }
     241    $template_path_list[ $base_directory ] = $path_list;
    235242    return $path_list;
    236243}
Note: See TracChangeset for help on using the changeset viewer.