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

No media upload when posted from the WordPress Android app. #17

Closed timnolte closed 1 year ago

timnolte commented 3 years ago

I have explicitly used the hook share_on_mastodon_enabled to get posts published from the mobile app to be shared, and while it works and includes my custom content leveraging the share_on_mastodon_status hook the featured image isn't being included.

janboddez commented 3 years ago

Hmm, I was just able to crosspost a featured image just fine, (even) from the WP app. (Force-enabled crossposting by adding add_filter( 'share_on_mastodon_enabled', '__return_true', 999 ); to a must-use plugin, but you got that part right or it wouldn't work at all ... [That prio number is pretty random, btw. Just didn't wanna take any risks.] Did not touch any of the other filters.)

Is your featured image correctly set, on the post itself?

Any idea what Mastodon version your instance is on? (I'm still on 3.2.2, I believe the latest one is 3.3.0.) One thing that comes to mind is the media endpoint I'm using was going to deprecated ... On 3.3.0 now, still works, I'm afraid: https://geekcompass.com/@ochtendgrijs/105510788586078078.

Another is that the image is too large or the request times out, although I set that to 15 seconds.

Would you mind enabling debug logging, by adding something like this to wp-config.php?

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );

I'm pretty much writing the entire error/response to the debug log, but only when logging is enabled.

https://github.com/janboddez/share-on-mastodon/blob/33aafa2d4e8d27db2c8143582ee8d6dc6d45ef7e/includes/class-post-handler.php#L428

Then, if something isn't going as planned, it should show in the log.

timnolte commented 3 years ago

Yeah, I'll do some more testing tonight with debugging turned on. I'm wondering if for new posts there is a timing/order issue since the image may not actually but uploaded to the site yet when the post is uploaded/published.

janboddez commented 3 years ago

Just curious: any update on this?

I mostly use a Micropub app (which is somewhat similar), rather than the official WP app, and because it doesn't do Featured Images per se, I sort of am delaying crossposting till I've "fixed" that (force-set a Featured Image, in a way, but after the post's already published) later on in WordPress's sequence. So that would be kind of similar.

I guess what I'm saying is, you could also not use share_on_mastodon_enabled, but rather set the underlying post meta field after crossposting would normally take place, and only then "trigger" crossposting (or, perhaps, schedule it a couple minutes into the future).

Just a quick thought:

// This would run _after_ crossposting to Mastodon would normally take place.
add_action( 'publish_post', function( $post_id, $post ) {
  if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) {
    // Do nothing.
    return;
  }

  // Bluntly enable sharing to Mastodon for each and every post.
  update_post_meta( $post_id, '_share_on_mastodon', '1' );

  // Quick and dirty way to re-trigger Share on Mastodon's `Post_Handler::toot()` method.
  // do_action( 'transition_post_status', 'publish', 'publish', $post ); // Still too soon?

  wp_schedule_single_event( time() + 60, 'my_custom_event', array( $post ) ); // Or something like this.
}, 10, 2 );

add_action( 'my_custom_event', function( $post ) {
  // Quick and dirty way to re-trigger Share on Mastodon's `Post_Handler::toot()` method.
  do_action( 'transition_post_status', 'publish', 'publish', $post );
} );

Of course, publish_post works only for, well, posts. But there are ways around that as well. (Was hoping to avoid endless loops here by not reusing transition_post_status.)

larsmagne commented 1 year ago

I ran into the same issue. I'm posting via xmlrpc, and I'm using a plugin to always set the first image as the featured image (and I've set Share on Mastodon to include the featured image, and no attached images). But perhaps that plugin is being run too late, so there's no featured image when Share on Mastodon is being triggered?

I wonder whether an alternate take on this would simplify things: I want the first image in the post to be included, so I could instead use share_on_mastodon_attached_images true -- if I could control the number of attached images to include. That's currently hardcoded at 4 (in post_to_mastodon). If that wasn't hardcoded, I could just change the number of images to 1, and things would presumably work as I want them to...

janboddez commented 1 year ago

But perhaps that plugin is being run too late, so there's no featured image when Share on Mastodon is being triggered?

I've been trying to replicate this behavior, unsuccessfully.

The latest version has an option for delayed posting. What happens when you use that? Set it to, e.g., 300 seconds or so?

You could even post a test post through XML-RPC, and then check in WP Admin (without saving anything, as that would trigger syndication) if the featured image is really there. If so, it should go through okay once the cron event is triggered.

Would that be something you're willing to give a try?

larsmagne commented 1 year ago

Sure, I can give delayed posting a try. I had a peek at the documentation, but didn't see any mention of "delay" there -- what's the incantation to use?

janboddez commented 1 year ago

There should be a field on the settings page. Maybe try setting it to 300 seconds (or 120 or something, but, say, more than a minute)?

larsmagne commented 1 year ago

Ah, right. I've now updated the setting and will be testing later today and get back to you on the results.

larsmagne commented 1 year ago

I've now tried it, and it seems to work perfectly. I used a 30 second delay, but I can probably make that shorter.

janboddez commented 1 year ago

Ah, cool! Yeah, either it's something using a later hook than the one I use to trigger syndication, or some background process that hasn't yet finished. Glad it works this way. I should update the documentation now.