| @@ -7,34 +7,22 @@ | ||
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | 9 | * Update the block content with elements class names. |
| 10 | 10 | * |
| 11 | - * @deprecated 6.6.0 Use `gutenberg_render_elements_class_name` instead. | |
| 12 | - * | |
| 13 | 11 | * @param string $block_content Rendered block content. |
| 12 | + * @param array $block Block object. | |
| 14 | 13 | * @return string Filtered block content. |
| 15 | 14 | */ |
| 16 | -function gutenberg_render_elements_support( $block_content ) { | |
| 17 | - _deprecated_function( __FUNCTION__, '6.6.0', 'gutenberg_render_elements_class_name' ); | |
| 18 | - return $block_content; | |
| 19 | -} | |
| 15 | +function gutenberg_render_elements_support( $block_content, $block ) { | |
| 16 | + if ( ! $block_content || ! isset( $block['attrs']['style']['elements'] ) ) { | |
| 17 | + return $block_content; | |
| 18 | + } | |
| 20 | 19 | |
| 21 | -/** | |
| 22 | - * Determines whether an elements class name should be added to the block. | |
| 23 | - * | |
| 24 | - * @param array $block Block object. | |
| 25 | - * @param array $options Per element type options e.g. whether to skip serialization. | |
| 26 | - * | |
| 27 | - * @return boolean Whether the block needs an elements class name. | |
| 28 | - */ | |
| 29 | -function gutenberg_should_add_elements_class_name( $block, $options ) { | |
| 30 | - if ( ! isset( $block['attrs']['style']['elements'] ) ) { | |
| 31 | - return false; | |
| 32 | - } | |
| 20 | + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); | |
| 33 | 21 | |
| 34 | 22 | $element_color_properties = array( |
| 35 | 23 | 'button' => array( |
| 36 | - 'skip' => $options['button']['skip'] ?? false, | |
| 24 | + 'skip' => wp_should_skip_block_supports_serialization( $block_type, 'color', 'button' ), | |
| 37 | 25 | 'paths' => array( |
| 38 | 26 | array( 'button', 'color', 'text' ), |
| 39 | 27 | array( 'button', 'color', 'background' ), |
| 40 | 28 | array( 'button', 'color', 'gradient' ), |
| @@ -40,9 +28,9 @@ | ||
| 40 | 28 | array( 'button', 'color', 'gradient' ), |
| 41 | 29 | ), |
| 42 | 30 | ), |
| 43 | 31 | 'link' => array( |
| 44 | - 'skip' => $options['link']['skip'] ?? false, | |
| 32 | + 'skip' => wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' ), | |
| 45 | 33 | 'paths' => array( |
| 46 | 34 | array( 'link', 'color', 'text' ), |
| 47 | 35 | array( 'link', ':hover', 'color', 'text' ), |
| 48 | 36 | ), |
| @@ -47,9 +35,9 @@ | ||
| 47 | 35 | array( 'link', ':hover', 'color', 'text' ), |
| 48 | 36 | ), |
| 49 | 37 | ), |
| 50 | 38 | 'heading' => array( |
| 51 | - 'skip' => $options['heading']['skip'] ?? false, | |
| 39 | + 'skip' => wp_should_skip_block_supports_serialization( $block_type, 'color', 'heading' ), | |
| 52 | 40 | 'paths' => array( |
| 53 | 41 | array( 'heading', 'color', 'text' ), |
| 54 | 42 | array( 'heading', 'color', 'background' ), |
| 55 | 43 | array( 'heading', 'color', 'gradient' ), |
| @@ -74,8 +62,16 @@ | ||
| 74 | 62 | ), |
| 75 | 63 | ), |
| 76 | 64 | ); |
| 77 | 65 | |
| 66 | + $skip_all_element_color_serialization = $element_color_properties['button']['skip'] && | |
| 67 | + $element_color_properties['link']['skip'] && | |
| 68 | + $element_color_properties['heading']['skip']; | |
| 69 | + | |
| 70 | + if ( $skip_all_element_color_serialization ) { | |
| 71 | + return $block_content; | |
| 72 | + } | |
| 73 | + | |
| 78 | 74 | $elements_style_attributes = $block['attrs']['style']['elements']; |
| 79 | 75 | |
| 80 | 76 | foreach ( $element_color_properties as $element_config ) { |
| 81 | 77 | if ( $element_config['skip'] ) { |
| @@ -83,53 +79,49 @@ | ||
| 83 | 79 | } |
| 84 | 80 | |
| 85 | 81 | foreach ( $element_config['paths'] as $path ) { |
| 86 | 82 | if ( null !== _wp_array_get( $elements_style_attributes, $path, null ) ) { |
| 87 | - return true; | |
| 83 | + /* | |
| 84 | + * It only takes a single custom attribute to require that the custom | |
| 85 | + * class name be added to the block, so once one is found there's no | |
| 86 | + * need to continue looking for others. | |
| 87 | + * | |
| 88 | + * As is done with the layout hook, this code assumes that the block | |
| 89 | + * contains a single wrapper and that it's the first element in the | |
| 90 | + * rendered output. That first element, if it exists, gets the class. | |
| 91 | + */ | |
| 92 | + $tags = new WP_HTML_Tag_Processor( $block_content ); | |
| 93 | + if ( $tags->next_tag() ) { | |
| 94 | + $tags->add_class( wp_get_elements_class_name( $block ) ); | |
| 95 | + } | |
| 96 | + | |
| 97 | + return $tags->get_updated_html(); | |
| 88 | 98 | } |
| 89 | 99 | } |
| 90 | 100 | } |
| 91 | 101 | |
| 92 | - return false; | |
| 102 | + // If no custom attributes were found then there's nothing to modify. | |
| 103 | + return $block_content; | |
| 93 | 104 | } |
| 94 | 105 | |
| 95 | 106 | /** |
| 96 | - * Render the elements stylesheet and adds elements class name to block as required. | |
| 107 | + * Render the elements stylesheet. | |
| 97 | 108 | * |
| 98 | 109 | * In the case of nested blocks we want the parent element styles to be rendered before their descendants. |
| 99 | 110 | * This solves the issue of an element (e.g.: link color) being styled in both the parent and a descendant: |
| 100 | 111 | * we want the descendant style to take priority, and this is done by loading it after, in DOM order. |
| 101 | 112 | * |
| 102 | - * @since 6.6.0 Element block support class and styles are generated via the `render_block_data` filter instead of `pre_render_block` | |
| 113 | + * @param string|null $pre_render The pre-rendered content. Default null. | |
| 114 | + * @param array $block The block being rendered. | |
| 103 | 115 | * |
| 104 | - * @param array $parsed_block The parsed block. | |
| 105 | - * | |
| 106 | - * @return array The same parsed block with elements classname added if appropriate. | |
| 116 | + * @return null | |
| 107 | 117 | */ |
| 108 | -function gutenberg_render_elements_support_styles( $parsed_block ) { | |
| 109 | - /* | |
| 110 | - * The generation of element styles and classname were moved to the | |
| 111 | - * `render_block_data` filter in 6.6.0 to avoid filtered attributes | |
| 112 | - * breaking the application of the elements CSS class. | |
| 113 | - * | |
| 114 | - * @link https://github.com/WordPress/gutenberg/pull/59535 | |
| 115 | - * | |
| 116 | - * The change in filter means, the argument types for this function | |
| 117 | - * have changed and require deprecating. | |
| 118 | - */ | |
| 119 | - if ( is_string( $parsed_block ) ) { | |
| 120 | - _deprecated_argument( | |
| 121 | - __FUNCTION__, | |
| 122 | - '6.6.0', | |
| 123 | - __( 'Use as a `pre_render_block` filter is deprecated. Use with `render_block_data` instead.', 'gutenberg' ) | |
| 124 | - ); | |
| 125 | - } | |
| 118 | +function gutenberg_render_elements_support_styles( $pre_render, $block ) { | |
| 119 | + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); | |
| 120 | + $element_block_styles = isset( $block['attrs']['style']['elements'] ) ? $block['attrs']['style']['elements'] : null; | |
| 126 | 121 | |
| 127 | - $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $parsed_block['blockName'] ); | |
| 128 | - $element_block_styles = $parsed_block['attrs']['style']['elements'] ?? null; | |
| 129 | - | |
| 130 | 122 | if ( ! $element_block_styles ) { |
| 131 | - return $parsed_block; | |
| 123 | + return null; | |
| 132 | 124 | } |
| 133 | 125 | |
| 134 | 126 | $skip_link_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' ); |
| 135 | 127 | $skip_heading_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'heading' ); |
| @@ -138,27 +130,13 @@ | ||
| 138 | 130 | $skip_heading_color_serialization && |
| 139 | 131 | $skip_button_color_serialization; |
| 140 | 132 | |
| 141 | 133 | if ( $skips_all_element_color_serialization ) { |
| 142 | - return $parsed_block; | |
| 134 | + return null; | |
| 143 | 135 | } |
| 144 | 136 | |
| 145 | - $options = array( | |
| 146 | - 'button' => array( 'skip' => $skip_button_color_serialization ), | |
| 147 | - 'link' => array( 'skip' => $skip_link_color_serialization ), | |
| 148 | - 'heading' => array( 'skip' => $skip_heading_color_serialization ), | |
| 149 | - ); | |
| 137 | + $class_name = wp_get_elements_class_name( $block ); | |
| 150 | 138 | |
| 151 | - if ( ! gutenberg_should_add_elements_class_name( $parsed_block, $options ) ) { | |
| 152 | - return $parsed_block; | |
| 153 | - } | |
| 154 | - | |
| 155 | - $class_name = wp_get_elements_class_name( $parsed_block ); | |
| 156 | - $updated_class_name = isset( $parsed_block['attrs']['className'] ) ? $parsed_block['attrs']['className'] . " $class_name" : $class_name; | |
| 157 | - | |
| 158 | - _wp_array_set( $parsed_block, array( 'attrs', 'className' ), $updated_class_name ); | |
| 159 | - | |
| 160 | - // Generate element styles based on selector and store in style engine for enqueuing. | |
| 161 | 139 | $element_types = array( |
| 162 | 140 | 'button' => array( |
| 163 | 141 | 'selector' => ".$class_name .wp-element-button, .$class_name .wp-block-button__link", |
| 164 | 142 | 'skip' => $skip_button_color_serialization, |
| @@ -163,11 +141,10 @@ | ||
| 163 | 141 | 'selector' => ".$class_name .wp-element-button, .$class_name .wp-block-button__link", |
| 164 | 142 | 'skip' => $skip_button_color_serialization, |
| 165 | 143 | ), |
| 166 | 144 | 'link' => array( |
| 167 | - // :where(:not) matches theme.json selector. | |
| 168 | - 'selector' => ".$class_name a:where(:not(.wp-element-button))", | |
| 169 | - 'hover_selector' => ".$class_name a:where(:not(.wp-element-button)):hover", | |
| 145 | + 'selector' => ".$class_name a", | |
| 146 | + 'hover_selector' => ".$class_name a:hover", | |
| 170 | 147 | 'skip' => $skip_link_color_serialization, |
| 171 | 148 | ), |
| 172 | 149 | 'heading' => array( |
| 173 | 150 | 'selector' => ".$class_name h1, .$class_name h2, .$class_name h3, .$class_name h4, .$class_name h5, .$class_name h6", |
| @@ -192,9 +169,9 @@ | ||
| 192 | 169 | 'context' => 'block-supports', |
| 193 | 170 | ) |
| 194 | 171 | ); |
| 195 | 172 | |
| 196 | - if ( isset( $element_style_object[':hover'], $element_config['hover_selector'] ) ) { | |
| 173 | + if ( isset( $element_style_object[':hover'] ) ) { | |
| 197 | 174 | gutenberg_style_engine_get_styles( |
| 198 | 175 | $element_style_object[':hover'], |
| 199 | 176 | array( |
| 200 | 177 | 'selector' => $element_config['hover_selector'], |
| @@ -221,66 +198,12 @@ | ||
| 221 | 198 | } |
| 222 | 199 | } |
| 223 | 200 | } |
| 224 | 201 | |
| 225 | - return $parsed_block; | |
| 202 | + return null; | |
| 226 | 203 | } |
| 227 | 204 | |
| 228 | -/** | |
| 229 | - * Ensure the elements block support class name generated, and added to | |
| 230 | - * block attributes, in the `render_block_data` filter gets applied to the | |
| 231 | - * block's markup. | |
| 232 | - * | |
| 233 | - * @see gutenberg_render_elements_support_styles | |
| 234 | - * | |
| 235 | - * @param string $block_content Rendered block content. | |
| 236 | - * @param array $block Block object. | |
| 237 | - * @return string Filtered block content. | |
| 238 | - * | |
| 239 | - * @phpstan-param array{ | |
| 240 | - * attrs: array{ | |
| 241 | - * className?: string, | |
| 242 | - * ... | |
| 243 | - * }, | |
| 244 | - * ... | |
| 245 | - * } $block | |
| 246 | - */ | |
| 247 | -function gutenberg_render_elements_class_name( $block_content, $block ) { | |
| 248 | - $class_name_attr = $block['attrs']['className'] ?? null; | |
| 249 | - $class_name_prefix = 'wp-elements-'; | |
| 250 | - if ( ! is_string( $class_name_attr ) || ! str_contains( $class_name_attr, $class_name_prefix ) ) { | |
| 251 | - return $block_content; | |
| 252 | - } | |
| 253 | - | |
| 254 | - // Parse out the 'wp-elements-*' class name. | |
| 255 | - $matched_class_name = null; | |
| 256 | - $token_delimiter = " \t\f\r\n"; | |
| 257 | - $class_token = strtok( $class_name_attr, $token_delimiter ); | |
| 258 | - while ( false !== $class_token ) { | |
| 259 | - if ( str_starts_with( $class_token, $class_name_prefix ) ) { | |
| 260 | - $matched_class_name = $class_token; | |
| 261 | - break; | |
| 262 | - } | |
| 263 | - $class_token = strtok( $token_delimiter ); | |
| 264 | - } | |
| 265 | - if ( null === $matched_class_name ) { | |
| 266 | - return $block_content; | |
| 267 | - } | |
| 268 | - | |
| 269 | - $tags = new WP_HTML_Tag_Processor( $block_content ); | |
| 270 | - if ( $tags->next_tag() ) { | |
| 271 | - $tags->add_class( $matched_class_name ); | |
| 272 | - } | |
| 273 | - | |
| 274 | - return $tags->get_updated_html(); | |
| 275 | -} | |
| 276 | - | |
| 277 | -// Remove deprecated WordPress core filters. | |
| 278 | -remove_filter( 'render_block', 'wp_render_elements_support', 10 ); | |
| 279 | -remove_filter( 'pre_render_block', 'wp_render_elements_support_styles', 10 ); | |
| 280 | - | |
| 281 | 205 | // Remove WordPress core filters to avoid rendering duplicate elements stylesheet & attaching classes twice. |
| 282 | -remove_filter( 'render_block', 'wp_render_elements_class_name', 10 ); | |
| 283 | -remove_filter( 'render_block_data', 'wp_render_elements_support_styles', 10 ); | |
| 284 | - | |
| 285 | -add_filter( 'render_block', 'gutenberg_render_elements_class_name', 10, 2 ); | |
| 286 | -add_filter( 'render_block_data', 'gutenberg_render_elements_support_styles', 10, 1 ); | |
| 206 | +remove_filter( 'render_block', 'wp_render_elements_support', 10, 2 ); | |
| 207 | +remove_filter( 'pre_render_block', 'wp_render_elements_support_styles', 10, 2 ); | |
| 208 | +add_filter( 'render_block', 'gutenberg_render_elements_support', 10, 2 ); | |
| 209 | +add_filter( 'pre_render_block', 'gutenberg_render_elements_support_styles', 10, 2 ); | |