@@ -209,24 +209,20 @@ function gutenberg_render_block_style_variation_class_name( $block_content, $blo
209209
210210/**
211211 * Collects block style variation data for merging with theme.json data.
212- * As each block style variation is processed it is registered if it hasn't
213- * been already. This registration is required for later sanitization of
214- * theme.json data.
215212 *
216213 * @since 6.6.0
217214 *
218215 * @param array $variations Shared block style variations.
219216 *
220217 * @return array Block variations data to be merged under `styles.blocks`.
221218 */
222- function gutenberg_resolve_and_register_block_style_variations ( $ variations ) {
219+ function gutenberg_resolve_block_style_variations ( $ variations ) {
223220 $ variations_data = array ();
224221
225222 if ( empty ( $ variations ) ) {
226223 return $ variations_data ;
227224 }
228225
229- $ registry = WP_Block_Styles_Registry::get_instance ();
230226 $ have_named_variations = ! wp_is_numeric_array ( $ variations );
231227
232228 foreach ( $ variations as $ key => $ variation ) {
@@ -248,23 +244,9 @@ function gutenberg_resolve_and_register_block_style_variations( $variations ) {
248244 * Block style variations read in via standalone theme.json partials
249245 * need to have their name set to the kebab case version of their title.
250246 */
251- $ variation_name = $ have_named_variations ? $ key : _wp_to_kebab_case ( $ variation ['title ' ] );
252- $ variation_label = $ variation ['title ' ] ?? $ variation_name ;
247+ $ variation_name = $ have_named_variations ? $ key : _wp_to_kebab_case ( $ variation ['title ' ] );
253248
254249 foreach ( $ supported_blocks as $ block_type ) {
255- $ registered_styles = $ registry ->get_registered_styles_for_block ( $ block_type );
256-
257- // Register block style variation if it hasn't already been registered.
258- if ( ! array_key_exists ( $ variation_name , $ registered_styles ) ) {
259- gutenberg_register_block_style (
260- $ block_type ,
261- array (
262- 'name ' => $ variation_name ,
263- 'label ' => $ variation_label ,
264- )
265- );
266- }
267-
268250 // Add block style variation data under current block type.
269251 $ path = array ( $ block_type , 'variations ' , $ variation_name );
270252 _wp_array_set ( $ variations_data , $ path , $ variation_data );
@@ -320,7 +302,7 @@ function gutenberg_merge_block_style_variations_data( $variations_data, $theme_j
320302function gutenberg_resolve_block_style_variations_from_theme_style_variation ( $ theme_json ) {
321303 $ theme_json_data = $ theme_json ->get_data ();
322304 $ shared_variations = $ theme_json_data ['styles ' ]['blocks ' ]['variations ' ] ?? array ();
323- $ variations_data = gutenberg_resolve_and_register_block_style_variations ( $ shared_variations );
305+ $ variations_data = gutenberg_resolve_block_style_variations ( $ shared_variations );
324306
325307 return gutenberg_merge_block_style_variations_data ( $ variations_data , $ theme_json , 'user ' );
326308}
@@ -337,7 +319,7 @@ function gutenberg_resolve_block_style_variations_from_theme_style_variation( $t
337319 */
338320function gutenberg_resolve_block_style_variations_from_theme_json_partials ( $ theme_json ) {
339321 $ block_style_variations = WP_Theme_JSON_Resolver_Gutenberg::get_style_variations ( 'block ' );
340- $ variations_data = gutenberg_resolve_and_register_block_style_variations ( $ block_style_variations );
322+ $ variations_data = gutenberg_resolve_block_style_variations ( $ block_style_variations );
341323
342324 return gutenberg_merge_block_style_variations_data ( $ variations_data , $ theme_json );
343325}
@@ -355,7 +337,7 @@ function gutenberg_resolve_block_style_variations_from_theme_json_partials( $the
355337function gutenberg_resolve_block_style_variations_from_primary_theme_json ( $ theme_json ) {
356338 $ theme_json_data = $ theme_json ->get_data ();
357339 $ block_style_variations = $ theme_json_data ['styles ' ]['blocks ' ]['variations ' ] ?? array ();
358- $ variations_data = gutenberg_resolve_and_register_block_style_variations ( $ block_style_variations );
340+ $ variations_data = gutenberg_resolve_block_style_variations ( $ block_style_variations );
359341
360342 return gutenberg_merge_block_style_variations_data ( $ variations_data , $ theme_json );
361343}
@@ -411,3 +393,101 @@ function gutenberg_enqueue_block_style_variation_styles() {
411393add_filter ( 'wp_theme_json_data_theme ' , 'gutenberg_resolve_block_style_variations_from_styles_registry ' , 10 , 1 );
412394
413395add_filter ( 'wp_theme_json_data_user ' , 'gutenberg_resolve_block_style_variations_from_theme_style_variation ' , 10 , 1 );
396+
397+
398+ /**
399+ * Registers any block style variations contained within the provided
400+ * theme.json data.
401+ *
402+ * @access private
403+ *
404+ * @param array $variations Shared block style variations.
405+ */
406+ function gutenberg_register_block_style_variations_from_theme_json_data ( $ variations ) {
407+ if ( empty ( $ variations ) ) {
408+ return ;
409+ }
410+
411+ $ registry = WP_Block_Styles_Registry::get_instance ();
412+ $ have_named_variations = ! wp_is_numeric_array ( $ variations );
413+
414+ foreach ( $ variations as $ key => $ variation ) {
415+ $ supported_blocks = $ variation ['blockTypes ' ] ?? array ();
416+
417+ /*
418+ * Standalone theme.json partial files for block style variations
419+ * will have their styles under a top-level property by the same name.
420+ * Variations defined within an existing theme.json or theme style
421+ * variation will themselves already be the required styles data.
422+ */
423+ $ variation_data = $ variation ['styles ' ] ?? $ variation ;
424+
425+ if ( empty ( $ variation_data ) ) {
426+ continue ;
427+ }
428+
429+ /*
430+ * Block style variations read in via standalone theme.json partials
431+ * need to have their name set to the kebab case version of their title.
432+ */
433+ $ variation_name = $ have_named_variations ? $ key : _wp_to_kebab_case ( $ variation ['title ' ] );
434+ $ variation_label = $ variation ['title ' ] ?? $ variation_name ;
435+
436+ foreach ( $ supported_blocks as $ block_type ) {
437+ $ registered_styles = $ registry ->get_registered_styles_for_block ( $ block_type );
438+
439+ // Register block style variation if it hasn't already been registered.
440+ if ( ! array_key_exists ( $ variation_name , $ registered_styles ) ) {
441+ register_block_style (
442+ $ block_type ,
443+ array (
444+ 'name ' => $ variation_name ,
445+ 'label ' => $ variation_label ,
446+ )
447+ );
448+ }
449+ }
450+ }
451+ }
452+
453+ /**
454+ * Register shared block style variations defined by the theme.
455+ *
456+ * These can come in three forms:
457+ * - the theme's theme.json
458+ * - the theme's partials (standalone files in `/styles` that only define block style variations)
459+ * - the user's theme.json (for example, theme style variations the user selected)
460+ *
461+ * @access private
462+ */
463+ function gutenberg_register_block_style_variations_from_theme () {
464+ // Partials from `/styles`.
465+ $ variations_partials = WP_Theme_JSON_Resolver_Gutenberg::get_style_variations ( 'block ' );
466+ gutenberg_register_block_style_variations_from_theme_json_data ( $ variations_partials );
467+
468+ /*
469+ * Pull the data from the specific origin instead of the merged data.
470+ * This is because, for 6.6, we only support registering block style variations
471+ * for the 'theme' and 'custom' origins but not for 'default' (core theme.json)
472+ * or 'custom' (theme.json in a block).
473+ *
474+ * When/If we add support for every origin, we should switch to using the public API
475+ * instead, e.g.: wp_get_global_styles( array( 'blocks', 'variations' ) ).
476+ */
477+
478+ // theme.json of the theme.
479+ $ theme_json_theme = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data ();
480+ $ variations_theme = $ theme_json_theme ->get_data ()['styles ' ]['blocks ' ]['variations ' ] ?? array ();
481+ gutenberg_register_block_style_variations_from_theme_json_data ( $ variations_theme );
482+
483+ // User data linked for this theme.
484+ $ theme_json_user = WP_Theme_JSON_Resolver_Gutenberg::get_user_data ();
485+ $ variations_user = $ theme_json_user ->get_data ()['styles ' ]['blocks ' ]['variations ' ] ?? array ();
486+ gutenberg_register_block_style_variations_from_theme_json_data ( $ variations_user );
487+ }
488+
489+ // Remove core init action registering variations.
490+ if ( function_exists ( 'wp_register_block_style_variations_from_theme ' ) ) {
491+ remove_action ( 'init ' , 'wp_register_block_style_variations_from_theme ' );
492+ }
493+ add_action ( 'init ' , 'gutenberg_register_block_style_variations_from_theme ' );
0 commit comments