mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Backport: Elements - Merge element style and classname generation to single filter #6214
Closed
aaronrobertshaw
wants to merge
6
commits into
WordPress:trunk
from
aaronrobertshaw:fix/element-block-support-application
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
15151e9
Move element block support class and styler generation to render_bloc…
aaronrobertshaw 9712e03
Correct references to version in which deprecation occurs
aaronrobertshaw 4632724
Fix copy pasta error
aaronrobertshaw 7b64d09
Fix deprecation calls replacement
aaronrobertshaw 1789ecf
Fix typo
aaronrobertshaw 8e60ca7
Add missing @since 6.6.0
aaronrobertshaw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ function wp_get_elements_class_name( $block ) { | |
/** | ||
* Updates the block content with elements class names. | ||
* | ||
* @deprecated 6.6.0 Generation of element class name is handled via `render_block_data` filter. | ||
* | ||
* @since 5.8.0 | ||
* @since 6.4.0 Added support for button and heading element styling. | ||
* @access private | ||
|
@@ -31,33 +33,43 @@ function wp_get_elements_class_name( $block ) { | |
* @return string Filtered block content. | ||
*/ | ||
function wp_render_elements_support( $block_content, $block ) { | ||
if ( ! $block_content || ! isset( $block['attrs']['style']['elements'] ) ) { | ||
return $block_content; | ||
} | ||
_deprecated_function( __FUNCTION__, '6.6.0', 'wp_render_elements_class_name' ); | ||
return $block_content; | ||
} | ||
|
||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); | ||
if ( ! $block_type ) { | ||
return $block_content; | ||
/** | ||
* Determines whether an elements class name should be added to the block. | ||
* | ||
* @since 6.6.0 | ||
* @access private | ||
* | ||
* @param array $block Block object. | ||
* @param array $options Per element type options e.g. whether to skip serialization. | ||
* @return boolean Whether the block needs an elements class name. | ||
*/ | ||
function wp_should_add_elements_class_name( $block, $options ) { | ||
if ( ! isset( $block['attrs']['style']['elements'] ) ) { | ||
return false; | ||
} | ||
|
||
$element_color_properties = array( | ||
'button' => array( | ||
'skip' => wp_should_skip_block_supports_serialization( $block_type, 'color', 'button' ), | ||
'skip' => isset( $options['button']['skip'] ) ? $options['button']['skip'] : false, | ||
'paths' => array( | ||
array( 'button', 'color', 'text' ), | ||
array( 'button', 'color', 'background' ), | ||
array( 'button', 'color', 'gradient' ), | ||
), | ||
), | ||
'link' => array( | ||
'skip' => wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' ), | ||
'skip' => isset( $options['link']['skip'] ) ? $options['link']['skip'] : false, | ||
'paths' => array( | ||
array( 'link', 'color', 'text' ), | ||
array( 'link', ':hover', 'color', 'text' ), | ||
), | ||
), | ||
'heading' => array( | ||
'skip' => wp_should_skip_block_supports_serialization( $block_type, 'color', 'heading' ), | ||
'skip' => isset( $options['heading']['skip'] ) ? $options['heading']['skip'] : false, | ||
'paths' => array( | ||
array( 'heading', 'color', 'text' ), | ||
array( 'heading', 'color', 'background' ), | ||
|
@@ -84,14 +96,6 @@ function wp_render_elements_support( $block_content, $block ) { | |
), | ||
); | ||
|
||
$skip_all_element_color_serialization = $element_color_properties['button']['skip'] && | ||
$element_color_properties['link']['skip'] && | ||
$element_color_properties['heading']['skip']; | ||
|
||
if ( $skip_all_element_color_serialization ) { | ||
return $block_content; | ||
} | ||
|
||
$elements_style_attributes = $block['attrs']['style']['elements']; | ||
|
||
foreach ( $element_color_properties as $element_config ) { | ||
|
@@ -101,50 +105,53 @@ function wp_render_elements_support( $block_content, $block ) { | |
|
||
foreach ( $element_config['paths'] as $path ) { | ||
if ( null !== _wp_array_get( $elements_style_attributes, $path, null ) ) { | ||
/* | ||
* It only takes a single custom attribute to require that the custom | ||
* class name be added to the block, so once one is found there's no | ||
* need to continue looking for others. | ||
* | ||
* As is done with the layout hook, this code assumes that the block | ||
* contains a single wrapper and that it's the first element in the | ||
* rendered output. That first element, if it exists, gets the class. | ||
*/ | ||
$tags = new WP_HTML_Tag_Processor( $block_content ); | ||
if ( $tags->next_tag() ) { | ||
$tags->add_class( wp_get_elements_class_name( $block ) ); | ||
} | ||
|
||
return $tags->get_updated_html(); | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
// If no custom attributes were found then there's nothing to modify. | ||
return $block_content; | ||
return false; | ||
} | ||
|
||
/** | ||
* Renders the elements stylesheet. | ||
* Render the elements stylesheet and adds elements class name to block as required. | ||
* | ||
* In the case of nested blocks we want the parent element styles to be rendered before their descendants. | ||
* 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.0.0 | ||
* @since 6.1.0 Implemented the style engine to generate CSS and classnames. | ||
* @since 6.6.0 Element block support class and styles are generated via the `render_block_data` filter instead of `pre_render_block`. | ||
* @access private | ||
* | ||
* @param string|null $pre_render The pre-rendered content. Default null. | ||
* @param array $block The block being rendered. | ||
* @return null | ||
* @param array $parsed_block The parsed block. | ||
* @return array The same parsed block with elements classname added if appropriate. | ||
*/ | ||
function wp_render_elements_support_styles( $pre_render, $block ) { | ||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); | ||
$element_block_styles = isset( $block['attrs']['style']['elements'] ) ? $block['attrs']['style']['elements'] : null; | ||
function wp_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.' ) | ||
); | ||
} | ||
|
||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $parsed_block['blockName'] ); | ||
$element_block_styles = isset( $parsed_block['attrs']['style']['elements'] ) ? $parsed_block['attrs']['style']['elements'] : null; | ||
|
||
if ( ! $element_block_styles ) { | ||
return null; | ||
return $parsed_block; | ||
} | ||
|
||
$skip_link_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' ); | ||
|
@@ -155,11 +162,25 @@ function wp_render_elements_support_styles( $pre_render, $block ) { | |
$skip_button_color_serialization; | ||
|
||
if ( $skips_all_element_color_serialization ) { | ||
return null; | ||
return $parsed_block; | ||
} | ||
|
||
$class_name = wp_get_elements_class_name( $block ); | ||
$options = array( | ||
'button' => array( 'skip' => $skip_button_color_serialization ), | ||
'link' => array( 'skip' => $skip_link_color_serialization ), | ||
'heading' => array( 'skip' => $skip_heading_color_serialization ), | ||
); | ||
|
||
if ( ! wp_should_add_elements_class_name( $parsed_block, $options ) ) { | ||
return $parsed_block; | ||
} | ||
|
||
$class_name = wp_get_elements_class_name( $parsed_block ); | ||
$updated_class_name = isset( $parsed_block['attrs']['className'] ) ? $parsed_block['attrs']['className'] . " $class_name" : $class_name; | ||
|
||
_wp_array_set( $parsed_block, array( 'attrs', 'className' ), $updated_class_name ); | ||
|
||
// Generate element styles based on selector and store in style engine for enqueuing. | ||
$element_types = array( | ||
'button' => array( | ||
'selector' => ".$class_name .wp-element-button, .$class_name .wp-block-button__link", | ||
|
@@ -225,8 +246,38 @@ function wp_render_elements_support_styles( $pre_render, $block ) { | |
} | ||
} | ||
|
||
return null; | ||
return $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 | ||
* block's markup. | ||
* | ||
aaronrobertshaw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @see wp_render_elements_support_styles | ||
* @since 6.6.0 | ||
* | ||
* @param string $block_content Rendered block content. | ||
* @param array $block Block object. | ||
* | ||
* @return string Filtered block content. | ||
Comment on lines
+261
to
+263
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove empty line. |
||
*/ | ||
function wp_render_elements_class_name( $block_content, $block ) { | ||
$class_string = $block['attrs']['className'] ?? ''; | ||
preg_match( '/\bwp-elements-\S+\b/', $class_string, $matches ); | ||
|
||
if ( empty( $matches ) ) { | ||
return $block_content; | ||
} | ||
|
||
$tags = new WP_HTML_Tag_Processor( $block_content ); | ||
|
||
if ( $tags->next_tag() ) { | ||
$tags->add_class( $matches[0] ); | ||
} | ||
|
||
return $tags->get_updated_html(); | ||
} | ||
|
||
add_filter( 'render_block', 'wp_render_elements_support', 10, 2 ); | ||
add_filter( 'pre_render_block', 'wp_render_elements_support_styles', 10, 2 ); | ||
add_filter( 'render_block', 'wp_render_elements_class_name', 10, 2 ); | ||
add_filter( 'render_block_data', 'wp_render_elements_support_styles', 10, 1 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove end period.