From dca9275574cbb343130fb00b87e505de73d91eae Mon Sep 17 00:00:00 2001 From: ramon Date: Thu, 22 Jun 2023 12:36:53 +1000 Subject: [PATCH] This feature won't make it to 6.3 without a Core change Move from 6.3 to experimental https://github.com/WordPress/gutenberg/pull/45215#issuecomment-1592704026 --- ...utenberg-rest-templates-controller-6-3.php | 65 +--------------- lib/compat/wordpress-6.3/rest-api.php | 5 +- ...gutenberg-rest-template-revision-count.php | 75 +++++++++++++++++++ lib/experimental/rest-api.php | 21 ++++++ lib/load.php | 1 + 5 files changed, 100 insertions(+), 67 deletions(-) create mode 100644 lib/experimental/class-gutenberg-rest-template-revision-count.php diff --git a/lib/compat/wordpress-6.3/class-gutenberg-rest-templates-controller-6-3.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-templates-controller-6-3.php index 7d59ccc9c71fc..a92cbd1e2c171 100644 --- a/lib/compat/wordpress-6.3/class-gutenberg-rest-templates-controller-6-3.php +++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-templates-controller-6-3.php @@ -1,6 +1,6 @@ prepare_item_for_response( $fallback_template, $request ); return rest_ensure_response( $response ); } - - /** - * Add revisions to the response. - * - * @since 6.3.0 Added prepare_revision_links() method to get revision links. - * - * @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.3.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; - } } diff --git a/lib/compat/wordpress-6.3/rest-api.php b/lib/compat/wordpress-6.3/rest-api.php index ed6832c3b8411..87b52ed9b8992 100644 --- a/lib/compat/wordpress-6.3/rest-api.php +++ b/lib/compat/wordpress-6.3/rest-api.php @@ -23,15 +23,14 @@ */ function gutenberg_update_templates_template_parts_rest_controller( $args, $post_type ) { if ( in_array( $post_type, array( 'wp_template', 'wp_template_part' ), true ) ) { - $template_edit_link = 'site-editor.php?' . build_query( + $template_edit_link = 'site-editor.php?' . build_query( array( 'postType' => $post_type, 'postId' => '%s', 'canvas' => 'edit', ) ); - $args['_edit_link'] = $template_edit_link; - $args['rest_controller_class'] = 'Gutenberg_REST_Templates_Controller_6_3'; + $args['_edit_link'] = $template_edit_link; } if ( in_array( $post_type, array( 'wp_global_styles' ), true ) ) { diff --git a/lib/experimental/class-gutenberg-rest-template-revision-count.php b/lib/experimental/class-gutenberg-rest-template-revision-count.php new file mode 100644 index 0000000000000..f3080f27af3d7 --- /dev/null +++ b/lib/experimental/class-gutenberg-rest-template-revision-count.php @@ -0,0 +1,75 @@ +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; + } +} diff --git a/lib/experimental/rest-api.php b/lib/experimental/rest-api.php index 4eca4232d3eb5..4a8bbea1548a6 100644 --- a/lib/experimental/rest-api.php +++ b/lib/experimental/rest-api.php @@ -111,3 +111,24 @@ function gutenberg_auto_draft_get_sample_permalink( $permalink, $id, $title, $na return $permalink; } add_filter( 'get_sample_permalink', 'gutenberg_auto_draft_get_sample_permalink', 10, 5 ); + +/** + * 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. + * + * @param array $args Current registered post type args. + * @param string $post_type Name of post type. + * + * @return array + */ +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'; + } + + return $args; +} +add_filter( 'register_post_type_args', 'wp_api_template_revision_args', 10, 2 ); diff --git a/lib/load.php b/lib/load.php index 0fd23fc28f513..0ef72a48ebdbb 100644 --- a/lib/load.php +++ b/lib/load.php @@ -59,6 +59,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-template-revision-count.php'; require_once __DIR__ . '/experimental/rest-api.php'; }