Skip to content

Commit a20c207

Browse files
committed
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
1 parent afc55a7 commit a20c207

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/wp-includes/functions.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,7 @@ function xmlrpc_removepostdata( $content ) {
820820
* Use RegEx to extract URLs from arbitrary content.
821821
*
822822
* @since 3.7.0
823+
* @since 6.0.0 Fixes support for HTML entities (Trac 30580).
823824
*
824825
* @param string $content Content to extract URLs from.
825826
* @return string[] Array of URLs found in passed string.
@@ -833,7 +834,7 @@ function wp_extract_urls( $content ) {
833834
. '(?:'
834835
. '\([\w\d]+\)|'
835836
. '(?:'
836-
. "[^`!()\[\]{};:'\".,<>«»“”‘’\s]|"
837+
. "[^`!()\[\]{}:'\".,<>«»“”‘’\s]|"
837838
. '(?:[:]\d+)?/?'
838839
. ')+'
839840
. ')'
@@ -842,7 +843,17 @@ function wp_extract_urls( $content ) {
842843
$post_links
843844
);
844845

845-
$post_links = array_unique( array_map( 'html_entity_decode', $post_links[2] ) );
846+
$post_links = array_unique(
847+
array_map(
848+
static function( $link ) {
849+
// Decode to replace valid entities, like &amp;.
850+
$link = html_entity_decode( $link );
851+
// Maintain backward compatibility by removing extraneous semi-colons (`;`).
852+
return str_replace( ';', '', $link );
853+
},
854+
$post_links[2]
855+
)
856+
);
846857

847858
return array_values( $post_links );
848859
}

tests/phpunit/tests/functions.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,10 @@ public function test_wp_extract_urls() {
990990
'ftp://127.0.0.1/',
991991
'http://www.woo.com/video?v=exvUH2qKLTU',
992992
'http://taco.com?burrito=enchilada#guac',
993+
'http://example.org/?post_type=post&p=4',
994+
'http://example.org/?post_type=post&p=5',
995+
'http://example.org/?post_type=post&p=6',
996+
'http://typo-in-query.org/?foo=bar&ampbaz=missing_semicolon',
993997
);
994998

995999
$blob = '
@@ -1052,6 +1056,12 @@ public function test_wp_extract_urls() {
10521056
http://www.woo.com/video?v=exvUH2qKLTU
10531057
10541058
http://taco.com?burrito=enchilada#guac
1059+
1060+
http://example.org/?post_type=post&amp;p=4
1061+
http://example.org/?post_type=post&#038;p=5
1062+
http://example.org/?post_type=post&p=6
1063+
1064+
http://typo-in-query.org/?foo=bar&ampbaz=missing_semicolon
10551065
';
10561066

10571067
$urls = wp_extract_urls( $blob );
@@ -1096,6 +1106,28 @@ public function test_wp_extract_urls() {
10961106
$this->assertSame( array_slice( $original_urls, 0, 8 ), $urls );
10971107
}
10981108

1109+
/**
1110+
* Tests for backward compatibility of `wp_extract_urls` to remove unused semicolons.
1111+
*
1112+
* @ticket 30580
1113+
*
1114+
* @covers ::wp_extract_urls
1115+
*/
1116+
public function test_wp_extract_urls_remove_semicolon() {
1117+
$expected = array(
1118+
'http://typo.com',
1119+
'http://example.org/?post_type=post&p=8',
1120+
);
1121+
$actual = wp_extract_urls(
1122+
'
1123+
http://typo.com;
1124+
http://example.org/?post_type=;p;o;s;t;&amp;p=8;
1125+
'
1126+
);
1127+
1128+
$this->assertSame( $expected, $actual );
1129+
}
1130+
10991131
/**
11001132
* @ticket 28786
11011133
*/

0 commit comments

Comments
 (0)