diff --git a/src/wp-includes/class-wp-embed.php b/src/wp-includes/class-wp-embed.php index 5f22e92a5c..888dcd33da 100644 --- a/src/wp-includes/class-wp-embed.php +++ b/src/wp-includes/class-wp-embed.php @@ -334,8 +334,12 @@ class WP_Embed { // Replace line breaks from all HTML elements with placeholders. $content = wp_replace_in_html_tags( $content, array( "\n" => '' ) ); - // Find URLs that are on their own line. - $content = preg_replace_callback( '|^(\s*)(https?://[^\s"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content ); + if ( preg_match( '#(^|\s|>)https?://#i', $content ) ) { + // Find URLs on their own line. + $content = preg_replace_callback( '|^(\s*)(https?://[^\s<>"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content ); + // Find URLs in their own paragraph. + $content = preg_replace_callback( '|(]*)?>\s*)(https?://[^\s<>"]+)(\s*<\/p>)|i', array( $this, 'autoembed_callback' ), $content ); + } // Put the line breaks back. return str_replace( '', "\n", $content ); diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 433943268b..5faa0cfae7 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -158,6 +158,72 @@ EOF; $this->assertEquals( $content, $result ); } + function data_autoembed() { + return array( + + // Should embed + array( +'https://w.org', +'[embed]' + ), + array( +'test + https://w.org +test', +'test + [embed] +test' + ), + array( +'

https://w.org

', +'

[embed]

' + ), + array( +'

https://w.org

', +'

[embed]

' + ), + array( +'

test +https://w.org +test

', +'

test +[embed] +test

' + ), + array( +'

https://w.org +

', +'

[embed] +

' + ), + + // Should NOT embed + array( +'test https://w.org

' + ), + array( +'https://w.org' + ), + array( +'
https://w.org
+

' + ), + array( +' +https://w.org' + ), + ); + } + + /** + * @dataProvider data_autoembed + */ + function test_autoembed( $content, $result = null ) { + $wp_embed = new Test_Autoembed; + + $this->assertEquals( $wp_embed->autoembed( $content ), $result ? $result : $content ); + } + function test_wp_prepare_attachment_for_js() { // Attachment without media $id = wp_insert_attachment(array( @@ -1612,3 +1678,12 @@ EOF; $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ) ); } } + +/** + * Helper class for `test_autoembed`. + */ +class Test_Autoembed extends WP_Embed { + public function shortcode( $attr, $url = '' ) { + return '[embed]'; + } +}