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

Add an (optional) delay before crossposting #24

Closed janboddez closed 2 years ago

janboddez commented 2 years ago

Might help prevent mobile app issues.

janboddez commented 2 years ago
add_action( 'plugins_loaded', function() {
    if ( class_exists( 'Share_On_Mastodon\\Share_On_Mastodon' ) ) {
        $m_plugin       = \Share_On_Mastodon\Share_On_Mastodon::get_instance();
        $m_post_handler = $m_plugin->get_post_handler();

        remove_action( 'transition_post_status', array( $m_post_handler, 'toot' ), 999 );

        add_action( 'toot_toot', function( $new_status, $old_status, $post_id ) use ( $m_post_handler ) {
            // Should get called no less than 3 minutes after a post is saved.
            $post = get_post( $post_id );
            $m_post_handler->toot( $new_status, $old_status, $post );
        }, 10, 3 );
    }

    if ( class_exists( 'Share_On_Pixelfed\\Share_On_Pixelfed' ) ) {
        $p_plugin       = \Share_On_Pixelfed\Share_On_Pixelfed::get_instance();
        $p_post_handler = $p_plugin->get_post_handler();

        remove_action( 'transition_post_status', array( $p_post_handler, 'toot' ), 999 );
        add_action( 'toot_toot', function( $new_status, $old_status, $post_id ) use ( $p_post_handler ) {
            // Should get called no less than 3 minutes after a post is saved.
            $post = get_post( $post_id );
            $p_post_handler->toot( $new_status, $old_status, $post );
        }, 10, 3 );
    }

    // Add a different callback function, 3 minutes after a post is saved (updated, etc.).
    add_action( 'transition_post_status', function( $new_status, $old_status, $post ) {
        wp_schedule_single_event( time() + 180, 'toot_toot', array( $new_status, $old_status, $post->ID ) );
    }, 10, 3 );

    add_filter( 'share_on_pixelfed_enabled', '__return_true' );
} );