diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index a48b4bfc86..7f57d234c1 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -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 &. + $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 ); } diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php index 294a642f86..17e4999e38 100644 --- a/tests/phpunit/tests/functions.php +++ b/tests/phpunit/tests/functions.php @@ -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&baz=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&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&baz=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;&p=8; + ' + ); + + $this->assertSame( $expected, $actual ); + } + /** * @ticket 28786 */