From ff53ef2e9d75ce2482db2a8525fc13324d22b3cc Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Sun, 28 Oct 2018 13:18:08 -0400 Subject: [PATCH] Handle incoming update and delete activities with linked comments --- includes/commentlinks.php | 9 +++++++++ includes/server/activities/delete.php | 12 ++++++++++++ includes/server/activities/update.php | 27 +++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/includes/commentlinks.php b/includes/commentlinks.php index 13d0721..2b6c9da 100644 --- a/includes/commentlinks.php +++ b/includes/commentlinks.php @@ -38,4 +38,13 @@ function link_comment( $comment_id, $object_id ) { '%d' ); } + +function unlink_comment( $comment_id, $object_id ) { + global $wpdb; + return $wpdb->delete( + "{$wpdb->prefix}pterotype_comments", + array( 'comment_id' => $comment_id, 'object_id' => $object_id ), + '%d' + ); +} ?> diff --git a/includes/server/activities/delete.php b/includes/server/activities/delete.php index be43087..0a6eae9 100644 --- a/includes/server/activities/delete.php +++ b/includes/server/activities/delete.php @@ -3,6 +3,7 @@ namespace pterotype\activities\delete; require_once plugin_dir_path( __FILE__ ) . '../objects.php'; require_once plugin_dir_path( __FILE__ ) . '../actors.php'; +require_once plugin_dir_path( __FILE__ ) . '../../commentlinks.php'; function handle_outbox( $actor, $activity ) { if ( !array_key_exists( 'object', $activity ) ) { @@ -48,6 +49,7 @@ function handle_inbox( $actor_slug, $activity ) { if ( is_wp_error( $authorized ) ) { return $authorized; } + delete_linked_comment( $object ); $res = \pterotype\objects\delete_object( $object ); if ( is_wp_error( $res ) ) { return $res; @@ -55,6 +57,16 @@ function handle_inbox( $actor_slug, $activity ) { return $activity; } +function delete_linked_comment( $object ) { + $object_id = \pterotype\objects\get_object_id( $object['id'] ); + $comment_id = \pterotype\commentlinks\get_comment_id( $object_id ); + if ( ! $comment_id ) { + return; + } + \pterotype\commentlinks\unlink_comment( $comment_id, $object_id ); + \wp_delete_comment( $comment_id, true ); +} + function check_authorization( $activity ) { $object = $activity['object']; $activity_origin = parse_url( $activity['id'] )['host']; diff --git a/includes/server/activities/update.php b/includes/server/activities/update.php index 41cc6ce..c4fd783 100644 --- a/includes/server/activities/update.php +++ b/includes/server/activities/update.php @@ -2,6 +2,8 @@ namespace pterotype\activities\update; require_once plugin_dir_path( __FILE__ ) . '../objects.php'; +require_once plugin_dir_path( __FILE__ ) . '../../commentlinks.php'; +require_once plugin_dir_path( __FILE__ ) . 'create.php'; function handle_outbox( $actor_slug, $activity ) { if ( !(array_key_exists( 'type', $activity ) && $activity['type'] === 'Update') ) { @@ -76,6 +78,7 @@ function handle_inbox( $actor_slug, $activity ) { if ( is_wp_error( $object_row ) ) { return $object_row; } + update_linked_comment( $object_row->object ); return $activity; } @@ -105,4 +108,28 @@ function make_update( $actor_slug, $object ) { 'object' => $object ); } + +function update_linked_comment( $updated_object ) { + $object_id = \pterotype\objects\get_object_id( $updated_object['id'] ); + $comment_id = \pterotype\commentlinks\get_comment_id( $object_id ); + if ( ! $comment_id ) { + return; + } + $comment = \get_comment( $comment_id ); + if ( ! $comment || is_wp_error( $comment ) ) { + return; + } + $post_id = $comment->comment_post_ID; + $comment_parent = null; + if ( $comment->comment_parent !== '0' ) { + $comment_parent = $comment->comment_parent; + } + $updated_comment = \pterotype\activities\create\make_comment_from_object( + $updated_object, $post_id, $comment_parent + ); + $updated_comment['comment_ID'] = $comment->comment_ID; + if ( $comment != $updated_comment ) { + \wp_update_comment( $updated_comment ); + } +} ?>