Make WordPress Core


Ignore:
Timestamp:
02/02/2023 06:50:54 PM (22 months ago)
Author:
flixos90
Message:

Editor: Add support for custom CSS in global styles.

This changeset introduces functions wp_get_global_styles_custom_css() and wp_enqueue_global_styles_custom_css(), which allow accessing and enqueuing custom CSS added via global styles.

Custom CSS via global styles is handled separately from custom CSS via the Customizer. If a site uses both features, the custom CSS from both sources will be loaded. The global styles custom CSS is then loaded after the Customizer custom CSS, so if there are any conflicts between the rules, the global styles take precedence.

Similarly to e.g. [55185], the result is cached in a non-persistent cache, except when WP_DEBUG is on to avoid interrupting the theme developer's workflow.

Props glendaviesnz, oandregal, ntsekouras, mamaduka, davidbaumwald, hellofromtonya, flixos90.
Fixes #57536.

File:
1 edited

Legend:

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

    r55185 r55192  
    219219
    220220    $stylesheet = $styles_variables . $styles_rest;
     221    if ( $can_use_cached ) {
     222        wp_cache_set( $cache_key, $stylesheet, $cache_group );
     223    }
     224
     225    return $stylesheet;
     226}
     227
     228/**
     229 * Gets the global styles custom css from theme.json.
     230 *
     231 * @since 6.2.0
     232 *
     233 * @return string Stylesheet.
     234 */
     235function wp_get_global_styles_custom_css() {
     236    if ( ! wp_theme_has_theme_json() ) {
     237        return '';
     238    }
     239    /*
     240     * Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme
     241     * developer's workflow.
     242     *
     243     * @todo Replace `WP_DEBUG` once an "in development mode" check is available in Core.
     244     */
     245    $can_use_cached = ! WP_DEBUG;
     246
     247    /*
     248     * By using the 'theme_json' group, this data is marked to be non-persistent across requests.
     249     * @see `wp_cache_add_non_persistent_groups()`.
     250     *
     251     * The rationale for this is to make sure derived data from theme.json
     252     * is always fresh from the potential modifications done via hooks
     253     * that can use dynamic data (modify the stylesheet depending on some option,
     254     * settings depending on user permissions, etc.).
     255     * See some of the existing hooks to modify theme.json behavior:
     256     * @see https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/
     257     *
     258     * A different alternative considered was to invalidate the cache upon certain
     259     * events such as options add/update/delete, user meta, etc.
     260     * It was judged not enough, hence this approach.
     261     * @see https://github.com/WordPress/gutenberg/pull/45372
     262     */
     263    $cache_key   = 'wp_get_global_styles_custom_css';
     264    $cache_group = 'theme_json';
     265    if ( $can_use_cached ) {
     266        $cached = wp_cache_get( $cache_key, $cache_group );
     267        if ( $cached ) {
     268            return $cached;
     269        }
     270    }
     271
     272    $tree       = WP_Theme_JSON_Resolver::get_merged_data();
     273    $stylesheet = $tree->get_custom_css();
     274
    221275    if ( $can_use_cached ) {
    222276        wp_cache_set( $cache_key, $stylesheet, $cache_group );
     
    370424    wp_cache_delete( 'wp_get_global_settings_custom', 'theme_json' );
    371425    wp_cache_delete( 'wp_get_global_settings_theme', 'theme_json' );
     426    wp_cache_delete( 'wp_get_global_styles_custom_css', 'theme_json' );
    372427    WP_Theme_JSON_Resolver::clean_cached_data();
    373428}
Note: See TracChangeset for help on using the changeset viewer.