From 2889f76e920f80f22bc7f35a3e0bbedfb9dbff67 Mon Sep 17 00:00:00 2001 From: youknowriad Date: Wed, 31 Jan 2024 10:41:14 +0000 Subject: [PATCH] REST API: Add route for single styles revisions. Adds a route for single global styles revisions: /wp/v2/global-styles/${ parentId }/revisions/${ revisionsId } This fixes the `getRevision` actions in the core-data package. Props ramonopoly, get_dave. Fixes #59810. Built from https://develop.svn.wordpress.org/trunk@57494 git-svn-id: http://core.svn.wordpress.org/trunk@56995 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- ...est-global-styles-revisions-controller.php | 77 +++++++++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php index 9ce7e2b3df..db1be197e5 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php @@ -47,6 +47,7 @@ class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Controller { * Registers the controller's routes. * * @since 6.3.0 + * @since 6.5.0 Added route to fetch individual global styles revisions. */ public function register_routes() { register_rest_route( @@ -68,6 +69,32 @@ class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Controller { 'schema' => array( $this, 'get_public_item_schema' ), ) ); + + register_rest_route( + $this->namespace, + '/' . $this->parent_base . '/(?P[\d]+)/' . $this->rest_base . '/(?P[\d]+)', + array( + 'args' => array( + 'parent' => array( + 'description' => __( 'The ID for the parent of the global styles revision.' ), + 'type' => 'integer', + ), + 'id' => array( + 'description' => __( 'Unique identifier for the global styles revision.' ), + 'type' => 'integer', + ), + ), + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_item' ), + 'permission_callback' => array( $this, 'get_item_permissions_check' ), + 'args' => array( + 'context' => $this->get_context_param( array( 'default' => 'view' ) ), + ), + ), + 'schema' => array( $this, 'get_public_item_schema' ), + ) + ); } /** @@ -241,6 +268,56 @@ class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Controller { return $response; } + /** + * Retrieves one global styles revision from the collection. + * + * @since 6.5.0 + * + * @param WP_REST_Request $request Full details about the request. + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. + */ + public function get_item( $request ) { + $parent = $this->get_parent( $request['parent'] ); + if ( is_wp_error( $parent ) ) { + return $parent; + } + + $revision = $this->get_revision( $request['id'] ); + if ( is_wp_error( $revision ) ) { + return $revision; + } + + $response = $this->prepare_item_for_response( $revision, $request ); + return rest_ensure_response( $response ); + } + + /** + * Gets the global styles revision, if the ID is valid. + * + * @since 6.5.0 + * + * @param int $id Supplied ID. + * @return WP_Post|WP_Error Revision post object if ID is valid, WP_Error otherwise. + */ + protected function get_revision( $id ) { + $error = new WP_Error( + 'rest_post_invalid_id', + __( 'Invalid global styles revision ID.' ), + array( 'status' => 404 ) + ); + + if ( (int) $id <= 0 ) { + return $error; + } + + $revision = get_post( (int) $id ); + if ( empty( $revision ) || empty( $revision->ID ) || 'revision' !== $revision->post_type ) { + return $error; + } + + return $revision; + } + /** * Checks the post_date_gmt or modified_gmt and prepare any post or * modified date for single post output. diff --git a/wp-includes/version.php b/wp-includes/version.php index 5be115df92..ac005eb0d5 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.5-alpha-57493'; +$wp_version = '6.5-alpha-57494'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.