Make WordPress Core

Ticket #38528: 38528.patch

File 38528.patch, 3.0 KB (added by ocean90, 7 years ago)
  • src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

     
    596596                        $data['link'] = get_author_posts_url( $user->ID, $user->user_nicename );
    597597                }
    598598
     599                if ( ! empty( $schema['properties']['locale'] ) ) {
     600                        $data['locale'] = get_user_locale( $user );
     601                }
     602
    599603                if ( ! empty( $schema['properties']['nickname'] ) ) {
    600604                        $data['nickname'] = $user->nickname;
    601605                }
     
    847851                                        'context'     => array( 'embed', 'view', 'edit' ),
    848852                                        'readonly'    => true,
    849853                                ),
     854                                'locale'    => array(
     855                                        'description' => __( 'Locale for the resource.' ),
     856                                        'type'        => 'string',
     857                                        'context'     => array( 'edit' ),
     858                                        'arg_options' => array(
     859                                                'sanitize_callback' => array( $this, 'sanitize_locale' ),
     860                                        ),
     861                                ),
    850862                                'nickname'    => array(
    851863                                        'description' => __( 'The nickname for the resource.' ),
    852864                                        'type'        => 'string',
     
    987999                );
    9881000                return $query_params;
    9891001        }
     1002
     1003        /**
     1004         * Sanitizes the locale value.
     1005         *
     1006         * @since 4.7.0
     1007         *
     1008         * @param string $locale Locale value passed in request.
     1009         * @return string Sanitized value for the locale.
     1010         */
     1011        public function sanitize_locale( $locale ) {
     1012                $locale = sanitize_text_field( $locale );
     1013                if ( ! in_array( $locale, get_available_languages(), true ) ) {
     1014                        $locale = '';
     1015                }
     1016
     1017                return ( '' === $locale ) ? 'en_US' : $locale;
     1018        }
    9901019}
  • tests/phpunit/tests/rest-api/rest-users-controller.php

     
    11161116                $data = $response->get_data();
    11171117                $properties = $data['schema']['properties'];
    11181118
    1119                 $this->assertEquals( 18, count( $properties ) );
     1119                $this->assertEquals( 19, count( $properties ) );
    11201120                $this->assertArrayHasKey( 'avatar_urls', $properties );
    11211121                $this->assertArrayHasKey( 'capabilities', $properties );
    11221122                $this->assertArrayHasKey( 'description', $properties );
     
    11261126                $this->assertArrayHasKey( 'id', $properties );
    11271127                $this->assertArrayHasKey( 'last_name', $properties );
    11281128                $this->assertArrayHasKey( 'link', $properties );
     1129                $this->assertArrayHasKey( 'locale', $properties );
    11291130                $this->assertArrayHasKey( 'meta', $properties );
    11301131                $this->assertArrayHasKey( 'name', $properties );
    11311132                $this->assertArrayHasKey( 'nickname', $properties );
     
    12611262                $this->assertEquals( $user->user_url, $data['url'] );
    12621263                $this->assertEquals( $user->description, $data['description'] );
    12631264                $this->assertEquals( get_author_posts_url( $user->ID ), $data['link'] );
     1265                $this->assertEquals( get_user_locale( $user ), $data['locale'] );
    12641266                $this->assertArrayHasKey( 'avatar_urls', $data );
    12651267                $this->assertEquals( $user->user_nicename, $data['slug'] );
    12661268