Make WordPress Core


Ignore:
Timestamp:
07/10/2024 06:17:44 AM (11 months ago)
Author:
isabel_brison
Message:

Editor: enqueue block custom CSS only when block renders on the page.

Updates the global styles custom CSS handling logic to be consistent with other global styles and take advantage of conditional enqueuing of block styles.

Props isabel_brison, aaronrobertshaw, andrewserong.
Fixes #61395.

File:
1 edited

Legend:

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

    r58322 r58703  
    63116311    return $parsed_block;
    63126312}
     6313
     6314/**
     6315 * Gets the global styles custom CSS from theme.json.
     6316 *
     6317 * @since 6.2.0
     6318 * @deprecated 6.7.0 Use {@see 'wp_get_global_stylesheet'} instead.
     6319 *
     6320 * @return string The global styles custom CSS.
     6321 */
     6322function wp_get_global_styles_custom_css() {
     6323    _deprecated_function( __FUNCTION__, '6.7.0', 'wp_get_global_stylesheet' );
     6324    if ( ! wp_theme_has_theme_json() ) {
     6325        return '';
     6326    }
     6327    /*
     6328     * Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme
     6329     * developer's workflow.
     6330     */
     6331    $can_use_cached = ! wp_is_development_mode( 'theme' );
     6332
     6333    /*
     6334     * By using the 'theme_json' group, this data is marked to be non-persistent across requests.
     6335     * @see `wp_cache_add_non_persistent_groups()`.
     6336     *
     6337     * The rationale for this is to make sure derived data from theme.json
     6338     * is always fresh from the potential modifications done via hooks
     6339     * that can use dynamic data (modify the stylesheet depending on some option,
     6340     * settings depending on user permissions, etc.).
     6341     * See some of the existing hooks to modify theme.json behavior:
     6342     * @see https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/
     6343     *
     6344     * A different alternative considered was to invalidate the cache upon certain
     6345     * events such as options add/update/delete, user meta, etc.
     6346     * It was judged not enough, hence this approach.
     6347     * @see https://github.com/WordPress/gutenberg/pull/45372
     6348     */
     6349    $cache_key   = 'wp_get_global_styles_custom_css';
     6350    $cache_group = 'theme_json';
     6351    if ( $can_use_cached ) {
     6352        $cached = wp_cache_get( $cache_key, $cache_group );
     6353        if ( $cached ) {
     6354            return $cached;
     6355        }
     6356    }
     6357
     6358    $tree       = WP_Theme_JSON_Resolver::get_merged_data();
     6359    $stylesheet = $tree->get_custom_css();
     6360
     6361    if ( $can_use_cached ) {
     6362        wp_cache_set( $cache_key, $stylesheet, $cache_group );
     6363    }
     6364
     6365    return $stylesheet;
     6366}
     6367
     6368/**
     6369 * Enqueues the global styles custom css defined via theme.json.
     6370 *
     6371 * @since 6.2.0
     6372 * @deprecated 6.7.0 Use {@see 'wp_enqueue_global_styles'} instead.
     6373 */
     6374function wp_enqueue_global_styles_custom_css() {
     6375    _deprecated_function( __FUNCTION__, '6.7.0', 'wp_enqueue_global_styles' );
     6376    if ( ! wp_is_block_theme() ) {
     6377        return;
     6378    }
     6379
     6380    // Don't enqueue Customizer's custom CSS separately.
     6381    remove_action( 'wp_head', 'wp_custom_css_cb', 101 );
     6382
     6383    $custom_css  = wp_get_custom_css();
     6384    $custom_css .= wp_get_global_styles_custom_css();
     6385
     6386    if ( ! empty( $custom_css ) ) {
     6387        wp_add_inline_style( 'global-styles', $custom_css );
     6388    }
     6389}
Note: See TracChangeset for help on using the changeset viewer.