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 all commits
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
8 changes: 1 addition & 7 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,7 @@ 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;
}

return addQueryArgs( link, { preview: 'true' } );
return getCurrentPost( state ).preview_link || null;
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be better to use getEditedPostAttribute( 'preview_link' ) so we could get rid of getCurrentPost at some point.

Copy link
Member

Choose a reason for hiding this comment

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

Can you make an issue for this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Doing PR

Copy link
Member Author

Choose a reason for hiding this comment

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

}

/**
Expand Down
6 changes: 3 additions & 3 deletions editor/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1065,14 +1065,14 @@ describe( 'selectors', () => {
expect( getEditedPostPreviewLink( state ) ).toBeNull();
} );

it( 'should return the correct url adding a preview parameter to the query string', () => {
it( 'should return the correct url when the post object has a preview_link', () => {
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'] );
}
}