<!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>[54703] trunk: Editor: Ensure global styles are rendered for third-party blocks.</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/54703">54703</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/54703","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>davidbaumwald</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-10-27 15:39:20 +0000 (Thu, 27 Oct 2022)</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: Ensure global styles are rendered for third-party blocks.

This change ensures custom styles for all third-party blocks are rendered on the front end if assets are set to be loaded on a per-block basis.  Additionally, this change includes new unit tests to help prevent a similar bug in the future.

Props scruffian, aristath, poena, wildworks, ajlende, andraganescu, ndiego, gigitux, cbravobernal, ramonopoly, andrewserong, oandregal, hellofromTonya, bernhard-reiter.
Fixes <a href="https://core.trac.wordpress.org/ticket/56915">#56915</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkphpcsxmldist">trunk/phpcs.xml.dist</a></li>
<li><a href="#trunksrcwpincludesglobalstylesandsettingsphp">trunk/src/wp-includes/global-styles-and-settings.php</a></li>
<li><a href="#trunktestsphpunitdatathemedir1blockthemethemejson">trunk/tests/phpunit/data/themedir1/block-theme/theme.json</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunitteststhemebasephp">trunk/tests/phpunit/tests/theme/base.php</a></li>
<li><a href="#trunktestsphpunitteststhemewpAddGlobalStylesForBlocksphp">trunk/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkphpcsxmldist"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/phpcs.xml.dist</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/phpcs.xml.dist      2022-10-27 15:21:47 UTC (rev 54702)
+++ trunk/phpcs.xml.dist        2022-10-27 15:39:20 UTC (rev 54703)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -244,6 +244,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                <element value="WP_Import_UnitTestCase"/>
</span><span class="cx" style="display: block; padding: 0 10px">                                <element value="WP_Test_Adjacent_Image_Link_TestCase"/>
</span><span class="cx" style="display: block; padding: 0 10px">                                <element value="WP_Tests_Image_Resize_UnitTestCase"/>
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                <element value="WP_Theme_UnitTestCase"/>
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                <!-- Mock classes. -->
</span><span class="cx" style="display: block; padding: 0 10px">                                <element value="Spy_REST_Server"/>
</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      2022-10-27 15:21:47 UTC (rev 54702)
+++ trunk/src/wp-includes/global-styles-and-settings.php        2022-10-27 15:39:20 UTC (rev 54703)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -218,14 +218,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        continue;
</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">+                $stylesheet_handle = 'global-styles';
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( isset( $metadata['name'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $block_name = str_replace( 'core/', '', $metadata['name'] );
</del><span class="cx" style="display: block; padding: 0 10px">                         /*
</span><span class="cx" style="display: block; padding: 0 10px">                         * These block styles are added on block_render.
</span><span class="cx" style="display: block; padding: 0 10px">                         * This hooks inline CSS to them so that they are loaded conditionally
</span><span class="cx" style="display: block; padding: 0 10px">                         * based on whether or not the block is used on the page.
</span><span class="cx" style="display: block; padding: 0 10px">                         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        wp_add_inline_style( 'wp-block-' . $block_name, $block_css );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( str_starts_with( $metadata['name'], 'core/' ) ) {
+                               $block_name        = str_replace( 'core/', '', $metadata['name'] );
+                               $stylesheet_handle = 'wp-block-' . $block_name;
+                       }
+                       wp_add_inline_style( $stylesheet_handle, $block_css );
</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">                // The likes of block element styles from theme.json do not have  $metadata['name'] set.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -242,8 +246,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">                        if ( isset( $result[0] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $block_name = str_replace( 'core/', '', $result[0] );
-                               wp_add_inline_style( 'wp-block-' . $block_name, $block_css );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( str_starts_with( $result[0], 'core/' ) ) {
+                                       $block_name        = str_replace( 'core/', '', $result[0] );
+                                       $stylesheet_handle = 'wp-block-' . $block_name;
+                               }
+                               wp_add_inline_style( $stylesheet_handle, $block_css );
</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">        }
</span></span></pre></div>
<a id="trunktestsphpunitdatathemedir1blockthemethemejson"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/data/themedir1/block-theme/theme.json</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/data/themedir1/block-theme/theme.json 2022-10-27 15:21:47 UTC (rev 54702)
+++ trunk/tests/phpunit/data/themedir1/block-theme/theme.json   2022-10-27 15:39:20 UTC (rev 54703)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -69,6 +69,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                "filter": {
</span><span class="cx" style="display: block; padding: 0 10px">                                        "duotone": "var(--wp--preset--duotone--custom-duotone)"
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        },
+                       "my/third-party-block": {
+                               "color": {
+                                       "background": "hotpink"
+                               }
</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">                "elements": {
</span></span></pre></div>
<a id="trunktestsphpunitteststhemebasephp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/theme/base.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/theme/base.php                          (rev 0)
+++ trunk/tests/phpunit/tests/theme/base.php    2022-10-27 15:39:20 UTC (rev 54703)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,66 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+abstract class WP_Theme_UnitTestCase extends WP_UnitTestCase {
+
+       /**
+        * Theme root directory.
+        *
+        * @var string
+        */
+       private $theme_root;
+
+       /**
+        * Original theme directory.
+        *
+        * @var string
+        */
+       private $orig_theme_dir;
+
+       public function set_up() {
+               parent::set_up();
+
+               $this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
+               $this->theme_root     = realpath( DIR_TESTDATA . '/themedir1' );
+
+               // /themes is necessary as theme.php functions assume /themes is the root if there is only one root.
+               $GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root );
+
+               // Set up the new root.
+               add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) );
+               add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) );
+               add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) );
+
+               // Clear caches.
+               wp_clean_themes_cache();
+               unset( $GLOBALS['wp_themes'] );
+       }
+
+       public function tear_down() {
+               $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;
+
+               // Clear up the filters to modify the theme root.
+               remove_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) );
+               remove_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) );
+               remove_filter( 'template_root', array( $this, 'filter_set_theme_root' ) );
+
+               wp_clean_themes_cache();
+               unset( $GLOBALS['wp_themes'] );
+
+               parent::tear_down();
+       }
+
+       /**
+        * Cleans up global scope.
+        *
+        * @global WP_Styles $wp_styles
+        */
+       public function clean_up_global_scope() {
+               global $wp_styles;
+               parent::clean_up_global_scope();
+               $wp_styles = null;
+       }
+
+       public function filter_set_theme_root() {
+               return $this->theme_root;
+       }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/theme/base.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunitteststhemewpAddGlobalStylesForBlocksphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php                            (rev 0)
+++ trunk/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php      2022-10-27 15:39:20 UTC (rev 54703)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,160 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+require_once __DIR__ . '/base.php';
+
+/**
+ * Tests wp_add_global_styles_for_blocks().
+ *
+ * @group themes
+ *
+ * @covers ::wp_add_global_styles_for_blocks
+ */
+class Tests_Theme_WpAddGlobalStylesForBlocks extends WP_Theme_UnitTestCase {
+
+       /**
+        * Test blocks to unregister at cleanup.
+        *
+        * @var array
+        */
+       private $test_blocks = array();
+
+       public function tear_down() {
+               // Unregister test blocks.
+               if ( ! empty( $this->test_blocks ) ) {
+                       foreach ( $this->test_blocks as $test_block ) {
+                               unregister_block_type( $test_block );
+                       }
+                       $this->test_blocks = array();
+               }
+
+               parent::tear_down();
+       }
+
+       /**
+        * @ticket 56915
+        */
+       public function test_third_party_blocks_inline_styles_not_register_to_global_styles() {
+               switch_theme( 'block-theme' );
+
+               wp_register_style( 'global-styles', false, array(), true, true );
+               wp_add_global_styles_for_blocks();
+
+               $this->assertNotContains(
+                       '.wp-block-my-third-party-block{background-color: hotpink;}',
+                       $this->get_global_styles()
+               );
+       }
+
+       /**
+        * @ticket 56915
+        */
+       public function test_third_party_blocks_inline_styles_get_registered_to_global_styles() {
+               $this->set_up_third_party_block();
+
+               wp_register_style( 'global-styles', false, array(), true, true );
+
+               $this->assertNotContains(
+                       '.wp-block-my-third-party-block{background-color: hotpink;}',
+                       $this->get_global_styles(),
+                       'Third party block inline style should not be registered before running wp_add_global_styles_for_blocks()'
+               );
+
+               wp_add_global_styles_for_blocks();
+
+               $this->assertContains(
+                       '.wp-block-my-third-party-block{background-color: hotpink;}',
+                       $this->get_global_styles(),
+                       'Third party block inline style should be registered after running wp_add_global_styles_for_blocks()'
+               );
+       }
+
+       /**
+        * @ticket 56915
+        */
+       public function test_third_party_blocks_inline_styles_get_registered_to_global_styles_when_per_block() {
+               $this->set_up_third_party_block();
+               add_filter( 'should_load_separate_core_block_assets', '__return_true' );
+
+               wp_register_style( 'global-styles', false, array(), true, true );
+
+               $this->assertNotContains(
+                       '.wp-block-my-third-party-block{background-color: hotpink;}',
+                       $this->get_global_styles(),
+                       'Third party block inline style should not be registered before running wp_add_global_styles_for_blocks()'
+               );
+
+               wp_add_global_styles_for_blocks();
+
+               $this->assertContains(
+                       '.wp-block-my-third-party-block{background-color: hotpink;}',
+                       $this->get_global_styles(),
+                       'Third party block inline style should be registered after running wp_add_global_styles_for_blocks()'
+               );
+       }
+
+       /**
+        * @ticket 56915
+        */
+       public function test_third_party_blocks_inline_styles_get_rendered_when_per_block() {
+               $this->set_up_third_party_block();
+               add_filter( 'should_load_separate_core_block_assets', '__return_true' );
+
+               wp_register_style( 'global-styles', false, array(), true, true );
+               wp_enqueue_style( 'global-styles' );
+               wp_add_global_styles_for_blocks();
+
+               $actual = get_echo( 'wp_print_styles' );
+
+               $this->assertStringContainsString(
+                       '.wp-block-my-third-party-block{background-color: hotpink;}',
+                       $actual,
+                       'Third party block inline style should render'
+               );
+               $this->assertStringNotContainsString(
+                       '.wp-block-post-featured-image',
+                       $actual,
+                       'Core block should not render'
+               );
+       }
+
+       /**
+        * @ticket 56915
+        */
+       public function test_blocks_inline_styles_get_rendered() {
+               wp_register_style( 'global-styles', false, array(), true, true );
+               wp_enqueue_style( 'global-styles' );
+               wp_add_global_styles_for_blocks();
+
+               $actual = get_echo( 'wp_print_styles' );
+
+               $this->assertStringContainsString(
+                       '.wp-block-my-third-party-block{background-color: hotpink;}',
+                       $actual,
+                       'Third party block inline style should render'
+               );
+               $this->assertStringContainsString(
+                       '.wp-block-post-featured-image',
+                       $actual,
+                       'Core block should render'
+               );
+       }
+
+       private function set_up_third_party_block() {
+               switch_theme( 'block-theme' );
+
+               $name     = 'my/third-party-block';
+               $settings = array(
+                       'icon'            => 'text',
+                       'category'        => 'common',
+                       'render_callback' => 'foo',
+               );
+               register_block_type( $name, $settings );
+
+               $this->test_blocks[] = $name;
+       }
+
+       private function get_global_styles() {
+               $actual = wp_styles()->get_data( 'global-styles', 'after' );
+               return is_array( $actual ) ? $actual : array();
+       }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>

</body>
</html>