@@ -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" ;
0 commit comments