From d7c9e10c21e81a01ceec57068cdbc4f5c2bd07ec Mon Sep 17 00:00:00 2001 From: Django Doucet Date: Tue, 15 Nov 2022 12:36:24 -0700 Subject: [PATCH] Separate file for Comment processing hooks --- activitypub.php | 3 + includes/class-activitypub.php | 106 ---------------------------- includes/class-comments.php | 123 +++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 106 deletions(-) create mode 100644 includes/class-comments.php diff --git a/activitypub.php b/activitypub.php index ed10429..b05f8b9 100644 --- a/activitypub.php +++ b/activitypub.php @@ -42,6 +42,9 @@ function init() { require_once \dirname( __FILE__ ) . '/includes/class-activitypub.php'; \Activitypub\Activitypub::init(); + require_once \dirname( __FILE__ ) . '/includes/class-comments.php'; + \Activitypub\Comments::init(); + // Configure the REST API route require_once \dirname( __FILE__ ) . '/includes/rest/class-outbox.php'; \Activitypub\Rest\Outbox::init(); diff --git a/includes/class-activitypub.php b/includes/class-activitypub.php index 68dabdd..d01c9ee 100644 --- a/includes/class-activitypub.php +++ b/includes/class-activitypub.php @@ -25,13 +25,6 @@ class Activitypub { \add_action( 'transition_post_status', array( '\Activitypub\Activitypub', 'schedule_post_activity' ), 10, 3 ); - \add_filter( 'preprocess_comment', array( '\Activitypub\Activitypub', 'preprocess_comment' ) ); - \add_filter( 'comment_post', array( '\Activitypub\Activitypub', 'postprocess_comment' ), 10, 3 ); - \add_filter( 'wp_update_comment_data', array( '\Activitypub\Activitypub', 'comment_updated_published' ), 20, 3 ); - \add_action( 'transition_comment_status', array( '\Activitypub\Activitypub', 'schedule_comment_activity' ), 20, 3 ); - \add_action( 'edit_comment', array( '\Activitypub\Activitypub', 'edit_comment' ), 20, 2 );//schedule_admin_comment_activity - \add_filter( 'get_comment_text', array( '\Activitypub\Activitypub', 'comment_append_edit_datetime' ), 10, 3 ); - } /** @@ -102,7 +95,6 @@ class Activitypub { //Collections review $vars[] = 'replies'; $vars[] = 'collection_page'; - //$vars[] = 'only_other_accounts'; return $vars; } @@ -144,104 +136,6 @@ class Activitypub { } } - /** - * preprocess local comments for federated replies - */ - public static function preprocess_comment( $commentdata ) { - // only process replies from local actors - if ( ! empty( $commentdata['user_id'] ) ) { - $commentdata['comment_type'] = 'activitypub'; - // 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']; - } - return $commentdata; - } - - /** - * comment_post() - * postprocess_comment for federating replies and inbox-forwarding - */ - public static function postprocess_comment( $comment_id, $comment_approved, $commentdata ) { - //Admin users comments bypass transition_comment_status (auto approved) - $user = \get_userdata( $commentdata['user_id'] ); - if ( 'activitypub' === $commentdata['comment_type'] ) { - if ( - ( 1 === $comment_approved ) && - ! empty( $commentdata['user_id'] ) && - \in_array( 'administrator', $user->roles ) - ) { - // Only for Admins - $mentions = \get_comment_meta( $comment_id, 'mentions', true ); - \wp_schedule_single_event( \time(), 'activitypub_send_comment_activity', array( $comment_id ) ); - } - } - } - - /** - * edit_comment() - * - * Fires immediately after a comment is updated in the database. - * Fires immediately before comment status transition hooks are fired. (useful only for admin) - */ - public static function edit_comment( $comment_id, $data ) { - if ( ! is_null( $data['user_id'] ) ) { - \wp_schedule_single_event( \time(), 'activitypub_send_update_comment_activity', array( $comment_id ) ); - } - } - - /** - * Schedule 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 ) { - //should only federate replies from local actors - //should only federate replies to federated 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_comment_activity', array( $activitypub_comment->comment_ID ) ); - } else { - $local_user = \get_author_posts_url( $ap_object['user_id'] ); - if ( ! is_null( $local_user ) ) { - if ( in_array( $local_user, $ap_object['to'] ) - || in_array( $local_user, $ap_object['cc'] ) - || in_array( $local_user, $ap_object['audience'] ) - || in_array( $local_user, $ap_object['tag'] ) - ) { - //if inReplyTo, object, target and/or tag are (local-wp) objects - \wp_schedule_single_event( \time(), 'activitypub_inbox_forward_activity', array( $activitypub_comment->comment_ID ) ); - } - } - } - } elseif ( 'trash' === $new_status ) { - \wp_schedule_single_event( \time(), 'activitypub_send_delete_comment_activity', array( $activitypub_comment ) ); - } elseif ( $old_status === $new_status ) { - //TODO Test with non-admin user - \wp_schedule_single_event( \time(), 'activitypub_send_update_comment_activity', array( $activitypub_comment->comment_ID ) ); - } - } - - /** - * 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; - } - /** * Replaces the default avatar. * diff --git a/includes/class-comments.php b/includes/class-comments.php new file mode 100644 index 0000000..e477a3b --- /dev/null +++ b/includes/class-comments.php @@ -0,0 +1,123 @@ +roles ) + ) { + // Only for Admins + $mentions = \get_comment_meta( $comment_id, 'mentions', true ); + \wp_schedule_single_event( \time(), 'activitypub_send_comment_activity', array( $comment_id ) ); + } + } + } + + /** + * edit_comment() + * + * Fires immediately after a comment is updated in the database. + * Fires immediately before comment status transition hooks are fired. (useful only for admin) + */ + public static function edit_comment( $comment_id, $data ) { + if ( ! is_null( $data['user_id'] ) ) { + \wp_schedule_single_event( \time(), 'activitypub_send_update_comment_activity', array( $comment_id ) ); + } + } + + /** + * Schedule 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 ) { + //should only federate replies from local actors + //should only federate replies to federated 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_comment_activity', array( $activitypub_comment->comment_ID ) ); + } else { + $local_user = \get_author_posts_url( $ap_object['user_id'] ); + if ( ! is_null( $local_user ) ) { + if ( in_array( $local_user, $ap_object['to'] ) + || in_array( $local_user, $ap_object['cc'] ) + || in_array( $local_user, $ap_object['audience'] ) + || in_array( $local_user, $ap_object['tag'] ) + ) { + //if inReplyTo, object, target and/or tag are (local-wp) objects + \wp_schedule_single_event( \time(), 'activitypub_inbox_forward_activity', array( $activitypub_comment->comment_ID ) ); + } + } + } + } elseif ( 'trash' === $new_status ) { + \wp_schedule_single_event( \time(), 'activitypub_send_delete_comment_activity', array( $activitypub_comment ) ); + } elseif ( $old_status === $new_status ) { + //TODO Test with non-admin user + \wp_schedule_single_event( \time(), 'activitypub_send_update_comment_activity', array( $activitypub_comment->comment_ID ) ); + } + } + + /** + * 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; + } + +}