Skip to content

Commit

Permalink
REST API: Move preview_link to autosaves controller
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Jun 6, 2018
1 parent 4df1bb6 commit 578c345
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 56 deletions.
24 changes: 23 additions & 1 deletion lib/class-wp-rest-autosaves-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,17 @@ public function get_items( $request ) {
* @return array Item schema data.
*/
public function get_item_schema() {
return $this->revisions_controller->get_item_schema();
$schema = $this->revisions_controller->get_item_schema();

$schema['properties']['preview_link'] = array(
'description' => __( 'Preview link for the post.', 'gutenberg' ),
'type' => 'string',
'format' => 'uri',
'context' => array( 'edit' ),
'readonly' => true,
);

return $schema;
}

/**
Expand Down Expand Up @@ -339,6 +349,18 @@ public function prepare_item_for_response( $post, $request ) {

$response = $this->revisions_controller->prepare_item_for_response( $post, $request );

$schema = $this->get_item_schema();

if ( ! empty( $schema['properties']['preview_link'] ) ) {
$response->data['preview_link'] = get_preview_post_link( $post->post_parent, array(
'preview_id' => $post->post_parent,
'preview_nonce' => wp_create_nonce( 'post_preview_' . $post->post_parent )
) );
}

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$response->data = $this->filter_response_by_context( $response->data, $context );

/**
* Filters a revision returned from the API.
*
Expand Down
39 changes: 0 additions & 39 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,45 +438,6 @@ function gutenberg_register_rest_api_post_revisions() {
}
add_action( 'rest_api_init', 'gutenberg_register_rest_api_post_revisions' );

/**
* Get the preview link for the post object.
*
* @see https://github.com/WordPress/gutenberg/issues/4555
*
* @param WP_Post $post Post object.
* @return string
*/
function gutenberg_get_post_preview_link( $post ) {
return get_preview_post_link( $post['id'] );
}

/**
* Adds the 'preview_link' attribute to the REST API response of a post.
*
* @see https://github.com/WordPress/gutenberg/issues/4555
*/
function gutenberg_register_rest_api_post_preview_link() {
foreach ( get_post_types( array( 'show_in_rest' => true ), 'names' ) as $post_type ) {
if ( ! is_post_type_viewable( $post_type ) ) {
continue;
}
register_rest_field( $post_type,
'preview_link',
array(
'get_callback' => 'gutenberg_get_post_preview_link',
'schema' => array(
'description' => __( 'Preview link for the post.', 'gutenberg' ),
'type' => 'string',
'format' => 'uri',
'context' => array( 'edit' ),
'readonly' => true,
),
)
);
}
}
add_action( 'rest_api_init', 'gutenberg_register_rest_api_post_preview_link' );

/**
* Ensure that the wp-json index contains the 'theme-supports' setting as
* part of its site info elements.
Expand Down
15 changes: 0 additions & 15 deletions phpunit/class-gutenberg-rest-api-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,19 +445,4 @@ public function test_get_pages_unbounded_per_page_unauthorized() {
$data = $response->get_data();
$this->assertEquals( 'rest_forbidden_per_page', $data['code'] );
}

public function test_get_page_edit_context_includes_preview() {
wp_set_current_user( $this->editor );
$page_id = $this->factory->post->create( array(
'post_type' => 'page',
'post_status' => 'draft',
) );
$page = get_post( $page_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/pages/' . $page_id );
$request->set_param( 'context', 'edit' );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( get_preview_post_link( $page ), $data['preview_link'] );
}
}
3 changes: 2 additions & 1 deletion phpunit/class-rest-autosaves-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function test_get_item_schema() {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertEquals( 12, count( $properties ) );
$this->assertEquals( 13, count( $properties ) );
$this->assertArrayHasKey( 'author', $properties );
$this->assertArrayHasKey( 'content', $properties );
$this->assertArrayHasKey( 'date', $properties );
Expand All @@ -252,6 +252,7 @@ public function test_get_item_schema() {
$this->assertArrayHasKey( 'parent', $properties );
$this->assertArrayHasKey( 'slug', $properties );
$this->assertArrayHasKey( 'title', $properties );
$this->assertArrayHasKey( 'preview_link', $properties );
}

public function test_create_item() {
Expand Down

0 comments on commit 578c345

Please sign in to comment.