From 15a7d98ce629e9fb2e81500ad02ed083d58f2852 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Tue, 28 Jul 2015 23:03:24 +0000 Subject: [PATCH] Protect newlines inside of `CDATA`. This was breaking things, notably inline JS that used comments for HTML standards compat. * Tokenize newlines in `WP_Embed::autoembed()` before running `->autoembed_callback()` * Tokenize newlines with placeholders in `wpautop()` * Introduce `wp_html_split()` to DRY the RegEx from `wp_replace_in_html_tags()` and `do_shortcodes_in_html_tags()` Adds unit tests. Props miqrogroove, kitchin, azaozz. Fixes #33106. Built from https://develop.svn.wordpress.org/trunk@33469 git-svn-id: http://core.svn.wordpress.org/trunk@33436 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-embed.php | 15 +++++-- wp-includes/formatting.php | 74 ++++++++++++++++++++++++---------- wp-includes/shortcodes.php | 25 ++---------- wp-includes/version.php | 2 +- 4 files changed, 69 insertions(+), 47 deletions(-) diff --git a/wp-includes/class-wp-embed.php b/wp-includes/class-wp-embed.php index 148c5f4bf7..16e5acd79f 100644 --- a/wp-includes/class-wp-embed.php +++ b/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/wp-includes/formatting.php b/wp-includes/formatting.php index 25fb02d608..f323e9c0af 100644 --- a/wp-includes/formatting.php +++ b/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