diff --git a/lib/block-supports/behaviors.php b/lib/block-supports/behaviors.php
index 6f442d7b0d2d7..c1b3dceceee94 100644
--- a/lib/block-supports/behaviors.php
+++ b/lib/block-supports/behaviors.php
@@ -84,17 +84,19 @@ function gutenberg_render_behaviors_support_lightbox( $block_content, $block ) {
$aria_label = __( 'Enlarge image', 'gutenberg' );
+ $processor->next_tag( 'img' );
$alt_attribute = $processor->get_attribute( 'alt' );
- if ( null !== $alt_attribute ) {
+ // An empty alt attribute `alt=""` is valid for decorative images.
+ if ( is_string( $alt_attribute ) ) {
$alt_attribute = trim( $alt_attribute );
}
+ // It only makes sense to append the alt text to the button aria-label when the alt text is non-empty.
if ( $alt_attribute ) {
/* translators: %s: Image alt text. */
$aria_label = sprintf( __( 'Enlarge image: %s', 'gutenberg' ), $alt_attribute );
}
- $content = $processor->get_updated_html();
// If we don't set a default, it won't work if Lightbox is set to enabled by default.
$lightbox_animation = 'zoom';
@@ -102,17 +104,15 @@ function gutenberg_render_behaviors_support_lightbox( $block_content, $block ) {
$lightbox_animation = $lightbox_settings['animation'];
}
- // We want to store the src in the context so we can set it dynamically when the lightbox is opened.
- $z = new WP_HTML_Tag_Processor( $content );
- $z->next_tag( 'img' );
-
+ // Note: We want to store the `src` in the context so we
+ // can set it dynamically when the lightbox is opened.
if ( isset( $block['attrs']['id'] ) ) {
$img_uploaded_src = wp_get_attachment_url( $block['attrs']['id'] );
$img_metadata = wp_get_attachment_metadata( $block['attrs']['id'] );
$img_width = $img_metadata['width'];
$img_height = $img_metadata['height'];
} else {
- $img_uploaded_src = $z->get_attribute( 'src' );
+ $img_uploaded_src = $processor->get_attribute( 'src' );
$img_width = 'none';
$img_height = 'none';
}
@@ -123,7 +123,7 @@ function gutenberg_render_behaviors_support_lightbox( $block_content, $block ) {
$scale_attr = false;
}
- $w = new WP_HTML_Tag_Processor( $content );
+ $w = new WP_HTML_Tag_Processor( $block_content );
$w->next_tag( 'figure' );
$w->add_class( 'wp-lightbox-container' );
$w->set_attribute( 'data-wp-interactive', true );
@@ -163,19 +163,20 @@ function gutenberg_render_behaviors_support_lightbox( $block_content, $block ) {
// Wrap the image in the body content with a button.
$img = null;
preg_match( '/]+>/', $body_content, $img );
- $button =
- ''
- . $img[0];
+
+ $button =
+ $img[0]
+ . '';
+
$body_content = preg_replace( '/
]+>/', $button, $body_content );
// We need both a responsive image and an enlarged image to animate
@@ -183,7 +184,7 @@ function gutenberg_render_behaviors_support_lightbox( $block_content, $block ) {
// image is a copy of the one in the body, which animates immediately
// as the lightbox is opened, while the enlarged one is a full-sized
// version that will likely still be loading as the animation begins.
- $m = new WP_HTML_Tag_Processor( $content );
+ $m = new WP_HTML_Tag_Processor( $block_content );
$m->next_tag( 'figure' );
$m->add_class( 'responsive-image' );
$m->next_tag( 'img' );
@@ -199,7 +200,7 @@ function gutenberg_render_behaviors_support_lightbox( $block_content, $block ) {
$m->set_attribute( 'data-wp-style--object-fit', 'selectors.core.image.lightboxObjectFit' );
$initial_image_content = $m->get_updated_html();
- $q = new WP_HTML_Tag_Processor( $content );
+ $q = new WP_HTML_Tag_Processor( $block_content );
$q->next_tag( 'figure' );
$q->add_class( 'enlarged-image' );
$q->next_tag( 'img' );
@@ -219,7 +220,7 @@ function gutenberg_render_behaviors_support_lightbox( $block_content, $block ) {
$close_button_icon = '';
$close_button_color = esc_attr( wp_get_global_styles( array( 'color', 'text' ) ) );
- $dialog_label = $alt_attribute ? esc_attr( $alt_attribute ) : esc_attr__( 'Image', 'gutenberg' );
+ $dialog_label = esc_attr__( 'Enlarged image', 'gutenberg' );
$close_button_label = esc_attr__( 'Close', 'gutenberg' );
$lightbox_html = <<next_tag( 'img' );
$alt_attribute = $processor->get_attribute( 'alt' );
- if ( null !== $alt_attribute ) {
+ // An empty alt attribute `alt=""` is valid for decorative images.
+ if ( is_string( $alt_attribute ) ) {
$alt_attribute = trim( $alt_attribute );
}
+ // It only makes sense to append the alt text to the button aria-label when the alt text is non-empty.
if ( $alt_attribute ) {
/* translators: %s: Image alt text. */
$aria_label = sprintf( __( 'Enlarge image: %s' ), $alt_attribute );
}
- $content = $processor->get_updated_html();
// Currently, we are only enabling the zoom animation.
$lightbox_animation = 'zoom';
- // We want to store the src in the context so we can set it dynamically when the lightbox is opened.
- $z = new WP_HTML_Tag_Processor( $content );
- $z->next_tag( 'img' );
-
+ // Note: We want to store the `src` in the context so we
+ // can set it dynamically when the lightbox is opened.
if ( isset( $block['attrs']['id'] ) ) {
$img_uploaded_src = wp_get_attachment_url( $block['attrs']['id'] );
$img_metadata = wp_get_attachment_metadata( $block['attrs']['id'] );
$img_width = $img_metadata['width'];
$img_height = $img_metadata['height'];
} else {
- $img_uploaded_src = $z->get_attribute( 'src' );
+ $img_uploaded_src = $processor->get_attribute( 'src' );
$img_width = 'none';
$img_height = 'none';
}
@@ -160,7 +163,7 @@ function block_core_image_render_lightbox( $block_content, $block ) {
$scale_attr = false;
}
- $w = new WP_HTML_Tag_Processor( $content );
+ $w = new WP_HTML_Tag_Processor( $block_content );
$w->next_tag( 'figure' );
$w->add_class( 'wp-lightbox-container' );
$w->set_attribute( 'data-wp-interactive', true );
@@ -180,7 +183,8 @@ function block_core_image_render_lightbox( $block_content, $block ) {
"imageCurrentSrc": "",
"targetWidth": "%s",
"targetHeight": "%s",
- "scaleAttr": "%s"
+ "scaleAttr": "%s",
+ "dialogLabel": "%s"
}
}
}',
@@ -188,7 +192,8 @@ function block_core_image_render_lightbox( $block_content, $block ) {
$img_uploaded_src,
$img_width,
$img_height,
- $scale_attr
+ $scale_attr,
+ __( 'Enlarged image' )
)
);
$w->next_tag( 'img' );
@@ -200,19 +205,20 @@ function block_core_image_render_lightbox( $block_content, $block ) {
// Wrap the image in the body content with a button.
$img = null;
preg_match( '/
]+>/', $body_content, $img );
- $button =
- ''
- . $img[0];
+
+ $button =
+ $img[0]
+ . '';
+
$body_content = preg_replace( '/
]+>/', $button, $body_content );
// We need both a responsive image and an enlarged image to animate
@@ -220,7 +226,7 @@ function block_core_image_render_lightbox( $block_content, $block ) {
// image is a copy of the one in the body, which animates immediately
// as the lightbox is opened, while the enlarged one is a full-sized
// version that will likely still be loading as the animation begins.
- $m = new WP_HTML_Tag_Processor( $content );
+ $m = new WP_HTML_Tag_Processor( $block_content );
$m->next_tag( 'figure' );
$m->add_class( 'responsive-image' );
$m->next_tag( 'img' );
@@ -236,7 +242,7 @@ function block_core_image_render_lightbox( $block_content, $block ) {
$m->set_attribute( 'data-wp-style--object-fit', 'selectors.core.image.lightboxObjectFit' );
$initial_image_content = $m->get_updated_html();
- $q = new WP_HTML_Tag_Processor( $content );
+ $q = new WP_HTML_Tag_Processor( $block_content );
$q->next_tag( 'figure' );
$q->add_class( 'enlarged-image' );
$q->next_tag( 'img' );
@@ -268,20 +274,16 @@ function block_core_image_render_lightbox( $block_content, $block ) {
}
$close_button_icon = '';
- $dialog_label = $alt_attribute ? esc_attr( $alt_attribute ) : esc_attr__( 'Image' );
$close_button_label = esc_attr__( 'Close' );
$lightbox_html = <<