Make WordPress Core

Ticket #38632: 38632.5.diff

File 38632.5.diff, 8.2 KB (added by swissspidy, 8 years ago)
  • src/wp-admin/includes/user.php

    diff --git src/wp-admin/includes/user.php src/wp-admin/includes/user.php
    index 6bf826b..e5f84fa 100644
    function edit_user( $user_id = 0 ) { 
    9898
    9999                if ( isset( $_POST['locale'] ) ) {
    100100                        $locale = sanitize_text_field( $_POST['locale'] );
    101                         if ( ! in_array( $locale, get_available_languages(), true ) ) {
     101                        if ( 'site-default' === $locale ) {
    102102                                $locale = '';
     103                        } elseif ( ! in_array( $locale, get_available_languages(), true ) ) {
     104                                $locale = 'en_US';
    103105                        }
    104106
    105                         $user->locale = ( '' === $locale ) ? 'en_US' : $locale;
     107                        $user->locale = $locale;
    106108                }
    107109        }
    108110
  • src/wp-admin/user-edit.php

    diff --git src/wp-admin/user-edit.php src/wp-admin/user-edit.php
    index a85de3b..f1dc417 100644
    if ( $languages ) : ?> 
    284284                $user_locale = $profileuser->locale;
    285285
    286286                if ( 'en_US' === $user_locale ) {
    287                         $user_locale = false;
    288                 } elseif ( ! in_array( $user_locale, $languages, true ) ) {
    289                         $user_locale = get_locale();
     287                        $user_locale = '';
     288                } elseif ( '' === $user_locale || ! in_array( $user_locale, $languages, true ) ) {
     289                        $user_locale = 'site-default';
    290290                }
    291291
    292292                wp_dropdown_languages( array(
    if ( $languages ) : ?> 
    294294                        'id'                          => 'locale',
    295295                        'selected'                    => $user_locale,
    296296                        'languages'                   => $languages,
    297                         'show_available_translations' => false
     297                        'show_available_translations' => false,
     298                        'show_site_locale_default'    => true
    298299                ) );
    299300                ?>
    300301        </td>
  • src/wp-includes/l10n.php

    diff --git src/wp-includes/l10n.php src/wp-includes/l10n.php
    index 4ae7e2d..39346c4 100644
    function wp_get_pomo_file_data( $po_file ) { 
    10481048 *
    10491049 * @since 4.0.0
    10501050 * @since 4.3.0 Introduced the `echo` argument.
     1051 * @since 4.7.0 Introduced the `show_site_locale_default` argument.
    10511052 *
    10521053 * @see get_available_languages()
    10531054 * @see wp_get_available_translations()
    function wp_get_pomo_file_data( $po_file ) { 
    10651066 *     @type bool|int $echo                         Whether to echo the generated markup. Accepts 0, 1, or their
    10661067 *                                                  boolean equivalents. Default 1.
    10671068 *     @type bool     $show_available_translations  Whether to show available translations. Default true.
     1069 *     @type bool     $show_site_locale_default     Whether to show an option to fall back to the site's locale. Default false.
    10681070 * }
    10691071 * @return string HTML content
    10701072 */
    function wp_dropdown_languages( $args = array() ) { 
    10781080                'selected'     => '',
    10791081                'echo'         => 1,
    10801082                'show_available_translations' => true,
     1083                'show_site_locale_default'    => false,
    10811084        ) );
    10821085
     1086        // English (United States) uses an empty string for the value attribute.
     1087        if ( 'en_US' === $args['selected'] ) {
     1088                $args['selected'] = '';
     1089        }
     1090
    10831091        $translations = $args['translations'];
    10841092        if ( empty( $translations ) ) {
    10851093                require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
    function wp_dropdown_languages( $args = array() ) { 
    11221130        if ( $translations_available ) {
    11231131                $structure[] = '<optgroup label="' . esc_attr_x( 'Installed', 'translations' ) . '">';
    11241132        }
    1125         $structure[] = '<option value="" lang="en" data-installed="1">English (United States)</option>';
     1133
     1134        if ( $args['show_site_locale_default'] ) {
     1135                $structure[] = sprintf(
     1136                        '<option value="site-default" data-installed="1"%s>%s</option>',
     1137                        selected( 'site-default', $args['selected'], false ),
     1138                        _x( 'Site Default', 'default site language' )
     1139                );
     1140        }
     1141
     1142        $structure[] = sprintf(
     1143                '<option value="" lang="en" data-installed="1"%s>English (United States)</option>',
     1144                selected( '', $args['selected'], false )
     1145        );
     1146
    11261147        foreach ( $languages as $language ) {
    11271148                $structure[] = sprintf(
    11281149                        '<option value="%s" lang="%s"%s data-installed="1">%s</option>',
  • src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php
    index 9fece46..08fc183 100644
    class WP_REST_Users_Controller extends WP_REST_Controller { 
    10741074                                'locale'    => array(
    10751075                                        'description' => __( 'Locale for the resource.' ),
    10761076                                        'type'        => 'string',
    1077                                         'enum'        => array_merge( array( 'en_US' ), get_available_languages() ),
     1077                                        'enum'        => array_merge( array( '', 'en_US' ), get_available_languages() ),
    10781078                                        'context'     => array( 'edit' ),
    10791079                                ),
    10801080                                'nickname'    => array(
  • tests/phpunit/tests/l10n.php

    diff --git tests/phpunit/tests/l10n.php tests/phpunit/tests/l10n.php
    index 39065c9..b378c83 100644
    class Tests_L10n extends WP_UnitTestCase { 
    100100        }
    101101
    102102        /**
     103         * @ticket 38632
     104         */
     105        function test_wp_dropdown_languages_site_default() {
     106                $args = array(
     107                        'id'                       => 'foo',
     108                        'name'                     => 'bar',
     109                        'languages'                => array( 'de_DE' ),
     110                        'translations'             => $this->wp_dropdown_languages_filter(),
     111                        'selected'                 => 'de_DE',
     112                        'echo'                     => false,
     113                        'show_site_locale_default' => true,
     114                );
     115                $actual = wp_dropdown_languages( $args );
     116
     117                $this->assertContains( 'id="foo"', $actual );
     118                $this->assertContains( 'name="bar"', $actual );
     119                $this->assertContains( '<option value="site-default" data-installed="1"Site Default></option>', $actual );
     120                $this->assertContains( '<option value="" lang="en" data-installed="1">English (United States)</option>', $actual );
     121                $this->assertContains( '<option value="de_DE" lang="de" selected=\'selected\' data-installed="1">Deutsch</option>', $actual );
     122                $this->assertContains( '<option value="it_IT" lang="it">Italiano</option>', $actual );
     123        }
     124
     125        /**
     126         * @ticket 38632
     127         */
     128        function test_wp_dropdown_languages_en_US_selected() {
     129                $args = array(
     130                        'id'           => 'foo',
     131                        'name'         => 'bar',
     132                        'languages'    => array( 'de_DE' ),
     133                        'translations' => $this->wp_dropdown_languages_filter(),
     134                        'selected'     => 'en_US',
     135                        'echo'         => false,
     136                );
     137                $actual = wp_dropdown_languages( $args );
     138
     139                $this->assertContains( 'id="foo"', $actual );
     140                $this->assertContains( 'name="bar"', $actual );
     141                $this->assertContains( '<option value="" lang="en" data-installed="1" selected=\'selected\'>English (United States)</option>', $actual );
     142                $this->assertContains( '<option value="de_DE" lang="de" data-installed="1">Deutsch</option>', $actual );
     143                $this->assertContains( '<option value="it_IT" lang="it">Italiano</option>', $actual );
     144        }
     145
     146        /**
    103147         * We don't want to call the API when testing.
    104148         *
    105149         * @return array
  • tests/phpunit/tests/rest-api/rest-users-controller.php

    diff --git tests/phpunit/tests/rest-api/rest-users-controller.php tests/phpunit/tests/rest-api/rest-users-controller.php
    index 09e11a6..08a1597 100644
    class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 
    879879                $this->assertEquals( 'en_US', $user->locale );
    880880        }
    881881
     882        /**
     883         * @ticket 38632
     884         */
     885        public function test_update_item_empty_locale() {
     886                $user_id = $this->factory->user->create( array( 'user_login' => 'test_json_user', 'user_email' => 'testjson@example.com', 'locale' => 'de_DE' ) );
     887                $this->allow_user_to_manage_multisite();
     888                wp_set_current_user( self::$user );
     889
     890                $request = new WP_REST_Request( 'PUT', '/wp/v2/users/' . $user_id );
     891                $request->set_param( 'locale', '' );
     892                $response = $this->server->dispatch( $request );
     893                $this->check_add_edit_user_response( $response, true );
     894
     895                $data = $response->get_data();
     896                $this->assertEquals( get_locale(), $data['locale'] );
     897                $user = get_userdata( $user_id );
     898                $this->assertEquals( '', $user->locale );
     899        }
     900
    882901        public function test_update_item_username_attempt() {
    883902                $user1 = $this->factory->user->create( array( 'user_login' => 'test_json_user', 'user_email' => 'testjson@example.com' ) );
    884903                $user2 = $this->factory->user->create( array( 'user_login' => 'test_json_user2', 'user_email' => 'testjson2@example.com' ) );