From 5160bd12bf36c6f4e4650b593f522218f8f6058d Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Wed, 8 Jan 2025 09:24:40 -0600 Subject: [PATCH 1/3] Comments: Show @-mentions in line with reply --- includes/transformer/class-comment.php | 40 +++++++++---------- .../transformer/class-test-comment.php | 2 +- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/includes/transformer/class-comment.php b/includes/transformer/class-comment.php index f689b8cf2..bb522992c 100644 --- a/includes/transformer/class-comment.php +++ b/includes/transformer/class-comment.php @@ -53,25 +53,8 @@ public function change_wp_user_id( $user_id ) { * @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( - '%s ', - esc_url( $url ), - esc_html( $acct ) - ); - } - - $at_replies = trim( $at_replies ); - - if ( $at_replies ) { - $content = sprintf( '

%s

%s', $at_replies, $content ); - } + $object = parent::to_object(); + $content = $this->get_content(); $object->set_content( $content ); $object->set_content_map( @@ -110,6 +93,21 @@ protected function get_content() { $comment = $this->wp_object; $content = $comment->comment_content; + $at_replies = ''; + $reply_context = $this->extract_reply_context(); + + foreach ( $reply_context as $acct => $url ) { + $at_replies .= sprintf( + '%s ', + esc_url( $url ), + esc_html( $acct ) + ); + } + + if ( $at_replies ) { + $content = $at_replies . $content; + } + /** * Filter the content of the comment. * @@ -258,11 +256,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; diff --git a/tests/includes/transformer/class-test-comment.php b/tests/includes/transformer/class-test-comment.php index a4a3efd73..258be48b1 100644 --- a/tests/includes/transformer/class-test-comment.php +++ b/tests/includes/transformer/class-test-comment.php @@ -89,7 +89,7 @@ public function test_content_with_reply_context() { $content = $object->get_content(); // Test that reply context is added. - $this->assertEquals( '

@remote@example.net @author@remote.example

This is a comment

', $content ); + $this->assertSame( '

@remote@example.net @author@remote.example This is a comment

', $content ); // Clean up. wp_delete_comment( $reply_comment_id, true ); From ef6c5c97370b71580b8806159c9f335973d53755 Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Wed, 8 Jan 2025 09:43:27 -0600 Subject: [PATCH 2/3] Changelog --- CHANGELOG.md | 1 + includes/transformer/class-comment.php | 17 ++++++----------- readme.txt | 1 + 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c2188304..f17b0f6c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ## [4.6.0] - 2024-12-20 diff --git a/includes/transformer/class-comment.php b/includes/transformer/class-comment.php index bb522992c..f4c2e6ff7 100644 --- a/includes/transformer/class-comment.php +++ b/includes/transformer/class-comment.php @@ -90,23 +90,18 @@ protected function get_attributed_to() { * @return string The content. */ protected function get_content() { - $comment = $this->wp_object; - $content = $comment->comment_content; - - $at_replies = ''; - $reply_context = $this->extract_reply_context(); + $comment = $this->wp_object; + $content = $comment->comment_content; + $mentions = ''; - foreach ( $reply_context as $acct => $url ) { - $at_replies .= sprintf( + foreach ( $this->extract_reply_context() as $acct => $url ) { + $mentions .= sprintf( '%s ', esc_url( $url ), esc_html( $acct ) ); } - - if ( $at_replies ) { - $content = $at_replies . $content; - } + $content = $mentions . $content; /** * Filter the content of the comment. diff --git a/readme.txt b/readme.txt index f13510122..ab45683b8 100644 --- a/readme.txt +++ b/readme.txt @@ -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 = 4.6.0 = From ef6ce3ca00f30a0a25b5dae46bcb3055c4cd63f6 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 10 Jan 2025 11:38:23 +0100 Subject: [PATCH 3/3] simplify code even more --- includes/transformer/class-comment.php | 34 +++++++------------ .../transformer/class-test-comment.php | 2 +- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/includes/transformer/class-comment.php b/includes/transformer/class-comment.php index f4c2e6ff7..9b1ec64ff 100644 --- a/includes/transformer/class-comment.php +++ b/includes/transformer/class-comment.php @@ -45,27 +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(); - - $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. * @@ -96,7 +75,7 @@ protected function get_content() { foreach ( $this->extract_reply_context() as $acct => $url ) { $mentions .= sprintf( - '%s ', + '%s ', esc_url( $url ), esc_html( $acct ) ); @@ -357,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(), + ); + } } diff --git a/tests/includes/transformer/class-test-comment.php b/tests/includes/transformer/class-test-comment.php index 258be48b1..f7dd29559 100644 --- a/tests/includes/transformer/class-test-comment.php +++ b/tests/includes/transformer/class-test-comment.php @@ -89,7 +89,7 @@ public function test_content_with_reply_context() { $content = $object->get_content(); // Test that reply context is added. - $this->assertSame( '

@remote@example.net @author@remote.example This is a comment

', $content ); + $this->assertSame( '

@remote@example.net @author@remote.example This is a comment

', $content ); // Clean up. wp_delete_comment( $reply_comment_id, true );