-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add template revision button (#45215)
* Initial work on revision button for templates * Fix warning * Refactor * Refactor * Update lib/experimental/class-gutenberg-rest-template-revision-count.php Co-authored-by: Jonny Harris <[email protected]> * Clean up controller loading * Fix contexts for revision data * Prepare links method. * Removing hrefs until Core issue resolved * Fix warnings * Lint * remove selectors and use API logic in the revisions hook * fix a comment * revisited the look and placement of the revisions button * small tweak to position of the revisions button * optimise revision count calculation to avoid rerenders * removes the selector to get currently edited template in favor of using the edit site hook for the same purpose --------- Co-authored-by: Jonny Harris <[email protected]> Co-authored-by: Jonny Harris <[email protected]> Co-authored-by: Andrei Draganescu <[email protected]>
- Loading branch information
1 parent
8dee3e6
commit a1f736b
Showing
6 changed files
with
202 additions
and
14 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
lib/experimental/class-gutenberg-rest-template-revision-count.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
/** | ||
* REST API: Gutenberg_REST_Template_Revision_Count class | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Gutenberg_REST_Template_Revision_Count class | ||
* | ||
* When merging into core, prepare_revision_links() should be merged with | ||
* WP_REST_Templates_Controller::prepare_links(). | ||
*/ | ||
class Gutenberg_REST_Template_Revision_Count extends WP_REST_Templates_Controller { | ||
/** | ||
* Add revisions to the response. | ||
* | ||
* @param WP_Block_Template $item Template instance. | ||
* @param WP_REST_Request $request Request object. | ||
* @return WP_REST_Response Response object. | ||
*/ | ||
public function prepare_item_for_response( $item, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | ||
$template = $item; | ||
|
||
$fields = $this->get_fields_for_response( $request ); | ||
|
||
$response = parent::prepare_item_for_response( $item, $request ); | ||
|
||
if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { | ||
$links = $this->prepare_revision_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 ); | ||
} | ||
} | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Adds revisions to links. | ||
* | ||
* @since 6.2.0 | ||
* | ||
* @param WP_Block_Template $template Template instance. | ||
* @return array Links for the given post. | ||
*/ | ||
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'], | ||
); | ||
} | ||
} | ||
|
||
return $links; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
packages/edit-site/src/components/sidebar-edit-mode/template-card/last-revision.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { 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 useEditedEntityRecord from '../../use-edited-entity-record'; | ||
|
||
const useRevisionData = () => { | ||
const { record: currentTemplate } = useEditedEntityRecord(); | ||
|
||
const lastRevisionId = | ||
currentTemplate?._links?.[ 'predecessor-version' ]?.[ 0 ]?.id ?? null; | ||
|
||
const revisionsCount = | ||
( currentTemplate?._links?.[ 'version-history' ]?.[ 0 ]?.count ?? 0 ) + | ||
1; | ||
|
||
return { | ||
currentTemplate, | ||
lastRevisionId, | ||
revisionsCount, | ||
}; | ||
}; | ||
|
||
function PostLastRevisionCheck( { children } ) { | ||
const { lastRevisionId, revisionsCount } = useRevisionData(); | ||
|
||
if ( ! lastRevisionId || revisionsCount < 2 ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<PostTypeSupportCheck supportKeys="revisions"> | ||
{ children } | ||
</PostTypeSupportCheck> | ||
); | ||
} | ||
|
||
const PostLastRevision = () => { | ||
const { lastRevisionId, revisionsCount } = useRevisionData(); | ||
|
||
return ( | ||
<PostLastRevisionCheck> | ||
<Button | ||
href={ addQueryArgs( 'revision.php', { | ||
revision: lastRevisionId, | ||
gutenberg: true, | ||
} ) } | ||
className="edit-site-template-last-revision__title" | ||
icon={ backup } | ||
> | ||
{ sprintf( | ||
/* translators: %d: number of revisions */ | ||
_n( '%d Revision', '%d Revisions', revisionsCount ), | ||
revisionsCount | ||
) } | ||
</Button> | ||
</PostLastRevisionCheck> | ||
); | ||
}; | ||
|
||
export default function LastRevision() { | ||
return ( | ||
<PostLastRevisionCheck> | ||
<PostLastRevision /> | ||
</PostLastRevisionCheck> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a1f736b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flaky tests detected in a1f736b.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4124409108
📝 Reported issues:
specs/editor/blocks/query.test.js
/test/e2e/specs/widgets/customizing-widgets.spec.js