Make WordPress Core

Changeset 55862


Ignore:
Timestamp:
05/26/2023 05:55:40 PM (12 months ago)
Author:
swissspidy
Message:

I18N: Refactor determine_locale() for performance and readability.

Refactors the function to avoid unnecessary get_locale() calls and slightly improve performance, while keeping it readable.

Adds tests.

Props Cybr, spacedmonkey, swissspidy.
Fixes #58317.

Location:
trunk
Files:
1 added
1 edited

Legend:

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

    r55760 r55862  
    133133    $determined_locale = apply_filters( 'pre_determine_locale', null );
    134134
    135     if ( ! empty( $determined_locale ) && is_string( $determined_locale ) ) {
     135    if ( $determined_locale && is_string( $determined_locale ) ) {
    136136        return $determined_locale;
    137137    }
    138138
    139     $determined_locale = get_locale();
    140 
    141     if ( is_admin() ) {
     139    if (
     140        isset( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] &&
     141        ( ! empty( $_GET['wp_lang'] ) || ! empty( $_COOKIE['wp_lang'] ) )
     142    ) {
     143        if ( ! empty( $_GET['wp_lang'] ) ) {
     144            $determined_locale = sanitize_locale_name( $_GET['wp_lang'] );
     145        } else {
     146            $determined_locale = sanitize_locale_name( $_COOKIE['wp_lang'] );
     147        }
     148    } else if (
     149        ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() ) ||
     150        is_admin()
     151    ) {
    142152        $determined_locale = get_user_locale();
    143     }
    144 
    145     if ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() ) {
    146         $determined_locale = get_user_locale();
    147     }
    148 
    149     $wp_lang = '';
    150 
    151     if ( ! empty( $_GET['wp_lang'] ) ) {
    152         $wp_lang = sanitize_locale_name( wp_unslash( $_GET['wp_lang'] ) );
    153     } elseif ( ! empty( $_COOKIE['wp_lang'] ) ) {
    154         $wp_lang = sanitize_locale_name( wp_unslash( $_COOKIE['wp_lang'] ) );
    155     }
    156 
    157     if ( ! empty( $wp_lang ) && ! empty( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) {
    158         $determined_locale = $wp_lang;
     153    } else {
     154        $determined_locale = get_locale();
    159155    }
    160156
Note: See TracChangeset for help on using the changeset viewer.