Make WordPress Core

Changeset 62396


Ignore:
Timestamp:
05/21/2026 06:52:59 AM (45 hours ago)
Author:
gziolo
Message:

Abilities: Return the site locale for the core/get-site-info language field

The core/get-site-info ability resolved the language field via get_bloginfo( 'language' ), which runs through determine_locale() and can return the current user's locale in the admin context. As a site-level ability, the language field should consistently represent the site locale. Use get_locale() (with _ normalized to -) instead so the value reflects the site locale regardless of the requesting user.

Props iamadisingh, afercia, audrasjb, yusufmudagal, r1k0, gziolo.
Fixes #64977.

Location:
trunk
Files:
2 edited

Legend:

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

    r61690 r62396  
    112112                $result = array();
    113113                foreach ( $requested_fields as $field ) {
    114                     $result[ $field ] = get_bloginfo( $field );
     114                    if ( 'language' === $field ) {
     115                        $result[ $field ] = str_replace( '_', '-', get_locale() );
     116                    } else {
     117                        $result[ $field ] = get_bloginfo( $field );
     118                    }
    115119                }
    116120
  • trunk/tests/phpunit/tests/abilities-api/wpRegisterCoreAbilities.php

    r61363 r62396  
    114114        $this->assertSame( get_bloginfo( 'name' ), $result['name'] );
    115115        $this->assertSame( get_bloginfo( 'url' ), $result['url'] );
     116    }
     117
     118    /**
     119     * Tests `core/get-site-info` language field returns the site locale, not the current user's locale.
     120     * @ticket 64977
     121     */
     122    public function test_core_get_site_info_language_uses_site_locale(): void {
     123        $admin_id = self::factory()->user->create(
     124            array(
     125                'role'   => 'administrator',
     126                'locale' => 'de_DE',
     127            )
     128        );
     129        wp_set_current_user( $admin_id );
     130
     131        $ability = wp_get_ability( 'core/get-site-info' );
     132        $result  = $ability->execute(
     133            array(
     134                'fields' => array( 'language' ),
     135            )
     136        );
     137
     138        $this->assertIsArray( $result );
     139        $this->assertArrayHasKey( 'language', $result );
     140        // Should return the site locale (en-US), not the user locale (de-DE).
     141        $this->assertSame( 'en-US', $result['language'] );
    116142    }
    117143
Note: See TracChangeset for help on using the changeset viewer.