diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md
index 6a8f3255297c46..f3b647e14c11cf 100644
--- a/docs/reference-guides/core-blocks.md
+++ b/docs/reference-guides/core-blocks.md
@@ -342,7 +342,7 @@ Display multiple images in a rich gallery. ([Source](https://github.com/WordPres
- **Category:** media
- **Allowed Blocks:** core/image
- **Supports:** align, anchor, color (background, gradients, ~~text~~), interactivity (clientNavigation), layout (default, ~~allowEditing~~, ~~allowInheriting~~, ~~allowSwitching~~), spacing (blockGap, margin, padding), units (em, px, rem, vh, vw), ~~html~~
-- **Attributes:** allowResize, caption, columns, fixedHeight, ids, imageCrop, images, linkTarget, linkTo, randomOrder, shortCodeTransforms, sizeSlug
+- **Attributes:** allowResize, caption, columns, fixedHeight, ids, imageCrop, images, lightBoxNavigation, linkTarget, linkTo, randomOrder, shortCodeTransforms, sizeSlug
## Group
diff --git a/packages/block-library/src/gallery/block.json b/packages/block-library/src/gallery/block.json
index 6a2129ec1e056f..f411b61c706cf3 100644
--- a/packages/block-library/src/gallery/block.json
+++ b/packages/block-library/src/gallery/block.json
@@ -4,6 +4,7 @@
"name": "core/gallery",
"title": "Gallery",
"category": "media",
+ "usesContext": [ "galleryId" ],
"allowedBlocks": [ "core/image" ],
"description": "Display multiple images in a rich gallery.",
"keywords": [ "images", "photos" ],
@@ -102,6 +103,10 @@
"allowResize": {
"type": "boolean",
"default": false
+ },
+ "lightBoxNavigation": {
+ "type": "boolean",
+ "default": true
}
},
"providesContext": {
diff --git a/packages/block-library/src/gallery/edit.js b/packages/block-library/src/gallery/edit.js
index 1936c02c468189..e253947dfb7946 100644
--- a/packages/block-library/src/gallery/edit.js
+++ b/packages/block-library/src/gallery/edit.js
@@ -125,8 +125,15 @@ export default function GalleryEdit( props ) {
)
: LINK_OPTIONS;
- const { columns, imageCrop, randomOrder, linkTarget, linkTo, sizeSlug } =
- attributes;
+ const {
+ columns,
+ imageCrop,
+ randomOrder,
+ linkTarget,
+ linkTo,
+ sizeSlug,
+ lightBoxNavigation,
+ } = attributes;
const {
__unstableMarkNextChangeAsNotPersistent,
@@ -442,6 +449,12 @@ export default function GalleryEdit( props ) {
} );
}
+ function toggleLightboxNavigation() {
+ setAttributes( {
+ lightBoxNavigation: ! attributes.lightBoxNavigation,
+ } );
+ }
+
function updateImagesSize( newSizeSlug ) {
setAttributes( { sizeSlug: newSizeSlug } );
const changedAttributes = {};
@@ -623,6 +636,15 @@ export default function GalleryEdit( props ) {
onChange={ toggleOpenInNewTab }
/>
) }
+
{ Platform.isWeb && ! imageSizeOptions && hasImageIds && (
'block-supports',
- )
+ array( 'context' => 'block-supports' )
);
+ if ( $attributes['lightBoxNavigation'] ) {
+ // Gets all image IDs from the state that match this gallery's ID.
+ $state = wp_interactivity_state( 'core/image' );
+ $gallery_id = $block->context['galleryId'] ?? null;
+ $image_ids = array();
+ if ( isset( $gallery_id ) && isset( $state['metadata'] ) ) {
+ foreach ( $state['metadata'] as $image_id => $metadata ) {
+ if ( isset( $metadata['galleryId'] ) && $metadata['galleryId'] === $gallery_id ) {
+ $image_ids[] = $image_id;
+ }
+ }
+ }
+
+ $processed_content->set_attribute( 'data-wp-interactive', 'core/gallery' );
+ $processed_content->set_attribute(
+ 'data-wp-context',
+ wp_json_encode(
+ array( 'galleryId' => $gallery_id ),
+ JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP
+ )
+ );
+
+ // Populates the aria label for each image in the gallery.
+ if ( ! empty( $image_ids ) ) {
+ if ( 1 <= count( $image_ids ) ) {
+ for ( $i = 0; $i < count( $image_ids ); $i++ ) {
+ $image_id = $image_ids[ $i ];
+ $alt = $state['metadata'][ $image_id ]['alt'];
+ wp_interactivity_state(
+ 'core/image',
+ array(
+ 'metadata' => array(
+ $image_id => array(
+ 'customAriaLabel' => empty( $alt )
+ /* translators: %1$s: current image index, %2$s: total number of images */
+ ? sprintf( __( 'Enlarged image %1$s of %2$s' ), $i + 1, count( $image_ids ) )
+ /* translators: %1$s: current image index, %2$s: total number of images, %3$s: Image alt text */
+ : sprintf( __( 'Enlarged image %1$s of %2$s: %3$s' ), $i + 1, count( $image_ids ), $alt ),
+ ),
+ ),
+ )
+ );
+ }
+ }
+ }
+ }
+
// The WP_HTML_Tag_Processor class calls get_updated_html() internally
// when the instance is treated as a string, but here we explicitly
// convert it to a string.
@@ -166,6 +230,7 @@ static function () use ( $image_blocks, &$i ) {
return $content;
}
+
/**
* Registers the `core/gallery` block on server.
*
diff --git a/packages/block-library/src/image/block.json b/packages/block-library/src/image/block.json
index 26835df9e856cd..b5d12b048f0112 100644
--- a/packages/block-library/src/image/block.json
+++ b/packages/block-library/src/image/block.json
@@ -10,7 +10,8 @@
"fixedHeight",
"postId",
"postType",
- "queryId"
+ "queryId",
+ "galleryId"
],
"description": "Insert an image to make a visual statement.",
"keywords": [ "img", "photo", "picture" ],
diff --git a/packages/block-library/src/image/index.php b/packages/block-library/src/image/index.php
index 697f67a927fc87..2c8b4ef7012c92 100644
--- a/packages/block-library/src/image/index.php
+++ b/packages/block-library/src/image/index.php
@@ -80,7 +80,7 @@ function render_block_core_image( $attributes, $content, $block ) {
* if the way the blocks are rendered changes, or if a new kind of filter is
* introduced.
*/
- add_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15, 2 );
+ add_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15, 3 );
} else {
/*
* Remove the filter if previously added by other Image blocks.
@@ -130,18 +130,20 @@ function block_core_image_get_lightbox_settings( $block ) {
*
* @since 6.4.0
*
- * @param string $block_content Rendered block content.
- * @param array $block Block object.
+ * @param string $block_content Rendered block content.
+ * @param array $block Block object.
+ * @param array $block_instance Block instance.
*
* @return string Filtered block content.
*/
-function block_core_image_render_lightbox( $block_content, $block ) {
+function block_core_image_render_lightbox( $block_content, $block, $block_instance ) {
/*
* If there's no IMG tag in the block then return the given block content
* as-is. There's nothing that this code can knowingly modify to add the
* lightbox behavior.
*/
$p = new WP_HTML_Tag_Processor( $block_content );
+
if ( $p->next_tag( 'figure' ) ) {
$p->set_bookmark( 'figure' );
}
@@ -149,14 +151,22 @@ function block_core_image_render_lightbox( $block_content, $block ) {
return $block_content;
}
- $alt = $p->get_attribute( 'alt' );
- $img_uploaded_src = $p->get_attribute( 'src' );
- $img_class_names = $p->get_attribute( 'class' );
- $img_styles = $p->get_attribute( 'style' );
- $img_width = 'none';
- $img_height = 'none';
- $aria_label = __( 'Enlarge' );
- $dialog_aria_label = __( 'Enlarged image' );
+ $alt = $p->get_attribute( 'alt' );
+ $img_uploaded_src = $p->get_attribute( 'src' );
+ $img_class_names = $p->get_attribute( 'class' );
+ $img_styles = $p->get_attribute( 'style' );
+ $img_width = 'none';
+ $img_height = 'none';
+
+ wp_interactivity_config(
+ 'core/image',
+ array( 'defaultAriaLabel' => __( 'Enlarged image' ) )
+ );
+
+ if ( $alt ) {
+ /* translators: %s: Image alt text. */
+ $custom_aria_label = sprintf( __( 'Enlarged image: %s' ), $alt );
+ }
if ( isset( $block['attrs']['id'] ) ) {
$img_uploaded_src = wp_get_attachment_url( $block['attrs']['id'] );
@@ -172,7 +182,6 @@ function block_core_image_render_lightbox( $block_content, $block ) {
// Create unique id and set the image metadata in the state.
$unique_image_id = uniqid();
-
wp_interactivity_state(
'core/image',
array(
@@ -186,8 +195,9 @@ function block_core_image_render_lightbox( $block_content, $block ) {
'targetWidth' => $img_width,
'targetHeight' => $img_height,
'scaleAttr' => $block['attrs']['scale'] ?? false,
- 'ariaLabel' => $dialog_aria_label,
'alt' => $alt,
+ 'galleryId' => $block_instance->context['galleryId'] ?? null,
+ 'customAriaLabel' => $custom_aria_label ?? null,
),
),
)
@@ -198,9 +208,7 @@ function block_core_image_render_lightbox( $block_content, $block ) {
$p->set_attribute(
'data-wp-context',
wp_json_encode(
- array(
- 'imageId' => $unique_image_id,
- ),
+ array( 'imageId' => $unique_image_id ),
JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP
)
);
@@ -229,11 +237,11 @@ function block_core_image_render_lightbox( $block_content, $block ) {
class="lightbox-trigger"
type="button"
aria-haspopup="dialog"
- aria-label="' . esc_attr( $aria_label ) . '"
+ aria-label="' . esc_attr( __( 'Enlarge' ) ) . '"
data-wp-init="callbacks.initTriggerButton"
data-wp-on-async--click="actions.showLightbox"
- data-wp-style--right="state.imageButtonRight"
- data-wp-style--top="state.imageButtonTop"
+ data-wp-style--right="state.thisImage.buttonRight"
+ data-wp-style--top="state.thisImage.buttonTop"
>