-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Template revisions API: move back to experimental #51774
Merged
ramonjd
merged 1 commit into
trunk
from
move/template-revisions-backend-to-experimental
Jun 25, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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 | ||
* | ||
* Template revision changes are waiting on a core change to be merged. | ||
* See: https://github.com/WordPress/gutenberg/pull/45215#issuecomment-1592704026 | ||
* When merging into core, prepare_revision_links() should be merged with | ||
* WP_REST_Templates_Controller::prepare_links(). | ||
*/ | ||
class Gutenberg_REST_Template_Revision_Count extends Gutenberg_REST_Templates_Controller_6_3 { | ||
/** | ||
* 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. | ||
* | ||
* @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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I kept the naming convention from #45215
Was considering
Gutenberg_REST_Templates_Controller
, but there are no other features here. Not sure.