Make WordPress Core

Ticket #33517: 33517.patch

File 33517.patch, 3.3 KB (added by miqrogroove, 10 years ago)
  • src/wp-includes/shortcodes.php

     
    195195        if (empty($shortcode_tags) || !is_array($shortcode_tags))
    196196                return $content;
    197197
    198         $tagnames = array_keys($shortcode_tags);
    199         $tagregexp = join( '|', array_map('preg_quote', $tagnames) );
    200         $pattern = "/\\[($tagregexp)/s";
     198        // Find all registered tag names in $content.
     199        preg_match_all( '@\[([^\s\[\]/]++)@', $content, $matches );
     200        $tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] );
    201201
    202         if ( 1 !== preg_match( $pattern, $content ) ) {
    203                 // Avoids parsing HTML when there are no shortcodes or embeds anyway.
     202        if ( empty( $tagnames ) ) {
    204203                return $content;
    205204        }
    206205
    207         $content = do_shortcodes_in_html_tags( $content, $ignore_html );
     206        $content = do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames );
    208207
    209         $pattern = get_shortcode_regex();
     208        $pattern = get_shortcode_regex( $tagnames );
    210209        $content = preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
    211210
    212211        // Always restore square braces so we don't break things like <!--[if IE ]>
     
    234233 *
    235234 * @global array $shortcode_tags
    236235 *
     236 * @param array $tagnames List of shortcodes to find. Optional. Defaults to all registered shortcodes.
    237237 * @return string The shortcode search regular expression
    238238 */
    239 function get_shortcode_regex() {
     239function get_shortcode_regex( $tagnames = null ) {
    240240        global $shortcode_tags;
    241         $tagnames = array_keys($shortcode_tags);
     241
     242        if ( empty( $tagnames ) ) {
     243                $tagnames = array_keys( $shortcode_tags );
     244        }
    242245        $tagregexp = join( '|', array_map('preg_quote', $tagnames) );
    243246
    244247        // WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag()
     
    324327 *
    325328 * @param string $content Content to search for shortcodes
    326329 * @param bool $ignore_html When true, all square braces inside elements will be encoded.
     330 * @param array $tagnames List of shortcodes to find.
    327331 * @return string Content with shortcodes filtered out.
    328332 */
    329 function do_shortcodes_in_html_tags( $content, $ignore_html ) {
     333function do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames ) {
    330334        // Normalize entities in unfiltered HTML before adding placeholders.
    331335        $trans = array( '&#91;' => '&#091;', '&#93;' => '&#093;' );
    332336        $content = strtr( $content, $trans );
    333337        $trans = array( '[' => '&#91;', ']' => '&#93;' );
    334338
    335         $pattern = get_shortcode_regex();
     339        $pattern = get_shortcode_regex( $tagnames );
    336340        $textarr = wp_html_split( $content );
    337341
    338342        foreach ( $textarr as &$element ) {
     
    532536        if (empty($shortcode_tags) || !is_array($shortcode_tags))
    533537                return $content;
    534538
    535         $content = do_shortcodes_in_html_tags( $content, true );
     539        // Find all registered tag names in $content.
     540        preg_match_all( '@\[([^\s\[\]/]++)@', $content, $matches );
     541        $tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] );
    536542
    537         $pattern = get_shortcode_regex();
     543        if ( empty( $tagnames ) ) {
     544                return $content;
     545        }
     546
     547        $content = do_shortcodes_in_html_tags( $content, true, $tagnames );
     548
     549        $pattern = get_shortcode_regex( $tagnames );
    538550        $content = preg_replace_callback( "/$pattern/s", 'strip_shortcode_tag', $content );
    539551
    540552        // Always restore square braces so we don't break things like <!--[if IE ]>