From 2335fb1b474d7a908093da6824c51d4c7e05888c Mon Sep 17 00:00:00 2001 From: Django Doucet Date: Fri, 27 Oct 2023 14:44:47 -0600 Subject: [PATCH] Schedule comments activities for non-admin users --- includes/class-comments.php | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/includes/class-comments.php b/includes/class-comments.php index 1025158..773fa3b 100644 --- a/includes/class-comments.php +++ b/includes/class-comments.php @@ -18,6 +18,7 @@ class Comments { \add_filter( 'comment_post', array( self::class, 'postprocess_comment' ), 10, 3 ); \add_filter( 'comment_reply_link', array( self::class, 'comment_reply_link' ), 10, 4 ); \add_action( 'edit_comment', array( self::class, 'edit_comment' ), 20, 2 ); //schedule_admin_comment_activity + \add_action( 'transition_comment_status', array( self::class, 'schedule_comment_activity' ), 20, 3 ); } /** @@ -96,9 +97,9 @@ class Comments { ( 1 === $comment_approved ) && \in_array( 'administrator', $user->roles ) ) { - // Only for Admins + // Only needed for Admins (because of transition bypass) $wp_comment = \get_comment( $comment_id ); - \wp_schedule_single_event( \time(), 'activitypub_send_comment_activity', array( $wp_comment, 'Create' ) ); + \wp_schedule_single_event( \time(), 'activitypub_send_activity', array( $wp_comment, 'Create' ) ); } } @@ -129,7 +130,28 @@ class Comments { $user = \get_userdata( $commentdata['user_id'] ); if ( \in_array( 'administrator', $user->roles ) ) { $wp_comment = \get_comment( $comment_id ); - \wp_schedule_single_event( \time(), 'activitypub_send_comment_activity', array( $wp_comment, 'Update' ) ); + \wp_schedule_single_event( \time(), 'activitypub_send_activity', array( $wp_comment, 'Update' ) ); + } + } + + /** + * Schedule Comment Activities + * + * transition_comment_status() + * @param int $comment + */ + public static function schedule_comment_activity( $new_status, $old_status, $activitypub_comment ) { + if ( 'approved' === $new_status && 'approved' !== $old_status ) { + + // Only federate replies from local actors + $ap_object = unserialize( \get_comment_meta( $activitypub_comment->comment_ID, 'ap_object', true ) ); + if ( empty( $ap_object ) ) { + \wp_schedule_single_event( \time(), 'activitypub_send_activity', array( $activitypub_comment, 'Create' ) ); + } + } elseif ( 'trash' === $new_status ) { + \wp_schedule_single_event( \time(), 'activitypub_send_activity', array( $activitypub_comment, 'Delete' ) ); + } elseif ( 'update' === $new_status ) { + \wp_schedule_single_event( \time(), 'activitypub_send_activity', array( $activitypub_comment, 'Update' ) ); } } }