Make WordPress Core


Ignore:
Timestamp:
10/01/2015 06:04:13 PM (11 years ago)
Author:
wonderboymusic
Message:

Shortcodes: Fix PCRE performance bugs in get_shortcode_regexp() and related to wptexturize(), do_shortcode(), and strip_shortcodes()

Alters unit tests.

Props miqrogroove.
Fixes #33517.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/shortcodes.php

    r34745 r34747  
    209209                return $content;
    210210
    211         $tagnames = array_keys($shortcode_tags);
    212         $tagregexp = join( '|', array_map('preg_quote', $tagnames) );
    213         $pattern = "/\\[($tagregexp)/s";
    214 
    215         if ( 1 !== preg_match( $pattern, $content ) ) {
    216                 // Avoids parsing HTML when there are no shortcodes or embeds anyway.
     211        // Find all registered tag names in $content.
     212        preg_match_all( '@\[([^<>&/\[\]\x00-\x20]++)@', $content, $matches );
     213        $tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] );
     214
     215        if ( empty( $tagnames ) ) {
    217216                return $content;
    218217        }
    219218
    220         $content = do_shortcodes_in_html_tags( $content, $ignore_html );
    221 
    222         $pattern = get_shortcode_regex();
     219        $content = do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames );
     220
     221        $pattern = get_shortcode_regex( $tagnames );
    223222        $content = preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
    224223
     
    248247 * @global array $shortcode_tags
    249248 *
     249 * @param array $tagnames List of shortcodes to find. Optional. Defaults to all registered shortcodes.
    250250 * @return string The shortcode search regular expression
    251251 */
    252 function get_shortcode_regex() {
    253         global $shortcode_tags;
    254         $tagnames = array_keys($shortcode_tags);
     252function get_shortcode_regex( $tagnames = null ) {
     253        global $shortcode_tags;
     254
     255        if ( empty( $tagnames ) ) {
     256                $tagnames = array_keys( $shortcode_tags );
     257        }
    255258        $tagregexp = join( '|', array_map('preg_quote', $tagnames) );
    256259
     
    338341 * @param string $content Content to search for shortcodes
    339342 * @param bool $ignore_html When true, all square braces inside elements will be encoded.
     343 * @param array $tagnames List of shortcodes to find.
    340344 * @return string Content with shortcodes filtered out.
    341345 */
    342 function do_shortcodes_in_html_tags( $content, $ignore_html ) {
     346function do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames ) {
    343347        // Normalize entities in unfiltered HTML before adding placeholders.
    344348        $trans = array( '&#91;' => '&#091;', '&#93;' => '&#093;' );
     
    346350        $trans = array( '[' => '&#91;', ']' => '&#93;' );
    347351
    348         $pattern = get_shortcode_regex();
     352        $pattern = get_shortcode_regex( $tagnames );
    349353        $textarr = wp_html_split( $content );
    350354
     
    558562                return $content;
    559563
    560         $content = do_shortcodes_in_html_tags( $content, true );
    561 
    562         $pattern = get_shortcode_regex();
     564        // Find all registered tag names in $content.
     565        preg_match_all( '@\[([^<>&/\[\]\x00-\x20]++)@', $content, $matches );
     566        $tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] );
     567
     568        if ( empty( $tagnames ) ) {
     569                return $content;
     570        }
     571
     572        $content = do_shortcodes_in_html_tags( $content, true, $tagnames );
     573
     574        $pattern = get_shortcode_regex( $tagnames );
    563575        $content = preg_replace_callback( "/$pattern/s", 'strip_shortcode_tag', $content );
    564576
Note: See TracChangeset for help on using the changeset viewer.