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

Previews: Use WordPress-generated preview links #6037

Merged
merged 7 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions assets/src/dashboard/app/serializers/stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default function reshapeStoryObject(editStoryURL) {
modified,
modified_gmt,
link,
preview_link: previewLink,
story_data: storyData,
_embedded: { author = [] } = {},
} = originalStoryData;
Expand Down Expand Up @@ -63,6 +64,7 @@ export default function reshapeStoryObject(editStoryURL) {
centerTargetAction: '',
bottomTargetAction: `${editStoryURL}&post=${id}`,
editStoryLink: `${editStoryURL}&post=${id}`,
previewLink,
link,
originalStoryData,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const generateStoryMenu = ({ menuItems, story }) =>
if (menuItem.value === STORY_CONTEXT_MENU_ACTIONS.OPEN_IN_EDITOR) {
return { ...menuItem, url: story.bottomTargetAction, newTab: false };
} else if (menuItem.value === STORY_CONTEXT_MENU_ACTIONS.OPEN_STORY_LINK) {
return { ...menuItem, url: story.link, newTab: true };
return { ...menuItem, url: story.previewLink, newTab: true };
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
}
return menuItem;
});
1 change: 1 addition & 0 deletions assets/src/edit-story/app/story/actions/useSaveStory.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ function useSaveStory({ storyId, pages, story, updateStory }) {
const properties = {
...objectPick(post, ['status', 'slug', 'link']),
featuredMediaUrl: post.featured_media_url,
previewLink: post.preview_link,
};
updateStory({ properties });

Expand Down
2 changes: 2 additions & 0 deletions assets/src/edit-story/app/story/effects/useLoadStory.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function useLoadStory({ storyId, shouldLoad, restore, isDemo }) {
permalink_template: permalinkTemplate,
style_presets: stylePresets,
password,
preview_link: previewLink,
_embedded: embedded = {},
} = post;

Expand Down Expand Up @@ -129,6 +130,7 @@ function useLoadStory({ storyId, shouldLoad, restore, isDemo }) {
permalinkConfig,
publisherLogoUrl,
password,
previewLink,
stylePresets,
autoAdvance: storyData?.autoAdvance,
defaultPageDuration: storyData?.defaultPageDuration,
Expand Down
17 changes: 5 additions & 12 deletions assets/src/edit-story/components/header/buttons/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,27 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { addQueryArgs } from '../../../../design-system';
import { trackEvent } from '../../../../tracking';
import { useStory, useLocalMedia, useConfig } from '../../../app';
import { useStory, useLocalMedia } from '../../../app';
import { Outline } from '../../button';
import escapeHTML from '../../../utils/escapeHTML';
import PreviewErrorDialog from '../previewErrorDialog';

const PREVIEW_TARGET = 'story-preview';

function Preview() {
const { isSaving, link, status, autoSave, saveStory } = useStory(
const { isSaving, previewLink, status, autoSave, saveStory } = useStory(
({
state: {
meta: { isSaving },
story: { link, status },
story: { status, previewLink },
},
actions: { autoSave, saveStory },
}) => ({ isSaving, link, status, autoSave, saveStory })
}) => ({ isSaving, status, previewLink, autoSave, saveStory })
);
const { isUploading } = useLocalMedia((state) => ({
isUploading: state.state.isUploading,
}));
const { previewLink: autoSaveLink } = useConfig();

const [previewLinkToOpenViaDialog, setPreviewLinkToOpenViaDialog] = useState(
null
Expand All @@ -62,11 +60,6 @@ function Preview() {
const openPreviewLink = useCallback(() => {
trackEvent('preview_story', 'editor');

// Display the actual link in case of a draft.
const previewLink = isDraft
? addQueryArgs(link, { preview: 'true' })
: autoSaveLink;

// Start a about:blank popup with waiting message until we complete
// the saving operation. That way we will not bust the popup timeout.
let popup;
Expand Down Expand Up @@ -112,7 +105,7 @@ function Preview() {
}
})
.catch(() => setPreviewLinkToOpenViaDialog(previewLink));
}, [autoSave, autoSaveLink, isDraft, link, saveStory]);
}, [autoSave, isDraft, previewLink, saveStory]);

const openPreviewLinkSync = useCallback(
(evt) => {
Expand Down
30 changes: 30 additions & 0 deletions includes/REST_API/Stories_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,28 @@ public function prepare_item_for_response( $post, $request ) {
$data['style_presets'] = is_array( $style_presets ) ? $style_presets : self::EMPTY_STYLE_PRESETS;
}

if ( in_array( 'preview_link', $fields, true ) ) {
// Based on https://github.com/WordPress/wordpress-develop/blob/8153c8ba020c4aec0b9d94243cd39c689a0730f7/src/wp-admin/includes/post.php#L1445-L1457
if ( 'draft' === $post->post_status || empty( $post->post_name ) ) {
$view_link = get_preview_post_link( $post );
} else {
if ( 'publish' === $post->post_status ) {
$view_link = get_permalink( $post );
} else {
if ( ! function_exists( 'get_sample_permalink' ) ) {
require_once ABSPATH . 'wp-admin/includes/post.php';
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
}

list ( $permalink ) = get_sample_permalink( $post->ID, $post->post_title, '' );

// Allow non-published (private, future) to be viewed at a pretty permalink, in case $post->post_name is set.
$view_link = str_replace( [ '%pagename%', '%postname%' ], $post->post_name, $permalink );
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
}
}

$data['preview_link'] = $view_link;
}

$data = $this->filter_response_by_context( $data, $context );
$links = $response->get_links();

Expand Down Expand Up @@ -173,6 +195,14 @@ public function get_item_schema() {
'context' => [ 'view', 'edit' ],
];

$schema['properties']['preview_link'] = [
'description' => __( 'Preview Link.', 'web-stories' ),
'type' => 'string',
'context' => [ 'edit' ],
'format' => 'uri',
'default' => '',
];

$schema['properties']['status']['enum'][] = 'auto-draft';

$this->schema = $schema;
Expand Down
7 changes: 0 additions & 7 deletions includes/Story_Post_Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,12 +517,6 @@ public function get_editor_settings() {
$max_upload_size = 0;
}

$preview_query_args = [
'preview_id' => $story_id,
// Leveraging the default WP post preview logic.
'preview_nonce' => wp_create_nonce( 'post_preview_' . $story_id ),
];

$is_demo = ( isset( $_GET['web-stories-demo'] ) && (bool) $_GET['web-stories-demo'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended

$settings = [
Expand All @@ -535,7 +529,6 @@ public function get_editor_settings() {
'allowedFileTypes' => $this->get_allowed_file_types(),
'postType' => self::POST_TYPE_SLUG,
'storyId' => $story_id,
'previewLink' => get_preview_post_link( $story_id, $preview_query_args ),
'assetsURL' => trailingslashit( WEBSTORIES_ASSETS_URL ),
'cdnURL' => trailingslashit( WEBSTORIES_CDN_URL ),
'maxUpload' => $max_upload_size,
Expand Down