Skip to content

Commit

Permalink
Only include wp:action-sticky for context=edit; move description
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbachhuber committed May 5, 2018
1 parent 8c5330f commit cce57b8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
10 changes: 5 additions & 5 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,19 +278,19 @@ function gutenberg_add_target_schema_to_links( $response, $post, $request ) {
$orig_links = $response->get_links();
$post_type = get_post_type_object( $post->post_type );
// Only Posts can be sticky.
if ( 'post' === $post->post_type ) {
if ( 'post' === $post->post_type && 'edit' === $request['context'] ) {
if ( current_user_can( $post_type->cap->edit_others_posts )
&& current_user_can( $post_type->cap->publish_posts ) ) {
$new_links['https://api.w.org/action-sticky'] = array(
array(
'title' => __( 'Sticky Post', 'gutenberg' ),
'href' => $orig_links['self'][0]['href'],
'targetSchema' => array(
'type' => 'object',
'properties' => array(
'type' => 'object',
'description' => __( 'Whether or not the current user can sticky the post.', 'gutenberg' ),
'properties' => array(
'sticky' => array(
'type' => 'boolean',
'description' => __( 'Whether or not the object should be treated as sticky.', 'gutenberg' ),
'type' => 'boolean',
),
),
),
Expand Down
13 changes: 11 additions & 2 deletions phpunit/class-gutenberg-rest-api-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,25 @@ function test_link_sticky_only_appears_for_editor() {
$check_key = 'https://api.w.org/action-sticky';
// authors cannot sticky.
wp_set_current_user( $this->author );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
$request->set_param( 'context', 'edit' );
$response = rest_do_request( $request );
$links = $response->get_links();
$this->assertFalse( isset( $links[ $check_key ] ) );
// editors can sticky.
wp_set_current_user( $this->editor );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
$request->set_param( 'context', 'edit' );
$response = rest_do_request( $request );
$links = $response->get_links();
$this->assertTrue( isset( $links[ $check_key ] ) );
// editors can sticky but not included for context != edit.
wp_set_current_user( $this->editor );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
$request->set_param( 'context', 'view' );
$response = rest_do_request( $request );
$links = $response->get_links();
$this->assertFalse( isset( $links[ $check_key ] ) );
}

/**
Expand Down

0 comments on commit cce57b8

Please sign in to comment.