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

Only share when post is newly published (not updated) #38

Closed placoderm closed 1 year ago

placoderm commented 1 year ago

As I understand it, this plugin remembers if a post has ever been shared on Mastodon, and if it has, then simply updating the post will not re-publish it on Mastodon.

However, what about old posts that were published in the past before the plugin was installed? In that case, if posts were automatically publishing on Mastodon, then simply updating one of these old (previously published on WordPress but not on Mastodon) would trigger it to be published on Mastodon.

If I'm going back and editing an old post, I wouldn't normally expect updating a change to trigger publication on Mastodon.

Would it be possible to only publish on Mastodon if it is being published on WP for the first time? I realize that might mess up when you actually intend to republish on Mastodon. Not sure how that could be worked around.

janboddez commented 1 year ago

You could, potentially, prevent older posts from being shared (at all). Untested, but this may work (I mean, it's just an idea):

add_filter( 'share_on_mastodon_enabled', function( $is_enabled, $post_id ) {
  if ( get_post_timestamp( $post_id ) < strtotime( '2022-11-27' ) ) {
    return false;
  }

  return $is_enabled;
}, 10, 2 );

Should not affect any newer posts. (Note: get_post_timestamp() was introduced in WP 5.3.)

I could also limit sharing to when a post is first published by comparing its new post status with the previous one, but this is tricky. The old status could be non-existent, or draft, or even trashed. Plus, I kinda like that I can go back and syndicate old posts (kind of like "retweeting" your old stuff) when I feel like it. (I mean, I could've always done that, manually, but if I let the plugin do it [again], it stores the syndication link for me, which I can then display on my site, and so on.)

janboddez commented 1 year ago

Oh, and it is of course possible to just untick the checkbox ... :-)

If I'm going back and editing an old post, I wouldn't normally expect updating a change to trigger publication on Mastodon.

Or have it disabled by default (so that it would only syndicate after you explicitly checking it):

add_filter( 'share_on_mastodon_optin', '__return_true' );
placoderm commented 1 year ago

Thanks. I can see that the best solution would be for me to pay better attention to what I'm doing. I understand now why it works the way it does and how changing that doesn't look easy.

Great plugin!

janboddez commented 1 year ago

I'm going to reinvestigate this, comparing post dates to the most recent plugin activation date (or similar). But that approach still won't prevent "3rd-party" posts from being mass-crossposted if (1) share always is enabled programmatically AND (2) these posts were first published after the plugin was (re)activated AND (3) they are later on mass-updated.

But it would (I think?) override "Share Always" and the checkbox---this is a risk, as we'd basically be overriding site authors' choices here---for posts deemed too old.

janboddez commented 1 year ago

Current implementation fails with Gutenberg.

janboddez commented 1 year ago

Some quick logging

[06-Apr-2023 14:38:22 UTC] transition_post_status [06-Apr-2023 14:38:22 UTC] New status: auto-draft [06-Apr-2023 14:38:22 UTC] Old status: new

<< This must be where I click publish >> [06-Apr-2023 14:38:35 UTC] transition_post_status [06-Apr-2023 14:38:35 UTC] New status: publish [06-Apr-2023 14:38:35 UTC] Old status: auto-draft [06-Apr-2023 14:38:35 UTC] transition_post_status [06-Apr-2023 14:38:35 UTC] New status: publish [06-Apr-2023 14:38:35 UTC] Old status: publish [06-Apr-2023 14:38:35 UTC] Not a new message

janboddez commented 1 year ago

I know that, with Gutenberg, the action is run at least twice.

I'm guessing the first time, wp_is_post_revision( $post->ID ) || wp_is_post_autosave( $post->ID ) is somehow true, and the second time, it sees an update and decides not to crosspost.