Make WordPress Core

Changeset 39090


Ignore:
Timestamp:
11/02/2016 06:23:12 AM (8 years ago)
Author:
rmccue
Message:

REST API: Add locale to user resource.

Props ocean90, joehoyle.
Fixes #38528.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    r39085 r39090  
    697697        }
    698698
     699        if ( ! empty( $schema['properties']['locale'] ) ) {
     700            $data['locale'] = get_user_locale( $user );
     701        }
     702
    699703        if ( ! empty( $schema['properties']['nickname'] ) ) {
    700704            $data['nickname'] = $user->nickname;
     
    832836        if ( isset( $request['url'] ) && ! empty( $schema['properties']['url'] ) ) {
    833837            $prepared_user->user_url = $request['url'];
     838        }
     839
     840        if ( isset( $request['locale'] ) && ! empty( $schema['properties']['locale'] ) ) {
     841            $prepared_user->locale = $request['locale'];
    834842        }
    835843
     
    978986                    'context'     => array( 'embed', 'view', 'edit' ),
    979987                    'readonly'    => true,
     988                ),
     989                'locale'    => array(
     990                    'description' => __( 'Locale for the resource.' ),
     991                    'type'        => 'string',
     992                    'enum'        => get_available_languages(),
     993                    'context'     => array( 'edit' ),
    980994                ),
    981995                'nickname'    => array(
  • trunk/tests/phpunit/tests/rest-api/rest-users-controller.php

    r39085 r39090  
    778778            'first_name' => 'Old Name',
    779779            'user_url' => 'http://apple.com',
     780            'locale' => 'en_US',
    780781        ));
    781782        $this->allow_user_to_manage_multisite();
     
    789790        $_POST['first_name'] = 'New Name';
    790791        $_POST['url'] = 'http://google.com';
     792        $_POST['locale'] = 'de_DE';
    791793
    792794        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) );
     
    805807        $this->assertEquals( 'http://google.com', $new_data['url'] );
    806808        $this->assertEquals( 'http://google.com', $user->user_url );
     809        $this->assertEquals( 'de_DE', $user->locale );
    807810
    808811        // Check that we haven't inadvertently changed the user's password,
     
    822825        $this->assertInstanceOf( 'WP_Error', $response->as_error() );
    823826        $this->assertEquals( 'rest_user_invalid_email', $response->as_error()->get_error_code() );
     827    }
     828
     829    public function test_update_item_invalid_locale() {
     830        $user1 = $this->factory->user->create( array( 'user_login' => 'test_json_user', 'user_email' => 'testjson@example.com' ) );
     831        $this->allow_user_to_manage_multisite();
     832        wp_set_current_user( self::$user );
     833
     834        $request = new WP_REST_Request( 'PUT', '/wp/v2/users/' . $user1 );
     835        $request->set_param( 'locale', 'klingon' );
     836        $response = $this->server->dispatch( $request );
     837        $this->assertInstanceOf( 'WP_Error', $response->as_error() );
     838        $this->assertEquals( 'rest_invalid_param', $response->as_error()->get_error_code() );
    824839    }
    825840
     
    11401155        $properties = $data['schema']['properties'];
    11411156
    1142         $this->assertEquals( 18, count( $properties ) );
     1157        $this->assertEquals( 19, count( $properties ) );
    11431158        $this->assertArrayHasKey( 'avatar_urls', $properties );
    11441159        $this->assertArrayHasKey( 'capabilities', $properties );
     
    11501165        $this->assertArrayHasKey( 'last_name', $properties );
    11511166        $this->assertArrayHasKey( 'link', $properties );
     1167        $this->assertArrayHasKey( 'locale', $properties );
    11521168        $this->assertArrayHasKey( 'meta', $properties );
    11531169        $this->assertArrayHasKey( 'name', $properties );
     
    12981314            $this->assertEquals( $user->user_login, $data['username'] );
    12991315            $this->assertEquals( $user->roles, $data['roles'] );
     1316            $this->assertEquals( get_user_locale( $user ), $data['locale'] );
    13001317        }
    13011318
Note: See TracChangeset for help on using the changeset viewer.