Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comments: Show @-mentions in line with reply #1137

Merged
merged 4 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

* Undefined array key warnings in various places
* @-mentions in federated comments being displayed with a line break
* Fetching replies from the same instance for Enable Mastodon Apps
* Image captions not being included in the ActivityPub representation when the image is attached to the post

Expand Down
67 changes: 25 additions & 42 deletions includes/transformer/class-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,44 +45,6 @@ public function change_wp_user_id( $user_id ) {
$this->wp_object->user_id = $user_id;
}

/**
* Transforms the WP_Comment object to an ActivityPub Object.
*
* @see \Activitypub\Activity\Base_Object
*
* @return \Activitypub\Activity\Base_Object The ActivityPub Object.
*/
public function to_object() {
$object = parent::to_object();

$content = $this->get_content();
$at_replies = '';
$reply_context = $this->extract_reply_context( array() );

foreach ( $reply_context as $acct => $url ) {
$at_replies .= sprintf(
'<a class="u-mention mention" href="%s">%s</a> ',
esc_url( $url ),
esc_html( $acct )
);
}

$at_replies = trim( $at_replies );

if ( $at_replies ) {
$content = sprintf( '<p>%s</p>%s', $at_replies, $content );
}

$object->set_content( $content );
$object->set_content_map(
array(
$this->get_locale() => $content,
)
);

return $object;
}

/**
* Returns the User-URL of the Author of the Post.
*
Expand All @@ -107,8 +69,18 @@ protected function get_attributed_to() {
* @return string The content.
*/
protected function get_content() {
$comment = $this->wp_object;
$content = $comment->comment_content;
$comment = $this->wp_object;
$content = $comment->comment_content;
$mentions = '';

foreach ( $this->extract_reply_context() as $acct => $url ) {
$mentions .= sprintf(
'<a rel="mention" class="u-url mention" href="%s">%s</a> ',
esc_url( $url ),
esc_html( $acct )
);
}
$content = $mentions . $content;

/**
* Filter the content of the comment.
Expand Down Expand Up @@ -258,11 +230,11 @@ function ( $comment_id ) {
* Collect all other Users that participated in this comment-thread
* to send them a notification about the new reply.
*
* @param array $mentions The already mentioned ActivityPub users.
* @param array $mentions Optional. The already mentioned ActivityPub users. Default empty array.
*
* @return array The list of all Repliers.
*/
public function extract_reply_context( $mentions ) {
public function extract_reply_context( $mentions = array() ) {
// Check if `$this->wp_object` is a WP_Comment.
if ( 'WP_Comment' !== get_class( $this->wp_object ) ) {
return $mentions;
Expand Down Expand Up @@ -364,4 +336,15 @@ public function get_to() {
get_rest_url_by_path( $path ),
);
}

/**
* Returns the content map for the comment.
*
* @return array The content map for the comment.
*/
public function get_content_map() {
return array(
$this->get_locale() => $this->get_content(),
);
}
}
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ For reasons of data protection, it is not possible to see the followers of other
* Added: A filter to make custom comment types manageable in WP.com Calypso
* Changed: Hide ActivityPub post meta keys from the custom Fields UI
* Fixed: Undefined array key warnings in various places
* Fixed: @-mentions in federated comments being displayed with a line break
* Fixed: Fetching replies from the same instance for Enable Mastodon Apps
* Fixed: Image captions not being included in the ActivityPub representation when the image is attached to the post

Expand Down
2 changes: 1 addition & 1 deletion tests/includes/transformer/class-test-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function test_content_with_reply_context() {
$content = $object->get_content();

// Test that reply context is added.
$this->assertEquals( '<p><a class="u-mention mention" href="https://example.net/@remote">@[email protected]</a> <a class="u-mention mention" href="https://remote.example/@author">@[email protected]</a></p><p>This is a comment</p>', $content );
$this->assertSame( '<p><a rel="mention" class="u-url mention" href="https://example.net/@remote">@[email protected]</a> <a rel="mention" class="u-url mention" href="https://remote.example/@author">@[email protected]</a> This is a comment</p>', $content );

// Clean up.
wp_delete_comment( $reply_comment_id, true );
Expand Down
Loading