Make WordPress Core


Ignore:
Timestamp:
09/17/2014 03:13:24 PM (12 years ago)
Author:
wonderboymusic
Message:

wptexturize() improvements:

  • Expand the wptexturize() RegEx to include the list of registered shortcodes.
  • Avoid backtracking after [ chars by not filtering params in registered shortcodes. This will cause escaped shortcodes and their params to become texturized if not registered.
  • Registered shortcode params will never be texturized, even when escaped.
  • Move all tests involving unregistered shortcodes to a new and improved unit.
  • Update one test involving HTML within shortcode params.

Props miqrogroove.
See #29557.

File:
1 edited

Legend:

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

    r29715 r29748  
    2929 */
    3030function wptexturize($text, $reset = false) {
    31         global $wp_cockneyreplace;
     31        global $wp_cockneyreplace, $shortcode_tags;
    3232        static $static_characters, $static_replacements, $dynamic_characters, $dynamic_replacements,
    3333                $default_no_texturize_tags, $default_no_texturize_shortcodes, $run_texturize = true;
    … …  
    206206        // Look for shortcodes and HTML elements.
    207207
     208        $tagnames = array_keys( $shortcode_tags );
     209        $tagregexp = join( '|', array_map( 'preg_quote', $tagnames ) );
     210        $tagregexp = "(?:$tagregexp)(?![\\w-])"; // Excerpt of get_shortcode_regex().
     211       
    208212        $regex =  '/('                  // Capture the entire match.
    209213                .       '<'             // Find start of element.
    … …  
    216220                .       '\['            // Find start of shortcode.
    217221                .       '\[?'           // Shortcodes may begin with [[
    218                 .       '(?:'
    219                 .               '[^\[\]<>]'     // Shortcodes do not contain other shortcodes.
    220                 .       '|'
    221                 .               '<[^>]+>'       // HTML elements permitted. Prevents matching ] before >.
    222                 .       ')++'
     222                .       '\/?'           // Closing slash may precede name.
     223                .       $tagregexp      // Only match registered shortcodes, because performance.
     224                .       '[^\[\]]*'      // Shortcodes do not contain other shortcodes.
    223225                .       '\]'            // Find end of shortcode.
    224226                .       '\]?'           // Shortcodes may end with ]]
    … …  
    242244                        continue;
    243245
    244                 } elseif ( '[' === $first && 1 === preg_match( '/^\[(?:[^\[\]<>]|<[^>]+>)++\]$/', $curl ) ) {
     246                } elseif ( '[' === $first && 1 === preg_match( '/^\[\[?\/?' . $tagregexp . '[^\[\]]*\]\]?$/', $curl ) ) {
    245247                        // This is a shortcode delimiter.
    246248
    247                         _wptexturize_pushpop_element( $curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes );
    248 
    249                 } elseif ( '[' === $first && 1 === preg_match( '/^\[\[?(?:[^\[\]<>]|<[^>]+>)++\]\]?$/', $curl ) ) {
    250                         // This is an escaped shortcode delimiter.
    251 
    252                         // Do not texturize.
    253                         // Do not push to the shortcodes stack.
    254 
    255                         continue;
     249                        if ( '[[' !== substr( $curl, 0, 2 ) && ']]' !== substr( $curl, -2 ) ) {
     250                                // Looks like a normal shortcode.
     251                                _wptexturize_pushpop_element( $curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes );
     252                        } else {
     253                                // Looks like an escaped shortcode.
     254                                // Do not texturize.
     255                                // Do not push to the shortcodes stack.
     256                                continue;
     257                        }
    256258
    257259                } elseif ( empty( $no_texturize_shortcodes_stack ) && empty( $no_texturize_tags_stack ) ) {
    … …  
    314316        // Parse out the tag name.
    315317        $space = strpos( $text, ' ' );
    316         if ( FALSE === $space ) {
     318        if ( false === $space ) {
    317319                $space = -1;
    318320        } else {
Note: See TracChangeset for help on using the changeset viewer.