Make WordPress Core


Ignore:
Timestamp:
10/03/2023 03:16:55 PM (16 months ago)
Author:
spacedmonkey
Message:

Editor: Improve performance of _register_theme_block_patterns function.

The _register_theme_block_patterns function imposed a significant resource overhead. This issue primarily stems from themes, such as TT4, that register a substantial number of block patterns. These patterns necessitate numerous file operations, including file lookups, file reading into memory, and related processes. To provide an overview, the _register_theme_block_patterns function performed the following file operations:

  • is_dir
  • is_readable
  • file_exists
  • glob
  • file_get_contents (utilized via get_file_data)

To address these issues, caching using a transient has been added to a new function call _wp_get_block_patterns. If theme development mode is disabled and theme exists, the block patterns are saved in a transient cache. This cache is used all requests after that, saving file lookups and reading files into memory. Cache invalidation is done, when themes are switched, deleted or updated. Meaning that block patterns are not stored in the cache incorrectly.

Props flixos90, joemcgill, peterwilsoncc, costdev, swissspidy, aristath, westonruter, spacedmonkey.
Fixes #59490

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme.php

    r56727 r56765  
    822822        $this->headers                = array();
    823823        $this->__construct( $this->stylesheet, $this->theme_root );
     824        $this->delete_pattern_cache();
     825    }
     826
     827    /**
     828     * Clear block pattern cache.
     829     *
     830     * @since 6.4.0
     831     */
     832    public function delete_pattern_cache() {
     833        delete_transient( 'wp_theme_patterns_' . $this->stylesheet );
    824834    }
    825835
Note: See TracChangeset for help on using the changeset viewer.