Make WordPress Core

Changeset 45813


Ignore:
Timestamp:
08/15/2019 10:11:07 PM (5 years ago)
Author:
kadamwhite
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

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

    r45811 r45813  
    211211
    212212        if ( in_array( 'types', $fields, true ) ) {
    213             $data['types'] = $taxonomy->object_type;
     213            $data['types'] = array_values( $taxonomy->object_type );
    214214        }
    215215
  • trunk/tests/phpunit/tests/rest-api/rest-taxonomies-controller.php

    r43571 r45813  
    194194            array_keys( $response->get_data() )
    195195        );
     196    }
     197
     198    public function test_object_types_is_an_array_if_object_type_is_unregistered() {
     199        register_taxonomy_for_object_type( 'category', 'page' );
     200        register_taxonomy_for_object_type( 'category', 'attachment' );
     201        unregister_taxonomy_for_object_type( 'category', 'page' );
     202
     203        $request  = new WP_REST_Request( 'GET', '/wp/v2/taxonomies/category' );
     204        $response = rest_get_server()->dispatch( $request );
     205
     206        $types = $response->get_data()['types'];
     207        $this->assertArrayHasKey( 0, $types );
     208        $this->assertEquals( 'post', $types[0] );
     209        $this->assertArrayHasKey( 1, $types );
     210        $this->assertEquals( 'attachment', $types[1] );
    196211    }
    197212
Note: See TracChangeset for help on using the changeset viewer.