-
Notifications
You must be signed in to change notification settings - Fork 85
Fix rel="me" not being added to pre-linked Extra Fields URLs #2874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
8b496b6
b25a3ff
7d953d7
632ebc6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: patch | ||
| Type: fixed | ||
|
|
||
| Ensure Extra Fields links always include `rel="me"` for Mastodon verification. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -39,6 +39,92 @@ public function test_get_attachment() { | |||||||||||||||
| $this->assertEquals( 1, $value_count['me'] ); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
| * Test that pre-linked URLs get rel="me" added to both PropertyValue and Link. | ||||||||||||||||
| * | ||||||||||||||||
| * When users create extra fields in the block editor, URLs are often | ||||||||||||||||
| * already wrapped in <a> tags without rel="me". This test ensures | ||||||||||||||||
| * verification works by always adding "me" to the rel attribute in both | ||||||||||||||||
| * the PropertyValue HTML and the Link attachment. | ||||||||||||||||
| * | ||||||||||||||||
| * @covers ::fields_to_attachments | ||||||||||||||||
| * @covers ::add_rel_me_to_links | ||||||||||||||||
| */ | ||||||||||||||||
| public function test_prelinked_url_gets_rel_me() { | ||||||||||||||||
| $post = self::factory()->post->create_and_get( | ||||||||||||||||
| array( | ||||||||||||||||
| 'post_type' => Extra_Fields::BLOG_POST_TYPE, | ||||||||||||||||
| 'post_content' => '<a href="https://mastodon.social/@user">Mastodon</a>', | ||||||||||||||||
| 'post_title' => 'Mastodon', | ||||||||||||||||
| ) | ||||||||||||||||
| ); | ||||||||||||||||
|
|
||||||||||||||||
| $attachments = Extra_Fields::fields_to_attachments( array( $post ) ); | ||||||||||||||||
|
|
||||||||||||||||
| // The PropertyValue HTML should have rel="me" for verification. | ||||||||||||||||
| $this->assertEquals( 'PropertyValue', $attachments[0]['type'] ); | ||||||||||||||||
| $this->assertStringContainsString( 'rel="me"', $attachments[0]['value'] ); | ||||||||||||||||
|
|
||||||||||||||||
| // The Link attachment should also have rel="me". | ||||||||||||||||
| $this->assertEquals( 'Link', $attachments[1]['type'] ); | ||||||||||||||||
| $this->assertEquals( 'https://mastodon.social/@user', $attachments[1]['href'] ); | ||||||||||||||||
| $this->assertContains( 'me', $attachments[1]['rel'] ); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
| * Test that pre-linked URLs with existing rel attributes get "me" added. | ||||||||||||||||
| * | ||||||||||||||||
| * @covers ::fields_to_attachments | ||||||||||||||||
| * @covers ::add_rel_me_to_links | ||||||||||||||||
| */ | ||||||||||||||||
| public function test_prelinked_url_with_rel_gets_me_added() { | ||||||||||||||||
| $post = self::factory()->post->create_and_get( | ||||||||||||||||
| array( | ||||||||||||||||
| 'post_type' => Extra_Fields::BLOG_POST_TYPE, | ||||||||||||||||
| 'post_content' => '<a href="https://example.com" rel="nofollow noopener">Example</a>', | ||||||||||||||||
| 'post_title' => 'Example', | ||||||||||||||||
| ) | ||||||||||||||||
| ); | ||||||||||||||||
|
|
||||||||||||||||
| $attachments = Extra_Fields::fields_to_attachments( array( $post ) ); | ||||||||||||||||
|
|
||||||||||||||||
| // The PropertyValue HTML should have rel with "me" added. | ||||||||||||||||
| $this->assertEquals( 'PropertyValue', $attachments[0]['type'] ); | ||||||||||||||||
| $this->assertMatchesRegularExpression( '/rel="[^"]*me[^"]*"/', $attachments[0]['value'] ); | ||||||||||||||||
| $this->assertStringContainsString( 'nofollow', $attachments[0]['value'] ); | ||||||||||||||||
|
|
||||||||||||||||
| // The Link attachment should have original rel values plus "me". | ||||||||||||||||
| $this->assertEquals( 'Link', $attachments[1]['type'] ); | ||||||||||||||||
| $this->assertContains( 'nofollow', $attachments[1]['rel'] ); | ||||||||||||||||
| $this->assertContains( 'noopener', $attachments[1]['rel'] ); | ||||||||||||||||
| $this->assertContains( 'me', $attachments[1]['rel'] ); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
| * Test that pre-linked URLs with existing rel="me" don't get duplicate. | ||||||||||||||||
| * | ||||||||||||||||
| * @covers ::fields_to_attachments | ||||||||||||||||
| * @covers ::add_rel_me_to_links | ||||||||||||||||
| */ | ||||||||||||||||
| public function test_prelinked_url_with_rel_me_no_duplicate() { | ||||||||||||||||
| $post = self::factory()->post->create_and_get( | ||||||||||||||||
| array( | ||||||||||||||||
| 'post_type' => Extra_Fields::BLOG_POST_TYPE, | ||||||||||||||||
| 'post_content' => '<a href="https://example.com" rel="me nofollow">Example</a>', | ||||||||||||||||
| 'post_title' => 'Example', | ||||||||||||||||
| ) | ||||||||||||||||
| ); | ||||||||||||||||
|
|
||||||||||||||||
| $attachments = Extra_Fields::fields_to_attachments( array( $post ) ); | ||||||||||||||||
|
|
||||||||||||||||
| // PropertyValue should have "me" exactly once in the HTML. | ||||||||||||||||
| $this->assertEquals( 1, \substr_count( $attachments[0]['value'], ' me' ) + \substr_count( $attachments[0]['value'], '"me' ) ); | ||||||||||||||||
|
Comment on lines
+120
to
+121
|
||||||||||||||||
| // PropertyValue should have "me" exactly once in the HTML. | |
| $this->assertEquals( 1, \substr_count( $attachments[0]['value'], ' me' ) + \substr_count( $attachments[0]['value'], '"me' ) ); | |
| // PropertyValue should have "me" exactly once in the rel attribute. | |
| $rel_match = array(); | |
| \preg_match( '/\brel="([^"]*)"/', $attachments[0]['value'], $rel_match ); | |
| $rel_value = isset( $rel_match[1] ) ? $rel_match[1] : ''; | |
| $this->assertEquals( 1, \preg_match_all( '/\bme\b/', $rel_value ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rel attribute value should be trimmed before exploding to handle cases where the attribute has leading/trailing whitespace (e.g., rel=' nofollow noopener '). Without trimming, explode will create empty string elements that could cause issues. Use
trim( $rel )before splitting.