Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose preview_link through the REST API and use within client #6882

Merged
merged 3 commits into from
May 22, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions editor/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import createSelector from 'rememo';
*/
import { serialize, getBlockType, getBlockTypes, hasBlockSupport } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';
import { addQueryArgs } from '@wordpress/url';
import { moment } from '@wordpress/date';

/***
Expand Down Expand Up @@ -354,12 +353,11 @@ export function getEditedPostExcerpt( state ) {
* @return {string} Preview URL.
*/
export function getEditedPostPreviewLink( state ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be simplified further, see getCurrentPostId() for an example.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const link = state.currentPost.link;
if ( ! link ) {
return null;
const link = state.currentPost.preview_link;
if ( link ) {
return link;
}

return addQueryArgs( link, { preview: 'true' } );
return null;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions editor/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1068,11 +1068,11 @@ describe( 'selectors', () => {
it( 'should return the correct url adding a preview parameter to the query string', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Description should be updated to match the new behaviour.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const state = {
currentPost: {
link: 'https://andalouses.com/beach',
preview_link: 'https://andalouses.com/?p=1&preview=true',
},
};

expect( getEditedPostPreviewLink( state ) ).toBe( 'https://andalouses.com/beach?preview=true' );
expect( getEditedPostPreviewLink( state ) ).toBe( 'https://andalouses.com/?p=1&preview=true' );
} );
} );

Expand Down
39 changes: 39 additions & 0 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,45 @@ 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: 15 additions & 0 deletions phpunit/class-gutenberg-rest-api-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,19 @@ 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'] );
}
}