@@ -304,41 +304,29 @@ public function create_post( $did, $post, $post_type, $hashtags, $access_token =
304304 }
305305
306306 if ( $ post_type === 'link ' && isset ( $ post ['post_url ' ] ) && ! empty ( $ post ['post_url ' ] ) ) {
307- $ record ['embed ' ] = array (
308- '$type ' => 'app.bsky.embed.external ' ,
309- 'external ' => array (
310- 'uri ' => $ post ['post_url ' ],
311- 'title ' => isset ( $ post ['title ' ] ) ? $ post ['title ' ] : '' ,
312- 'description ' => isset ( $ post ['content ' ] ) ? $ post ['content ' ] : '' ,
313- ),
314- );
315-
316- if (
317- isset ( $ post ['post_image ' ], $ post ['mimetype ' ] ) &&
318- ! empty ( $ post ['post_image ' ] ) &&
319- ! empty ( $ post ['mimetype ' ] )
320- ) {
321- $ image_blob = $ this ->upload_blob ( $ access_token , $ post ['post_image ' ], $ post ['mimetype ' ]['type ' ] );
322-
323- if ( false !== $ image_blob ) {
324- $ record ['embed ' ]['external ' ]['thumb ' ] = $ image_blob ;
325- }
326- }
307+ $ record ['embed ' ] = $ this ->get_external_post_url ( $ post );
327308 }
328309
329- if ( $ post_type === 'image ' && isset ( $ post ['post_image ' ], $ post ['mimetype ' ] ) && ! empty ( $ post ['post_image ' ] ) && ! empty ( $ post ['mimetype ' ] ) ) {
330- $ image_blob = $ this ->upload_blob ( $ access_token , $ post ['post_image ' ], $ post ['mimetype ' ]['type ' ] );
331-
332- if ( false !== $ image_blob ) {
333- $ record ['embed ' ] = array (
334- '$type ' => 'app.bsky.embed.images ' ,
335- 'images ' => array (
336- array (
337- 'alt ' => isset ( $ post ['title ' ] ) ? $ post ['title ' ] : '' ,
338- 'image ' => $ image_blob ,
310+ if ( $ post_type === 'image ' ) {
311+ $ external_embed = $ this ->get_external_post_url ( $ post );
312+ if ( isset ( $ post ['post_image ' ], $ post ['mimetype ' ] ) && ! empty ( $ post ['post_image ' ] ) && ! empty ( $ post ['mimetype ' ] ) ) {
313+ $ image_blob = $ this ->upload_blob ( $ access_token , $ post ['post_image ' ], $ post ['mimetype ' ]['type ' ] );
314+ if ( ! empty ( $ external_embed ) ) {
315+ $ external_embed ['external ' ]['thumb ' ] = $ image_blob ;
316+ $ record ['embed ' ] = $ external_embed ;
317+ } else {
318+ $ record ['embed ' ] = array (
319+ '$type ' => 'app.bsky.embed.images ' ,
320+ 'images ' => array (
321+ array (
322+ 'alt ' => isset ( $ post ['title ' ] ) ? $ post ['title ' ] : '' ,
323+ 'image ' => $ image_blob ,
324+ ),
339325 ),
340- ),
341- );
326+ );
327+ }
328+ } elseif ( ! empty ( $ external_embed ) ) {
329+ $ record ['embed ' ] = $ external_embed ;
342330 }
343331 }
344332
@@ -499,4 +487,25 @@ public function get_image_blob_from_url( $image_url ) {
499487
500488 return $ body ;
501489 }
490+
491+ /**
492+ * Construct the embed object for an external post URL.
493+ *
494+ * @param array<string, string> $post The post data.
495+ * @return array<string, mixed> The embed object for the external post URL.
496+ */
497+ private function get_external_post_url ( $ post ) {
498+ if ( empty ( $ post ) || ! isset ( $ post ['post_url ' ] ) || empty ( $ post ['post_url ' ] ) ) {
499+ return array ();
500+ }
501+
502+ return array (
503+ '$type ' => 'app.bsky.embed.external ' ,
504+ 'external ' => array (
505+ 'uri ' => $ post ['post_url ' ],
506+ 'title ' => isset ( $ post ['title ' ] ) ? $ post ['title ' ] : '' ,
507+ 'description ' => isset ( $ post ['content ' ] ) ? $ post ['content ' ] : '' ,
508+ ),
509+ );
510+ }
502511}
0 commit comments