Make WordPress Core


Ignore:
Timestamp:
11/01/2021 03:26:06 AM (3 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Support custom namespaces for taxonomies.

While a taxonomy 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_taxonomy_items function has been introduced and the rest_get_route_for_term function updated to facilitate getting the correct route for taxonomies.

For maximum compatibility sticking with the default wp/v2 namespace is recommended until the API functions see wider use.

Props spacedmonkey.
Fixes #54267.
See [51962].

File:
1 edited

Legend:

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

    r51962 r51964  
    19641964
    19651965    /**
     1966     * @ticket 54267
     1967     */
     1968    public function test_rest_get_route_for_taxonomy_custom_namespace() {
     1969        register_taxonomy(
     1970            'ct',
     1971            'post',
     1972            array(
     1973                'show_in_rest'   => true,
     1974                'rest_base'      => 'ct',
     1975                'rest_namespace' => 'wordpress/v1',
     1976            )
     1977        );
     1978        $term = self::factory()->term->create_and_get( array( 'taxonomy' => 'ct' ) );
     1979
     1980        $this->assertSame( '/wordpress/v1/ct/' . $term->term_id, rest_get_route_for_term( $term ) );
     1981        unregister_taxonomy( 'ct' );
     1982    }
     1983
     1984    /**
     1985     * @ticket 54267
     1986     */
     1987    public function test_rest_get_route_for_taxonomy_items() {
     1988        $this->assertSame( '/wp/v2/categories', rest_get_route_for_taxonomy_items( 'category' ) );
     1989    }
     1990
     1991    /**
     1992     * @ticket 54267
     1993     */
     1994    public function test_rest_get_route_for_taxonomy_items_custom_namespace() {
     1995        register_taxonomy(
     1996            'ct',
     1997            'post',
     1998            array(
     1999                'show_in_rest'   => true,
     2000                'rest_base'      => 'ct',
     2001                'rest_namespace' => 'wordpress/v1',
     2002            )
     2003        );
     2004
     2005        $this->assertSame( '/wordpress/v1/ct', rest_get_route_for_taxonomy_items( 'ct' ) );
     2006        unregister_post_type( 'ct' );
     2007    }
     2008
     2009    /**
    19662010     * @ticket 50300
    19672011     *
Note: See TracChangeset for help on using the changeset viewer.