diff --git a/lib/block-supports/elements.php b/lib/block-supports/elements.php index eefab9ceffbaf..35a41270a1980 100644 --- a/lib/block-supports/elements.php +++ b/lib/block-supports/elements.php @@ -5,6 +5,19 @@ * @package gutenberg */ +/** + * Update the block content with elements class names. + * + * @deprecated 6.6.0 Use `gutenberg_render_elements_class_name` instead. + * + * @param string $block_content Rendered block content. + * @return string Filtered block content. + */ +function gutenberg_render_elements_support( $block_content ) { + _deprecated_function( __FUNCTION__, '6.6.0', 'gutenberg_render_elements_class_name' ); + return $block_content; +} + /** * Determines whether an elements class name should be added to the block. * @@ -86,11 +99,31 @@ function gutenberg_should_add_elements_class_name( $block, $options ) { * This solves the issue of an element (e.g.: link color) being styled in both the parent and a descendant: * we want the descendant style to take priority, and this is done by loading it after, in DOM order. * + * @since 6.6.0 Element block support class and styles are generated via the `render_block_data` filter instead of `pre_render_block` + * * @param array $parsed_block The parsed block. * * @return array The same parsed block with elements classname added if appropriate. */ function gutenberg_render_elements_support_styles( $parsed_block ) { + /* + * The generation of element styles and classname were moved to the + * `render_block_data` filter in 6.6.0 to avoid filtered attributes + * breaking the application of the elements CSS class. + * + * @see https://github.com/WordPress/gutenberg/pull/59535. + * + * The change in filter means, the argument types for this function + * have changed and require deprecating. + */ + if ( is_string( $parsed_block ) ) { + _deprecated_argument( + __FUNCTION__, + '6.6.0', + __( 'Use as a `pre_render_block` filter is deprecated. Use with `render_block_data` instead.', 'gutenberg' ) + ); + } + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $parsed_block['blockName'] ); $element_block_styles = $parsed_block['attrs']['style']['elements'] ?? null; @@ -193,8 +226,8 @@ function gutenberg_render_elements_support_styles( $parsed_block ) { } /** - * Ensure the elements block support class name generated and added to - * block attributes in the `render_block_data` filter gets applied to the + * Ensure the elements block support class name generated, and added to + * block attributes, in the `render_block_data` filter gets applied to the * block's markup. * * @see gutenberg_render_elements_support_styles @@ -215,17 +248,19 @@ function gutenberg_render_elements_class_name( $block_content, $block ) { $tags = new WP_HTML_Tag_Processor( $block_content ); if ( $tags->next_tag() ) { - // Ensure the elements class name set in render_block_data filter is applied in markup. - // See `gutenberg_render_elements_support_styles`. $tags->add_class( $matches[0] ); } return $tags->get_updated_html(); } -// Remove WordPress core filters to avoid rendering duplicate elements stylesheet & attaching classes twice. +// Remove deprecated WordPress core filters. remove_filter( 'render_block', 'wp_render_elements_support', 10, 2 ); remove_filter( 'pre_render_block', 'wp_render_elements_support_styles', 10, 2 ); + +// Remove WordPress core filters to avoid rendering duplicate elements stylesheet & attaching classes twice. remove_filter( 'render_block', 'wp_render_elements_class_name', 10, 2 ); +remove_filter( 'render_block_data', 'wp_render_elements_support_styles', 10, 1 ); + add_filter( 'render_block', 'gutenberg_render_elements_class_name', 10, 2 ); -add_filter( 'render_block_data', 'gutenberg_render_elements_support_styles', 10, 2 ); +add_filter( 'render_block_data', 'gutenberg_render_elements_support_styles', 10, 1 ); diff --git a/phpunit/block-supports/elements-test.php b/phpunit/block-supports/elements-test.php index 827b624dea241..f7351c0eb26ab 100644 --- a/phpunit/block-supports/elements-test.php +++ b/phpunit/block-supports/elements-test.php @@ -72,10 +72,12 @@ public function test_elements_block_support_class( $color_settings, $elements_st ), ); - // To ensure a consistent elements class name it is generated within a - // `render_block_data` filter and stored in the `className` attribute. - // As a result the block data needs to be passed through the same - // function for this test. + /* + * To ensure a consistent elements class name it is generated within a + * `render_block_data` filter and stored in the `className` attribute. + * As a result the block data needs to be passed through the same + * function for this test. + */ $filtered_block = gutenberg_render_elements_support_styles( $block ); $actual = gutenberg_render_elements_class_name( $block_markup, $filtered_block );