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 / dimensions.php

dimensions.php in Gutenberg 19.6.1, at lib/block-supports/dimensions.php

162 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Dimensions block support flag.
4 *
5 * This does not include the `spacing` block support even though that visually
6 * appears under the "Dimensions" panel in the editor. It remains in its
7 * original `spacing.php` file for compatibility with core.
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_dimensions_support( $block_type ) {
18 // Setup attributes and styles within that if needed.
19 if ( ! $block_type->attributes ) {
20 $block_type->attributes = array();
21 }
22
23 // Check for existing style attribute definition e.g. from block.json.
24 if ( array_key_exists( 'style', $block_type->attributes ) ) {
25 return;
26 }
27
28 $has_dimensions_support = block_has_support( $block_type, array( 'dimensions' ), false );
29
30 if ( $has_dimensions_support ) {
31 $block_type->attributes['style'] = array(
32 'type' => 'object',
33 );
34 }
35 }
36
37 /**
38 * Add CSS classes for block dimensions to the incoming attributes array.
39 * This will be applied to the block markup in the front-end.
40 *
41 * @param WP_Block_Type $block_type Block Type.
42 * @param array $block_attributes Block attributes.
43 *
44 * @return array Block dimensions CSS classes and inline styles.
45 */
46 function gutenberg_apply_dimensions_support( $block_type, $block_attributes ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
47 if ( wp_should_skip_block_supports_serialization( $block_type, 'dimensions' ) ) {
48 return array();
49 }
50
51 $attributes = array();
52
53 // Width support to be added in near future.
54
55 $has_min_height_support = block_has_support( $block_type, array( 'dimensions', 'minHeight' ), false );
56 $block_styles = isset( $block_attributes['style'] ) ? $block_attributes['style'] : null;
57
58 if ( ! $block_styles ) {
59 return $attributes;
60 }
61
62 $skip_min_height = wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'minHeight' );
63 $dimensions_block_styles = array();
64 $dimensions_block_styles['minHeight'] = null;
65 if ( $has_min_height_support && ! $skip_min_height ) {
66 $dimensions_block_styles['minHeight'] = $block_styles['dimensions']['minHeight'] ?? null;
67 }
68 $styles = gutenberg_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );
69
70 if ( ! empty( $styles['css'] ) ) {
71 $attributes['style'] = $styles['css'];
72 }
73
74 return $attributes;
75 }
76
77 /**
78 * Renders server-side dimensions styles to the block wrapper.
79 * This block support uses the `render_block` hook to ensure that
80 * it is also applied to non-server-rendered blocks.
81 *
82 * @param string $block_content Rendered block content.
83 * @param array $block Block object.
84 * @return string Filtered block content.
85 */
86 function gutenberg_render_dimensions_support( $block_content, $block ) {
87 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
88 $block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array();
89 $has_aspect_ratio_support = block_has_support( $block_type, array( 'dimensions', 'aspectRatio' ), false );
90
91 if (
92 ! $has_aspect_ratio_support ||
93 wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'aspectRatio' )
94 ) {
95 return $block_content;
96 }
97
98 $dimensions_block_styles = array();
99 $dimensions_block_styles['aspectRatio'] = $block_attributes['style']['dimensions']['aspectRatio'] ?? null;
100
101 // To ensure the aspect ratio does not get overridden by `minHeight` unset any existing rule.
102 if (
103 isset( $dimensions_block_styles['aspectRatio'] )
104 ) {
105 $dimensions_block_styles['minHeight'] = 'unset';
106 } elseif (
107 isset( $block_attributes['style']['dimensions']['minHeight'] ) ||
108 isset( $block_attributes['minHeight'] )
109 ) {
110 $dimensions_block_styles['aspectRatio'] = 'unset';
111 }
112
113 $styles = gutenberg_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );
114
115 if ( ! empty( $styles['css'] ) ) {
116 // Inject dimensions styles to the first element, presuming it's the wrapper, if it exists.
117 $tags = new WP_HTML_Tag_Processor( $block_content );
118
119 if ( $tags->next_tag() ) {
120 $existing_style = $tags->get_attribute( 'style' );
121 $updated_style = '';
122
123 if ( ! empty( $existing_style ) ) {
124 $updated_style = $existing_style;
125 if ( ! str_ends_with( $existing_style, ';' ) ) {
126 $updated_style .= ';';
127 }
128 }
129
130 $updated_style .= $styles['css'];
131 $tags->set_attribute( 'style', $updated_style );
132
133 if ( ! empty( $styles['classnames'] ) ) {
134 foreach ( explode( ' ', $styles['classnames'] ) as $class_name ) {
135 if (
136 str_contains( $class_name, 'aspect-ratio' ) &&
137 ! isset( $block_attributes['style']['dimensions']['aspectRatio'] )
138 ) {
139 continue;
140 }
141 $tags->add_class( $class_name );
142 }
143 }
144 }
145
146 return $tags->get_updated_html();
147 }
148
149 return $block_content;
150 }
151
152 add_filter( 'render_block', 'gutenberg_render_dimensions_support', 10, 2 );
153
154 // Register the block support.
155 WP_Block_Supports::get_instance()->register(
156 'dimensions',
157 array(
158 'register_attribute' => 'gutenberg_register_dimensions_support',
159 'apply' => 'gutenberg_apply_dimensions_support',
160 )
161 );
162