Editor: Fix bug where it was not possible to style custom block elements in theme.json.

This changeset resolves a bug where WordPress would only allow HTML elements within core's own blocks to be styled in `theme.json`. Prior to this change, any `theme.json` rules applying to elements in custom blocks were ignored. With this fix it is now possible to style third-party block elements in `theme.json`.

Props flixos90, azaozz, costdev, glendaviesnz, spacedmonkey, oandregal.
Fixes #57868.

Built from https://develop.svn.wordpress.org/trunk@56254


git-svn-id: http://core.svn.wordpress.org/trunk@55766 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Felix Arntz 2023-07-18 00:02:22 +00:00
parent dce6425913
commit 75f25fd334
2 changed files with 46 additions and 15 deletions

View File

@ -320,9 +320,45 @@ function wp_add_global_styles_for_blocks() {
// The likes of block element styles from theme.json do not have $metadata['name'] set. // The likes of block element styles from theme.json do not have $metadata['name'] set.
if ( ! isset( $metadata['name'] ) && ! empty( $metadata['path'] ) ) { if ( ! isset( $metadata['name'] ) && ! empty( $metadata['path'] ) ) {
$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 );
}
}
}
}
/**
* 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 ) {
// Block name is expected to be the third item after 'styles' and 'blocks'.
if (
count( $path ) >= 3
&& '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( $result = array_values(
array_filter( array_filter(
$metadata['path'], $path,
static function ( $item ) { static function ( $item ) {
if ( str_contains( $item, 'core/' ) ) { if ( str_contains( $item, 'core/' ) ) {
return true; return true;
@ -332,14 +368,9 @@ function wp_add_global_styles_for_blocks() {
) )
); );
if ( isset( $result[0] ) ) { if ( isset( $result[0] ) ) {
if ( str_starts_with( $result[0], 'core/' ) ) { return $result[0];
$block_name = str_replace( 'core/', '', $result[0] );
$stylesheet_handle = 'wp-block-' . $block_name;
}
wp_add_inline_style( $stylesheet_handle, $block_css );
}
}
} }
return '';
} }
/** /**

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.3-beta4-56253'; $wp_version = '6.3-beta4-56254';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.