Make WordPress Core

Changeset 58710


Ignore:
Timestamp:
07/12/2024 07:24:12 PM (5 months ago)
Author:
joemcgill
Message:

Editor: Revert caching of global styles for blocks.

This reverts [58334] to fix a bug where edits to block styles made in the site editor were not showing in the front end.

Props joemcgill, spacedmonkey, andrewserong, hellofromtonya, audrasjb.
See #59595.

Location:
trunk
Files:
2 edited

Legend:

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

    r58703 r58710  
    256256    $tree        = WP_Theme_JSON_Resolver::get_merged_data();
    257257    $block_nodes = $tree->get_styles_block_nodes();
    258 
    259     $can_use_cached = ! wp_is_development_mode( 'theme' );
    260     if ( $can_use_cached ) {
    261         // Hash global settings and block nodes together to optimize performance of key generation.
    262         $hash = md5(
    263             wp_json_encode(
    264                 array(
    265                     'global_setting' => wp_get_global_settings(),
    266                     'block_nodes'    => $block_nodes,
    267                 )
    268             )
    269         );
    270 
    271         $cache_key = "wp_styles_for_blocks:$hash";
    272         $cached    = get_site_transient( $cache_key );
    273         if ( ! is_array( $cached ) ) {
    274             $cached = array();
    275         }
    276     }
    277 
    278     $update_cache = false;
    279 
    280258    foreach ( $block_nodes as $metadata ) {
    281 
    282         if ( $can_use_cached ) {
    283             // Use the block name as the key for cached CSS data. Otherwise, use a hash of the metadata.
    284             $cache_node_key = isset( $metadata['name'] ) ? $metadata['name'] : md5( wp_json_encode( $metadata ) );
    285 
    286             if ( isset( $cached[ $cache_node_key ] ) ) {
    287                 $block_css = $cached[ $cache_node_key ];
    288             } else {
    289                 $block_css                 = $tree->get_styles_for_block( $metadata );
    290                 $cached[ $cache_node_key ] = $block_css;
    291                 $update_cache              = true;
    292             }
    293         } else {
    294             $block_css = $tree->get_styles_for_block( $metadata );
    295         }
     259        $block_css = $tree->get_styles_for_block( $metadata );
    296260
    297261        if ( ! wp_should_load_separate_core_block_assets() ) {
     
    339303        }
    340304    }
    341 
    342     if ( $update_cache ) {
    343         set_site_transient( $cache_key, $cached, HOUR_IN_SECONDS );
    344     }
    345305}
    346306
  • trunk/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php

    r58334 r58710  
    7777
    7878    /**
    79      * Ensure that the block cache is set for global styles.
    80      *
    81      * @ticket 59595
    82      */
    83     public function test_styles_for_blocks_cache_is_set() {
    84         $this->set_up_third_party_block();
    85 
    86         wp_register_style( 'global-styles', false, array(), true, true );
    87 
    88         $cache_key                = $this->get_wp_styles_for_blocks_cache_key();
    89         $styles_for_blocks_before = get_site_transient( $cache_key );
    90         $this->assertFalse( $styles_for_blocks_before );
    91 
    92         wp_add_global_styles_for_blocks();
    93 
    94         $styles_for_blocks_after = get_site_transient( $cache_key );
    95         $this->assertNotEmpty( $styles_for_blocks_after );
    96     }
    97 
    98     /**
    99      * Confirm that the block cache is skipped when in dev mode for themes.
    100      *
    101      * @ticket 59595
    102      */
    103     public function test_styles_for_blocks_skips_cache_in_dev_mode() {
    104         global $_wp_tests_development_mode;
    105 
    106         $orig_dev_mode = $_wp_tests_development_mode;
    107 
    108         // Setting development mode to theme should skip the cache.
    109         $_wp_tests_development_mode = 'theme';
    110 
    111         wp_register_style( 'global-styles', false, array(), true, true );
    112 
    113         // Initial register of global styles.
    114         wp_add_global_styles_for_blocks();
    115 
    116         $cache_key                 = $this->get_wp_styles_for_blocks_cache_key();
    117         $styles_for_blocks_initial = get_site_transient( $cache_key );
    118 
    119         // Cleanup.
    120         $_wp_tests_development_mode = $orig_dev_mode;
    121 
    122         $this->assertFalse( $styles_for_blocks_initial );
    123     }
    124 
    125     /**
    126      * Confirm that the block cache is updated if the block meta has changed.
    127      *
    128      * @ticket 59595
    129      */
    130     public function test_styles_for_blocks_cache_is_skipped() {
    131         wp_register_style( 'global-styles', false, array(), true, true );
    132 
    133         // Initial register of global styles.
    134         wp_add_global_styles_for_blocks();
    135 
    136         $cache_key                 = $this->get_wp_styles_for_blocks_cache_key();
    137         $styles_for_blocks_initial = get_site_transient( $cache_key );
    138         $this->assertNotEmpty( $styles_for_blocks_initial, 'Initial cache was not set.' );
    139 
    140         $this->set_up_third_party_block();
    141 
    142         /*
    143          * Call register of global styles again to ensure the cache is updated.
    144          * In normal conditions, this function is only called once per request.
    145          */
    146         wp_add_global_styles_for_blocks();
    147 
    148         $cache_key                 = $this->get_wp_styles_for_blocks_cache_key();
    149         $styles_for_blocks_updated = get_site_transient( $cache_key );
    150         $this->assertNotEmpty( $styles_for_blocks_updated, 'Updated cache was not set.' );
    151 
    152         $this->assertNotEquals(
    153             $styles_for_blocks_initial,
    154             $styles_for_blocks_updated,
    155             'Block style cache was not updated.'
    156         );
    157     }
    158 
    159     /**
    16079     * @ticket 56915
    16180     * @ticket 61165
     
    335254        return is_array( $actual ) ? $actual : array();
    336255    }
    337 
    338     /**
    339      * Get cache key for `wp_styles_for_blocks`.
    340      *
    341      * @return string The cache key.
    342      */
    343     private function get_wp_styles_for_blocks_cache_key() {
    344         $tree        = WP_Theme_JSON_Resolver::get_merged_data();
    345         $block_nodes = $tree->get_styles_block_nodes();
    346         // md5 is a costly operation, so we hashing global settings and block_node in a single call.
    347         $hash = md5(
    348             wp_json_encode(
    349                 array(
    350                     'global_setting' => wp_get_global_settings(),
    351                     'block_nodes'    => $block_nodes,
    352                 )
    353             )
    354         );
    355 
    356         return "wp_styles_for_blocks:$hash";
    357     }
    358256}
Note: See TracChangeset for help on using the changeset viewer.