Skip to content
Open
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
45 changes: 35 additions & 10 deletions src/wp-includes/block-supports/custom-css.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,28 @@
*
* @param array $parsed_block The parsed block.
* @return array The same parsed block with custom CSS class name added if appropriate.
*
* @phpstan-param array{
* blockName: string|null,
* attrs: array{
* className?: string,
* style?: array{
* css?: string,
* ...
* },
* ...
* },
* ...
* } $parsed_block
*/
function wp_render_custom_css_support_styles( $parsed_block ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $parsed_block['blockName'] );

if ( ! block_has_support( $block_type, 'customCSS', true ) ) {
$custom_css = $parsed_block['attrs']['style']['css'] ?? null;
if ( ! is_string( $custom_css ) || '' === trim( $custom_css ) ) {
return $parsed_block;
}

$custom_css = trim( $parsed_block['attrs']['style']['css'] ?? '' );

if ( empty( $custom_css ) ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $parsed_block['blockName'] );
if ( ! block_has_support( $block_type, 'customCSS', true ) ) {
return $parsed_block;
}

Expand All @@ -32,9 +43,10 @@ function wp_render_custom_css_support_styles( $parsed_block ) {
}

// Generate a unique class name for this block instance.
$class_name = wp_unique_id_from_values( $parsed_block, 'wp-custom-css-' );
$updated_class_name = isset( $parsed_block['attrs']['className'] )
? $parsed_block['attrs']['className'] . " $class_name"
$class_name = wp_unique_id_from_values( $parsed_block, 'wp-custom-css-' );
$existing_class_name = $parsed_block['attrs']['className'] ?? null;
$updated_class_name = is_string( $existing_class_name )
? "$existing_class_name $class_name"
: $class_name;

_wp_array_set( $parsed_block, array( 'attrs', 'className' ), $updated_class_name );
Expand Down Expand Up @@ -76,9 +88,22 @@ function wp_enqueue_block_custom_css() {
* @param string $block_content Rendered block content.
* @param array $block Block object.
* @return string Filtered block content.
*
* @phpstan-param array{
* attrs: array{
* className?: string,
* ...
* },
* ...
* } $block
*/
function wp_render_custom_css_class_name( $block_content, $block ) {
$class_string = $block['attrs']['className'] ?? '';
$class_string = $block['attrs']['className'] ?? null;

if ( ! is_string( $class_string ) || '' === $class_string ) {
return $block_content;
}

preg_match( '/\bwp-custom-css-\S+\b/', $class_string, $matches );

if ( empty( $matches ) ) {
Expand Down
6 changes: 3 additions & 3 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2604,9 +2604,9 @@ function unregister_block_style( $block_name, $block_style_name ) {
* @since 5.8.0
* @since 6.4.0 The `$feature` parameter now supports a string.
*
* @param WP_Block_Type $block_type Block type to check for support.
* @param string|array $feature Feature slug, or path to a specific feature to check support for.
* @param mixed $default_value Optional. Fallback value for feature support. Default false.
* @param WP_Block_Type|null $block_type Block type to check for support.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This change is because the return value of WP_Block_Type_Registry::get_instance()->get_registered() is often passed right into block_has_support(). And the function specifically is checking for whether the arg is a WP_Block_Type.

* @param string|array $feature Feature slug, or path to a specific feature to check support for.
* @param mixed $default_value Optional. Fallback value for feature support. Default false.
* @return bool Whether the feature is supported.
*/
function block_has_support( $block_type, $feature, $default_value = false ) {
Expand Down
Loading