Make WordPress Core


Ignore:
Timestamp:
05/14/2024 06:03:43 PM (19 months ago)
Author:
dmsnell
Message:

Normalize UTF-8 charset slug detection.

There are several exist places in Core that attempt to detect if a blog charset
is UTF-8. Each place attempts to perform the same check, except the logic is
spread throughout and there's no single method provided to make this
determination in a consistent way. The _canonical_charset() method exists,
but is marked private for use.

In this patch the new unicode module provides is_utf8_charset() as a method
taking an optional charset slug and indicating if it represents UTF-8,
examining all of the allowable variants of that slug. Associated code is
updated to use this new function, including _canonical_charset(). If no slug
is provided, it will look up the current get_option( 'blog_charset' ).

Finally, the test functions governing _canonical_charset() have been
rewritten as a single test with a data provider instead of as separate test
functions.

Developed in https://github.com/WordPress/wordpress-develop/pull/6535
Discussed in https://core.trac.wordpress.org/ticket/61182

Fixes #61182.
Props dmsnell, jonsurrell.

File:
1 edited

Legend:

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

    r57910 r58147  
    961961    }
    962962
    963     // Store the site charset as a static to avoid multiple calls to wp_load_alloptions().
    964     if ( ! $charset ) {
    965         static $_charset = null;
    966         if ( ! isset( $_charset ) ) {
    967             $alloptions = wp_load_alloptions();
    968             $_charset   = isset( $alloptions['blog_charset'] ) ? $alloptions['blog_charset'] : '';
    969         }
    970         $charset = $_charset;
    971     }
    972 
    973     if ( in_array( $charset, array( 'utf8', 'utf-8', 'UTF8' ), true ) ) {
    974         $charset = 'UTF-8';
    975     }
     963    $charset = _canonical_charset( $charset ? $charset : get_option( 'blog_charset' ) );
    976964
    977965    $_quote_style = $quote_style;
     
    11151103    static $is_utf8 = null;
    11161104    if ( ! isset( $is_utf8 ) ) {
    1117         $is_utf8 = in_array( get_option( 'blog_charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ), true );
     1105        $is_utf8 = is_utf8_charset();
    11181106    }
    11191107    if ( ! $is_utf8 ) {
Note: See TracChangeset for help on using the changeset viewer.