Skip to content

Commit

Permalink
Move current logic to new private function and add test coverage for …
Browse files Browse the repository at this point in the history
…both unit and integration.
  • Loading branch information
felixarntz committed Mar 6, 2023
1 parent d97e56f commit 308066b
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 14 deletions.
45 changes: 31 additions & 14 deletions src/wp-includes/global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,20 +352,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'],
function ( $item ) {
if ( strpos( $item, 'core/' ) !== false ) {
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 @@ -374,6 +364,33 @@ function ( $item ) {
}
}

/**
* Gets the block name from a given theme.json path.
*
* @since 6.3.0
* @access private
*
* @param string $path 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 ) {
$result = array_values(
array_filter(
$path,
function ( $item ) {
if ( strpos( $item, 'core/' ) !== false ) {
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
73 changes: 73 additions & 0 deletions tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,79 @@ 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
*/
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 );
}

public function data_wp_get_block_name_from_theme_json_path() {
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

0 comments on commit 308066b

Please sign in to comment.