REST API: Declare user capabilities using JSON Hyper Schema's "targetSchema".

There are a variety of operations a WordPress user can only perform if they have the correct capabilities. A REST API client should only display UI for one of these operations if the WordPress user can perform the operation.

Rather than requiring REST API clients to calculate whether to display UI based on potentially complicated combinations of user capabilities, `targetSchema` allows us to expose a single flag to show whether the corresponding UI should be displayed.

This change also includes flags on post objects for the following actions:

- `action-publish`: The current user can publish this post.
- `action-sticky`: The current user can make this post sticky, and the post type supports sticking.
- `action-assign-author': The current user can change the author on this post.
- `action-assign-{$taxonomy}`: The current user can assign terms from the "$taxonomy" taxonomy to this post.
- `action-create-{$taxonomy}`: The current user can create terms int the "$taxonomy" taxonomy.

Props TimothyBlynJacobs, danielbachhuber.
Fixes #44287.



git-svn-id: https://develop.svn.wordpress.org/trunk@43437 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast
2018-07-11 06:22:10 +00:00
parent 7569772234
commit 81d2390d29
4 changed files with 535 additions and 3 deletions
@@ -1350,6 +1350,50 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
}
}
public function test_links_exist() {
wp_set_current_user( self::$editor_id );
$post = self::factory()->attachment->create( array( 'post_author' => self::$editor_id ) );
$this->assertGreaterThan( 0, $post );
$request = new WP_REST_Request( 'GET', "/wp/v2/media/{$post}" );
$request->set_query_params( array( 'context' => 'edit' ) );
$response = rest_get_server()->dispatch( $request );
$links = $response->get_links();
$this->assertArrayHasKey( 'self', $links );
}
public function test_publish_action_ldo_not_registered() {
$response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/media' ) );
$data = $response->get_data();
$schema = $data['schema'];
$this->assertArrayHasKey( 'links', $schema );
$publish = wp_list_filter( $schema['links'], array( 'rel' => 'https://api.w.org/action-publish' ) );
$this->assertCount( 0, $publish, 'LDO not found on schema.' );
}
public function test_publish_action_link_does_not_exists() {
wp_set_current_user( self::$editor_id );
$post = self::factory()->attachment->create( array( 'post_author' => self::$editor_id ) );
$this->assertGreaterThan( 0, $post );
$request = new WP_REST_Request( 'GET', "/wp/v2/media/{$post}" );
$request->set_query_params( array( 'context' => 'edit' ) );
$response = rest_get_server()->dispatch( $request );
$links = $response->get_links();
$this->assertArrayNotHasKey( 'https://api.w.org/action-publish', $links );
}
public function tearDown() {
parent::tearDown();
if ( file_exists( $this->test_file ) ) {