Make WordPress Core

Changeset 56003


Ignore:
Timestamp:
06/23/2023 04:01:08 PM (18 months ago)
Author:
swissspidy
Message:

I18N: Ensure determine_locale() does not potentially return an empty string.

Call get_locale() as a last resort in case the sanitized locale is an empty string.

Also swaps conditionals to cater for more typical use case and adds tests.

Follow-up to [55862]

Props Cybr, swissspidy.
Fixes #58317.

Location:
trunk
Files:
2 edited

Legend:

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

    r55990 r56003  
    146146            $determined_locale = sanitize_locale_name( $_COOKIE['wp_lang'] );
    147147        }
    148     } else if (
    149         ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() ) ||
    150         is_admin()
     148    } elseif (
     149        is_admin() ||
     150        ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() )
    151151    ) {
    152152        $determined_locale = get_user_locale();
    153     } else {
     153    }
     154
     155    if ( ! $determined_locale ) {
    154156        $determined_locale = get_locale();
    155157    }
  • trunk/tests/phpunit/tests/l10n/determineLocale.php

    r55862 r56003  
    212212    }
    213213
     214    public function test_wp_login_get_param_on_login_page_incorrect_string() {
     215        add_filter(
     216            'locale',
     217            static function() {
     218                return 'siteLocale';
     219            }
     220        );
     221
     222        wp_set_current_user( self::$user_id );
     223
     224        $GLOBALS['pagenow'] = 'wp-login.php';
     225        $_GET['wp_lang']    = '###'; // Something sanitize_locale_name() strips away.
     226
     227        $this->assertSame( 'siteLocale', determine_locale() );
     228    }
     229
    214230    public function test_wp_login_cookie_not_on_login_page() {
    215231        add_filter(
Note: See TracChangeset for help on using the changeset viewer.