diff --git a/src/wp-includes/block-supports/custom-css.php b/src/wp-includes/block-supports/custom-css.php index 2cd7644ed54b1..e80fe0c47cdab 100644 --- a/src/wp-includes/block-supports/custom-css.php +++ b/src/wp-includes/block-supports/custom-css.php @@ -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; } @@ -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 ); @@ -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 ) ) { diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index cc1ac60667773..6a6418d966457 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -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. + * @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 ) {