Make WordPress Core


Ignore:
Timestamp:
07/23/2015 05:00:44 AM (11 years ago)
Author:
pento
Message:

Shortcodes: Improve the reliablity of shortcodes inside HTML tags.

Merge of [33359] to the 3.9 branch.

Props miqrogroove.

See #15694.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.9/src/wp-includes/formatting.php

    r32190 r33386  
    292292        $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
    293293
     294        // Strip newlines from all elements.
     295        $pee = wp_replace_in_html_tags( $pee, array( "\n" => " " ) );
     296
    294297        if ( strpos( $pee, '</object>' ) !== false ) {
    295298                // no P/BR around param and embed
    … …  
    338341
    339342        return $pee;
     343}
     344
     345/**
     346 * Replace characters or phrases within HTML elements only.
     347 *
     348 * @since 4.2.3
     349 *
     350 * @param string $haystack The text which has to be formatted.
     351 * @param array $replace_pairs In the form array('from' => 'to', ...).
     352 * @return string The formatted text.
     353 */
     354function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
     355        // Find all elements.
     356        $comments =
     357                  '!'           // Start of comment, after the <.
     358                . '(?:'         // Unroll the loop: Consume everything until --> is found.
     359                .     '-(?!->)' // Dash not followed by end of comment.
     360                .     '[^\-]*+' // Consume non-dashes.
     361                . ')*+'         // Loop possessively.
     362                . '(?:-->)?';   // End of comment. If not found, match all input.
     363
     364        $regex =
     365                  '/('              // Capture the entire match.
     366                .     '<'           // Find start of element.
     367                .     '(?(?=!--)'   // Is this a comment?
     368                .         $comments // Find end of comment.
     369                .     '|'
     370                .         '[^>]*>?' // Find end of element. If not found, match all input.
     371                .     ')'
     372                . ')/s';
     373
     374        $textarr = preg_split( $regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE );
     375        $changed = false;
     376
     377        // Optimize when searching for one item.
     378        if ( 1 === count( $replace_pairs ) ) {
     379                // Extract $needle and $replace.
     380                foreach ( $replace_pairs as $needle => $replace );
     381
     382                // Loop through delimeters (elements) only.
     383                for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) {
     384                        if ( false !== strpos( $textarr[$i], $needle ) ) {
     385                                $textarr[$i] = str_replace( $needle, $replace, $textarr[$i] );
     386                                $changed = true;
     387                        }
     388                }
     389        } else {
     390                // Extract all $needles.
     391                $needles = array_keys( $replace_pairs );
     392
     393                // Loop through delimeters (elements) only.
     394                for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) {
     395                        foreach ( $needles as $needle ) {
     396                                if ( false !== strpos( $textarr[$i], $needle ) ) {
     397                                        $textarr[$i] = strtr( $textarr[$i], $replace_pairs );
     398                                        $changed = true;
     399                                        // After one strtr() break out of the foreach loop and look at next element.
     400                                        break;
     401                                }
     402                        }
     403                }
     404        }
     405
     406        if ( $changed ) {
     407                $haystack = implode( $textarr );
     408        }
     409
     410        return $haystack;
    340411}
    341412
Note: See TracChangeset for help on using the changeset viewer.