Skip to content

Commit 093bec6

Browse files
authored
Enhance gallery and image block markup for ap_posts (#2519)
1 parent 39199af commit 093bec6

File tree

3 files changed

+80
-12
lines changed

3 files changed

+80
-12
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: changed
3+
4+
Improve gallery and image block markup for ap_posts with better alt text and optimized layouts.

includes/class-attachments.php

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,12 @@ private static function generate_media_markup( $attachment_ids ) {
634634
);
635635
}
636636

637-
// Multiple attachments or images: use gallery block.
637+
// Single image: use standalone image block.
638+
if ( 1 === \count( $attachment_ids ) && 'image' === $type ) {
639+
return self::get_image_block( $attachment_ids[0] );
640+
}
641+
642+
// Multiple attachments: use gallery block.
638643
return self::get_gallery_block( $attachment_ids );
639644
}
640645

@@ -684,10 +689,65 @@ private static function generate_files_markup( $files ) {
684689
);
685690
}
686691

687-
// Multiple attachments or images: use gallery block.
692+
// Single image: use standalone image block.
693+
if ( 1 === \count( $files ) && 'image' === $type ) {
694+
return self::get_files_image_block( $files[0] );
695+
}
696+
697+
// Multiple attachments: use gallery block.
688698
return self::get_files_gallery_block( $files );
689699
}
690700

701+
/**
702+
* Get standalone image block markup for file-based attachments.
703+
*
704+
* @param array $file {
705+
* File data array.
706+
*
707+
* @type string $url Full URL to the file.
708+
* @type string $mime_type MIME type of the file.
709+
* @type string $alt Alt text for the file.
710+
* }
711+
*
712+
* @return string The image block markup.
713+
*/
714+
private static function get_files_image_block( $file ) {
715+
$block = '<!-- wp:image {"sizeSlug":"large","linkDestination":"none"} -->' . "\n";
716+
$block .= '<figure class="wp-block-image size-large">';
717+
$block .= '<img src="' . \esc_url( $file['url'] ) . '" alt="' . \esc_attr( $file['alt'] ) . '"/>';
718+
$block .= '</figure>' . "\n";
719+
$block .= '<!-- /wp:image -->';
720+
721+
return $block;
722+
}
723+
724+
/**
725+
* Get standalone image block markup.
726+
*
727+
* @param int $attachment_id The attachment ID.
728+
*
729+
* @return string The image block markup.
730+
*/
731+
private static function get_image_block( $attachment_id ) {
732+
$image_src = \wp_get_attachment_image_src( $attachment_id, 'large' );
733+
if ( ! $image_src ) {
734+
return '';
735+
}
736+
737+
$alt = \get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
738+
if ( ! $alt ) {
739+
$alt = \get_post_field( 'post_excerpt', $attachment_id );
740+
}
741+
742+
$block = '<!-- wp:image {"id":' . \esc_attr( $attachment_id ) . ',"sizeSlug":"large","linkDestination":"none"} -->' . "\n";
743+
$block .= '<figure class="wp-block-image size-large">';
744+
$block .= '<img src="' . \esc_url( $image_src[0] ) . '" alt="' . \esc_attr( $alt ) . '" class="' . \esc_attr( 'wp-image-' . $attachment_id ) . '"/>';
745+
$block .= '</figure>' . "\n";
746+
$block .= '<!-- /wp:image -->';
747+
748+
return $block;
749+
}
750+
691751
/**
692752
* Get gallery block markup.
693753
*
@@ -696,19 +756,23 @@ private static function generate_files_markup( $files ) {
696756
* @return string The gallery block markup.
697757
*/
698758
private static function get_gallery_block( $attachment_ids ) {
699-
$gallery = '<!-- wp:gallery {"ids":[' . \implode( ',', $attachment_ids ) . '],"linkTo":"none"} -->' . "\n";
700-
$gallery .= '<figure class="wp-block-gallery has-nested-images columns-default is-cropped">';
759+
$gallery = '<!-- wp:gallery {"columns":2,"linkTo":"none","sizeSlug":"large","imageCrop":true} -->' . "\n";
760+
$gallery .= '<figure class="wp-block-gallery has-nested-images columns-2 is-cropped">';
701761

702762
foreach ( $attachment_ids as $id ) {
703763
$image_src = \wp_get_attachment_image_src( $id, 'large' );
704764
if ( ! $image_src ) {
705765
continue;
706766
}
707767

708-
$caption = \get_post_field( 'post_content', $id );
709-
$gallery .= "\n<!-- wp:image {\"id\":{$id},\"sizeSlug\":\"large\",\"linkDestination\":\"none\"} -->\n";
768+
$alt = \get_post_meta( $id, '_wp_attachment_image_alt', true );
769+
if ( ! $alt ) {
770+
$alt = \get_post_field( 'post_excerpt', $id );
771+
}
772+
773+
$gallery .= "\n" . '<!-- wp:image {"id":' . \esc_attr( $id ) . ',"sizeSlug":"large","linkDestination":"none"} -->' . "\n";
710774
$gallery .= '<figure class="wp-block-image size-large">';
711-
$gallery .= '<img src="' . \esc_url( $image_src[0] ) . '" alt="' . \esc_attr( $caption ) . '" class="' . \esc_attr( 'wp-image-' . $id ) . '"/>';
775+
$gallery .= '<img src="' . \esc_url( $image_src[0] ) . '" alt="' . \esc_attr( $alt ) . '" class="' . \esc_attr( 'wp-image-' . $id ) . '"/>';
712776
$gallery .= '</figure>';
713777
$gallery .= "\n<!-- /wp:image -->\n";
714778
}
@@ -733,8 +797,8 @@ private static function get_gallery_block( $attachment_ids ) {
733797
* @return string The gallery block markup.
734798
*/
735799
private static function get_files_gallery_block( $files ) {
736-
$gallery = '<!-- wp:gallery {"linkTo":"none"} -->' . "\n";
737-
$gallery .= '<figure class="wp-block-gallery has-nested-images columns-default is-cropped">';
800+
$gallery = '<!-- wp:gallery {"columns":2,"linkTo":"none","imageCrop":true} -->' . "\n";
801+
$gallery .= '<figure class="wp-block-gallery has-nested-images columns-2 is-cropped">';
738802

739803
foreach ( $files as $file ) {
740804
$gallery .= "\n<!-- wp:image {\"sizeSlug\":\"large\",\"linkDestination\":\"none\"} -->\n";

tests/phpunit/tests/includes/class-test-attachments.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public function test_process_multiple_attachments() {
300300

301301
// Verify content includes gallery block.
302302
$post = get_post( self::$post_id );
303-
$this->assertStringContainsString( 'wp:gallery', $post->post_content );
303+
$this->assertStringContainsString( '<!-- wp:gallery', $post->post_content );
304304
}
305305

306306
/**
@@ -486,8 +486,8 @@ public function test_inline_images_with_attachment_overlap() {
486486
$attachments = \get_attached_media( '', $post_id );
487487
$this->assertCount( 3, $attachments ); // shared.jpg, inline-only.jpg, attachment-only.jpg.
488488

489-
// Verify gallery was added for attachment-only.jpg.
490-
$this->assertStringContainsString( '<!-- wp:gallery', $post->post_content );
489+
// Verify image block was added for attachment-only.jpg (single images use standalone blocks).
490+
$this->assertStringContainsString( '<!-- wp:image', $post->post_content );
491491

492492
// Clean up.
493493
\wp_delete_post( $post_id, true );

0 commit comments

Comments
 (0)