WordPress.org

Make WordPress Core

Ticket #7056: texturize-stack.diff

File texturize-stack.diff, 2.8 KB (added by nbachiyski, 9 years ago)
  • wp-includes/formatting.php

     
    2828 */
    2929function wptexturize($text) {
    3030        global $wp_cockneyreplace;
    31         $next = true;
    32         $has_pre_parent = false;
    3331        $output = '';
    3432        $curl = '';
    3533        $textarr = preg_split('/(<.*>|\[.*\])/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
    3634        $stop = count($textarr);
     35        $no_texturize_tags = apply_filters('no_texturize_tags', array('pre', 'code', 'kbd', 'style', 'script'));
     36        $no_texturize_shortcodes = apply_filters('no_texturize_shortcodes', array('code'));
     37        $no_texturize_tags_stack = array();
     38        $no_texturize_shortcodes_stack = array();
    3739
    3840        // if a plugin has provided an autocorrect array, use it
    3941        if ( isset($wp_cockneyreplace) ) {
     
    5355        for ( $i = 0; $i < $stop; $i++ ) {
    5456                $curl = $textarr[$i];
    5557
    56                 if ( !empty($curl) && '<' != $curl{0} && '[' != $curl{0} && $next && !$has_pre_parent) { // If it's not a tag
     58                if ( !empty($curl) && '<' != $curl{0} && '[' != $curl{0}
     59                                && empty($no_texturize_shortcodes_stack) && empty($no_texturize_tags_stack)) { // If it's not a tag
    5760                        // static strings
    5861                        $curl = str_replace($static_characters, $static_replacements, $curl);
    5962                        // regular expressions
    6063                        $curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);
    61                 } elseif (strpos($curl, '<code') !== false || strpos($curl, '<kbd') !== false || strpos($curl, '<style') !== false || strpos($curl, '<script') !== false) {
    62                         $next = false;
    63                 } elseif (strpos($curl, '<pre') !== false) {
    64                         $has_pre_parent = true;
    65                 } elseif (strpos($curl, '</pre>') !== false) {
    66                         $has_pre_parent = false;
    6764                } else {
    68                         $next = true;
     65                        wptexturize_pushpop_element($curl, $no_texturize_tags_stack, $no_texturize_tags, '<', '>');
     66                        wptexturize_pushpop_element($curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes, '[', ']');
    6967                }
    7068
    7169                $curl = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&#038;$1', $curl);
     
    7573        return $output;
    7674}
    7775
     76function wptexturize_pushpop_element($text, &$stack, $disabled_elements, $opening = '<', $closing = '>') {
     77        $o = preg_quote($opening);
     78        $c = preg_quote($closing);
     79        foreach($disabled_elements as $element) {
     80                if (preg_match('/^'.$o.$element.'\b/', $text)) array_push($stack, $element);
     81                if (preg_match('/^'.$o.'\/'.$element.$c.'/', $text)) {
     82                        $last = array_pop($stack);
     83                        // disable texturize until we find a closing tag of our type (e.g. <pre>)
     84                        // even if there was invalid nesting before that
     85                        // Example: in the case <pre>sadsadasd</code>"baba"</pre> "baba" won't be texturized
     86                        if ($last != $element) array_push($stack, $last);
     87                }
     88        }
     89}
     90
    7891/**
    7992 * Accepts matches array from preg_replace_callback in wpautop() or a string.
    8093 *