PluginProbe
Gutenberg / 19.6.1
Gutenberg v19.6.1
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 19.6.1, at lib/block-supports/background.php

109 lines 3.9 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 ! isset( $block_attributes['style']['background'] )
51 ) {
52 return $block_content;
53 }
54
55 $background_styles = array();
56 $background_styles['backgroundImage'] = $block_attributes['style']['background']['backgroundImage'] ?? null;
57 $background_styles['backgroundSize'] = $block_attributes['style']['background']['backgroundSize'] ?? null;
58 $background_styles['backgroundPosition'] = $block_attributes['style']['background']['backgroundPosition'] ?? null;
59 $background_styles['backgroundRepeat'] = $block_attributes['style']['background']['backgroundRepeat'] ?? null;
60 $background_styles['backgroundAttachment'] = $block_attributes['style']['background']['backgroundAttachment'] ?? null;
61 if ( ! empty( $background_styles['backgroundImage'] ) ) {
62 $background_styles['backgroundSize'] = $background_styles['backgroundSize'] ?? 'cover';
63 // If the background size is set to `contain` and no position is set, set the position to `center`.
64 if ( 'contain' === $background_styles['backgroundSize'] && ! $background_styles['backgroundPosition'] ) {
65 $background_styles['backgroundPosition'] = '50% 50%';
66 }
67 }
68
69 $styles = gutenberg_style_engine_get_styles( array( 'background' => $background_styles ) );
70
71 if ( ! empty( $styles['css'] ) ) {
72 // Inject background styles to the first element, presuming it's the wrapper, if it exists.
73 $tags = new WP_HTML_Tag_Processor( $block_content );
74
75 if ( $tags->next_tag() ) {
76 $existing_style = $tags->get_attribute( 'style' );
77 $updated_style = '';
78
79 if ( ! empty( $existing_style ) ) {
80 $updated_style = $existing_style;
81 if ( ! str_ends_with( $existing_style, ';' ) ) {
82 $updated_style .= ';';
83 }
84 }
85
86 $updated_style .= $styles['css'];
87 $tags->set_attribute( 'style', $updated_style );
88 $tags->add_class( 'has-background' );
89 }
90
91 return $tags->get_updated_html();
92 }
93
94 return $block_content;
95 }
96
97 // Register the block support.
98 WP_Block_Supports::get_instance()->register(
99 'background',
100 array(
101 'register_attribute' => 'gutenberg_register_background_support',
102 )
103 );
104
105 if ( function_exists( 'wp_render_background_support' ) ) {
106 remove_filter( 'render_block', 'wp_render_background_support' );
107 }
108 add_filter( 'render_block', 'gutenberg_render_background_support', 10, 2 );
109