Comments: Add rel="nofollow ugc" attribute when converting plain URLs to <a> tags in comments via make_clickable().

Introduce `make_clickable_rel` filter for the `rel` value that is added to URL matches converted to links.

This is a follow-up to [46349], which added the `rel="nofollow ugc"` attribute to existing `<a>` tags in comments via `wp_rel_ugc()`.

UGC stands for User Generated Content, and the `ugc` attribute value is recommended for links within user generated content, such as comments and forum posts.

See https://webmasters.googleblog.com/2019/09/evolving-nofollow-new-ways-to-identify.html.

Props blogginglife, SergeyBiryukov.
Reviewed by desrosj, audrasjb.
Fixes #48022.

git-svn-id: https://develop.svn.wordpress.org/trunk@46564 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2019-10-21 20:39:16 +00:00
parent 56aa018065
commit 1f7f0a1357
2 changed files with 62 additions and 4 deletions

View File

@ -2835,7 +2835,24 @@ function _make_url_clickable_cb( $matches ) {
return $matches[0];
}
return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>" . $suffix;
if ( 'comment_text' === current_filter() ) {
$rel = 'nofollow ugc';
} else {
$rel = 'nofollow';
}
/**
* Filters the rel value that is added to URL matches converted to links.
*
* @since 5.3.0
*
* @param string $rel The rel value.
* @param string $url The matched URL being converted to a link tag.
*/
$rel = apply_filters( 'make_clickable_rel', $rel, $url );
$rel = esc_attr( $rel );
return $matches[1] . "<a href=\"$url\" rel=\"$rel\">$url</a>" . $suffix;
}
/**
@ -2865,7 +2882,17 @@ function _make_web_ftp_clickable_cb( $matches ) {
return $matches[0];
}
return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>$ret";
if ( 'comment_text' === current_filter() ) {
$rel = 'nofollow ugc';
} else {
$rel = 'nofollow';
}
/** This filter is documented in wp-includes/formatting.php */
$rel = apply_filters( 'make_clickable_rel', $rel, $dest );
$rel = esc_attr( $rel );
return $matches[1] . "<a href=\"$dest\" rel=\"$rel\">$dest</a>$ret";
}
/**
@ -3148,7 +3175,7 @@ function wp_targeted_link_rel_callback( $matches ) {
*
* @since 5.1.0
*
* @param string The rel values.
* @param string $rel The rel values.
* @param string $link_html The matched content of the link tag including all HTML attributes.
*/
$rel = apply_filters( 'wp_targeted_link_rel', 'noopener noreferrer', $link_html );

View File

@ -375,8 +375,8 @@ class Tests_Formatting_MakeClickable extends WP_UnitTestCase {
}
/**
* @dataProvider data_script_and_style_tags
* @ticket 30162
* @dataProvider data_script_and_style_tags
*/
public function test_dont_link_script_and_style_tags( $tag ) {
$this->assertEquals( $tag, make_clickable( $tag ) );
@ -399,4 +399,35 @@ class Tests_Formatting_MakeClickable extends WP_UnitTestCase {
);
}
/**
* @ticket 48022
* @dataProvider data_add_rel_ugc_in_comments
*/
public function test_add_rel_ugc_in_comments( $content, $expected ) {
$comment_id = self::factory()->comment->create(
array(
'comment_content' => $content,
)
);
ob_start();
comment_text( $comment_id );
$comment_text = ob_get_clean();
$this->assertContains( $expected, make_clickable( $comment_text ) );
}
public function data_add_rel_ugc_in_comments() {
return array(
array(
'http://wordpress.org',
'<a href="http://wordpress.org" rel="nofollow ugc">http://wordpress.org</a>',
),
array(
'www.wordpress.org',
'<p><a href="http://www.wordpress.org" rel="nofollow ugc">http://www.wordpress.org</a>',
),
);
}
}