Changeset 59823
- Timestamp:
- 02/14/2025 06:36:48 PM (3 months ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-block.php
r59662 r59823 609 609 } 610 610 611 /* 612 * For Core blocks, these styles are only enqueued if `wp_should_load_separate_core_block_assets()` returns 613 * true. Otherwise these `wp_enqueue_style()` calls will not have any effect, as the Core blocks are relying on 614 * the combined 'wp-block-library' stylesheet instead, which is unconditionally enqueued. 615 */ 611 616 if ( ( ! empty( $this->block_type->style_handles ) ) ) { 612 617 foreach ( $this->block_type->style_handles as $style_handle ) { -
trunk/src/wp-includes/global-styles-and-settings.php
r59499 r59823 299 299 } 300 300 301 if ( ! wp_should_load_ separate_core_block_assets() ) {301 if ( ! wp_should_load_block_assets_on_demand() ) { 302 302 wp_add_inline_style( 'global-styles', $block_css ); 303 303 continue; … … 307 307 308 308 /* 309 * When `wp_should_load_ separate_core_block_assets()` is true, block styles are309 * When `wp_should_load_block_assets_on_demand()` is true, block styles are 310 310 * enqueued for each block on the page in class WP_Block's render function. 311 311 * This means there will be a handle in the styles queue for each of those blocks. … … 314 314 * before adding the inline style. 315 315 * This conditional loading only applies to core blocks. 316 * TODO: Explore how this could be expanded to third-party blocks as well. 316 317 */ 317 318 if ( isset( $metadata['name'] ) ) { -
trunk/src/wp-includes/script-loader.php
r59793 r59823 2488 2488 */ 2489 2489 function wp_enqueue_global_styles() { 2490 $ separate_assets = wp_should_load_separate_core_block_assets();2490 $assets_on_demand = wp_should_load_block_assets_on_demand(); 2491 2491 $is_block_theme = wp_is_block_theme(); 2492 2492 $is_classic_theme = ! $is_block_theme; 2493 2493 2494 2494 /* 2495 * Global styles should be printed in the head when loading all styles combined. 2496 * The footer should only be used to print global styles for classic themes with separate core assets enabled. 2497 * 2498 * See https://core.trac.wordpress.org/ticket/53494. 2495 * Global styles should be printed in the head for block themes, or for classic themes when loading assets on 2496 * demand is disabled, which is the default. 2497 * The footer should only be used for classic themes when loading assets on demand is enabled. 2498 * 2499 * See https://core.trac.wordpress.org/ticket/53494 and https://core.trac.wordpress.org/ticket/61965. 2499 2500 */ 2500 2501 if ( 2501 2502 ( $is_block_theme && doing_action( 'wp_footer' ) ) || 2502 ( $is_classic_theme && doing_action( 'wp_footer' ) && ! $ separate_assets) ||2503 ( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) && $ separate_assets)2503 ( $is_classic_theme && doing_action( 'wp_footer' ) && ! $assets_on_demand ) || 2504 ( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) && $assets_on_demand ) 2504 2505 ) { 2505 2506 return; … … 2568 2569 2569 2570 /** 2570 * Checks whether separate styles should be loaded for core blocks on-render. 2571 * 2572 * When this function returns true, other functions ensure that core blocks 2573 * only load their assets on-render, and each block loads its own, individual 2574 * assets. Third-party blocks only load their assets when rendered. 2575 * 2576 * When this function returns false, all core block assets are loaded regardless 2577 * of whether they are rendered in a page or not, because they are all part of 2578 * the `block-library/style.css` file. Assets for third-party blocks are always 2579 * enqueued regardless of whether they are rendered or not. 2571 * Checks whether separate styles should be loaded for core blocks. 2572 * 2573 * When this function returns true, other functions ensure that core blocks use their own separate stylesheets. 2574 * When this function returns false, all core blocks will use the single combined 'wp-block-library' stylesheet. 2575 * 2576 * As a side effect, the return value will by default result in block assets to be loaded on demand, via the 2577 * {@see wp_should_load_block_assets_on_demand()} function. This behavior can be separately altered via that function. 2580 2578 * 2581 2579 * This only affects front end and not the block editor screens. 2582 2580 * 2581 * @since 5.8.0 2582 * @see @see wp_should_load_block_assets_on_demand() 2583 2583 * @see wp_enqueue_registered_block_scripts_and_styles() 2584 2584 * @see register_block_style_handle() 2585 2585 * 2586 * @since 5.8.0 2587 * 2588 * @return bool Whether separate assets will be loaded. 2586 * @return bool Whether separate core block assets will be loaded. 2589 2587 */ 2590 2588 function wp_should_load_separate_core_block_assets() { … … 2608 2606 2609 2607 /** 2608 * Checks whether block styles should be loaded only on-render. 2609 * 2610 * When this function returns true, other functions ensure that blocks only load their assets on-render. 2611 * When this function returns false, all block assets are loaded regardless of whether they are rendered in a page. 2612 * 2613 * The default return value depends on the result of {@see wp_should_load_separate_core_block_assets()}, which controls 2614 * whether Core block stylesheets should be loaded separately or via a combined 'wp-block-library' stylesheet. 2615 * 2616 * This only affects front end and not the block editor screens. 2617 * 2618 * @since 6.8.0 2619 * @see wp_should_load_separate_core_block_assets() 2620 * 2621 * @return bool Whether to load block assets only when they are rendered. 2622 */ 2623 function wp_should_load_block_assets_on_demand() { 2624 if ( is_admin() || is_feed() || wp_is_rest_endpoint() ) { 2625 return false; 2626 } 2627 2628 /* 2629 * For backward compatibility, the default return value for this function is based on the return value of 2630 * `wp_should_load_separate_core_block_assets()`. Initially, this function used to control both of these concerns. 2631 */ 2632 $load_assets_on_demand = wp_should_load_separate_core_block_assets(); 2633 2634 /** 2635 * Filters whether block styles should be loaded on demand. 2636 * 2637 * Returning false loads all block assets, regardless of whether they are rendered in a page or not. 2638 * Returning true loads block assets only when they are rendered. 2639 * 2640 * The default value of the filter depends on the result of {@see wp_should_load_separate_core_block_assets()}, 2641 * which controls whether Core block stylesheets should be loaded separately or via a combined 'wp-block-library' 2642 * stylesheet. 2643 * 2644 * @since 6.8.0 2645 * 2646 * @param bool $load_assets_on_demand Whether to load block assets only when they are rendered. 2647 */ 2648 return apply_filters( 'should_load_block_assets_on_demand', $load_assets_on_demand ); 2649 } 2650 2651 /** 2610 2652 * Enqueues registered block scripts and styles, depending on current rendered 2611 2653 * context (only enqueuing editor scripts while in context of the editor). … … 2618 2660 global $current_screen; 2619 2661 2620 if ( wp_should_load_ separate_core_block_assets() ) {2662 if ( wp_should_load_block_assets_on_demand() ) { 2621 2663 return; 2622 2664 } … … 2625 2667 2626 2668 $block_registry = WP_Block_Type_Registry::get_instance(); 2669 2670 /* 2671 * Block styles are only enqueued if they're registered. For core blocks, this is only the case if 2672 * `wp_should_load_separate_core_block_assets()` returns true. Otherwise they use the single combined 2673 * 'wp-block-library` stylesheet. See also `register_core_block_style_handles()`. 2674 * Since `wp_enqueue_style()` does not trigger warnings if the style is not registered, it is okay to not cater for 2675 * this behavior here and simply call `wp_enqueue_style()` unconditionally. 2676 */ 2627 2677 foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) { 2628 2678 // Front-end and editor styles. … … 2666 2716 if ( isset( $style_properties['style_handle'] ) ) { 2667 2717 2668 // If the site loads separate styles per-block, enqueue the stylesheet on render.2669 if ( wp_should_load_ separate_core_block_assets() ) {2718 // If the site loads block styles on demand, enqueue the stylesheet on render. 2719 if ( wp_should_load_block_assets_on_demand() ) { 2670 2720 add_filter( 2671 2721 'render_block', … … 2688 2738 $handle = 'wp-block-library'; 2689 2739 2690 // If the site loads separate styles per-block, check if the block has a stylesheet registered.2691 if ( wp_should_load_ separate_core_block_assets() ) {2740 // If the site loads block styles on demand, check if the block has a stylesheet registered. 2741 if ( wp_should_load_block_assets_on_demand() ) { 2692 2742 $block_stylesheet_handle = generate_block_asset_handle( $block_name, 'style' ); 2693 2743 … … 3188 3238 * Enqueues a stylesheet for a specific block. 3189 3239 * 3190 * If the theme has opted-in to separate-styles loading,3240 * If the theme has opted-in to load block styles on demand, 3191 3241 * then the stylesheet will be enqueued on-render, 3192 3242 * otherwise when the block inits. … … 3256 3306 3257 3307 $hook = did_action( 'wp_enqueue_scripts' ) ? 'wp_footer' : 'wp_enqueue_scripts'; 3258 if ( wp_should_load_ separate_core_block_assets() ) {3308 if ( wp_should_load_block_assets_on_demand() ) { 3259 3309 /** 3260 3310 * Callback function to register and enqueue styles. -
trunk/src/wp-includes/theme.php
r59813 r59823 4396 4396 4397 4397 add_filter( 'should_load_separate_core_block_assets', '__return_true' ); 4398 add_filter( 'should_load_block_assets_on_demand', '__return_true' ); 4398 4399 4399 4400 /* -
trunk/tests/phpunit/tests/theme.php
r59579 r59823 967 967 968 968 /** 969 * Tests that block themes load block assets on demand by default. 970 * 971 * @ticket 61965 972 * 973 * @covers ::_add_default_theme_supports 974 * @covers ::wp_should_load_block_assets_on_demand 975 */ 976 public function test_block_theme_should_load_block_assets_on_demand_by_default() { 977 $this->helper_requires_block_theme(); 978 979 add_filter( 'should_load_block_assets_on_demand', '__return_false' ); 980 981 $this->assertFalse( 982 wp_should_load_block_assets_on_demand(), 983 'Could not disable loading block assets on demand.' 984 ); 985 986 do_action( 'after_setup_theme' ); 987 add_filter( 'should_load_separate_core_block_assets', '__return_false' ); 988 989 $this->assertTrue( 990 wp_should_load_block_assets_on_demand(), 991 'Block themes do not load block assets on demand by default.' 992 ); 993 } 994 995 /** 996 * Tests that block themes load block assets on demand by default even when loading separate core block assets is disabled. 997 * 998 * @ticket 61965 999 * 1000 * @covers ::_add_default_theme_supports 1001 * @covers ::wp_should_load_block_assets_on_demand 1002 */ 1003 public function test_block_theme_should_load_block_assets_on_demand_by_default_even_with_separate_core_block_assets_disabled() { 1004 $this->helper_requires_block_theme(); 1005 1006 do_action( 'after_setup_theme' ); 1007 add_filter( 'should_load_separate_core_block_assets', '__return_false' ); 1008 1009 $this->assertTrue( wp_should_load_block_assets_on_demand() ); 1010 } 1011 1012 /** 969 1013 * Tests that a theme in the custom test data theme directory is recognized. 970 1014 *
Note: See TracChangeset
for help on using the changeset viewer.