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/src/wp-includes/rest-api.php

    r51962 r51964  
    31163116    }
    31173117
    3118     $taxonomy = get_taxonomy( $term->taxonomy );
    3119     if ( ! $taxonomy ) {
     3118    $taxonomy_route = rest_get_route_for_taxonomy_items( $term->taxonomy );
     3119    if ( ! $taxonomy_route ) {
    31203120        return '';
    31213121    }
    31223122
    3123     $controller = $taxonomy->get_rest_controller();
    3124     if ( ! $controller ) {
    3125         return '';
    3126     }
    3127 
    3128     $route = '';
    3129 
    3130     // The only controller that works is the Terms controller.
    3131     if ( $controller instanceof WP_REST_Terms_Controller ) {
    3132         $namespace = 'wp/v2';
    3133         $rest_base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
    3134         $route     = sprintf( '/%s/%s/%d', $namespace, $rest_base, $term->term_id );
    3135     }
     3123    $route = sprintf( '%s/%d', $taxonomy_route, $term->term_id );
    31363124
    31373125    /**
     
    31443132     */
    31453133    return apply_filters( 'rest_route_for_term', $route, $term );
     3134}
     3135
     3136/**
     3137 * Gets the REST API route for a taxonomy.
     3138 *
     3139 * @since 5.9.0
     3140 *
     3141 * @param string $taxonomy Name of taxonomy.
     3142 * @return string The route path with a leading slash for the given taxonomy.
     3143 */
     3144function rest_get_route_for_taxonomy_items( $taxonomy ) {
     3145    $taxonomy = get_taxonomy( $taxonomy );
     3146    if ( ! $taxonomy ) {
     3147        return '';
     3148    }
     3149
     3150    if ( ! $taxonomy->show_in_rest ) {
     3151        return '';
     3152    }
     3153
     3154    $namespace = ! empty( $taxonomy->rest_namespace ) ? $taxonomy->rest_namespace : 'wp/v2';
     3155    $rest_base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
     3156    $route     = sprintf( '/%s/%s', $namespace, $rest_base );
     3157
     3158    /**
     3159     * Filters the REST API route for a taxonomy.
     3160     *
     3161     * @since 5.9.0
     3162     *
     3163     * @param string      $route    The route path.
     3164     * @param WP_Taxonomy $taxonomy The taxonomy object.
     3165     */
     3166    return apply_filters( 'rest_route_for_taxonomy_items', $route, $taxonomy );
    31463167}
    31473168
Note: See TracChangeset for help on using the changeset viewer.