28 } |
28 } |
29 } |
29 } |
30 } |
30 } |
31 |
31 |
32 /** |
32 /** |
|
33 * Generates the CSS corresponding to the provided layout. |
|
34 * |
|
35 * @since 5.9.0 |
|
36 * @access private |
|
37 * |
|
38 * @param string $selector CSS selector. |
|
39 * @param array $layout Layout object. The one that is passed has already checked |
|
40 * the existence of default block layout. |
|
41 * @param boolean $has_block_gap_support Whether the theme has support for the block gap. |
|
42 * @param string $gap_value The block gap value to apply. |
|
43 * @param boolean $should_skip_gap_serialization Whether to skip applying the user-defined value set in the editor. |
|
44 * @param string $fallback_gap_value The custom fallback value for block gap. |
|
45 * @return string CSS style. |
|
46 */ |
|
47 function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false, $gap_value = null, $should_skip_gap_serialization = false, $fallback_gap_value = '0.5em' ) { |
|
48 $layout_type = isset( $layout['type'] ) ? $layout['type'] : 'default'; |
|
49 |
|
50 $style = ''; |
|
51 if ( 'default' === $layout_type ) { |
|
52 $content_size = isset( $layout['contentSize'] ) ? $layout['contentSize'] : ''; |
|
53 $wide_size = isset( $layout['wideSize'] ) ? $layout['wideSize'] : ''; |
|
54 |
|
55 $all_max_width_value = $content_size ? $content_size : $wide_size; |
|
56 $wide_max_width_value = $wide_size ? $wide_size : $content_size; |
|
57 |
|
58 // Make sure there is a single CSS rule, and all tags are stripped for security. |
|
59 $all_max_width_value = safecss_filter_attr( explode( ';', $all_max_width_value )[0] ); |
|
60 $wide_max_width_value = safecss_filter_attr( explode( ';', $wide_max_width_value )[0] ); |
|
61 |
|
62 if ( $content_size || $wide_size ) { |
|
63 $style = "$selector > :where(:not(.alignleft):not(.alignright)) {"; |
|
64 $style .= 'max-width: ' . esc_html( $all_max_width_value ) . ';'; |
|
65 $style .= 'margin-left: auto !important;'; |
|
66 $style .= 'margin-right: auto !important;'; |
|
67 $style .= '}'; |
|
68 |
|
69 $style .= "$selector > .alignwide { max-width: " . esc_html( $wide_max_width_value ) . ';}'; |
|
70 $style .= "$selector .alignfull { max-width: none; }"; |
|
71 } |
|
72 |
|
73 $style .= "$selector > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }"; |
|
74 $style .= "$selector > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }"; |
|
75 $style .= "$selector > .aligncenter { margin-left: auto !important; margin-right: auto !important; }"; |
|
76 if ( $has_block_gap_support ) { |
|
77 if ( is_array( $gap_value ) ) { |
|
78 $gap_value = isset( $gap_value['top'] ) ? $gap_value['top'] : null; |
|
79 } |
|
80 $gap_style = $gap_value && ! $should_skip_gap_serialization ? $gap_value : 'var( --wp--style--block-gap )'; |
|
81 $style .= "$selector > * { margin-block-start: 0; margin-block-end: 0; }"; |
|
82 $style .= "$selector > * + * { margin-block-start: $gap_style; margin-block-end: 0; }"; |
|
83 } |
|
84 } elseif ( 'flex' === $layout_type ) { |
|
85 $layout_orientation = isset( $layout['orientation'] ) ? $layout['orientation'] : 'horizontal'; |
|
86 |
|
87 $justify_content_options = array( |
|
88 'left' => 'flex-start', |
|
89 'right' => 'flex-end', |
|
90 'center' => 'center', |
|
91 ); |
|
92 |
|
93 if ( 'horizontal' === $layout_orientation ) { |
|
94 $justify_content_options += array( 'space-between' => 'space-between' ); |
|
95 } |
|
96 |
|
97 $flex_wrap_options = array( 'wrap', 'nowrap' ); |
|
98 $flex_wrap = ! empty( $layout['flexWrap'] ) && in_array( $layout['flexWrap'], $flex_wrap_options, true ) ? |
|
99 $layout['flexWrap'] : |
|
100 'wrap'; |
|
101 |
|
102 $style = "$selector {"; |
|
103 $style .= 'display: flex;'; |
|
104 if ( $has_block_gap_support ) { |
|
105 if ( is_array( $gap_value ) ) { |
|
106 $gap_row = isset( $gap_value['top'] ) ? $gap_value['top'] : $fallback_gap_value; |
|
107 $gap_column = isset( $gap_value['left'] ) ? $gap_value['left'] : $fallback_gap_value; |
|
108 $gap_value = $gap_row === $gap_column ? $gap_row : $gap_row . ' ' . $gap_column; |
|
109 } |
|
110 $gap_style = $gap_value && ! $should_skip_gap_serialization ? $gap_value : "var( --wp--style--block-gap, $fallback_gap_value )"; |
|
111 $style .= "gap: $gap_style;"; |
|
112 } else { |
|
113 $style .= "gap: $fallback_gap_value;"; |
|
114 } |
|
115 |
|
116 $style .= "flex-wrap: $flex_wrap;"; |
|
117 if ( 'horizontal' === $layout_orientation ) { |
|
118 $style .= 'align-items: center;'; |
|
119 /** |
|
120 * Add this style only if is not empty for backwards compatibility, |
|
121 * since we intend to convert blocks that had flex layout implemented |
|
122 * by custom css. |
|
123 */ |
|
124 if ( ! empty( $layout['justifyContent'] ) && array_key_exists( $layout['justifyContent'], $justify_content_options ) ) { |
|
125 $style .= "justify-content: {$justify_content_options[ $layout['justifyContent'] ]};"; |
|
126 } |
|
127 } else { |
|
128 $style .= 'flex-direction: column;'; |
|
129 if ( ! empty( $layout['justifyContent'] ) && array_key_exists( $layout['justifyContent'], $justify_content_options ) ) { |
|
130 $style .= "align-items: {$justify_content_options[ $layout['justifyContent'] ]};"; |
|
131 } else { |
|
132 $style .= 'align-items: flex-start;'; |
|
133 } |
|
134 } |
|
135 $style .= '}'; |
|
136 |
|
137 $style .= "$selector > * { margin: 0; }"; |
|
138 } |
|
139 |
|
140 return $style; |
|
141 } |
|
142 |
|
143 /** |
33 * Renders the layout config to the block wrapper. |
144 * Renders the layout config to the block wrapper. |
34 * |
145 * |
35 * @since 5.8.0 |
146 * @since 5.8.0 |
36 * @access private |
147 * @access private |
37 * |
148 * |
38 * @param string $block_content Rendered block content. |
149 * @param string $block_content Rendered block content. |
39 * @param array $block Block object. |
150 * @param array $block Block object. |
40 * @return string Filtered block content. |
151 * @return string Filtered block content. |
41 */ |
152 */ |
42 function wp_render_layout_support_flag( $block_content, $block ) { |
153 function wp_render_layout_support_flag( $block_content, $block ) { |
43 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); |
154 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); |
44 $support_layout = block_has_support( $block_type, array( '__experimentalLayout' ), false ); |
155 $support_layout = block_has_support( $block_type, array( '__experimentalLayout' ), false ); |
45 if ( ! $support_layout || ! isset( $block['attrs']['layout'] ) ) { |
156 |
|
157 if ( ! $support_layout ) { |
46 return $block_content; |
158 return $block_content; |
47 } |
159 } |
48 |
160 |
49 $used_layout = $block['attrs']['layout']; |
161 $block_gap = wp_get_global_settings( array( 'spacing', 'blockGap' ) ); |
|
162 $default_layout = wp_get_global_settings( array( 'layout' ) ); |
|
163 $has_block_gap_support = isset( $block_gap ) ? null !== $block_gap : false; |
|
164 $default_block_layout = _wp_array_get( $block_type->supports, array( '__experimentalLayout', 'default' ), array() ); |
|
165 $used_layout = isset( $block['attrs']['layout'] ) ? $block['attrs']['layout'] : $default_block_layout; |
50 if ( isset( $used_layout['inherit'] ) && $used_layout['inherit'] ) { |
166 if ( isset( $used_layout['inherit'] ) && $used_layout['inherit'] ) { |
51 $tree = WP_Theme_JSON_Resolver::get_merged_data(); |
|
52 $default_layout = _wp_array_get( $tree->get_settings(), array( 'layout' ) ); |
|
53 if ( ! $default_layout ) { |
167 if ( ! $default_layout ) { |
54 return $block_content; |
168 return $block_content; |
55 } |
169 } |
56 $used_layout = $default_layout; |
170 $used_layout = $default_layout; |
57 } |
171 } |
58 |
172 |
59 $id = uniqid(); |
173 $class_names = array(); |
60 $content_size = isset( $used_layout['contentSize'] ) ? $used_layout['contentSize'] : null; |
174 $container_class = wp_unique_id( 'wp-container-' ); |
61 $wide_size = isset( $used_layout['wideSize'] ) ? $used_layout['wideSize'] : null; |
175 $class_names[] = $container_class; |
62 |
176 |
63 $all_max_width_value = $content_size ? $content_size : $wide_size; |
177 // The following section was added to reintroduce a small set of layout classnames that were |
64 $wide_max_width_value = $wide_size ? $wide_size : $content_size; |
178 // removed in the 5.9 release (https://github.com/WordPress/gutenberg/issues/38719). It is |
65 |
179 // not intended to provide an extended set of classes to match all block layout attributes |
66 // Make sure there is a single CSS rule, and all tags are stripped for security. |
180 // here. |
67 $all_max_width_value = safecss_filter_attr( explode( ';', $all_max_width_value )[0] ); |
181 if ( ! empty( $block['attrs']['layout']['orientation'] ) ) { |
68 $wide_max_width_value = safecss_filter_attr( explode( ';', $wide_max_width_value )[0] ); |
182 $class_names[] = 'is-' . sanitize_title( $block['attrs']['layout']['orientation'] ); |
69 |
183 } |
70 $style = ''; |
184 |
71 if ( $content_size || $wide_size ) { |
185 if ( ! empty( $block['attrs']['layout']['justifyContent'] ) ) { |
72 $style = ".wp-container-$id > * {"; |
186 $class_names[] = 'is-content-justification-' . sanitize_title( $block['attrs']['layout']['justifyContent'] ); |
73 $style .= 'max-width: ' . esc_html( $all_max_width_value ) . ';'; |
187 } |
74 $style .= 'margin-left: auto !important;'; |
188 |
75 $style .= 'margin-right: auto !important;'; |
189 if ( ! empty( $block['attrs']['layout']['flexWrap'] ) && 'nowrap' === $block['attrs']['layout']['flexWrap'] ) { |
76 $style .= '}'; |
190 $class_names[] = 'is-nowrap'; |
77 |
191 } |
78 $style .= ".wp-container-$id > .alignwide { max-width: " . esc_html( $wide_max_width_value ) . ';}'; |
192 |
79 |
193 $gap_value = _wp_array_get( $block, array( 'attrs', 'style', 'spacing', 'blockGap' ) ); |
80 $style .= ".wp-container-$id .alignfull { max-width: none; }"; |
194 // Skip if gap value contains unsupported characters. |
81 } |
195 // Regex for CSS value borrowed from `safecss_filter_attr`, and used here |
82 |
196 // because we only want to match against the value, not the CSS attribute. |
83 $style .= ".wp-container-$id .alignleft { float: left; margin-right: 2em; }"; |
197 if ( is_array( $gap_value ) ) { |
84 $style .= ".wp-container-$id .alignright { float: right; margin-left: 2em; }"; |
198 foreach ( $gap_value as $key => $value ) { |
85 |
199 $gap_value[ $key ] = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value; |
|
200 } |
|
201 } else { |
|
202 $gap_value = $gap_value && preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ? null : $gap_value; |
|
203 } |
|
204 |
|
205 $fallback_gap_value = _wp_array_get( $block_type->supports, array( 'spacing', 'blockGap', '__experimentalDefault' ), '0.5em' ); |
|
206 |
|
207 // If a block's block.json skips serialization for spacing or spacing.blockGap, |
|
208 // don't apply the user-defined value to the styles. |
|
209 $should_skip_gap_serialization = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'blockGap' ); |
|
210 $style = wp_get_layout_style( ".$container_class", $used_layout, $has_block_gap_support, $gap_value, $should_skip_gap_serialization, $fallback_gap_value ); |
86 // This assumes the hook only applies to blocks with a single wrapper. |
211 // This assumes the hook only applies to blocks with a single wrapper. |
87 // I think this is a reasonable limitation for that particular hook. |
212 // I think this is a reasonable limitation for that particular hook. |
88 $content = preg_replace( |
213 $content = preg_replace( |
89 '/' . preg_quote( 'class="', '/' ) . '/', |
214 '/' . preg_quote( 'class="', '/' ) . '/', |
90 'class="wp-container-' . $id . ' ', |
215 'class="' . esc_attr( implode( ' ', $class_names ) ) . ' ', |
91 $block_content, |
216 $block_content, |
92 1 |
217 1 |
93 ); |
218 ); |
94 |
219 |
95 return $content . '<style>' . $style . '</style>'; |
220 wp_enqueue_block_support_styles( $style ); |
|
221 |
|
222 return $content; |
96 } |
223 } |
97 |
224 |
98 // Register the block support. |
225 // Register the block support. |
99 WP_Block_Supports::get_instance()->register( |
226 WP_Block_Supports::get_instance()->register( |
100 'layout', |
227 'layout', |
112 * @since 5.8.0 |
239 * @since 5.8.0 |
113 * @access private |
240 * @access private |
114 * |
241 * |
115 * @param string $block_content Rendered block content. |
242 * @param string $block_content Rendered block content. |
116 * @param array $block Block object. |
243 * @param array $block Block object. |
117 * |
|
118 * @return string Filtered block content. |
244 * @return string Filtered block content. |
119 */ |
245 */ |
120 function wp_restore_group_inner_container( $block_content, $block ) { |
246 function wp_restore_group_inner_container( $block_content, $block ) { |
121 $group_with_inner_container_regex = '/(^\s*<div\b[^>]*wp-block-group(\s|")[^>]*>)(\s*<div\b[^>]*wp-block-group__inner-container(\s|")[^>]*>)((.|\S|\s)*)/'; |
247 $tag_name = isset( $block['attrs']['tagName'] ) ? $block['attrs']['tagName'] : 'div'; |
|
248 $group_with_inner_container_regex = sprintf( |
|
249 '/(^\s*<%1$s\b[^>]*wp-block-group(\s|")[^>]*>)(\s*<div\b[^>]*wp-block-group__inner-container(\s|")[^>]*>)((.|\S|\s)*)/U', |
|
250 preg_quote( $tag_name, '/' ) |
|
251 ); |
122 |
252 |
123 if ( |
253 if ( |
124 'core/group' !== $block['blockName'] || |
|
125 WP_Theme_JSON_Resolver::theme_has_support() || |
254 WP_Theme_JSON_Resolver::theme_has_support() || |
126 1 === preg_match( $group_with_inner_container_regex, $block_content ) |
255 1 === preg_match( $group_with_inner_container_regex, $block_content ) || |
|
256 ( isset( $block['attrs']['layout']['type'] ) && 'default' !== $block['attrs']['layout']['type'] ) |
127 ) { |
257 ) { |
128 return $block_content; |
258 return $block_content; |
129 } |
259 } |
130 |
260 |
131 $replace_regex = '/(^\s*<div\b[^>]*wp-block-group[^>]*>)(.*)(<\/div>\s*$)/ms'; |
261 $replace_regex = sprintf( |
|
262 '/(^\s*<%1$s\b[^>]*wp-block-group[^>]*>)(.*)(<\/%1$s>\s*$)/ms', |
|
263 preg_quote( $tag_name, '/' ) |
|
264 ); |
132 $updated_content = preg_replace_callback( |
265 $updated_content = preg_replace_callback( |
133 $replace_regex, |
266 $replace_regex, |
134 function( $matches ) { |
267 static function( $matches ) { |
135 return $matches[1] . '<div class="wp-block-group__inner-container">' . $matches[2] . '</div>' . $matches[3]; |
268 return $matches[1] . '<div class="wp-block-group__inner-container">' . $matches[2] . '</div>' . $matches[3]; |
136 }, |
269 }, |
137 $block_content |
270 $block_content |
138 ); |
271 ); |
139 return $updated_content; |
272 return $updated_content; |
140 } |
273 } |
141 |
274 |
142 add_filter( 'render_block', 'wp_restore_group_inner_container', 10, 2 ); |
275 add_filter( 'render_block_core/group', 'wp_restore_group_inner_container', 10, 2 ); |
|
276 |
|
277 /** |
|
278 * For themes without theme.json file, make sure |
|
279 * to restore the outer div for the aligned image block |
|
280 * to avoid breaking styles relying on that div. |
|
281 * |
|
282 * @since 6.0.0 |
|
283 * @access private |
|
284 * |
|
285 * @param string $block_content Rendered block content. |
|
286 * @param array $block Block object. |
|
287 * @return string Filtered block content. |
|
288 */ |
|
289 function wp_restore_image_outer_container( $block_content, $block ) { |
|
290 $image_with_align = " |
|
291 /# 1) everything up to the class attribute contents |
|
292 ( |
|
293 ^\s* |
|
294 <figure\b |
|
295 [^>]* |
|
296 \bclass= |
|
297 [\"'] |
|
298 ) |
|
299 # 2) the class attribute contents |
|
300 ( |
|
301 [^\"']* |
|
302 \bwp-block-image\b |
|
303 [^\"']* |
|
304 \b(?:alignleft|alignright|aligncenter)\b |
|
305 [^\"']* |
|
306 ) |
|
307 # 3) everything after the class attribute contents |
|
308 ( |
|
309 [\"'] |
|
310 [^>]* |
|
311 > |
|
312 .* |
|
313 <\/figure> |
|
314 )/iUx"; |
|
315 |
|
316 if ( |
|
317 WP_Theme_JSON_Resolver::theme_has_support() || |
|
318 0 === preg_match( $image_with_align, $block_content, $matches ) |
|
319 ) { |
|
320 return $block_content; |
|
321 } |
|
322 |
|
323 $wrapper_classnames = array( 'wp-block-image' ); |
|
324 |
|
325 // If the block has a classNames attribute these classnames need to be removed from the content and added back |
|
326 // to the new wrapper div also. |
|
327 if ( ! empty( $block['attrs']['className'] ) ) { |
|
328 $wrapper_classnames = array_merge( $wrapper_classnames, explode( ' ', $block['attrs']['className'] ) ); |
|
329 } |
|
330 $content_classnames = explode( ' ', $matches[2] ); |
|
331 $filtered_content_classnames = array_diff( $content_classnames, $wrapper_classnames ); |
|
332 |
|
333 return '<div class="' . implode( ' ', $wrapper_classnames ) . '">' . $matches[1] . implode( ' ', $filtered_content_classnames ) . $matches[3] . '</div>'; |
|
334 } |
|
335 |
|
336 add_filter( 'render_block_core/image', 'wp_restore_image_outer_container', 10, 2 ); |