Skip to content

Commit 0c8d8da

Browse files
REST API: only include revisioned meta in revision responses.
Fix an issue where restoring a revision in the editor overwrote meta values that weren't associated with the revision. Visual Revisions in the block editor, introduced in 7.0, restores a revision by saving the `meta` returned from the revisions endpoint onto the post. That endpoint returned every `show_in_rest` meta key for the parent post type, but only revisioned keys are stored on a revision, so the rest came back as their default (or `null`) and the restore overwrote the post's real values. Unsynced patterns silently became synced, synced patterns failed to restore with a `rest_invalid_stored_value` error, and any other non-revisioned `show_in_rest` key was reset. Introduce `WP_REST_Revision_Meta_Fields`, used by `WP_REST_Revisions_Controller`, which limits a revision's `meta` and its schema to the keys from `wp_post_revision_meta_keys()`. This matches what `wp_restore_post_revision()` restores from the classic revisions screen, and requires no editor change. Autosave responses follow the same rules, since they are prepared by the revisions controller; autosave requests still accept all of the parent post type's meta. Plugins that write meta onto revisions directly should add those keys with the `wp_post_revision_meta_keys` filter to keep them in responses. Also fixes WordPress/gutenberg#84809. Props dlocc, davidsword, talldanwp, rachelbaker, prasadkarmalkar, umeshnevase. Fixes #66329. git-svn-id: https://develop.svn.wordpress.org/trunk@64731 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 9c9e35e commit 0c8d8da

6 files changed

Lines changed: 532 additions & 31 deletions

File tree

‎src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class WP_REST_Revisions_Controller extends WP_REST_Controller {
2828
* Instance of a revision meta fields object.
2929
*
3030
* @since 6.4.0
31+
* @since 7.2.0 Now a WP_REST_Revision_Meta_Fields instance, which only exposes revisioned meta keys.
3132
* @var WP_REST_Post_Meta_Fields
3233
*/
3334
protected $meta;
@@ -68,7 +69,7 @@ public function __construct( $parent_post_type ) {
6869
$this->rest_base = 'revisions';
6970
$this->parent_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
7071
$this->namespace = ! empty( $post_type_object->rest_namespace ) ? $post_type_object->rest_namespace : 'wp/v2';
71-
$this->meta = new WP_REST_Post_Meta_Fields( $parent_post_type );
72+
$this->meta = new WP_REST_Revision_Meta_Fields( $parent_post_type );
7273
}
7374

7475
/**
@@ -596,6 +597,7 @@ protected function prepare_items_query( $prepared_args = array(), $request = nul
596597
* @since 4.7.0
597598
* @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support.
598599
* @since 7.1.0 The global post is now restored to its previous value before returning.
600+
* @since 7.2.0 The `meta` field only includes the meta keys that are revisioned for the parent post type.
599601
*
600602
* @global WP_Post|null $post Global post object.
601603
*
@@ -813,6 +815,7 @@ protected function prepare_date_response( $date_gmt, $date = null ) {
813815
* Retrieves the revision's schema, conforming to JSON Schema.
814816
*
815817
* @since 4.7.0
818+
* @since 7.2.0 The `meta` property only describes the meta keys that are revisioned for the parent post type.
816819
*
817820
* @return array Item schema data.
818821
*/
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* REST API: WP_REST_Revision_Meta_Fields class
4+
*
5+
* @package WordPress
6+
* @subpackage REST_API
7+
* @since 7.2.0
8+
*/
9+
10+
/**
11+
* Core class used to read the meta values of post revisions via the REST API.
12+
*
13+
* Only the meta keys that are revisioned for the parent post type are exposed,
14+
* because those are the only values that are stored on a revision. Any other
15+
* meta key registered for the post type would be read from the revision ID,
16+
* find nothing, and be reported with its default value instead.
17+
*
18+
* @since 7.2.0
19+
*
20+
* @see WP_REST_Post_Meta_Fields
21+
* @see wp_post_revision_meta_keys()
22+
*/
23+
class WP_REST_Revision_Meta_Fields extends WP_REST_Post_Meta_Fields {
24+
25+
/**
26+
* Retrieves the meta field values for a revision.
27+
*
28+
* When an author autosaves their own draft, the autosaves controller updates
29+
* the post itself and returns it instead of an autosave. That post stores all
30+
* of its meta, so its values are read as regular post meta.
31+
*
32+
* @since 7.2.0
33+
*
34+
* @param int $object_id Object ID to fetch meta for.
35+
* @param WP_REST_Request $request Full details about the request.
36+
* @return array Array containing the meta values keyed by name.
37+
*/
38+
public function get_value( $object_id, $request ) {
39+
if ( 'revision' !== get_post_type( $object_id ) ) {
40+
$post_meta = new WP_REST_Post_Meta_Fields( $this->post_type );
41+
return $post_meta->get_value( $object_id, $request );
42+
}
43+
44+
return parent::get_value( $object_id, $request );
45+
}
46+
47+
/**
48+
* Retrieves the registered meta fields that are revisioned for the parent post type.
49+
*
50+
* @since 7.2.0
51+
*
52+
* @return array Registered fields, limited to the revisioned meta keys.
53+
*/
54+
protected function get_registered_fields() {
55+
// The keys are filterable, so ignore anything that isn't a meta key.
56+
$revisioned_keys = array_filter( (array) wp_post_revision_meta_keys( $this->post_type ), 'is_string' );
57+
58+
return array_intersect_key(
59+
parent::get_registered_fields(),
60+
array_flip( $revisioned_keys )
61+
);
62+
}
63+
}

‎src/wp-settings.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@
398398
require ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-meta-fields.php';
399399
require ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-comment-meta-fields.php';
400400
require ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-post-meta-fields.php';
401+
require ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-revision-meta-fields.php';
401402
require ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-term-meta-fields.php';
402403
require ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-user-meta-fields.php';
403404
require ABSPATH . WPINC . '/rest-api/search/class-wp-rest-search-handler.php';

‎tests/phpunit/tests/rest-api/rest-autosaves-controller.php‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,98 @@ public function test_update_item_with_meta() {
492492
$this->assertSame( 'bar', $data['meta']['foo'] );
493493
}
494494

495+
/**
496+
* The autosave's meta only includes the meta keys that are revisioned,
497+
* since those are the only meta values stored on an autosave.
498+
*
499+
* @ticket 66329
500+
*
501+
* @covers WP_REST_Autosaves_Controller::prepare_item_for_response
502+
* @covers WP_REST_Revision_Meta_Fields::get_registered_fields
503+
*/
504+
public function test_update_item_with_meta_only_includes_revisioned_meta_keys() {
505+
wp_set_current_user( self::$editor_id );
506+
$request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . self::$post_id . '/autosaves' );
507+
$request->add_header( 'Content-Type', 'application/x-www-form-urlencoded' );
508+
register_post_meta(
509+
'post',
510+
'foo',
511+
array(
512+
'show_in_rest' => true,
513+
'revisions_enabled' => true,
514+
'single' => true,
515+
)
516+
);
517+
register_post_meta(
518+
'post',
519+
'not_revisioned',
520+
array(
521+
'show_in_rest' => true,
522+
'single' => true,
523+
)
524+
);
525+
$params = $this->set_post_data(
526+
array(
527+
'id' => self::$post_id,
528+
'author' => self::$contributor_id,
529+
'meta' => array(
530+
'foo' => 'bar',
531+
'not_revisioned' => 'baz',
532+
),
533+
)
534+
);
535+
536+
$request->set_body_params( $params );
537+
$response = rest_get_server()->dispatch( $request );
538+
539+
$this->check_create_autosave_response( $response );
540+
541+
$data = $response->get_data();
542+
$this->assertSame( 'bar', $data['meta']['foo'] );
543+
$this->assertArrayNotHasKey( 'not_revisioned', $data['meta'] );
544+
}
545+
546+
/**
547+
* Autosaving your own draft updates the post itself instead of creating an
548+
* autosave, so the response includes all of the post's meta.
549+
*
550+
* @ticket 66329
551+
*
552+
* @covers WP_REST_Autosaves_Controller::create_item
553+
* @covers WP_REST_Revision_Meta_Fields::get_value
554+
*/
555+
public function test_create_item_for_own_draft_includes_meta_that_is_not_revisioned() {
556+
register_post_meta(
557+
'post',
558+
'not_revisioned',
559+
array(
560+
'show_in_rest' => true,
561+
'single' => true,
562+
'type' => 'string',
563+
)
564+
);
565+
566+
$draft_id = self::factory()->post->create(
567+
array(
568+
'post_status' => 'draft',
569+
'post_author' => self::$editor_id,
570+
)
571+
);
572+
update_post_meta( $draft_id, 'not_revisioned', 'kept value' );
573+
574+
wp_set_current_user( self::$editor_id );
575+
$request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $draft_id . '/autosaves' );
576+
$request->add_header( 'Content-Type', 'application/x-www-form-urlencoded' );
577+
$request->set_body_params( array( 'content' => 'Autosaved content.' ) );
578+
$response = rest_get_server()->dispatch( $request );
579+
$data = $response->get_data();
580+
581+
$this->assertSame( 200, $response->get_status() );
582+
$this->assertSame( $draft_id, $data['id'], 'Autosaving your own draft should update the post itself.' );
583+
$this->assertArrayHasKey( 'not_revisioned', $data['meta'], 'The post itself should include meta that is not revisioned.' );
584+
$this->assertSame( 'kept value', $data['meta']['not_revisioned'] );
585+
}
586+
495587
public function test_update_item_with_json_meta() {
496588
$meta = '[{\"content\":\"foot 1\",\"id\":\"fa97a10d-7401-42b9-ac54-df8f4510749a\"},{\"content\":\"fdddddoot 2\\\"\",\"id\":\"2216d0aa-34b8-42b4-b441-84dedc0406e0\"}]';
497589
wp_set_current_user( self::$editor_id );

0 commit comments

Comments
 (0)