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
gutenberg / lib / block-supports / background.php

background.php in Gutenberg 17.0.2, at lib/block-supports/background.php

107 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Background block support flag.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Registers the style block attribute for block types that support it.
10 *
11 * @param WP_Block_Type $block_type Block Type.
12 */
13 function gutenberg_register_background_support( $block_type ) {
14 // Setup attributes and styles within that if needed.
15 if ( ! $block_type->attributes ) {
16 $block_type->attributes = array();
17 }
18
19 // Check for existing style attribute definition e.g. from block.json.
20 if ( array_key_exists( 'style', $block_type->attributes ) ) {
21 return;
22 }
23
24 $has_background_support = block_has_support( $block_type, array( 'background' ), false );
25
26 if ( $has_background_support ) {
27 $block_type->attributes['style'] = array(
28 'type' => 'object',
29 );
30 }
31 }
32
33 /**
34 * Renders the background styles to the block wrapper.
35 * This block support uses the `render_block` hook to ensure that
36 * it is also applied to non-server-rendered blocks.
37 *
38 * @param string $block_content Rendered block content.
39 * @param array $block Block object.
40 * @return string Filtered block content.
41 */
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
47 if (
48 ! $has_background_image_support ||
49 wp_should_skip_block_supports_serialization( $block_type, 'background', 'backgroundImage' )
50 ) {
51 return $block_content;
52 }
53
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';
57
58 $background_block_styles = array();
59
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;
69 }
70
71 $styles = gutenberg_style_engine_get_styles( array( 'background' => $background_block_styles ) );
72
73 if ( ! empty( $styles['css'] ) ) {
74 // Inject background styles to the first element, presuming it's the wrapper, if it exists.
75 $tags = new WP_HTML_Tag_Processor( $block_content );
76
77 if ( $tags->next_tag() ) {
78 $existing_style = $tags->get_attribute( 'style' );
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 }
86 }
87
88 $updated_style .= $styles['css'];
89 $tags->set_attribute( 'style', $updated_style );
90 }
91
92 return $tags->get_updated_html();
93 }
94
95 return $block_content;
96 }
97
98 // Register the block support.
99 WP_Block_Supports::get_instance()->register(
100 'background',
101 array(
102 'register_attribute' => 'gutenberg_register_background_support',
103 )
104 );
105
106 add_filter( 'render_block', 'gutenberg_render_background_support', 10, 2 );
107