<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[59823] trunk: Editor: Introduce `wp_should_load_block_assets_on_demand()` with filter `'should_load_block_assets_on_demand'`.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/59823">59823</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/59823","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>flixos90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2025-02-14 18:36:48 +0000 (Fri, 14 Feb 2025)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Editor: Introduce `wp_should_load_block_assets_on_demand()` with filter `'should_load_block_assets_on_demand'`.

This function and filter complement the existing `wp_should_load_separate_core_block_assets()` with filter `'should_load_separate_core_block_assets'`, which until now was responsible for two different purposes:
1. Loading separate stylesheets for Core blocks, instead of a combined `wp-block-library` stylesheet (as the name indicates).
2. Loading block scripts and stylesheets on demand only if the blocks are included in the page (not indicated by the name).

The new function and filter handles exclusively the 2nd purpose, making it possible to individually adjust both behaviors. For backward compatibility, the return value of `wp_should_load_separate_core_block_assets()` is used as the filterable default for `wp_should_load_block_assets_on_demand()`. Yet, the two filters can now be individually be controlled: For example, a site owner that wants to keep loading the combined `wp-block-library` stylesheet can now do so without giving up on the ability to load block scripts and stylesheets on demand.

Block themes now opt in by default to both features, similar to how they were already doing before via just the one filter. This way, block themes that opt out of loading separate stylesheets for Core blocks will still benefit from loading block scripts and stylesheets on demand, which in the case of block themes is strongly recommended.

Props fabiankaegy, flixos90, gziolo.
Fixes <a href="https://core.trac.wordpress.org/ticket/61965">#61965</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpblockphp">trunk/src/wp-includes/class-wp-block.php</a></li>
<li><a href="#trunksrcwpincludesglobalstylesandsettingsphp">trunk/src/wp-includes/global-styles-and-settings.php</a></li>
<li><a href="#trunksrcwpincludesscriptloaderphp">trunk/src/wp-includes/script-loader.php</a></li>
<li><a href="#trunksrcwpincludesthemephp">trunk/src/wp-includes/theme.php</a></li>
<li><a href="#trunktestsphpunitteststhemephp">trunk/tests/phpunit/tests/theme.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpblockphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-block.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-block.php  2025-02-14 10:34:23 UTC (rev 59822)
+++ trunk/src/wp-includes/class-wp-block.php    2025-02-14 18:36:48 UTC (rev 59823)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -608,6 +608,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                /*
+                * For Core blocks, these styles are only enqueued if `wp_should_load_separate_core_block_assets()` returns
+                * true. Otherwise these `wp_enqueue_style()` calls will not have any effect, as the Core blocks are relying on
+                * the combined 'wp-block-library' stylesheet instead, which is unconditionally enqueued.
+                */
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( ( ! empty( $this->block_type->style_handles ) ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        foreach ( $this->block_type->style_handles as $style_handle ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                wp_enqueue_style( $style_handle );
</span></span></pre></div>
<a id="trunksrcwpincludesglobalstylesandsettingsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/global-styles-and-settings.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/global-styles-and-settings.php      2025-02-14 10:34:23 UTC (rev 59822)
+++ trunk/src/wp-includes/global-styles-and-settings.php        2025-02-14 18:36:48 UTC (rev 59823)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -298,7 +298,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $block_css = $tree->get_styles_for_block( $metadata );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! wp_should_load_separate_core_block_assets() ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! wp_should_load_block_assets_on_demand() ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         wp_add_inline_style( 'global-styles', $block_css );
</span><span class="cx" style="display: block; padding: 0 10px">                        continue;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -306,7 +306,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $stylesheet_handle = 'global-styles';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                /*
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 * When `wp_should_load_separate_core_block_assets()` is true, block styles are
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * When `wp_should_load_block_assets_on_demand()` is true, block styles are
</ins><span class="cx" style="display: block; padding: 0 10px">                  * enqueued for each block on the page in class WP_Block's render function.
</span><span class="cx" style="display: block; padding: 0 10px">                 * This means there will be a handle in the styles queue for each of those blocks.
</span><span class="cx" style="display: block; padding: 0 10px">                 * Block-specific global styles should be attached to the global-styles handle, but
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -313,6 +313,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 * only for blocks on the page, thus we check if the block's handle is in the queue
</span><span class="cx" style="display: block; padding: 0 10px">                 * before adding the inline style.
</span><span class="cx" style="display: block; padding: 0 10px">                 * This conditional loading only applies to core blocks.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 * TODO: Explore how this could be expanded to third-party blocks as well.
</ins><span class="cx" style="display: block; padding: 0 10px">                  */
</span><span class="cx" style="display: block; padding: 0 10px">                if ( isset( $metadata['name'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( str_starts_with( $metadata['name'], 'core/' ) ) {
</span></span></pre></div>
<a id="trunksrcwpincludesscriptloaderphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/script-loader.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/script-loader.php   2025-02-14 10:34:23 UTC (rev 59822)
+++ trunk/src/wp-includes/script-loader.php     2025-02-14 18:36:48 UTC (rev 59823)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2487,20 +2487,21 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 5.8.0
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_enqueue_global_styles() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $separate_assets  = wp_should_load_separate_core_block_assets();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $assets_on_demand = wp_should_load_block_assets_on_demand();
</ins><span class="cx" style="display: block; padding: 0 10px">         $is_block_theme   = wp_is_block_theme();
</span><span class="cx" style="display: block; padding: 0 10px">        $is_classic_theme = ! $is_block_theme;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /*
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Global styles should be printed in the head when loading all styles combined.
-        * The footer should only be used to print global styles for classic themes with separate core assets enabled.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Global styles should be printed in the head for block themes, or for classic themes when loading assets on
+        * demand is disabled, which is the default.
+        * The footer should only be used for classic themes when loading assets on demand is enabled.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * See https://core.trac.wordpress.org/ticket/53494.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * See https://core.trac.wordpress.org/ticket/53494 and https://core.trac.wordpress.org/ticket/61965.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        if (
</span><span class="cx" style="display: block; padding: 0 10px">                ( $is_block_theme && doing_action( 'wp_footer' ) ) ||
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                ( $is_classic_theme && doing_action( 'wp_footer' ) && ! $separate_assets ) ||
-               ( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) && $separate_assets )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         ( $is_classic_theme && doing_action( 'wp_footer' ) && ! $assets_on_demand ) ||
+               ( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) && $assets_on_demand )
</ins><span class="cx" style="display: block; padding: 0 10px">         ) {
</span><span class="cx" style="display: block; padding: 0 10px">                return;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2567,25 +2568,22 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Checks whether separate styles should be loaded for core blocks on-render.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Checks whether separate styles should be loaded for core blocks.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * When this function returns true, other functions ensure that core blocks
- * only load their assets on-render, and each block loads its own, individual
- * assets. Third-party blocks only load their assets when rendered.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * When this function returns true, other functions ensure that core blocks use their own separate stylesheets.
+ * When this function returns false, all core blocks will use the single combined 'wp-block-library' stylesheet.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * When this function returns false, all core block assets are loaded regardless
- * of whether they are rendered in a page or not, because they are all part of
- * the `block-library/style.css` file. Assets for third-party blocks are always
- * enqueued regardless of whether they are rendered or not.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * As a side effect, the return value will by default result in block assets to be loaded on demand, via the
+ * {@see wp_should_load_block_assets_on_demand()} function. This behavior can be separately altered via that function.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * This only affects front end and not the block editor screens.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.8.0
+ * @see @see wp_should_load_block_assets_on_demand()
</ins><span class="cx" style="display: block; padding: 0 10px">  * @see wp_enqueue_registered_block_scripts_and_styles()
</span><span class="cx" style="display: block; padding: 0 10px">  * @see register_block_style_handle()
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 5.8.0
- *
- * @return bool Whether separate assets will be loaded.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return bool Whether separate core block assets will be loaded.
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_should_load_separate_core_block_assets() {
</span><span class="cx" style="display: block; padding: 0 10px">        if ( is_admin() || is_feed() || wp_is_rest_endpoint() ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2607,6 +2605,50 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Checks whether block styles should be loaded only on-render.
+ *
+ * When this function returns true, other functions ensure that blocks only load their assets on-render.
+ * When this function returns false, all block assets are loaded regardless of whether they are rendered in a page.
+ *
+ * The default return value depends on the result of {@see wp_should_load_separate_core_block_assets()}, which controls
+ * whether Core block stylesheets should be loaded separately or via a combined 'wp-block-library' stylesheet.
+ *
+ * This only affects front end and not the block editor screens.
+ *
+ * @since 6.8.0
+ * @see wp_should_load_separate_core_block_assets()
+ *
+ * @return bool Whether to load block assets only when they are rendered.
+ */
+function wp_should_load_block_assets_on_demand() {
+       if ( is_admin() || is_feed() || wp_is_rest_endpoint() ) {
+               return false;
+       }
+
+       /*
+        * For backward compatibility, the default return value for this function is based on the return value of
+        * `wp_should_load_separate_core_block_assets()`. Initially, this function used to control both of these concerns.
+        */
+       $load_assets_on_demand = wp_should_load_separate_core_block_assets();
+
+       /**
+        * Filters whether block styles should be loaded on demand.
+        *
+        * Returning false loads all block assets, regardless of whether they are rendered in a page or not.
+        * Returning true loads block assets only when they are rendered.
+        *
+        * The default value of the filter depends on the result of {@see wp_should_load_separate_core_block_assets()},
+        * which controls whether Core block stylesheets should be loaded separately or via a combined 'wp-block-library'
+        * stylesheet.
+        *
+        * @since 6.8.0
+        *
+        * @param bool $load_assets_on_demand Whether to load block assets only when they are rendered.
+        */
+       return apply_filters( 'should_load_block_assets_on_demand', $load_assets_on_demand );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Enqueues registered block scripts and styles, depending on current rendered
</span><span class="cx" style="display: block; padding: 0 10px">  * context (only enqueuing editor scripts while in context of the editor).
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2617,7 +2659,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_enqueue_registered_block_scripts_and_styles() {
</span><span class="cx" style="display: block; padding: 0 10px">        global $current_screen;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( wp_should_load_separate_core_block_assets() ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( wp_should_load_block_assets_on_demand() ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 return;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2624,6 +2666,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $load_editor_scripts_and_styles = is_admin() && wp_should_load_block_editor_scripts_and_styles();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $block_registry = WP_Block_Type_Registry::get_instance();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /*
+        * Block styles are only enqueued if they're registered. For core blocks, this is only the case if
+        * `wp_should_load_separate_core_block_assets()` returns true. Otherwise they use the single combined
+        * 'wp-block-library` stylesheet. See also `register_core_block_style_handles()`.
+        * Since `wp_enqueue_style()` does not trigger warnings if the style is not registered, it is okay to not cater for
+        * this behavior here and simply call `wp_enqueue_style()` unconditionally.
+        */
</ins><span class="cx" style="display: block; padding: 0 10px">         foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
</span><span class="cx" style="display: block; padding: 0 10px">                // Front-end and editor styles.
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $block_type->style_handles as $style_handle ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2665,8 +2715,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $styles as $style_properties ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( isset( $style_properties['style_handle'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                // If the site loads separate styles per-block, enqueue the stylesheet on render.
-                               if ( wp_should_load_separate_core_block_assets() ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         // If the site loads block styles on demand, enqueue the stylesheet on render.
+                               if ( wp_should_load_block_assets_on_demand() ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                         add_filter(
</span><span class="cx" style="display: block; padding: 0 10px">                                                'render_block',
</span><span class="cx" style="display: block; padding: 0 10px">                                                static function ( $html, $block ) use ( $block_name, $style_properties ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2687,8 +2737,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                // Default to "wp-block-library".
</span><span class="cx" style="display: block; padding: 0 10px">                                $handle = 'wp-block-library';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                // If the site loads separate styles per-block, check if the block has a stylesheet registered.
-                               if ( wp_should_load_separate_core_block_assets() ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         // If the site loads block styles on demand, check if the block has a stylesheet registered.
+                               if ( wp_should_load_block_assets_on_demand() ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                         $block_stylesheet_handle = generate_block_asset_handle( $block_name, 'style' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                        if ( isset( $wp_styles->registered[ $block_stylesheet_handle ] ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3187,7 +3237,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px">  * Enqueues a stylesheet for a specific block.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * If the theme has opted-in to separate-styles loading,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * If the theme has opted-in to load block styles on demand,
</ins><span class="cx" style="display: block; padding: 0 10px">  * then the stylesheet will be enqueued on-render,
</span><span class="cx" style="display: block; padding: 0 10px">  * otherwise when the block inits.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3255,7 +3305,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        };
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $hook = did_action( 'wp_enqueue_scripts' ) ? 'wp_footer' : 'wp_enqueue_scripts';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( wp_should_load_separate_core_block_assets() ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( wp_should_load_block_assets_on_demand() ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 /**
</span><span class="cx" style="display: block; padding: 0 10px">                 * Callback function to register and enqueue styles.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span></span></pre></div>
<a id="trunksrcwpincludesthemephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/theme.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/theme.php   2025-02-14 10:34:23 UTC (rev 59822)
+++ trunk/src/wp-includes/theme.php     2025-02-14 18:36:48 UTC (rev 59823)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4395,6 +4395,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        add_theme_support( 'automatic-feed-links' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        add_filter( 'should_load_separate_core_block_assets', '__return_true' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        add_filter( 'should_load_block_assets_on_demand', '__return_true' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /*
</span><span class="cx" style="display: block; padding: 0 10px">         * Remove the Customizer's Menus panel when block theme is active.
</span></span></pre></div>
<a id="trunktestsphpunitteststhemephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/theme.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/theme.php       2025-02-14 10:34:23 UTC (rev 59822)
+++ trunk/tests/phpunit/tests/theme.php 2025-02-14 18:36:48 UTC (rev 59823)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -966,6 +966,50 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Tests that block themes load block assets on demand by default.
+        *
+        * @ticket 61965
+        *
+        * @covers ::_add_default_theme_supports
+        * @covers ::wp_should_load_block_assets_on_demand
+        */
+       public function test_block_theme_should_load_block_assets_on_demand_by_default() {
+               $this->helper_requires_block_theme();
+
+               add_filter( 'should_load_block_assets_on_demand', '__return_false' );
+
+               $this->assertFalse(
+                       wp_should_load_block_assets_on_demand(),
+                       'Could not disable loading block assets on demand.'
+               );
+
+               do_action( 'after_setup_theme' );
+               add_filter( 'should_load_separate_core_block_assets', '__return_false' );
+
+               $this->assertTrue(
+                       wp_should_load_block_assets_on_demand(),
+                       'Block themes do not load block assets on demand by default.'
+               );
+       }
+
+       /**
+        * Tests that block themes load block assets on demand by default even when loading separate core block assets is disabled.
+        *
+        * @ticket 61965
+        *
+        * @covers ::_add_default_theme_supports
+        * @covers ::wp_should_load_block_assets_on_demand
+        */
+       public function test_block_theme_should_load_block_assets_on_demand_by_default_even_with_separate_core_block_assets_disabled() {
+               $this->helper_requires_block_theme();
+
+               do_action( 'after_setup_theme' );
+               add_filter( 'should_load_separate_core_block_assets', '__return_false' );
+
+               $this->assertTrue( wp_should_load_block_assets_on_demand() );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Tests that a theme in the custom test data theme directory is recognized.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 18298
</span></span></pre>
</div>
</div>

</body>
</html>