Make WordPress Core

Changeset 10355


Ignore:
Timestamp:
01/13/2009 03:18:37 PM (16 years ago)
Author:
azaozz
Message:

Speed up wp_specialchars, props sambauers, see #8767

File:
1 edited

Legend:

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

    r10339 r10355  
    208208    }
    209209
     210    // Don't bother if there are no specialchars - saves some processing
     211    if ( !preg_match( '/[&<>"\']/', $string ) ) {
     212        return $string;
     213    }
     214
     215    // Store the site charset as a static to avoid multiple calls to wp_load_alloptions()
    210216    if ( !$charset ) {
    211         $alloptions = wp_load_alloptions();
    212         $charset = isset( $alloptions['blog_charset'] ) ? $alloptions['blog_charset'] : '';
     217        static $_charset;
     218        if ( !isset( $_charset ) ) {
     219            $alloptions = wp_load_alloptions();
     220            $_charset = isset( $alloptions['blog_charset'] ) ? $alloptions['blog_charset'] : '';
     221        }
     222        $charset = $_charset;
    213223    }
    214224    if ( in_array( $charset, array( 'utf8', 'utf-8', 'UTF8' ) ) ) {
     
    284294    }
    285295
     296    // Don't bother if there are no entities - saves a lot of processing
     297    if ( strpos( $string, '&' ) === false ) {
     298        return $string;
     299    }
     300
    286301    // More complete than get_html_translation_table( HTML_SPECIALCHARS )
    287302    $single = array( '&#039;'  => '\'', '&#x27;' => '\'' );
     
    341356    }
    342357
    343     if ( !in_array( get_option( 'blog_charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
     358    // Store the site charset as a static to avoid multiple calls to get_option()
     359    static $is_utf8;
     360    if ( !isset( $is_utf8 ) ) {
     361        $is_utf8 = in_array( get_option( 'blog_charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) );
     362    }
     363    if ( !$is_utf8 ) {
    344364        return $string;
    345365    }
    346366
     367    // Check for support for utf8 in the installed PCRE library once and store the result in a static
     368    static $utf8_pcre;
     369    if ( !isset( $utf8_pcre ) ) {
     370        $utf8_pcre = @preg_match( '/^./u', 'a' );
     371    }
     372    // We can't demand utf8 in the PCRE installation, so just return the string in those cases
     373    if ( !$utf8_pcre ) {
     374        return $string;
     375    }
     376
    347377    // preg_match fails when it encounters invalid UTF8 in $string
    348     if ( 1 === @preg_match( '@^.@us', $string ) ) {
     378    if ( 1 === @preg_match( '/^./us', $string ) ) {
    349379        return $string;
    350380    }
    351381
     382    // Attempt to strip the bad chars if requested (not recommended)
    352383    if ( $strip && function_exists( 'iconv' ) ) {
    353384        return iconv( 'utf-8', 'utf-8', $string );
    354     } else {
    355         return '';
    356     }
     385    }
     386
     387    return '';
    357388}
    358389
Note: See TracChangeset for help on using the changeset viewer.