PluginProbe
Gutenberg / 12.1.0
Gutenberg v12.1.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 7.4.0 All 402 releases
gutenberg / lib / block-supports / spacing.php

spacing.php in Gutenberg 12.1.0, at lib/block-supports/spacing.php

154 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Spacing block support flag.
4 *
5 * For backwards compatibility with core, this remains separate to the
6 * dimensions.php block support despite both belonging under a single panel in
7 * the editor.
8 *
9 * @package gutenberg
10 */
11
12 /**
13 * Registers the style block attribute for block types that support it.
14 *
15 * @param WP_Block_Type $block_type Block Type.
16 */
17 function gutenberg_register_spacing_support( $block_type ) {
18 $has_spacing_support = gutenberg_block_has_support( $block_type, array( 'spacing' ), false );
19
20 // Setup attributes and styles within that if needed.
21 if ( ! $block_type->attributes ) {
22 $block_type->attributes = array();
23 }
24
25 if ( $has_spacing_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
26 $block_type->attributes['style'] = array(
27 'type' => 'object',
28 );
29 }
30 }
31
32 /**
33 * Add CSS classes for block spacing to the incoming attributes array.
34 * This will be applied to the block markup in the front-end.
35 *
36 * @param WP_Block_Type $block_type Block Type.
37 * @param array $block_attributes Block attributes.
38 *
39 * @return array Block spacing CSS classes and inline styles.
40 */
41 function gutenberg_apply_spacing_support( $block_type, $block_attributes ) {
42 if ( gutenberg_skip_spacing_serialization( $block_type ) ) {
43 return array();
44 }
45
46 $has_padding_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'padding' ), false );
47 $has_margin_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'margin' ), false );
48 $styles = array();
49
50 if ( $has_padding_support ) {
51 $padding_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'padding' ), null );
52
53 if ( is_array( $padding_value ) ) {
54 foreach ( $padding_value as $key => $value ) {
55 $styles[] = sprintf( 'padding-%s: %s;', $key, $value );
56 }
57 } elseif ( null !== $padding_value ) {
58 $styles[] = sprintf( 'padding: %s;', $padding_value );
59 }
60 }
61
62 if ( $has_margin_support ) {
63 $margin_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'margin' ), null );
64
65 if ( is_array( $margin_value ) ) {
66 foreach ( $margin_value as $key => $value ) {
67 $styles[] = sprintf( 'margin-%s: %s;', $key, $value );
68 }
69 } elseif ( null !== $margin_value ) {
70 $styles[] = sprintf( 'margin: %s;', $margin_value );
71 }
72 }
73
74 return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) );
75 }
76
77 /**
78 * Checks whether serialization of the current block's spacing properties should
79 * occur.
80 *
81 * @param WP_Block_type $block_type Block type.
82 *
83 * @return boolean Whether to serialize spacing support styles & classes.
84 */
85 function gutenberg_skip_spacing_serialization( $block_type ) {
86 $spacing_support = _wp_array_get( $block_type->supports, array( 'spacing' ), false );
87
88 return is_array( $spacing_support ) &&
89 array_key_exists( '__experimentalSkipSerialization', $spacing_support ) &&
90 $spacing_support['__experimentalSkipSerialization'];
91 }
92
93
94 /**
95 * Renders the spacing gap support to the block wrapper, to ensure
96 * that the CSS variable is rendered in all environments.
97 *
98 * @param string $block_content Rendered block content.
99 * @param array $block Block object.
100 * @return string Filtered block content.
101 */
102 function gutenberg_render_spacing_gap_support( $block_content, $block ) {
103 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
104 $has_gap_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'blockGap' ), false );
105 if ( ! $has_gap_support || ! isset( $block['attrs']['style']['spacing']['blockGap'] ) ) {
106 return $block_content;
107 }
108
109 $gap_value = $block['attrs']['style']['spacing']['blockGap'];
110
111 // Skip if gap value contains unsupported characters.
112 // Regex for CSS value borrowed from `safecss_filter_attr`, and used here
113 // because we only want to match against the value, not the CSS attribute.
114 if ( preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ) {
115 return $block_content;
116 }
117
118 $style = sprintf(
119 '--wp--style--block-gap: %s',
120 esc_attr( $gap_value )
121 );
122
123 // Attempt to update an existing style attribute on the wrapper element.
124 $injected_style = preg_replace(
125 '/^([^>.]+?)(' . preg_quote( 'style="', '/' ) . ')(?=.+?>)/',
126 '$1$2' . $style . '; ',
127 $block_content,
128 1
129 );
130
131 // If there is no existing style attribute, add one to the wrapper element.
132 if ( $injected_style === $block_content ) {
133 $injected_style = preg_replace(
134 '/<([a-zA-Z0-9]+)([ >])/',
135 '<$1 style="' . $style . '"$2',
136 $block_content,
137 1
138 );
139 };
140
141 return $injected_style;
142 }
143
144 // Register the block support.
145 WP_Block_Supports::get_instance()->register(
146 'spacing',
147 array(
148 'register_attribute' => 'gutenberg_register_spacing_support',
149 'apply' => 'gutenberg_apply_spacing_support',
150 )
151 );
152
153 add_filter( 'render_block', 'gutenberg_render_spacing_gap_support', 10, 2 );
154