diff --git a/src/wp-includes/blocks/image.php b/src/wp-includes/blocks/image.php
index acefd5714bbd4..7df0949c8dfa0 100644
--- a/src/wp-includes/blocks/image.php
+++ b/src/wp-includes/blocks/image.php
@@ -229,26 +229,32 @@ function block_core_image_render_lightbox( $block_content, $block ) {
$body_content = $w->get_updated_html();
// Add a button alongside image in the body content.
- $img = null;
- preg_match( '/]+>/', $body_content, $img );
-
- $button =
- $img[0]
- . '';
-
- $body_content = preg_replace( '/
]+>/', $button, $body_content );
+ $body_content = preg_replace_callback(
+ '/
]+>/',
+ static function ( $img_match ) use ( $aria_label ) {
+ $button_html = WP_HTML::render(
+ <<<'HTML'
+
+HTML,
+ array(
+ 'label' => $aria_label,
+ 'interactivity' => array(
+ 'data-wp-on--click' => 'actions.core.image.showLightbox',
+ 'data-wp-style--right' => 'context.core.image.imageButtonRight',
+ 'data-wp-style--top' => 'context.core.image.imageButtonTop',
+ ),
+ )
+ );
+
+ return $img_match[0] . $button_html;
+ },
+ $body_content,
+ 1
+ );
// We need both a responsive image and an enlarged image to animate
// the zoom seamlessly on slow internet connections; the responsive
@@ -295,40 +301,46 @@ class="lightbox-trigger"
if ( wp_theme_has_theme_json() ) {
$global_styles_color = wp_get_global_styles( array( 'color' ) );
if ( ! empty( $global_styles_color['background'] ) ) {
- $background_color = esc_attr( $global_styles_color['background'] );
+ $background_color = $global_styles_color['background'];
}
if ( ! empty( $global_styles_color['text'] ) ) {
- $close_button_color = esc_attr( $global_styles_color['text'] );
+ $close_button_color = $global_styles_color['text'];
}
}
- $close_button_icon = '';
- $close_button_label = esc_attr__( 'Close' );
-
- $lightbox_html = <<
-
-