From 227147749c8653739c92ab265bffcbcad00f973d Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 8 Sep 2022 16:50:02 +0100 Subject: [PATCH 1/2] Scale back the code. --- includes/Admin/Editor.php | 3 + includes/REST_API/Stories_Controller.php | 91 ------------------- includes/templates/admin/edit-story.php | 1 + .../src/app/story/actions/useSaveStory.js | 2 + .../src/app/story/effects/useLoadStory.js | 4 +- .../src/api/constants/index.js | 1 + packages/wp-story-editor/src/api/story.js | 9 +- .../src/api/utils/transformStoryResponse.js | 4 + .../documentPane/publish/publish.js | 23 ++--- 9 files changed, 31 insertions(+), 107 deletions(-) diff --git a/includes/Admin/Editor.php b/includes/Admin/Editor.php index 4052ef7f562b..9a437d61ebac 100644 --- a/includes/Admin/Editor.php +++ b/includes/Admin/Editor.php @@ -342,6 +342,8 @@ public function get_editor_settings(): array { admin_url( 'edit.php' ) ); + $revision_url = admin_url( 'revision.php' ); + $dashboard_settings_url = add_query_arg( [ 'post_type' => $this->story_post_type->get_slug(), @@ -394,6 +396,7 @@ public function get_editor_settings(): array { 'postType' => $this->story_post_type->get_slug(), 'storyId' => $story_id, 'dashboardLink' => $dashboard_url, + 'revisionLink' => $revision_url, 'dashboardSettingsLink' => $dashboard_settings_url, 'generalSettingsLink' => $general_settings_url, 'cdnURL' => trailingslashit( WEBSTORIES_CDN_URL ), diff --git a/includes/REST_API/Stories_Controller.php b/includes/REST_API/Stories_Controller.php index 3face6f9025c..837a8bc2ef29 100644 --- a/includes/REST_API/Stories_Controller.php +++ b/includes/REST_API/Stories_Controller.php @@ -148,8 +148,6 @@ public function prepare_item_for_response( $post, $request ): WP_REST_Response { } } - $data['revision'] = $this->get_revisions_count_and_link( $post ); - $data = $this->filter_response_by_context( $data, $context ); $links = $response->get_links(); @@ -517,12 +515,7 @@ protected function add_response_headers( WP_REST_Response $response, WP_REST_Req * @phpstan-return Links */ protected function prepare_links( $post ): array { - // Workaround so that WP_REST_Posts_Controller::prepare_links() does not call wp_get_post_revisions(), - // avoiding a currently unneeded database query. - // TODO(#85): Remove if proper revisions support is ever needed. - remove_post_type_support( $this->post_type, 'revisions' ); $links = parent::prepare_links( $post ); - add_post_type_support( $this->post_type, 'revisions' ); $links = $this->add_post_locking_link( $links, $post ); $links = $this->add_publisher_logo_link( $links, $post ); @@ -691,88 +684,4 @@ private function get_story_poster( WP_Post $post ): ?array { return null; } - - - /** - * Get post revision information for latest id and count. - * - * Replace this with wp_get_latest_revision_id_and_total_count - * Once 6.1 becomes the lowest version supported - * - * @since 1.25.0 - * - * @param WP_Post $post Current post object. - * @return array Revison links and count. - */ - public function get_latest_revision_id_and_total_count( $post ): array { - if ( ! $post->ID ) { - return []; - } - - if ( ! wp_revisions_enabled( $post ) ) { - return []; - } - - $args = [ - 'post_parent' => $post->ID, - 'fields' => 'ids', - 'post_type' => 'revision', - 'post_status' => 'inherit', - 'order' => 'DESC', - 'orderby' => 'date ID', - 'posts_per_page' => 1, - 'ignore_sticky_posts' => true, - ]; - - $revision_query = new \WP_Query(); - $revisions = $revision_query->query( $args ); - - if ( ! $revisions ) { - return [ - 'latest_id' => 0, - 'count' => 0, - ]; - } - - return [ - 'latest_id' => $revisions[0], - 'count' => $revision_query->found_posts, - ]; - } - - /** - * Get post revisions count and link. - * - * @since 1.25.0 - * - * @param WP_Post|null $post Current post object. - * @return array Revison links and count. - */ - public function get_revisions_count_and_link( $post ): array { - $revision = [ - 'href' => '', - 'count' => 0, - ]; - - if ( ! $post || ! $post->ID ) { - return $revision; - } - - if ( $post->post_type ) { - if ( \in_array( $post->post_type, [ 'post', 'page' ], true ) || post_type_supports( $post->post_type, 'revisions' ) ) { - - $revisions = $this->get_latest_revision_id_and_total_count( $post ); - $revisions_count = \count( $revisions ) >= 1 ? $revisions['count'] : 0; - - $revision = [ - 'href' => $revisions['latest_id'] ? admin_url() . 'revision.php?revision=' . $revisions['latest_id'] : '', - 'count' => $revisions_count, - ]; - } - } - - return $revision; - } - - } diff --git a/includes/templates/admin/edit-story.php b/includes/templates/admin/edit-story.php index a85b5f60576f..4a8616e9edc5 100644 --- a/includes/templates/admin/edit-story.php +++ b/includes/templates/admin/edit-story.php @@ -119,6 +119,7 @@ 'permalink_template', 'style_presets', 'password', + '_links', ] ) ), diff --git a/packages/story-editor/src/app/story/actions/useSaveStory.js b/packages/story-editor/src/app/story/actions/useSaveStory.js index 8e5165c9d524..f4889d51c5e6 100644 --- a/packages/story-editor/src/app/story/actions/useSaveStory.js +++ b/packages/story-editor/src/app/story/actions/useSaveStory.js @@ -96,6 +96,7 @@ function useSaveStory({ storyId, pages, story, updateStory }) { slug, link, previewLink, + revisions, editLink: newEditLink, embedPostLink, featuredMedia, @@ -109,6 +110,7 @@ function useSaveStory({ storyId, pages, story, updateStory }) { editLink: newEditLink, embedPostLink, featuredMedia, + revisions, }; updateStory({ properties }); diff --git a/packages/story-editor/src/app/story/effects/useLoadStory.js b/packages/story-editor/src/app/story/effects/useLoadStory.js index 170cd92aa085..7f96809be109 100644 --- a/packages/story-editor/src/app/story/effects/useLoadStory.js +++ b/packages/story-editor/src/app/story/effects/useLoadStory.js @@ -54,7 +54,7 @@ function loadStory(storyId, post, restore, clearHistory) { publisherLogo, taxonomies, terms, - revision, + revisions, } = post; const date = @@ -113,7 +113,7 @@ function loadStory(storyId, post, restore, clearHistory) { previewLink, editLink, embedPostLink, - revision, + revisions, currentStoryStyles: { colors: storyData?.currentStoryStyles?.colors ? getUniquePresets(storyData.currentStoryStyles.colors) diff --git a/packages/wp-story-editor/src/api/constants/index.js b/packages/wp-story-editor/src/api/constants/index.js index b4afa3e1fe09..6d52c2a10de7 100644 --- a/packages/wp-story-editor/src/api/constants/index.js +++ b/packages/wp-story-editor/src/api/constants/index.js @@ -32,6 +32,7 @@ export const STORY_FIELDS = [ 'permalink_template', 'style_presets', 'password', + '_links', ].join(','); export const STORY_EMBED = 'wp:lockuser,author,wp:publisherlogo,wp:term'; diff --git a/packages/wp-story-editor/src/api/story.js b/packages/wp-story-editor/src/api/story.js index ceb6497a37b2..f7889a64a29a 100644 --- a/packages/wp-story-editor/src/api/story.js +++ b/packages/wp-story-editor/src/api/story.js @@ -108,6 +108,7 @@ export function saveStoryById(config, story) { 'link', 'preview_link', 'edit_link', + '_links', 'embed_post_link', 'story_poster', ].join(','), @@ -119,7 +120,12 @@ export function saveStoryById(config, story) { data: storySaveData, method: 'POST', }).then((data) => { - const { story_poster: storyPoster, ...rest } = data; + const { story_poster: storyPoster, _links: links = {}, ...rest } = data; + + const revisions = { + count: links?.['version-history']?.[0]?.count, + id: links?.['predecessor-version']?.[0]?.id + } const featuredMedia = storyPoster ? { @@ -138,6 +144,7 @@ export function saveStoryById(config, story) { return { ...snakeToCamelCaseObjectKeys(rest), featuredMedia, + revisions, }; }); } diff --git a/packages/wp-story-editor/src/api/utils/transformStoryResponse.js b/packages/wp-story-editor/src/api/utils/transformStoryResponse.js index c997b2278581..0c6761277fe0 100644 --- a/packages/wp-story-editor/src/api/utils/transformStoryResponse.js +++ b/packages/wp-story-editor/src/api/utils/transformStoryResponse.js @@ -61,6 +61,10 @@ function transformStoryResponse(post) { url: embedded?.['wp:publisherlogo']?.[0]?.source_url || '', }, taxonomies: links?.['wp:term']?.map(({ taxonomy }) => taxonomy) || [], + revisions: { + count: links?.['version-history']?.[0]?.count, + id: links?.['predecessor-version']?.[0]?.id + }, terms: embedded?.['wp:term'] || [], }; diff --git a/packages/wp-story-editor/src/components/documentPane/publish/publish.js b/packages/wp-story-editor/src/components/documentPane/publish/publish.js index ed60b59a593f..3c33fd91a4a2 100644 --- a/packages/wp-story-editor/src/components/documentPane/publish/publish.js +++ b/packages/wp-story-editor/src/components/documentPane/publish/publish.js @@ -50,6 +50,7 @@ import { useSidebar, } from '@googleforcreators/story-editor'; import { useFeature } from 'flagged'; +import { addQueryArgs } from '@googleforcreators/url'; /** * Internal dependencies */ @@ -148,6 +149,7 @@ function PublishPanel({ nameOverride }) { dashboardSettingsLink, capabilities: { hasUploadMediaAction, canManageSettings }, MediaUpload, + revisionLink } = useConfig(); const improvedAutosaves = useFeature('improvedAutosaves'); @@ -175,14 +177,14 @@ function PublishPanel({ nameOverride }) { }) ); - const { featuredMedia, publisherLogo, updateStory, capabilities, revision } = + const { featuredMedia, publisherLogo, updateStory, capabilities, revisions } = useStory( ({ state: { story: { featuredMedia = { id: 0, url: '', height: 0, width: 0 }, publisherLogo = { id: 0, url: '', height: 0, width: 0 }, - revision, + revisions, }, capabilities, }, @@ -193,18 +195,13 @@ function PublishPanel({ nameOverride }) { publisherLogo, updateStory, capabilities, - revision, + revisions, }; } ); - let revision_count = 0; - let revision_link = ''; - - if (improvedAutosaves && revision) { - revision_count = revision.count; - revision_link = revision.href; - } + const revisionCount = revisions?.count ? revisions?.count : 0; + const revisionId = revisions?.id ? revisions?.id : 0; const handleChangePoster = useCallback( /** @@ -452,16 +449,16 @@ function PublishPanel({ nameOverride }) { - {revision_count} {__('Revisions', 'web-stories')} + {revisionCount} {__('Revisions', 'web-stories')} - {revision_link && ( + {revisionLink && revisionId && ( {__('Manage', 'web-stories')} From aff3d8c30863fd29e1f64e5bb331081322a2f253 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 8 Sep 2022 17:20:08 +0100 Subject: [PATCH 2/2] Fix lint --- packages/wp-story-editor/src/api/story.js | 4 ++-- .../wp-story-editor/src/api/utils/transformStoryResponse.js | 2 +- .../src/components/documentPane/publish/publish.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/wp-story-editor/src/api/story.js b/packages/wp-story-editor/src/api/story.js index f7889a64a29a..c36350804b17 100644 --- a/packages/wp-story-editor/src/api/story.js +++ b/packages/wp-story-editor/src/api/story.js @@ -124,8 +124,8 @@ export function saveStoryById(config, story) { const revisions = { count: links?.['version-history']?.[0]?.count, - id: links?.['predecessor-version']?.[0]?.id - } + id: links?.['predecessor-version']?.[0]?.id, + }; const featuredMedia = storyPoster ? { diff --git a/packages/wp-story-editor/src/api/utils/transformStoryResponse.js b/packages/wp-story-editor/src/api/utils/transformStoryResponse.js index 0c6761277fe0..e2c516db4733 100644 --- a/packages/wp-story-editor/src/api/utils/transformStoryResponse.js +++ b/packages/wp-story-editor/src/api/utils/transformStoryResponse.js @@ -63,7 +63,7 @@ function transformStoryResponse(post) { taxonomies: links?.['wp:term']?.map(({ taxonomy }) => taxonomy) || [], revisions: { count: links?.['version-history']?.[0]?.count, - id: links?.['predecessor-version']?.[0]?.id + id: links?.['predecessor-version']?.[0]?.id, }, terms: embedded?.['wp:term'] || [], }; diff --git a/packages/wp-story-editor/src/components/documentPane/publish/publish.js b/packages/wp-story-editor/src/components/documentPane/publish/publish.js index 3c33fd91a4a2..a6b46a3c379f 100644 --- a/packages/wp-story-editor/src/components/documentPane/publish/publish.js +++ b/packages/wp-story-editor/src/components/documentPane/publish/publish.js @@ -149,7 +149,7 @@ function PublishPanel({ nameOverride }) { dashboardSettingsLink, capabilities: { hasUploadMediaAction, canManageSettings }, MediaUpload, - revisionLink + revisionLink, } = useConfig(); const improvedAutosaves = useFeature('improvedAutosaves');