diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 9b1884c660..c747fe9426 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -219,7 +219,7 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) { // Check whether styles should have a ".min" suffix or not. $suffix = SCRIPT_DEBUG ? '' : '.min'; if ( $is_core_block ) { - $style_path = "style$suffix.css"; + $style_path = ( 'editorStyle' === $field_name ) ? "editor{$suffix}.css" : "style{$suffix}.css"; } $style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) ); @@ -239,7 +239,8 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) { if ( $is_theme_block ) { $style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) ); } elseif ( $is_core_block ) { - $style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . "/style$suffix.css" ); + // All possible $style_path variants for core blocks are hard-coded above. + $style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . '/' . $style_path ); } } else { $style_uri = false; diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index ac9ba7ada3..5711d9c1a7 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -322,6 +322,76 @@ class Tests_Blocks_Register extends WP_UnitTestCase { $this->assertFalse( $result ); } + /** + * @ticket 58605 + * + * @dataProvider data_register_block_style_handle_uses_correct_core_stylesheet + * + * @param string $block_json_path Path to the `block.json` file, relative to ABSPATH. + * @param string $style_field Either 'style' or 'editorStyle'. + * @param string|bool $expected_path Expected path of registered stylesheet, relative to ABSPATH. + */ + public function test_register_block_style_handle_uses_correct_core_stylesheet( $block_json_path, $style_field, $expected_path ) { + $metadata_file = ABSPATH . $block_json_path; + $metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) ); + + $block_name = str_replace( 'core/', '', $metadata['name'] ); + + // Normalize metadata similar to `register_block_type_from_metadata()`. + $metadata['file'] = wp_normalize_path( realpath( $metadata_file ) ); + if ( ! isset( $metadata['style'] ) ) { + $metadata['style'] = "wp-block-$block_name"; + } + if ( ! isset( $metadata['editorStyle'] ) ) { + $metadata['editorStyle'] = "wp-block-{$block_name}-editor"; + } + + // Ensure block assets are separately registered. + add_filter( 'should_load_separate_core_block_assets', '__return_true' ); + + /* + * Account for minified asset path and ensure the file exists. + * This may not be the case in the testing environment since it requires the build process to place them. + */ + if ( is_string( $expected_path ) ) { + $expected_path = str_replace( '.css', wp_scripts_get_suffix() . '.css', $expected_path ); + self::touch( ABSPATH . $expected_path ); + } + + $result = register_block_style_handle( $metadata, $style_field ); + $this->assertSame( $metadata[ $style_field ], $result, 'Core block registration failed' ); + if ( $expected_path ) { + $this->assertStringEndsWith( $expected_path, wp_styles()->registered[ $result ]->src, 'Core block stylesheet path incorrect' ); + } else { + $this->assertFalse( wp_styles()->registered[ $result ]->src, 'Core block stylesheet src should be false' ); + } + } + + public function data_register_block_style_handle_uses_correct_core_stylesheet() { + return array( + 'block with style' => array( + WPINC . '/blocks/archives/block.json', + 'style', + WPINC . '/blocks/archives/style.css', + ), + 'block with editor style' => array( + WPINC . '/blocks/archives/block.json', + 'editorStyle', + WPINC . '/blocks/archives/editor.css', + ), + 'block without style' => array( + WPINC . '/blocks/widget-group/block.json', + 'style', + false, + ), + 'block without editor style' => array( + WPINC . '/blocks/widget-group/block.json', + 'editorStyle', + false, + ), + ); + } + /** * @ticket 50263 */