From 37b6f746d7792aa604f31b9b8d65e5f763c9ca8d Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Thu, 27 Oct 2022 09:20:29 -0500 Subject: [PATCH] Check for core block in tests. --- .../global-styles-and-settings.php | 26 ++++++++------ .../theme/wpAddGlobalStylesForBlocks.php | 34 +++++++++++++++++-- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index b6c8c91b464c0..e2be7d0e5f709 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -218,18 +218,20 @@ function wp_add_global_styles_for_blocks() { continue; } - $stylesheet_handle = 'global-styles'; +// $stylesheet_handle = 'global-styles'; if ( isset( $metadata['name'] ) ) { + $block_name = str_replace( 'core/', '', $metadata['name'] ); /* * These block styles are added on block_render. * This hooks inline CSS to them so that they are loaded conditionally * based on whether or not the block is used on the page. */ - 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 ); + wp_add_inline_style( 'wp-block-' . $block_name, $block_css ); +// 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 ); } // The likes of block element styles from theme.json do not have $metadata['name'] set. @@ -246,11 +248,13 @@ function ( $item ) { ) ); if ( isset( $result[0] ) ) { - 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 ); + $block_name = str_replace( 'core/', '', $result[0] ); + wp_add_inline_style( 'wp-block-' . $block_name, $block_css ); +// 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 ); } } } diff --git a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php index 33bf1d52c836f..6d55f24ec9988 100644 --- a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php +++ b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php @@ -95,7 +95,7 @@ public function test_third_party_blocks_inline_styles_get_registered_to_global_s /** * @ticket 56915 */ - public function test_third_party_blocks_inline_styles_get_rendered() { + 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' ); @@ -103,9 +103,39 @@ public function test_third_party_blocks_inline_styles_get_rendered() { 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;}', - get_echo( 'wp_print_styles' ) + $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' ); }