Schedule comments activities for non-admin users

This commit is contained in:
Django Doucet 2023-10-27 14:44:47 -06:00
parent e04816a12b
commit 2335fb1b47

View file

@ -18,6 +18,7 @@ class Comments {
\add_filter( 'comment_post', array( self::class, 'postprocess_comment' ), 10, 3 ); \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_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( '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 ) && ( 1 === $comment_approved ) &&
\in_array( 'administrator', $user->roles ) \in_array( 'administrator', $user->roles )
) { ) {
// Only for Admins // Only needed for Admins (because of transition bypass)
$wp_comment = \get_comment( $comment_id ); $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'] ); $user = \get_userdata( $commentdata['user_id'] );
if ( \in_array( 'administrator', $user->roles ) ) { if ( \in_array( 'administrator', $user->roles ) ) {
$wp_comment = \get_comment( $comment_id ); $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' ) );
} }
} }
} }