I18N: Add ability to change user's locale back to site's locale.
Previously there was no way to remove the user locale setting again, even though that might be desirable. This adds a new 'Site Default' option to the user-specific language setting by introducing a new `show_site_locale_default` argument to `wp_dropdown_languages()`. Props ocean90. See #29783. Fixes #38632. git-svn-id: https://develop.svn.wordpress.org/trunk@39169 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
5f610350e1
commit
21e874e00a
@ -98,11 +98,13 @@ function edit_user( $user_id = 0 ) {
|
||||
|
||||
if ( isset( $_POST['locale'] ) ) {
|
||||
$locale = sanitize_text_field( $_POST['locale'] );
|
||||
if ( ! in_array( $locale, get_available_languages(), true ) ) {
|
||||
if ( 'site-default' === $locale ) {
|
||||
$locale = '';
|
||||
} elseif ( ! in_array( $locale, get_available_languages(), true ) ) {
|
||||
$locale = 'en_US';
|
||||
}
|
||||
|
||||
$user->locale = ( '' === $locale ) ? 'en_US' : $locale;
|
||||
$user->locale = $locale;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -284,9 +284,9 @@ if ( $languages ) : ?>
|
||||
$user_locale = $profileuser->locale;
|
||||
|
||||
if ( 'en_US' === $user_locale ) {
|
||||
$user_locale = false;
|
||||
} elseif ( ! in_array( $user_locale, $languages, true ) ) {
|
||||
$user_locale = get_locale();
|
||||
$user_locale = '';
|
||||
} elseif ( '' === $user_locale || ! in_array( $user_locale, $languages, true ) ) {
|
||||
$user_locale = 'site-default';
|
||||
}
|
||||
|
||||
wp_dropdown_languages( array(
|
||||
@ -294,7 +294,8 @@ if ( $languages ) : ?>
|
||||
'id' => 'locale',
|
||||
'selected' => $user_locale,
|
||||
'languages' => $languages,
|
||||
'show_available_translations' => false
|
||||
'show_available_translations' => false,
|
||||
'show_site_locale_default' => true
|
||||
) );
|
||||
?>
|
||||
</td>
|
||||
|
@ -1048,6 +1048,7 @@ function wp_get_pomo_file_data( $po_file ) {
|
||||
*
|
||||
* @since 4.0.0
|
||||
* @since 4.3.0 Introduced the `echo` argument.
|
||||
* @since 4.7.0 Introduced the `show_site_locale_default` argument.
|
||||
*
|
||||
* @see get_available_languages()
|
||||
* @see wp_get_available_translations()
|
||||
@ -1065,6 +1066,7 @@ function wp_get_pomo_file_data( $po_file ) {
|
||||
* @type bool|int $echo Whether to echo the generated markup. Accepts 0, 1, or their
|
||||
* boolean equivalents. Default 1.
|
||||
* @type bool $show_available_translations Whether to show available translations. Default true.
|
||||
* @type bool $show_site_locale_default Whether to show an option to fall back to the site's locale. Default false.
|
||||
* }
|
||||
* @return string HTML content
|
||||
*/
|
||||
@ -1078,8 +1080,14 @@ function wp_dropdown_languages( $args = array() ) {
|
||||
'selected' => '',
|
||||
'echo' => 1,
|
||||
'show_available_translations' => true,
|
||||
'show_site_locale_default' => false,
|
||||
) );
|
||||
|
||||
// English (United States) uses an empty string for the value attribute.
|
||||
if ( 'en_US' === $args['selected'] ) {
|
||||
$args['selected'] = '';
|
||||
}
|
||||
|
||||
$translations = $args['translations'];
|
||||
if ( empty( $translations ) ) {
|
||||
require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
|
||||
@ -1122,7 +1130,20 @@ function wp_dropdown_languages( $args = array() ) {
|
||||
if ( $translations_available ) {
|
||||
$structure[] = '<optgroup label="' . esc_attr_x( 'Installed', 'translations' ) . '">';
|
||||
}
|
||||
$structure[] = '<option value="" lang="en" data-installed="1">English (United States)</option>';
|
||||
|
||||
if ( $args['show_site_locale_default'] ) {
|
||||
$structure[] = sprintf(
|
||||
'<option value="site-default" data-installed="1"%s>%s</option>',
|
||||
selected( 'site-default', $args['selected'], false ),
|
||||
_x( 'Site Default', 'default site language' )
|
||||
);
|
||||
}
|
||||
|
||||
$structure[] = sprintf(
|
||||
'<option value="" lang="en" data-installed="1"%s>English (United States)</option>',
|
||||
selected( '', $args['selected'], false )
|
||||
);
|
||||
|
||||
foreach ( $languages as $language ) {
|
||||
$structure[] = sprintf(
|
||||
'<option value="%s" lang="%s"%s data-installed="1">%s</option>',
|
||||
|
@ -1074,7 +1074,7 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
|
||||
'locale' => array(
|
||||
'description' => __( 'Locale for the resource.' ),
|
||||
'type' => 'string',
|
||||
'enum' => array_merge( array( 'en_US' ), get_available_languages() ),
|
||||
'enum' => array_merge( array( '', 'en_US' ), get_available_languages() ),
|
||||
'context' => array( 'edit' ),
|
||||
),
|
||||
'nickname' => array(
|
||||
|
@ -99,6 +99,50 @@ class Tests_L10n extends WP_UnitTestCase {
|
||||
$this->assertContains( '<option value="it_IT" lang="it">Italiano</option>', $actual );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 38632
|
||||
*/
|
||||
function test_wp_dropdown_languages_site_default() {
|
||||
$args = array(
|
||||
'id' => 'foo',
|
||||
'name' => 'bar',
|
||||
'languages' => array( 'de_DE' ),
|
||||
'translations' => $this->wp_dropdown_languages_filter(),
|
||||
'selected' => 'de_DE',
|
||||
'echo' => false,
|
||||
'show_site_locale_default' => true,
|
||||
);
|
||||
$actual = wp_dropdown_languages( $args );
|
||||
|
||||
$this->assertContains( 'id="foo"', $actual );
|
||||
$this->assertContains( 'name="bar"', $actual );
|
||||
$this->assertContains( '<option value="site-default" data-installed="1">Site Default</option>', $actual );
|
||||
$this->assertContains( '<option value="" lang="en" data-installed="1">English (United States)</option>', $actual );
|
||||
$this->assertContains( '<option value="de_DE" lang="de" selected=\'selected\' data-installed="1">Deutsch</option>', $actual );
|
||||
$this->assertContains( '<option value="it_IT" lang="it">Italiano</option>', $actual );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 38632
|
||||
*/
|
||||
function test_wp_dropdown_languages_en_US_selected() {
|
||||
$args = array(
|
||||
'id' => 'foo',
|
||||
'name' => 'bar',
|
||||
'languages' => array( 'de_DE' ),
|
||||
'translations' => $this->wp_dropdown_languages_filter(),
|
||||
'selected' => 'en_US',
|
||||
'echo' => false,
|
||||
);
|
||||
$actual = wp_dropdown_languages( $args );
|
||||
|
||||
$this->assertContains( 'id="foo"', $actual );
|
||||
$this->assertContains( 'name="bar"', $actual );
|
||||
$this->assertContains( '<option value="" lang="en" data-installed="1" selected=\'selected\'>English (United States)</option>', $actual );
|
||||
$this->assertContains( '<option value="de_DE" lang="de" data-installed="1">Deutsch</option>', $actual );
|
||||
$this->assertContains( '<option value="it_IT" lang="it">Italiano</option>', $actual );
|
||||
}
|
||||
|
||||
/**
|
||||
* We don't want to call the API when testing.
|
||||
*
|
||||
|
@ -879,6 +879,25 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase {
|
||||
$this->assertEquals( 'en_US', $user->locale );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 38632
|
||||
*/
|
||||
public function test_update_item_empty_locale() {
|
||||
$user_id = $this->factory->user->create( array( 'user_login' => 'test_json_user', 'user_email' => 'testjson@example.com', 'locale' => 'de_DE' ) );
|
||||
$this->allow_user_to_manage_multisite();
|
||||
wp_set_current_user( self::$user );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wp/v2/users/' . $user_id );
|
||||
$request->set_param( 'locale', '' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->check_add_edit_user_response( $response, true );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( get_locale(), $data['locale'] );
|
||||
$user = get_userdata( $user_id );
|
||||
$this->assertEquals( '', $user->locale );
|
||||
}
|
||||
|
||||
public function test_update_item_username_attempt() {
|
||||
$user1 = $this->factory->user->create( array( 'user_login' => 'test_json_user', 'user_email' => 'testjson@example.com' ) );
|
||||
$user2 = $this->factory->user->create( array( 'user_login' => 'test_json_user2', 'user_email' => 'testjson2@example.com' ) );
|
||||
|
Loading…
x
Reference in New Issue
Block a user