From 91a1b078d28908341e20671de7f062bd4e9d551d Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 27 Oct 2022 20:05:43 +0100 Subject: [PATCH] Prepare links method. --- ...gutenberg-rest-template-revision-count.php | 85 +++++++++++-------- packages/edit-site/src/store/selectors.js | 4 +- 2 files changed, 50 insertions(+), 39 deletions(-) diff --git a/lib/experimental/class-gutenberg-rest-template-revision-count.php b/lib/experimental/class-gutenberg-rest-template-revision-count.php index 44bfc34a6060d8..00ea9a50476f8e 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 +} diff --git a/packages/edit-site/src/store/selectors.js b/packages/edit-site/src/store/selectors.js index d2ed050f16c475..d3e0f32ce63ef1 100644 --- a/packages/edit-site/src/store/selectors.js +++ b/packages/edit-site/src/store/selectors.js @@ -429,7 +429,7 @@ export function getCurrentTemplateRevisionsCount( state ) { if ( template.source === 'theme' ) { return 0; } - return ( template.revision_count ?? 0 ) + 1; + return ( template?._links?.['version-history']?.[ 0 ]?.count ?? 0 ) + 1; } /** @@ -441,5 +441,5 @@ export function getCurrentTemplateRevisionsCount( state ) { * @return {?number} ID of the last revision. */ export function getCurrentTemplateLastRevisionId( state ) { - return getCurrentTemplate( state ).latest_revision_id ?? null; + return getCurrentTemplate( state )?._links?.['predecessor-version']?.[ 0 ]?.id ?? null; }