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

Ensure that custom block element styles in theme.json are not ignored #4174

Closed
wants to merge 6 commits into from
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
59 changes: 45 additions & 14 deletions src/wp-includes/global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,20 +320,10 @@ function wp_add_global_styles_for_blocks() {

// The likes of block element styles from theme.json do not have $metadata['name'] set.
if ( ! isset( $metadata['name'] ) && ! empty( $metadata['path'] ) ) {
$result = array_values(
array_filter(
$metadata['path'],
static function ( $item ) {
if ( str_contains( $item, 'core/' ) ) {
return true;
}
return false;
}
)
);
if ( isset( $result[0] ) ) {
if ( str_starts_with( $result[0], 'core/' ) ) {
$block_name = str_replace( 'core/', '', $result[0] );
$block_name = wp_get_block_name_from_theme_json_path( $metadata['path'] );
if ( $block_name ) {
if ( str_starts_with( $block_name, 'core/' ) ) {
$block_name = str_replace( 'core/', '', $block_name );
$stylesheet_handle = 'wp-block-' . $block_name;
}
wp_add_inline_style( $stylesheet_handle, $block_css );
Expand All @@ -342,6 +332,47 @@ static function ( $item ) {
}
}

/**
* Gets the block name from a given theme.json path.
*
* @since 6.3.0
* @access private
*
* @param array $path An array of keys describing the path to a property in theme.json.
* @return string Identified block name, or empty string if none found.
*/
function wp_get_block_name_from_theme_json_path( $path ) {
felixarntz marked this conversation as resolved.
Show resolved Hide resolved
// Block name is expected to be the third item after 'styles' and 'blocks'.
if (
count( $path ) >= 3
felixarntz marked this conversation as resolved.
Show resolved Hide resolved
&& 'styles' === $path[0]
&& 'blocks' === $path[1]
&& str_contains( $path[2], '/' )
) {
return $path[2];
}

/*
* As fallback and for backward compatibility, allow any core block to be
* at any position.
*/
$result = array_values(
array_filter(
$path,
static function ( $item ) {
if ( str_contains( $item, 'core/' ) ) {
felixarntz marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
return false;
}
)
);
if ( isset( $result[0] ) ) {
return $result[0];
}
return '';
}

/**
* Checks whether a theme or its parent has a theme.json file.
*
Expand Down
7 changes: 7 additions & 0 deletions tests/phpunit/data/themedir1/block-theme/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@
"my/third-party-block": {
"color": {
"background": "hotpink"
},
"elements": {
"cite": {
"color": {
"text": "white"
}
}
}
}
},
Expand Down
82 changes: 82 additions & 0 deletions tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,88 @@ public function test_blocks_inline_styles_get_rendered() {
);
}

/**
* @ticket 57868
*/
public function test_third_party_blocks_inline_styles_for_elements_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 cite{color: white;}',
$actual
);
}

/**
* @ticket 57868
*/
public function test_third_party_blocks_inline_styles_for_elements_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 cite{color: white;}',
$actual
);
}

/**
* @ticket 57868
*
* @dataProvider data_wp_get_block_name_from_theme_json_path
*
* @param array $path An array of keys describing the path to a property in theme.json.
* @param string $expected The expected block name.
*/
public function test_wp_get_block_name_from_theme_json_path( $path, $expected ) {
$block_name = wp_get_block_name_from_theme_json_path( $path );
$this->assertSame( $expected, $block_name );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_wp_get_block_name_from_theme_json_path() {
felixarntz marked this conversation as resolved.
Show resolved Hide resolved
return array(
'core block styles' => array(
array( 'styles', 'blocks', 'core/navigation' ),
'core/navigation',
),
'core block element styles' => array(
array( 'styles', 'blocks', 'core/navigation', 'elements', 'link' ),
'core/navigation',
),
'custom block styles' => array(
array( 'styles', 'blocks', 'my/third-party-block' ),
'my/third-party-block',
),
'custom block element styles' => array(
array( 'styles', 'blocks', 'my/third-party-block', 'elements', 'cite' ),
'my/third-party-block',
),
'custom block wrong format' => array(
array( 'styles', 'my/third-party-block' ),
'',
),
'invalid path but works for BC' => array(
array( 'something', 'core/image' ),
'core/image',
),
);
}

private function set_up_third_party_block() {
switch_theme( 'block-theme' );

Expand Down