Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Elements: Deprecate old block support filter callbacks #59538

Merged
merged 6 commits into from
Apr 22, 2024
Merged
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
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
Loading