Speed up wp_specialchars, props sambauers, see #8767
git-svn-id: http://svn.automattic.com/wordpress/trunk@10355 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
b619cefe1d
commit
d7001aa719
|
@ -207,9 +207,19 @@ function wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Don't bother if there are no specialchars - saves some processing
|
||||||
|
if ( !preg_match( '/[&<>"\']/', $string ) ) {
|
||||||
|
return $string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store the site charset as a static to avoid multiple calls to wp_load_alloptions()
|
||||||
if ( !$charset ) {
|
if ( !$charset ) {
|
||||||
$alloptions = wp_load_alloptions();
|
static $_charset;
|
||||||
$charset = isset( $alloptions['blog_charset'] ) ? $alloptions['blog_charset'] : '';
|
if ( !isset( $_charset ) ) {
|
||||||
|
$alloptions = wp_load_alloptions();
|
||||||
|
$_charset = isset( $alloptions['blog_charset'] ) ? $alloptions['blog_charset'] : '';
|
||||||
|
}
|
||||||
|
$charset = $_charset;
|
||||||
}
|
}
|
||||||
if ( in_array( $charset, array( 'utf8', 'utf-8', 'UTF8' ) ) ) {
|
if ( in_array( $charset, array( 'utf8', 'utf-8', 'UTF8' ) ) ) {
|
||||||
$charset = 'UTF-8';
|
$charset = 'UTF-8';
|
||||||
|
@ -283,6 +293,11 @@ function wp_specialchars_decode( $string, $quote_style = ENT_NOQUOTES )
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Don't bother if there are no entities - saves a lot of processing
|
||||||
|
if ( strpos( $string, '&' ) === false ) {
|
||||||
|
return $string;
|
||||||
|
}
|
||||||
|
|
||||||
// More complete than get_html_translation_table( HTML_SPECIALCHARS )
|
// More complete than get_html_translation_table( HTML_SPECIALCHARS )
|
||||||
$single = array( ''' => '\'', ''' => '\'' );
|
$single = array( ''' => '\'', ''' => '\'' );
|
||||||
$single_preg = array( '/�*39;/' => ''', '/�*27;/i' => ''' );
|
$single_preg = array( '/�*39;/' => ''', '/�*27;/i' => ''' );
|
||||||
|
@ -340,20 +355,36 @@ function wp_check_invalid_utf8( $string, $strip = false )
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !in_array( get_option( 'blog_charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
|
// Store the site charset as a static to avoid multiple calls to get_option()
|
||||||
|
static $is_utf8;
|
||||||
|
if ( !isset( $is_utf8 ) ) {
|
||||||
|
$is_utf8 = in_array( get_option( 'blog_charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) );
|
||||||
|
}
|
||||||
|
if ( !$is_utf8 ) {
|
||||||
|
return $string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for support for utf8 in the installed PCRE library once and store the result in a static
|
||||||
|
static $utf8_pcre;
|
||||||
|
if ( !isset( $utf8_pcre ) ) {
|
||||||
|
$utf8_pcre = @preg_match( '/^./u', 'a' );
|
||||||
|
}
|
||||||
|
// We can't demand utf8 in the PCRE installation, so just return the string in those cases
|
||||||
|
if ( !$utf8_pcre ) {
|
||||||
return $string;
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
// preg_match fails when it encounters invalid UTF8 in $string
|
// preg_match fails when it encounters invalid UTF8 in $string
|
||||||
if ( 1 === @preg_match( '@^.@us', $string ) ) {
|
if ( 1 === @preg_match( '/^./us', $string ) ) {
|
||||||
return $string;
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Attempt to strip the bad chars if requested (not recommended)
|
||||||
if ( $strip && function_exists( 'iconv' ) ) {
|
if ( $strip && function_exists( 'iconv' ) ) {
|
||||||
return iconv( 'utf-8', 'utf-8', $string );
|
return iconv( 'utf-8', 'utf-8', $string );
|
||||||
} else {
|
|
||||||
return '';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue