PluginProbe
Gutenberg / 14.5.2
Gutenberg v14.5.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
← All changes | lib/block-supports/dimensions.php +9 -128 23.5.0 → 14.5.2 View file →
@@ -24,9 +24,10 @@
24 24 if ( array_key_exists( 'style', $block_type->attributes ) ) {
25 25 return;
26 26 }
27 27
28 - $has_dimensions_support = block_has_support( $block_type, array( 'dimensions' ), false );
28 + $has_dimensions_support = block_has_support( $block_type, array( '__experimentalDimensions' ), false );
29 + // Future block supports such as height & width will be added here.
29 30
30 31 if ( $has_dimensions_support ) {
31 32 $block_type->attributes['style'] = array(
32 33 'type' => 'object',
@@ -42,140 +43,20 @@
42 43 * @param array $block_attributes Block attributes.
43 44 *
44 45 * @return array Block dimensions CSS classes and inline styles.
45 46 */
46 -function gutenberg_apply_dimensions_support( $block_type, $block_attributes ) {
47 - $attributes = array();
48 -
49 - if ( wp_should_skip_block_supports_serialization( $block_type, 'dimensions' ) ) {
50 - return $attributes;
47 +function gutenberg_apply_dimensions_support( $block_type, $block_attributes ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
48 + if ( gutenberg_should_skip_block_supports_serialization( $block_type, '__experimentalDimensions' ) ) {
49 + return array();
51 50 }
52 51
53 - $block_styles = $block_attributes['style'] ?? null;
52 + $styles = array();
54 53
55 - if ( ! $block_styles ) {
56 - return $attributes;
57 - }
54 + // Height support to be added in near future.
55 + // Width support to be added in near future.
58 56
59 - $dimensions_block_styles = array();
60 - $supported_features = array( 'minHeight', 'minWidth', 'height', 'width' );
61 -
62 - foreach ( $supported_features as $feature ) {
63 - $has_support = block_has_support( $block_type, array( 'dimensions', $feature ), false );
64 - $skip_serialization = wp_should_skip_block_supports_serialization( $block_type, 'dimensions', $feature );
65 -
66 - $dimensions_block_styles[ $feature ] = null;
67 -
68 - if ( $has_support && ! $skip_serialization ) {
69 - $dimensions_block_styles[ $feature ] = $block_styles['dimensions'][ $feature ] ?? null;
70 - }
71 - }
72 -
73 - $styles = gutenberg_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );
74 -
75 - if ( ! empty( $styles['css'] ) ) {
76 - $attributes['style'] = $styles['css'];
77 - }
78 -
79 - return $attributes;
57 + return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) );
80 58 }
81 -
82 -/**
83 - * Checks whether an aspectRatio block-support value is explicitly set.
84 - *
85 - * @param mixed $aspect_ratio Aspect-ratio value.
86 - * @return bool Whether the value is an explicit aspect ratio.
87 - */
88 -function gutenberg_is_explicit_aspect_ratio_value( $aspect_ratio ) {
89 - if ( ! is_string( $aspect_ratio ) && ! is_numeric( $aspect_ratio ) ) {
90 - return false;
91 - }
92 -
93 - $aspect_ratio = strtolower( trim( (string) $aspect_ratio ) );
94 -
95 - return '' !== $aspect_ratio && 'auto' !== $aspect_ratio;
96 -}
97 -
98 -/**
99 - * Renders server-side dimensions styles to the block wrapper.
100 - * This block support uses the `render_block` hook to ensure that
101 - * it is also applied to non-server-rendered blocks.
102 - *
103 - * @param string $block_content Rendered block content.
104 - * @param array $block Block object.
105 - * @return string Filtered block content.
106 - */
107 -function gutenberg_render_dimensions_support( $block_content, $block ) {
108 - $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
109 - $block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array();
110 - $has_aspect_ratio_support = block_has_support( $block_type, array( 'dimensions', 'aspectRatio' ), false );
111 -
112 - if (
113 - ! $has_aspect_ratio_support ||
114 - wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'aspectRatio' )
115 - ) {
116 - return $block_content;
117 - }
118 -
119 - $dimensions_block_styles = array();
120 - $dimensions_block_styles['aspectRatio'] = $block_attributes['style']['dimensions']['aspectRatio'] ?? null;
121 -
122 - // To ensure the aspect ratio does not get overridden by `minHeight` or `height` unset any existing rule.
123 - if (
124 - gutenberg_is_explicit_aspect_ratio_value( $dimensions_block_styles['aspectRatio'] )
125 - ) {
126 - $dimensions_block_styles['minHeight'] = 'unset';
127 - $dimensions_block_styles['height'] = 'unset';
128 - } elseif (
129 - isset( $block_attributes['style']['dimensions']['minHeight'] ) ||
130 - isset( $block_attributes['minHeight'] ) ||
131 - isset( $block_attributes['style']['dimensions']['height'] ) ||
132 - isset( $block_attributes['height'] )
133 - ) {
134 - $dimensions_block_styles['aspectRatio'] = 'unset';
135 - }
136 -
137 - $styles = gutenberg_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );
138 -
139 - if ( ! empty( $styles['css'] ) ) {
140 - // Inject dimensions styles to the first element, presuming it's the wrapper, if it exists.
141 - $tags = new WP_HTML_Tag_Processor( $block_content );
142 -
143 - if ( $tags->next_tag() ) {
144 - $existing_style = $tags->get_attribute( 'style' );
145 - $updated_style = '';
146 -
147 - if ( ! empty( $existing_style ) ) {
148 - $updated_style = $existing_style;
149 - if ( ! str_ends_with( $existing_style, ';' ) ) {
150 - $updated_style .= ';';
151 - }
152 - }
153 -
154 - $updated_style .= $styles['css'];
155 - $tags->set_attribute( 'style', $updated_style );
156 -
157 - if ( ! empty( $styles['classnames'] ) ) {
158 - foreach ( explode( ' ', $styles['classnames'] ) as $class_name ) {
159 - if (
160 - str_contains( $class_name, 'aspect-ratio' ) &&
161 - ! gutenberg_is_explicit_aspect_ratio_value( $block_attributes['style']['dimensions']['aspectRatio'] ?? null )
162 - ) {
163 - continue;
164 - }
165 - $tags->add_class( $class_name );
166 - }
167 - }
168 - }
169 -
170 - return $tags->get_updated_html();
171 - }
172 -
173 - return $block_content;
174 -}
175 -
176 -remove_filter( 'render_block', 'wp_render_dimensions_support', 10 );
177 -add_filter( 'render_block', 'gutenberg_render_dimensions_support', 10, 2 );
178 59
179 60 // Register the block support.
180 61 WP_Block_Supports::get_instance()->register(
181 62 'dimensions',