diff --git a/.github/changelog/fix-mention-summary-map b/.github/changelog/fix-mention-summary-map new file mode 100644 index 0000000000..d8be9e3647 --- /dev/null +++ b/.github/changelog/fix-mention-summary-map @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Mentions, hashtags and links are now linked the same way in every language version of a post or profile. diff --git a/includes/class-hashtag.php b/includes/class-hashtag.php index 9115dc8c58..95f991b0d3 100644 --- a/includes/class-hashtag.php +++ b/includes/class-hashtag.php @@ -42,6 +42,15 @@ public static function filter_activity_object( $activity ) { $activity['content'] = self::the_content( $activity['content'] ); } + // The maps hold the same text in other languages, so they get the same links. + if ( ! empty( $activity['summaryMap'] ) && \is_array( $activity['summaryMap'] ) && is_actor( $activity ) ) { + $activity['summaryMap'] = \array_map( array( self::class, 'the_content' ), $activity['summaryMap'] ); + } + + if ( ! empty( $activity['contentMap'] ) && \is_array( $activity['contentMap'] ) ) { + $activity['contentMap'] = \array_map( array( self::class, 'the_content' ), $activity['contentMap'] ); + } + return $activity; } diff --git a/includes/class-link.php b/includes/class-link.php index 1bf7da83f2..38b13db79f 100644 --- a/includes/class-link.php +++ b/includes/class-link.php @@ -38,6 +38,15 @@ public static function filter_activity_object( $activity ) { $activity['content'] = self::the_content( $activity['content'] ); } + // The maps hold the same text in other languages, so they get the same links. + if ( ! empty( $activity['summaryMap'] ) && \is_array( $activity['summaryMap'] ) && is_actor( $activity ) ) { + $activity['summaryMap'] = \array_map( array( self::class, 'the_content' ), $activity['summaryMap'] ); + } + + if ( ! empty( $activity['contentMap'] ) && \is_array( $activity['contentMap'] ) ) { + $activity['contentMap'] = \array_map( array( self::class, 'the_content' ), $activity['contentMap'] ); + } + return $activity; } diff --git a/includes/class-mention.php b/includes/class-mention.php index c05c6bd29a..c82bf343c4 100644 --- a/includes/class-mention.php +++ b/includes/class-mention.php @@ -47,6 +47,15 @@ public static function filter_activity_object( $object_array ) { $object_array['content'] = self::the_content( $object_array['content'] ); } + // The maps hold the same text in other languages, so they get the same links. + if ( ! empty( $object_array['summaryMap'] ) && \is_array( $object_array['summaryMap'] ) ) { + $object_array['summaryMap'] = \array_map( array( self::class, 'the_content' ), $object_array['summaryMap'] ); + } + + if ( ! empty( $object_array['contentMap'] ) && \is_array( $object_array['contentMap'] ) ) { + $object_array['contentMap'] = \array_map( array( self::class, 'the_content' ), $object_array['contentMap'] ); + } + return $object_array; } diff --git a/tests/phpunit/tests/includes/class-test-hashtag.php b/tests/phpunit/tests/includes/class-test-hashtag.php index 86e1a4026f..f0ef1f1d26 100644 --- a/tests/phpunit/tests/includes/class-test-hashtag.php +++ b/tests/phpunit/tests/includes/class-test-hashtag.php @@ -465,4 +465,40 @@ private function disable_hashtags() { \remove_filter( 'the_content', array( \Activitypub\Hashtag::class, 'the_content' ) ); \remove_filter( 'activitypub_activity_object_array', array( \Activitypub\Hashtag::class, 'filter_activity_object' ), 99 ); } + + /** + * The language maps get the same hashtag links as `summary` and `content`. + * + * @covers ::filter_activity_object + */ + public function test_filter_activity_object_links_language_maps() { + \wp_create_term( 'object', 'post_tag' ); + $link = \get_term_link( \get_term_by( 'name', 'object', 'post_tag' ), 'post_tag' ); + + $fields = array( + 'summary' => 'Hello #object', + 'summaryMap' => array( + 'en' => 'Hello #object', + 'de' => 'Hallo #object', + ), + 'content' => '
Hello #object
', + 'contentMap' => array( + 'en' => 'Hello #object
', + 'de' => 'Hallo #object
', + ), + ); + + $actor = \Activitypub\Hashtag::filter_activity_object( \array_merge( array( 'type' => 'Person' ), $fields ) ); + + $this->assertSame( $actor['summary'], $actor['summaryMap']['en'] ); + $this->assertSame( $actor['content'], $actor['contentMap']['en'] ); + $this->assertStringContainsString( \sprintf( 'href="%s"', $link ), $actor['summaryMap']['de'] ); + $this->assertStringContainsString( \sprintf( 'href="%s"', $link ), $actor['contentMap']['de'] ); + + // The summary of a post stays plain text, in every language. + $note = \Activitypub\Hashtag::filter_activity_object( \array_merge( array( 'type' => 'Note' ), $fields ) ); + + $this->assertSame( $fields['summaryMap'], $note['summaryMap'] ); + $this->assertSame( $note['content'], $note['contentMap']['en'] ); + } } diff --git a/tests/phpunit/tests/includes/class-test-link.php b/tests/phpunit/tests/includes/class-test-link.php index 3a6640a6b6..b99c92efdd 100644 --- a/tests/phpunit/tests/includes/class-test-link.php +++ b/tests/phpunit/tests/includes/class-test-link.php @@ -113,4 +113,37 @@ private function create_url_post() { ) ); } + + /** + * The language maps get the same URL links as `summary` and `content`. + * + * @covers ::filter_activity_object + */ + public function test_filter_activity_object_links_language_maps() { + $fields = array( + 'summary' => 'see https://example.com/page', + 'summaryMap' => array( + 'en' => 'see https://example.com/page', + 'de' => 'siehe https://example.com/page', + ), + 'content' => 'see https://example.com/page
', + 'contentMap' => array( + 'en' => 'see https://example.com/page
', + 'de' => 'siehe https://example.com/page
', + ), + ); + + $actor = \Activitypub\Link::filter_activity_object( \array_merge( array( 'type' => 'Person' ), $fields ) ); + + $this->assertSame( $actor['summary'], $actor['summaryMap']['en'] ); + $this->assertSame( $actor['content'], $actor['contentMap']['en'] ); + $this->assertStringContainsString( 'href="https://example.com/page"', $actor['summaryMap']['de'] ); + $this->assertStringContainsString( 'href="https://example.com/page"', $actor['contentMap']['de'] ); + + // The summary of a post stays plain text, in every language. + $note = \Activitypub\Link::filter_activity_object( \array_merge( array( 'type' => 'Note' ), $fields ) ); + + $this->assertSame( $fields['summaryMap'], $note['summaryMap'] ); + $this->assertSame( $note['content'], $note['contentMap']['en'] ); + } } diff --git a/tests/phpunit/tests/includes/class-test-mention.php b/tests/phpunit/tests/includes/class-test-mention.php index f24854393b..7925cecdf0 100644 --- a/tests/phpunit/tests/includes/class-test-mention.php +++ b/tests/phpunit/tests/includes/class-test-mention.php @@ -475,4 +475,32 @@ public function test_extract_mention_links_accepts_rel_mention() { $this->assertSame( array( 'https://mention.example/@alice' ), Mention::extract_mention_links( $content ) ); } + + /** + * The language maps get the same mention links as `summary` and `content`. + * + * @covers ::filter_activity_object + */ + public function test_filter_activity_object_links_language_maps() { + $object = Mention::filter_activity_object( + array( + 'type' => 'Note', + 'summary' => 'Hello @username@example.org', + 'summaryMap' => array( + 'en' => 'Hello @username@example.org', + 'de' => 'Hallo @username@example.org', + ), + 'content' => 'Hello @username@example.org
', + 'contentMap' => array( + 'en' => 'Hello @username@example.org
', + 'de' => 'Hallo @username@example.org
', + ), + ) + ); + + $this->assertSame( $object['summary'], $object['summaryMap']['en'] ); + $this->assertSame( $object['content'], $object['contentMap']['en'] ); + $this->assertStringContainsString( 'href="https://example.org/users/username"', $object['summaryMap']['de'] ); + $this->assertStringContainsString( 'href="https://example.org/users/username"', $object['contentMap']['de'] ); + } }