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 -112 23.1.1 → 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,124 +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 - * Renders server-side dimensions styles to the block wrapper.
84 - * This block support uses the `render_block` hook to ensure that
85 - * it is also applied to non-server-rendered blocks.
86 - *
87 - * @param string $block_content Rendered block content.
88 - * @param array $block Block object.
89 - * @return string Filtered block content.
90 - */
91 -function gutenberg_render_dimensions_support( $block_content, $block ) {
92 - $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
93 - $block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array();
94 - $has_aspect_ratio_support = block_has_support( $block_type, array( 'dimensions', 'aspectRatio' ), false );
95 -
96 - if (
97 - ! $has_aspect_ratio_support ||
98 - wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'aspectRatio' )
99 - ) {
100 - return $block_content;
101 - }
102 -
103 - $dimensions_block_styles = array();
104 - $dimensions_block_styles['aspectRatio'] = $block_attributes['style']['dimensions']['aspectRatio'] ?? null;
105 -
106 - // To ensure the aspect ratio does not get overridden by `minHeight` or `height` unset any existing rule.
107 - if (
108 - isset( $dimensions_block_styles['aspectRatio'] )
109 - ) {
110 - $dimensions_block_styles['minHeight'] = 'unset';
111 - $dimensions_block_styles['height'] = 'unset';
112 - } elseif (
113 - isset( $block_attributes['style']['dimensions']['minHeight'] ) ||
114 - isset( $block_attributes['minHeight'] ) ||
115 - isset( $block_attributes['style']['dimensions']['height'] ) ||
116 - isset( $block_attributes['height'] )
117 - ) {
118 - $dimensions_block_styles['aspectRatio'] = 'unset';
119 - }
120 -
121 - $styles = gutenberg_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );
122 -
123 - if ( ! empty( $styles['css'] ) ) {
124 - // Inject dimensions styles to the first element, presuming it's the wrapper, if it exists.
125 - $tags = new WP_HTML_Tag_Processor( $block_content );
126 -
127 - if ( $tags->next_tag() ) {
128 - $existing_style = $tags->get_attribute( 'style' );
129 - $updated_style = '';
130 -
131 - if ( ! empty( $existing_style ) ) {
132 - $updated_style = $existing_style;
133 - if ( ! str_ends_with( $existing_style, ';' ) ) {
134 - $updated_style .= ';';
135 - }
136 - }
137 -
138 - $updated_style .= $styles['css'];
139 - $tags->set_attribute( 'style', $updated_style );
140 -
141 - if ( ! empty( $styles['classnames'] ) ) {
142 - foreach ( explode( ' ', $styles['classnames'] ) as $class_name ) {
143 - if (
144 - str_contains( $class_name, 'aspect-ratio' ) &&
145 - ! isset( $block_attributes['style']['dimensions']['aspectRatio'] )
146 - ) {
147 - continue;
148 - }
149 - $tags->add_class( $class_name );
150 - }
151 - }
152 - }
153 -
154 - return $tags->get_updated_html();
155 - }
156 -
157 - return $block_content;
158 -}
159 -
160 -remove_filter( 'render_block', 'wp_render_dimensions_support', 10 );
161 -add_filter( 'render_block', 'gutenberg_render_dimensions_support', 10, 2 );
162 59
163 60 // Register the block support.
164 61 WP_Block_Supports::get_instance()->register(
165 62 'dimensions',