Changeset 58074
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/block-supports/elements.php
r57664 r58074 22 22 /** 23 23 * Updates the block content with elements class names. 24 * 25 * @deprecated 6.6.0 Generation of element class name is handled via `render_block_data` filter. 24 26 * 25 27 * @since 5.8.0 … … 32 34 */ 33 35 function wp_render_elements_support( $block_content, $block ) { 34 if ( ! $block_content || ! isset( $block['attrs']['style']['elements'] ) ) { 35 return $block_content; 36 } 37 38 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); 39 if ( ! $block_type ) { 40 return $block_content; 36 _deprecated_function( __FUNCTION__, '6.6.0', 'wp_render_elements_class_name' ); 37 return $block_content; 38 } 39 40 /** 41 * Determines whether an elements class name should be added to the block. 42 * 43 * @since 6.6.0 44 * @access private 45 * 46 * @param array $block Block object. 47 * @param array $options Per element type options e.g. whether to skip serialization. 48 * @return boolean Whether the block needs an elements class name. 49 */ 50 function wp_should_add_elements_class_name( $block, $options ) { 51 if ( ! isset( $block['attrs']['style']['elements'] ) ) { 52 return false; 41 53 } 42 54 43 55 $element_color_properties = array( 44 56 'button' => array( 45 'skip' => wp_should_skip_block_supports_serialization( $block_type, 'color', 'button' ),57 'skip' => isset( $options['button']['skip'] ) ? $options['button']['skip'] : false, 46 58 'paths' => array( 47 59 array( 'button', 'color', 'text' ), … … 51 63 ), 52 64 'link' => array( 53 'skip' => wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' ),65 'skip' => isset( $options['link']['skip'] ) ? $options['link']['skip'] : false, 54 66 'paths' => array( 55 67 array( 'link', 'color', 'text' ), … … 58 70 ), 59 71 'heading' => array( 60 'skip' => wp_should_skip_block_supports_serialization( $block_type, 'color', 'heading' ),72 'skip' => isset( $options['heading']['skip'] ) ? $options['heading']['skip'] : false, 61 73 'paths' => array( 62 74 array( 'heading', 'color', 'text' ), … … 85 97 ); 86 98 87 $skip_all_element_color_serialization = $element_color_properties['button']['skip'] &&88 $element_color_properties['link']['skip'] &&89 $element_color_properties['heading']['skip'];90 91 if ( $skip_all_element_color_serialization ) {92 return $block_content;93 }94 95 99 $elements_style_attributes = $block['attrs']['style']['elements']; 96 100 … … 102 106 foreach ( $element_config['paths'] as $path ) { 103 107 if ( null !== _wp_array_get( $elements_style_attributes, $path, null ) ) { 104 /* 105 * It only takes a single custom attribute to require that the custom 106 * class name be added to the block, so once one is found there's no 107 * need to continue looking for others. 108 * 109 * As is done with the layout hook, this code assumes that the block 110 * contains a single wrapper and that it's the first element in the 111 * rendered output. That first element, if it exists, gets the class. 112 */ 113 $tags = new WP_HTML_Tag_Processor( $block_content ); 114 if ( $tags->next_tag() ) { 115 $tags->add_class( wp_get_elements_class_name( $block ) ); 116 } 117 118 return $tags->get_updated_html(); 108 return true; 119 109 } 120 110 } 121 111 } 122 112 123 // If no custom attributes were found then there's nothing to modify. 124 return $block_content; 125 } 126 127 /** 128 * Renders the elements stylesheet. 113 return false; 114 } 115 116 /** 117 * Render the elements stylesheet and adds elements class name to block as required. 129 118 * 130 119 * In the case of nested blocks we want the parent element styles to be rendered before their descendants. … … 134 123 * @since 6.0.0 135 124 * @since 6.1.0 Implemented the style engine to generate CSS and classnames. 136 * @access private 137 * 138 * @param string|null $pre_render The pre-rendered content. Default null. 139 * @param array $block The block being rendered. 140 * @return null 141 */ 142 function wp_render_elements_support_styles( $pre_render, $block ) { 143 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); 144 $element_block_styles = isset( $block['attrs']['style']['elements'] ) ? $block['attrs']['style']['elements'] : null; 125 * @since 6.6.0 Element block support class and styles are generated via the `render_block_data` filter instead of `pre_render_block`. 126 * @access private 127 * 128 * @param array $parsed_block The parsed block. 129 * @return array The same parsed block with elements classname added if appropriate. 130 */ 131 function wp_render_elements_support_styles( $parsed_block ) { 132 /* 133 * The generation of element styles and classname were moved to the 134 * `render_block_data` filter in 6.6.0 to avoid filtered attributes 135 * breaking the application of the elements CSS class. 136 * 137 * @see https://github.com/WordPress/gutenberg/pull/59535. 138 * 139 * The change in filter means, the argument types for this function 140 * have changed and require deprecating. 141 */ 142 if ( is_string( $parsed_block ) ) { 143 _deprecated_argument( 144 __FUNCTION__, 145 '6.6.0', 146 __( 'Use as a `pre_render_block` filter is deprecated. Use with `render_block_data` instead.' ) 147 ); 148 } 149 150 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $parsed_block['blockName'] ); 151 $element_block_styles = isset( $parsed_block['attrs']['style']['elements'] ) ? $parsed_block['attrs']['style']['elements'] : null; 145 152 146 153 if ( ! $element_block_styles ) { 147 return null;154 return $parsed_block; 148 155 } 149 156 … … 156 163 157 164 if ( $skips_all_element_color_serialization ) { 158 return null; 159 } 160 161 $class_name = wp_get_elements_class_name( $block ); 162 165 return $parsed_block; 166 } 167 168 $options = array( 169 'button' => array( 'skip' => $skip_button_color_serialization ), 170 'link' => array( 'skip' => $skip_link_color_serialization ), 171 'heading' => array( 'skip' => $skip_heading_color_serialization ), 172 ); 173 174 if ( ! wp_should_add_elements_class_name( $parsed_block, $options ) ) { 175 return $parsed_block; 176 } 177 178 $class_name = wp_get_elements_class_name( $parsed_block ); 179 $updated_class_name = isset( $parsed_block['attrs']['className'] ) ? $parsed_block['attrs']['className'] . " $class_name" : $class_name; 180 181 _wp_array_set( $parsed_block, array( 'attrs', 'className' ), $updated_class_name ); 182 183 // Generate element styles based on selector and store in style engine for enqueuing. 163 184 $element_types = array( 164 185 'button' => array( … … 226 247 } 227 248 228 return null; 229 } 230 231 add_filter( 'render_block', 'wp_render_elements_support', 10, 2 ); 232 add_filter( 'pre_render_block', 'wp_render_elements_support_styles', 10, 2 ); 249 return $parsed_block; 250 } 251 252 /** 253 * Ensure the elements block support class name generated, and added to 254 * block attributes, in the `render_block_data` filter gets applied to the 255 * block's markup. 256 * 257 * @see wp_render_elements_support_styles 258 * @since 6.6.0 259 * 260 * @param string $block_content Rendered block content. 261 * @param array $block Block object. 262 * 263 * @return string Filtered block content. 264 */ 265 function wp_render_elements_class_name( $block_content, $block ) { 266 $class_string = $block['attrs']['className'] ?? ''; 267 preg_match( '/\bwp-elements-\S+\b/', $class_string, $matches ); 268 269 if ( empty( $matches ) ) { 270 return $block_content; 271 } 272 273 $tags = new WP_HTML_Tag_Processor( $block_content ); 274 275 if ( $tags->next_tag() ) { 276 $tags->add_class( $matches[0] ); 277 } 278 279 return $tags->get_updated_html(); 280 } 281 282 add_filter( 'render_block', 'wp_render_elements_class_name', 10, 2 ); 283 add_filter( 'render_block_data', 'wp_render_elements_support_styles', 10, 1 ); -
trunk/tests/phpunit/tests/block-supports/wpRenderElementsSupport.php
r56943 r58074 45 45 46 46 $block_markup = '<p>Hello <a href="http://www.wordpress.org/">WordPress</a>!</p>'; 47 $actual = wp_render_elements_ support( $block_markup, $block );47 $actual = wp_render_elements_class_name( $block_markup, $block ); 48 48 49 49 $this->assertSame( $block_markup, $actual, 'Expected to leave block content unmodified, but found changes.' ); … … 91 91 ); 92 92 93 $actual = wp_render_elements_support( $block_markup, $block ); 93 /* 94 * To ensure a consistent elements class name it is generated within a 95 * `render_block_data` filter and stored in the `className` attribute. 96 * As a result, the block data needs to be passed through the same 97 * function for this test. 98 */ 99 $filtered_block = wp_render_elements_support_styles( $block ); 100 $actual = wp_render_elements_class_name( $block_markup, $filtered_block ); 94 101 95 102 $this->assertMatchesRegularExpression( -
trunk/tests/phpunit/tests/block-supports/wpRenderElementsSupportStyles.php
r57664 r58074 60 60 ); 61 61 62 wp_render_elements_support_styles( null,$block );62 wp_render_elements_support_styles( $block ); 63 63 $actual_stylesheet = wp_style_engine_get_stylesheet_from_context( 'block-supports', array( 'prettify' => false ) ); 64 64
Note: See TracChangeset
for help on using the changeset viewer.