Make WordPress Core

Changeset 17636


Ignore:
Timestamp:
04/13/2011 05:11:35 PM (13 years ago)
Author:
ryan
Message:

Performance improvements for wptexturize(). Props solarissmoke, hakre. fixes #16684

File:
1 edited

Legend:

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

    r17614 r17636  
    2929function wptexturize($text) {
    3030    global $wp_cockneyreplace;
    31     static $static_setup = false, $opening_quote, $closing_quote, $default_no_texturize_tags, $default_no_texturize_shortcodes, $static_characters, $static_replacements, $dynamic_characters, $dynamic_replacements;
    32     $output = '';
    33     $curl = '';
    34     $textarr = preg_split('/(<.*>|\[.*\])/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
    35     $stop = count($textarr);
    36 
    37     // No need to set up these variables more than once
    38     if (!$static_setup) {
     31    static $opening_quote, $closing_quote, $default_no_texturize_tags, $default_no_texturize_shortcodes, $static_characters, $static_replacements, $dynamic_characters, $dynamic_replacements;
     32
     33    // No need to set up these static variables more than once
     34    if ( empty( $opening_quote ) ) {
    3935        /* translators: opening curly quote */
    4036        $opening_quote = _x('&#8220;', 'opening curly quote');
     
    5955        $dynamic_characters = array('/\'(\d\d(?:&#8217;|\')?s)/', '/\'(\d)/', '/(\s|\A|[([{<]|")\'/', '/(\d)"/', '/(\d)\'/', '/(\S)\'([^\'\s])/', '/(\s|\A|[([{<])"(?!\s)/', '/"(\s|\S|\Z)/', '/\'([\s.]|\Z)/', '/\b(\d+)x(\d+)\b/');
    6056        $dynamic_replacements = array('&#8217;$1','&#8217;$1', '$1&#8216;', '$1&#8243;', '$1&#8242;', '$1&#8217;$2', '$1' . $opening_quote . '$2', $closing_quote . '$1', '&#8217;$1', '$1&#215;$2');
    61 
    62         $static_setup = true;
    6357    }
    6458
     
    7165    $no_texturize_shortcodes_stack = array();
    7266
    73     for ( $i = 0; $i < $stop; $i++ ) {
    74         $curl = $textarr[$i];
    75 
    76         if ( !empty($curl) && '<' != $curl[0] && '[' != $curl[0]
    77                 && empty($no_texturize_shortcodes_stack) && empty($no_texturize_tags_stack)) {
    78             // This is not a tag, nor is the texturization disabled
    79             // static strings
     67    $textarr = preg_split('/(<.*>|\[.*\])/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
     68
     69    foreach ( $textarr as &$curl ) {
     70        if ( empty( $curl ) )
     71            continue;
     72
     73        // Only call _wptexturize_pushpop_element if first char is correct tag opening
     74        $first = $curl[0];
     75        if ( '<' === $first ) {
     76            _wptexturize_pushpop_element($curl, $no_texturize_tags_stack, $no_texturize_tags, '<', '>');
     77        } elseif ( '[' === $first ) {
     78            _wptexturize_pushpop_element($curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes, '[', ']');
     79        } elseif ( empty($no_texturize_shortcodes_stack) && empty($no_texturize_tags_stack) ) {
     80            // This is not a tag, nor is the texturization disabled static strings
    8081            $curl = str_replace($static_characters, $static_replacements, $curl);
    8182            // regular expressions
    8283            $curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);
    83         } elseif (!empty($curl)) {
    84             /*
    85              * Only call _wptexturize_pushpop_element if first char is correct
    86              * tag opening
    87              */
    88             if ('<' == $curl[0])
    89                 _wptexturize_pushpop_element($curl, $no_texturize_tags_stack, $no_texturize_tags, '<', '>');
    90             elseif ('[' == $curl[0])
    91                 _wptexturize_pushpop_element($curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes, '[', ']');
    9284        }
    93 
    9485        $curl = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&#038;$1', $curl);
    95         $output .= $curl;
    96     }
    97 
    98     return $output;
     86    }
     87    return implode( '', $textarr );
    9988}
    10089
Note: See TracChangeset for help on using the changeset viewer.