Make WordPress Core


Ignore:
Timestamp:
07/11/2018 06:22:10 AM (8 years ago)
Author:
pento
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r43087 r43437  
    13511351        }
    13521352
     1353        public function test_links_exist() {
     1354
     1355                wp_set_current_user( self::$editor_id );
     1356
     1357                $post = self::factory()->attachment->create( array( 'post_author' => self::$editor_id ) );
     1358                $this->assertGreaterThan( 0, $post );
     1359
     1360                $request = new WP_REST_Request( 'GET', "/wp/v2/media/{$post}" );
     1361                $request->set_query_params( array( 'context' => 'edit' ) );
     1362
     1363                $response = rest_get_server()->dispatch( $request );
     1364                $links    = $response->get_links();
     1365
     1366                $this->assertArrayHasKey( 'self', $links );
     1367        }
     1368
     1369        public function test_publish_action_ldo_not_registered() {
     1370
     1371                $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/media' ) );
     1372                $data     = $response->get_data();
     1373                $schema   = $data['schema'];
     1374
     1375                $this->assertArrayHasKey( 'links', $schema );
     1376                $publish = wp_list_filter( $schema['links'], array( 'rel' => 'https://api.w.org/action-publish' ) );
     1377
     1378                $this->assertCount( 0, $publish, 'LDO not found on schema.' );
     1379        }
     1380
     1381        public function test_publish_action_link_does_not_exists() {
     1382
     1383                wp_set_current_user( self::$editor_id );
     1384
     1385                $post = self::factory()->attachment->create( array( 'post_author' => self::$editor_id ) );
     1386                $this->assertGreaterThan( 0, $post );
     1387
     1388                $request = new WP_REST_Request( 'GET', "/wp/v2/media/{$post}" );
     1389                $request->set_query_params( array( 'context' => 'edit' ) );
     1390
     1391                $response = rest_get_server()->dispatch( $request );
     1392                $links    = $response->get_links();
     1393
     1394                $this->assertArrayNotHasKey( 'https://api.w.org/action-publish', $links );
     1395        }
     1396
    13531397        public function tearDown() {
    13541398                parent::tearDown();
Note: See TracChangeset for help on using the changeset viewer.