Skip to content

Commit

Permalink
Elements: Deprecate old block support filter callbacks (#59538)
Browse files Browse the repository at this point in the history
Co-authored-by: aaronrobertshaw <[email protected]>
Co-authored-by: andrewserong <[email protected]>
Co-authored-by: peterwilsoncc <[email protected]>
  • Loading branch information
4 people committed Apr 22, 2024
1 parent 29254e5 commit c817886
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
47 changes: 41 additions & 6 deletions lib/block-supports/elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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 );
10 changes: 6 additions & 4 deletions phpunit/block-supports/elements-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down

0 comments on commit c817886

Please sign in to comment.