From 308066bd8493d5aad4b253f5bd034e750259b3bd Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Sun, 5 Mar 2023 16:46:30 -0800 Subject: [PATCH] Move current logic to new private function and add test coverage for both unit and integration. --- .../global-styles-and-settings.php | 45 ++++++++---- .../data/themedir1/block-theme/theme.json | 7 ++ .../theme/wpAddGlobalStylesForBlocks.php | 73 +++++++++++++++++++ 3 files changed, 111 insertions(+), 14 deletions(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index fe2393abea974..d4a9da6949be9 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -352,20 +352,10 @@ function wp_add_global_styles_for_blocks() { // The likes of block element styles from theme.json do not have $metadata['name'] set. if ( ! isset( $metadata['name'] ) && ! empty( $metadata['path'] ) ) { - $result = array_values( - array_filter( - $metadata['path'], - function ( $item ) { - if ( strpos( $item, 'core/' ) !== false ) { - return true; - } - return false; - } - ) - ); - if ( isset( $result[0] ) ) { - if ( str_starts_with( $result[0], 'core/' ) ) { - $block_name = str_replace( 'core/', '', $result[0] ); + $block_name = wp_get_block_name_from_theme_json_path( $metadata['path'] ); + if ( $block_name ) { + if ( str_starts_with( $block_name, 'core/' ) ) { + $block_name = str_replace( 'core/', '', $block_name ); $stylesheet_handle = 'wp-block-' . $block_name; } wp_add_inline_style( $stylesheet_handle, $block_css ); @@ -374,6 +364,33 @@ function ( $item ) { } } +/** + * Gets the block name from a given theme.json path. + * + * @since 6.3.0 + * @access private + * + * @param string $path Path to a property in theme.json. + * @return string Identified block name, or empty string if none found. + */ +function wp_get_block_name_from_theme_json_path( $path ) { + $result = array_values( + array_filter( + $path, + function ( $item ) { + if ( strpos( $item, 'core/' ) !== false ) { + return true; + } + return false; + } + ) + ); + if ( isset( $result[0] ) ) { + return $result[0]; + } + return ''; +} + /** * Checks whether a theme or its parent has a theme.json file. * diff --git a/tests/phpunit/data/themedir1/block-theme/theme.json b/tests/phpunit/data/themedir1/block-theme/theme.json index 76d075aab7d28..982ad8ca39f0e 100644 --- a/tests/phpunit/data/themedir1/block-theme/theme.json +++ b/tests/phpunit/data/themedir1/block-theme/theme.json @@ -73,6 +73,13 @@ "my/third-party-block": { "color": { "background": "hotpink" + }, + "elements": { + "cite": { + "color": { + "text": "white" + } + } } } }, diff --git a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php index 6d55f24ec9988..2dabe531fa3f5 100644 --- a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php +++ b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php @@ -139,6 +139,79 @@ public function test_blocks_inline_styles_get_rendered() { ); } + /** + * @ticket 57868 + */ + public function test_third_party_blocks_inline_styles_for_elements_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 cite{color: white;}', + $actual + ); + } + + /** + * @ticket 57868 + */ + public function test_third_party_blocks_inline_styles_for_elements_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 cite{color: white;}', + $actual + ); + } + + /** + * @ticket 57868 + * @dataProvider data_wp_get_block_name_from_theme_json_path + */ + public function test_wp_get_block_name_from_theme_json_path( $path, $expected ) { + $block_name = wp_get_block_name_from_theme_json_path( $path ); + $this->assertSame( $expected, $block_name ); + } + + public function data_wp_get_block_name_from_theme_json_path() { + return array( + 'core block styles' => array( + array( 'styles', 'blocks', 'core/navigation' ), + 'core/navigation', + ), + 'core block element styles' => array( + array( 'styles', 'blocks', 'core/navigation', 'elements', 'link' ), + 'core/navigation', + ), + 'custom block styles' => array( + array( 'styles', 'blocks', 'my/third-party-block' ), + 'my/third-party-block', + ), + 'custom block element styles' => array( + array( 'styles', 'blocks', 'my/third-party-block', 'elements', 'cite' ), + 'my/third-party-block', + ), + 'custom block wrong format' => array( + array( 'styles', 'my/third-party-block' ), + '', + ), + 'invalid path but works for BC' => array( + array( 'something', 'core/image' ), + 'core/image', + ), + ); + } + private function set_up_third_party_block() { switch_theme( 'block-theme' );