42 /** |
41 /** |
43 * Adds CSS classes for block dimensions to the incoming attributes array. |
42 * Adds CSS classes for block dimensions to the incoming attributes array. |
44 * This will be applied to the block markup in the front-end. |
43 * This will be applied to the block markup in the front-end. |
45 * |
44 * |
46 * @since 5.9.0 |
45 * @since 5.9.0 |
|
46 * @since 6.2.0 Added `minHeight` support. |
47 * @access private |
47 * @access private |
48 * |
48 * |
49 * @param WP_Block_Type $block_type Block Type. |
49 * @param WP_Block_Type $block_type Block Type. |
50 * @param array $block_attributes Block attributes. |
50 * @param array $block_attributes Block attributes. |
51 * @return array Block dimensions CSS classes and inline styles. |
51 * @return array Block dimensions CSS classes and inline styles. |
52 */ |
52 */ |
53 function wp_apply_dimensions_support( $block_type, $block_attributes ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
53 function wp_apply_dimensions_support( $block_type, $block_attributes ) { |
54 if ( wp_should_skip_block_supports_serialization( $block_type, '__experimentalDimensions' ) ) { |
54 if ( wp_should_skip_block_supports_serialization( $block_type, 'dimensions' ) ) { |
55 return array(); |
55 return array(); |
56 } |
56 } |
57 |
57 |
58 $styles = array(); |
58 $attributes = array(); |
59 |
59 |
60 // Height support to be added in near future. |
|
61 // Width support to be added in near future. |
60 // Width support to be added in near future. |
62 |
61 |
63 return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) ); |
62 $has_min_height_support = block_has_support( $block_type, array( 'dimensions', 'minHeight' ), false ); |
|
63 $block_styles = isset( $block_attributes['style'] ) ? $block_attributes['style'] : null; |
|
64 |
|
65 if ( ! $block_styles ) { |
|
66 return $attributes; |
|
67 } |
|
68 |
|
69 $skip_min_height = wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'minHeight' ); |
|
70 $dimensions_block_styles = array(); |
|
71 $dimensions_block_styles['minHeight'] = null; |
|
72 if ( $has_min_height_support && ! $skip_min_height ) { |
|
73 $dimensions_block_styles['minHeight'] = isset( $block_styles['dimensions']['minHeight'] ) |
|
74 ? $block_styles['dimensions']['minHeight'] |
|
75 : null; |
|
76 } |
|
77 $styles = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) ); |
|
78 |
|
79 if ( ! empty( $styles['css'] ) ) { |
|
80 $attributes['style'] = $styles['css']; |
|
81 } |
|
82 |
|
83 return $attributes; |
64 } |
84 } |
|
85 |
|
86 /** |
|
87 * Renders server-side dimensions styles to the block wrapper. |
|
88 * This block support uses the `render_block` hook to ensure that |
|
89 * it is also applied to non-server-rendered blocks. |
|
90 * |
|
91 * @since 6.5.0 |
|
92 * @access private |
|
93 * |
|
94 * @param string $block_content Rendered block content. |
|
95 * @param array $block Block object. |
|
96 * @return string Filtered block content. |
|
97 */ |
|
98 function wp_render_dimensions_support( $block_content, $block ) { |
|
99 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); |
|
100 $block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array(); |
|
101 $has_aspect_ratio_support = block_has_support( $block_type, array( 'dimensions', 'aspectRatio' ), false ); |
|
102 |
|
103 if ( |
|
104 ! $has_aspect_ratio_support || |
|
105 wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'aspectRatio' ) |
|
106 ) { |
|
107 return $block_content; |
|
108 } |
|
109 |
|
110 $dimensions_block_styles = array(); |
|
111 $dimensions_block_styles['aspectRatio'] = $block_attributes['style']['dimensions']['aspectRatio'] ?? null; |
|
112 |
|
113 // To ensure the aspect ratio does not get overridden by `minHeight` unset any existing rule. |
|
114 if ( |
|
115 isset( $dimensions_block_styles['aspectRatio'] ) |
|
116 ) { |
|
117 $dimensions_block_styles['minHeight'] = 'unset'; |
|
118 } elseif ( |
|
119 isset( $block_attributes['style']['dimensions']['minHeight'] ) || |
|
120 isset( $block_attributes['minHeight'] ) |
|
121 ) { |
|
122 $dimensions_block_styles['aspectRatio'] = 'unset'; |
|
123 } |
|
124 |
|
125 $styles = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) ); |
|
126 |
|
127 if ( ! empty( $styles['css'] ) ) { |
|
128 // Inject dimensions styles to the first element, presuming it's the wrapper, if it exists. |
|
129 $tags = new WP_HTML_Tag_Processor( $block_content ); |
|
130 |
|
131 if ( $tags->next_tag() ) { |
|
132 $existing_style = $tags->get_attribute( 'style' ); |
|
133 $updated_style = ''; |
|
134 |
|
135 if ( ! empty( $existing_style ) ) { |
|
136 $updated_style = $existing_style; |
|
137 if ( ! str_ends_with( $existing_style, ';' ) ) { |
|
138 $updated_style .= ';'; |
|
139 } |
|
140 } |
|
141 |
|
142 $updated_style .= $styles['css']; |
|
143 $tags->set_attribute( 'style', $updated_style ); |
|
144 |
|
145 if ( ! empty( $styles['classnames'] ) ) { |
|
146 foreach ( explode( ' ', $styles['classnames'] ) as $class_name ) { |
|
147 if ( |
|
148 str_contains( $class_name, 'aspect-ratio' ) && |
|
149 ! isset( $block_attributes['style']['dimensions']['aspectRatio'] ) |
|
150 ) { |
|
151 continue; |
|
152 } |
|
153 $tags->add_class( $class_name ); |
|
154 } |
|
155 } |
|
156 } |
|
157 |
|
158 return $tags->get_updated_html(); |
|
159 } |
|
160 |
|
161 return $block_content; |
|
162 } |
|
163 |
|
164 add_filter( 'render_block', 'wp_render_dimensions_support', 10, 2 ); |
65 |
165 |
66 // Register the block support. |
166 // Register the block support. |
67 WP_Block_Supports::get_instance()->register( |
167 WP_Block_Supports::get_instance()->register( |
68 'dimensions', |
168 'dimensions', |
69 array( |
169 array( |