From 5dbf365c58e7ace682eb339ef4afcc927dd9cb16 Mon Sep 17 00:00:00 2001 From: Django Doucet Date: Fri, 18 Nov 2022 22:19:08 -0700 Subject: [PATCH] associate comments to back compat post --- includes/functions.php | 37 +++++++++++++++++++++++++++++++++++ includes/rest/class-inbox.php | 4 ++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 8f5108e..795de5b 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -356,6 +356,43 @@ function url_to_authorid( $url ) { return 0; } +/** + * Verify if in_replyto_url is a local Post, + * (For backwards compatibility) + * + * @param string activitypub object id URI + * @return int post_id + */ +function url_to_postid( $in_replyto_url ) { + if ( !empty( $in_replyto_url ) ) { + $tentative_postid = \url_to_postid( $in_replyto_url ); + if ( is_null( $tentative_postid ) ) { + $post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ); + $query_args = array( + 'type' => $post_types, + 'meta_query' => array( + array( + 'key' => '_activitypub_permalink_compat', + 'value' => $in_replyto_url, + ), + ), + ); + $posts_query = new \WP_Query(); + $posts = $posts_query->query( $query_args ); + $found_post_ids = array(); + if ( $posts ) { + foreach ( $posts as $post ) { + $found_post_ids[] = $post->comment_ID; + } + return $found_post_ids[0]; + } + } else { + return $tentative_postid; + } + } + return null; +} + /** * Verify if in_replyto_url is a local comment, * Or if it is a previously received remote comment diff --git a/includes/rest/class-inbox.php b/includes/rest/class-inbox.php index 2106b47..6d5f94a 100644 --- a/includes/rest/class-inbox.php +++ b/includes/rest/class-inbox.php @@ -415,7 +415,7 @@ class Inbox { $audience = \Activitypub\get_audience( $object ); if ( isset( $object['object']['inReplyTo'] ) ) { - $comment_parent_id = \Activitypub\url_to_commentid( \esc_url_raw( $object['object']['inReplyTo'] ) ); + $comment_parent_id = \Activitypub\url_to_commentid( \esc_url_raw( $object['object']['inReplyTo'] ) ); // Only checks ap_comment_id for local or source_url for remote if ( ! is_null( $comment_parent_id ) ) { //inReplyTo a known local comment @@ -423,7 +423,7 @@ class Inbox { $comment_post_id = $comment_parent->comment_post_ID; } else { //inReplyTo a known post - $comment_post_id = \url_to_postid( $object['object']['inReplyTo'] ); + $comment_post_id = \Activitypub\url_to_postid( $object['object']['inReplyTo'] ); } }