diff --git a/packages/block-library/src/gallery/index.php b/packages/block-library/src/gallery/index.php
index 5af6be16d39a2b..d0ac13d40391a7 100644
--- a/packages/block-library/src/gallery/index.php
+++ b/packages/block-library/src/gallery/index.php
@@ -43,7 +43,7 @@ function block_core_gallery_data_id_backcompatibility( $parsed_block ) {
* @param string $content Content of the block being rendered.
* @return string The content of the block being rendered.
*/
-function block_core_gallery_render( $attributes, $content ) {
+function block_core_gallery_render( $attributes, $content, $block ) {
// Adds a style tag for the --wp--style--unstable-gallery-gap var.
// The Gallery block needs to recalculate Image block width based on
// the current gap setting in order to maintain the number of flex columns
@@ -121,6 +121,35 @@ function block_core_gallery_render( $attributes, $content ) {
)
);
+ $lightbox_settings = block_core_image_get_lightbox_settings( $block->parsed_block );
+
+ if (
+ isset( $lightbox_settings ) &&
+ // 'none' === $link_destination &&
+ isset( $lightbox_settings['enabled'] ) &&
+ true === $lightbox_settings['enabled']
+ ) {
+ $processed_content->set_attribute( 'data-wp-interactive', 'core/gallery' );
+ $processed_content->set_attribute( 'data-wp-context', '{"lightbox": true, "images": []}' );
+ $processed_content->set_attribute( 'data-wp-init', 'callbacks.init' );
+ $processed_content->set_attribute( 'data-wp-on-async--load', 'callbacks.init' );
+
+ $suffix = wp_scripts_get_suffix();
+ if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) {
+ $module_url = gutenberg_url( '/build/interactivity/gallery.min.js' );
+ }
+
+ wp_register_script_module(
+ '@wordpress/block-library/gallery',
+ isset( $module_url ) ? $module_url : includes_url( "blocks/gallery/view{$suffix}.js" ),
+ array( '@wordpress/interactivity' ),
+ defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' )
+ );
+
+ wp_enqueue_script_module( '@wordpress/block-library/gallery' );
+
+ }
+
// 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 +195,33 @@ static function () use ( $image_blocks, &$i ) {
return $content;
}
+
+// NOTE: this setting isn't required for the gallery block.
+// since lightbox implementation is done in the image block, it can totally be removed.
+function block_core_gallery_get_lightbox_settings( $block ) {
+ // Gets the lightbox setting from the block attributes.
+ if ( isset( $block['attrs']['lightbox'] ) ) {
+ $lightbox_settings = $block['attrs']['lightbox'];
+ }
+
+ if ( ! isset( $lightbox_settings ) ) {
+ // TODO: change it to gallery block name.
+ $lightbox_settings = wp_get_global_settings( array( 'lightbox' ), array( 'block_name' => 'core/image' ) );
+
+ // If not present in global settings, check the top-level global settings.
+ //
+ // NOTE: If no block-level settings are found, the previous call to
+ // `wp_get_global_settings` will return the whole `theme.json` structure in
+ // which case we can check if the "lightbox" key is present at the top-level
+ // of the global settings and use its value.
+ if ( isset( $lightbox_settings['lightbox'] ) ) {
+ $lightbox_settings = wp_get_global_settings( array( 'lightbox' ) );
+ }
+ }
+
+ return $lightbox_settings ?? null;
+}
+
/**
* Registers the `core/gallery` block on server.
*
diff --git a/packages/block-library/src/gallery/view.js b/packages/block-library/src/gallery/view.js
new file mode 100644
index 00000000000000..0dc914fcf699eb
--- /dev/null
+++ b/packages/block-library/src/gallery/view.js
@@ -0,0 +1,16 @@
+/**
+ * WordPress dependencies
+ */
+import { store } from '@wordpress/interactivity';
+
+store(
+ 'core/gallery',
+ {
+ callbacks: {
+ init() {},
+ },
+ },
+ {
+ lock: false,
+ }
+);
diff --git a/packages/block-library/src/image/index.php b/packages/block-library/src/image/index.php
index 886843d83ba720..4ed43510cfb651 100644
--- a/packages/block-library/src/image/index.php
+++ b/packages/block-library/src/image/index.php
@@ -220,6 +220,7 @@ function block_core_image_render_lightbox( $block_content, $block ) {
JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP
)
);
+ $p->set_attribute( 'data-wp-init', 'callbacks.initImage' );
// Image.
$p->next_tag( 'img' );
@@ -266,6 +267,8 @@ class="lightbox-trigger"
*/
function block_core_image_print_lightbox_overlay() {
$close_button_label = esc_attr__( 'Close' );
+ $prev_button_label = esc_attr__( 'Previous' );
+ $next_button_label = esc_attr__( 'Next' );
// If the current theme does NOT have a `theme.json`, or the colors are not
// defined, it needs to set the background color & close button color to some
@@ -286,7 +289,7 @@ function block_core_image_print_lightbox_overlay() {