PluginProbe
Gutenberg / 17.0.2
Gutenberg v17.0.2
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
← All changes | lib/block-supports/background.php +27 -41 23.7.0 → 17.0.2 View file →
@@ -39,51 +39,38 @@
39 39 * @param array $block Block object.
40 40 * @return string Filtered block content.
41 41 */
42 42 function gutenberg_render_background_support( $block_content, $block ) {
43 - $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
44 - $block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array();
45 - $has_background_image_support = block_has_support( $block_type, array( 'background', 'backgroundImage' ), false );
46 - $has_background_gradient_support = block_has_support( $block_type, array( 'background', 'gradient' ), false );
43 + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
44 + $block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array();
45 + $has_background_image_support = block_has_support( $block_type, array( 'background', 'backgroundImage' ), false );
47 46
48 47 if (
49 - ( ! $has_background_image_support && ! $has_background_gradient_support ) ||
50 - ! isset( $block_attributes['style']['background'] )
48 + ! $has_background_image_support ||
49 + wp_should_skip_block_supports_serialization( $block_type, 'background', 'backgroundImage' )
51 50 ) {
52 51 return $block_content;
53 52 }
54 53
55 - // Check serialization skip for each feature individually.
56 - $skip_background_image = ! $has_background_image_support || wp_should_skip_block_supports_serialization( $block_type, 'background', 'backgroundImage' );
57 - $skip_background_gradient = ! $has_background_gradient_support || wp_should_skip_block_supports_serialization( $block_type, 'background', 'gradient' );
54 + $background_image_source = $block_attributes['style']['background']['backgroundImage']['source'] ?? null;
55 + $background_image_url = $block_attributes['style']['background']['backgroundImage']['url'] ?? null;
56 + $background_size = $block_attributes['style']['background']['backgroundSize'] ?? 'cover';
58 57
59 - if ( $skip_background_image && $skip_background_gradient ) {
60 - return $block_content;
61 - }
58 + $background_block_styles = array();
62 59
63 - $background_styles = array();
64 -
65 - if ( ! $skip_background_image ) {
66 - $background_styles['backgroundImage'] = $block_attributes['style']['background']['backgroundImage'] ?? null;
67 - $background_styles['backgroundSize'] = $block_attributes['style']['background']['backgroundSize'] ?? null;
68 - $background_styles['backgroundPosition'] = $block_attributes['style']['background']['backgroundPosition'] ?? null;
69 - $background_styles['backgroundRepeat'] = $block_attributes['style']['background']['backgroundRepeat'] ?? null;
70 - $background_styles['backgroundAttachment'] = $block_attributes['style']['background']['backgroundAttachment'] ?? null;
71 - if ( ! empty( $background_styles['backgroundImage'] ) ) {
72 - $background_styles['backgroundSize'] = $background_styles['backgroundSize'] ?? 'cover';
73 - // If the background size is set to `contain` and no position is set, set the position to `center`.
74 - if ( 'contain' === $background_styles['backgroundSize'] && ! $background_styles['backgroundPosition'] ) {
75 - $background_styles['backgroundPosition'] = '50% 50%';
76 - }
77 - }
60 + if (
61 + 'file' === $background_image_source &&
62 + $background_image_url
63 + ) {
64 + // Set file based background URL.
65 + // TODO: In a follow-up, similar logic could be added to inject a featured image url.
66 + $background_block_styles['backgroundImage']['url'] = $background_image_url;
67 + // Only output the background size when an image url is set.
68 + $background_block_styles['backgroundSize'] = $background_size;
78 69 }
79 70
80 - if ( ! $skip_background_gradient ) {
81 - $background_styles['gradient'] = $block_attributes['style']['background']['gradient'] ?? null;
82 - }
71 + $styles = gutenberg_style_engine_get_styles( array( 'background' => $background_block_styles ) );
83 72
84 - $styles = gutenberg_style_engine_get_styles( array( 'background' => $background_styles ) );
85 -
86 73 if ( ! empty( $styles['css'] ) ) {
87 74 // Inject background styles to the first element, presuming it's the wrapper, if it exists.
88 75 $tags = new WP_HTML_Tag_Processor( $block_content );
89 76
@@ -88,17 +75,19 @@
88 75 $tags = new WP_HTML_Tag_Processor( $block_content );
89 76
90 77 if ( $tags->next_tag() ) {
91 78 $existing_style = $tags->get_attribute( 'style' );
92 - if ( is_string( $existing_style ) && '' !== $existing_style ) {
93 - $separator = str_ends_with( $existing_style, ';' ) ? '' : ';';
94 - $updated_style = "{$existing_style}{$separator}{$styles['css']}";
95 - } else {
96 - $updated_style = $styles['css'];
79 + $updated_style = '';
80 +
81 + if ( ! empty( $existing_style ) ) {
82 + $updated_style = $existing_style;
83 + if ( ! str_ends_with( $existing_style, ';' ) ) {
84 + $updated_style .= ';';
85 + }
97 86 }
98 87
88 + $updated_style .= $styles['css'];
99 89 $tags->set_attribute( 'style', $updated_style );
100 - $tags->add_class( 'has-background' );
101 90 }
102 91
103 92 return $tags->get_updated_html();
104 93 }
@@ -113,8 +102,5 @@
113 102 'register_attribute' => 'gutenberg_register_background_support',
114 103 )
115 104 );
116 105
117 -if ( function_exists( 'wp_render_background_support' ) ) {
118 - remove_filter( 'render_block', 'wp_render_background_support' );
119 -}
120 106 add_filter( 'render_block', 'gutenberg_render_background_support', 10, 2 );