Skip to content

Commit 13bbfe0

Browse files
committed
fix #23
1 parent 0fa7fc6 commit 13bbfe0

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

activitypub.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function activitypub_init() {
3636
require_once dirname( __FILE__ ) . '/includes/class-rest-activitypub-outbox.php';
3737
add_action( 'rest_api_init', array( 'Rest_Activitypub_Outbox', 'register_routes' ) );
3838
add_action( 'activitypub_send_post_activity', array( 'Rest_Activitypub_Outbox', 'send_post_activity' ) );
39+
add_action( 'activitypub_send_update_activity', array( 'Rest_Activitypub_Outbox', 'send_update_activity' ) );
3940

4041
require_once dirname( __FILE__ ) . '/includes/class-rest-activitypub-inbox.php';
4142
add_action( 'rest_api_init', array( 'Rest_Activitypub_Inbox', 'register_routes' ) );
@@ -62,9 +63,7 @@ function activitypub_init() {
6263
add_post_type_support( 'page', 'activitypub' );
6364

6465
$post_types = get_post_types_by_support( 'activitypub' );
65-
foreach ( $post_types as $post_type ) {
66-
add_action( 'publish_' . $post_type, array( 'Activitypub', 'schedule_post_activity' ) );
67-
}
66+
add_action( 'transition_post_status', array( 'Activitypub', 'schedule_post_activity' ), 10, 3 );
6867

6968
require_once dirname( __FILE__ ) . '/includes/class-activitypub-admin.php';
7069
add_action( 'admin_menu', array( 'Activitypub_Admin', 'admin_menu' ) );

includes/class-activitypub.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ public static function add_rewrite_endpoint() {
7676
*
7777
* @param int $post_id
7878
*/
79-
public static function schedule_post_activity( $post_id ) {
80-
wp_schedule_single_event( time() + wp_rand( 0, 120 ), 'activitypub_send_post_activity', array( $post_id ) );
79+
public static function schedule_post_activity( $new_status, $old_status, $post ) {
80+
if ( 'publish' === $new_status && 'publish' !== $old_status ) {
81+
wp_schedule_single_event( time() + wp_rand( 0, 120 ), 'activitypub_send_post_activity', array( $post->ID ) );
82+
} elseif ( 'publish' === $new_status ) {
83+
wp_schedule_single_event( time() + wp_rand( 0, 120 ), 'activitypub_send_update_activity', array( $post->ID ) );
84+
}
8185
}
8286
}

includes/class-rest-activitypub-outbox.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,21 @@ public static function send_post_activity( $post_id ) {
124124
$response = activitypub_safe_remote_post( $inbox, $activity, $user_id );
125125
}
126126
}
127+
128+
public static function send_update_activity( $post_id ) {
129+
$post = get_post( $post_id );
130+
$user_id = $post->post_author;
131+
132+
$activitypub_post = new Activitypub_Post( $post );
133+
$activitypub_activity = new Activitypub_Activity( 'Update', Activitypub_Activity::TYPE_FULL );
134+
$activitypub_activity->from_post( $activitypub_post->to_array() );
135+
136+
$activity = $activitypub_activity->to_json(); // phpcs:ignore
137+
138+
$followers = Db_Activitypub_Followers::get_followers( $user_id );
139+
140+
foreach ( activitypub_get_follower_inboxes( $user_id, $followers ) as $inbox ) {
141+
$response = activitypub_safe_remote_post( $inbox, $activity, $user_id );
142+
}
143+
}
127144
}

0 commit comments

Comments
 (0)