@@ -23,41 +23,87 @@ function wp_get_elements_class_name( $block ) {
2323 * Updates the block content with elements class names.
2424 *
2525 * @since 5.8.0
26+ * @since 6.4.0 Added support for button and heading element styling.
2627 * @access private
2728 *
2829 * @param string $block_content Rendered block content.
2930 * @param array $block Block object.
3031 * @return string Filtered block content.
3132 */
3233function wp_render_elements_support ( $ block_content , $ block ) {
33- if ( ! $ block_content ) {
34+ if ( ! $ block_content || empty ( $ block [ ' attrs ' ] ) ) {
3435 return $ block_content ;
3536 }
3637
37- $ block_type = WP_Block_Type_Registry::get_instance ()->get_registered ( $ block ['blockName ' ] );
38- $ skip_link_color_serialization = wp_should_skip_block_supports_serialization ( $ block_type , 'color ' , 'link ' );
38+ $ block_type = WP_Block_Type_Registry::get_instance ()->get_registered ( $ block ['blockName ' ] );
39+
40+ $ element_color_properties = array (
41+ 'button ' => array (
42+ 'skip ' => wp_should_skip_block_supports_serialization ( $ block_type , 'color ' , 'button ' ),
43+ 'paths ' => array (
44+ 'style.elements.button.color.text ' ,
45+ 'style.elements.button.color.background ' ,
46+ 'style.elements.button.color.gradient ' ,
47+ ),
48+ ),
49+ 'link ' => array (
50+ 'skip ' => wp_should_skip_block_supports_serialization ( $ block_type , 'color ' , 'link ' ),
51+ 'paths ' => array (
52+ 'style.elements.link.color.text ' ,
53+ 'style.elements.link.:hover.color.text ' ,
54+ ),
55+ ),
56+ 'heading ' => array (
57+ 'skip ' => wp_should_skip_block_supports_serialization ( $ block_type , 'color ' , 'heading ' ),
58+ 'paths ' => array (
59+ 'style.elements.heading.color.text ' ,
60+ 'style.elements.heading.color.background ' ,
61+ 'style.elements.heading.color.gradient ' ,
62+ 'style.elements.h1.color.text ' ,
63+ 'style.elements.h1.color.background ' ,
64+ 'style.elements.h1.color.gradient ' ,
65+ 'style.elements.h2.color.text ' ,
66+ 'style.elements.h2.color.background ' ,
67+ 'style.elements.h2.color.gradient ' ,
68+ 'style.elements.h3.color.text ' ,
69+ 'style.elements.h3.color.background ' ,
70+ 'style.elements.h3.color.gradient ' ,
71+ 'style.elements.h4.color.text ' ,
72+ 'style.elements.h4.color.background ' ,
73+ 'style.elements.h4.color.gradient ' ,
74+ 'style.elements.h5.color.text ' ,
75+ 'style.elements.h5.color.background ' ,
76+ 'style.elements.h5.color.gradient ' ,
77+ 'style.elements.h6.color.text ' ,
78+ 'style.elements.h6.color.background ' ,
79+ 'style.elements.h6.color.gradient ' ,
80+ ),
81+ ),
82+ );
83+
84+ $ skip_all_element_color_serialization = $ element_color_properties ['button ' ]['skip ' ] &&
85+ $ element_color_properties ['link ' ]['skip ' ] &&
86+ $ element_color_properties ['heading ' ]['skip ' ];
3987
40- if ( $ skip_link_color_serialization ) {
88+ if ( $ skip_all_element_color_serialization ) {
4189 return $ block_content ;
4290 }
4391
44- $ link_color = null ;
45- if ( ! empty ( $ block ['attrs ' ] ) ) {
46- $ link_color = _wp_array_get ( $ block ['attrs ' ], array ( 'style ' , 'elements ' , 'link ' , 'color ' , 'text ' ), null );
47- }
92+ $ element_colors_set = 0 ;
93+
94+ foreach ( $ element_color_properties as $ element_config ) {
95+ if ( $ element_config ['skip ' ] ) {
96+ continue ;
97+ }
4898
49- $ hover_link_color = null ;
50- if ( ! empty ( $ block ['attrs ' ] ) ) {
51- $ hover_link_color = _wp_array_get ( $ block ['attrs ' ], array ( 'style ' , 'elements ' , 'link ' , ':hover ' , 'color ' , 'text ' ), null );
99+ foreach ( $ element_config ['paths ' ] as $ path ) {
100+ if ( null !== _wp_array_get ( $ block ['attrs ' ], explode ( '. ' , $ path ), null ) ) {
101+ $ element_colors_set ++;
102+ }
103+ }
52104 }
53105
54- /*
55- * For now we only care about link colors.
56- * This code in the future when we have a public API
57- * should take advantage of WP_Theme_JSON::compute_style_properties
58- * and work for any element and style.
59- */
60- if ( null === $ link_color && null === $ hover_link_color ) {
106+ if ( ! $ element_colors_set ) {
61107 return $ block_content ;
62108 }
63109
@@ -90,33 +136,84 @@ function wp_render_elements_support_styles( $pre_render, $block ) {
90136 $ block_type = WP_Block_Type_Registry::get_instance ()->get_registered ( $ block ['blockName ' ] );
91137 $ element_block_styles = isset ( $ block ['attrs ' ]['style ' ]['elements ' ] ) ? $ block ['attrs ' ]['style ' ]['elements ' ] : null ;
92138
93- /*
94- * For now we only care about link color.
95- */
96- $ skip_link_color_serialization = wp_should_skip_block_supports_serialization ( $ block_type , 'color ' , 'link ' );
139+ if ( ! $ element_block_styles ) {
140+ return null ;
141+ }
97142
98- if ( $ skip_link_color_serialization ) {
143+ $ skip_link_color_serialization = wp_should_skip_block_supports_serialization ( $ block_type , 'color ' , 'link ' );
144+ $ skip_heading_color_serialization = wp_should_skip_block_supports_serialization ( $ block_type , 'color ' , 'heading ' );
145+ $ skip_button_color_serialization = wp_should_skip_block_supports_serialization ( $ block_type , 'color ' , 'button ' );
146+ $ skips_all_element_color_serialization = $ skip_link_color_serialization &&
147+ $ skip_heading_color_serialization &&
148+ $ skip_button_color_serialization ;
149+
150+ if ( $ skips_all_element_color_serialization ) {
99151 return null ;
100152 }
101- $ class_name = wp_get_elements_class_name ( $ block );
102- $ link_block_styles = isset ( $ element_block_styles ['link ' ] ) ? $ element_block_styles ['link ' ] : null ;
103-
104- wp_style_engine_get_styles (
105- $ link_block_styles ,
106- array (
107- 'selector ' => ". $ class_name a " ,
108- 'context ' => 'block-supports ' ,
109- )
153+
154+ $ class_name = wp_get_elements_class_name ( $ block );
155+
156+ $ element_types = array (
157+ 'button ' => array (
158+ 'selector ' => ". $ class_name .wp-element-button, . $ class_name .wp-block-button__link " ,
159+ 'skip ' => $ skip_button_color_serialization ,
160+ ),
161+ 'link ' => array (
162+ 'selector ' => ". $ class_name a " ,
163+ 'hover_selector ' => ". $ class_name a:hover " ,
164+ 'skip ' => $ skip_link_color_serialization ,
165+ ),
166+ 'heading ' => array (
167+ 'selector ' => ". $ class_name h1, . $ class_name h2, . $ class_name h3, . $ class_name h4, . $ class_name h5, . $ class_name h6 " ,
168+ 'skip ' => $ skip_heading_color_serialization ,
169+ 'elements ' => array ( 'h1 ' , 'h2 ' , 'h3 ' , 'h4 ' , 'h5 ' , 'h6 ' ),
170+ ),
110171 );
111172
112- if ( isset ( $ link_block_styles [':hover ' ] ) ) {
113- wp_style_engine_get_styles (
114- $ link_block_styles [':hover ' ],
115- array (
116- 'selector ' => ". $ class_name a:hover " ,
117- 'context ' => 'block-supports ' ,
118- )
119- );
173+ foreach ( $ element_types as $ element_type => $ element_config ) {
174+ if ( $ element_config ['skip ' ] ) {
175+ continue ;
176+ }
177+
178+ $ element_style_object = _wp_array_get ( $ element_block_styles , array ( $ element_type ), null );
179+
180+ // Process primary element type styles.
181+ if ( $ element_style_object ) {
182+ wp_style_engine_get_styles (
183+ $ element_style_object ,
184+ array (
185+ 'selector ' => $ element_config ['selector ' ],
186+ 'context ' => 'block-supports ' ,
187+ )
188+ );
189+
190+ if ( isset ( $ element_style_object [':hover ' ] ) ) {
191+ wp_style_engine_get_styles (
192+ $ element_style_object [':hover ' ],
193+ array (
194+ 'selector ' => $ element_config ['hover_selector ' ],
195+ 'context ' => 'block-supports ' ,
196+ )
197+ );
198+ }
199+ }
200+
201+ // Process related elements e.g. h1-h6 for headings.
202+ if ( isset ( $ element_config ['elements ' ] ) ) {
203+ foreach ( $ element_config ['elements ' ] as $ element ) {
204+ $ element_style_object = _wp_array_get ( $ element_block_styles , array ( $ element ), null );
205+
206+ if ( $ element_style_object ) {
207+ wp_style_engine_get_styles (
208+ $ element_style_object ,
209+ array (
210+ 'selector ' => ". $ class_name $ element " ,
211+ 'context ' => 'block-supports ' ,
212+ )
213+ );
214+ }
215+ }
216+ }
120217 }
121218
122219 return null ;
0 commit comments