Skip to content

Commit 370d8c8

Browse files
obenlandpfefferle
andauthored
Comments: Show @-mentions in line with reply (#1137)
* Comments: Show @-mentions in line with reply * Changelog * simplify code even more --------- Co-authored-by: Matthias Pfefferle <[email protected]>
1 parent 8c3e052 commit 370d8c8

File tree

4 files changed

+28
-43
lines changed

4 files changed

+28
-43
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
### Fixed
2020

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

includes/transformer/class-comment.php

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -45,44 +45,6 @@ public function change_wp_user_id( $user_id ) {
4545
$this->wp_object->user_id = $user_id;
4646
}
4747

48-
/**
49-
* Transforms the WP_Comment object to an ActivityPub Object.
50-
*
51-
* @see \Activitypub\Activity\Base_Object
52-
*
53-
* @return \Activitypub\Activity\Base_Object The ActivityPub Object.
54-
*/
55-
public function to_object() {
56-
$object = parent::to_object();
57-
58-
$content = $this->get_content();
59-
$at_replies = '';
60-
$reply_context = $this->extract_reply_context( array() );
61-
62-
foreach ( $reply_context as $acct => $url ) {
63-
$at_replies .= sprintf(
64-
'<a class="u-mention mention" href="%s">%s</a> ',
65-
esc_url( $url ),
66-
esc_html( $acct )
67-
);
68-
}
69-
70-
$at_replies = trim( $at_replies );
71-
72-
if ( $at_replies ) {
73-
$content = sprintf( '<p>%s</p>%s', $at_replies, $content );
74-
}
75-
76-
$object->set_content( $content );
77-
$object->set_content_map(
78-
array(
79-
$this->get_locale() => $content,
80-
)
81-
);
82-
83-
return $object;
84-
}
85-
8648
/**
8749
* Returns the User-URL of the Author of the Post.
8850
*
@@ -107,8 +69,18 @@ protected function get_attributed_to() {
10769
* @return string The content.
10870
*/
10971
protected function get_content() {
110-
$comment = $this->wp_object;
111-
$content = $comment->comment_content;
72+
$comment = $this->wp_object;
73+
$content = $comment->comment_content;
74+
$mentions = '';
75+
76+
foreach ( $this->extract_reply_context() as $acct => $url ) {
77+
$mentions .= sprintf(
78+
'<a rel="mention" class="u-url mention" href="%s">%s</a> ',
79+
esc_url( $url ),
80+
esc_html( $acct )
81+
);
82+
}
83+
$content = $mentions . $content;
11284

11385
/**
11486
* Filter the content of the comment.
@@ -258,11 +230,11 @@ function ( $comment_id ) {
258230
* Collect all other Users that participated in this comment-thread
259231
* to send them a notification about the new reply.
260232
*
261-
* @param array $mentions The already mentioned ActivityPub users.
233+
* @param array $mentions Optional. The already mentioned ActivityPub users. Default empty array.
262234
*
263235
* @return array The list of all Repliers.
264236
*/
265-
public function extract_reply_context( $mentions ) {
237+
public function extract_reply_context( $mentions = array() ) {
266238
// Check if `$this->wp_object` is a WP_Comment.
267239
if ( 'WP_Comment' !== get_class( $this->wp_object ) ) {
268240
return $mentions;
@@ -364,4 +336,15 @@ public function get_to() {
364336
get_rest_url_by_path( $path ),
365337
);
366338
}
339+
340+
/**
341+
* Returns the content map for the comment.
342+
*
343+
* @return array The content map for the comment.
344+
*/
345+
public function get_content_map() {
346+
return array(
347+
$this->get_locale() => $this->get_content(),
348+
);
349+
}
367350
}

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ For reasons of data protection, it is not possible to see the followers of other
138138
* Changed: Hide ActivityPub post meta keys from the custom Fields UI
139139
* Changed: Bumped minimum required PHP version to 7.2
140140
* Fixed: Undefined array key warnings in various places
141+
* Fixed: @-mentions in federated comments being displayed with a line break
141142
* Fixed: Fetching replies from the same instance for Enable Mastodon Apps
142143
* Fixed: Image captions not being included in the ActivityPub representation when the image is attached to the post
143144

tests/includes/transformer/class-test-comment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function test_content_with_reply_context() {
8989
$content = $object->get_content();
9090

9191
// Test that reply context is added.
92-
$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 );
92+
$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 );
9393

9494
// Clean up.
9595
wp_delete_comment( $reply_comment_id, true );

0 commit comments

Comments
 (0)