Make WordPress Core


Ignore:
Timestamp:
10/31/2021 11:15:10 PM (4 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Support custom namespaces for custom post types.

While a custom post type can define a custom route by using the rest_base argument, a namespace of wp/v2 was assumed. This commit introduces support for a rest_namespace argument.

A new rest_get_route_for_post_type_items function has been introduced and the rest_get_route_for_post function updated to facilitate getting the correct route for custom post types.

While the WordPress Core Block Editor bootstrap code has been updated to use these API functions, for maximum compatibility sticking with the default wp/v2 namespace is recommended until the API functions see wider use.

Props spacedmonkey, swissspidy.
Fixes #53656.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api.php

    r51648 r51962  
    18311831
    18321832    /**
     1833     * @ticket 53656
     1834     */
     1835    public function test_rest_get_route_for_post_custom_namespace() {
     1836        register_post_type(
     1837            'cpt',
     1838            array(
     1839                'show_in_rest'   => true,
     1840                'rest_base'      => 'cpt',
     1841                'rest_namespace' => 'wordpress/v1',
     1842            )
     1843        );
     1844        $post = self::factory()->post->create_and_get( array( 'post_type' => 'cpt' ) );
     1845
     1846        $this->assertSame( '/wordpress/v1/cpt/' . $post->ID, rest_get_route_for_post( $post ) );
     1847        unregister_post_type( 'cpt' );
     1848    }
     1849
     1850    /**
     1851     * @ticket 53656
     1852     */
     1853    public function test_rest_get_route_for_post_type_items() {
     1854        $this->assertSame( '/wp/v2/posts', rest_get_route_for_post_type_items( 'post' ) );
     1855    }
     1856
     1857    /**
     1858     * @ticket 53656
     1859     */
     1860    public function test_rest_get_route_for_post_type_items_custom_namespace() {
     1861        register_post_type(
     1862            'cpt',
     1863            array(
     1864                'show_in_rest'   => true,
     1865                'rest_base'      => 'cpt',
     1866                'rest_namespace' => 'wordpress/v1',
     1867            )
     1868        );
     1869
     1870        $this->assertSame( '/wordpress/v1/cpt', rest_get_route_for_post_type_items( 'cpt' ) );
     1871        unregister_post_type( 'cpt' );
     1872    }
     1873
     1874    /**
    18331875     * @ticket 49116
    18341876     */
     
    18401882    /**
    18411883     * @ticket 49116
     1884     * @ticket 53656
    18421885     */
    18431886    public function test_rest_get_route_for_post_custom_controller() {
    18441887        $post = self::factory()->post->create_and_get( array( 'post_type' => 'wp_block' ) );
    1845         $this->assertSame( '', rest_get_route_for_post( $post ) );
     1888        $this->assertSame( '/wp/v2/blocks/' . $post->ID, rest_get_route_for_post( $post ) );
    18461889    }
    18471890
Note: See TracChangeset for help on using the changeset viewer.