Skip to content

Commit

Permalink
Comments: Show @-mentions in line with reply (#1137)
Browse files Browse the repository at this point in the history
* Comments: Show @-mentions in line with reply

* Changelog

* simplify code even more

---------

Co-authored-by: Matthias Pfefferle <[email protected]>
  • Loading branch information
obenland and pfefferle authored Jan 10, 2025
1 parent 8c3e052 commit 370d8c8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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 @@ -138,6 +138,7 @@ For reasons of data protection, it is not possible to see the followers of other
* Changed: Hide ActivityPub post meta keys from the custom Fields UI
* Changed: Bumped minimum required PHP version to 7.2
* 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

0 comments on commit 370d8c8

Please sign in to comment.