Make WordPress Core


Ignore:
Timestamp:
10/31/2021 11:15:10 PM (2 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/src/wp-includes/rest-api.php

    r51958 r51962  
    30503050    }
    30513051
    3052     $post_type = get_post_type_object( $post->post_type );
    3053     if ( ! $post_type ) {
     3052    $post_type_route = rest_get_route_for_post_type_items( $post->post_type );
     3053    if ( ! $post_type_route ) {
    30543054        return '';
    30553055    }
    30563056
    3057     $controller = $post_type->get_rest_controller();
    3058     if ( ! $controller ) {
    3059         return '';
    3060     }
    3061 
    3062     $route = '';
    3063 
    3064     // The only two controllers that we can detect are the Attachments and Posts controllers.
    3065     if ( in_array( get_class( $controller ), array( 'WP_REST_Attachments_Controller', 'WP_REST_Posts_Controller' ), true ) ) {
    3066         $namespace = 'wp/v2';
    3067         $rest_base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
    3068         $route     = sprintf( '/%s/%s/%d', $namespace, $rest_base, $post->ID );
    3069     }
     3057    $route = sprintf( '%s/%d', $post_type_route, $post->ID );
    30703058
    30713059    /**
     
    30813069
    30823070/**
     3071 * Gets the REST API route for a post type.
     3072 *
     3073 * @since 5.9.0
     3074 *
     3075 * @param string $post_type The name of a registered post type.
     3076 * @return string The route path with a leading slash for the given post type, or an empty string if there is not a route.
     3077 */
     3078function rest_get_route_for_post_type_items( $post_type ) {
     3079    $post_type = get_post_type_object( $post_type );
     3080    if ( ! $post_type ) {
     3081        return '';
     3082    }
     3083
     3084    if ( ! $post_type->show_in_rest ) {
     3085        return '';
     3086    }
     3087
     3088    $namespace = ! empty( $post_type->rest_namespace ) ? $post_type->rest_namespace : 'wp/v2';
     3089    $rest_base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
     3090    $route     = sprintf( '/%s/%s', $namespace, $rest_base );
     3091
     3092    /**
     3093     * Filters the REST API route for a post type.
     3094     *
     3095     * @since 5.9.0
     3096     *
     3097     * @param string       $route      The route path.
     3098     * @param WP_Post_Type $post_type  The post type object.
     3099     */
     3100    return apply_filters( 'rest_route_for_post_type_items', $route, $post_type );
     3101}
     3102
     3103/**
    30833104 * Gets the REST API route for a term.
    30843105 *
Note: See TracChangeset for help on using the changeset viewer.