Make WordPress Core

Ticket #33106: 33106.4.patch

File 33106.4.patch, 4.3 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] || false === strpos( $content[$i], 'http' ) ) {
     326                                continue;
     327                        }
    323328
    324                 // Find URLs that are on their own line.
    325                 return preg_replace_callback( '|^(\s*)(https?://[^\s"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content );
     329                        // Find URLs that are on their own line.
     330                        $content[$i] = preg_replace_callback( '|^(\s*)(https?://[^\s"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content[$i] );
     331
     332                }
     333               
     334                return implode( $content );
    326335        }
    327336
    328337        /**
  • src/wp-includes/formatting.php

     
    504504        // Standardize newline characters to "\n".
    505505        $pee = str_replace(array("\r\n", "\r"), "\n", $pee);
    506506
    507         // Strip newlines from all elements.
    508         $pee = wp_replace_in_html_tags( $pee, array( "\n" => " " ) );
     507        // Find newlines in all elements and add placeholders.
     508        $pee = wp_replace_in_html_tags( $pee, array( "\n" => " <!-- wpnl --> " ) );
    509509
    510510        // Collapse line breaks before and after <option> elements so they don't get autop'd.
    511511        if ( strpos( $pee, '<option' ) !== false ) {
     
    592592        if ( !empty($pre_tags) )
    593593                $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee);
    594594
     595        // Restore newlines in all elements.
     596        $pee = str_replace( " <!-- wpnl --> ", "\n", $pee );
     597
    595598        return $pee;
    596599}
    597600
    598601/**
    599  * Replace characters or phrases within HTML elements only.
     602 * Separate HTML elements and comments from the text.
    600603 *
    601  * @since 4.2.3
     604 * @since 4.2.4
    602605 *
    603  * @param string $haystack The text which has to be formatted.
    604  * @param array $replace_pairs In the form array('from' => 'to', ...).
    605  * @return string The formatted text.
     606 * @param string $input The text which has to be formatted.
     607 * @return array The formatted text.
    606608 */
    607 function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
     609function wp_html_split( $input ) {
    608610        // Find all elements.
    609611        $comments =
    610612                  '!'           // Start of comment, after the <.
     
    614616                . ')*+'         // Loop possessively.
    615617                . '(?:-->)?';   // End of comment. If not found, match all input.
    616618
     619        $cdata =
     620                  '!\[CDATA\['  // Start of comment, after the <.
     621                . '[^\]]*+'     // Consume non-].
     622                . '(?:'         // Unroll the loop: Consume everything until ]]> is found.
     623                .     '](?!]>)' // One ] not followed by end of comment.
     624                .     '[^\]]*+' // Consume non-].
     625                . ')*+'         // Loop possessively.
     626                . '(?:]]>)?';   // End of comment. If not found, match all input.
     627
    617628        $regex =
    618629                  '/('              // Capture the entire match.
    619630                .     '<'           // Find start of element.
     
    620631                .     '(?(?=!--)'   // Is this a comment?
    621632                .         $comments // Find end of comment.
    622633                .     '|'
    623                 .         '[^>]*>?' // Find end of element. If not found, match all input.
     634                .         '(?(?=!\[CDATA\[)' // Is this a comment?
     635                .             $cdata // Find end of comment.
     636                .         '|'
     637                .             '[^>]*>?' // Find end of element. If not found, match all input.
     638                .         ')'
    624639                .     ')'
    625640                . ')/s';
    626641
    627         $textarr = preg_split( $regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE );
     642        return preg_split( $regex, $input, -1, PREG_SPLIT_DELIM_CAPTURE );
     643}
     644
     645/**
     646 * Replace characters or phrases within HTML elements only.
     647 *
     648 * @since 4.2.3
     649 *
     650 * @param string $haystack The text which has to be formatted.
     651 * @param array $replace_pairs In the form array('from' => 'to', ...).
     652 * @return string The formatted text.
     653 */
     654function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
     655        // Find all elements.
     656        $textarr = wp_html_split( $haystack );
    628657        $changed = false;
    629658
    630659        // Optimize when searching for one item.