Formatting: Account for HTML entities in wp_extract_urls().

Prevent `wp_extract_urls()` trimming HTML entities within URLs. Correctly escaped URLs such as https://youtube.com/watch?v=dQw4w9WgXcQ&t=1 will now be extracted as https://youtube.com/watch?v=dQw4w9WgXcQ&t=1 rather than truncated.

Props trex005, voldemortensen, johnbillion, ironprogrammer, costdev, hellofromtonya.
Fixes #30580



git-svn-id: https://develop.svn.wordpress.org/trunk@53044 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson
2022-04-01 03:38:55 +00:00
parent afc55a72de
commit a20c20774f
2 changed files with 45 additions and 2 deletions
+13 -2
View File
@@ -820,6 +820,7 @@ function xmlrpc_removepostdata( $content ) {
* Use RegEx to extract URLs from arbitrary content.
*
* @since 3.7.0
* @since 6.0.0 Fixes support for HTML entities (Trac 30580).
*
* @param string $content Content to extract URLs from.
* @return string[] Array of URLs found in passed string.
@@ -833,7 +834,7 @@ function wp_extract_urls( $content ) {
. '(?:'
. '\([\w\d]+\)|'
. '(?:'
. "[^`!()\[\]{};:'\".,<>«»“”‘’\s]|"
. "[^`!()\[\]{}:'\".,<>«»“”‘’\s]|"
. '(?:[:]\d+)?/?'
. ')+'
. ')'
@@ -842,7 +843,17 @@ function wp_extract_urls( $content ) {
$post_links
);
$post_links = array_unique( array_map( 'html_entity_decode', $post_links[2] ) );
$post_links = array_unique(
array_map(
static function( $link ) {
// Decode to replace valid entities, like &amp;.
$link = html_entity_decode( $link );
// Maintain backward compatibility by removing extraneous semi-colons (`;`).
return str_replace( ';', '', $link );
},
$post_links[2]
)
);
return array_values( $post_links );
}
+32
View File
@@ -990,6 +990,10 @@ class Tests_Functions extends WP_UnitTestCase {
'ftp://127.0.0.1/',
'http://www.woo.com/video?v=exvUH2qKLTU',
'http://taco.com?burrito=enchilada#guac',
'http://example.org/?post_type=post&p=4',
'http://example.org/?post_type=post&p=5',
'http://example.org/?post_type=post&p=6',
'http://typo-in-query.org/?foo=bar&ampbaz=missing_semicolon',
);
$blob = '
@@ -1052,6 +1056,12 @@ class Tests_Functions extends WP_UnitTestCase {
http://www.woo.com/video?v=exvUH2qKLTU
http://taco.com?burrito=enchilada#guac
http://example.org/?post_type=post&amp;p=4
http://example.org/?post_type=post&#038;p=5
http://example.org/?post_type=post&p=6
http://typo-in-query.org/?foo=bar&ampbaz=missing_semicolon
';
$urls = wp_extract_urls( $blob );
@@ -1096,6 +1106,28 @@ class Tests_Functions extends WP_UnitTestCase {
$this->assertSame( array_slice( $original_urls, 0, 8 ), $urls );
}
/**
* Tests for backward compatibility of `wp_extract_urls` to remove unused semicolons.
*
* @ticket 30580
*
* @covers ::wp_extract_urls
*/
public function test_wp_extract_urls_remove_semicolon() {
$expected = array(
'http://typo.com',
'http://example.org/?post_type=post&p=8',
);
$actual = wp_extract_urls(
'
http://typo.com;
http://example.org/?post_type=;p;o;s;t;&amp;p=8;
'
);
$this->assertSame( $expected, $actual );
}
/**
* @ticket 28786
*/