Ticket #38420: 38420.3.diff
| File 38420.3.diff, 5.4 KB (added by , 9 years ago) |
|---|
-
src/wp-includes/rest-api/class-wp-rest-server.php
1184 1184 if ( isset( $opts['description'] ) ) { 1185 1185 $arg_data['description'] = $opts['description']; 1186 1186 } 1187 if ( isset( $opts['items'] ) ) { 1188 if ( isset( $opts['items']['enum'] ) ) { 1189 $arg_data['items']['enum'] = $opts['items']['enum']; 1190 } 1191 if ( isset( $opts['items']['type'] ) ) { 1192 $arg_data['items']['type'] = $opts['items']['type']; 1193 } 1194 } 1187 1195 $endpoint_data['args'][ $key ] = $arg_data; 1188 1196 } 1189 1197 } -
src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
1905 1905 $params['status'] = array( 1906 1906 'default' => 'publish', 1907 1907 'description' => __( 'Limit result set to posts assigned a specific status; can be comma-delimited list of status types.' ), 1908 'enum' => array_merge( array_keys( get_post_stati() ), array( 'any' ) ), 1909 'sanitize_callback' => 'sanitize_key', 1910 'type' => 'string', 1908 'sanitize_callback' => 'wp_parse_slug_list', 1909 'type' => 'array', 1910 'items' => array( 1911 'enum' => array_merge( array_keys( get_post_stati() ), array( 'any' ) ), 1912 'type' => 'string', 1913 ), 1911 1914 'validate_callback' => array( $this, 'validate_user_can_query_private_statuses' ), 1912 1915 ); 1913 1916 $params['filter'] = array( … … 1946 1949 * @return WP_Error|boolean 1947 1950 */ 1948 1951 public function validate_user_can_query_private_statuses( $value, $request, $parameter ) { 1949 if ( 'publish' === $value ) {1950 return true;1952 if ( 'publish' === $value || is_array( $value ) && count( 1 === $value ) && in_array( 'publish', $value, true ) ) { 1953 return rest_validate_request_arg( $value, $request, $param ); 1951 1954 } 1952 1955 $post_type_obj = get_post_type_object( $this->post_type ); 1953 1956 if ( current_user_can( $post_type_obj->cap->edit_posts ) ) { 1954 return true;1957 return rest_validate_request_arg( $value, $request, $param ); 1955 1958 } 1956 1959 return new WP_Error( 'rest_forbidden_status', __( 'Status is forbidden.' ), array( 'status' => rest_authorization_required_code() ) ); 1957 1960 } -
src/wp-includes/rest-api.php
825 825 } 826 826 } 827 827 828 // Handle enum arrays. 829 if ( 'array' === $args['type'] && ! empty( $args['items']['enum'] ) && ! empty( $args['items']['type'] ) && 'string' === $args['items']['type'] ) { 830 $values = wp_parse_slug_list( $value ); 831 $wrong_params = array(); 832 foreach ( $values as $val ) { 833 if ( ! in_array( $val, $args['items']['enum'], true ) ) { 834 $wrong_params[] = $val; 835 } 836 } 837 838 if ( count( $wrong_params ) > 0 ) { 839 return new WP_Error( 'rest_invalid_param', sprintf( /* translators: 1: parameter, 2: list of valid values */ __( '%1$s contains values not of %2$s.' ), $param, implode( ', ', $args['items']['enum'] ) ) ); 840 } 841 } 842 828 843 if ( 'integer' === $args['type'] && ! is_numeric( $value ) ) { 829 844 return new WP_Error( 'rest_invalid_param', sprintf( /* translators: 1: parameter, 2: type name */ __( '%1$s is not of type %2$s.' ), $param, 'integer' ) ); 830 845 } -
tests/phpunit/tests/rest-api/rest-posts-controller.php
240 240 $this->assertEquals( 1, count( $response->get_data() ) ); 241 241 } 242 242 243 public function test_get_items_multiple_status_query_1() { 244 wp_set_current_user( 0 ); 245 $this->factory->post->create( array( 'post_status' => 'draft' ) ); 246 247 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 248 $request->set_param( 'status', array( 'publish' ) ); 249 $response = $this->server->dispatch( $request ); 250 $this->assertEquals( 200, $response->get_status() ); 251 $this->assertEquals( 1, count( $response->get_data() ) ); 252 253 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 254 $request->set_param( 'status', array( 'draft', 'post' ) ); 255 $response = $this->server->dispatch( $request ); 256 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 257 258 wp_set_current_user( $this->editor_id ); 259 260 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 261 $request->set_param( 'status', 'draft,publish' ); 262 $response = $this->server->dispatch( $request ); 263 $this->assertEquals( 200, $response->get_status() ); 264 $this->assertEquals( 2, count( $response->get_data() ) ); 265 266 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 267 $request->set_param( 'status', array( 'draft', 'publish' ) ); 268 $response = $this->server->dispatch( $request ); 269 $this->assertEquals( 200, $response->get_status() ); 270 $this->assertEquals( 2, count( $response->get_data() ) ); 271 } 272 243 273 public function test_get_items_status_without_permissions() { 244 274 $draft_id = $this->factory->post->create( array( 245 275 'post_status' => 'draft',