REST API: Always return post types list in taxonomies endpoint response as an array.

Prevents a non-sequential post type array such as [ 0 => 'post', 2 => 'page' ] from being improperly converted to an object in the taxonomy endpoint's response JSON.

Props TimothyBlynJacobs, birgire, spectacula.
Fixes #42209.


git-svn-id: https://develop.svn.wordpress.org/trunk@45813 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
K. Adam White 2019-08-15 22:11:07 +00:00
parent 8f100777e8
commit d65b89c5e4
2 changed files with 16 additions and 1 deletions

View File

@ -210,7 +210,7 @@ class WP_REST_Taxonomies_Controller extends WP_REST_Controller {
}
if ( in_array( 'types', $fields, true ) ) {
$data['types'] = $taxonomy->object_type;
$data['types'] = array_values( $taxonomy->object_type );
}
if ( in_array( 'show_cloud', $fields, true ) ) {

View File

@ -195,6 +195,21 @@ class WP_Test_REST_Taxonomies_Controller extends WP_Test_REST_Controller_Testcas
);
}
public function test_object_types_is_an_array_if_object_type_is_unregistered() {
register_taxonomy_for_object_type( 'category', 'page' );
register_taxonomy_for_object_type( 'category', 'attachment' );
unregister_taxonomy_for_object_type( 'category', 'page' );
$request = new WP_REST_Request( 'GET', '/wp/v2/taxonomies/category' );
$response = rest_get_server()->dispatch( $request );
$types = $response->get_data()['types'];
$this->assertArrayHasKey( 0, $types );
$this->assertEquals( 'post', $types[0] );
$this->assertArrayHasKey( 1, $types );
$this->assertEquals( 'attachment', $types[1] );
}
public function test_get_item_schema() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/taxonomies' );
$response = rest_get_server()->dispatch( $request );