PluginProbe
Gutenberg / 24.0.0
Gutenberg v24.0.0
24.1.0 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 All 404 releases
← All changes | build/scripts/block-library/gallery.php +498 -59 23.2.2 → 24.0.0 View file →
@@ -52,8 +52,337 @@
52 52
53 53 add_filter( 'render_block_context', 'gutenberg_block_core_gallery_render_context', 10, 2 );
54 54
55 55 /**
56 + * Returns the column gap value used for Gallery image width calculations.
57 + *
58 + * @since 7.1.0
59 + *
60 + * @param string|array|null $gap Gallery block gap value.
61 + * @param string $fallback_gap Fallback gap value.
62 + * @return string Gallery column gap value.
63 + */
64 +function gutenberg_block_core_gallery_get_column_gap_value( $gap, $fallback_gap ) {
65 + if ( is_array( $gap ) ) {
66 + $gap = $gap['left'] ?? $fallback_gap;
67 + }
68 +
69 + // Make sure $gap is a string to avoid PHP 8.1 deprecation error in preg_match() when the value is null.
70 + $gap = is_string( $gap ) ? $gap : '';
71 +
72 + // Skip if gap value contains unsupported characters.
73 + // Regex for CSS value borrowed from `safecss_filter_attr`, and used here
74 + // because we only want to match against the value, not the CSS attribute.
75 + $gap = $gap && preg_match( '%[\\\(&=}]|/\*%', $gap ) ? null : $gap;
76 +
77 + // Get spacing CSS variable from preset value if provided.
78 + if ( is_string( $gap ) && str_contains( $gap, 'var:preset|spacing|' ) ) {
79 + $index_to_splice = strrpos( $gap, '|' ) + 1;
80 + $slug = _wp_to_kebab_case( substr( $gap, $index_to_splice ) );
81 + $gap = "var(--wp--preset--spacing--$slug)";
82 + }
83 +
84 + $gap_column = ( null !== $gap && '' !== $gap ) ? $gap : $fallback_gap;
85 +
86 + // The unstable gallery gap calculation requires a real value (such as `0px`) and not `0`.
87 + return '0' === $gap_column ? '0px' : $gap_column;
88 +}
89 +
90 +/**
91 + * Returns Gallery-specific responsive Flex rules for a viewport.
92 + *
93 + * @since 7.1.0
94 + *
95 + * @param string $selector Gallery block selector.
96 + * @param mixed $viewport_style Viewport style data.
97 + * @param string $media_query Viewport media query.
98 + * @return array[] Gallery responsive Flex rules.
99 + */
100 +function gutenberg_block_core_gallery_get_responsive_flex_style_rules( $selector, $viewport_style, $media_query ) {
101 + if ( ! is_array( $viewport_style ) || ! is_string( $media_query ) ) {
102 + return array();
103 + }
104 +
105 + $rules = array();
106 + $gallery_selector = "{$selector}.wp-block-gallery.has-nested-images:where(.is-layout-flex)";
107 + $image_selector = "{$gallery_selector} figure.wp-block-image:not(#individual-image)";
108 + $columns = $viewport_style['columns'] ?? null;
109 +
110 + if ( is_int( $columns ) && $columns >= 1 && $columns <= 8 ) {
111 + $width = 1 === $columns
112 + ? '100%'
113 + : sprintf(
114 + 'calc((100%% - (var(--wp--style--unstable-gallery-gap, 16px) * %1$d)) / %2$d)',
115 + $columns - 1,
116 + $columns
117 + );
118 + $rules[] = array(
119 + 'selector' => $image_selector,
120 + 'declarations' => array( 'width' => "{$width} !important" ),
121 + 'rules_group' => $media_query,
122 + );
123 + }
124 +
125 + $image_crop = $viewport_style['imageCrop'] ?? null;
126 + if ( ! is_bool( $image_crop ) ) {
127 + return $rules;
128 + }
129 +
130 + $rules[] = array(
131 + 'selector' => $image_selector,
132 + 'declarations' => $image_crop
133 + ? array(
134 + 'align-self' => 'inherit !important',
135 + 'margin-bottom' => '0 !important',
136 + )
137 + : array(
138 + 'align-self' => 'auto !important',
139 + 'margin-top' => '0 !important',
140 + 'margin-bottom' => 'auto !important',
141 + ),
142 + 'rules_group' => $media_query,
143 + );
144 + $rules[] = array(
145 + 'selector' => "{$image_selector} > div:not(.components-drop-zone)",
146 + 'declarations' => array( 'display' => $image_crop ? 'flex !important' : 'block !important' ),
147 + 'rules_group' => $media_query,
148 + );
149 + $rules[] = array(
150 + 'selector' => "{$image_selector} > a",
151 + 'declarations' => array( 'display' => $image_crop ? 'flex !important' : 'inline-block !important' ),
152 + 'rules_group' => $media_query,
153 + );
154 + $rules[] = array(
155 + 'selector' => "{$image_selector} a,{$image_selector} img",
156 + 'declarations' => $image_crop
157 + ? array(
158 + 'width' => '100% !important',
159 + 'flex' => '1 0 0% !important',
160 + 'height' => '100% !important',
161 + 'object-fit' => 'cover !important',
162 + )
163 + : array(
164 + 'width' => 'auto !important',
165 + 'flex' => '0 1 auto !important',
166 + 'height' => 'auto !important',
167 + 'object-fit' => 'fill !important',
168 + ),
169 + 'rules_group' => $media_query,
170 + );
171 +
172 + return $rules;
173 +}
174 +
175 +/**
176 + * Resolves a Gallery block's `dynamicContent` to an ordered list of image
177 + * attachment IDs.
178 + *
179 + * The `source` key is the dispatch discriminator and `args` holds the source's
180 + * parameters. This `{ source, args }` shape mirrors the Block Bindings metadata
181 + * shape so dynamic mode can migrate to an `innerBlocks` binding with minimal
182 + * change. `core/attached-media` is a context-relative anchor (the post the gallery is
183 + * rendered within); future sources translate their REST-named `args` (`author`,
184 + * `categories`, `after`/`before`, `media_type`, etc.) into `WP_Query` arguments
185 + * here.
186 + *
187 + * @since 7.0.0
188 + *
189 + * @param array $source The gallery's `dynamicContent` attribute.
190 + * @param WP_Block $block The gallery block instance being rendered.
191 + * @return int[] Ordered list of image attachment IDs.
192 + */
193 +function gutenberg_block_core_gallery_resolve_dynamic_source( $source, $block ) {
194 + if ( ! is_array( $source ) ) {
195 + return array();
196 + }
197 +
198 + $source_name = $source['source'] ?? null;
199 + $args = isset( $source['args'] ) && is_array( $source['args'] ) ? $source['args'] : array();
200 +
201 + switch ( $source_name ) {
202 + case 'core/attached-media':
203 + // Prefer the post supplied via block context, falling back to the post
204 + // being rendered. The fallback is what lets a post-bound template (e.g.
205 + // `single`/`page`) resolve against the actual post at render time even
206 + // though the editor has no concrete post to preview — the editor gates
207 + // the dynamic-mode UI on that same context (see `use-dynamic-gallery.js`).
208 + $post_id = $block->context['postId'] ?? get_the_ID();
209 + if ( ! $post_id ) {
210 + return array();
211 + }
212 +
213 + // Map the camelCase `args` (block-attribute convention) to WP_Query
214 + // names, defaulting to the same order as the editor preview (see
215 + // `dynamic-source.js`). Only REST-supported orderby values are
216 + // allowed; `menu_order` is intentionally unsupported (it isn't a
217 + // valid media REST `orderby`).
218 + $orderby = $args['orderBy'] ?? 'date';
219 + if ( ! in_array( $orderby, array( 'date', 'title' ), true ) ) {
220 + $orderby = 'date';
221 + }
222 + $order = strtoupper( $args['order'] ?? 'desc' ) === 'ASC' ? 'ASC' : 'DESC';
223 +
224 + // Bound the number of resolved images until the gallery supports
225 + // pagination. Kept in sync with the editor query's `per_page` cap; a
226 + // case-insensitive grep for `max_images` finds both this and
227 + // `MAX_IMAGES` in `dynamic-source.js`.
228 + $max_images = 100;
229 +
230 + $query = new WP_Query(
231 + array(
232 + 'post_parent' => $post_id,
233 + 'post_type' => 'attachment',
234 + 'post_status' => 'inherit',
235 + 'post_mime_type' => 'image',
236 + 'orderby' => $orderby,
237 + 'order' => $order,
238 + 'posts_per_page' => $max_images,
239 + 'fields' => 'ids',
240 + 'no_found_rows' => true,
241 + )
242 + );
243 +
244 + return array_map( 'intval', $query->posts );
245 + }
246 +
247 + // Unknown or not-yet-implemented source type.
248 + return array();
249 +}
250 +
251 +/**
252 + * Builds the link-related image block attributes for a dynamically rendered
253 + * gallery image, mapping the gallery-wide `linkTo` setting onto a single image.
254 + *
255 + * Mirrors the editor's `getHrefAndDestination()` (see `gallery/utils.js`).
256 + *
257 + * @since 7.0.0
258 + *
259 + * @param int $attachment_id The image attachment ID.
260 + * @param array $attributes The gallery block attributes.
261 + * @return array Partial image block attributes (`href`, `linkDestination`,
262 + * `linkTarget`, `rel`, `lightbox`).
263 + */
264 +function gutenberg_block_core_gallery_dynamic_image_link_attributes( $attachment_id, $attributes ) {
265 + $link_to = $attributes['linkTo'] ?? 'none';
266 + $attrs = array();
267 +
268 + switch ( $link_to ) {
269 + // Gutenberg uses 'media'/'attachment'; WP Core uses 'file'/'post'.
270 + case 'media':
271 + case 'file':
272 + $attrs['href'] = wp_get_attachment_url( $attachment_id );
273 + $attrs['linkDestination'] = 'media';
274 + break;
275 + case 'attachment':
276 + case 'post':
277 + $attrs['href'] = get_attachment_link( $attachment_id );
278 + $attrs['linkDestination'] = 'attachment';
279 + break;
280 + case 'lightbox':
281 + $attrs['linkDestination'] = 'none';
282 + $attrs['lightbox'] = array( 'enabled' => true );
283 + break;
284 + }
285 +
286 + if ( ! empty( $attrs['href'] ) && '_blank' === ( $attributes['linkTarget'] ?? '' ) ) {
287 + $attrs['linkTarget'] = '_blank';
288 + $attrs['rel'] = 'noopener';
289 + }
290 +
291 + return $attrs;
292 +}
293 +
294 +/**
295 + * Renders a single `core/image` block for a Gallery block running in dynamic
296 + * mode, applying the gallery-wide settings that affect how an image renders.
297 + *
298 + * The image markup is generated here (via `wp_get_attachment_image()`) and
299 + * rendered through a real `core/image` block instance so that the image block's
300 + * own render callback and lightbox behavior run, and so the gallery's existing
301 + * lightbox/interactivity post-processing can pick it up.
302 + *
303 + * @since 7.0.0
304 + *
305 + * @param int $attachment_id The image attachment ID.
306 + * @param array $attributes The gallery block attributes.
307 + * @param array $context Context to expose to the inner image block.
308 + * @return string The rendered image block HTML, or an empty string on failure.
309 + */
310 +function gutenberg_block_core_gallery_render_dynamic_image( $attachment_id, $attributes, $context ) {
311 + $size_slug = $attributes['sizeSlug'] ?? 'large';
312 + $aspect_ratio = $attributes['aspectRatio'] ?? 'auto';
313 +
314 + $img_attr = array( 'class' => 'wp-image-' . $attachment_id );
315 + if ( $aspect_ratio && 'auto' !== $aspect_ratio ) {
316 + // Run the aspect ratio through the same sanitization used for every other
317 + // block inline style, so an unsafe value can't break out of the style
318 + // attribute or inject additional markup.
319 + $img_attr['style'] = safecss_filter_attr(
320 + sprintf( 'aspect-ratio:%s;object-fit:cover;', $aspect_ratio )
321 + );
322 + }
323 +
324 + $image_markup = wp_get_attachment_image( $attachment_id, $size_slug, false, $img_attr );
325 + if ( ! $image_markup ) {
326 + return '';
327 + }
328 +
329 + $image_attributes = array_merge(
330 + array(
331 + 'id' => $attachment_id,
332 + 'data-id' => (string) $attachment_id,
333 + 'sizeSlug' => $size_slug,
334 + ),
335 + gutenberg_block_core_gallery_dynamic_image_link_attributes( $attachment_id, $attributes )
336 + );
337 +
338 + if ( $aspect_ratio && 'auto' !== $aspect_ratio ) {
339 + $image_attributes['aspectRatio'] = $aspect_ratio;
340 + $image_attributes['scale'] = 'cover';
341 + }
342 +
343 + // Wrap in a link when the gallery links images somewhere.
344 + if ( ! empty( $image_attributes['href'] ) ) {
345 + $image_markup = sprintf(
346 + '<a href="%1$s"%2$s%3$s>%4$s</a>',
347 + esc_url( $image_attributes['href'] ),
348 + isset( $image_attributes['linkTarget'] ) ? ' target="' . esc_attr( $image_attributes['linkTarget'] ) . '"' : '',
349 + isset( $image_attributes['rel'] ) ? ' rel="' . esc_attr( $image_attributes['rel'] ) . '"' : '',
350 + $image_markup
351 + );
352 + }
353 +
354 + // Use the raw caption (`post_excerpt`) so the frontend mirrors the editor
355 + // preview, which builds the caption from the REST `caption.raw` value. Gap:
356 + // the REST API exposes no caption run through `wp_get_attachment_caption`, so
357 + // that filter isn't applied here either.
358 + $attachment = get_post( $attachment_id );
359 + $caption = $attachment ? $attachment->post_excerpt : '';
360 + if ( '' !== $caption ) {
361 + $image_markup .= sprintf(
362 + '<figcaption class="wp-element-caption">%s</figcaption>',
363 + wp_kses_post( $caption )
364 + );
365 + }
366 +
367 + $figure = sprintf(
368 + '<figure class="wp-block-image size-%1$s">%2$s</figure>',
369 + esc_attr( $size_slug ),
370 + $image_markup
371 + );
372 +
373 + $image_block = array(
374 + 'blockName' => 'core/image',
375 + 'attrs' => $image_attributes,
376 + 'innerBlocks' => array(),
377 + 'innerHTML' => $figure,
378 + 'innerContent' => array( $figure ),
379 + );
380 +
381 + return ( new WP_Block( $image_block, $context ) )->render();
382 +}
383 +
384 +/**
56 385 * Renders the `core/gallery` block on the server.
57 386 *
58 387 * @since 6.0.0
59 388 *
@@ -62,82 +391,192 @@
62 391 * @param array $block The block instance being rendered.
63 392 * @return string The content of the block being rendered.
64 393 */
65 394 function gutenberg_block_core_gallery_render( $attributes, $content, $block ) {
66 - // Adds a style tag for the --wp--style--unstable-gallery-gap var.
67 - // The Gallery block needs to recalculate Image block width based on
68 - // the current gap setting in order to maintain the number of flex columns
69 - // so a css var is added to allow this.
395 + static $global_styles = null;
70 396
71 - $gap = $attributes['style']['spacing']['blockGap'] ?? null;
72 - // Skip if gap value contains unsupported characters.
73 - // Regex for CSS value borrowed from `safecss_filter_attr`, and used here
74 - // because we only want to match against the value, not the CSS attribute.
75 - if ( is_array( $gap ) ) {
76 - foreach ( $gap as $key => $value ) {
77 - // Make sure $value is a string to avoid PHP 8.1 deprecation error in preg_match() when the value is null.
78 - $value = is_string( $value ) ? $value : '';
79 - $value = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value;
397 + // Gallery blocks created before layout variations existed do not have an
398 + // explicit layout attribute. Missing and malformed layout data therefore
399 + // falls back to Flex so existing galleries retain their current appearance.
400 + $layout = is_array( $attributes['layout'] ?? null ) ? $attributes['layout'] : array();
401 + $layout_type = $layout['type'] ?? null;
402 + $is_flex_layout = ! is_string( $layout_type ) || '' === $layout_type || 'flex' === $layout_type;
80 403
81 - // Get spacing CSS variable from preset value if provided.
82 - if ( is_string( $value ) && str_contains( $value, 'var:preset|spacing|' ) ) {
83 - $index_to_splice = strrpos( $value, '|' ) + 1;
84 - $slug = _wp_to_kebab_case( substr( $value, $index_to_splice ) );
85 - $value = "var(--wp--preset--spacing--$slug)";
86 - }
404 + // In dynamic mode the gallery's images are resolved at render time instead of
405 + // being authored as inner blocks, so `save.jsx` persists at most the
406 + // gallery-level caption — a bare `<figcaption>`, or nothing when there is no
407 + // caption. Resolve the configured source to a list of attachments, render an
408 + // image block for each, and build the gallery `<figure>` wrapper from scratch.
409 + // The gap/randomOrder/lightbox post-processing below then runs over the
410 + // constructed markup unchanged.
411 + if ( ! empty( $attributes['dynamicContent'] ) ) {
412 + $attachment_ids = gutenberg_block_core_gallery_resolve_dynamic_source( $attributes['dynamicContent'], $block );
87 413
88 - $gap[ $key ] = $value;
414 + // Nothing resolved — no attachments, or an unrecognized source. Render
415 + // nothing rather than an empty gallery wrapper; a saved caption is
416 + // meaningless without images, so it is intentionally dropped too.
417 + if ( empty( $attachment_ids ) ) {
418 + return '';
89 419 }
90 - } else {
91 - // Make sure $gap is a string to avoid PHP 8.1 deprecation error in preg_match() when the value is null.
92 - $gap = is_string( $gap ) ? $gap : '';
93 - $gap = $gap && preg_match( '%[\\\(&=}]|/\*%', $gap ) ? null : $gap;
94 420
95 - // Get spacing CSS variable from preset value if provided.
96 - if ( is_string( $gap ) && str_contains( $gap, 'var:preset|spacing|' ) ) {
97 - $index_to_splice = strrpos( $gap, '|' ) + 1;
98 - $slug = _wp_to_kebab_case( substr( $gap, $index_to_splice ) );
99 - $gap = "var(--wp--preset--spacing--$slug)";
421 + // The source query only fetched IDs (`fields => ids`), which skips
422 + // WP_Query's cache priming. Each image rendered below reads the
423 + // attachment post and its meta (via `wp_get_attachment_image()`,
424 + // `get_post()`, etc.), so warm the post and meta caches in a single pair
425 + // of queries up front instead of paying ~two queries per attachment.
426 + // Term cache is left cold: the render path doesn't read attachment terms.
427 + if ( count( $attachment_ids ) > 1 ) {
428 + _prime_post_caches( $attachment_ids, false, true );
100 429 }
430 +
431 + // Expose the gallery's provided context (plus galleryId/postId/postType)
432 + // to each image block, since these images are rendered outside the
433 + // gallery's real inner-block tree.
434 + $image_context = array_merge(
435 + is_array( $block->context ) ? $block->context : array(),
436 + array(
437 + 'allowResize' => $attributes['allowResize'] ?? false,
438 + 'imageCrop' => $attributes['imageCrop'] ?? true,
439 + 'fixedHeight' => $attributes['fixedHeight'] ?? true,
440 + 'navigationButtonType' => $attributes['navigationButtonType'] ?? 'icon',
441 + )
442 + );
443 +
444 + $images_markup = '';
445 + foreach ( $attachment_ids as $attachment_id ) {
446 + $images_markup .= gutenberg_block_core_gallery_render_dynamic_image( $attachment_id, $attributes, $image_context );
447 + }
448 +
449 + // Build the wrapper rather than parsing/splicing saved markup.
450 + // `get_block_wrapper_attributes()` supplies the block-support
451 + // classes/styles (align, color, border, spacing, anchor id); the layout
452 + // render filter adds the active layout classes downstream — the same way a
453 + // static gallery's wrapper is composed (`useBlockProps.save()` plus that
454 + // filter). Only the gallery-specific classes are added explicitly, and
455 + // they mirror `save.jsx` (kept in sync deliberately — see that file).
456 + $gallery_classes = 'wp-block-gallery has-nested-images';
457 + if ( $is_flex_layout ) {
458 + $gallery_classes .= isset( $attributes['columns'] )
459 + ? ' columns-' . (int) $attributes['columns']
460 + : ' columns-default';
461 + if ( $attributes['imageCrop'] ?? true ) {
462 + $gallery_classes .= ' is-cropped';
463 + }
464 + }
465 + $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $gallery_classes ) );
466 +
467 + // In dynamic mode `save.jsx` persists only the gallery-level caption, so
468 + // `$content` is the saved `<figcaption>` (or empty). Append it after the
469 + // resolved images — matching the static gallery's `{images}{caption}`
470 + // order — without parsing it.
471 + $content = sprintf( '<figure %s>%s%s</figure>', $wrapper_attributes, $images_markup, $content );
101 472 }
102 473
103 - $unique_gallery_classname = wp_unique_id( 'wp-block-gallery-' );
104 - $processed_content = new WP_HTML_Tag_Processor( $content );
474 + $processed_content = new WP_HTML_Tag_Processor( $content );
105 475 $processed_content->next_tag();
106 - $processed_content->add_class( $unique_gallery_classname );
107 476
108 - // --gallery-block--gutter-size is deprecated. --wp--style--gallery-gap-default should be used by themes that want to set a default
109 - // gap on the gallery.
110 - $fallback_gap = 'var( --wp--style--gallery-gap-default, var( --gallery-block--gutter-size, var( --wp--style--block-gap, 0.5em ) ) )';
111 - $gap_value = $gap ? $gap : $fallback_gap;
112 - $gap_column = $gap_value;
477 + if ( $is_flex_layout ) {
478 + // Add a style tag for the --wp--style--unstable-gallery-gap var. The
479 + // Gallery's custom Flex layout recalculates Image block widths based on
480 + // the current gap so it can maintain the selected number of columns.
481 + $style_attr = is_array( $attributes['style'] ?? null )
482 + ? $attributes['style']
483 + : array();
484 + if (
485 + defined( 'IS_GUTENBERG_PLUGIN' ) &&
486 + IS_GUTENBERG_PLUGIN &&
487 + function_exists( 'gutenberg_resolve_style_state_aliases' )
488 + ) {
489 + $style_attr = gutenberg_resolve_style_state_aliases( $style_attr, 'core/gallery' );
490 + }
113 491
114 - if ( is_array( $gap_value ) ) {
115 - $gap_row = $gap_value['top'] ?? $fallback_gap;
116 - $gap_column = $gap_value['left'] ?? $fallback_gap;
117 - $gap_value = $gap_row === $gap_column ? $gap_row : $gap_row . ' ' . $gap_column;
118 - }
492 + $unique_gallery_classname = wp_unique_id( 'wp-block-gallery-' );
493 + $processed_content->add_class( $unique_gallery_classname );
119 494
120 - // The unstable gallery gap calculation requires a real value (such as `0px`) and not `0`.
121 - if ( '0' === $gap_column ) {
122 - $gap_column = '0px';
123 - }
495 + // --gallery-block--gutter-size is deprecated. --wp--style--gallery-gap-default should be used by themes that want to set a default
496 + // gap on the gallery.
497 + $fallback_gap = 'var( --wp--style--gallery-gap-default, var( --gallery-block--gutter-size, var( --wp--style--block-gap, 0.5em ) ) )';
124 498
125 - // Set the CSS variable to the column value, and the `gap` property to the combined gap value.
126 - $gallery_styles = array(
127 - array(
128 - 'selector' => ".wp-block-gallery.{$unique_gallery_classname}",
129 - 'declarations' => array(
130 - '--wp--style--unstable-gallery-gap' => $gap_column,
131 - 'gap' => $gap_value,
499 + if ( null === $global_styles ) {
500 + $global_styles = function_exists( 'gutenberg_get_global_styles' ) ? gutenberg_get_global_styles() : array();
501 + }
502 +
503 + $global_gallery_styles = $global_styles['blocks']['core/gallery'] ?? array();
504 + $global_gallery_gap = $global_gallery_styles['spacing']['blockGap'] ?? $fallback_gap;
505 + $has_block_gap = is_array( $style_attr['spacing'] ?? null ) && array_key_exists( 'blockGap', $style_attr['spacing'] );
506 + // Prefer the block's own gap value, then Gallery global styles. Missing
507 + // values fall back to the Gallery blockGap default.
508 + $block_gap = $has_block_gap
509 + ? $style_attr['spacing']['blockGap']
510 + : $global_gallery_gap;
511 + $gap_column = gutenberg_block_core_gallery_get_column_gap_value( $block_gap, $fallback_gap );
512 +
513 + // Set the CSS variable to the column value for Gallery's flex width calculations.
514 + $gallery_styles = array(
515 + array(
516 + 'selector' => ".wp-block-gallery.{$unique_gallery_classname}",
517 + 'declarations' => array(
518 + '--wp--style--unstable-gallery-gap' => $gap_column,
519 + ),
132 520 ),
133 - ),
134 - );
521 + );
135 522
136 - gutenberg_style_engine_get_stylesheet_from_css_rules(
137 - $gallery_styles,
138 - array( 'context' => 'block-supports' )
139 - );
523 + $global_settings = gutenberg_get_global_settings();
524 + $viewport_settings = $global_settings['viewport'] ?? null;
525 + $responsive_media_queries = array();
526 + foreach ( array( 'WP_Theme_JSON_Gutenberg', 'WP_Theme_JSON' ) as $theme_json_class_name ) {
527 + if ( method_exists( $theme_json_class_name, 'get_viewport_media_queries' ) ) {
528 + $responsive_media_queries = $theme_json_class_name::get_viewport_media_queries( $viewport_settings );
529 + break;
530 + }
531 + }
532 +
533 + foreach ( $responsive_media_queries as $breakpoint => $media_query ) {
534 + $viewport_style = $style_attr[ $breakpoint ] ?? null;
535 + $has_viewport_block_gap = is_array( $viewport_style ) &&
536 + is_array( $viewport_style['spacing'] ?? null ) &&
537 + array_key_exists( 'blockGap', $viewport_style['spacing'] );
538 + $has_global_viewport_block_gap = is_array( $global_gallery_styles[ $breakpoint ]['spacing'] ?? null ) &&
539 + array_key_exists( 'blockGap', $global_gallery_styles[ $breakpoint ]['spacing'] );
540 +
541 + // Viewport-specific block values win. Gallery global viewport values
542 + // only apply when the block has no base gap, so they do not override an instance value.
543 + if ( $has_viewport_block_gap ) {
544 + $viewport_gap = $viewport_style['spacing']['blockGap'];
545 + } elseif ( ! $has_block_gap && $has_global_viewport_block_gap ) {
546 + $viewport_gap = $global_gallery_styles[ $breakpoint ]['spacing']['blockGap'];
547 + } else {
548 + $viewport_gap = null;
549 + }
550 +
551 + if ( null !== $viewport_gap ) {
552 + $gallery_styles[] = array(
553 + 'selector' => ".wp-block-gallery.{$unique_gallery_classname}",
554 + 'declarations' => array(
555 + '--wp--style--unstable-gallery-gap' => gutenberg_block_core_gallery_get_column_gap_value(
556 + $viewport_gap,
557 + $fallback_gap
558 + ),
559 + ),
560 + 'rules_group' => $media_query,
561 + );
562 + }
563 +
564 + $gallery_styles = array_merge(
565 + $gallery_styles,
566 + gutenberg_block_core_gallery_get_responsive_flex_style_rules(
567 + ".{$unique_gallery_classname}",
568 + $viewport_style,
569 + $media_query
570 + )
571 + );
572 + }
573 +
574 + gutenberg_style_engine_get_stylesheet_from_css_rules(
575 + $gallery_styles,
576 + array( 'context' => 'block-supports' )
577 + );
578 + }
140 579
141 580 // The WP_HTML_Tag_Processor class calls get_updated_html() internally
142 581 // when the instance is treated as a string, but here we explicitly
143 582 // convert it to a string.