-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Add revisioning of post meta, including ‘footnotes’ by default #4859
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
Changes from 1 commit
b8bf5ef
6667610
15e9e4e
b892323
6235af5
3a87749
05d0f86
19e1f8a
0b86da9
52dd943
ef14c11
31a6d61
da8abe6
d2f0db1
6c9bece
2e74964
268385f
4fd4302
3722d1e
b26a695
5d6e5b9
b4fa33f
1603c55
d244a39
d86ec77
a80620a
885640d
ea4f181
e63334b
f4234f8
1f1e5a0
6c5061b
5bd7e7b
65bfe81
7254e3a
8c3fba2
26a9d0b
210573b
485f20b
167261e
5b5dc0d
f8d0d30
ea9b8ce
15e8bbd
1e54aa3
a3fa561
877420e
f57b79b
cad9272
57c872a
ca01058
92590aa
6fd9a9e
f83d353
7f70990
432a810
d80b7b4
7160abb
96301da
e2d0cc7
5f07145
afc1109
ac279e3
4205d65
e7b4495
68f6410
ee35085
61bfafc
c27258a
20874e3
68a419b
3ee633f
1e17f5b
982c553
d04574a
3a8f6fc
192e7c0
225a91b
c2482fd
2331d36
5c48c7a
e211ca1
7247e93
891a0e4
c35cc5f
826bebb
750dcdc
0b88e50
2c02dbc
b5ba2d3
95508d9
b0b880b
f04a319
e231886
e1950b0
6f6e3ce
b77bf22
b6e221a
40febf2
69c6107
f9b760b
8360faa
a1971f7
99b2aa8
b94e1a6
e09f011
01a4788
8e7b72a
d6c6ca2
73f6a23
3db644c
95c9e9d
9bab9a9
dc0b907
ab2d500
8bf1727
c7904ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -365,11 +365,34 @@ function _wp_put_post_revision( $post = null, $autosave = false ) { | |||||
| * @param int $revision_id Post revision ID. | ||||||
| */ | ||||||
| do_action( '_wp_put_post_revision', $revision_id ); | ||||||
|
|
||||||
| // Save any revisioned meta fields. | ||||||
| wp_save_revisioned_meta_fields( $revision_id ); | ||||||
| } | ||||||
|
|
||||||
| return $revision_id; | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| /** | ||||||
| * Save the revisioned meta fields. | ||||||
| * | ||||||
| * @param int $revision_id The ID of the revision to save the meta to. | ||||||
| * | ||||||
| * @since 6.4.0 | ||||||
| */ | ||||||
| function wp_save_revisioned_meta_fields( $revision_id ) { | ||||||
| $revision = get_post( $revision_id ); | ||||||
| $post_id = $revision->post_parent; | ||||||
|
|
||||||
| // Save revisioned meta fields. | ||||||
| foreach ( wp_post_revision_meta_keys() as $meta_key ) { | ||||||
| if ( metadata_exists( 'post', $post_id, $meta_key ) ) { | ||||||
| _wp_copy_post_meta( $post_id, $revision_id, $meta_key ); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Gets a post revision. | ||||||
| * | ||||||
|
|
@@ -450,6 +473,9 @@ function wp_restore_post_revision( $revision, $fields = null ) { | |||||
| // Update last edit user. | ||||||
| update_post_meta( $post_id, '_edit_last', get_current_user_id() ); | ||||||
|
|
||||||
| // Restore any revisioned meta fields. | ||||||
| wp_restore_post_revision_meta( $post_id, $revision['ID'] ); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we do this
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good idea, let me check.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great! Added in
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @adamsilverstein Has this been addressed?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, this is added
Comment on lines
+503
to
+504
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
@adamsilverstein This is not need anymore, as it hooked in the next line.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏼 will commit in a follow up |
||||||
|
|
||||||
| /** | ||||||
| * Fires after a post revision has been restored. | ||||||
| * | ||||||
|
|
@@ -463,6 +489,83 @@ function wp_restore_post_revision( $revision, $fields = null ) { | |||||
| return $post_id; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Restore the revisioned meta values for a post. | ||||||
| * | ||||||
| * @param int $post_id The ID of the post to restore the meta to. | ||||||
| * @param int $revision_id The ID of the revision to restore the meta from. | ||||||
| * | ||||||
| * @since 6.4.0 | ||||||
| */ | ||||||
| function wp_restore_post_revision_meta( $post_id, $revision_id ) { | ||||||
|
|
||||||
| // Restore revisioned meta fields. | ||||||
| foreach ( (array) wp_post_revision_meta_keys() as $meta_key ) { | ||||||
|
|
||||||
| // Clear any existing meta. | ||||||
| delete_post_meta( $post_id, $meta_key ); | ||||||
|
|
||||||
| _wp_copy_post_meta( $revision_id, $post_id, $meta_key ); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Copy post meta for the given key from one post to another. | ||||||
| * | ||||||
| * @param int $source_post_id Post ID to copy meta value(s) from. | ||||||
| * @param int $target_post_id Post ID to copy meta value(s) to. | ||||||
| * @param string $meta_key Meta key to copy. | ||||||
| * | ||||||
| * @since 6.4.0 | ||||||
| */ | ||||||
| function _wp_copy_post_meta( $source_post_id, $target_post_id, $meta_key ) { | ||||||
|
|
||||||
| foreach ( get_post_meta( $source_post_id, $meta_key ) as $meta_value ) { | ||||||
TimothyBJacobs marked this conversation as resolved.
Show resolved
Hide resolved
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @spacedmonkey - I think the same issue for defaults applies here. If a default is set for a meta field, this code will copy the default value over instead of the actual (missing) value, which is unintentional. switching is to get_metadata_raw as well (although may have been prefented by check for existence of meta in calling function)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing this to |
||||||
| /** | ||||||
| * We use add_metadata() function vs add_post_meta() here | ||||||
| * to allow for a revision post target OR regular post. | ||||||
| */ | ||||||
| add_metadata( 'post', $target_post_id, $meta_key, wp_slash( $meta_value ) ); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Determine which post meta fields should be revisioned. | ||||||
| * | ||||||
| * @since 6.4.0 | ||||||
| * | ||||||
| * @return array An array of meta keys to be revisioned. | ||||||
| */ | ||||||
| function wp_post_revision_meta_keys() { | ||||||
| /** | ||||||
| * Filter the list of post meta keys to be revisioned. | ||||||
| * | ||||||
| * @since 6.4.0 | ||||||
| * | ||||||
| * @param array $keys An array of default meta fields to be revisioned. | ||||||
| */ | ||||||
| return apply_filters( 'wp_post_revision_meta_keys', array( 'footnotes' ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Check whether revisioned post meta fields have changed. | ||||||
| * | ||||||
| * @param bool $post_has_changed Whether the post has changed. | ||||||
| * @param WP_Post $last_revision The last revision post object. | ||||||
| * @param WP_Post $post The post object. | ||||||
| * | ||||||
| * @since 6.4.0 | ||||||
| */ | ||||||
| function wp_check_revisioned_meta_fields_have_changed( $post_has_changed, WP_Post $last_revision, WP_Post $post ) { | ||||||
| foreach ( wp_post_revision_meta_keys() as $meta_key ) { | ||||||
| if ( get_post_meta( $post->ID, $meta_key ) !== get_post_meta( $last_revision->ID, $meta_key ) ) { | ||||||
| $post_has_changed = true; | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| return $post_has_changed; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Deletes a revision. | ||||||
| * | ||||||
|
|
@@ -728,6 +831,7 @@ function _set_preview( $post ) { | |||||
|
|
||||||
| add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 ); | ||||||
| add_filter( 'get_post_metadata', '_wp_preview_post_thumbnail_filter', 10, 3 ); | ||||||
| add_filter( 'get_post_metadata', '_wp_preview_meta_filter', 10, 4 ); | ||||||
|
|
||||||
| return $post; | ||||||
| } | ||||||
|
|
@@ -948,3 +1052,38 @@ function _wp_upgrade_revisions_of_post( $post, $revisions ) { | |||||
|
|
||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Filters preview post meta retrieval to get values from the autosave. | ||||||
| * | ||||||
| * Filters revisioned meta keys only. | ||||||
| * | ||||||
| * @since 6.4.0 | ||||||
| * | ||||||
| * @param mixed $value Meta value to filter. | ||||||
| * @param int $object_id Object ID. | ||||||
| * @param string $meta_key Meta key to filter a value for. | ||||||
| * @param bool $single Whether to return a single value. Default false. | ||||||
| * @return mixed Original meta value if the meta key isn't revisioned, the object doesn't exist, | ||||||
| * the post type is a revision or the post ID doesn't match the object ID. | ||||||
| * Otherwise, the revisioned meta value is returned for the preview. | ||||||
| */ | ||||||
| function _wp_preview_meta_filter( $value, $object_id, $meta_key, $single ) { | ||||||
|
|
||||||
| $post = get_post(); | ||||||
| if ( | ||||||
| empty( $post ) || | ||||||
| $post->ID !== $object_id || | ||||||
| ! in_array( $meta_key, wp_post_revision_meta_keys(), true ) || | ||||||
| 'revision' === $post->post_type | ||||||
| ) { | ||||||
| return $value; | ||||||
| } | ||||||
|
|
||||||
| $preview = wp_get_post_autosave( $post->ID ); | ||||||
| if ( ! is_object( $preview ) ) { | ||||||
adamsilverstein marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| return $value; | ||||||
| } | ||||||
|
|
||||||
| return get_post_meta( $preview->ID, $meta_key, $single ); | ||||||
| } | ||||||
Uh oh!
There was an error while loading. Please reload this page.