Make WordPress Core

Changeset 56524

Timestamp:
09/06/2023 06:06:47 PM (11 months ago)
Author:
joemcgill
Message:

Themes: Avoid stale caches for core block styles.

This fixes an issue introduced in [56044] in which the path to core block styles are cached to avoid expensive file operations on every page load. The original caching strategy is now modified so that only the path relative to the blocks location are stored, rather than the full path, since the path to the wp-includes folder can change after the value is generated. The new cached value also includes the current WordPress version value to ensure it is rebuilt when the version changes.

Props lhe2012, coreyw, colorful tones, petitphp, mukesh27, spacedmonkey, joemcgill, flixos90, kimannwall, desmith.
Fixes #59111.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks/index.php

    r56249 r56524  
    2121 *
    2222 * @since 6.3.0
     23
     24
    2325 */
    2426function register_core_block_style_handles() {
     27
     28
    2529    if ( ! wp_should_load_separate_core_block_assets() ) {
    2630        return;
    2731    }
    2832
    29     static $core_blocks_meta;
    30     if ( ! $core_blocks_meta ) {
    31         $core_blocks_meta = require ABSPATH . WPINC . '/blocks/blocks-json.php';
    32     }
    33 
    34     $includes_url  = includes_url();
    35     $includes_path = ABSPATH . WPINC . '/';
    36     $suffix        = wp_scripts_get_suffix();
    37     $wp_styles     = wp_styles();
    38     $style_fields  = array(
     33    $blocks_url   = includes_url( 'blocks/' );
     34    $suffix       = wp_scripts_get_suffix();
     35    $wp_styles    = wp_styles();
     36    $style_fields = array(
    3937        'style'       => 'style',
    4038        'editorStyle' => 'editor',
    4139    );
     40
     41
     42
     43
     44
     45
     46
     47
    4248
    4349    /*
     
    4551     * the core developer's workflow.
    4652     */
    47     if ( ! wp_is_development_mode( 'core' ) ) {
    48         $transient_name = 'wp_core_block_css_files';
    49         $files          = get_transient( $transient_name );
    50         if ( ! $files ) {
    51             $files = glob( wp_normalize_path( __DIR__ . '/**/**.css' ) );
    52             set_transient( $transient_name, $files );
     53    $can_use_cached = ! wp_is_development_mode( 'core' );
     54
     55    if ( $can_use_cached ) {
     56        $cached_files = get_transient( $transient_name );
     57
     58        // Check the validity of cached values by checking against the current WordPress version.
     59        if (
     60            is_array( $cached_files )
     61            && isset( $cached_files['version'] )
     62            && $cached_files['version'] === $wp_version
     63            && isset( $cached_files['files'] )
     64        ) {
     65            $files = $cached_files['files'];
    5366        }
    54     } else {
    55         $files = glob( wp_normalize_path( __DIR__ . '/**/**.css' ) );
    5667    }
    5768
    58     $register_style = static function( $name, $filename, $style_handle ) use ( $includes_path, $includes_url, $suffix, $wp_styles, $files ) {
    59         $style_path = "blocks/{$name}/{$filename}{$suffix}.css";
    60         $path       = wp_normalize_path( $includes_path . $style_path );
     69    if ( ! $files ) {
     70        $files = glob( wp_normalize_path( BLOCKS_PATH . '**/**.css' ) );
     71        $files = array_map(
     72            static function ( $file ) {
     73                return str_replace( BLOCKS_PATH, '', $file );
     74            },
     75            $files
     76        );
    6177
    62         if ( ! in_array( $path, $files, true ) ) {
     78        // Save core block style paths in cache when not in development mode.
     79        if ( $can_use_cached ) {
     80            set_transient(
     81                $transient_name,
     82                array(
     83                    'version' => $wp_version,
     84                    'files'   => $files,
     85                )
     86            );
     87        }
     88    }
     89
     90    $register_style = static function( $name, $filename, $style_handle ) use ( $blocks_url, $suffix, $wp_styles, $files ) {
     91        $style_path = "{$name}/{$filename}{$suffix}.css";
     92        $path       = wp_normalize_path( BLOCKS_PATH . $style_path );
     93
     94        if ( ! in_array( $style_path, $files, true ) ) {
    6395            $wp_styles->add(
    6496                $style_handle,
     
    68100        }
    69101
    70         $wp_styles->add( $style_handle, $includes_url . $style_path );
     102        $wp_styles->add( $style_handle, $s_url . $style_path );
    71103        $wp_styles->add_data( $style_handle, 'path', $path );
    72104
Note: See TracChangeset for help on using the changeset viewer.