diff --git a/src/wp-includes/class-wp-embed.php b/src/wp-includes/class-wp-embed.php
index 148c5f4bf7..16e5acd79f 100644
--- a/src/wp-includes/class-wp-embed.php
+++ b/src/wp-includes/class-wp-embed.php
@@ -129,6 +129,12 @@ class WP_Embed {
* `->maybe_make_link()` can return false on failure.
*/
public function shortcode( $attr, $url = '' ) {
+ // This filter can be used to output custom HTML instead of allowing oEmbed to run.
+ $custom = apply_filters( 'wp_embed_shortcode_custom', false, $attr, $url );
+ if ( false !== $custom ) {
+ return $custom;
+ }
+
$post = get_post();
if ( empty( $url ) && ! empty( $attr['src'] ) ) {
@@ -318,11 +324,14 @@ class WP_Embed {
* @return string Potentially modified $content.
*/
public function autoembed( $content ) {
- // Strip newlines from all elements.
- $content = wp_replace_in_html_tags( $content, array( "\n" => " " ) );
+ // 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.
- return preg_replace_callback( '|^(\s*)(https?://[^\s"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content );
+ $content = preg_replace_callback( '|^(\s*)(https?://[^\s"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content );
+
+ // Put the line breaks back.
+ return str_replace( '', "\n", $content );
}
/**
diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php
index 8765d3d0b6..6449e91500 100644
--- a/src/wp-includes/formatting.php
+++ b/src/wp-includes/formatting.php
@@ -504,8 +504,8 @@ function wpautop( $pee, $br = true ) {
// Standardize newline characters to "\n".
$pee = str_replace(array("\r\n", "\r"), "\n", $pee);
- // Strip newlines from all elements.
- $pee = wp_replace_in_html_tags( $pee, array( "\n" => " " ) );
+ // Find newlines in all elements and add placeholders.
+ $pee = wp_replace_in_html_tags( $pee, array( "\n" => " " ) );
// Collapse line breaks before and after
+
+EOF;
+
+ $result = apply_filters( 'the_content', $content );
+ $this->assertEquals( $expected, $result );
+ }
+
+ /**
+ * @ticket 33016
+ */
+ function filter_wp_embed_shortcode_custom( $custom, $attr, $url ) {
+ if ( 'https://www.example.com/?video=1' == $url ) {
+ $custom = "";
+ }
+ return $custom;
+ }
+
+ /**
+ * @ticket 33016
+ */
+ function test_oembed_explicit_media_link() {
+ global $wp_embed;
+ add_filter( 'wp_embed_shortcode_custom', array( $this, 'filter_wp_embed_shortcode_custom' ), 10, 3 );
+
+ $content = <<
+EOF;
+
+ $result = $wp_embed->autoembed( $content );
+ $this->assertEquals( $expected, $result );
+
+ $content = <<https://www.example.com/?video=1
+
+EOF;
+
+ $result = $wp_embed->autoembed( $content );
+ $this->assertEquals( $content, $result );
+
+ remove_filter( 'wp_embed_shortcode_custom', array( $this, 'filter_wp_embed_shortcode_custom' ), 10 );
+ }
}