@@ -124,12 +124,13 @@ function wp_process_custom_css_state_entries( $state_entries, $selector ) {
124124}
125125
126126/**
127- * Render the custom CSS stylesheet and add class name to block as required .
127+ * Generates the custom CSS class name for a block and registers its stylesheet .
128128 *
129- * @since 7.0.0
129+ * @since 7.2.0
130+ * @access private
130131 *
131132 * @param array $parsed_block The parsed block.
132- * @return array The same parsed block with custom CSS class name added if appropriate .
133+ * @return string|null The generated class name, or null if the block has no custom CSS to render .
133134 *
134135 * @phpstan-param array{
135136 * blockName: string|null,
@@ -143,51 +144,32 @@ function wp_process_custom_css_state_entries( $state_entries, $selector ) {
143144 * },
144145 * ...
145146 * } $parsed_block
146- * @phpstan-return array{
147- * blockName: string|null,
148- * attrs: array{
149- * className?: string,
150- * style?: array{
151- * css?: string,
152- * ...
153- * },
154- * ...
155- * },
156- * ...
157- * }
158147 */
159- function wp_render_custom_css_support_styles ( $ parsed_block ) {
148+ function wp_generate_custom_css_class_name ( $ parsed_block ) {
160149 $ style = $ parsed_block ['attrs ' ]['style ' ] ?? null ;
161150 if ( empty ( $ style ) || ! is_array ( $ style ) ) {
162- return $ parsed_block ;
151+ return null ;
163152 }
164153
165154 $ block_type = WP_Block_Type_Registry ::get_instance ()->get_registered ( $ parsed_block ['blockName ' ] );
166155 if ( ! block_has_support ( $ block_type , 'customCSS ' , true ) ) {
167- return $ parsed_block ;
156+ return null ;
168157 }
169158
170159 $ viewport_settings = wp_get_global_settings ( array ( 'viewport ' ) );
171160 $ responsive_media_queries = WP_Theme_JSON ::get_viewport_media_queries ( $ viewport_settings );
172161 $ state_entries = wp_get_custom_css_state_entries ( $ style , $ parsed_block ['blockName ' ], $ responsive_media_queries );
173162 if ( empty ( $ state_entries ) ) {
174- return $ parsed_block ;
163+ return null ;
175164 }
176165
177166 // Generate a unique class name for this block instance.
178167 $ class_name = wp_unique_id_from_values ( $ parsed_block , 'wp-custom-css- ' );
179168 $ processed_css = wp_process_custom_css_state_entries ( $ state_entries , '. ' . $ class_name );
180169 if ( '' === $ processed_css ) {
181- return $ parsed_block ;
170+ return null ;
182171 }
183172
184- $ existing_class_name = $ parsed_block ['attrs ' ]['className ' ] ?? null ;
185- $ updated_class_name = is_string ( $ existing_class_name )
186- ? "$ existing_class_name $ class_name "
187- : $ class_name ;
188-
189- $ parsed_block ['attrs ' ]['className ' ] = $ updated_class_name ;
190-
191173 /**
192174 * Reuse one handle so identical custom CSS is enqueued only once via
193175 * {@see wp_unique_id_from_values()}. Explicitly declare the `wp-block-library`
@@ -206,6 +188,55 @@ function wp_render_custom_css_support_styles( $parsed_block ) {
206188 wp_add_inline_style ( $ handle , $ processed_css );
207189 }
208190
191+ return $ class_name ;
192+ }
193+
194+ /**
195+ * Render the custom CSS stylesheet and add class name to block as required.
196+ *
197+ * @since 7.0.0
198+ *
199+ * @param array $parsed_block The parsed block.
200+ * @return array The same parsed block with custom CSS class name added if appropriate.
201+ *
202+ * @phpstan-param array{
203+ * blockName: string|null,
204+ * attrs: array{
205+ * className?: string,
206+ * style?: array{
207+ * css?: string,
208+ * ...
209+ * },
210+ * ...
211+ * },
212+ * ...
213+ * } $parsed_block
214+ * @phpstan-return array{
215+ * blockName: string|null,
216+ * attrs: array{
217+ * className?: string,
218+ * style?: array{
219+ * css?: string,
220+ * ...
221+ * },
222+ * ...
223+ * },
224+ * ...
225+ * }
226+ */
227+ function wp_render_custom_css_support_styles ( $ parsed_block ) {
228+ $ class_name = wp_generate_custom_css_class_name ( $ parsed_block );
229+ if ( null === $ class_name ) {
230+ return $ parsed_block ;
231+ }
232+
233+ $ existing_class_name = $ parsed_block ['attrs ' ]['className ' ] ?? null ;
234+ $ updated_class_name = is_string ( $ existing_class_name )
235+ ? "$ existing_class_name $ class_name "
236+ : $ class_name ;
237+
238+ $ parsed_block ['attrs ' ]['className ' ] = $ updated_class_name ;
239+
209240 return $ parsed_block ;
210241}
211242
@@ -221,41 +252,54 @@ function wp_enqueue_block_custom_css() {
221252/**
222253 * Applies the custom CSS class name to the block's rendered HTML.
223254 *
224- * The class name is generated in {@see wp_render_custom_css_support_styles()}
225- * and stored in block attributes. This filter adds it to the actual markup.
255+ * Uses the class name that {@see wp_render_custom_css_support_styles()}
256+ * stored in the block attributes. Blocks that a parent parsed and rendered
257+ * itself never reach that filter, so the class name is generated here instead
258+ * via {@see wp_generate_custom_css_class_name()}.
226259 *
227260 * @since 7.0.0
261+ * @since 7.2.0 Generates the class name when the block attributes lack one.
228262 *
229263 * @param string $block_content Rendered block content.
230264 * @param array $block Block object.
231265 * @return string Filtered block content.
232266 *
233267 * @phpstan-param array{
268+ * blockName: string|null,
234269 * attrs: array{
235270 * className?: string,
271+ * style?: array{
272+ * css?: string,
273+ * ...
274+ * },
236275 * ...
237276 * },
238277 * ...
239278 * } $block
240279 */
241280function wp_render_custom_css_class_name ( $ block_content , $ block ) {
242- $ class_name_attr = $ block ['attrs ' ]['className ' ] ?? null ;
243- $ class_name_prefix = 'wp-custom-css- ' ;
244- if ( ! is_string ( $ class_name_attr ) || ! str_contains ( $ class_name_attr , $ class_name_prefix ) ) {
245- return $ block_content ;
246- }
281+ $ class_name_attr = $ block ['attrs ' ]['className ' ] ?? null ;
282+ $ class_name_prefix = 'wp-custom-css- ' ;
283+ $ matched_class_name = null ;
247284
248285 // Parse out the 'wp-custom-css-*' class name added by wp_render_custom_css_support_styles().
249- $ matched_class_name = null ;
250- $ token_delimiter = " \t\f\r\n" ;
251- $ class_token = strtok ( $ class_name_attr , $ token_delimiter );
252- while ( false !== $ class_token ) {
253- if ( str_starts_with ( $ class_token , $ class_name_prefix ) ) {
254- $ matched_class_name = $ class_token ;
255- break ;
286+ if ( is_string ( $ class_name_attr ) && str_contains ( $ class_name_attr , $ class_name_prefix ) ) {
287+ $ token_delimiter = " \t\f\r\n" ;
288+ $ class_token = strtok ( $ class_name_attr , $ token_delimiter );
289+ while ( false !== $ class_token ) {
290+ if ( str_starts_with ( $ class_token , $ class_name_prefix ) ) {
291+ $ matched_class_name = $ class_token ;
292+ break ;
293+ }
294+ $ class_token = strtok ( $ token_delimiter );
256295 }
257- $ class_token = strtok ( $ token_delimiter );
258296 }
297+
298+ // Direct WP_Block::render() calls skip render_block_data for the block itself.
299+ if ( null === $ matched_class_name ) {
300+ $ matched_class_name = wp_generate_custom_css_class_name ( $ block );
301+ }
302+
259303 if ( null === $ matched_class_name ) {
260304 return $ block_content ;
261305 }
0 commit comments