Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions src/wp-includes/block-supports/elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ function wp_render_elements_support_styles( $parsed_block ) {
*/
function wp_render_elements_class_name( $block_content, $block ) {
$class_string = $block['attrs']['className'] ?? '';

if ( ! is_string( $class_string ) ) {
return $block_content;
}
preg_match( '/\bwp-elements-\S+\b/', $class_string, $matches );

if ( empty( $matches ) ) {
Expand Down
30 changes: 30 additions & 0 deletions tests/phpunit/tests/block-supports/wpRenderElementsSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,36 @@ public function test_elements_block_support_class( $color_settings, $elements_st
);
}

/**
* Tests that a non-string `className` attribute does not cause a fatal
* error and the block content is returned unmodified.
*
* @ticket 65379
*
* Block attributes such as `className` are always expected to be strings,
* however invalid stored data can result in other types being present. The
* render filter should fail gracefully rather than passing an array to
* `preg_match()`.
Comment thread
westonruter marked this conversation as resolved.
Outdated
*
* @covers ::wp_render_elements_class_name
*/
public function test_elements_block_support_class_with_non_string_class_name() {
$block = array(
'blockName' => 'core/paragraph',
'attrs' => array(
'className' => array( '0', '1' ),
),
);

$block_content = "<p class=\"0 1\">Test</p>\n";

$this->assertSame(
$block_content,
wp_render_elements_class_name( $block_content, $block ),
'Block content should be returned unchanged when className is not a string'
);
}

/**
* Data provider.
*
Expand Down
Loading