@@ -213,9 +213,6 @@ function wp_render_block_style_variation_class_name( $block_content, $block ) {
213213
214214/**
215215 * Collects block style variation data for merging with theme.json data.
216- * As each block style variation is processed it is registered if it hasn't
217- * been already. This registration is required for later sanitization of
218- * theme.json data.
219216 *
220217 * @since 6.6.0
221218 * @access private
@@ -224,14 +221,13 @@ function wp_render_block_style_variation_class_name( $block_content, $block ) {
224221 *
225222 * @return array Block variations data to be merged under `styles.blocks`.
226223 */
227- function wp_resolve_and_register_block_style_variations ( $ variations ) {
224+ function wp_resolve_block_style_variations ( $ variations ) {
228225 $ variations_data = array ();
229226
230227 if ( empty ( $ variations ) ) {
231228 return $ variations_data ;
232229 }
233230
234- $ registry = WP_Block_Styles_Registry::get_instance ();
235231 $ have_named_variations = ! wp_is_numeric_array ( $ variations );
236232
237233 foreach ( $ variations as $ key => $ variation ) {
@@ -253,23 +249,9 @@ function wp_resolve_and_register_block_style_variations( $variations ) {
253249 * Block style variations read in via standalone theme.json partials
254250 * need to have their name set to the kebab case version of their title.
255251 */
256- $ variation_name = $ have_named_variations ? $ key : _wp_to_kebab_case ( $ variation ['title ' ] );
257- $ variation_label = $ variation ['title ' ] ?? $ variation_name ;
252+ $ variation_name = $ have_named_variations ? $ key : _wp_to_kebab_case ( $ variation ['title ' ] );
258253
259254 foreach ( $ supported_blocks as $ block_type ) {
260- $ registered_styles = $ registry ->get_registered_styles_for_block ( $ block_type );
261-
262- // Register block style variation if it hasn't already been registered.
263- if ( ! array_key_exists ( $ variation_name , $ registered_styles ) ) {
264- register_block_style (
265- $ block_type ,
266- array (
267- 'name ' => $ variation_name ,
268- 'label ' => $ variation_label ,
269- )
270- );
271- }
272-
273255 // Add block style variation data under current block type.
274256 $ path = array ( $ block_type , 'variations ' , $ variation_name );
275257 _wp_array_set ( $ variations_data , $ path , $ variation_data );
@@ -327,7 +309,7 @@ function wp_merge_block_style_variations_data( $variations_data, $theme_json, $o
327309function wp_resolve_block_style_variations_from_theme_style_variation ( $ theme_json ) {
328310 $ theme_json_data = $ theme_json ->get_data ();
329311 $ shared_variations = $ theme_json_data ['styles ' ]['blocks ' ]['variations ' ] ?? array ();
330- $ variations_data = wp_resolve_and_register_block_style_variations ( $ shared_variations );
312+ $ variations_data = wp_resolve_block_style_variations ( $ shared_variations );
331313
332314 return wp_merge_block_style_variations_data ( $ variations_data , $ theme_json , 'user ' );
333315}
@@ -345,7 +327,7 @@ function wp_resolve_block_style_variations_from_theme_style_variation( $theme_js
345327 */
346328function wp_resolve_block_style_variations_from_theme_json_partials ( $ theme_json ) {
347329 $ block_style_variations = WP_Theme_JSON_Resolver::get_style_variations ( 'block ' );
348- $ variations_data = wp_resolve_and_register_block_style_variations ( $ block_style_variations );
330+ $ variations_data = wp_resolve_block_style_variations ( $ block_style_variations );
349331
350332 return wp_merge_block_style_variations_data ( $ variations_data , $ theme_json );
351333}
@@ -364,7 +346,7 @@ function wp_resolve_block_style_variations_from_theme_json_partials( $theme_json
364346function wp_resolve_block_style_variations_from_primary_theme_json ( $ theme_json ) {
365347 $ theme_json_data = $ theme_json ->get_data ();
366348 $ block_style_variations = $ theme_json_data ['styles ' ]['blocks ' ]['variations ' ] ?? array ();
367- $ variations_data = wp_resolve_and_register_block_style_variations ( $ block_style_variations );
349+ $ variations_data = wp_resolve_block_style_variations ( $ block_style_variations );
368350
369351 return wp_merge_block_style_variations_data ( $ variations_data , $ theme_json );
370352}
@@ -422,3 +404,97 @@ function wp_enqueue_block_style_variation_styles() {
422404add_filter ( 'wp_theme_json_data_theme ' , 'wp_resolve_block_style_variations_from_styles_registry ' , 10 , 1 );
423405
424406add_filter ( 'wp_theme_json_data_user ' , 'wp_resolve_block_style_variations_from_theme_style_variation ' , 10 , 1 );
407+
408+ /**
409+ * Registers any block style variations contained within the provided
410+ * theme.json data.
411+ *
412+ * @since 6.6.0
413+ * @access private
414+ *
415+ * @param array $variations Shared block style variations.
416+ */
417+ function wp_register_block_style_variations_from_theme_json_data ( $ variations ) {
418+ if ( empty ( $ variations ) ) {
419+ return $ variations ;
420+ }
421+
422+ $ registry = WP_Block_Styles_Registry::get_instance ();
423+ $ have_named_variations = ! wp_is_numeric_array ( $ variations );
424+
425+ foreach ( $ variations as $ key => $ variation ) {
426+ $ supported_blocks = $ variation ['blockTypes ' ] ?? array ();
427+
428+ /*
429+ * Standalone theme.json partial files for block style variations
430+ * will have their styles under a top-level property by the same name.
431+ * Variations defined within an existing theme.json or theme style
432+ * variation will themselves already be the required styles data.
433+ */
434+ $ variation_data = $ variation ['styles ' ] ?? $ variation ;
435+
436+ if ( empty ( $ variation_data ) ) {
437+ continue ;
438+ }
439+
440+ /*
441+ * Block style variations read in via standalone theme.json partials
442+ * need to have their name set to the kebab case version of their title.
443+ */
444+ $ variation_name = $ have_named_variations ? $ key : _wp_to_kebab_case ( $ variation ['title ' ] );
445+ $ variation_label = $ variation ['title ' ] ?? $ variation_name ;
446+
447+ foreach ( $ supported_blocks as $ block_type ) {
448+ $ registered_styles = $ registry ->get_registered_styles_for_block ( $ block_type );
449+
450+ // Register block style variation if it hasn't already been registered.
451+ if ( ! array_key_exists ( $ variation_name , $ registered_styles ) ) {
452+ register_block_style (
453+ $ block_type ,
454+ array (
455+ 'name ' => $ variation_name ,
456+ 'label ' => $ variation_label ,
457+ )
458+ );
459+ }
460+ }
461+ }
462+ }
463+
464+ /**
465+ * Register shared block style variations defined by the theme.
466+ *
467+ * These can come in three forms:
468+ * - the theme's theme.json
469+ * - the theme's partials (standalone files in `/styles` that only define block style variations)
470+ * - the user's theme.json (for example, theme style variations the user selected)
471+ *
472+ * @since 6.6.0
473+ * @access private
474+ */
475+ function wp_register_block_style_variations_from_theme () {
476+ // Partials from `/styles`.
477+ $ variations_partials = WP_Theme_JSON_Resolver::get_style_variations ( 'block ' );
478+ wp_register_block_style_variations_from_theme_json_data ( $ variations_partials );
479+
480+ /*
481+ * Pull the data from the specific origin instead of the merged data.
482+ * This is because, for 6.6, we only support registering block style variations
483+ * for the 'theme' and 'custom' origins but not for 'default' (core theme.json)
484+ * or 'custom' (theme.json in a block).
485+ *
486+ * When/If we add support for every origin, we should switch to using the public API
487+ * instead, e.g.: wp_get_global_styles( array( 'blocks', 'variations' ) ).
488+ */
489+
490+ // theme.json of the theme.
491+ $ theme_json_theme = WP_Theme_JSON_Resolver::get_theme_data ();
492+ $ variations_theme = $ theme_json_theme ->get_data ()['styles ' ]['blocks ' ]['variations ' ] ?? array ();
493+ wp_register_block_style_variations_from_theme_json_data ( $ variations_theme );
494+
495+ // User data linked for this theme.
496+ $ theme_json_user = WP_Theme_JSON_Resolver::get_user_data ();
497+ $ variations_user = $ theme_json_user ->get_data ()['styles ' ]['blocks ' ]['variations ' ] ?? array ();
498+ wp_register_block_style_variations_from_theme_json_data ( $ variations_user );
499+ }
500+ add_action ( 'init ' , 'wp_register_block_style_variations_from_theme ' );
0 commit comments