Index: /trunk/src/wp-includes/class-wp-embed.php =================================================================== --- /trunk/src/wp-includes/class-wp-embed.php (revision 37626) +++ /trunk/src/wp-includes/class-wp-embed.php (revision 37627) @@ -335,6 +335,10 @@ $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. Index: /trunk/tests/phpunit/tests/media.php =================================================================== --- /trunk/tests/phpunit/tests/media.php (revision 37626) +++ /trunk/tests/phpunit/tests/media.php (revision 37627) @@ -157,4 +157,70 @@ $result = $wp_embed->autoembed( $content ); $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 ); } @@ -1613,2 +1679,11 @@ } } + +/** + * Helper class for `test_autoembed`. + */ +class Test_Autoembed extends WP_Embed { + public function shortcode( $attr, $url = '' ) { + return '[embed]'; + } +}