@@ -101,7 +101,8 @@ public function __construct( Feed $friends_feed ) {
101
101
add_filter ( 'mastodon_api_timelines_args ' , array ( $ this , 'mastodon_api_timelines_args ' ) );
102
102
add_filter ( 'mastodon_api_account ' , array ( $ this , 'mastodon_api_account_augment_friend_posts ' ), 9 , 4 );
103
103
add_filter ( 'mastodon_api_status ' , array ( $ this , 'mastodon_api_status_add_reblogs ' ), 30 , 3 );
104
- add_filter ( 'mastodon_api_status ' , array ( $ this , 'mastodon_api_status_add_attachments ' ), 20 , 3 );
104
+ add_filter ( 'mastodon_api_status ' , array ( $ this , 'mastodon_api_status_add_image_attachments ' ), 20 , 3 );
105
+ add_filter ( 'mastodon_api_status ' , array ( $ this , 'mastodon_api_status_add_video_attachments ' ), 20 , 3 );
105
106
add_filter ( 'mastodon_api_canonical_user_id ' , array ( $ this , 'mastodon_api_canonical_user_id ' ), 20 , 3 );
106
107
}
107
108
@@ -228,7 +229,7 @@ public function mastodon_api_account_augment_friend_posts( $account, $user_id, $
228
229
return $ account ;
229
230
}
230
231
231
- public function mastodon_api_status_add_attachments ( $ status , $ post_id , $ request ) {
232
+ public function mastodon_api_status_add_image_attachments ( $ status , $ post_id , $ request ) {
232
233
if ( Friends::CPT !== get_post_type ( $ post_id ) ) {
233
234
return $ status ;
234
235
}
@@ -242,16 +243,79 @@ public function mastodon_api_status_add_attachments( $status, $post_id, $request
242
243
243
244
foreach ( $ matches as $ match ) {
244
245
$ status ->content = str_replace ( $ match [0 ], '' , $ status ->content );
245
- if ( ! preg_match ( '/<img src="(?P<url>[^"]+)" width="(?P<width>[^"]*)" height="(?P<height>[^"]*)"/ ' , $ match [2 ], $ block ) ) {
246
+ if ( ! preg_match ( '/<img\b([^>]+)>/ ' , $ match [2 ], $ img ) ) {
247
+ continue ;
248
+ }
249
+ $ block = array ();
250
+ foreach ( array ( 'src ' , 'width ' , 'height ' ) as $ attr ) {
251
+ if ( preg_match ( '/\s ' . $ attr . '="(?P< ' . $ attr . '>[^"]+)"/ ' , $ img [1 ], $ m ) ) {
252
+ $ block [ $ attr ] = $ m [ $ attr ];
253
+ }
254
+ }
255
+ if ( ! isset ( $ block ['src ' ] ) ) {
246
256
continue ;
247
257
}
248
258
249
259
$ attachment = new \Enable_Mastodon_Apps \Entity \Media_Attachment ();
250
- $ attachment ->id = strval ( 2e10 + crc32 ( $ block ['url ' ] ) );
260
+ $ attachment ->id = strval ( 2e10 + crc32 ( $ block ['src ' ] ) );
251
261
$ attachment ->type = 'image ' ;
252
- $ attachment ->url = $ block ['url ' ];
253
- $ attachment ->preview_url = $ block ['url ' ];
254
- $ attachment ->remote_url = $ block ['url ' ];
262
+ $ attachment ->url = $ block ['src ' ];
263
+ $ attachment ->preview_url = $ block ['src ' ];
264
+ $ attachment ->remote_url = $ block ['src ' ];
265
+ if ( isset ( $ block ['width ' ] ) && $ block ['width ' ] > 0 && isset ( $ block ['height ' ] ) && $ block ['height ' ] > 0 ) {
266
+ $ attachment ->meta = array (
267
+ 'width ' => intval ( $ block ['width ' ] ),
268
+ 'height ' => intval ( $ block ['height ' ] ),
269
+ 'size ' => $ block ['width ' ] . 'x ' . $ block ['height ' ],
270
+ 'aspect ' => $ block ['width ' ] / $ block ['height ' ],
271
+ );
272
+ } else {
273
+ $ attachment ->meta = array (
274
+ 'width ' => 0 ,
275
+ 'height ' => 0 ,
276
+ 'size ' => 0x0 ,
277
+ 'aspect ' => 1 ,
278
+ );
279
+ }
280
+ $ attachment ->description = '' ;
281
+ $ status ->media_attachments [] = $ attachment ;
282
+ }
283
+ return $ status ;
284
+ }
285
+
286
+ public function mastodon_api_status_add_video_attachments ( $ status , $ post_id , $ request ) {
287
+ if ( Friends::CPT !== get_post_type ( $ post_id ) ) {
288
+ return $ status ;
289
+ }
290
+ if ( false === strpos ( $ status ->content , '<video ' ) ) {
291
+ return $ status ;
292
+ }
293
+ preg_match_all ( '/<video\b([^>]+)>/ ' , $ status ->content , $ matches , PREG_SET_ORDER );
294
+ if ( empty ( $ matches ) ) {
295
+ return $ status ;
296
+ }
297
+
298
+ foreach ( $ matches as $ match ) {
299
+ $ status ->content = str_replace ( $ match [0 ], '' , $ status ->content );
300
+ $ block = array ();
301
+ foreach ( array ( 'src ' , 'width ' , 'height ' , 'poster ' ) as $ attr ) {
302
+ if ( preg_match ( '/\s ' . $ attr . '="(?P< ' . $ attr . '>[^"]+)"/ ' , $ match [1 ], $ m ) ) {
303
+ $ block [ $ attr ] = $ m [ $ attr ];
304
+ }
305
+ }
306
+
307
+ if ( ! isset ( $ block ['src ' ] ) ) {
308
+ continue ;
309
+ }
310
+
311
+ $ attachment = new \Enable_Mastodon_Apps \Entity \Media_Attachment ();
312
+ $ attachment ->id = strval ( 2e10 + crc32 ( $ block ['src ' ] ) );
313
+ $ attachment ->type = 'video ' ;
314
+ $ attachment ->url = $ block ['src ' ];
315
+ if ( isset ( $ block ['poster ' ] ) ) {
316
+ $ attachment ->preview_url = $ block ['poster ' ];
317
+ }
318
+ $ attachment ->remote_url = $ block ['src ' ];
255
319
if ( isset ( $ block ['width ' ] ) && $ block ['width ' ] > 0 && isset ( $ block ['height ' ] ) && $ block ['height ' ] > 0 ) {
256
320
$ attachment ->meta = array (
257
321
'width ' => intval ( $ block ['width ' ] ),
0 commit comments