@@ -30,6 +30,62 @@ function gutenberg_register_background_support( $block_type ) {
3030 }
3131}
3232
33+ /**
34+ * Gets the background classes and styles for a block's attributes.
35+ *
36+ * @since 7.2.0
37+ *
38+ * @param array $block_attributes Block attributes.
39+ * @return array Background `class` and `style` strings, each present only when non-empty.
40+ */
41+ function gutenberg_get_background_classes_and_styles ( $ block_attributes ) {
42+ $ background = $ block_attributes ['style ' ]['background ' ] ?? null ;
43+
44+ if ( ! is_array ( $ background ) ) {
45+ return array ();
46+ }
47+
48+ $ background_styles = array (
49+ 'backgroundImage ' => $ background ['backgroundImage ' ] ?? null ,
50+ 'backgroundSize ' => $ background ['backgroundSize ' ] ?? null ,
51+ 'backgroundPosition ' => $ background ['backgroundPosition ' ] ?? null ,
52+ 'backgroundRepeat ' => $ background ['backgroundRepeat ' ] ?? null ,
53+ 'backgroundAttachment ' => $ background ['backgroundAttachment ' ] ?? null ,
54+ 'gradient ' => $ background ['gradient ' ] ?? null ,
55+ 'backgroundClip ' => $ background ['backgroundClip ' ] ?? null ,
56+ );
57+
58+ if ( ! empty ( $ background_styles ['backgroundImage ' ] ) ) {
59+ $ background_styles ['backgroundSize ' ] = $ background_styles ['backgroundSize ' ] ?? 'cover ' ;
60+ // If the background size is set to `contain` and no position is set, set the position to `center`.
61+ if ( 'contain ' === $ background_styles ['backgroundSize ' ] && ! $ background_styles ['backgroundPosition ' ] ) {
62+ $ background_styles ['backgroundPosition ' ] = '50% 50% ' ;
63+ }
64+ }
65+
66+ $ styles = gutenberg_style_engine_get_styles ( array ( 'background ' => $ background_styles ) );
67+
68+ if ( empty ( $ styles ['css ' ] ) ) {
69+ return array ();
70+ }
71+
72+ $ attributes = array ();
73+
74+ /*
75+ * A background clipped to the text paints the glyphs rather than the
76+ * block's box, so there is no block background to announce.
77+ */
78+ $ has_background = ! empty ( $ background_styles ['backgroundImage ' ] ) || ! empty ( $ background_styles ['gradient ' ] );
79+
80+ if ( $ has_background && 'text ' !== $ background_styles ['backgroundClip ' ] ) {
81+ $ attributes ['class ' ] = 'has-background ' ;
82+ }
83+
84+ $ attributes ['style ' ] = $ styles ['css ' ];
85+
86+ return $ attributes ;
87+ }
88+
3389/**
3490 * Renders the background styles to the block wrapper.
3591 * This block support uses the `render_block` hook to ensure that
@@ -70,13 +126,6 @@ function gutenberg_render_background_support( $block_content, $block ) {
70126 $ background_styles ['backgroundPosition ' ] = $ block_attributes ['style ' ]['background ' ]['backgroundPosition ' ] ?? null ;
71127 $ background_styles ['backgroundRepeat ' ] = $ block_attributes ['style ' ]['background ' ]['backgroundRepeat ' ] ?? null ;
72128 $ background_styles ['backgroundAttachment ' ] = $ block_attributes ['style ' ]['background ' ]['backgroundAttachment ' ] ?? null ;
73- if ( ! empty ( $ background_styles ['backgroundImage ' ] ) ) {
74- $ background_styles ['backgroundSize ' ] = $ background_styles ['backgroundSize ' ] ?? 'cover ' ;
75- // If the background size is set to `contain` and no position is set, set the position to `center`.
76- if ( 'contain ' === $ background_styles ['backgroundSize ' ] && ! $ background_styles ['backgroundPosition ' ] ) {
77- $ background_styles ['backgroundPosition ' ] = '50% 50% ' ;
78- }
79- }
80129 }
81130
82131 if ( ! $ skip_background_gradient ) {
@@ -87,34 +136,28 @@ function gutenberg_render_background_support( $block_content, $block ) {
87136 $ background_styles ['backgroundClip ' ] = $ block_attributes ['style ' ]['background ' ]['backgroundClip ' ] ?? null ;
88137 }
89138
90- $ styles = gutenberg_style_engine_get_styles ( array ( 'background ' => $ background_styles ) );
139+ // Pass only serialized values, so a skipped or unsupported clip cannot suppress `has-background`.
140+ $ styles = gutenberg_get_background_classes_and_styles (
141+ array ( 'style ' => array ( 'background ' => $ background_styles ) )
142+ );
91143
92- if ( ! empty ( $ styles ['css ' ] ) ) {
144+ if ( ! empty ( $ styles ['style ' ] ) ) {
93145 // Inject background styles to the first element, presuming it's the wrapper, if it exists.
94146 $ tags = new WP_HTML_Tag_Processor ( $ block_content );
95147
96148 if ( $ tags ->next_tag () ) {
97149 $ existing_style = $ tags ->get_attribute ( 'style ' );
98150 if ( is_string ( $ existing_style ) && '' !== $ existing_style ) {
99151 $ separator = str_ends_with ( $ existing_style , '; ' ) ? '' : '; ' ;
100- $ updated_style = "{$ existing_style }{$ separator }{$ styles ['css ' ]}" ;
152+ $ updated_style = "{$ existing_style }{$ separator }{$ styles ['style ' ]}" ;
101153 } else {
102- $ updated_style = $ styles ['css ' ];
154+ $ updated_style = $ styles ['style ' ];
103155 }
104156
105157 $ tags ->set_attribute ( 'style ' , $ updated_style );
106158
107- /*
108- * A background clipped to the text paints the glyphs rather than the
109- * block's box, so there is no block background to announce. Read the
110- * clip from the serialized styles so an unsupported or skipped value
111- * left in the block's attributes cannot suppress the class.
112- */
113- $ background_clip = $ background_styles ['backgroundClip ' ] ?? null ;
114- $ has_background = ! empty ( $ background_styles ['backgroundImage ' ] ) || ! empty ( $ background_styles ['gradient ' ] );
115-
116- if ( $ has_background && 'text ' !== $ background_clip ) {
117- $ tags ->add_class ( 'has-background ' );
159+ if ( ! empty ( $ styles ['class ' ] ) ) {
160+ $ tags ->add_class ( $ styles ['class ' ] );
118161 }
119162 }
120163
0 commit comments