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. Built from https://develop.svn.wordpress.org/trunk@51962 git-svn-id: http://core.svn.wordpress.org/trunk@51551 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
@@ -1288,7 +1288,7 @@ class WP_REST_Server {
|
||||
if ( $site_logo_id ) {
|
||||
$response->add_link(
|
||||
'https://api.w.org/featuredmedia',
|
||||
rest_url( 'wp/v2/media/' . $site_logo_id ),
|
||||
rest_url( rest_get_route_for_post( $site_logo_id ) ),
|
||||
array(
|
||||
'embeddable' => true,
|
||||
)
|
||||
|
||||
@@ -67,8 +67,8 @@ class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller {
|
||||
|
||||
$this->parent_controller = $parent_controller;
|
||||
$this->revisions_controller = new WP_REST_Revisions_Controller( $parent_post_type );
|
||||
$this->namespace = 'wp/v2';
|
||||
$this->rest_base = 'autosaves';
|
||||
$this->namespace = ! empty( $post_type_object->rest_namespace ) ? $post_type_object->rest_namespace : 'wp/v2';
|
||||
$this->parent_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
|
||||
}
|
||||
|
||||
|
||||
@@ -263,10 +263,11 @@ class WP_REST_Post_Statuses_Controller extends WP_REST_Controller {
|
||||
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$rest_url = rest_url( rest_get_route_for_post_type_items( 'post' ) );
|
||||
if ( 'publish' === $status->name ) {
|
||||
$response->add_link( 'archives', rest_url( 'wp/v2/posts' ) );
|
||||
$response->add_link( 'archives', $rest_url );
|
||||
} else {
|
||||
$response->add_link( 'archives', add_query_arg( 'status', $status->name, rest_url( 'wp/v2/posts' ) ) );
|
||||
$response->add_link( 'archives', add_query_arg( 'status', $status->name, $rest_url ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -180,6 +180,7 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller {
|
||||
$taxonomies = wp_list_filter( get_object_taxonomies( $post_type->name, 'objects' ), array( 'show_in_rest' => true ) );
|
||||
$taxonomies = wp_list_pluck( $taxonomies, 'name' );
|
||||
$base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
|
||||
$namespace = ! empty( $post_type->rest_namespace ) ? $post_type->rest_namespace : 'wp/v2';
|
||||
$supports = get_all_post_type_supports( $post_type->name );
|
||||
|
||||
$fields = $this->get_fields_for_response( $request );
|
||||
@@ -232,6 +233,10 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller {
|
||||
$data['rest_base'] = $base;
|
||||
}
|
||||
|
||||
if ( in_array( 'rest_namespace', $fields, true ) ) {
|
||||
$data['rest_namespace'] = $namespace;
|
||||
}
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
@@ -245,7 +250,7 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller {
|
||||
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
|
||||
),
|
||||
'https://api.w.org/items' => array(
|
||||
'href' => rest_url( sprintf( 'wp/v2/%s', $base ) ),
|
||||
'href' => rest_url( rest_get_route_for_post_type_items( $post_type->name ) ),
|
||||
),
|
||||
)
|
||||
);
|
||||
@@ -269,7 +274,7 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller {
|
||||
*
|
||||
* @since 4.7.0
|
||||
* @since 4.8.0 The `supports` property was added.
|
||||
* @since 5.9.0 The `visibility` property was added.
|
||||
* @since 5.9.0 The `visibility` and `rest_namespace` properties were added.
|
||||
*
|
||||
* @return array Item schema data.
|
||||
*/
|
||||
@@ -283,55 +288,55 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller {
|
||||
'title' => 'type',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'capabilities' => array(
|
||||
'capabilities' => array(
|
||||
'description' => __( 'All capabilities used by the post type.' ),
|
||||
'type' => 'object',
|
||||
'context' => array( 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'description' => array(
|
||||
'description' => array(
|
||||
'description' => __( 'A human-readable description of the post type.' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'hierarchical' => array(
|
||||
'hierarchical' => array(
|
||||
'description' => __( 'Whether or not the post type should have children.' ),
|
||||
'type' => 'boolean',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'viewable' => array(
|
||||
'viewable' => array(
|
||||
'description' => __( 'Whether or not the post type can be viewed.' ),
|
||||
'type' => 'boolean',
|
||||
'context' => array( 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'labels' => array(
|
||||
'labels' => array(
|
||||
'description' => __( 'Human-readable labels for the post type for various contexts.' ),
|
||||
'type' => 'object',
|
||||
'context' => array( 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'name' => array(
|
||||
'name' => array(
|
||||
'description' => __( 'The title for the post type.' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit', 'embed' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'slug' => array(
|
||||
'slug' => array(
|
||||
'description' => __( 'An alphanumeric identifier for the post type.' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit', 'embed' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'supports' => array(
|
||||
'supports' => array(
|
||||
'description' => __( 'All features, supported by the post type.' ),
|
||||
'type' => 'object',
|
||||
'context' => array( 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'taxonomies' => array(
|
||||
'taxonomies' => array(
|
||||
'description' => __( 'Taxonomies associated with post type.' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
@@ -340,13 +345,19 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller {
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'rest_base' => array(
|
||||
'rest_base' => array(
|
||||
'description' => __( 'REST base route for the post type.' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit', 'embed' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'visibility' => array(
|
||||
'rest_namespace' => array(
|
||||
'description' => __( 'REST route\'s namespace for the post type.' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit', 'embed' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'visibility' => array(
|
||||
'description' => __( 'The visibility settings for the post type.' ),
|
||||
'type' => 'object',
|
||||
'context' => array( 'edit' ),
|
||||
|
||||
@@ -48,9 +48,9 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
|
||||
*/
|
||||
public function __construct( $post_type ) {
|
||||
$this->post_type = $post_type;
|
||||
$this->namespace = 'wp/v2';
|
||||
$obj = get_post_type_object( $post_type );
|
||||
$this->rest_base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name;
|
||||
$this->namespace = ! empty( $obj->rest_namespace ) ? $obj->rest_namespace : 'wp/v2';
|
||||
|
||||
$this->meta = new WP_REST_Post_Meta_Fields( $this->post_type );
|
||||
}
|
||||
@@ -2037,7 +2037,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
|
||||
// If we have a featured media, add that.
|
||||
$featured_media = get_post_thumbnail_id( $post->ID );
|
||||
if ( $featured_media ) {
|
||||
$image_url = rest_url( 'wp/v2/media/' . $featured_media );
|
||||
$image_url = rest_url( rest_get_route_for_post( $featured_media ) );
|
||||
|
||||
$links['https://api.w.org/featuredmedia'] = array(
|
||||
'href' => $image_url,
|
||||
@@ -2046,7 +2046,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
|
||||
}
|
||||
|
||||
if ( ! in_array( $post->post_type, array( 'attachment', 'nav_menu_item', 'revision' ), true ) ) {
|
||||
$attachments_url = rest_url( 'wp/v2/media' );
|
||||
$attachments_url = rest_url( rest_get_route_for_post_type_items( 'attachment' ) );
|
||||
$attachments_url = add_query_arg( 'parent', $post->ID, $attachments_url );
|
||||
|
||||
$links['https://api.w.org/attachment'] = array(
|
||||
|
||||
@@ -49,10 +49,10 @@ class WP_REST_Revisions_Controller extends WP_REST_Controller {
|
||||
*/
|
||||
public function __construct( $parent_post_type ) {
|
||||
$this->parent_post_type = $parent_post_type;
|
||||
$this->namespace = 'wp/v2';
|
||||
$this->rest_base = 'revisions';
|
||||
$post_type_object = get_post_type_object( $parent_post_type );
|
||||
$this->parent_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
|
||||
$this->namespace = ! empty( $post_type_object->rest_namespace ) ? $post_type_object->rest_namespace : 'wp/v2';
|
||||
$this->parent_controller = $post_type_object->get_rest_controller();
|
||||
|
||||
if ( ! $this->parent_controller ) {
|
||||
|
||||
@@ -33,9 +33,9 @@ class WP_REST_Templates_Controller extends WP_REST_Controller {
|
||||
*/
|
||||
public function __construct( $post_type ) {
|
||||
$this->post_type = $post_type;
|
||||
$this->namespace = 'wp/v2';
|
||||
$obj = get_post_type_object( $post_type );
|
||||
$this->rest_base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name;
|
||||
$this->namespace = ! empty( $obj->rest_namespace ) ? $obj->rest_namespace : 'wp/v2';
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -914,15 +914,14 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
|
||||
$post_type_links = array();
|
||||
|
||||
foreach ( $taxonomy_obj->object_type as $type ) {
|
||||
$post_type_object = get_post_type_object( $type );
|
||||
$rest_path = rest_get_route_for_post_type_items( $type );
|
||||
|
||||
if ( empty( $post_type_object->show_in_rest ) ) {
|
||||
if ( empty( $rest_path ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$rest_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
|
||||
$post_type_links[] = array(
|
||||
'href' => add_query_arg( $this->rest_base, $term->term_id, rest_url( sprintf( 'wp/v2/%s', $rest_base ) ) ),
|
||||
'href' => add_query_arg( $this->rest_base, $term->term_id, rest_url( $rest_path ) ),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user