Make WordPress Core


Ignore:
Timestamp:
10/18/2024 09:53:45 PM (7 months ago)
Author:
joemcgill
Message:

Editor: Cache global styles for blocks.

This caches the generated CSS from block nodes in merged Theme JSON data to avoid repeated costly operations required to compute style properties for blocks. The generated CSS is saved to a transient that expires every hour.

This is a follow-up that reimplements [58334], which was previously reverted in [58710].

Props thekt12, spacedmonkey, pereirinha, mukesh27, isabel_brison, oandregal, andrewserong, ramonjd, joemcgill, costdev, aaronrobertshaw, peterwilsoncc.
Fixes #61679. See #59595.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/global-styles-and-settings.php

    r58797 r59256  
    258258    $tree        = WP_Theme_JSON_Resolver::resolve_theme_file_uris( $tree );
    259259    $block_nodes = $tree->get_styles_block_nodes();
     260
     261    $can_use_cached = ! wp_is_development_mode( 'theme' );
     262    $update_cache   = false;
     263
     264    if ( $can_use_cached ) {
     265        // Hash the merged WP_Theme_JSON data to bust cache on settings or styles change.
     266        $cache_hash = md5( wp_json_encode( $tree->get_raw_data() ) );
     267        $cache_key  = 'wp_styles_for_blocks';
     268        $cached     = get_transient( $cache_key );
     269
     270        // Reset the cached data if there is no value or if the hash has changed.
     271        if ( ! is_array( $cached ) || $cached['hash'] !== $cache_hash ) {
     272            $cached = array(
     273                'hash'   => $cache_hash,
     274                'blocks' => array(),
     275            );
     276
     277            // Update the cache if the hash has changed.
     278            $update_cache = true;
     279        }
     280    }
     281
    260282    foreach ( $block_nodes as $metadata ) {
    261         $block_css = $tree->get_styles_for_block( $metadata );
     283
     284        if ( $can_use_cached ) {
     285            // Use the block name as the key for cached CSS data. Otherwise, use a hash of the metadata.
     286            $cache_node_key = isset( $metadata['name'] ) ? $metadata['name'] : md5( wp_json_encode( $metadata ) );
     287
     288            if ( isset( $cached['blocks'][ $cache_node_key ] ) ) {
     289                $block_css = $cached['blocks'][ $cache_node_key ];
     290            } else {
     291                $block_css                           = $tree->get_styles_for_block( $metadata );
     292                $cached['blocks'][ $cache_node_key ] = $block_css;
     293
     294                // Update the cache if the cache contents have changed.
     295                $update_cache = true;
     296            }
     297        } else {
     298            $block_css = $tree->get_styles_for_block( $metadata );
     299        }
    262300
    263301        if ( ! wp_should_load_separate_core_block_assets() ) {
     
    305343        }
    306344    }
     345
     346    if ( $update_cache ) {
     347        set_transient( $cache_key, $cached );
     348    }
    307349}
    308350
Note: See TracChangeset for help on using the changeset viewer.