Make WordPress Core

Ticket #33106: 33106.2.patch

File 33106.2.patch, 3.5 KB (added by miqrogroove, 10 years ago)
  • src/wp-includes/class-wp-embed.php

     
    318318         * @return string Potentially modified $content.
    319319         */
    320320        public function autoembed( $content ) {
    321                 // Strip newlines from all elements.
    322                 $content = wp_replace_in_html_tags( $content, array( "\n" => " " ) );
     321                // Avoid multiline elements and comments.
     322                $content = wp_html_split( $content );
     323                for ( $i = 0, $c = count( $content ); $i < $c; $i += 2 ) {
     324               
     325                        if ( '' == $content[$i] ) continue;
    323326
    324                 // Find URLs that are on their own line.
    325                 return preg_replace_callback( '|^(\s*)(https?://[^\s"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content );
     327                        // Find URLs that are on their own line.
     328                        $content[$i] = preg_replace_callback( '|^(\s*)(https?://[^\s"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content[$i] );
     329
     330                }
     331               
     332                return implode( $content );
    326333        }
    327334
    328335        /**
  • src/wp-includes/formatting.php

     
    596596}
    597597
    598598/**
    599  * Replace characters or phrases within HTML elements only.
     599 * Separate HTML elements and comments from the text.
    600600 *
    601  * @since 4.2.3
     601 * @since 4.2.4
    602602 *
    603  * @param string $haystack The text which has to be formatted.
    604  * @param array $replace_pairs In the form array('from' => 'to', ...).
     603 * @param string $input The text which has to be formatted.
    605604 * @return string The formatted text.
    606605 */
    607 function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
     606function wp_html_split( $input ) {
    608607        // Find all elements.
    609608        $comments =
    610609                  '!'           // Start of comment, after the <.
     
    614613                . ')*+'         // Loop possessively.
    615614                . '(?:-->)?';   // End of comment. If not found, match all input.
    616615
     616        $cdata =
     617                  '!\[CDATA\['  // Start of comment, after the <.
     618                . '[^\]]*+'     // Consume non-].
     619                . '(?:'         // Unroll the loop: Consume everything until ]]> is found.
     620                .     '](?!]>)' // One ] not followed by end of comment.
     621                .     '[^\]]*+' // Consume non-].
     622                . ')*+'         // Loop possessively.
     623                . '(?:]]>)?';   // End of comment. If not found, match all input.
     624
    617625        $regex =
    618626                  '/('              // Capture the entire match.
    619627                .     '<'           // Find start of element.
     
    620628                .     '(?(?=!--)'   // Is this a comment?
    621629                .         $comments // Find end of comment.
    622630                .     '|'
    623                 .         '[^>]*>?' // Find end of element. If not found, match all input.
     631                .         '(?(?=!\[CDATA\[)' // Is this a comment?
     632                .             $cdata // Find end of comment.
     633                .         '|'
     634                .             '[^>]*>?' // Find end of element. If not found, match all input.
     635                .         ')'
    624636                .     ')'
    625637                . ')/s';
    626638
    627         $textarr = preg_split( $regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE );
     639        return preg_split( $regex, $input, -1, PREG_SPLIT_DELIM_CAPTURE );
     640}
     641
     642/**
     643 * Replace characters or phrases within HTML elements only.
     644 *
     645 * @since 4.2.3
     646 *
     647 * @param string $haystack The text which has to be formatted.
     648 * @param array $replace_pairs In the form array('from' => 'to', ...).
     649 * @return string The formatted text.
     650 */
     651function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
     652        // Find all elements.
     653        $textarr = wp_html_split( $haystack );
    628654        $changed = false;
    629655
    630656        // Optimize when searching for one item.