diff --git a/src/block/posts/index.php b/src/block/posts/index.php
index b4e7a6b9b..c82e26681 100644
--- a/src/block/posts/index.php
+++ b/src/block/posts/index.php
@@ -4,6 +4,42 @@
exit;
}
+if ( ! function_exists( 'stackable_posts_ensure_featured_image_alt' ) ) {
+ /**
+ * Keep a non-empty featured image alt as-is.
+ * When alt is empty or missing, use the escaped post title.
+ * Untitled posts keep an empty alt so the attribute is still present.
+ *
+ * @param string $img_html
+ * @param string $post_title
+ * @return string
+ */
+ function stackable_posts_ensure_featured_image_alt( $img_html, $post_title ) {
+ $has_alt = preg_match( '/(?:^|\s)alt=(["\'])(.*?)\1/', $img_html, $alt_match );
+
+ if ( $has_alt && $alt_match[2] !== '' ) {
+ return $img_html;
+ }
+
+ $alt_value = $post_title === '' ? '' : esc_attr( $post_title );
+
+ if ( $has_alt ) {
+ if ( $alt_value === '' ) {
+ return $img_html;
+ }
+ return str_replace( $alt_match[0], ' alt="' . $alt_value . '"', $img_html );
+ }
+
+ $img_tag_pos = strpos( $img_html, '
0 ) {
// Only remove the style tag inside the image tag.
$new_img = preg_replace( '/style=\"[^\"]*\"\s?/', "", $match[ 0 ] );
- $featured_image = preg_replace( "/
]*>/", $new_img, $thumbnail );
+ $post_title = isset( $post['post_title'] ) ? $post['post_title'] : '';
+ $new_img = stackable_posts_ensure_featured_image_alt( $new_img, $post_title );
+ // Treat the replacement as plain text. preg_replace would
+ // read $n / ${n} / \n in the post title as backreferences.
+ $featured_image = str_replace( $match[ 0 ], $new_img, $thumbnail );
}
}
@@ -48,7 +88,13 @@ function generate_render_item_from_stackable_posts_block( $post, $attributes, $t
if ( empty( $featured_image ) ) {
$new_template = preg_replace( '/]*>(.*)<\/figure>/', '', $new_template );
} else {
- $new_template = preg_replace( '/
]*>/', $featured_image, $new_template );
+ $new_template = preg_replace_callback(
+ '/
]*>/',
+ function() use ( $featured_image ) {
+ return $featured_image;
+ },
+ $new_template
+ );
}
}
diff --git a/tests/phpunit/PostsRenderTest.php b/tests/phpunit/PostsRenderTest.php
index 77a5d8f8e..a740f6c9a 100644
--- a/tests/phpunit/PostsRenderTest.php
+++ b/tests/phpunit/PostsRenderTest.php
@@ -80,4 +80,87 @@ public function test_missing_thumbnail_removes_figure() {
$this->assertStringNotContainsString( 'assertStringContainsString( 'A
', $html );
}
+
+ /**
+ * Seam: generate_render_item_from_stackable_posts_block featured-image output.
+ * WAVE flags linked featured images when Media Library alt is empty.
+ */
+ private function render_featured_image( $thumbnail, $post_title ) {
+ Functions\when( 'get_the_post_thumbnail' )->justReturn( $thumbnail );
+ return generate_render_item_from_stackable_posts_block(
+ array(
+ 'ID' => 1,
+ 'post_title' => $post_title,
+ ),
+ $this->attributes(),
+ '
'
+ );
+ }
+
+ public function test_empty_thumbnail_alt_falls_back_to_post_title() {
+ $html = $this->render_featured_image(
+ '
',
+ 'Hello World'
+ );
+ $this->assertStringContainsString( 'alt="Hello World"', $html );
+ $this->assertStringContainsString( 'src="photo.jpg"', $html );
+ }
+
+ public function test_existing_thumbnail_alt_is_kept() {
+ $html = $this->render_featured_image(
+ '
',
+ 'Hello World'
+ );
+ $this->assertStringContainsString( 'alt="Custom alt"', $html );
+ $this->assertStringNotContainsString( 'alt="Hello World"', $html );
+ }
+
+ public function test_missing_alt_attribute_uses_post_title() {
+ $html = $this->render_featured_image(
+ '
',
+ 'Hello World'
+ );
+ $this->assertStringContainsString( 'alt="Hello World"', $html );
+ }
+
+ public function test_empty_title_and_empty_alt_keeps_empty_alt() {
+ $html = $this->render_featured_image(
+ '
',
+ ''
+ );
+ $this->assertStringContainsString( 'alt=""', $html );
+ $this->assertStringContainsString( '
render_featured_image(
+ '
',
+ 'A "quoted" title'
+ );
+ $this->assertStringContainsString( 'alt="A "quoted" title"', $html );
+ }
+
+ public function test_empty_alt_fallback_keeps_dollar_amount_in_title() {
+ $html = $this->render_featured_image(
+ '
',
+ 'Save $50 today'
+ );
+ $this->assertStringContainsString( 'alt="Save $50 today"', $html );
+ }
+
+ public function test_empty_alt_fallback_keeps_backslash_one_in_title() {
+ $html = $this->render_featured_image(
+ '
',
+ 'C:\\1drive tips'
+ );
+ $this->assertStringContainsString( 'alt="C:\\1drive tips"', $html );
+ }
+
+ public function test_empty_alt_fallback_keeps_dollar_brace_group_in_title() {
+ $html = $this->render_featured_image(
+ '
',
+ 'Use ${1} here'
+ );
+ $this->assertStringContainsString( 'alt="Use ${1} here"', $html );
+ }
}