Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global Styles: Fix for third-party blocks #3529

Closed
Closed
Show file tree
Hide file tree
Changes from 3 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
15 changes: 11 additions & 4 deletions src/wp-includes/global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,18 @@ function wp_add_global_styles_for_blocks() {
continue;
}

$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.
*/
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.
Expand All @@ -242,8 +246,11 @@ function ( $item ) {
)
);
if ( isset( $result[0] ) ) {
$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 );
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/phpunit/data/themedir1/block-theme/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
"filter": {
"duotone": "var(--wp--preset--duotone--custom-duotone)"
}
},
"my/third-party-block": {
Copy link
Member

@oandregal oandregal Oct 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These styles will be ignored by any other tests because the block my/third-party-block won't be registered.

"color": {
"background": "hotpink"
}
}
},
"elements": {
Expand Down
94 changes: 94 additions & 0 deletions tests/phpunit/tests/theme/wpGetGlobalStylesForBlocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/**
* Tests wp_add_global_styles_for_blocks().
*
* @group themes
*/
class Tests_Theme_wpGetGlobalStylesForBlocks extends WP_UnitTestCase {
ockham marked this conversation as resolved.
Show resolved Hide resolved

/**
* Theme root directory.
*
* @var string
*/
private $theme_root;

/**
* Original theme directory.
*
* @var string
*/
private $orig_theme_dir;

public function set_up() {
parent::set_up();

$this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
$this->theme_root = realpath( DIR_TESTDATA . '/themedir1' );

// /themes is necessary as theme.php functions assume /themes is the root if there is only one root.
$GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root );

// Set up the new root.
add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) );
add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) );
add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) );

// Clear caches.
wp_clean_themes_cache();
unset( $GLOBALS['wp_themes'] );
}

public function tear_down() {
$GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;

// Clear up the filters to modify the theme root.
remove_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) );
remove_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) );
remove_filter( 'template_root', array( $this, 'filter_set_theme_root' ) );

wp_clean_themes_cache();
unset( $GLOBALS['wp_themes'] );

parent::tear_down();
}

/**
* Cleans up global scope.
*
* @global WP_Styles $wp_styles
*/
public function clean_up_global_scope() {
global $wp_styles;
parent::clean_up_global_scope();
$wp_styles = null;
}

public function filter_set_theme_root() {
return $this->theme_root;
}

public function test_styles_for_blocks() {
ockham marked this conversation as resolved.
Show resolved Hide resolved
switch_theme( 'block-theme' );

// The 3rd party block styles are only missing
// if the assets are loaded per block.
add_filter( 'should_load_separate_core_block_assets', '__return_true' );

$name = 'my/third-party-block';
$settings = array(
'icon' => 'text',
'category' => 'common',
'render_callback' => 'foo',
);
register_block_type( $name, $settings );
wp_enqueue_global_styles();
Copy link
Member

@oandregal oandregal Oct 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are interested in testing wp_add_global_styles_for_blocks. Though, because of how it's designed, I wasn't lucky using it directly. Hence, I had to resort to call this function (the one that orchestrates all the styles).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it'd be great if we could isolate the test to really just run wp_add_global_styles_for_blocks 🤔

unregister_block_type( $name );

$this->assertStringContainsString( '.wp-block-my-third-party-block{background-color: hotpink;}', get_echo( 'wp_print_styles' ), '3rd party block styles are included' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The styles added to theme.json for the my/third-party-block generate this style rule: .wp-block-my-third-party-block{background-color: hotpink;}.


switch_theme( WP_DEFAULT_THEME );
}

}