Fix rel="me" not being added to pre-linked Extra Fields URLs#2874
Fix rel="me" not being added to pre-linked Extra Fields URLs#2874
Conversation
When users create Extra Fields using the block editor, URLs are often already wrapped in <a> tags before the activitypub_link_rel filter runs. These pre-linked URLs bypassed the filter, resulting in no rel="me" attribute being added. This fix ensures all links in Extra Fields have rel="me" for proper Mastodon/fediverse verification by: 1. Adding a new add_rel_me_to_links() method that uses WP_HTML_Tag_Processor to inject "me" into the rel attribute of all <a> tags 2. Processing content through this method before creating attachments 3. Both PropertyValue HTML and Link attachments now include rel="me" Fixes #2832 Related to #1441
There was a problem hiding this comment.
Pull request overview
This PR fixes an issue where rel="me" attributes were not being added to Extra Fields URLs that were already wrapped in <a> tags (e.g., from the block editor), preventing proper Mastodon/fediverse verification.
Changes:
- Added
add_rel_me_to_links()method usingWP_HTML_Tag_Processorto inject "me" into rel attributes of existing links - Updated
fields_to_attachments()to process content through the new method before creating attachments - Added comprehensive test coverage for pre-linked URLs with various rel attribute scenarios
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| includes/collection/class-extra-fields.php | Implements new add_rel_me_to_links() method and integrates it into the attachment generation flow |
| tests/phpunit/tests/includes/collection/class-test-extra-fields.php | Adds three test cases covering pre-linked URLs without rel, with existing rel, and with existing rel="me" |
| .github/changelog/2874-from-description | Adds changelog entry documenting the bug fix |
| // PropertyValue should have "me" exactly once in the HTML. | ||
| $this->assertEquals( 1, \substr_count( $attachments[0]['value'], ' me' ) + \substr_count( $attachments[0]['value'], '"me' ) ); |
There was a problem hiding this comment.
This test logic is fragile and could produce false positives. It counts occurrences of ' me' and '"me' separately, which could match unintended strings like 'some' or 'home'. A more reliable approach would be to parse the rel attribute value and count 'me' as a distinct token within it, or use a regex that specifically matches 'me' as a word boundary within the rel attribute.
| // 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 ) ); |
|
|
||
| while ( $tags->next_tag( 'A' ) ) { | ||
| $rel = $tags->get_attribute( 'rel' ); | ||
| $rel_parts = $rel && \is_string( $rel ) ? \explode( ' ', $rel ) : array(); |
There was a problem hiding this comment.
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.
| $rel_parts = $rel && \is_string( $rel ) ? \explode( ' ', $rel ) : array(); | |
| $rel_parts = $rel && \is_string( $rel ) ? \explode( ' ', \trim( $rel ) ) : array(); |
Fixes #2832
Related to #1441
Proposed changes:
rel="me"not being added to Extra Fields links that are already wrapped in<a>tags (e.g., from the block editor)add_rel_me_to_links()method that usesWP_HTML_Tag_Processorto inject "me" into the rel attribute of all linksrel="me"for proper Mastodon/fediverse verificationBackground
When users create Extra Fields using the block editor, URLs are often already wrapped in
<a>tags before theactivitypub_link_relfilter runs. These pre-linked URLs bypassed the filter, resulting in norel="me"attribute being added.Before (broken):
{"type":"Link","name":"Blog","href":"https://example.com","rel":["nofollow","noopener"]}After (fixed):
{"type":"Link","name":"Blog","href":"https://example.com","rel":["nofollow","noopener","me"]}Other information:
Testing instructions:
<a>tag link)curl -H "Accept: application/activity+json" https://yoursite.com/@usernamerel: ["me"]in the Link type andrel="me"in the PropertyValue HTMLChangelog entry
Changelog Entry Details
Significance
Type
Message
Ensure Extra Fields links always include
rel="me"for Mastodon verification.