Skip to content
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
4 changes: 4 additions & 0 deletions .github/changelog/fix-mention-summary-map
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 9 additions & 0 deletions includes/class-hashtag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
9 changes: 9 additions & 0 deletions includes/class-link.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
9 changes: 9 additions & 0 deletions includes/class-mention.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
36 changes: 36 additions & 0 deletions tests/phpunit/tests/includes/class-test-hashtag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<p>Hello #object</p>',
'contentMap' => array(
'en' => '<p>Hello #object</p>',
'de' => '<p>Hallo #object</p>',
),
);

$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'] );
}
}
33 changes: 33 additions & 0 deletions tests/phpunit/tests/includes/class-test-link.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<p>see https://example.com/page</p>',
'contentMap' => array(
'en' => '<p>see https://example.com/page</p>',
'de' => '<p>siehe https://example.com/page</p>',
),
);

$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'] );
}
}
28 changes: 28 additions & 0 deletions tests/phpunit/tests/includes/class-test-mention.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<p>Hello @username@example.org</p>',
'contentMap' => array(
'en' => '<p>Hello @username@example.org</p>',
'de' => '<p>Hallo @username@example.org</p>',
),
)
);

$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'] );
}
}
Loading