Make WordPress Core


Ignore:
Timestamp:
07/23/2015 04:36:55 AM (9 years ago)
Author:
pento
Message:

Shortcodes: Improve the reliablity of shortcodes inside HTML tags.

Merge of [33359] to the 4.1 branch.

Props miqrogroove.

See #15694.

File:
1 edited

Legend:

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

    r32165 r33380  
    411411    $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
    412412
     413    // Strip newlines from all elements.
     414    $pee = wp_replace_in_html_tags( $pee, array( "\n" => " " ) );
     415
    413416    if ( strpos( $pee, '<option' ) !== false ) {
    414417        // no P/BR around option
     
    463466
    464467    return $pee;
     468}
     469
     470/**
     471 * Replace characters or phrases within HTML elements only.
     472 *
     473 * @since 4.2.3
     474 *
     475 * @param string $haystack The text which has to be formatted.
     476 * @param array $replace_pairs In the form array('from' => 'to', ...).
     477 * @return string The formatted text.
     478 */
     479function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
     480    // Find all elements.
     481    $comments =
     482          '!'           // Start of comment, after the <.
     483        . '(?:'         // Unroll the loop: Consume everything until --> is found.
     484        .     '-(?!->)' // Dash not followed by end of comment.
     485        .     '[^\-]*+' // Consume non-dashes.
     486        . ')*+'         // Loop possessively.
     487        . '(?:-->)?';   // End of comment. If not found, match all input.
     488
     489    $regex =
     490          '/('              // Capture the entire match.
     491        .     '<'           // Find start of element.
     492        .     '(?(?=!--)'   // Is this a comment?
     493        .         $comments // Find end of comment.
     494        .     '|'
     495        .         '[^>]*>?' // Find end of element. If not found, match all input.
     496        .     ')'
     497        . ')/s';
     498
     499    $textarr = preg_split( $regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE );
     500    $changed = false;
     501
     502    // Optimize when searching for one item.
     503    if ( 1 === count( $replace_pairs ) ) {
     504        // Extract $needle and $replace.
     505        foreach ( $replace_pairs as $needle => $replace );
     506
     507        // Loop through delimeters (elements) only.
     508        for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) {
     509            if ( false !== strpos( $textarr[$i], $needle ) ) {
     510                $textarr[$i] = str_replace( $needle, $replace, $textarr[$i] );
     511                $changed = true;
     512            }
     513        }
     514    } else {
     515        // Extract all $needles.
     516        $needles = array_keys( $replace_pairs );
     517
     518        // Loop through delimeters (elements) only.
     519        for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) {
     520            foreach ( $needles as $needle ) {
     521                if ( false !== strpos( $textarr[$i], $needle ) ) {
     522                    $textarr[$i] = strtr( $textarr[$i], $replace_pairs );
     523                    $changed = true;
     524                    // After one strtr() break out of the foreach loop and look at next element.
     525                    break;
     526                }
     527            }
     528        }
     529    }
     530
     531    if ( $changed ) {
     532        $haystack = implode( $textarr );
     533    }
     534
     535    return $haystack;
    465536}
    466537
Note: See TracChangeset for help on using the changeset viewer.