Make WordPress Core

Changeset 58703

Timestamp:
07/10/2024 06:17:44 AM (4 weeks 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.

Location:
trunk
Files:
7 edited

Legend:

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

    r58328 r58703  
    533533         */
    534534        $global_styles[] = array(
    535             'css'            => wp_get_global_styles_custom_css(),
     535            'css'            => wp_get_global_styles),
    536536            '__unstableType' => 'user',
    537537            'isGlobalStyles' => true,
  • trunk/src/wp-includes/class-wp-theme-json.php

    r58685 r58703  
    14241424        }
    14251425
     1426
     1427
     1428
     1429
     1430
     1431
    14261432        return $stylesheet;
    14271433    }
     
    14681474     *
    14691475     * @since 6.2.0
     1476
    14701477     *
    14711478     * @return string The global styles custom CSS.
    14721479     */
    14731480    public function get_custom_css() {
     1481
    14741482        // Add the global styles root CSS.
    14751483        $stylesheet = isset( $this->theme_json['styles']['css'] ) ? $this->theme_json['styles']['css'] : '';
     
    26932701                'features'   => $feature_selectors,
    26942702                'variations' => $variation_selectors,
     2703
    26952704            );
    26962705
     
    29092918        }
    29102919
    2911         // 7. Generate and append any custom CSS rules pertaining to nested block style variations.
     2920        // 7. Generate and append any custom CSS rules.
    29122921        if ( isset( $node['css'] ) && ! $is_root_selector ) {
    29132922            $block_rules .= $this->process_blocks_custom_css( $node['css'], $selector );
  • trunk/src/wp-includes/default-filters.php

    r58381 r58703  
    601601add_action( 'wp_footer', 'wp_enqueue_global_styles', 1 );
    602602
    603 // Global styles custom CSS.
    604 add_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles_custom_css' );
    605 
    606603// Block supports, and other styles parsed and stored in the Style Engine.
    607604add_action( 'wp_enqueue_scripts', 'wp_enqueue_stored_styles' );
  • trunk/src/wp-includes/deprecated.php

    r58322 r58703  
    63116311    return $parsed_block;
    63126312}
     6313
     6314
     6315
     6316
     6317
     6318
     6319
     6320
     6321
     6322
     6323
     6324
     6325
     6326
     6327
     6328
     6329
     6330
     6331
     6332
     6333
     6334
     6335
     6336
     6337
     6338
     6339
     6340
     6341
     6342
     6343
     6344
     6345
     6346
     6347
     6348
     6349
     6350
     6351
     6352
     6353
     6354
     6355
     6356
     6357
     6358
     6359
     6360
     6361
     6362
     6363
     6364
     6365
     6366
     6367
     6368
     6369
     6370
     6371
     6372
     6373
     6374
     6375
     6376
     6377
     6378
     6379
     6380
     6381
     6382
     6383
     6384
     6385
     6386
     6387
     6388
     6389
  • trunk/src/wp-includes/global-styles-and-settings.php

    r58334 r58703  
    237237
    238238    $stylesheet = $styles_variables . $styles_rest;
    239     if ( $can_use_cached ) {
    240         wp_cache_set( $cache_key, $stylesheet, $cache_group );
    241     }
    242 
    243     return $stylesheet;
    244 }
    245 
    246 /**
    247  * Gets the global styles custom CSS from theme.json.
    248  *
    249  * @since 6.2.0
    250  *
    251  * @return string The global styles custom CSS.
    252  */
    253 function wp_get_global_styles_custom_css() {
    254     if ( ! wp_theme_has_theme_json() ) {
    255         return '';
    256     }
    257     /*
    258      * Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme
    259      * developer's workflow.
    260      */
    261     $can_use_cached = ! wp_is_development_mode( 'theme' );
    262 
    263     /*
    264      * By using the 'theme_json' group, this data is marked to be non-persistent across requests.
    265      * @see `wp_cache_add_non_persistent_groups()`.
    266      *
    267      * The rationale for this is to make sure derived data from theme.json
    268      * is always fresh from the potential modifications done via hooks
    269      * that can use dynamic data (modify the stylesheet depending on some option,
    270      * settings depending on user permissions, etc.).
    271      * See some of the existing hooks to modify theme.json behavior:
    272      * @see https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/
    273      *
    274      * A different alternative considered was to invalidate the cache upon certain
    275      * events such as options add/update/delete, user meta, etc.
    276      * It was judged not enough, hence this approach.
    277      * @see https://github.com/WordPress/gutenberg/pull/45372
    278      */
    279     $cache_key   = 'wp_get_global_styles_custom_css';
    280     $cache_group = 'theme_json';
    281     if ( $can_use_cached ) {
    282         $cached = wp_cache_get( $cache_key, $cache_group );
    283         if ( $cached ) {
    284             return $cached;
    285         }
    286     }
    287 
    288     $tree       = WP_Theme_JSON_Resolver::get_merged_data();
    289     $stylesheet = $tree->get_custom_css();
    290 
    291239    if ( $can_use_cached ) {
    292240        wp_cache_set( $cache_key, $stylesheet, $cache_group );
  • trunk/src/wp-includes/script-loader.php

    r58408 r58703  
    25052505    $stylesheet = wp_get_global_stylesheet();
    25062506
     2507
     2508
     2509
     2510
     2511
     2512
     2513
     2514
     2515
     2516
     2517
     2518
     2519
     2520
    25072521    if ( empty( $stylesheet ) ) {
    25082522        return;
     
    25152529    // Add each block as an inline css.
    25162530    wp_add_global_styles_for_blocks();
    2517 }
    2518 
    2519 /**
    2520  * Enqueues the global styles custom css defined via theme.json.
    2521  *
    2522  * @since 6.2.0
    2523  */
    2524 function wp_enqueue_global_styles_custom_css() {
    2525     if ( ! wp_is_block_theme() ) {
    2526         return;
    2527     }
    2528 
    2529     // Don't enqueue Customizer's custom CSS separately.
    2530     remove_action( 'wp_head', 'wp_custom_css_cb', 101 );
    2531 
    2532     $custom_css  = wp_get_custom_css();
    2533     $custom_css .= wp_get_global_styles_custom_css();
    2534 
    2535     if ( ! empty( $custom_css ) ) {
    2536         wp_add_inline_style( 'global-styles', $custom_css );
    2537     }
    25382531}
    25392532
  • trunk/tests/phpunit/tests/theme/wpThemeJson.php

    r58685 r58703  
    50365036
    50375037    /**
    5038      * @ticket 57536
    5039      * @ticket 61165
    5040      */
    5041     public function test_get_custom_css_handles_global_custom_css() {
     5038     * Tests that base custom CSS is generated correctly.
     5039     *
     5040     * @ticket 61395
     5041     */
     5042    public function test_get_stylesheet_handles_base_custom_css() {
    50425043        $theme_json = new WP_Theme_JSON(
    50435044            array(
    50445045                'version' => WP_Theme_JSON::LATEST_SCHEMA,
    50455046                'styles'  => array(
    5046                     'css'    => 'body {color:purple;}',
     5047                    'css' => 'body {color:purple;}',
     5048                ),
     5049            )
     5050        );
     5051
     5052        $custom_css = 'body {color:purple;}';
     5053        $this->assertSame( $custom_css, $theme_json->get_stylesheet( array( 'custom-css' ) ) );
     5054    }
     5055
     5056    /**
     5057     * Tests that block custom CSS is generated correctly.
     5058     *
     5059     * @ticket 61395
     5060     */
     5061    public function test_get_styles_for_block_handles_block_custom_css() {
     5062        $theme_json = new WP_Theme_JSON(
     5063            array(
     5064                'version' => WP_Theme_JSON::LATEST_SCHEMA,
     5065                'styles'  => array(
    50475066                    'blocks' => array(
    50485067                        'core/paragraph' => array(
     
    50545073        );
    50555074
    5056         $custom_css = 'body {color:purple;}:root :where(p){color:red;}';
    5057         $this->assertSame( $custom_css, $theme_json->get_custom_css() );
     5075        $paragraph_node = array(
     5076            'name'      => 'core/paragraph',
     5077            'path'      => array( 'styles', 'blocks', 'core/paragraph' ),
     5078            'selector'  => 'p',
     5079            'selectors' => array(
     5080                'root' => 'p',
     5081            ),
     5082        );
     5083
     5084        $custom_css = ':root :where(p){color:red;}';
     5085        $this->assertSame( $custom_css, $theme_json->get_styles_for_block( $paragraph_node ) );
    50585086    }
    50595087
Note: See TracChangeset for help on using the changeset viewer.