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

Scale back the code. #12264

Closed
Closed
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
3 changes: 3 additions & 0 deletions includes/Admin/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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 ),
Expand Down
91 changes: 0 additions & 91 deletions includes/REST_API/Stories_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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<string,mixed> 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<string,mixed> 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;
}


}
1 change: 1 addition & 0 deletions includes/templates/admin/edit-story.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
'permalink_template',
'style_presets',
'password',
'_links',
]
)
),
Expand Down
2 changes: 2 additions & 0 deletions packages/story-editor/src/app/story/actions/useSaveStory.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ function useSaveStory({ storyId, pages, story, updateStory }) {
slug,
link,
previewLink,
revisions,
editLink: newEditLink,
embedPostLink,
featuredMedia,
Expand All @@ -109,6 +110,7 @@ function useSaveStory({ storyId, pages, story, updateStory }) {
editLink: newEditLink,
embedPostLink,
featuredMedia,
revisions,
};
updateStory({ properties });

Expand Down
4 changes: 2 additions & 2 deletions packages/story-editor/src/app/story/effects/useLoadStory.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function loadStory(storyId, post, restore, clearHistory) {
publisherLogo,
taxonomies,
terms,
revision,
revisions,
} = post;

const date =
Expand Down Expand Up @@ -113,7 +113,7 @@ function loadStory(storyId, post, restore, clearHistory) {
previewLink,
editLink,
embedPostLink,
revision,
revisions,
currentStoryStyles: {
colors: storyData?.currentStoryStyles?.colors
? getUniquePresets(storyData.currentStoryStyles.colors)
Expand Down
1 change: 1 addition & 0 deletions packages/wp-story-editor/src/api/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
9 changes: 8 additions & 1 deletion packages/wp-story-editor/src/api/story.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export function saveStoryById(config, story) {
'link',
'preview_link',
'edit_link',
'_links',
'embed_post_link',
'story_poster',
].join(','),
Expand All @@ -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
? {
Expand All @@ -138,6 +144,7 @@ export function saveStoryById(config, story) {
return {
...snakeToCamelCaseObjectKeys(rest),
featuredMedia,
revisions,
};
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'] || [],
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
useSidebar,
} from '@googleforcreators/story-editor';
import { useFeature } from 'flagged';
import { addQueryArgs } from '@googleforcreators/url';
/**
* Internal dependencies
*/
Expand Down Expand Up @@ -148,6 +149,7 @@ function PublishPanel({ nameOverride }) {
dashboardSettingsLink,
capabilities: { hasUploadMediaAction, canManageSettings },
MediaUpload,
revisionLink,
} = useConfig();

const improvedAutosaves = useFeature('improvedAutosaves');
Expand Down Expand Up @@ -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,
},
Expand All @@ -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(
/**
Expand Down Expand Up @@ -452,16 +449,16 @@ function PublishPanel({ nameOverride }) {
<Icons.History width={24} height={24} aria-hidden />
</LabelIconWrapper>
<RevisionsLabel>
{revision_count} {__('Revisions', 'web-stories')}
{revisionCount} {__('Revisions', 'web-stories')}
</RevisionsLabel>
</Label>

{revision_link && (
{revisionLink && revisionId && (
<Row>
<Link
rel="noopener noreferrer"
target="_blank"
href={revision_link}
href={addQueryArgs(revisionLink, { revision: revisionId })}
size={THEME_CONSTANTS.TYPOGRAPHY.PRESET_SIZES.X_SMALL}
>
{__('Manage', 'web-stories')}
Expand Down