Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add revisioning of post meta, including ‘footnotes’ by default #4859

Closed
wants to merge 117 commits into from
Closed
Show file tree
Hide file tree
Changes from 56 commits
Commits
Show all changes
117 commits
Select commit Hold shift + click to select a range
b8bf5ef
Add revisioning of post meta, including ‘footnotes’ by default
adamsilverstein Jul 17, 2023
6667610
correct typo in `src/wp-includes/default-filters.php`
adamsilverstein Jul 17, 2023
15e9e4e
add tests for meta revisioning
adamsilverstein Jul 17, 2023
b892323
Try: revisioning in the rest endpoint directly, after meta is saved
adamsilverstein Jul 17, 2023
6235af5
Add _wp_save_post_revision_meta helper that copies revisioned meta to…
adamsilverstein Jul 18, 2023
3a87749
remove direct REST API integration, move save to wp_after_insert_post
adamsilverstein Jul 18, 2023
05d0f86
Clean up revisions save
adamsilverstein Jul 18, 2023
19e1f8a
phpcbf
adamsilverstein Jul 18, 2023
0b86da9
Ensure `$revisions[0]->post_id ` is set before using
adamsilverstein Jul 18, 2023
52dd943
Test work
adamsilverstein Jul 18, 2023
ef14c11
Expected revision count is 6
adamsilverstein Jul 19, 2023
31a6d61
Merge branch 'trunk' into ticket/20564
adamsilverstein Jul 21, 2023
da8abe6
Add REST API tests for revisioning of post meta.
adamsilverstein Jul 25, 2023
d2f0db1
Merge branch 'trunk' into ticket/20564
adamsilverstein Jul 25, 2023
6c9bece
Remove footnote specific revisioning of meta
adamsilverstein Jul 25, 2023
2e74964
phpcbf
adamsilverstein Jul 25, 2023
268385f
Fix test setup
adamsilverstein Jul 25, 2023
4fd4302
Save revisioned meta in `_wp_put_post_revision`
adamsilverstein Jul 25, 2023
3722d1e
Enable setting revisioning in register_meta
adamsilverstein Jul 25, 2023
b26a695
phpcs
adamsilverstein Jul 26, 2023
5d6e5b9
remove unused $revisioned_keys
adamsilverstein Jul 26, 2023
b4fa33f
Add testing for enabling revisions in `register_meta`
adamsilverstein Jul 26, 2023
1603c55
phpcbf
adamsilverstein Jul 26, 2023
d244a39
Use a separate global for revisioned keys `$wp_revisioned_meta_keys`
adamsilverstein Jul 26, 2023
d86ec77
Merge branch 'trunk' into ticket/20564
adamsilverstein Jul 26, 2023
a80620a
Meta test cleanup to match new defaults
adamsilverstein Jul 26, 2023
885640d
Adjust expected for `test_should_register_persisted_preferences_meta`
adamsilverstein Jul 26, 2023
ea4f181
Refine REST tests for meta revisioning
adamsilverstein Jul 26, 2023
e63334b
Avoid duplicate keys in `wp_revisioned_meta_keys`
adamsilverstein Jul 26, 2023
f4234f8
remove unused `original_post_id`
adamsilverstein Jul 26, 2023
1f1e5a0
REST posts endpoint: update revision meta when saving meta
adamsilverstein Jul 26, 2023
6c5061b
test work
adamsilverstein Jul 26, 2023
5bd7e7b
Hook `wp_save_revisioned_meta_fields` on `_wp_put_post_revision` so i…
adamsilverstein Jul 27, 2023
65bfe81
Test cleanup
adamsilverstein Jul 27, 2023
7254e3a
Improve footnote display, show number and content
adamsilverstein Jul 27, 2023
8c3fba2
Save the revisioned meta directly in the REST endpoint
adamsilverstein Jul 27, 2023
26a9d0b
spacing
adamsilverstein Jul 27, 2023
210573b
Merge branch 'trunk' into ticket/20564
adamsilverstein Jul 27, 2023
485f20b
add meta to revisions endpoint
adamsilverstein Jul 31, 2023
167261e
test revisions endpoint returns meta
adamsilverstein Jul 31, 2023
5b5dc0d
new revision meta fields class
adamsilverstein Jul 31, 2023
f8d0d30
INCLUDE class-wp-rest-revision-meta-fields
adamsilverstein Aug 1, 2023
ea9b8ce
Revision existing meta even when update/insert lacks meta
adamsilverstein Aug 1, 2023
15e8bbd
Test that the revisions endpoint includes meta
adamsilverstein Aug 1, 2023
1e54aa3
Merge branch 'trunk' into ticket/20564
adamsilverstein Aug 1, 2023
a3fa561
phpcbf
adamsilverstein Aug 1, 2023
877420e
Merge branch 'trunk' into ticket/20564
adamsilverstein Aug 17, 2023
f57b79b
Update REST tests to reflect changed schema
adamsilverstein Aug 17, 2023
cad9272
phpcbf
adamsilverstein Aug 18, 2023
57c872a
Improve tests
adamsilverstein Aug 18, 2023
ca01058
Finetune tests based on git action failures
adamsilverstein Aug 18, 2023
92590aa
test
adamsilverstein Aug 18, 2023
6fd9a9e
restore footnotes.php from trunk
adamsilverstein Aug 24, 2023
f83d353
Merge branch 'trunk' into ticket/20564
adamsilverstein Aug 24, 2023
7f70990
Update wp-api-generated fixtures
adamsilverstein Aug 24, 2023
432a810
fix generated
adamsilverstein Aug 24, 2023
d80b7b4
Try: move hooking of revisions
adamsilverstein Aug 24, 2023
7160abb
fire after hooks in revisions controller test
adamsilverstein Aug 24, 2023
96301da
Try only adding revision on update.
TimothyBJacobs Aug 24, 2023
e2d0cc7
Use actions properly
TimothyBJacobs Aug 24, 2023
5f07145
Revert "Try only adding revision on update."
TimothyBJacobs Aug 24, 2023
afc1109
Don't rewrite everything
TimothyBJacobs Aug 24, 2023
ac279e3
Correct logic
TimothyBJacobs Aug 24, 2023
4205d65
Check if the legacy hook is being used
TimothyBJacobs Aug 24, 2023
e7b4495
Separate single and multi meta tests. Use native meta fields handler.
TimothyBJacobs Aug 24, 2023
68f6410
Regenerate fixtures
TimothyBJacobs Aug 24, 2023
ee35085
Use post meta fields directly
TimothyBJacobs Aug 24, 2023
61bfafc
Respect the object subtype.
TimothyBJacobs Aug 24, 2023
c27258a
Always pass a post array
TimothyBJacobs Aug 24, 2023
20874e3
Remove manually revisioning meta. This happens automatically now.
TimothyBJacobs Aug 24, 2023
68a419b
Correct has meta key assertion
TimothyBJacobs Aug 24, 2023
3ee633f
Add clarifying comment and some more fail safes
TimothyBJacobs Aug 24, 2023
1e17f5b
Merge branch 'trunk' into ticket/20564
adamsilverstein Aug 27, 2023
982c553
Merge branch 'trunk' into ticket/20564
adamsilverstein Aug 28, 2023
d04574a
Test cleanup, add page and cpt tests
adamsilverstein Aug 29, 2023
3a8f6fc
omyac
adamsilverstein Aug 29, 2023
192e7c0
Merge branch 'trunk' into ticket/20564
adamsilverstein Sep 8, 2023
225a91b
Clean up default filters after trunk merge
adamsilverstein Sep 8, 2023
c2482fd
Avoid duplicate revisions since hook change
adamsilverstein Sep 9, 2023
2331d36
Merge branch 'trunk' into ticket/20564
adamsilverstein Sep 11, 2023
5c48c7a
Restore hooks on wp_insert_post for revision - issue was plugin related
adamsilverstein Sep 11, 2023
e211ca1
Ensure meta data is revisioned with autosaves
adamsilverstein Sep 12, 2023
7247e93
Cleanup, only check meta when passed
adamsilverstein Sep 12, 2023
891a0e4
Merge branch 'trunk' into ticket/20564
adamsilverstein Sep 19, 2023
c35cc5f
Merge branch 'trunk' into ticket/20564
adamsilverstein Sep 22, 2023
826bebb
Merge branch 'trunk' into ticket/20564
adamsilverstein Sep 25, 2023
750dcdc
Hook wp_autosave_post_revisioned_meta_fields on wp_creating_autosave
adamsilverstein Sep 25, 2023
0b88e50
Improve error checking
adamsilverstein Sep 25, 2023
2c02dbc
Update src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-con…
adamsilverstein Sep 25, 2023
b5ba2d3
Update src/wp-includes/revision.php
adamsilverstein Sep 25, 2023
95508d9
Hook revision restore on action
adamsilverstein Sep 25, 2023
b0b880b
Test revisioning of meta with a default value
adamsilverstein Sep 25, 2023
f04a319
phpcs
adamsilverstein Sep 25, 2023
e231886
phpcbf
adamsilverstein Sep 25, 2023
e1950b0
Autosaves controller - get_metadata_raw is used to avoid retrieving t…
adamsilverstein Sep 25, 2023
6f6e3ce
Update src/wp-includes/revision.php
adamsilverstein Sep 25, 2023
b77bf22
Check post_type available in wp_save_revisioned_meta_fields
adamsilverstein Sep 25, 2023
b6e221a
Ensure post_type available in wp_restore_post_revision_meta
adamsilverstein Sep 25, 2023
40febf2
typo - missing closing parenthesis
adamsilverstein Sep 25, 2023
69c6107
phpcbf
adamsilverstein Sep 25, 2023
f9b760b
If the object_type is set, require a type that supports revisions wh…
adamsilverstein Sep 25, 2023
8360faa
Test enabling revisions for meta when object type doesn’t support rev…
adamsilverstein Sep 25, 2023
a1971f7
phpcbf
adamsilverstein Sep 25, 2023
99b2aa8
Update src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-con…
adamsilverstein Sep 26, 2023
b94e1a6
Update src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-con…
adamsilverstein Sep 26, 2023
e09f011
Update src/wp-admin/includes/post.php
adamsilverstein Sep 26, 2023
01a4788
Update src/wp-admin/includes/post.php
adamsilverstein Sep 26, 2023
8e7b72a
spacing
adamsilverstein Sep 26, 2023
d6c6ca2
Guard against _wp_put_post_revision returning 0
adamsilverstein Sep 26, 2023
73f6a23
Update src/wp-includes/meta.php
adamsilverstein Sep 26, 2023
3db644c
tabs vs spaces
adamsilverstein Sep 26, 2023
95c9e9d
Update src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-con…
adamsilverstein Sep 26, 2023
9bab9a9
Improve error message
adamsilverstein Sep 26, 2023
dc0b907
Merge branch 'trunk' into ticket/20564
adamsilverstein Sep 26, 2023
ab2d500
Merge branch 'trunk' into ticket/20564
adamsilverstein Sep 26, 2023
8bf1727
phpcbf
adamsilverstein Sep 26, 2023
c7904ce
phpcbf pt 2
adamsilverstein Sep 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions src/wp-admin/includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -1961,15 +1961,73 @@ function wp_create_post_autosave( $post_data ) {
* @param array $new_autosave Post array - the autosave that is about to be saved.
*/
do_action( 'wp_creating_autosave', $new_autosave );

wp_autosave_post_revisioned_meta_fields( $new_autosave );
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved
return wp_update_post( $new_autosave );
}

// _wp_put_post_revision() expects unescaped.
$post_data = wp_unslash( $post_data );

// Otherwise create the new autosave as a special post revision.
return _wp_put_post_revision( $post_data, true );
$revision = _wp_put_post_revision( $post_data, true );
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved

// Update the revisioned meta data as well.
wp_autosave_post_revisioned_meta_fields( $revision );
return $revision;
}

/**
* Autosave the revisioned meta fields.
*
* Iterates thru the revisioned meta fields and checks each to see if they are set,
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved
* and have a changed value. If so, the meta value is saved and attached to the autosave.
*
* @since 6.4.0
*
* @param Post object $new_autosave The new post being autosaved.
*/
function wp_autosave_post_revisioned_meta_fields( $new_autosave ) {
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
/*
* The post data arrives as either $_POST['data']['wp_autosave'] or the $_POST
* itself. This sets $posted_data to the correct variable.
*
* Ignoring sanitization to avoid altering meta. Ignoring the nonce check because
* this is hooked on inner core hooks where a valid nonce was already checked.
*
* @phpcs:disable WordPress.Security
*/
$posted_data = isset( $_POST['data']['wp_autosave'] ) ? $_POST['data']['wp_autosave'] : $_POST;
// phpcs:enable

/*
* Go thru the revisioned meta keys and save them as part of the autosave, if
* the meta key is part of the posted data, the meta value is not blank and
* the the meta value has changes from the last autosaved value.
*/
foreach ( wp_post_revision_meta_keys() as $meta_key ) {

if (
isset( $posted_data[ $meta_key ] ) &&
get_post_meta( $new_autosave['ID'], $meta_key, true ) !== wp_unslash( $posted_data[ $meta_key ] )
) {
/*
* Use the underlying delete_metadata() and add_metadata() functions
* vs delete_post_meta() and add_post_meta() to make sure we're working
* with the actual revision meta.
*/
delete_metadata( 'post', $new_autosave['ID'], $meta_key );

/*
* One last check to ensure meta value not empty().
*/
if ( ! empty( $posted_data[ $meta_key ] ) ) {
/*
* Add the revisions meta data to the autosave.
*/
add_metadata( 'post', $new_autosave['ID'], $meta_key, $posted_data[ $meta_key ] );
}
}
}
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -719,4 +719,10 @@
// CPT wp_block custom postmeta field.
add_action( 'init', 'wp_create_initial_post_meta' );

// Including revisioned meta when considering whether a post revision has changed.
add_filter( 'wp_save_post_revision_post_has_changed', 'wp_check_revisioned_meta_fields_have_changed', 10, 3 );

// Save revisioned post meta immediately after a revision is saved
add_action( '_wp_put_post_revision', 'wp_save_revisioned_meta_fields', 10, 2 );

unset( $filter, $action );
13 changes: 13 additions & 0 deletions src/wp-includes/meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,7 @@ function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype =
* @since 4.9.8 The `$object_subtype` argument was added to the arguments array.
* @since 5.3.0 Valid meta types expanded to include "array" and "object".
* @since 5.5.0 The `$default` argument was added to the arguments array.
* @since 6.4.0 The `$revisions_enabled` argument was added to the arguments array.
*
* @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
Expand All @@ -1392,6 +1393,7 @@ function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype =
* support for custom fields for registered meta to be accessible via REST.
* When registering complex meta values this argument may optionally be an
* array with 'schema' or 'prepare_callback' keys instead of a boolean.
* @type bool $revisions_enabled Whether to enable revisions support for this meta_key.
* }
* @param string|array $deprecated Deprecated. Use `$args` instead.
* @return bool True if the meta key was successfully registered in the global array, false if not.
Expand All @@ -1400,11 +1402,16 @@ function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype =
*/
function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
global $wp_meta_keys;
global $wp_revisioned_meta_keys;

if ( ! is_array( $wp_meta_keys ) ) {
$wp_meta_keys = array();
}

if ( ! is_array( $wp_revisioned_meta_keys ) ) {
$wp_revisioned_meta_keys = array();
}

$defaults = array(
'object_subtype' => '',
'type' => 'string',
Expand All @@ -1414,6 +1421,7 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
'sanitize_callback' => null,
'auth_callback' => null,
'show_in_rest' => false,
'revisions_enabled' => false,
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved
);

// There used to be individual args for sanitize and auth callbacks.
Expand Down Expand Up @@ -1459,6 +1467,11 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
}
}

// Store the revisioned meta fields.
if ( isset( $args['revisions_enabled'] ) && $args['revisions_enabled'] && ! in_array( $meta_key, $wp_revisioned_meta_keys ) ) {
array_push( $wp_revisioned_meta_keys, $meta_key );
}

$object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : '';

// If `auth_callback` is not provided, fall back to `is_protected_meta()`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,9 @@ public function create_item( $request ) {
);
}

// Don't save the post meta revision because they aren't stored yet.
remove_action( '_wp_put_post_revision', 'wp_save_revisioned_meta_fields', 10, 2 );
Copy link
Member

@ellatrix ellatrix Jul 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I like this. The root of the problem is that the order of filters and actions in this REST API request is wrong. wp_insert_post and wp_update_post fire the post_updated action after meta is updated (through meta_input). The REST API should do the same. Why does the REST API only fix the wp_after_insert_post to fire after meta is updated? It should do the same for post_updated and all those other filters.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know who else has recently worked on these endpoints. Maybe @TimothyBJacobs, @kadamwhite?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also not sure why the REST API decides not to use meta_input. Probably because it has limitation, but then those limitations should be addressed? Ideally everything goes through one code path. Since the REST API updates meta by itself, it had to move the wp_after_insert_post action, and will now have to do the same for all other actions.

So either we have to move those other actions, or use meta_input and address whatever the reasons were for not using that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The meta_input parameter is poorly designed for doing anything of complexity. It can't handle multi meta and support can't easily be added in a BC manner. It doesn't handle deleting meta. It doesn't provide any error handling.

Perhaps the REST API could've been architected differently, but that ship has long since sailed.

The REST API didn't move that hook, we introduced that hook specifically for the REST API. Core should be moving appropriate actions to that hook.

Copy link
Member

@ellatrix ellatrix Jul 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not because something has been poorly designed that it can't be improved? Is there something fundamentally unfixable about meta_input?

Core could move actions to that hook, but that ship has also sailed? We'd need to make sure plugins removing these hooks are still removing the moved hooks somehow.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ellatrix I also agree this solution isn't fantastic, it is a bit of a compromise approach. The one part I do like is hooking wp_save_revisioned_meta_fields on an action which lets any plugin easily disable the feature entirely.

I don't quite understand the part about meta_input. Are you saying the REST endpoint could populate that before updating the post? What would be a more ideal solution in your mind?

I am open to considering changing this - of course we would need to be careful not to break any cases where plugins already expect the current behavior. Also, this change could easily be done after this PR lands, so I don't feel it should be a blocker here. We would still hook the revisioning as is, we would just remove the special unhooking/re-hooking logic in the posts controller if the order was fixed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand the part about meta_input. Are you saying the REST endpoint could populate that before updating the post? What would be a more ideal solution in your mind?

What I mean is that there's a meta property that can be passed to the post data on the posts endpoint, which is essentially the same as meta_input that can be passed to the post data for wp_insert_post and wp_update_post. So there's great potential for symmetry here. I don't get the argument of it being poorly designed for complex things. The input for both should be almost the same: a keys to values array. So you're telling me that whatever handling the REST API is doing for meta, cannot be done inside of wp_update_post? Why can't we add deletion and error handling? If we do that, the action order will be correct and all will be well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you're telling me that whatever handling the REST API is doing for meta, cannot be done inside of wp_update_post? Why can't we add deletion and error handling? If we do that, the action order will be correct and all will be well?

Hmmm good point! I will test out making the change you are suggesting, I'm not sure why that wouldn't be possible maybe I am missing something and @TimothyBJacobs can clarify. I do see that there are other changes also completed after wp_update_post, but I don't see why we can't just include the meta in wp_update_post as meta_input and - as you say - all will be well.


$post_id = wp_insert_post( wp_slash( (array) $prepared_post ), true, false );

if ( is_wp_error( $post_id ) ) {
Expand Down Expand Up @@ -743,6 +746,13 @@ public function create_item( $request ) {
}
}

// Update any revisioned meta.
$revisions = wp_get_post_revisions( $post_id, array( 'posts_per_page' => 1 ) );
if ( ! empty( $revisions ) ) {
$revision = array_shift( $revisions );
wp_save_revisioned_meta_fields( $revision->ID, $post_id );
}

$post = get_post( $post_id );
$fields_update = $this->update_additional_fields_for_object( $post, $request );

Expand Down Expand Up @@ -876,6 +886,9 @@ public function update_item( $request ) {
);
}

// Don't save the post meta revision because they aren't stored yet.
remove_action( '_wp_put_post_revision', 'wp_save_revisioned_meta_fields', 10, 2 );

// Convert the post object to an array, otherwise wp_update_post() will expect non-escaped input.
$post_id = wp_update_post( wp_slash( (array) $post ), true, false );

Expand Down Expand Up @@ -928,6 +941,12 @@ public function update_item( $request ) {
return $meta_update;
}
}
// Update any revisioned meta.
$revisions = wp_get_post_revisions( $post_id, array( 'posts_per_page' => 1 ) );
if ( ! empty( $revisions ) ) {
$revision = array_shift( $revisions );
wp_save_revisioned_meta_fields( $revision->ID, $post_id );
}

$post = get_post( $post_id );
$fields_update = $this->update_additional_fields_for_object( $post, $request );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ class WP_REST_Revisions_Controller extends WP_REST_Controller {
*/
private $parent_post_type;

/**
* Instance of a revision meta fields object.
*
* @since 4.7.0
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved
* @var WP_REST_Revision_Meta_Fields
*/
protected $meta;

/**
* Parent controller.
*
Expand Down Expand Up @@ -60,6 +68,7 @@ public function __construct( $parent_post_type ) {
$this->rest_base = 'revisions';
$this->parent_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
$this->namespace = ! empty( $post_type_object->rest_namespace ) ? $post_type_object->rest_namespace : 'wp/v2';
$this->meta = new WP_REST_Revision_Meta_Fields( 'revision' );
}

/**
Expand Down Expand Up @@ -619,6 +628,10 @@ public function prepare_item_for_response( $item, $request ) {
);
}

if ( rest_is_field_included( 'meta', $fields ) ) {
$data['meta'] = $this->meta->get_value( $post->ID, $request );
}

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
Expand Down Expand Up @@ -752,6 +765,8 @@ public function get_item_schema() {
$schema['properties']['guid'] = $parent_schema['properties']['guid'];
}

$schema['properties']['meta'] = $this->meta->get_field_schema();

$this->schema = $schema;

return $this->add_additional_fields_schema( $this->schema );
Expand Down
111 changes: 111 additions & 0 deletions src/wp-includes/rest-api/fields/class-wp-rest-revision-meta-fields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
/**
* REST API: WP_REST_Revision_Meta_Fields class
*
* @package WordPress
* @subpackage REST_API
* @since 6.4.0
*/

/**
* Core class used to manage meta values for revisions via the REST API.
*
* @since 6.4.0
*
* @see WP_REST_Meta_Fields
*/
class WP_REST_Revision_Meta_Fields extends WP_REST_Meta_Fields {

/**
* Revision type to register fields for.
*
* @since 6.4.0
* @var string
*/
protected $post_type;

/**
* Constructor.
*
* @since 6.4.0
*
* @param string $post_type Revision type to register fields for.
*/
public function __construct( $post_type ) {
$this->post_type = $post_type;
}

/**
* Retrieves the post meta type.
*
* @since 6.4.0
*
* @return string The meta type.
*/
protected function get_meta_type() {
return 'revision';
}

/**
* Retrieves the post meta subtype.
*
* @since 6.4.0
*
* @return string Subtype for the meta type, or empty string if no specific subtype.
*/
protected function get_meta_subtype() {
return $this->post_type;
}

/**
* Retrieves the type for register_rest_field().
*
* @since 6.4.0
*
* @see register_rest_field()
*
* @return string The REST field type.
*/
public function get_rest_field_type() {
return $this->post_type;
}

/**
* Retrieves the meta field value.
*
* @since 4.7.0
*
* @param int $object_id Object ID to fetch meta for.
* @param WP_REST_Request $request Full details about the request.
* @return array Array containing the meta values keyed by name.
*/
public function get_value( $object_id, $request ) {
$data = get_post_meta( $object_id );
return $data;
}

/**
* Retrieves raw metadata value for the specified object.
*
* @since 5.5.0
*
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
* the specified object. Default empty string.
* @param bool $single Optional. If true, return only the first value of the specified `$meta_key`.
* This parameter has no effect if `$meta_key` is not specified. Default false.
* @return mixed An array of values if `$single` is false.
* The value of the meta field if `$single` is true.
* False for an invalid `$object_id` (non-numeric, zero, or negative value),
* or if `$meta_type` is not specified.
* Null if the value does not exist.
*/
function get_metadata_raw( $meta_type, $object_id, $meta_key = '', $single = false ) {
$data = get_post_meta( $object_id, $request );
return $data;
}


}
Loading
Loading