From 3d5be534be5e7a00415f77dc4d1b40ae267ffbc5 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Sat, 28 Nov 2026 16:09:18 -0800 Subject: [PATCH 1/2] REST API: Add a route to restore a revision. The block editor restores a revision by copying its fields into a post update, so `wp_restore_post_revision()` does not run: the `wp_restore_post_revision` action does not fire and revisioned meta that is not shown in REST is not restored. Add `POST /wp/v2///revisions//restore`, which restores through `wp_restore_post_revision()` as the revisions screen does and returns the restored parent in the `edit` context. See #66865. --- .../class-wp-rest-revisions-controller.php | 121 +++++++ ...-wp-rest-template-revisions-controller.php | 60 +++ .../tests/rest-api/rest-schema-setup.php | 6 + .../wpRestRevisionsControllerRestore.php | 341 ++++++++++++++++++ tests/qunit/fixtures/wp-api-generated.js | 150 ++++++++ 5 files changed, 678 insertions(+) create mode 100644 tests/phpunit/tests/rest-api/wpRestRevisionsControllerRestore.php diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php index a68f0cd5a513f..f4ca5d6ca269e 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php @@ -138,6 +138,28 @@ public function register_routes() { 'schema' => array( $this, 'get_public_item_schema' ), ) ); + + register_rest_route( + $this->namespace, + '/' . $this->parent_base . '/(?P[\d]+)/' . $this->rest_base . '/(?P[\d]+)/restore', + array( + 'args' => array( + 'parent' => array( + 'description' => __( 'The ID for the parent of the revision.' ), + 'type' => 'integer', + ), + 'id' => array( + 'description' => __( 'Unique identifier for the revision.' ), + 'type' => 'integer', + ), + ), + array( + 'methods' => WP_REST_Server::CREATABLE, + 'callback' => array( $this, 'restore_item' ), + 'permission_callback' => array( $this, 'restore_item_permissions_check' ), + ), + ) + ); } /** @@ -553,6 +575,105 @@ public function delete_item( $request ) { return $response; } + /** + * Checks if a given request has access to restore a revision. + * + * @since 7.3.0 + * + * @param WP_REST_Request $request Full details about the request. + * @return true|WP_Error True if the request has access to restore the revision, WP_Error object otherwise. + */ + public function restore_item_permissions_check( $request ) { + $parent = $this->get_parent( $request['parent'] ); + if ( is_wp_error( $parent ) ) { + return $parent; + } + + if ( ! current_user_can( 'edit_post', $parent->ID ) ) { + return new WP_Error( + 'rest_cannot_restore_revision', + __( 'Sorry, you are not allowed to restore revisions of this post.' ), + array( 'status' => rest_authorization_required_code() ) + ); + } + + return true; + } + + /** + * Restores a revision and returns the updated parent. + * + * The restore runs through wp_restore_post_revision(), as it does on the + * revisions screen, so the `wp_restore_post_revision` action fires and all + * revisioned meta is restored, including meta that is not shown in REST. + * + * @since 7.3.0 + * + * @param WP_REST_Request $request Full details about the request. + * @return WP_REST_Response|WP_Error The parent as its own endpoint returns it for editing, or WP_Error object on failure. + */ + public function restore_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; + } + + if ( (int) $parent->ID !== (int) $revision->post_parent ) { + return new WP_Error( + 'rest_revision_parent_id_mismatch', + /* translators: %d: A post id. */ + sprintf( __( 'The revision does not belong to the specified parent with id of "%d"' ), $parent->ID ), + array( 'status' => 404 ) + ); + } + + // The revisions screen only restores autosaves while revisions are disabled. + if ( ! wp_revisions_enabled( $parent ) && ! wp_is_post_autosave( $revision ) ) { + return new WP_Error( + 'rest_revisions_disabled', + __( 'Revisions are disabled for this post.' ), + array( 'status' => 400 ) + ); + } + + if ( ! wp_restore_post_revision( $revision->ID ) ) { + return new WP_Error( + 'rest_cannot_restore_revision', + __( 'The revision could not be restored.' ), + array( 'status' => 500 ) + ); + } + + $parent_request = new WP_REST_Request( WP_REST_Server::READABLE ); + $parent_request->set_param( 'context', 'edit' ); + + return $this->prepare_restored_parent_for_response( $parent->ID, $request, $parent_request ); + } + + /** + * Prepares the parent of a restored revision as its own endpoint returns it. + * + * @since 7.3.0 + * + * @param int $parent_id ID of the restored post. + * @param WP_REST_Request $request The restore request. + * @param WP_REST_Request $parent_request Request to prepare the parent with. + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. + */ + protected function prepare_restored_parent_for_response( $parent_id, $request, $parent_request ) { + $parent = $this->get_parent( $parent_id ); + if ( is_wp_error( $parent ) ) { + return $parent; + } + + return rest_ensure_response( $this->parent_controller->prepare_item_for_response( $parent, $parent_request ) ); + } + /** * Determines the allowed query_vars for a get_items() response and prepares * them for WP_Query. diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-template-revisions-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-template-revisions-controller.php index f88b60372b038..696a2bde7df4e 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-template-revisions-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-template-revisions-controller.php @@ -153,6 +153,41 @@ public function register_routes() { 'schema' => array( $this, 'get_public_item_schema' ), ) ); + + register_rest_route( + $this->namespace, + sprintf( + '/%s/(?P%s%s)/%s/%s/restore', + $this->parent_base, + /* + * Matches theme's directory: `/themes///` or `/themes//`. + * Excludes invalid directory name characters: `/:<>*?"|`. + */ + '([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)', + // Matches the template name. + '[\/\w%-]+', + $this->rest_base, + '(?P[\d]+)' + ), + array( + 'args' => array( + 'parent' => array( + 'description' => __( 'The id of a template' ), + 'type' => 'string', + 'sanitize_callback' => array( $this->parent_controller, '_sanitize_template_id' ), + ), + 'id' => array( + 'description' => __( 'Unique identifier for the revision.' ), + 'type' => 'integer', + ), + ), + array( + 'methods' => WP_REST_Server::CREATABLE, + 'callback' => array( $this, 'restore_item' ), + 'permission_callback' => array( $this, 'restore_item_permissions_check' ), + ), + ) + ); } /** @@ -187,6 +222,31 @@ protected function get_parent( $parent_template_id ) { return get_post( $template->wp_id ); } + /** + * Prepares the template of a restored revision as its own endpoint returns it. + * + * @since 7.3.0 + * + * @param int $parent_id ID of the restored post. + * @param WP_REST_Request $request The restore request. + * @param WP_REST_Request $parent_request Request to prepare the template with. + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. + */ + protected function prepare_restored_parent_for_response( $parent_id, $request, $parent_request ) { + $template_id = $request['parent']; + $template = is_string( $template_id ) ? get_block_template( $template_id, $this->parent_post_type ) : null; + + if ( ! $template ) { + return new WP_Error( + 'rest_post_invalid_parent', + __( 'Invalid template parent ID.' ), + array( 'status' => WP_Http::NOT_FOUND ) + ); + } + + return rest_ensure_response( $this->parent_controller->prepare_item_for_response( $template, $parent_request ) ); + } + /** * Prepares the item for the REST response. * diff --git a/tests/phpunit/tests/rest-api/rest-schema-setup.php b/tests/phpunit/tests/rest-api/rest-schema-setup.php index 76bdbc74eb2e8..0c26fa20b5cb2 100644 --- a/tests/phpunit/tests/rest-api/rest-schema-setup.php +++ b/tests/phpunit/tests/rest-api/rest-schema-setup.php @@ -93,6 +93,7 @@ public function test_expected_routes_in_schema() { '/wp/v2/posts/(?P[\\d]+)', '/wp/v2/posts/(?P[\\d]+)/revisions', '/wp/v2/posts/(?P[\\d]+)/revisions/(?P[\\d]+)', + '/wp/v2/posts/(?P[\\d]+)/revisions/(?P[\\d]+)/restore', '/wp/v2/posts/(?P[\\d]+)/autosaves', '/wp/v2/posts/(?P[\\d]+)/autosaves/(?P[\\d]+)', '/wp/v2/menu-items', @@ -107,6 +108,7 @@ public function test_expected_routes_in_schema() { '/wp/v2/pages/(?P[\\d]+)', '/wp/v2/pages/(?P[\\d]+)/revisions', '/wp/v2/pages/(?P[\\d]+)/revisions/(?P[\\d]+)', + '/wp/v2/pages/(?P[\\d]+)/revisions/(?P[\\d]+)/restore', '/wp/v2/pages/(?P[\\d]+)/autosaves', '/wp/v2/pages/(?P[\\d]+)/autosaves/(?P[\\d]+)', '/wp/v2/pattern-directory/patterns', @@ -122,6 +124,7 @@ public function test_expected_routes_in_schema() { '/wp/v2/blocks/(?P[\d]+)/autosaves/(?P[\d]+)', '/wp/v2/blocks/(?P[\d]+)/revisions', '/wp/v2/blocks/(?P[\d]+)/revisions/(?P[\d]+)', + '/wp/v2/blocks/(?P[\d]+)/revisions/(?P[\d]+)/restore', '/wp/v2/types', '/wp/v2/types/(?P[\\w-]+)', '/wp/v2/statuses', @@ -157,6 +160,7 @@ public function test_expected_routes_in_schema() { '/wp/v2/template-parts/(?P([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/autosaves/(?P[\d]+)', '/wp/v2/template-parts/(?P([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/revisions', '/wp/v2/template-parts/(?P([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/revisions/(?P[\d]+)', + '/wp/v2/template-parts/(?P([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/revisions/(?P[\d]+)/restore', '/wp/v2/template-parts/lookup', '/wp/v2/templates', '/wp/v2/templates/(?P([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)', @@ -164,6 +168,7 @@ public function test_expected_routes_in_schema() { '/wp/v2/templates/(?P([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/autosaves/(?P[\d]+)', '/wp/v2/templates/(?P([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/revisions', '/wp/v2/templates/(?P([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/revisions/(?P[\d]+)', + '/wp/v2/templates/(?P([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/revisions/(?P[\d]+)/restore', '/wp/v2/templates/lookup', '/wp/v2/themes', '/wp/v2/themes/(?P[^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)', @@ -186,6 +191,7 @@ public function test_expected_routes_in_schema() { '/wp/v2/navigation/(?P[\d]+)/autosaves/(?P[\d]+)', '/wp/v2/navigation/(?P[\d]+)/revisions', '/wp/v2/navigation/(?P[\d]+)/revisions/(?P[\d]+)', + '/wp/v2/navigation/(?P[\d]+)/revisions/(?P[\d]+)/restore', '/wp-site-health/v1', '/wp-site-health/v1/tests/background-updates', '/wp-site-health/v1/tests/loopback-requests', diff --git a/tests/phpunit/tests/rest-api/wpRestRevisionsControllerRestore.php b/tests/phpunit/tests/rest-api/wpRestRevisionsControllerRestore.php new file mode 100644 index 0000000000000..5965161f3c657 --- /dev/null +++ b/tests/phpunit/tests/rest-api/wpRestRevisionsControllerRestore.php @@ -0,0 +1,341 @@ + + */ + protected array $restore_calls = array(); + + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ): void { + $admin_id = $factory->user->create( array( 'role' => 'administrator' ) ); + $contributor_id = $factory->user->create( array( 'role' => 'contributor' ) ); + self::assertIsInt( $admin_id ); + self::assertIsInt( $contributor_id ); + + self::$admin_id = $admin_id; + self::$contributor_id = $contributor_id; + } + + public static function wpTearDownAfterClass(): void { + self::delete_user( self::$admin_id ); + self::delete_user( self::$contributor_id ); + } + + public function set_up(): void { + parent::set_up(); + + // Revisioned, and not shown in REST: the editor can't read or save it. + register_post_meta( + 'post', + 'test_hidden_revisioned_meta', + array( + 'type' => 'string', + 'single' => true, + 'revisions_enabled' => true, + ) + ); + + wp_set_current_user( self::$admin_id ); + + $this->post_id = $this->create_post( + array( + 'post_content' => 'Original content', + 'post_title' => 'Original title', + ) + ); + update_post_meta( $this->post_id, 'test_hidden_revisioned_meta', 'Original meta' ); + // The meta is copied to a revision when the post is saved again. + wp_update_post( + array( + 'ID' => $this->post_id, + 'post_content' => 'Original content', + ) + ); + + update_post_meta( $this->post_id, 'test_hidden_revisioned_meta', 'Updated meta' ); + wp_update_post( + array( + 'ID' => $this->post_id, + 'post_content' => 'Updated content', + 'post_title' => 'Updated title', + ) + ); + + $this->restore_calls = array(); + add_action( 'wp_restore_post_revision', array( $this, 'record_restore_call' ), 10, 2 ); + + // Routes are registered when the server is first built. + global $wp_rest_server; + $wp_rest_server = null; + } + + public function tear_down(): void { + unregister_post_meta( 'post', 'test_hidden_revisioned_meta' ); + + global $wp_rest_server; + $wp_rest_server = null; + + parent::tear_down(); + } + + /** + * Records a `wp_restore_post_revision` call. + * + * @param int $post_id Post ID. + * @param int $revision_id Post revision ID. + */ + public function record_restore_call( $post_id, $revision_id ): void { + $this->restore_calls[] = array( (int) $post_id, (int) $revision_id ); + } + + /** + * Creates a post by the administrator. + * + * @param array $args Post arguments. + * @return int The post ID. + */ + private function create_post( array $args = array() ): int { + $post_id = self::factory()->post->create( array_merge( array( 'post_author' => self::$admin_id ), $args ) ); + $this->assertIsInt( $post_id ); + + return $post_id; + } + + /** + * Returns the saved content of a post. + * + * @param int $post_id Post ID. + * @return string The post content. + */ + private function get_content( int $post_id ): string { + $post = get_post( $post_id ); + $this->assertInstanceOf( WP_Post::class, $post ); + + return $post->post_content; + } + + /** + * Returns a value from the data of a response. + * + * @param WP_REST_Response $response The response. + * @param string ...$path Keys leading to the value. + * @return mixed The value. + */ + private function get_response_value( WP_REST_Response $response, string ...$path ) { + $data = $response->get_data(); + $this->assertIsArray( $data ); + + return _wp_array_get( $data, $path ); + } + + /** + * Returns the revision holding the original content and meta. + * + * @return WP_Post The revision. + */ + private function get_original_revision(): WP_Post { + foreach ( wp_get_post_revisions( $this->post_id ) as $revision ) { + if ( + $revision instanceof WP_Post && + 'Original content' === $revision->post_content && + 'Original meta' === get_post_meta( $revision->ID, 'test_hidden_revisioned_meta', true ) + ) { + return $revision; + } + } + + self::fail( 'No revision holds the original content and meta.' ); + } + + /** + * Dispatches a request to restore a revision of a post. + * + * @param int $revision_id Revision ID. + * @param int|null $post_id Parent post ID. Defaults to the test post. + * @return WP_REST_Response The response. + */ + private function restore( int $revision_id, ?int $post_id = null ): WP_REST_Response { + $request = new WP_REST_Request( + 'POST', + '/wp/v2/posts/' . ( $post_id ?? $this->post_id ) . '/revisions/' . $revision_id . '/restore' + ); + + return rest_get_server()->dispatch( $request ); + } + + public function test_registers_the_route_for_post_types_with_revisions(): void { + $routes = rest_get_server()->get_routes(); + + $this->assertArrayHasKey( '/wp/v2/posts/(?P[\d]+)/revisions/(?P[\d]+)/restore', $routes ); + $this->assertArrayHasKey( '/wp/v2/pages/(?P[\d]+)/revisions/(?P[\d]+)/restore', $routes ); + } + + public function test_does_not_register_the_route_for_global_styles(): void { + $restore_routes = array_filter( + array_keys( rest_get_server()->get_routes() ), + static function ( string $route ): bool { + return str_ends_with( $route, '/restore' ) && str_contains( $route, 'global-styles' ); + } + ); + + $this->assertSame( array(), $restore_routes ); + } + + public function test_restores_post_fields_and_meta_not_shown_in_rest(): void { + $revision = $this->get_original_revision(); + + $response = $this->restore( $revision->ID ); + + $this->assertSame( 200, $response->get_status() ); + + $this->assertSame( 'Original content', $this->get_content( $this->post_id ) ); + $this->assertSame( 'Original title', get_the_title( $this->post_id ) ); + $this->assertSame( 'Original meta', get_post_meta( $this->post_id, 'test_hidden_revisioned_meta', true ) ); + } + + public function test_fires_the_restore_action_once(): void { + $revision = $this->get_original_revision(); + + $this->restore( $revision->ID ); + + $this->assertSame( array( array( $this->post_id, $revision->ID ) ), $this->restore_calls ); + } + + public function test_returns_the_restored_post_for_editing(): void { + $revision = $this->get_original_revision(); + + $response = $this->restore( $revision->ID ); + + $this->assertSame( $this->post_id, $this->get_response_value( $response, 'id' ) ); + $this->assertSame( 'Original title', $this->get_response_value( $response, 'title', 'raw' ) ); + $this->assertSame( 'Original content', $this->get_response_value( $response, 'content', 'raw' ) ); + } + + public function test_requires_permission_to_edit_the_post(): void { + $revision = $this->get_original_revision(); + + wp_set_current_user( self::$contributor_id ); + $response = $this->restore( $revision->ID ); + + $this->assertSame( 403, $response->get_status() ); + $this->assertSame( 'rest_cannot_restore_revision', $this->get_response_value( $response, 'code' ) ); + $this->assertSame( 'Updated content', $this->get_content( $this->post_id ) ); + $this->assertSame( array(), $this->restore_calls ); + } + + public function test_rejects_a_revision_of_another_post(): void { + $revision = $this->get_original_revision(); + $other_post = $this->create_post(); + + $response = $this->restore( $revision->ID, $other_post ); + + $this->assertSame( 404, $response->get_status() ); + $this->assertSame( 'rest_revision_parent_id_mismatch', $this->get_response_value( $response, 'code' ) ); + $this->assertSame( array(), $this->restore_calls ); + } + + public function test_rejects_an_unknown_revision(): void { + $response = $this->restore( 999999 ); + + $this->assertSame( 404, $response->get_status() ); + $this->assertSame( 'rest_post_invalid_id', $this->get_response_value( $response, 'code' ) ); + } + + public function test_rejects_a_post_that_is_not_a_revision(): void { + $other_post = $this->create_post( array( 'post_parent' => $this->post_id ) ); + + $response = $this->restore( $other_post ); + + $this->assertSame( 404, $response->get_status() ); + $this->assertSame( 'rest_post_invalid_id', $this->get_response_value( $response, 'code' ) ); + } + + public function test_rejects_a_parent_of_another_post_type(): void { + $page_id = $this->create_post( array( 'post_type' => 'page' ) ); + + $response = $this->restore( $this->get_original_revision()->ID, $page_id ); + + $this->assertSame( 404, $response->get_status() ); + $this->assertSame( 'rest_post_invalid_parent', $this->get_response_value( $response, 'code' ) ); + } + + public function test_does_not_restore_while_revisions_are_disabled(): void { + $revision = $this->get_original_revision(); + add_filter( 'wp_revisions_to_keep', '__return_zero' ); + + $response = $this->restore( $revision->ID ); + + $this->assertSame( 400, $response->get_status() ); + $this->assertSame( 'rest_revisions_disabled', $this->get_response_value( $response, 'code' ) ); + $this->assertSame( 'Updated content', $this->get_content( $this->post_id ) ); + } + + public function test_restores_a_template_revision(): void { + $original_content = '

Original template

'; + $template_post_id = $this->create_post( + array( + 'post_type' => 'wp_template', + 'post_name' => 'test-restore-revision', + 'post_title' => 'Restore test', + 'post_content' => $original_content, + 'post_status' => 'publish', + ) + ); + wp_set_post_terms( $template_post_id, get_stylesheet(), 'wp_theme' ); + // Store the original content as a revision, then change it. + wp_save_post_revision( $template_post_id ); + wp_update_post( + array( + 'ID' => $template_post_id, + 'post_content' => '

Updated template

', + ) + ); + $revisions = wp_get_post_revisions( $template_post_id, array( 'order' => 'ASC' ) ); + $original = reset( $revisions ); + $this->assertInstanceOf( WP_Post::class, $original ); + $this->assertSame( $original_content, $original->post_content ); + + $template_id = get_stylesheet() . '//test-restore-revision'; + $response = rest_get_server()->dispatch( + new WP_REST_Request( 'POST', '/wp/v2/templates/' . $template_id . '/revisions/' . $original->ID . '/restore' ) + ); + + $this->assertSame( 200, $response->get_status() ); + $this->assertSame( $template_id, $this->get_response_value( $response, 'id' ) ); + $this->assertSame( $original_content, $this->get_response_value( $response, 'content', 'raw' ) ); + $this->assertSame( $original_content, $this->get_content( $template_post_id ) ); + $this->assertSame( array( array( $template_post_id, $original->ID ) ), $this->restore_calls ); + } +} diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index ba63e6425b7fd..66f3951c19ca6 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -1351,6 +1351,31 @@ mockedApiResponse.Schema = { } ] }, + "/wp/v2/posts/(?P[\\d]+)/revisions/(?P[\\d]+)/restore": { + "namespace": "wp/v2", + "methods": [ + "POST" + ], + "endpoints": [ + { + "methods": [ + "POST" + ], + "args": { + "parent": { + "description": "The ID for the parent of the revision.", + "type": "integer", + "required": false + }, + "id": { + "description": "Unique identifier for the revision.", + "type": "integer", + "required": false + } + } + } + ] + }, "/wp/v2/posts/(?P[\\d]+)/autosaves": { "namespace": "wp/v2", "methods": [ @@ -2501,6 +2526,31 @@ mockedApiResponse.Schema = { } ] }, + "/wp/v2/pages/(?P[\\d]+)/revisions/(?P[\\d]+)/restore": { + "namespace": "wp/v2", + "methods": [ + "POST" + ], + "endpoints": [ + { + "methods": [ + "POST" + ], + "args": { + "parent": { + "description": "The ID for the parent of the revision.", + "type": "integer", + "required": false + }, + "id": { + "description": "Unique identifier for the revision.", + "type": "integer", + "required": false + } + } + } + ] + }, "/wp/v2/pages/(?P[\\d]+)/autosaves": { "namespace": "wp/v2", "methods": [ @@ -5443,6 +5493,31 @@ mockedApiResponse.Schema = { } ] }, + "/wp/v2/blocks/(?P[\\d]+)/revisions/(?P[\\d]+)/restore": { + "namespace": "wp/v2", + "methods": [ + "POST" + ], + "endpoints": [ + { + "methods": [ + "POST" + ], + "args": { + "parent": { + "description": "The ID for the parent of the revision.", + "type": "integer", + "required": false + }, + "id": { + "description": "Unique identifier for the revision.", + "type": "integer", + "required": false + } + } + } + ] + }, "/wp/v2/blocks/(?P[\\d]+)/autosaves": { "namespace": "wp/v2", "methods": [ @@ -5827,6 +5902,31 @@ mockedApiResponse.Schema = { } ] }, + "/wp/v2/templates/(?P([^\\/:<>\\*\\?\"\\|]+(?:\\/[^\\/:<>\\*\\?\"\\|]+)?)[\\/\\w%-]+)/revisions/(?P[\\d]+)/restore": { + "namespace": "wp/v2", + "methods": [ + "POST" + ], + "endpoints": [ + { + "methods": [ + "POST" + ], + "args": { + "parent": { + "description": "The id of a template", + "type": "string", + "required": false + }, + "id": { + "description": "Unique identifier for the revision.", + "type": "integer", + "required": false + } + } + } + ] + }, "/wp/v2/templates/(?P([^\\/:<>\\*\\?\"\\|]+(?:\\/[^\\/:<>\\*\\?\"\\|]+)?)[\\/\\w%-]+)/autosaves": { "namespace": "wp/v2", "methods": [ @@ -6511,6 +6611,31 @@ mockedApiResponse.Schema = { } ] }, + "/wp/v2/template-parts/(?P([^\\/:<>\\*\\?\"\\|]+(?:\\/[^\\/:<>\\*\\?\"\\|]+)?)[\\/\\w%-]+)/revisions/(?P[\\d]+)/restore": { + "namespace": "wp/v2", + "methods": [ + "POST" + ], + "endpoints": [ + { + "methods": [ + "POST" + ], + "args": { + "parent": { + "description": "The id of a template", + "type": "string", + "required": false + }, + "id": { + "description": "Unique identifier for the revision.", + "type": "integer", + "required": false + } + } + } + ] + }, "/wp/v2/template-parts/(?P([^\\/:<>\\*\\?\"\\|]+(?:\\/[^\\/:<>\\*\\?\"\\|]+)?)[\\/\\w%-]+)/autosaves": { "namespace": "wp/v2", "methods": [ @@ -7928,6 +8053,31 @@ mockedApiResponse.Schema = { } ] }, + "/wp/v2/navigation/(?P[\\d]+)/revisions/(?P[\\d]+)/restore": { + "namespace": "wp/v2", + "methods": [ + "POST" + ], + "endpoints": [ + { + "methods": [ + "POST" + ], + "args": { + "parent": { + "description": "The ID for the parent of the revision.", + "type": "integer", + "required": false + }, + "id": { + "description": "Unique identifier for the revision.", + "type": "integer", + "required": false + } + } + } + ] + }, "/wp/v2/navigation/(?P[\\d]+)/autosaves": { "namespace": "wp/v2", "methods": [ From d5a9f46e639b4e8203aa37cf733854fb002ae2fc Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Sat, 28 Nov 2026 16:26:24 -0800 Subject: [PATCH 2/2] REST API: Check the type of the IDs a restore request passes on. A request value is untyped, and a template is addressed by a string ID where other post types use an integer. Read the parent through a method that each revisions controller implements for its own ID type, so that the restore route passes PHPStan at level 10. See #66865. --- .../class-wp-rest-revisions-controller.php | 21 ++++++++++++++++--- ...-wp-rest-template-revisions-controller.php | 14 +++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php index f4ca5d6ca269e..c0e37329227be 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php @@ -584,7 +584,7 @@ public function delete_item( $request ) { * @return true|WP_Error True if the request has access to restore the revision, WP_Error object otherwise. */ public function restore_item_permissions_check( $request ) { - $parent = $this->get_parent( $request['parent'] ); + $parent = $this->get_parent_for_restore( $request ); if ( is_wp_error( $parent ) ) { return $parent; } @@ -613,12 +613,13 @@ public function restore_item_permissions_check( $request ) { * @return WP_REST_Response|WP_Error The parent as its own endpoint returns it for editing, or WP_Error object on failure. */ public function restore_item( $request ) { - $parent = $this->get_parent( $request['parent'] ); + $parent = $this->get_parent_for_restore( $request ); if ( is_wp_error( $parent ) ) { return $parent; } - $revision = $this->get_revision( $request['id'] ); + $revision_id = $request['id']; + $revision = $this->get_revision( is_numeric( $revision_id ) ? (int) $revision_id : 0 ); if ( is_wp_error( $revision ) ) { return $revision; } @@ -655,6 +656,20 @@ public function restore_item( $request ) { return $this->prepare_restored_parent_for_response( $parent->ID, $request, $parent_request ); } + /** + * Gets the parent post of the revision that a request restores. + * + * @since 7.3.0 + * + * @param WP_REST_Request $request Full details about the request. + * @return WP_Post|WP_Error Post object if the parent ID is valid, WP_Error otherwise. + */ + protected function get_parent_for_restore( $request ) { + $parent_id = $request['parent']; + + return $this->get_parent( is_numeric( $parent_id ) ? (int) $parent_id : 0 ); + } + /** * Prepares the parent of a restored revision as its own endpoint returns it. * diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-template-revisions-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-template-revisions-controller.php index 696a2bde7df4e..6bc98e87a3de8 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-template-revisions-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-template-revisions-controller.php @@ -222,6 +222,20 @@ protected function get_parent( $parent_template_id ) { return get_post( $template->wp_id ); } + /** + * Gets the parent post of the template revision that a request restores. + * + * @since 7.3.0 + * + * @param WP_REST_Request $request Full details about the request. + * @return WP_Post|WP_Error Post object if the template ID is valid, WP_Error otherwise. + */ + protected function get_parent_for_restore( $request ) { + $template_id = $request['parent']; + + return $this->get_parent( is_string( $template_id ) ? $template_id : '' ); + } + /** * Prepares the template of a restored revision as its own endpoint returns it. *