PluginProbe
Gutenberg / 23.3.1
Gutenberg v23.3.1
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 7.4.0 All 402 releases
gutenberg / lib / block-supports / background.php

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

121 lines 4.6 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 $has_background_gradient_support = block_has_support( $block_type, array( 'background', 'gradient' ), false );
47
48 if (
49 ( ! $has_background_image_support && ! $has_background_gradient_support ) ||
50 ! isset( $block_attributes['style']['background'] )
51 ) {
52 return $block_content;
53 }
54
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' );
58
59 if ( $skip_background_image && $skip_background_gradient ) {
60 return $block_content;
61 }
62
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 }
78 }
79
80 if ( ! $skip_background_gradient ) {
81 $background_styles['gradient'] = $block_attributes['style']['background']['gradient'] ?? null;
82 }
83
84 $styles = gutenberg_style_engine_get_styles( array( 'background' => $background_styles ) );
85
86 if ( ! empty( $styles['css'] ) ) {
87 // Inject background styles to the first element, presuming it's the wrapper, if it exists.
88 $tags = new WP_HTML_Tag_Processor( $block_content );
89
90 if ( $tags->next_tag() ) {
91 $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'];
97 }
98
99 $tags->set_attribute( 'style', $updated_style );
100 $tags->add_class( 'has-background' );
101 }
102
103 return $tags->get_updated_html();
104 }
105
106 return $block_content;
107 }
108
109 // Register the block support.
110 WP_Block_Supports::get_instance()->register(
111 'background',
112 array(
113 'register_attribute' => 'gutenberg_register_background_support',
114 )
115 );
116
117 if ( function_exists( 'wp_render_background_support' ) ) {
118 remove_filter( 'render_block', 'wp_render_background_support' );
119 }
120 add_filter( 'render_block', 'gutenberg_render_background_support', 10, 2 );
121