Comments: Improve rel attribute usage in comments.

Internal links should be followed and it should be easier to modify other rel attributes on comments. This adds a helper function for determining if a URL is internal and also adds some new filters to make it easy to modify rel attributes in comments.

Props thomasplevy, desrosj, sabernhardt, benish74, samiamnot, galbaras, jorbin.

Fixes #53290, #56444.


git-svn-id: https://develop.svn.wordpress.org/trunk@55289 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Aaron Jorbin
2023-02-07 18:52:24 +00:00
parent b1f084389c
commit ad2ba3ed0d
7 changed files with 184 additions and 136 deletions

View File

@@ -108,12 +108,12 @@ class Tests_Formatting_MakeClickable extends WP_UnitTestCase {
'There was a spoon named www.wordpress.org) said Alice.',
);
$urls_expected = array(
'<a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>',
'There was a spoon named <a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>. Alice!',
'There was a spoon named <a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>, said Alice.',
'There was a spoon named <a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>; said Alice.',
'There was a spoon named <a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>: said Alice.',
'There was a spoon named <a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>) said Alice.',
"<a href='http://www.wordpress.org' rel=\"nofollow\">http://www.wordpress.org</a>",
"There was a spoon named <a href='http://www.wordpress.org' rel=\"nofollow\">http://www.wordpress.org</a>. Alice!",
"There was a spoon named <a href='http://www.wordpress.org' rel=\"nofollow\">http://www.wordpress.org</a>, said Alice.",
"There was a spoon named <a href='http://www.wordpress.org' rel=\"nofollow\">http://www.wordpress.org</a>; said Alice.",
"There was a spoon named <a href='http://www.wordpress.org' rel=\"nofollow\">http://www.wordpress.org</a>: said Alice.",
"There was a spoon named <a href='http://www.wordpress.org' rel=\"nofollow\">http://www.wordpress.org</a>) said Alice.",
);
foreach ( $urls_before as $key => $url ) {
@@ -135,12 +135,12 @@ class Tests_Formatting_MakeClickable extends WP_UnitTestCase {
'There was a spoon named www.wordpress.org)',
);
$urls_expected = array(
'<a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>',
'There was a spoon named <a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>.',
'There was a spoon named <a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>,',
'There was a spoon named <a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>;',
'There was a spoon named <a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>:',
'There was a spoon named <a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>)',
"<a href='http://www.wordpress.org' rel=\"nofollow\">http://www.wordpress.org</a>",
"There was a spoon named <a href='http://www.wordpress.org' rel=\"nofollow\">http://www.wordpress.org</a>.",
"There was a spoon named <a href='http://www.wordpress.org' rel=\"nofollow\">http://www.wordpress.org</a>,",
"There was a spoon named <a href='http://www.wordpress.org' rel=\"nofollow\">http://www.wordpress.org</a>;",
"There was a spoon named <a href='http://www.wordpress.org' rel=\"nofollow\">http://www.wordpress.org</a>:",
"There was a spoon named <a href='http://www.wordpress.org' rel=\"nofollow\">http://www.wordpress.org</a>)",
);
foreach ( $urls_before as $key => $url ) {
@@ -217,7 +217,7 @@ class Tests_Formatting_MakeClickable extends WP_UnitTestCase {
'In his famous speech “You and Your research” (here: http://www.cs.virginia.edu/~robins/YouAndYourResearch.html) Richard Hamming wrote about people getting more done with their doors closed...',
);
$urls_expected = array(
'Example: WordPress, test (some text), I love example.com (<a href="http://example.org" rel="nofollow">http://example.org</a>), it is brilliant',
'Example: WordPress, test (some text), I love example.com (<a href="http://example.org">http://example.org</a>), it is brilliant',
'Example: WordPress, test (some text), I love example.com (<a href="http://example.com" rel="nofollow">http://example.com</a>), it is brilliant',
'Some text followed by a bracketed link with a trailing elipsis (<a href="http://example.com" rel="nofollow">http://example.com</a>)...',
'In his famous speech “You and Your research” (here: <a href="http://www.cs.virginia.edu/~robins/YouAndYourResearch.html" rel="nofollow">http://www.cs.virginia.edu/~robins/YouAndYourResearch.html</a>) Richard Hamming wrote about people getting more done with their doors closed...',
@@ -421,6 +421,7 @@ class Tests_Formatting_MakeClickable extends WP_UnitTestCase {
/**
* @ticket 48022
* @ticket 56444
* @dataProvider data_add_rel_ugc_in_comments
*/
public function test_add_rel_ugc_in_comments( $content, $expected ) {
@@ -438,14 +439,32 @@ class Tests_Formatting_MakeClickable extends WP_UnitTestCase {
}
public function data_add_rel_ugc_in_comments() {
$home_url_http = set_url_scheme( home_url(), 'http' );
$home_url_https = set_url_scheme( home_url(), 'https' );
return array(
// @ticket 48022
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>',
'<p><a href=\'http://www.wordpress.org\' rel="nofollow ugc">http://www.wordpress.org</a>',
),
// @ticket 56444
array(
'www.example.org',
'<p><a href=\'http://www.example.org\' rel="nofollow ugc">http://www.example.org</a>',
),
array(
$home_url_http,
'<a href="' . $home_url_http . '" rel="ugc">' . $home_url_http . '</a>',
),
array(
$home_url_https,
'<a href="' . $home_url_https . '" rel="ugc">' . $home_url_https . '</a>',
),
);
}

View File

@@ -11,16 +11,6 @@ class Tests_Formatting_wpRelNofollow extends WP_UnitTestCase {
* @ticket 9959
*/
public function test_add_no_follow() {
if ( PHP_VERSION_ID >= 80100 ) {
/*
* For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in
* via hooked in filter functions until a more structural solution to the
* "missing input validation" conundrum has been architected and implemented.
*/
$this->expectDeprecation();
$this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' );
}
$content = '<p>This is some cool <a href="/">Code</a></p>';
$expected = '<p>This is some cool <a href=\"/\" rel=\"nofollow\">Code</a></p>';
$this->assertSame( $expected, wp_rel_nofollow( $content ) );
@@ -30,16 +20,6 @@ class Tests_Formatting_wpRelNofollow extends WP_UnitTestCase {
* @ticket 9959
*/
public function test_convert_no_follow() {
if ( PHP_VERSION_ID >= 80100 ) {
/*
* For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in
* via hooked in filter functions until a more structural solution to the
* "missing input validation" conundrum has been architected and implemented.
*/
$this->expectDeprecation();
$this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' );
}
$content = '<p>This is some cool <a href="/" rel="weird">Code</a></p>';
$expected = '<p>This is some cool <a href=\"/\" rel=\"weird nofollow\">Code</a></p>';
$this->assertSame( $expected, wp_rel_nofollow( $content ) );
@@ -50,16 +30,6 @@ class Tests_Formatting_wpRelNofollow extends WP_UnitTestCase {
* @dataProvider data_wp_rel_nofollow
*/
public function test_wp_rel_nofollow( $input, $output, $expect_deprecation = false ) {
if ( true === $expect_deprecation && PHP_VERSION_ID >= 80100 ) {
/*
* For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in
* via hooked in filter functions until a more structural solution to the
* "missing input validation" conundrum has been architected and implemented.
*/
$this->expectDeprecation();
$this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' );
}
$this->assertSame( wp_slash( $output ), wp_rel_nofollow( $input ) );
}
@@ -109,16 +79,6 @@ class Tests_Formatting_wpRelNofollow extends WP_UnitTestCase {
}
public function test_append_no_follow_with_valueless_attribute() {
if ( PHP_VERSION_ID >= 80100 ) {
/*
* For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in
* via hooked in filter functions until a more structural solution to the
* "missing input validation" conundrum has been architected and implemented.
*/
$this->expectDeprecation();
$this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' );
}
$content = '<p>This is some cool <a href="demo.com" download rel="hola">Code</a></p>';
$expected = '<p>This is some cool <a href=\"demo.com\" download rel=\"hola nofollow\">Code</a></p>';
$this->assertSame( $expected, wp_rel_nofollow( $content ) );

View File

@@ -11,16 +11,6 @@ class Tests_Formatting_wpRelUgc extends WP_UnitTestCase {
* @ticket 48022
*/
public function test_add_ugc() {
if ( PHP_VERSION_ID >= 80100 ) {
/*
* For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in
* via hooked in filter functions until a more structural solution to the
* "missing input validation" conundrum has been architected and implemented.
*/
$this->expectDeprecation();
$this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' );
}
$content = '<p>This is some cool <a href="/">Code</a></p>';
$expected = '<p>This is some cool <a href=\"/\" rel=\"nofollow ugc\">Code</a></p>';
$this->assertSame( $expected, wp_rel_ugc( $content ) );
@@ -30,16 +20,6 @@ class Tests_Formatting_wpRelUgc extends WP_UnitTestCase {
* @ticket 48022
*/
public function test_convert_ugc() {
if ( PHP_VERSION_ID >= 80100 ) {
/*
* For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in
* via hooked in filter functions until a more structural solution to the
* "missing input validation" conundrum has been architected and implemented.
*/
$this->expectDeprecation();
$this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' );
}
$content = '<p>This is some cool <a href="/" rel="weird">Code</a></p>';
$expected = '<p>This is some cool <a href=\"/\" rel=\"weird nofollow ugc\">Code</a></p>';
$this->assertSame( $expected, wp_rel_ugc( $content ) );
@@ -50,16 +30,6 @@ class Tests_Formatting_wpRelUgc extends WP_UnitTestCase {
* @dataProvider data_wp_rel_ugc
*/
public function test_wp_rel_ugc( $input, $output, $expect_deprecation = false ) {
if ( true === $expect_deprecation && PHP_VERSION_ID >= 80100 ) {
/*
* For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in
* via hooked in filter functions until a more structural solution to the
* "missing input validation" conundrum has been architected and implemented.
*/
$this->expectDeprecation();
$this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' );
}
$this->assertSame( wp_slash( $output ), wp_rel_ugc( $input ) );
}
@@ -99,25 +69,16 @@ class Tests_Formatting_wpRelUgc extends WP_UnitTestCase {
),
array(
'<a href="' . $home_url_http . '/some-url">Home URL (http)</a>',
'<a href="' . $home_url_http . '/some-url">Home URL (http)</a>',
'<a href="' . $home_url_http . '/some-url" rel="ugc">Home URL (http)</a>',
),
array(
'<a href="' . $home_url_https . '/some-url">Home URL (https)</a>',
'<a href="' . $home_url_https . '/some-url">Home URL (https)</a>',
'<a href="' . $home_url_https . '/some-url" rel="ugc">Home URL (https)</a>',
),
);
}
public function test_append_ugc_with_valueless_attribute() {
if ( PHP_VERSION_ID >= 80100 ) {
/*
* For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in
* via hooked in filter functions until a more structural solution to the
* "missing input validation" conundrum has been architected and implemented.
*/
$this->expectDeprecation();
$this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' );
}
$content = '<p>This is some cool <a href="demo.com" download rel="hola">Code</a></p>';
$expected = '<p>This is some cool <a href=\"demo.com\" download rel=\"hola nofollow ugc\">Code</a></p>';