Skip to content

Commit

Permalink
Check for core block in tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
hellofromtonya committed Oct 27, 2022
1 parent 19f6271 commit 37b6f74
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
26 changes: 15 additions & 11 deletions src/wp-includes/global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,18 +218,20 @@ function wp_add_global_styles_for_blocks() {
continue;
}

$stylesheet_handle = 'global-styles';
// $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.
*/
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 );
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 @@ -246,11 +248,13 @@ function ( $item ) {
)
);
if ( isset( $result[0] ) ) {
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 );
$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
34 changes: 32 additions & 2 deletions tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,47 @@ public function test_third_party_blocks_inline_styles_get_registered_to_global_s
/**
* @ticket 56915
*/
public function test_third_party_blocks_inline_styles_get_rendered() {
public function test_third_party_blocks_inline_styles_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{background-color: hotpink;}',
get_echo( 'wp_print_styles' )
$actual,
'Third party block inline style should render'
);
$this->assertStringNotContainsString(
'.wp-block-post-featured-image',
$actual,
'Core block should not render'
);
}

/**
* @ticket 56915
*/
public function test_blocks_inline_styles_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{background-color: hotpink;}',
$actual,
'Third party block inline style should render'
);
$this->assertStringContainsString(
'.wp-block-post-featured-image',
$actual,
'Core block should render'
);
}

Expand Down

0 comments on commit 37b6f74

Please sign in to comment.