Make WordPress Core

Changeset 58334

Timestamp:
06/04/2024 02:50:24 PM (2 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.

Props thekt12, spacedmonkey, pereirinha, mukesh27, isabel_brison, oandregal, andrewserong, ramonjd.
Fixes #59595.

Location:
trunk
Files:
2 edited

Legend:

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

    r58262 r58334  
    308308    $tree        = WP_Theme_JSON_Resolver::get_merged_data();
    309309    $block_nodes = $tree->get_styles_block_nodes();
     310
     311
     312
     313
     314
     315
     316
     317
     318
     319
     320
     321
     322
     323
     324
     325
     326
     327
     328
     329
     330
     331
    310332    foreach ( $block_nodes as $metadata ) {
    311         $block_css = $tree->get_styles_for_block( $metadata );
     333
     334        if ( $can_use_cached ) {
     335            // Use the block name as the key for cached CSS data. Otherwise, use a hash of the metadata.
     336            $cache_node_key = isset( $metadata['name'] ) ? $metadata['name'] : md5( wp_json_encode( $metadata ) );
     337
     338            if ( isset( $cached[ $cache_node_key ] ) ) {
     339                $block_css = $cached[ $cache_node_key ];
     340            } else {
     341                $block_css                 = $tree->get_styles_for_block( $metadata );
     342                $cached[ $cache_node_key ] = $block_css;
     343                $update_cache              = true;
     344            }
     345        } else {
     346            $block_css = $tree->get_styles_for_block( $metadata );
     347        }
    312348
    313349        if ( ! wp_should_load_separate_core_block_assets() ) {
     
    355391        }
    356392    }
     393
     394
     395
     396
    357397}
    358398
  • trunk/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php

    r58295 r58334  
    7777
    7878    /**
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
     100
     101
     102
     103
     104
     105
     106
     107
     108
     109
     110
     111
     112
     113
     114
     115
     116
     117
     118
     119
     120
     121
     122
     123
     124
     125
     126
     127
     128
     129
     130
     131
     132
     133
     134
     135
     136
     137
     138
     139
     140
     141
     142
     143
     144
     145
     146
     147
     148
     149
     150
     151
     152
     153
     154
     155
     156
     157
     158
     159
    79160     * @ticket 56915
    80161     * @ticket 61165
     
    254335        return is_array( $actual ) ? $actual : array();
    255336    }
     337
     338
     339
     340
     341
     342
     343
     344
     345
     346
     347
     348
     349
     350
     351
     352
     353
     354
     355
     356
     357
    256358}
Note: See TracChangeset for help on using the changeset viewer.