@@ -368,12 +368,18 @@ function ( $tag ) {
368368 'tmp_name ' => $ file_path ,
369369 );
370370
371+ $ meta = array ();
372+ if ( 'image ' === strtok ( $ attachment ->{'mediaType ' }, '/ ' ) && ! empty ( $ attachment ->name ) ) {
373+ $ meta = array ( '_wp_attachment_image_alt ' => $ attachment ->name );
374+ }
375+
371376 $ attachment_data = array (
372377 'post_mime_type ' => $ attachment ->{'mediaType ' },
373378 'post_title ' => $ attachment ->name ?? '' ,
374379 'post_content ' => $ attachment ->name ?? '' ,
375380 'post_status ' => 'inherit ' ,
376381 'post_author ' => self ::$ author ,
382+ 'meta_input ' => $ meta ,
377383 );
378384
379385 $ attachment_id = \media_handle_sideload ( $ file_array , $ post_id , '' , $ attachment_data );
@@ -387,36 +393,33 @@ function ( $tag ) {
387393
388394 // If we have attachments, add them to the post content.
389395 if ( ! empty ( $ attachment_ids ) ) {
396+ $ type = strtok ( \get_post_mime_type ( $ attachment_ids [0 ] ), '/ ' );
397+
390398 if ( site_supports_blocks () ) {
391- // Block editor: Use gallery block.
392- $ gallery = '<!-- wp:gallery {"ids":[ ' . \implode ( ', ' , $ attachment_ids ) . '],"linkTo":"none"} --> ' . "\n" ;
393- $ gallery .= '<figure class="wp-block-gallery has-nested-images columns-default is-cropped"> ' ;
394-
395- foreach ( $ attachment_ids as $ id ) {
396- $ image_src = \wp_get_attachment_image_src ( $ id , 'large ' );
397- if ( ! $ image_src ) {
398- continue ;
399- }
400-
401- $ caption = \get_post_field ( 'post_content ' , $ id );
402- $ gallery .= "\n<!-- wp:image { \"id \": {$ id }, \"sizeSlug \": \"large \", \"linkDestination \": \"none \"} --> \n" ;
403- $ gallery .= '<figure class="wp-block-image size-large"> ' ;
404- $ gallery .= '<img src=" ' . \esc_url ( $ image_src [0 ] ) . '" alt=" ' . \esc_attr ( $ caption ) . '" class=" ' . \esc_attr ( 'wp-image- ' . $ id ) . '"/> ' ;
405- $ gallery .= '</figure> ' ;
406- $ gallery .= "\n<!-- /wp:image --> \n" ;
399+ if ( 1 === \count ( $ attachment_ids ) && ( 'video ' === $ type || 'audio ' === $ type ) ) {
400+ $ media = sprintf (
401+ '<!-- wp:%1$s {"id":"%2$s"} --><figure class="wp-block-%1$s"><%1$s controls src="%3$s"></%1$s></figure><!-- /wp:%1$s --> ' ,
402+ \esc_attr ( $ type ),
403+ \esc_attr ( $ attachment_ids [0 ] ),
404+ \esc_url ( \wp_get_attachment_url ( $ attachment_ids [0 ] ) )
405+ );
406+ } else {
407+ $ media = self ::get_gallery_block ( $ attachment_ids );
408+ }
409+ } else { // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found
410+ // Classic editor: Use shortcodes.
411+ if ( 1 === \count ( $ attachment_ids ) && ( 'video ' === $ type || 'audio ' === $ type ) ) {
412+ // Block editor: Use video block.
413+ $ media = sprintf ( '[%1$s src="%2$s"] ' , \esc_attr ( $ type ), \esc_url ( \wp_get_attachment_url ( $ attachment_ids [0 ] ) ) );
414+ } else {
415+ $ media = '[gallery ids=" ' . \implode ( ', ' , $ attachment_ids ) . '" link="none"] ' ;
407416 }
408-
409- $ gallery .= "</figure> \n" ;
410- $ gallery .= '<!-- /wp:gallery --> ' ;
411- } else {
412- // Classic editor: Use gallery shortcode.
413- $ gallery = '[gallery ids=" ' . \implode ( ', ' , $ attachment_ids ) . '" link="none"] ' ;
414417 }
415418
416419 \wp_update_post (
417420 array (
418421 'ID ' => $ post_id ,
419- 'post_content ' => $ post_data ['post_content ' ] . "\n\n" . $ gallery ,
422+ 'post_content ' => $ post_data ['post_content ' ] . "\n\n" . $ media ,
420423 )
421424 );
422425 }
@@ -487,4 +490,35 @@ public static function greet() {
487490 \wp_import_upload_form ( 'admin.php?import=mastodon&step=1 ' );
488491 echo '</div> ' ;
489492 }
493+
494+ /**
495+ * Get gallery block.
496+ *
497+ * @param array $attachment_ids The attachment IDs to use.
498+ * @return string The gallery block markup.
499+ */
500+ private static function get_gallery_block ( $ attachment_ids ) {
501+ // Block editor: Use gallery block.
502+ $ gallery = '<!-- wp:gallery {"ids":[ ' . \implode ( ', ' , $ attachment_ids ) . '],"linkTo":"none"} --> ' . "\n" ;
503+ $ gallery .= '<figure class="wp-block-gallery has-nested-images columns-default is-cropped"> ' ;
504+
505+ foreach ( $ attachment_ids as $ id ) {
506+ $ image_src = \wp_get_attachment_image_src ( $ id , 'large ' );
507+ if ( ! $ image_src ) {
508+ continue ;
509+ }
510+
511+ $ caption = \get_post_field ( 'post_content ' , $ id );
512+ $ gallery .= "\n<!-- wp:image { \"id \": {$ id }, \"sizeSlug \": \"large \", \"linkDestination \": \"none \"} --> \n" ;
513+ $ gallery .= '<figure class="wp-block-image size-large"> ' ;
514+ $ gallery .= '<img src=" ' . \esc_url ( $ image_src [0 ] ) . '" alt=" ' . \esc_attr ( $ caption ) . '" class=" ' . \esc_attr ( 'wp-image- ' . $ id ) . '"/> ' ;
515+ $ gallery .= '</figure> ' ;
516+ $ gallery .= "\n<!-- /wp:image --> \n" ;
517+ }
518+
519+ $ gallery .= "</figure> \n" ;
520+ $ gallery .= '<!-- /wp:gallery --> ' ;
521+
522+ return $ gallery ;
523+ }
490524}
0 commit comments