From 13bbfe05018a20062ddbf21d7bd4879ea6aac245 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Sun, 17 Feb 2019 21:10:53 +0100 Subject: [PATCH] fix #23 --- activitypub.php | 5 ++--- includes/class-activitypub.php | 8 ++++++-- includes/class-rest-activitypub-outbox.php | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/activitypub.php b/activitypub.php index 58fee8b..5ba34e1 100644 --- a/activitypub.php +++ b/activitypub.php @@ -36,6 +36,7 @@ function activitypub_init() { require_once dirname( __FILE__ ) . '/includes/class-rest-activitypub-outbox.php'; add_action( 'rest_api_init', array( 'Rest_Activitypub_Outbox', 'register_routes' ) ); add_action( 'activitypub_send_post_activity', array( 'Rest_Activitypub_Outbox', 'send_post_activity' ) ); + add_action( 'activitypub_send_update_activity', array( 'Rest_Activitypub_Outbox', 'send_update_activity' ) ); require_once dirname( __FILE__ ) . '/includes/class-rest-activitypub-inbox.php'; add_action( 'rest_api_init', array( 'Rest_Activitypub_Inbox', 'register_routes' ) ); @@ -62,9 +63,7 @@ function activitypub_init() { add_post_type_support( 'page', 'activitypub' ); $post_types = get_post_types_by_support( 'activitypub' ); - foreach ( $post_types as $post_type ) { - add_action( 'publish_' . $post_type, array( 'Activitypub', 'schedule_post_activity' ) ); - } + add_action( 'transition_post_status', array( 'Activitypub', 'schedule_post_activity' ), 10, 3 ); require_once dirname( __FILE__ ) . '/includes/class-activitypub-admin.php'; add_action( 'admin_menu', array( 'Activitypub_Admin', 'admin_menu' ) ); diff --git a/includes/class-activitypub.php b/includes/class-activitypub.php index ebc42ed..e1088fc 100644 --- a/includes/class-activitypub.php +++ b/includes/class-activitypub.php @@ -76,7 +76,11 @@ class Activitypub { * * @param int $post_id */ - public static function schedule_post_activity( $post_id ) { - wp_schedule_single_event( time() + wp_rand( 0, 120 ), 'activitypub_send_post_activity', array( $post_id ) ); + public static function schedule_post_activity( $new_status, $old_status, $post ) { + if ( 'publish' === $new_status && 'publish' !== $old_status ) { + wp_schedule_single_event( time() + wp_rand( 0, 120 ), 'activitypub_send_post_activity', array( $post->ID ) ); + } elseif ( 'publish' === $new_status ) { + wp_schedule_single_event( time() + wp_rand( 0, 120 ), 'activitypub_send_update_activity', array( $post->ID ) ); + } } } diff --git a/includes/class-rest-activitypub-outbox.php b/includes/class-rest-activitypub-outbox.php index d10cc5f..0abf285 100644 --- a/includes/class-rest-activitypub-outbox.php +++ b/includes/class-rest-activitypub-outbox.php @@ -124,4 +124,21 @@ class Rest_Activitypub_Outbox { $response = activitypub_safe_remote_post( $inbox, $activity, $user_id ); } } + + public static function send_update_activity( $post_id ) { + $post = get_post( $post_id ); + $user_id = $post->post_author; + + $activitypub_post = new Activitypub_Post( $post ); + $activitypub_activity = new Activitypub_Activity( 'Update', Activitypub_Activity::TYPE_FULL ); + $activitypub_activity->from_post( $activitypub_post->to_array() ); + + $activity = $activitypub_activity->to_json(); // phpcs:ignore + + $followers = Db_Activitypub_Followers::get_followers( $user_id ); + + foreach ( activitypub_get_follower_inboxes( $user_id, $followers ) as $inbox ) { + $response = activitypub_safe_remote_post( $inbox, $activity, $user_id ); + } + } }