Make WordPress Core

Ticket #38607: post-type-links.patch

File post-type-links.patch, 2.4 KB (added by youknowriad, 9 years ago)

Links to Post Types

  • src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php

     
    197197                $response = rest_ensure_response( $data );
    198198
    199199                $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
    200                 $response->add_links( array(
     200                $links = array(
    201201                        'collection'                => array(
    202202                                'href'                  => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
    203203                        ),
     
    204204                        'https://api.w.org/items'   => array(
    205205                                'href'                  => rest_url( sprintf( 'wp/v2/%s', $base ) ),
    206206                        ),
    207                 ) );
     207                );
    208208
     209                $post_type_links = array();
     210                foreach ( $taxonomy->object_type as $type ) {
     211                        $post_type_object = get_post_type_object( $type );
     212
     213                        // Skip post types that are not public.
     214                        if ( empty( $post_type_object->show_in_rest ) ) {
     215                                continue;
     216                        }
     217
     218                        $rest_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
     219                        $post_type_links[] = array(
     220                                'href'      => rest_url( 'wp/v2/' . $rest_base ),
     221                                'post_type' => $type,
     222                        );
     223                }
     224
     225                if ( ! empty( $post_type_links ) ) {
     226                        $links['https://api.w.org/post_type'] = $post_type_links;
     227                }
     228
     229                $response->add_links( $links );
     230
    209231                /**
    210232                 * Filters a taxonomy returned from the REST API.
    211233                 *
  • tests/phpunit/tests/rest-api/rest-taxonomies-controller.php

     
    170170                $this->assertEquals( $tax_obj->hierarchical, $data['hierarchical'] );
    171171                $this->assertEquals( rest_url( 'wp/v2/taxonomies' ), $links['collection'][0]['href'] );
    172172                $this->assertArrayHasKey( 'https://api.w.org/items', $links );
     173                $this->assertArrayHasKey( 'https://api.w.org/post_type', $links );
     174                $this->assertEquals( rest_url( 'wp/v2/posts' ), $links['https://api.w.org/post_type'][0]['href'] );
    173175                if ( 'edit' === $context ) {
    174176                        $this->assertEquals( $tax_obj->cap, $data['capabilities'] );
    175177                        $this->assertEquals( $tax_obj->labels, $data['labels'] );