Skip to content

Commit

Permalink
modularize retrieving of comment link via comment meta
Browse files Browse the repository at this point in the history
  • Loading branch information
Menrath committed Sep 23, 2024
1 parent 1a9e638 commit 769e434
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 31 deletions.
44 changes: 31 additions & 13 deletions includes/class-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,31 @@ public static function comment_class( $classes, $css_class, $comment_id ) {
return $classes;
}

/**
* Gets the public comment link (id/url) via the WordPress comments meta.
*
* If $prefer_id is true it prefers the 'source_id' meta-key over 'source_url'.
* If it is false, it's the other way round.
*
* @param int $wp_comment_id The internal WordPress comment ID.
* @param bool $prefer_id Whether to prefer the source_id comment meta field over the source_url one.
* @return string|null The ActivityPub id/url of the comment.
*/
public static function get_comment_link_from_meta( $wp_comment_id, $prefer_id = true ) {
$preferred_source = $prefer_id ? 'source_id' : 'source_url';
$fallback_source = $prefer_id ? 'source_url' : 'source_id';

$comment_meta = \get_comment_meta( $wp_comment_id );

if ( ! empty( $comment_meta[ $preferred_source ][0] ) ) {
return $comment_meta[ $preferred_source ][0];
} elseif ( ! empty( $comment_meta[ $fallback_source ][0] ) ) {
return $comment_meta[ $fallback_source ][0];
}

return null;
}

/**
* Link remote comments to source url.
*
Expand All @@ -353,15 +378,9 @@ public static function remote_comment_link( $comment_link, $comment ) {
return $comment_link;
}

$comment_meta = \get_comment_meta( $comment->comment_ID );
$public_comment_link = self::get_comment_link_from_meta( $comment->comment_ID, false);

if ( ! empty( $comment_meta['source_url'][0] ) ) {
return $comment_meta['source_url'][0];
} elseif ( ! empty( $comment_meta['source_id'][0] ) ) {
return $comment_meta['source_id'][0];
}

return $comment_link;
return $public_comment_link ?? $comment_link;
}


Expand All @@ -374,13 +393,12 @@ public static function remote_comment_link( $comment_link, $comment ) {
*/
public static function generate_id( $comment ) {
$comment = \get_comment( $comment );
$comment_meta = \get_comment_meta( $comment->comment_ID );

// show external comment ID if it exists
if ( ! empty( $comment_meta['source_id'][0] ) ) {
return $comment_meta['source_id'][0];
} elseif ( ! empty( $comment_meta['source_url'][0] ) ) {
return $comment_meta['source_url'][0];
$public_comment_link = self::get_comment_link_from_meta( $comment->comment_ID );

if ( $public_comment_link ) {
return $public_comment_link;
}

// generate URI based on comment ID
Expand Down
10 changes: 5 additions & 5 deletions includes/collection/class-replies.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use WP_Comment;
use WP_Error;

use Activitypub\Comment;

use function Activitypub\is_local_comment;
use function Activitypub\get_rest_url_by_path;

Expand Down Expand Up @@ -114,11 +116,9 @@ private static function get_reply_ids( $comments ) {
continue;
}

$comment_meta = \get_comment_meta( $comment->comment_ID );
if ( ! empty( $comment_meta['source_id'][0] ) ) {
$comment_ids[] = $comment_meta['source_id'][0];
} elseif ( ! empty( $comment_meta['source_url'][0] ) ) {
$comment_ids[] = $comment_meta['source_url'][0];
$public_comment_id = Comment::get_comment_link_from_meta( $comment->comment_ID );
if ( $public_comment_id ) {
$comment_ids[] = $public_comment_id;
}
}
return $comment_ids;
Expand Down
8 changes: 2 additions & 6 deletions includes/rest/class-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,9 @@ public static function remote_reply_get( WP_REST_Request $request ) {
return $template;
}

$comment_meta = \get_comment_meta( $comment_id );
$resource = Comment_Utils::get_comment_link_from_meta( $comment_id );

if ( ! empty( $comment_meta['source_id'][0] ) ) {
$resource = $comment_meta['source_id'][0];
} elseif ( ! empty( $comment_meta['source_url'][0] ) ) {
$resource = $comment_meta['source_url'][0];
} else {
if ( ! $resource ) {
$resource = Comment_Utils::generate_id( $comment );
}

Expand Down
9 changes: 2 additions & 7 deletions includes/transformer/class-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,8 @@ protected function get_in_reply_to() {
}

if ( $parent_comment ) {
$comment_meta = \get_comment_meta( $parent_comment->comment_ID );

if ( ! empty( $comment_meta['source_id'][0] ) ) {
$in_reply_to = $comment_meta['source_id'][0];
} elseif ( ! empty( $comment_meta['source_url'][0] ) ) {
$in_reply_to = $comment_meta['source_url'][0];
} elseif ( ! empty( $parent_comment->user_id ) ) {
$in_reply_to = Comment_Utils::get_comment_link_from_meta( $parent_comment->comment_ID );
if ( ! $in_reply_to && ! empty( $parent_comment->user_id ) ) {
$in_reply_to = Comment_Utils::generate_id( $parent_comment );
}
} else {
Expand Down

0 comments on commit 769e434

Please sign in to comment.