diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 5fa7f1d6d0ddc..7b96ebc2ba9c6 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -373,6 +373,12 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { if ( ! isset( $metadata['style'] ) ) { $metadata['style'] = "wp-block-$block_name"; } + + if ( current_theme_supports( 'wp-block-styles' ) && wp_should_load_separate_core_block_assets() ) { + $metadata['style'] = (array) $metadata['style']; + $metadata['style'][] = "wp-block-{$block_name}-theme"; + } + if ( ! isset( $metadata['editorStyle'] ) ) { $metadata['editorStyle'] = "wp-block-{$block_name}-editor"; } diff --git a/src/wp-includes/blocks/index.php b/src/wp-includes/blocks/index.php index 49bf114393b6a..403930c5ca55b 100644 --- a/src/wp-includes/blocks/index.php +++ b/src/wp-includes/blocks/index.php @@ -40,6 +40,10 @@ function register_core_block_style_handles() { 'editorStyle' => 'editor', ); + if( current_theme_supports( 'wp-block-styles' ) ){ + $style_fields['themeStyle'] = 'theme'; + } + /* * Ignore transient cache when `WP_DEBUG` is enabled. Why? To avoid interfering with * the core developer's workflow. @@ -68,6 +72,9 @@ function register_core_block_style_handles() { if ( ! isset( $schema['editorStyle'] ) ) { $schema['editorStyle'] = "wp-block-{$name}-editor"; } + if ( ! isset( $schema['themeStyle'] ) ) { + $schema['themeStyle'] = "wp-block-{$name}-theme"; + } foreach ( $style_fields as $style_field => $filename ) { $style_handle = $schema[ $style_field ]; diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 3ee28ce23c9d4..168e88d2e5809 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -2372,20 +2372,8 @@ function wp_common_block_scripts_and_styles() { wp_enqueue_style( 'wp-block-library' ); - if ( current_theme_supports( 'wp-block-styles' ) ) { - if ( wp_should_load_separate_core_block_assets() ) { - $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? 'css' : 'min.css'; - $files = glob( __DIR__ . "/blocks/**/theme.$suffix" ); - foreach ( $files as $path ) { - $block_name = basename( dirname( $path ) ); - if ( is_rtl() && file_exists( __DIR__ . "/blocks/$block_name/theme-rtl.$suffix" ) ) { - $path = __DIR__ . "/blocks/$block_name/theme-rtl.$suffix"; - } - wp_add_inline_style( "wp-block-{$block_name}", file_get_contents( $path ) ); - } - } else { - wp_enqueue_style( 'wp-block-library-theme' ); - } + if ( current_theme_supports( 'wp-block-styles' ) && ! wp_should_load_separate_core_block_assets() ) { + wp_enqueue_style( 'wp-block-library-theme' ); } /** diff --git a/tests/phpunit/tests/blocks/registerCoreBlockStyleHandles.php b/tests/phpunit/tests/blocks/registerCoreBlockStyleHandles.php index 0f11962248bf2..7edd1d4f25c28 100644 --- a/tests/phpunit/tests/blocks/registerCoreBlockStyleHandles.php +++ b/tests/phpunit/tests/blocks/registerCoreBlockStyleHandles.php @@ -99,6 +99,39 @@ public function test_wp_should_load_separate_core_block_assets_true( $name, $sch } } + /** + * @ticket 58528 + * + * @dataProvider data_block_data + */ + public function test_wp_should_load_separate_core_block_assets_current_theme_supports( $name, $schema ) { + add_filter( 'should_load_separate_core_block_assets', '__return_true' ); + add_theme_support( 'wp-block-styles' ); + register_core_block_style_handles(); + + $wp_styles = $GLOBALS['wp_styles']; + + $style_fields = self::STYLE_FIELDS; + $style_fields['themeStyle'] = 'theme'; + + foreach ( $style_fields as $style_field => $filename ) { + $style_handle = $schema[ $style_field ]; + if ( is_array( $style_handle ) ) { + continue; + } + + $this->assertArrayHasKey( $style_handle, $wp_styles->registered, 'The key should exist, as this style should be registered' ); + if ( false === $wp_styles->registered[ $style_handle ]->src ) { + $this->assertEmpty( $wp_styles->registered[ $style_handle ]->extra, 'If source is false, not style path should be set' ); + } else { + $this->assertStringContainsString( $this->includes_url, $wp_styles->registered[ $style_handle ]->src, 'Source of style should contain the includes url' ); + $this->assertNotEmpty( $wp_styles->registered[ $style_handle ]->extra, 'The path of the style should exist' ); + $this->assertArrayHasKey( 'path', $wp_styles->registered[ $style_handle ]->extra, 'The path key of the style should exist in extra array' ); + $this->assertNotEmpty( $wp_styles->registered[ $style_handle ]->extra['path'], 'The path key of the style should not be empty' ); + } + } + } + public function data_block_data() { $core_blocks_meta = require ABSPATH . WPINC . '/blocks/blocks-json.php'; @@ -112,6 +145,10 @@ public function data_block_data() { $schema['editorStyle'] = "wp-block-{$name}-editor"; } + if ( ! isset( $schema['themeStyle'] ) ) { + $schema['themeStyle'] = "wp-block-{$name}-theme"; + } + $data[ $name ] = array( $name, $schema ); }