Make WordPress Core

Ticket #12690: miqro-12690.patch

File miqro-12690.patch, 4.0 KB (added by miqrogroove, 9 years ago)

Fix shortcode matching and also #27602

  • src/wp-includes/formatting.php

     
    130130                $dynamic_replacements = array_values( $dynamic );
    131131        }
    132132
     133        // If there's nothing to do, just stop.
     134        if ( empty( $text ) ) {
     135                return $text;
     136        }
     137
    133138        // Transform into regexp sub-expression used in _wptexturize_pushpop_element
    134139        // Must do this every time in case plugins use these filters in a context sensitive manner
    135140        /**
     
    152157        $no_texturize_tags_stack = array();
    153158        $no_texturize_shortcodes_stack = array();
    154159
    155         $textarr = preg_split('/(<.*>|\[.*\])/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
     160        // Look for shortcodes and HTML elements.
    156161
     162        $regex =  '/('                  // Capture the entire match.
     163                .       '<[^>]+>'       // Find all HTML elements.
     164                . '|'
     165                .       '(?<!\[)'       // Shortcodes do not begin with [[
     166                .       '\['            // Find start of shortcode.
     167                .       '[^\[\]<>]+'    // Shortcodes do not contain other shortcodes or HTML elements.
     168                .       '\]'            // Find end of shortcode.
     169                .       '(?!\])'        // Shortcodes do not end with ]]
     170                . ')/';
     171
     172        $textarr = preg_split($regex, $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
     173
    157174        foreach ( $textarr as &$curl ) {
    158                 if ( empty( $curl ) ) {
    159                         continue;
    160                 }
    161 
    162                 // Only call _wptexturize_pushpop_element if first char is correct tag opening
     175                // Only call _wptexturize_pushpop_element if $curl is a delimeter.
    163176                $first = $curl[0];
    164                 if ( '<' === $first ) {
     177                if ( '<' === $first && '>' === substr($curl, -1) ) {
    165178                        _wptexturize_pushpop_element($curl, $no_texturize_tags_stack, $no_texturize_tags, '<', '>');
    166                 } elseif ( '[' === $first ) {
     179                } elseif ( '[' === $first && 1 === preg_match('/^\[[^\[\]<>]+\]$/', $curl) ) {
    167180                        _wptexturize_pushpop_element($curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes, '[', ']');
    168181                } elseif ( empty($no_texturize_shortcodes_stack) && empty($no_texturize_tags_stack) ) {
    169182
    170                         // This is not a tag, nor is the texturization disabled static strings
     183                        // This is neither a delimeter, nor is this content inside of no_texturize pairs.
    171184                        $curl = str_replace($static_characters, $static_replacements, $curl);
    172 
    173                         // regular expressions
    174185                        $curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);
    175186
    176187                        // 9x9 (times)
  • tests/phpunit/tests/formatting/WPTexturize.php

     
    994994                        ),
    995995                );
    996996        }
     997
     998        /**
     999         * Test HTML and shortcode avoidance.
     1000         *
     1001         * @ticket 12690
     1002         * @dataProvider data_tag_avoidance
     1003         */
     1004        function test_tag_avoidance( $input, $output ) {
     1005                return $this->assertEquals( $output, wptexturize( $input ) );
     1006        }
     1007
     1008        function data_tag_avoidance() {
     1009                return array(
     1010                        array(
     1011                                '[ photos by <a href="http://example.com/?a[]=1&a[]=2"> this guy </a> ]',
     1012                                '[ photos by <a href="http://example.com/?a[]=1&#038;a[]=2"> this guy </a> ],
     1013                        ),
     1014                        array(
     1015                                '[gallery ...]',
     1016                                '[gallery ...]',
     1017                        ),
     1018                        array(
     1019                                '[gal>ery ...]',
     1020                                '[gal>ery &#8230;]',
     1021                        ),
     1022                        array(
     1023                                '[gallery ...',
     1024                                '[gallery &#8230;',
     1025                        ),
     1026                        array(
     1027                                '[[gallery ...]]',
     1028                                '[[gallery &#8230;]]',
     1029                        ),
     1030                        array(
     1031                                '[[gallery ...]',
     1032                                '[[gallery &#8230;]',
     1033                        ),
     1034                        array(
     1035                                '[gallery ...]]',
     1036                                '[gallery &#8230;]]',
     1037                        ),
     1038                        array(
     1039                                '[gallery <br ... /> ...]',
     1040                                '[gallery <br ... /> &#8230;]',
     1041                        ),
     1042                        array(
     1043                                '<br [gallery ...] ... />',
     1044                                '<br [gallery ...] ... />',
     1045                        ),
     1046                        array(
     1047                                '<br [gallery ...] ... /',
     1048                                '<br [gallery ...] &#8230; /',
     1049                        ),
     1050                        array(
     1051                                '<br ... />',
     1052                                '<br ... />',
     1053                        ),
     1054                        array(
     1055                                '<br ... />...<br ... />',
     1056                                '<br ... />&#8230;<br ... />',
     1057                        ),
     1058                        array(
     1059                                '[gallery ...]...[gallery ...]',
     1060                                '[gallery ...]&#8230;[gallery ...]',
     1061                        ),
     1062                );
     1063        }
    9971064}