From 352394fc0381fa4094caf2d5177977de7365fc66 Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Fri, 21 Oct 2022 14:14:01 -0400 Subject: [PATCH 01/17] Initial work on revision button for templates --- .../data/data-core-edit-site.md | 39 +++++++++ ...ss-gutenberg-rest-templates-controller.php | 69 ++++++++++++++++ lib/experimental/rest-api.php | 13 +++ lib/load.php | 2 + .../sidebar-edit-mode/template-card/index.js | 82 ++++++++++++++++++- packages/edit-site/src/store/selectors.js | 55 +++++++++++++ 6 files changed, 258 insertions(+), 2 deletions(-) create mode 100644 lib/experimental/class-gutenberg-rest-templates-controller.php diff --git a/docs/reference-guides/data/data-core-edit-site.md b/docs/reference-guides/data/data-core-edit-site.md index 53fd4d138383a..acc9a211815c2 100644 --- a/docs/reference-guides/data/data-core-edit-site.md +++ b/docs/reference-guides/data/data-core-edit-site.md @@ -18,10 +18,49 @@ _Returns_ - `Object`: Whether the current user can create media or not. +### getCurrentTemplate + +Returns the post currently being edited in its last known saved state, not +including unsaved edits. Returns an object containing relevant default post +values if the post has not yet been saved. + +_Parameters_ + +- _state_ `Object`: Global application state. + +_Returns_ + +- `Object`: Post object. + +### getCurrentTemplateLastRevisionId + +Returns the last revision ID of the post currently being edited, +or null if the post has no revisions. + +_Parameters_ + +- _state_ `Object`: Global application state. + +_Returns_ + +- `?number`: ID of the last revision. + ### getCurrentTemplateNavigationPanelSubMenu > **Deprecated** +### getCurrentTemplateRevisionsCount + +Returns the number of revisions of the post currently being edited. + +_Parameters_ + +- _state_ `Object`: Global application state. + +_Returns_ + +- `number`: Number of revisions. + ### getCurrentTemplateTemplateParts Returns the template parts and their blocks for the current edited template. diff --git a/lib/experimental/class-gutenberg-rest-templates-controller.php b/lib/experimental/class-gutenberg-rest-templates-controller.php new file mode 100644 index 0000000000000..e29d27d7805c3 --- /dev/null +++ b/lib/experimental/class-gutenberg-rest-templates-controller.php @@ -0,0 +1,69 @@ +get_data(); + $fields = $this->get_fields_for_response( $request ); + + if ( !is_null( $template->wp_id ) && $template->wp_id !== 0 ) { + $revisions = wp_get_latest_revision_id_and_total_count( $template->wp_id ); + } else { + // TODO: Should this be included if it's a file-based template? + $revisions = array( + 'revision_id' => 0, + 'revision_count' => 0, + ); + } + + if ( rest_is_field_included( 'revision_count', $fields ) ) { + $data['revision_count'] = (int) $revisions['count']; + } + + if ( rest_is_field_included( 'latest_revision_id', $fields ) ) { + $data['latest_revision_id'] = (int) $revisions['latest_id']; + } + + $response->set_data( $data ); + return $response; + } + + /** + * Adds revision_count and latest_revision_id to the template schema. + * + * @return array Item schema data. + */ + public function get_item_schema() { + $schema = parent::get_item_schema(); + + $schema['properties']['revision_count'] = array( + 'description' => __( 'The number of revisions of the template.' ), + 'type' => 'integer', + 'context' => array( 'view', 'edit', 'embed' ), + 'readonly' => true, + ); + + $schema['properties']['latest_revision_id'] = array( + 'description' => __( 'The id of the latest revision of the template.' ), + 'type' => 'integer', + 'context' => array( 'view', 'edit', 'embed' ), + 'readonly' => true, + ); + + return $schema; + } +} \ No newline at end of file diff --git a/lib/experimental/rest-api.php b/lib/experimental/rest-api.php index 8ab4b1ad509fe..1519a25fb6d52 100644 --- a/lib/experimental/rest-api.php +++ b/lib/experimental/rest-api.php @@ -28,6 +28,19 @@ function gutenberg_register_block_editor_settings() { } add_action( 'rest_api_init', 'gutenberg_register_block_editor_settings' ); + +/** + * Registers the template REST API routes. + */ +function gutenberg_register_rest_template() { + $template_controller = new Gutenberg_Experimental_REST_Templates_Controller('wp_template'); + $template_controller->register_routes(); + $template_parts_controller = new Gutenberg_Experimental_REST_Templates_Controller('wp_template_part'); + $template_parts_controller->register_routes(); +} + +add_action( 'rest_api_init', 'gutenberg_register_rest_template' ); + /** * Shim for get_sample_permalink() to add support for auto-draft status. * diff --git a/lib/load.php b/lib/load.php index 91c52c4bf649a..64b25eb6bc112 100644 --- a/lib/load.php +++ b/lib/load.php @@ -56,6 +56,8 @@ function gutenberg_is_experiment_enabled( $name ) { if ( ! class_exists( 'WP_Rest_Customizer_Nonces' ) ) { require_once __DIR__ . '/experimental/class-wp-rest-customizer-nonces.php'; } + require_once __DIR__ . '/experimental/class-gutenberg-rest-templates-controller.php'; + require_once __DIR__ . '/experimental/rest-api.php'; } diff --git a/packages/edit-site/src/components/sidebar-edit-mode/template-card/index.js b/packages/edit-site/src/components/sidebar-edit-mode/template-card/index.js index 96333e3ab01f2..4853f4c15c43e 100644 --- a/packages/edit-site/src/components/sidebar-edit-mode/template-card/index.js +++ b/packages/edit-site/src/components/sidebar-edit-mode/template-card/index.js @@ -1,11 +1,14 @@ /** * WordPress dependencies */ -import { useSelect } from '@wordpress/data'; -import { Icon } from '@wordpress/components'; +import { useSelect, withSelect } from '@wordpress/data'; +import { Icon, PanelBody, Button } from '@wordpress/components'; import { store as editorStore } from '@wordpress/editor'; import { store as coreStore } from '@wordpress/core-data'; import { decodeEntities } from '@wordpress/html-entities'; +import { sprintf, _n } from '@wordpress/i18n'; +import { backup } from '@wordpress/icons'; +import { addQueryArgs } from '@wordpress/url'; /** * Internal dependencies @@ -13,6 +16,80 @@ import { decodeEntities } from '@wordpress/html-entities'; import { store as editSiteStore } from '../../../store'; import TemplateActions from './template-actions'; import TemplateAreas from './template-areas'; +import PostTypeSupportCheck from '../../../../../editor/src/components/post-type-support-check'; + +/* + * TODO Refactor all of these components copied from the editor. + */ + +export function _PostLastRevisionCheck( { + lastRevisionId, + revisionsCount, + children, +} ) { + if ( ! lastRevisionId || revisionsCount < 2 ) { + return null; + } + + return ( + + { children } + + ); +} + +const PostLastRevisionCheck = withSelect( ( select ) => { + const { + getCurrentTemplateLastRevisionId, + getCurrentTemplateRevisionsCount, + } = select( editSiteStore ); + return { + lastRevisionId: getCurrentTemplateLastRevisionId(), + revisionsCount: getCurrentTemplateRevisionsCount(), + }; +} )( _PostLastRevisionCheck ); + +const _PostLastRevision = ( { lastRevisionId, revisionsCount } ) => { + return ( + + + + ); +}; + +const PostLastRevision = withSelect( ( select ) => { + const { + getCurrentTemplateLastRevisionId, + getCurrentTemplateRevisionsCount, + } = select( editSiteStore ); + return { + lastRevisionId: getCurrentTemplateLastRevisionId(), + revisionsCount: getCurrentTemplateRevisionsCount(), + }; +} )( _PostLastRevision ); + +function LastRevision() { + return ( + + + + + + ); +} export default function TemplateCard() { const { @@ -51,6 +128,7 @@ export default function TemplateCard() { { decodeEntities( description ) } + ); diff --git a/packages/edit-site/src/store/selectors.js b/packages/edit-site/src/store/selectors.js index 0b620a4b05c5d..54fb8f301b338 100644 --- a/packages/edit-site/src/store/selectors.js +++ b/packages/edit-site/src/store/selectors.js @@ -288,6 +288,61 @@ export function getEditorMode( state ) { return __unstableGetPreference( state, 'editorMode' ); } +/** + * Returns the post currently being edited in its last known saved state, not + * including unsaved edits. Returns an object containing relevant default post + * values if the post has not yet been saved. + * + * @param {Object} state Global application state. + * + * @return {Object} Post object. + */ +export const getCurrentTemplate = createRegistrySelector( + ( select ) => ( state ) => { + const templateType = getEditedPostType( state ); + const templateId = getEditedPostId( state ); + const template = templateId + ? select( coreDataStore ).getEntityRecord( + 'postType', + templateType, + templateId + ) + : null; + + return template; + } +); + +/** + * Returns the number of revisions of the template currently being edited. + * + * @param {Object} state Global application state. + * + * @return {number} Number of revisions. + */ +export function getCurrentTemplateRevisionsCount( state ) { + const template = getCurrentTemplate( state ); + if ( template.source === 'theme' ) { + return 0; + } + return ( template?._links?.[ 'version-history' ]?.[ 0 ]?.count ?? 0 ) + 1; +} + +/** + * Returns the last revision ID of the template currently being edited, + * or null if the template is from the theme + * + * @param {Object} state Global application state. + * + * @return {?number} ID of the last revision. + */ +export function getCurrentTemplateLastRevisionId( state ) { + return ( + getCurrentTemplate( state )?._links?.[ 'predecessor-version' ]?.[ 0 ] + ?.id ?? null + ); +} + /** * @deprecated */ From 0971f4caea62fb041890d4ba59f781a2d7cd5568 Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Fri, 21 Oct 2022 14:26:46 -0400 Subject: [PATCH 02/17] Fix warning --- .../class-gutenberg-rest-templates-controller.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/experimental/class-gutenberg-rest-templates-controller.php b/lib/experimental/class-gutenberg-rest-templates-controller.php index e29d27d7805c3..a7d1febd1136b 100644 --- a/lib/experimental/class-gutenberg-rest-templates-controller.php +++ b/lib/experimental/class-gutenberg-rest-templates-controller.php @@ -20,13 +20,13 @@ public function prepare_item_for_response( $item, $request ) { // phpcs:ignore V $data = $response->get_data(); $fields = $this->get_fields_for_response( $request ); - if ( !is_null( $template->wp_id ) && $template->wp_id !== 0 ) { + if ( ! is_null( $template->wp_id ) && $template->wp_id !== 0 ) { $revisions = wp_get_latest_revision_id_and_total_count( $template->wp_id ); } else { // TODO: Should this be included if it's a file-based template? $revisions = array( - 'revision_id' => 0, - 'revision_count' => 0, + 'count' => 0, + 'latest_id' => 0, ); } From e91724ac9819659ce3369138d8344d754bbb842b Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Mon, 24 Oct 2022 14:49:00 -0400 Subject: [PATCH 03/17] Refactor --- .../sidebar-edit-mode/last-revision/index.js | 73 +++++++++++++++++ .../sidebar-edit-mode/template-card/index.js | 82 +------------------ 2 files changed, 76 insertions(+), 79 deletions(-) create mode 100644 packages/edit-site/src/components/sidebar-edit-mode/last-revision/index.js diff --git a/packages/edit-site/src/components/sidebar-edit-mode/last-revision/index.js b/packages/edit-site/src/components/sidebar-edit-mode/last-revision/index.js new file mode 100644 index 0000000000000..90aad1d9fad66 --- /dev/null +++ b/packages/edit-site/src/components/sidebar-edit-mode/last-revision/index.js @@ -0,0 +1,73 @@ +/** + * WordPress dependencies + */ +import { useSelect } from '@wordpress/data'; +import { PanelBody, Button } from '@wordpress/components'; +import { sprintf, _n } from '@wordpress/i18n'; +import { backup } from '@wordpress/icons'; +import { addQueryArgs } from '@wordpress/url'; +import { PostTypeSupportCheck } from '@wordpress/editor'; + +/** + * Internal dependencies + */ +import { store as editSiteStore } from '../../../store'; + +const useRevisionData = () => + useSelect( ( select ) => { + const { + getCurrentTemplateLastRevisionId, + getCurrentTemplateRevisionsCount, + } = select( editSiteStore ); + return { + lastRevisionId: getCurrentTemplateLastRevisionId(), + revisionsCount: getCurrentTemplateRevisionsCount(), + }; + } ); + +function PostLastRevisionCheck( { children } ) { + const { lastRevisionId, revisionsCount } = useRevisionData(); + + if ( ! lastRevisionId || revisionsCount < 2 ) { + return null; + } + + return ( + + { children } + + ); +} + +const PostLastRevision = () => { + const { lastRevisionId, revisionsCount } = useRevisionData(); + + return ( + + + + ); +}; + +export default function LastRevision() { + return ( + + + + + + ); +} diff --git a/packages/edit-site/src/components/sidebar-edit-mode/template-card/index.js b/packages/edit-site/src/components/sidebar-edit-mode/template-card/index.js index 4853f4c15c43e..51ff3614d194c 100644 --- a/packages/edit-site/src/components/sidebar-edit-mode/template-card/index.js +++ b/packages/edit-site/src/components/sidebar-edit-mode/template-card/index.js @@ -1,14 +1,11 @@ /** * WordPress dependencies */ -import { useSelect, withSelect } from '@wordpress/data'; -import { Icon, PanelBody, Button } from '@wordpress/components'; +import { useSelect } from '@wordpress/data'; +import { Icon } from '@wordpress/components'; import { store as editorStore } from '@wordpress/editor'; import { store as coreStore } from '@wordpress/core-data'; import { decodeEntities } from '@wordpress/html-entities'; -import { sprintf, _n } from '@wordpress/i18n'; -import { backup } from '@wordpress/icons'; -import { addQueryArgs } from '@wordpress/url'; /** * Internal dependencies @@ -16,80 +13,7 @@ import { addQueryArgs } from '@wordpress/url'; import { store as editSiteStore } from '../../../store'; import TemplateActions from './template-actions'; import TemplateAreas from './template-areas'; -import PostTypeSupportCheck from '../../../../../editor/src/components/post-type-support-check'; - -/* - * TODO Refactor all of these components copied from the editor. - */ - -export function _PostLastRevisionCheck( { - lastRevisionId, - revisionsCount, - children, -} ) { - if ( ! lastRevisionId || revisionsCount < 2 ) { - return null; - } - - return ( - - { children } - - ); -} - -const PostLastRevisionCheck = withSelect( ( select ) => { - const { - getCurrentTemplateLastRevisionId, - getCurrentTemplateRevisionsCount, - } = select( editSiteStore ); - return { - lastRevisionId: getCurrentTemplateLastRevisionId(), - revisionsCount: getCurrentTemplateRevisionsCount(), - }; -} )( _PostLastRevisionCheck ); - -const _PostLastRevision = ( { lastRevisionId, revisionsCount } ) => { - return ( - - - - ); -}; - -const PostLastRevision = withSelect( ( select ) => { - const { - getCurrentTemplateLastRevisionId, - getCurrentTemplateRevisionsCount, - } = select( editSiteStore ); - return { - lastRevisionId: getCurrentTemplateLastRevisionId(), - revisionsCount: getCurrentTemplateRevisionsCount(), - }; -} )( _PostLastRevision ); - -function LastRevision() { - return ( - - - - - - ); -} +import LastRevision from '../last-revision'; export default function TemplateCard() { const { From ab33526ba7407b9b609e10e771020e5a1cadb6a7 Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Tue, 25 Oct 2022 11:25:27 -0400 Subject: [PATCH 04/17] Refactor --- .../data/data-core-edit-site.md | 6 ++-- ...utenberg-rest-template-revision-count.php} | 5 +++- lib/experimental/rest-api.php | 4 +-- lib/load.php | 2 +- .../sidebar-edit-mode/template-card/index.js | 2 +- .../last-revision.js} | 4 +-- .../template-card/style.scss | 28 +++++++++++++++++++ 7 files changed, 41 insertions(+), 10 deletions(-) rename lib/experimental/{class-gutenberg-rest-templates-controller.php => class-gutenberg-rest-template-revision-count.php} (90%) rename packages/edit-site/src/components/sidebar-edit-mode/{last-revision/index.js => template-card/last-revision.js} (93%) diff --git a/docs/reference-guides/data/data-core-edit-site.md b/docs/reference-guides/data/data-core-edit-site.md index acc9a211815c2..9eb0b8466b276 100644 --- a/docs/reference-guides/data/data-core-edit-site.md +++ b/docs/reference-guides/data/data-core-edit-site.md @@ -34,8 +34,8 @@ _Returns_ ### getCurrentTemplateLastRevisionId -Returns the last revision ID of the post currently being edited, -or null if the post has no revisions. +Returns the last revision ID of the template currently being edited, +or null if the template is from the theme _Parameters_ @@ -51,7 +51,7 @@ _Returns_ ### getCurrentTemplateRevisionsCount -Returns the number of revisions of the post currently being edited. +Returns the number of revisions of the template currently being edited. _Parameters_ diff --git a/lib/experimental/class-gutenberg-rest-templates-controller.php b/lib/experimental/class-gutenberg-rest-template-revision-count.php similarity index 90% rename from lib/experimental/class-gutenberg-rest-templates-controller.php rename to lib/experimental/class-gutenberg-rest-template-revision-count.php index a7d1febd1136b..401a0fe252205 100644 --- a/lib/experimental/class-gutenberg-rest-templates-controller.php +++ b/lib/experimental/class-gutenberg-rest-template-revision-count.php @@ -2,10 +2,13 @@ /** * WP_REST_Templates_Controller class * + * When merging into core, prepare_item_for_response() and get_item_schema() + * should be merged with the parent methods + * * @package gutenberg */ - class Gutenberg_Experimental_REST_Templates_Controller extends WP_REST_Templates_Controller { + class Gutenberg_REST_Template_Revision_Count extends WP_REST_Templates_Controller { /** * Add revisions to the response. * diff --git a/lib/experimental/rest-api.php b/lib/experimental/rest-api.php index 1519a25fb6d52..4b3403bbf4dcf 100644 --- a/lib/experimental/rest-api.php +++ b/lib/experimental/rest-api.php @@ -33,9 +33,9 @@ function gutenberg_register_block_editor_settings() { * Registers the template REST API routes. */ function gutenberg_register_rest_template() { - $template_controller = new Gutenberg_Experimental_REST_Templates_Controller('wp_template'); + $template_controller = new Gutenberg_REST_Template_Revision_Count('wp_template'); $template_controller->register_routes(); - $template_parts_controller = new Gutenberg_Experimental_REST_Templates_Controller('wp_template_part'); + $template_parts_controller = new Gutenberg_REST_Template_Revision_Count('wp_template_part'); $template_parts_controller->register_routes(); } diff --git a/lib/load.php b/lib/load.php index 64b25eb6bc112..fd8a7b9057753 100644 --- a/lib/load.php +++ b/lib/load.php @@ -56,7 +56,7 @@ function gutenberg_is_experiment_enabled( $name ) { if ( ! class_exists( 'WP_Rest_Customizer_Nonces' ) ) { require_once __DIR__ . '/experimental/class-wp-rest-customizer-nonces.php'; } - require_once __DIR__ . '/experimental/class-gutenberg-rest-templates-controller.php'; + require_once __DIR__ . '/experimental/class-gutenberg-rest-template-revision-count.php'; require_once __DIR__ . '/experimental/rest-api.php'; } diff --git a/packages/edit-site/src/components/sidebar-edit-mode/template-card/index.js b/packages/edit-site/src/components/sidebar-edit-mode/template-card/index.js index 51ff3614d194c..56b01f071a3fb 100644 --- a/packages/edit-site/src/components/sidebar-edit-mode/template-card/index.js +++ b/packages/edit-site/src/components/sidebar-edit-mode/template-card/index.js @@ -13,7 +13,7 @@ import { decodeEntities } from '@wordpress/html-entities'; import { store as editSiteStore } from '../../../store'; import TemplateActions from './template-actions'; import TemplateAreas from './template-areas'; -import LastRevision from '../last-revision'; +import LastRevision from './last-revision'; export default function TemplateCard() { const { diff --git a/packages/edit-site/src/components/sidebar-edit-mode/last-revision/index.js b/packages/edit-site/src/components/sidebar-edit-mode/template-card/last-revision.js similarity index 93% rename from packages/edit-site/src/components/sidebar-edit-mode/last-revision/index.js rename to packages/edit-site/src/components/sidebar-edit-mode/template-card/last-revision.js index 90aad1d9fad66..54ef3d3953d34 100644 --- a/packages/edit-site/src/components/sidebar-edit-mode/last-revision/index.js +++ b/packages/edit-site/src/components/sidebar-edit-mode/template-card/last-revision.js @@ -49,7 +49,7 @@ const PostLastRevision = () => { revision: lastRevisionId, gutenberg: true, } ) } - className="editor-post-last-revision__title" + className="edit-site-template-last-revision__title" icon={ backup } > { sprintf( @@ -65,7 +65,7 @@ const PostLastRevision = () => { export default function LastRevision() { return ( - + diff --git a/packages/edit-site/src/components/sidebar-edit-mode/template-card/style.scss b/packages/edit-site/src/components/sidebar-edit-mode/template-card/style.scss index b02717793bb9b..fec050143bf5c 100644 --- a/packages/edit-site/src/components/sidebar-edit-mode/template-card/style.scss +++ b/packages/edit-site/src/components/sidebar-edit-mode/template-card/style.scss @@ -57,6 +57,34 @@ min-width: auto; } } + + .edit-site-template-last-revision__title { + width: 100%; + font-weight: 600; + + .dashicon { + margin-right: 5px; + } + } + + .components-button.edit-site-template-last-revision__title { + height: 100%; + + &:hover, + &:active { + // Override the default button hover style + background: $gray-100; + } + + &:focus { + box-shadow: inset 0 0 0 var(--wp-admin-border-width-focus) var(--wp-admin-theme-color); + border-radius: 0; + } + } + + .edit-site-template-last-revision__title.components-button { + padding: $grid-unit-20; + } } h3.edit-site-template-card__template-areas-title { From cc1c9c3798d1d8e57be735c0f6ee8189c5771855 Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Wed, 26 Oct 2022 11:07:46 -0400 Subject: [PATCH 05/17] Update lib/experimental/class-gutenberg-rest-template-revision-count.php Co-authored-by: Jonny Harris --- .../class-gutenberg-rest-template-revision-count.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/class-gutenberg-rest-template-revision-count.php b/lib/experimental/class-gutenberg-rest-template-revision-count.php index 401a0fe252205..3ad6e52ef7f78 100644 --- a/lib/experimental/class-gutenberg-rest-template-revision-count.php +++ b/lib/experimental/class-gutenberg-rest-template-revision-count.php @@ -23,7 +23,7 @@ public function prepare_item_for_response( $item, $request ) { // phpcs:ignore V $data = $response->get_data(); $fields = $this->get_fields_for_response( $request ); - if ( ! is_null( $template->wp_id ) && $template->wp_id !== 0 ) { + if ( ! empty( $template->wp_id ) && $template->wp_id > 0 ) { $revisions = wp_get_latest_revision_id_and_total_count( $template->wp_id ); } else { // TODO: Should this be included if it's a file-based template? From 2badd0b7ed288337e1acf082bf91216e50085305 Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Wed, 26 Oct 2022 11:30:36 -0400 Subject: [PATCH 06/17] Clean up controller loading --- lib/experimental/rest-api.php | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/experimental/rest-api.php b/lib/experimental/rest-api.php index 4b3403bbf4dcf..c67167be33e71 100644 --- a/lib/experimental/rest-api.php +++ b/lib/experimental/rest-api.php @@ -30,16 +30,24 @@ function gutenberg_register_block_editor_settings() { /** - * Registers the template REST API routes. + * Hook in to the nav menu item post type and decorate the template rest endpoint. + * + * When merging to core, this can be removed once Gutenberg_REST_Template_Revision_Count is + * merged with WP_REST_Template_Controller. + * + * @param array $args Current registered post type args. + * @param string $post_type Name of post type. + * + * @return array */ -function gutenberg_register_rest_template() { - $template_controller = new Gutenberg_REST_Template_Revision_Count('wp_template'); - $template_controller->register_routes(); - $template_parts_controller = new Gutenberg_REST_Template_Revision_Count('wp_template_part'); - $template_parts_controller->register_routes(); -} +function wp_api_template_revision_args( $args, $post_type ) { + if ( 'wp_template' === $post_type || 'wp_template_part' === $post_type ) { + $args['rest_controller_class'] = 'Gutenberg_REST_Template_Revision_Count'; + } -add_action( 'rest_api_init', 'gutenberg_register_rest_template' ); + return $args; +} +add_filter( 'register_post_type_args', 'wp_api_template_revision_args', 10, 2 ); /** * Shim for get_sample_permalink() to add support for auto-draft status. From b6caca0f77e5a01d1f522e948c9602be028baeb9 Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Wed, 26 Oct 2022 11:31:44 -0400 Subject: [PATCH 07/17] Fix contexts for revision data --- .../class-gutenberg-rest-template-revision-count.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/experimental/class-gutenberg-rest-template-revision-count.php b/lib/experimental/class-gutenberg-rest-template-revision-count.php index 3ad6e52ef7f78..44bfc34a6060d 100644 --- a/lib/experimental/class-gutenberg-rest-template-revision-count.php +++ b/lib/experimental/class-gutenberg-rest-template-revision-count.php @@ -56,14 +56,14 @@ public function get_item_schema() { $schema['properties']['revision_count'] = array( 'description' => __( 'The number of revisions of the template.' ), 'type' => 'integer', - 'context' => array( 'view', 'edit', 'embed' ), + 'context' => array( 'edit', 'embed' ), 'readonly' => true, ); $schema['properties']['latest_revision_id'] = array( 'description' => __( 'The id of the latest revision of the template.' ), 'type' => 'integer', - 'context' => array( 'view', 'edit', 'embed' ), + 'context' => array( 'edit', 'embed' ), 'readonly' => true, ); From c646a493ae8df6b58f4f57032715008dab7ad40c Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 27 Oct 2022 20:05:43 +0100 Subject: [PATCH 08/17] Prepare links method. --- ...gutenberg-rest-template-revision-count.php | 85 +++++++++++-------- 1 file changed, 48 insertions(+), 37 deletions(-) diff --git a/lib/experimental/class-gutenberg-rest-template-revision-count.php b/lib/experimental/class-gutenberg-rest-template-revision-count.php index 44bfc34a6060d..00ea9a50476f8 100644 --- a/lib/experimental/class-gutenberg-rest-template-revision-count.php +++ b/lib/experimental/class-gutenberg-rest-template-revision-count.php @@ -8,7 +8,7 @@ * @package gutenberg */ - class Gutenberg_REST_Template_Revision_Count extends WP_REST_Templates_Controller { +class Gutenberg_REST_Template_Revision_Count extends WP_REST_Templates_Controller { /** * Add revisions to the response. * @@ -19,54 +19,65 @@ class Gutenberg_REST_Template_Revision_Count extends WP_REST_Templates_Controlle public function prepare_item_for_response( $item, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable $template = $item; - $response = parent::prepare_item_for_response( $item, $request ); - $data = $response->get_data(); $fields = $this->get_fields_for_response( $request ); - if ( ! empty( $template->wp_id ) && $template->wp_id > 0 ) { - $revisions = wp_get_latest_revision_id_and_total_count( $template->wp_id ); - } else { - // TODO: Should this be included if it's a file-based template? - $revisions = array( - 'count' => 0, - 'latest_id' => 0, - ); - } - - if ( rest_is_field_included( 'revision_count', $fields ) ) { - $data['revision_count'] = (int) $revisions['count']; - } + $response = parent::prepare_item_for_response( $item, $request ); - if ( rest_is_field_included( 'latest_revision_id', $fields ) ) { - $data['latest_revision_id'] = (int) $revisions['latest_id']; + if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { + $links = $this->prepare_links( $template ); + $response->add_links( $links ); + if ( ! empty( $links['self']['href'] ) ) { + $actions = $this->get_available_actions(); + $self = $links['self']['href']; + foreach ( $actions as $rel ) { + $response->add_link( $rel, $self ); + } + } } - $response->set_data( $data ); return $response; } /** - * Adds revision_count and latest_revision_id to the template schema. + * Prepares links for the request. + * + * @since 6.2.0 * - * @return array Item schema data. + * @param WP_Block_Template $template Template instance. + * @return array Links for the given post. */ - public function get_item_schema() { - $schema = parent::get_item_schema(); - - $schema['properties']['revision_count'] = array( - 'description' => __( 'The number of revisions of the template.' ), - 'type' => 'integer', - 'context' => array( 'edit', 'embed' ), - 'readonly' => true, + protected function prepare_links( $template ) { + $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); + $links = array( + 'self' => array( + 'href' => rest_url( trailingslashit( $base ) . $template->id ), + ), + 'collection' => array( + 'href' => rest_url( rest_get_route_for_post_type_items( $this->post_type ) ), + ), + 'about' => array( + 'href' => rest_url( 'wp/v2/types/' . $this->post_type ), + ), ); - $schema['properties']['latest_revision_id'] = array( - 'description' => __( 'The id of the latest revision of the template.' ), - 'type' => 'integer', - 'context' => array( 'edit', 'embed' ), - 'readonly' => true, - ); + if ( post_type_supports( $this->post_type, 'revisions' ) && (int) $template->wp_id ) { + $revisions = wp_get_latest_revision_id_and_total_count( (int) $template->wp_id ); + $revisions_count = ! is_wp_error( $revisions ) ? $revisions['count'] : 0; + $revisions_base = sprintf( '/%s/%s/%s/revisions', $this->namespace, $this->rest_base, $template->id ); + + $links['version-history'] = array( + 'href' => rest_url( $revisions_base ), + 'count' => $revisions_count, + ); + + if ( $revisions_count > 0 ) { + $links['predecessor-version'] = array( + 'href' => rest_url( $revisions_base . '/' . $revisions['latest_id'] ), + 'id' => $revisions['latest_id'], + ); + } + } - return $schema; + return $links; } -} \ No newline at end of file +} From 865fbd7d14d7a3250a1383d0fb546d30d6eede9a Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Fri, 4 Nov 2022 15:23:00 -0400 Subject: [PATCH 09/17] Removing hrefs until Core issue resolved --- .../class-gutenberg-rest-template-revision-count.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/experimental/class-gutenberg-rest-template-revision-count.php b/lib/experimental/class-gutenberg-rest-template-revision-count.php index 00ea9a50476f8..365f71319096e 100644 --- a/lib/experimental/class-gutenberg-rest-template-revision-count.php +++ b/lib/experimental/class-gutenberg-rest-template-revision-count.php @@ -63,16 +63,13 @@ protected function prepare_links( $template ) { if ( post_type_supports( $this->post_type, 'revisions' ) && (int) $template->wp_id ) { $revisions = wp_get_latest_revision_id_and_total_count( (int) $template->wp_id ); $revisions_count = ! is_wp_error( $revisions ) ? $revisions['count'] : 0; - $revisions_base = sprintf( '/%s/%s/%s/revisions', $this->namespace, $this->rest_base, $template->id ); $links['version-history'] = array( - 'href' => rest_url( $revisions_base ), 'count' => $revisions_count, ); if ( $revisions_count > 0 ) { $links['predecessor-version'] = array( - 'href' => rest_url( $revisions_base . '/' . $revisions['latest_id'] ), 'id' => $revisions['latest_id'], ); } From 083114f9fa96f3d414cfac62fa4109d973d9e4ce Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Fri, 4 Nov 2022 16:15:42 -0400 Subject: [PATCH 10/17] Fix warnings --- ...gutenberg-rest-template-revision-count.php | 28 +++++++------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/lib/experimental/class-gutenberg-rest-template-revision-count.php b/lib/experimental/class-gutenberg-rest-template-revision-count.php index 365f71319096e..5690a371df9db 100644 --- a/lib/experimental/class-gutenberg-rest-template-revision-count.php +++ b/lib/experimental/class-gutenberg-rest-template-revision-count.php @@ -2,8 +2,8 @@ /** * WP_REST_Templates_Controller class * - * When merging into core, prepare_item_for_response() and get_item_schema() - * should be merged with the parent methods + * When merging into core, prepare_revision_links() should be merged with + * WP_REST_Templates_Controller::prepare_links(). * * @package gutenberg */ @@ -24,7 +24,7 @@ public function prepare_item_for_response( $item, $request ) { // phpcs:ignore V $response = parent::prepare_item_for_response( $item, $request ); if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { - $links = $this->prepare_links( $template ); + $links = $this->prepare_revision_links( $template ); $response->add_links( $links ); if ( ! empty( $links['self']['href'] ) ) { $actions = $this->get_available_actions(); @@ -39,37 +39,29 @@ public function prepare_item_for_response( $item, $request ) { // phpcs:ignore V } /** - * Prepares links for the request. + * Adds revisions to links. * * @since 6.2.0 * - * @param WP_Block_Template $template Template instance. + * @param WP_Block_Template $template Template instance. * @return array Links for the given post. */ - protected function prepare_links( $template ) { - $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); - $links = array( - 'self' => array( - 'href' => rest_url( trailingslashit( $base ) . $template->id ), - ), - 'collection' => array( - 'href' => rest_url( rest_get_route_for_post_type_items( $this->post_type ) ), - ), - 'about' => array( - 'href' => rest_url( 'wp/v2/types/' . $this->post_type ), - ), - ); + protected function prepare_revision_links( $template ) { + $links = array(); if ( post_type_supports( $this->post_type, 'revisions' ) && (int) $template->wp_id ) { $revisions = wp_get_latest_revision_id_and_total_count( (int) $template->wp_id ); $revisions_count = ! is_wp_error( $revisions ) ? $revisions['count'] : 0; + $revisions_base = sprintf( '/%s/%s/%s/revisions', $this->namespace, $this->rest_base, $template->id ); $links['version-history'] = array( + 'href' => rest_url( $revisions_base ), 'count' => $revisions_count, ); if ( $revisions_count > 0 ) { $links['predecessor-version'] = array( + 'href' => rest_url( $revisions_base . '/' . $revisions['latest_id'] ), 'id' => $revisions['latest_id'], ); } From ec47d4e6199407f41269219864d44995b8211662 Mon Sep 17 00:00:00 2001 From: George Hotelling Date: Fri, 4 Nov 2022 16:42:46 -0400 Subject: [PATCH 11/17] Lint --- .../class-gutenberg-rest-template-revision-count.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/experimental/class-gutenberg-rest-template-revision-count.php b/lib/experimental/class-gutenberg-rest-template-revision-count.php index 5690a371df9db..82bedd6e0c8ff 100644 --- a/lib/experimental/class-gutenberg-rest-template-revision-count.php +++ b/lib/experimental/class-gutenberg-rest-template-revision-count.php @@ -1,13 +1,16 @@ Date: Wed, 8 Feb 2023 11:00:41 +0200 Subject: [PATCH 12/17] remove selectors and use API logic in the revisions hook --- .../data/data-core-edit-site.md | 25 ---------------- .../template-card/last-revision.js | 25 +++++++++++----- packages/edit-site/src/store/selectors.js | 30 ------------------- 3 files changed, 17 insertions(+), 63 deletions(-) diff --git a/docs/reference-guides/data/data-core-edit-site.md b/docs/reference-guides/data/data-core-edit-site.md index 9eb0b8466b276..454da443fbf91 100644 --- a/docs/reference-guides/data/data-core-edit-site.md +++ b/docs/reference-guides/data/data-core-edit-site.md @@ -32,35 +32,10 @@ _Returns_ - `Object`: Post object. -### getCurrentTemplateLastRevisionId - -Returns the last revision ID of the template currently being edited, -or null if the template is from the theme - -_Parameters_ - -- _state_ `Object`: Global application state. - -_Returns_ - -- `?number`: ID of the last revision. - ### getCurrentTemplateNavigationPanelSubMenu > **Deprecated** -### getCurrentTemplateRevisionsCount - -Returns the number of revisions of the template currently being edited. - -_Parameters_ - -- _state_ `Object`: Global application state. - -_Returns_ - -- `number`: Number of revisions. - ### getCurrentTemplateTemplateParts Returns the template parts and their blocks for the current edited template. diff --git a/packages/edit-site/src/components/sidebar-edit-mode/template-card/last-revision.js b/packages/edit-site/src/components/sidebar-edit-mode/template-card/last-revision.js index 54ef3d3953d34..1bad1d6d5d801 100644 --- a/packages/edit-site/src/components/sidebar-edit-mode/template-card/last-revision.js +++ b/packages/edit-site/src/components/sidebar-edit-mode/template-card/last-revision.js @@ -13,18 +13,27 @@ import { PostTypeSupportCheck } from '@wordpress/editor'; */ import { store as editSiteStore } from '../../../store'; -const useRevisionData = () => - useSelect( ( select ) => { - const { - getCurrentTemplateLastRevisionId, - getCurrentTemplateRevisionsCount, - } = select( editSiteStore ); +const useRevisionData = () => { + const { currentTemplate } = useSelect( ( select ) => { + const { getCurrentTemplate } = select( editSiteStore ); return { - lastRevisionId: getCurrentTemplateLastRevisionId(), - revisionsCount: getCurrentTemplateRevisionsCount(), + currentTemplate: getCurrentTemplate(), }; } ); + const lastRevisionId = + currentTemplate?._links?.[ 'predecessor-version' ]?.[ 0 ]?.id ?? null; + + const revisionsCount = + ( currentTemplate?._links?.[ 'version-history' ]?.[ 0 ]?.count ?? 0 ) + + 1; + + return { + lastRevisionId, + revisionsCount, + }; +}; + function PostLastRevisionCheck( { children } ) { const { lastRevisionId, revisionsCount } = useRevisionData(); diff --git a/packages/edit-site/src/store/selectors.js b/packages/edit-site/src/store/selectors.js index 54fb8f301b338..6e148b225de47 100644 --- a/packages/edit-site/src/store/selectors.js +++ b/packages/edit-site/src/store/selectors.js @@ -313,36 +313,6 @@ export const getCurrentTemplate = createRegistrySelector( } ); -/** - * Returns the number of revisions of the template currently being edited. - * - * @param {Object} state Global application state. - * - * @return {number} Number of revisions. - */ -export function getCurrentTemplateRevisionsCount( state ) { - const template = getCurrentTemplate( state ); - if ( template.source === 'theme' ) { - return 0; - } - return ( template?._links?.[ 'version-history' ]?.[ 0 ]?.count ?? 0 ) + 1; -} - -/** - * Returns the last revision ID of the template currently being edited, - * or null if the template is from the theme - * - * @param {Object} state Global application state. - * - * @return {?number} ID of the last revision. - */ -export function getCurrentTemplateLastRevisionId( state ) { - return ( - getCurrentTemplate( state )?._links?.[ 'predecessor-version' ]?.[ 0 ] - ?.id ?? null - ); -} - /** * @deprecated */ From cb99b7ab9d6b55812966f723f61109d6ea3d2e5e Mon Sep 17 00:00:00 2001 From: Andrei Draganescu Date: Wed, 8 Feb 2023 11:03:41 +0200 Subject: [PATCH 13/17] fix a comment --- lib/experimental/rest-api.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/experimental/rest-api.php b/lib/experimental/rest-api.php index c67167be33e71..31a96b6572f26 100644 --- a/lib/experimental/rest-api.php +++ b/lib/experimental/rest-api.php @@ -30,7 +30,8 @@ function gutenberg_register_block_editor_settings() { /** - * Hook in to the nav menu item post type and decorate the template rest endpoint. + * Hook in to the template and template part post types and decorate + * the rest endpoint with the revision count. * * When merging to core, this can be removed once Gutenberg_REST_Template_Revision_Count is * merged with WP_REST_Template_Controller. From 6020618a66170047022264e5558f4c88378bf2e6 Mon Sep 17 00:00:00 2001 From: Andrei Draganescu Date: Wed, 8 Feb 2023 11:48:04 +0200 Subject: [PATCH 14/17] revisited the look and placement of the revisions button --- .../sidebar-edit-mode/template-card/index.js | 38 +++++++++++-------- .../template-card/last-revision.js | 6 +-- .../template-card/style.scss | 30 ++------------- 3 files changed, 28 insertions(+), 46 deletions(-) diff --git a/packages/edit-site/src/components/sidebar-edit-mode/template-card/index.js b/packages/edit-site/src/components/sidebar-edit-mode/template-card/index.js index 56b01f071a3fb..da03a769b306c 100644 --- a/packages/edit-site/src/components/sidebar-edit-mode/template-card/index.js +++ b/packages/edit-site/src/components/sidebar-edit-mode/template-card/index.js @@ -1,8 +1,9 @@ /** * WordPress dependencies */ +import { __ } from '@wordpress/i18n'; import { useSelect } from '@wordpress/data'; -import { Icon } from '@wordpress/components'; +import { PanelRow, Icon } from '@wordpress/components'; import { store as editorStore } from '@wordpress/editor'; import { store as coreStore } from '@wordpress/core-data'; import { decodeEntities } from '@wordpress/html-entities'; @@ -39,21 +40,28 @@ export default function TemplateCard() { } return ( -
- -
-
-

- { decodeEntities( title ) } -

- + <> +
+ +
+
+

+ { decodeEntities( title ) } +

+ +
+
+ { decodeEntities( description ) } +
+
-
- { decodeEntities( description ) } -
- -
-
+ + + + ); } diff --git a/packages/edit-site/src/components/sidebar-edit-mode/template-card/last-revision.js b/packages/edit-site/src/components/sidebar-edit-mode/template-card/last-revision.js index 1bad1d6d5d801..146f86dc9da54 100644 --- a/packages/edit-site/src/components/sidebar-edit-mode/template-card/last-revision.js +++ b/packages/edit-site/src/components/sidebar-edit-mode/template-card/last-revision.js @@ -2,7 +2,7 @@ * WordPress dependencies */ import { useSelect } from '@wordpress/data'; -import { PanelBody, Button } from '@wordpress/components'; +import { Button } from '@wordpress/components'; import { sprintf, _n } from '@wordpress/i18n'; import { backup } from '@wordpress/icons'; import { addQueryArgs } from '@wordpress/url'; @@ -74,9 +74,7 @@ const PostLastRevision = () => { export default function LastRevision() { return ( - - - + ); } diff --git a/packages/edit-site/src/components/sidebar-edit-mode/template-card/style.scss b/packages/edit-site/src/components/sidebar-edit-mode/template-card/style.scss index fec050143bf5c..a877dae5a7677 100644 --- a/packages/edit-site/src/components/sidebar-edit-mode/template-card/style.scss +++ b/packages/edit-site/src/components/sidebar-edit-mode/template-card/style.scss @@ -57,34 +57,10 @@ min-width: auto; } } +} - .edit-site-template-last-revision__title { - width: 100%; - font-weight: 600; - - .dashicon { - margin-right: 5px; - } - } - - .components-button.edit-site-template-last-revision__title { - height: 100%; - - &:hover, - &:active { - // Override the default button hover style - background: $gray-100; - } - - &:focus { - box-shadow: inset 0 0 0 var(--wp-admin-border-width-focus) var(--wp-admin-theme-color); - border-radius: 0; - } - } - - .edit-site-template-last-revision__title.components-button { - padding: $grid-unit-20; - } +.edit-site-template-revisions { + padding-left: -$grid-unit-10; } h3.edit-site-template-card__template-areas-title { From 4d8636c5074bc5098a21b5a72696157acd02eb69 Mon Sep 17 00:00:00 2001 From: Andrei Draganescu Date: Wed, 8 Feb 2023 11:57:05 +0200 Subject: [PATCH 15/17] small tweak to position of the revisions button --- .../src/components/sidebar-edit-mode/template-card/style.scss | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/edit-site/src/components/sidebar-edit-mode/template-card/style.scss b/packages/edit-site/src/components/sidebar-edit-mode/template-card/style.scss index a877dae5a7677..67054c25d2476 100644 --- a/packages/edit-site/src/components/sidebar-edit-mode/template-card/style.scss +++ b/packages/edit-site/src/components/sidebar-edit-mode/template-card/style.scss @@ -60,7 +60,7 @@ } .edit-site-template-revisions { - padding-left: -$grid-unit-10; + margin-left: math.div(-$grid-unit-10, 2); } h3.edit-site-template-card__template-areas-title { From 7e098514ef17fd5746b29acd529cf8488f014d8d Mon Sep 17 00:00:00 2001 From: Andrei Draganescu Date: Wed, 8 Feb 2023 12:06:39 +0200 Subject: [PATCH 16/17] optimise revision count calculation to avoid rerenders --- .../template-card/last-revision.js | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/edit-site/src/components/sidebar-edit-mode/template-card/last-revision.js b/packages/edit-site/src/components/sidebar-edit-mode/template-card/last-revision.js index 146f86dc9da54..9756c7c366eb6 100644 --- a/packages/edit-site/src/components/sidebar-edit-mode/template-card/last-revision.js +++ b/packages/edit-site/src/components/sidebar-edit-mode/template-card/last-revision.js @@ -13,27 +13,27 @@ import { PostTypeSupportCheck } from '@wordpress/editor'; */ import { store as editSiteStore } from '../../../store'; -const useRevisionData = () => { - const { currentTemplate } = useSelect( ( select ) => { +const useRevisionData = () => + useSelect( ( select ) => { const { getCurrentTemplate } = select( editSiteStore ); + + const currentTemplate = getCurrentTemplate(); + + const lastRevisionId = + currentTemplate?._links?.[ 'predecessor-version' ]?.[ 0 ]?.id ?? + null; + + const revisionsCount = + ( currentTemplate?._links?.[ 'version-history' ]?.[ 0 ]?.count ?? + 0 ) + 1; + return { currentTemplate: getCurrentTemplate(), + lastRevisionId, + revisionsCount, }; } ); - const lastRevisionId = - currentTemplate?._links?.[ 'predecessor-version' ]?.[ 0 ]?.id ?? null; - - const revisionsCount = - ( currentTemplate?._links?.[ 'version-history' ]?.[ 0 ]?.count ?? 0 ) + - 1; - - return { - lastRevisionId, - revisionsCount, - }; -}; - function PostLastRevisionCheck( { children } ) { const { lastRevisionId, revisionsCount } = useRevisionData(); From a0ab799ae2039febb271cd856ced8364b5ab4464 Mon Sep 17 00:00:00 2001 From: Andrei Draganescu Date: Wed, 8 Feb 2023 13:40:19 +0200 Subject: [PATCH 17/17] removes the selector to get currently edited template in favor of using the edit site hook for the same purpose --- .../data/data-core-edit-site.md | 14 -------- .../template-card/last-revision.js | 33 ++++++++----------- packages/edit-site/src/store/selectors.js | 25 -------------- 3 files changed, 14 insertions(+), 58 deletions(-) diff --git a/docs/reference-guides/data/data-core-edit-site.md b/docs/reference-guides/data/data-core-edit-site.md index 454da443fbf91..53fd4d138383a 100644 --- a/docs/reference-guides/data/data-core-edit-site.md +++ b/docs/reference-guides/data/data-core-edit-site.md @@ -18,20 +18,6 @@ _Returns_ - `Object`: Whether the current user can create media or not. -### getCurrentTemplate - -Returns the post currently being edited in its last known saved state, not -including unsaved edits. Returns an object containing relevant default post -values if the post has not yet been saved. - -_Parameters_ - -- _state_ `Object`: Global application state. - -_Returns_ - -- `Object`: Post object. - ### getCurrentTemplateNavigationPanelSubMenu > **Deprecated** diff --git a/packages/edit-site/src/components/sidebar-edit-mode/template-card/last-revision.js b/packages/edit-site/src/components/sidebar-edit-mode/template-card/last-revision.js index 9756c7c366eb6..3782dccfb7e28 100644 --- a/packages/edit-site/src/components/sidebar-edit-mode/template-card/last-revision.js +++ b/packages/edit-site/src/components/sidebar-edit-mode/template-card/last-revision.js @@ -1,7 +1,6 @@ /** * WordPress dependencies */ -import { useSelect } from '@wordpress/data'; import { Button } from '@wordpress/components'; import { sprintf, _n } from '@wordpress/i18n'; import { backup } from '@wordpress/icons'; @@ -11,28 +10,24 @@ import { PostTypeSupportCheck } from '@wordpress/editor'; /** * Internal dependencies */ -import { store as editSiteStore } from '../../../store'; +import useEditedEntityRecord from '../../use-edited-entity-record'; -const useRevisionData = () => - useSelect( ( select ) => { - const { getCurrentTemplate } = select( editSiteStore ); +const useRevisionData = () => { + const { record: currentTemplate } = useEditedEntityRecord(); - const currentTemplate = getCurrentTemplate(); + const lastRevisionId = + currentTemplate?._links?.[ 'predecessor-version' ]?.[ 0 ]?.id ?? null; - const lastRevisionId = - currentTemplate?._links?.[ 'predecessor-version' ]?.[ 0 ]?.id ?? - null; + const revisionsCount = + ( currentTemplate?._links?.[ 'version-history' ]?.[ 0 ]?.count ?? 0 ) + + 1; - const revisionsCount = - ( currentTemplate?._links?.[ 'version-history' ]?.[ 0 ]?.count ?? - 0 ) + 1; - - return { - currentTemplate: getCurrentTemplate(), - lastRevisionId, - revisionsCount, - }; - } ); + return { + currentTemplate, + lastRevisionId, + revisionsCount, + }; +}; function PostLastRevisionCheck( { children } ) { const { lastRevisionId, revisionsCount } = useRevisionData(); diff --git a/packages/edit-site/src/store/selectors.js b/packages/edit-site/src/store/selectors.js index 6e148b225de47..0b620a4b05c5d 100644 --- a/packages/edit-site/src/store/selectors.js +++ b/packages/edit-site/src/store/selectors.js @@ -288,31 +288,6 @@ export function getEditorMode( state ) { return __unstableGetPreference( state, 'editorMode' ); } -/** - * Returns the post currently being edited in its last known saved state, not - * including unsaved edits. Returns an object containing relevant default post - * values if the post has not yet been saved. - * - * @param {Object} state Global application state. - * - * @return {Object} Post object. - */ -export const getCurrentTemplate = createRegistrySelector( - ( select ) => ( state ) => { - const templateType = getEditedPostType( state ); - const templateId = getEditedPostId( state ); - const template = templateId - ? select( coreDataStore ).getEntityRecord( - 'postType', - templateType, - templateId - ) - : null; - - return template; - } -); - /** * @deprecated */