Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
7 changes: 7 additions & 0 deletions src/wp-includes/blocks/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 ];
Expand Down
16 changes: 2 additions & 14 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}

/**
Expand Down
37 changes: 37 additions & 0 deletions tests/phpunit/tests/blocks/registerCoreBlockStyleHandles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 );
}

Expand Down