From f943de1e79de0363a000fe027a40b5d0fd9205f4 Mon Sep 17 00:00:00 2001 From: Django Doucet Date: Sun, 29 Jan 2023 21:44:15 -0700 Subject: [PATCH] remove type and mention meta from comment filters --- includes/class-comments.php | 56 ++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/includes/class-comments.php b/includes/class-comments.php index b95b931..fd625f0 100644 --- a/includes/class-comments.php +++ b/includes/class-comments.php @@ -13,11 +13,11 @@ class Comments { public static function init() { \add_filter( 'preprocess_comment', array( '\Activitypub\Comments', 'preprocess_comment' ) ); + \add_filter( 'comment_text', array( '\Activitypub\Comments', 'comment_content_filter' ), 10, 3 ); \add_filter( 'comment_post', array( '\Activitypub\Comments', 'postprocess_comment' ), 10, 3 ); \add_filter( 'wp_update_comment_data', array( '\Activitypub\Comments', 'comment_updated_published' ), 20, 3 ); - \add_action( 'transition_comment_status', array( '\Activitypub\Comments', 'schedule_comment_activity' ), 20, 3 ); \add_action( 'edit_comment', array( '\Activitypub\Comments', 'edit_comment' ), 20, 2 );//schedule_admin_comment_activity - \add_filter( 'get_comment_text', array( '\Activitypub\Comments', 'comment_append_edit_datetime' ), 10, 3 ); + \add_action( 'transition_comment_status', array( '\Activitypub\Comments', 'schedule_comment_activity' ), 20, 3 ); } @@ -26,23 +26,44 @@ class Comments { * preprocess local comments for federated replies */ public static function preprocess_comment( $commentdata ) { - // only process replies from local actors + // only process replies from authorized local actors, for ap enabled post types $user = \get_userdata( $commentdata['user_id'] ); - if ( $user->has_cap( 'publish_post' ) ) { - // transform webfinger mentions to links and add @mentions to cc - $tagged_content = \Activitypub\transform_tags( $commentdata['comment_content'] ); - $commentdata['comment_content'] = $tagged_content['content']; - $commentdata['comment_meta']['mentions'] = $tagged_content['mentions']; + $comment_post_type = \get_post_type( $commentdata['comment_post_ID'] ); + $post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ); + + if ( $user->has_cap( 'publish_post' ) && \in_array( $comment_post_type, $post_types, true ) ) { + $commentdata['comment_meta']['protocol'] = 'activitypub'; } return $commentdata; } + /** + * comment_text( $comment ) + * Filters the comment text for display. + * + * @param string $comment_text + * @param WP_Comment $comment + * @param array $args + * @return void + */ + public static function comment_content_filter( $comment_text, $comment, $args ) { + $protocol = \get_comment_meta( $comment->comment_ID, 'protocol', true ); + // TODO Test if this is returned by Model/Comment + if ( 'activitypub' === $protocol ) { + $updated = \wp_date( 'Y-m-d H:i:s', \strtotime( \get_comment_meta( $comment->comment_ID, 'ap_last_modified', true ) ) ); + if ( $updated ) { + $comment_text .= "
(Last edited on )
"; + } + } + return $comment_text; + } + /** * comment_post() * postprocess_comment for federating replies and inbox-forwarding */ public static function postprocess_comment( $comment_id, $comment_approved, $commentdata ) { - //Adminstrator role users comments bypass transition_comment_status (auto approved) + //Administrator role users comments bypass transition_comment_status (auto approved) $user = \get_userdata( $commentdata['user_id'] ); if ( ( 1 === $comment_approved ) && @@ -61,6 +82,7 @@ class Comments { */ public static function edit_comment( $comment_id, $data ) { if ( ! is_null( $data['user_id'] ) ) { + // TODO Test with non-admin user has_publish cap \wp_schedule_single_event( \time(), 'activitypub_send_update_comment_activity', array( $comment_id ) ); } } @@ -100,20 +122,4 @@ class Comments { } } - /** - * get_comment_text( $comment ) - * - * Filters the comment content before it is updated in the database. - */ - public static function comment_append_edit_datetime( $comment_text, $comment, $args ) { - if ( 'activitypub' === $comment->comment_type ) { - $updated = \wp_date( 'Y-m-d H:i:s', \strtotime( \get_comment_meta( $comment->comment_ID, 'ap_last_modified', true ) ) ); - if ( $updated ) { - $append_updated = "
(Last edited on )
"; - $comment_text .= $append_updated; - } - } - return $comment_text; - } - }