mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 22:30:04 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@51962 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -1829,6 +1829,48 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
$this->assertSame( '', rest_get_route_for_post( $post ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 53656
|
||||
*/
|
||||
public function test_rest_get_route_for_post_custom_namespace() {
|
||||
register_post_type(
|
||||
'cpt',
|
||||
array(
|
||||
'show_in_rest' => true,
|
||||
'rest_base' => 'cpt',
|
||||
'rest_namespace' => 'wordpress/v1',
|
||||
)
|
||||
);
|
||||
$post = self::factory()->post->create_and_get( array( 'post_type' => 'cpt' ) );
|
||||
|
||||
$this->assertSame( '/wordpress/v1/cpt/' . $post->ID, rest_get_route_for_post( $post ) );
|
||||
unregister_post_type( 'cpt' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 53656
|
||||
*/
|
||||
public function test_rest_get_route_for_post_type_items() {
|
||||
$this->assertSame( '/wp/v2/posts', rest_get_route_for_post_type_items( 'post' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 53656
|
||||
*/
|
||||
public function test_rest_get_route_for_post_type_items_custom_namespace() {
|
||||
register_post_type(
|
||||
'cpt',
|
||||
array(
|
||||
'show_in_rest' => true,
|
||||
'rest_base' => 'cpt',
|
||||
'rest_namespace' => 'wordpress/v1',
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertSame( '/wordpress/v1/cpt', rest_get_route_for_post_type_items( 'cpt' ) );
|
||||
unregister_post_type( 'cpt' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 49116
|
||||
*/
|
||||
@@ -1839,10 +1881,11 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* @ticket 49116
|
||||
* @ticket 53656
|
||||
*/
|
||||
public function test_rest_get_route_for_post_custom_controller() {
|
||||
$post = self::factory()->post->create_and_get( array( 'post_type' => 'wp_block' ) );
|
||||
$this->assertSame( '', rest_get_route_for_post( $post ) );
|
||||
$this->assertSame( '/wp/v2/blocks/' . $post->ID, rest_get_route_for_post( $post ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -62,6 +62,23 @@ class WP_Test_REST_Post_Types_Controller extends WP_Test_REST_Controller_Testcas
|
||||
$this->assertSame( array( 'category', 'post_tag' ), $data['taxonomies'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 53656
|
||||
*/
|
||||
public function test_get_item_cpt() {
|
||||
register_post_type(
|
||||
'cpt',
|
||||
array(
|
||||
'show_in_rest' => true,
|
||||
'rest_base' => 'cpt',
|
||||
'rest_namespace' => 'wordpress/v1',
|
||||
)
|
||||
);
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/types/cpt' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->check_post_type_object_response( 'view', $response, 'cpt' );
|
||||
}
|
||||
|
||||
public function test_get_item_page() {
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/types/page' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
@@ -144,7 +161,7 @@ class WP_Test_REST_Post_Types_Controller extends WP_Test_REST_Controller_Testcas
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
$this->assertCount( 11, $properties );
|
||||
$this->assertCount( 12, $properties );
|
||||
$this->assertArrayHasKey( 'capabilities', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'hierarchical', $properties );
|
||||
@@ -155,6 +172,7 @@ class WP_Test_REST_Post_Types_Controller extends WP_Test_REST_Controller_Testcas
|
||||
$this->assertArrayHasKey( 'supports', $properties );
|
||||
$this->assertArrayHasKey( 'taxonomies', $properties );
|
||||
$this->assertArrayHasKey( 'rest_base', $properties );
|
||||
$this->assertArrayHasKey( 'rest_namespace', $properties );
|
||||
$this->assertArrayHasKey( 'visibility', $properties );
|
||||
}
|
||||
|
||||
@@ -204,6 +222,7 @@ class WP_Test_REST_Post_Types_Controller extends WP_Test_REST_Controller_Testcas
|
||||
$this->assertSame( $post_type_obj->description, $data['description'] );
|
||||
$this->assertSame( $post_type_obj->hierarchical, $data['hierarchical'] );
|
||||
$this->assertSame( $post_type_obj->rest_base, $data['rest_base'] );
|
||||
$this->assertSame( $post_type_obj->rest_namespace, $data['rest_namespace'] );
|
||||
|
||||
$links = test_rest_expand_compact_links( $links );
|
||||
$this->assertSame( rest_url( 'wp/v2/types' ), $links['collection'][0]['href'] );
|
||||
|
||||
Reference in New Issue
Block a user