WordPress.org

Make WordPress Core

Ticket #19855: 19855.patch

File 19855.patch, 1.7 KB (added by kurtpayne, 4 years ago)

Replace pre blocks with tokens at the start of wpautop, then reverse the process at the end

  • wp-includes/formatting.php

     
    177177                return ''; 
    178178        $pee = $pee . "\n"; // just to make things a little easier, pad the end 
    179179        $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee); 
     180 
     181        // Save pre tags 
     182        $pre_blocks = array(); 
     183        if ( preg_match_all( '/<pre[^>]*>(.*?)<\/pre>/is', $pee, $matches ) ) { 
     184                $counter = 0; 
     185                foreach ( (array) $matches as $match ) { 
     186                        $token = '<WPPre_' . $counter++ . ' />'; 
     187                        $pre_blocks[$token] = $match[0]; 
     188                        $pos = strpos( $pee, $match[0] ); 
     189                        if ( false !== $pos ) 
     190                                $pee = substr( $pee, 0, $pos ) . $token . substr( $pee, $pos + strlen( $match[0] ) ); 
     191                } 
     192        } 
     193 
    180194        // Space things out a little 
    181195        $allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|option|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)'; 
    182196        $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee); 
     
    207221        } 
    208222        $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee); 
    209223        $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee); 
    210         if (strpos($pee, '<pre') !== false) 
    211                 $pee = preg_replace_callback('!(<pre[^>]*>)(.*?)</pre>!is', 'clean_pre', $pee ); 
     224         
     225        // Replace pre tags 
     226        if ( !empty( $pre_blocks ) ) 
     227                $pee = str_replace( array_keys( $pre_blocks ), array_values( $pre_blocks ), $pee ); 
     228 
    212229        $pee = preg_replace( "|\n</p>$|", '</p>', $pee ); 
    213230 
    214231        return $pee;