janboddez / share-on-mastodon

Easily share WordPress posts on Mastodon.
https://jan.boddez.net/wordpress/share-on-mastodon
GNU General Public License v3.0
39 stars 5 forks source link

Audio and video attachments #64

Closed janboddez closed 1 year ago

janboddez commented 1 year ago

Probable small audience, but it shouldn't be too hard to expand toward other media formats.

The tricky bit is going to be remote errors (due to file size etc.) and PHP timeouts, most likely.

janboddez commented 1 year ago

95% sure the upload_image() method kind of already works with any (supported) media file.

janboddez commented 1 year ago

Looks like Mastodon's web client supports up to 4 images/videos (in any combination), or e.g. up to 3 images/videos and 1 audio file? But that as soon as an audio file is uploaded, the upload icon/button becomes unresponsive.

Also, this may be something for an add-on plugin, because: how are we going to fetch an audio or video file in the first place? What if there's 4 images to be uploaded but the post also contains an audio file, maybe even before the images? Which are we going to include? Should we always, per the limitation above, include audio last? And only if there's a spot available?

A: No. If a specific blog decides it needs audio support, it's likely the want to prioritize it over (some) images. They may want to fetch the file similar to how we fetch "in-post" images. We should look at the audio/video shortcodes (and similar blocks) to see what HTML they output. We can probably guess file paths from that; the rest should be fairly simple.

We can and maybe should include video in the list of "in-post" images.

For audio, we can perhaps limit the no. of files to 1 and full disregard images, or upload up to 3 images/videos before uploading the audio file.

Except, the best way to tackle this probably make the media array filterable, then any developer can choose how to deal with other mime types themselves. https://github.com/janboddez/share-on-mastodon/blob/6dbb07e35b4a3ff38fbdd36c80ff0bd88a10a0e2/includes/class-image-handler.php#L57

janboddez commented 1 year ago

If the audio (and I think the same goes for video) was uploaded from the Create/Edit Post screen, we should be able to simply use ...

$audios = get_attached_media( 'audio', $post_id );

if ( empty( $audios ) ) {
    return;
}

$audio       = reset( $audios );
$atts['src'] = wp_get_attachment_url( $audio->ID );

In fact, we only need the ID.

janboddez commented 1 year ago

Added the share_on_mastodon_media filter, which makes it easy enough to do this. I'd close the issue, but there's one potential bug: wp_get_attachment_image_src(), which we use to fetch an image's large thumbnail (for uploading) instead of the full image, could potentially also return a thumbnail for non-image files. Which would then be uploaded instead of the [video or audio]. Better check that an attachment really is an image before using this function's result.