Skip to content

Commit fad2f32

Browse files
authored
Extra Fields: Fix rel me count (#1241)
* Blog Model: Add failing test * Move to Extra Fields test * Remove rel me filter after using it * Add changelog * Skip tests for oder versions of WP
1 parent 708c592 commit fad2f32

File tree

4 files changed

+62
-12
lines changed

4 files changed

+62
-12
lines changed

CHANGELOG.md

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

1616
* Handle deletes from remote servers that leave behind an accessible Tombstone object.
1717
* No longer parses tags for post types that don't support Activitypub.
18+
* rel attribute will now contain no more than one "me" value.
1819

1920
## [4.7.3] - 2025-01-21
2021

includes/collection/class-extra-fields.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,7 @@ public static function get_formatted_content( $post ) {
9393
*/
9494
public static function fields_to_attachments( $fields ) {
9595
$attachments = array();
96-
\add_filter(
97-
'activitypub_link_rel',
98-
function ( $rel ) {
99-
$rel .= ' me';
100-
101-
return $rel;
102-
}
103-
);
96+
\add_filter( 'activitypub_link_rel', array( self::class, 'add_rel_me' ) );
10497

10598
foreach ( $fields as $post ) {
10699
$content = self::get_formatted_content( $post );
@@ -114,7 +107,7 @@ function ( $rel ) {
114107
),
115108
);
116109

117-
$link_added = false;
110+
$attachment = false;
118111

119112
// Add support for FEP-fb2a, for more information see FEDERATION.md.
120113
$link_content = \trim( \strip_tags( $content, '<a>' ) );
@@ -139,12 +132,10 @@ function ( $rel ) {
139132
if ( $rel && \is_string( $rel ) ) {
140133
$attachment['rel'] = \explode( ' ', $rel );
141134
}
142-
143-
$link_added = true;
144135
}
145136
}
146137

147-
if ( ! $link_added ) {
138+
if ( ! $attachment ) {
148139
$attachment = array(
149140
'type' => 'Note',
150141
'name' => \get_the_title( $post ),
@@ -159,6 +150,8 @@ function ( $rel ) {
159150
$attachments[] = $attachment;
160151
}
161152

153+
\remove_filter( 'activitypub_link_rel', array( self::class, 'add_rel_me' ) );
154+
162155
return $attachments;
163156
}
164157

@@ -285,6 +278,16 @@ public static function make_paragraph_block( $content ) {
285278
return '<!-- wp:paragraph --><p>' . $content . '</p><!-- /wp:paragraph -->';
286279
}
287280

281+
/**
282+
* Add the 'me' rel to the link.
283+
*
284+
* @param string $rel The rel attribute.
285+
* @return string The modified rel attribute.
286+
*/
287+
public static function add_rel_me( $rel ) {
288+
return $rel . ' me';
289+
}
290+
288291
/**
289292
* Checks if the user is the blog user.
290293
*

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: Improved content negotiation and AUTHORIZED_FETCH support for third-party plugins
139139
* Fixed: Handle deletes from remote servers that leave behind an accessible Tombstone object.
140140
* Fixed: No longer parses tags for post types that don't support Activitypub.
141+
* Fixed: rel attribute will now contain no more than one "me" value.
141142

142143
= 4.7.3 =
143144

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Test file for Extra Fields.
4+
*
5+
* @package Activitypub
6+
*/
7+
8+
namespace Activitypub\Tests\Collection;
9+
10+
use Activitypub\Collection\Extra_Fields;
11+
12+
/**
13+
* Test class for Extra Fields.
14+
*
15+
* @coversDefaultClass \Activitypub\Collection\Extra_Fields
16+
*/
17+
class Test_Extra_Fields extends \WP_UnitTestCase {
18+
19+
/**
20+
* Test the get_attachment.
21+
*
22+
* @covers ::get_attachment
23+
*/
24+
public function test_get_attachment() {
25+
if ( ! class_exists( '\WP_HTML_Tag_Processor' ) ) {
26+
$this->markTestSkipped( 'WP_HTML_Tag_Processor not available' );
27+
}
28+
29+
$post = self::factory()->post->create_and_get(
30+
array(
31+
'post_type' => Extra_Fields::BLOG_POST_TYPE,
32+
'post_content' => 'https://wordpress.org/plugins/activitypub/',
33+
'post_title' => 'ActivityPub',
34+
)
35+
);
36+
37+
// Multiple calls should not result in multiple "me" values in rel attribute.
38+
Extra_Fields::fields_to_attachments( array( $post ) );
39+
Extra_Fields::fields_to_attachments( array( $post ) );
40+
$attachments = Extra_Fields::fields_to_attachments( array( $post ) );
41+
$value_count = array_count_values( $attachments[1]['rel'] );
42+
43+
$this->assertEquals( 1, $value_count['me'] );
44+
}
45+
}

0 commit comments

Comments
 (0)