Merged in feature/81-dev-dev01 (pull request #5)

auto-patch  81-dev-dev01-2023-12-05T22_45_26

* auto-patch  81-dev-dev01-2023-12-05T22_45_26
This commit is contained in:
Tony Volpe
2023-12-05 23:05:59 +00:00
parent ba16964e7a
commit 725d3043d5
1463 changed files with 142461 additions and 89421 deletions

View File

@@ -95,6 +95,27 @@ function _wp_post_revision_data( $post = array(), $autosave = false ) {
return $revision_data;
}
/**
* Saves revisions for a post after all changes have been made.
*
* @since 6.4.0
*
* @param int $post_id The post id that was inserted.
* @param WP_Post $post The post object that was inserted.
* @param bool $update Whether this insert is updating an existing post.
*/
function wp_save_post_revision_on_insert( $post_id, $post, $update ) {
if ( ! $update ) {
return;
}
if ( ! has_action( 'post_updated', 'wp_save_post_revision' ) ) {
return;
}
wp_save_post_revision( $post_id );
}
/**
* Creates a revision for the current version of a post.
*
@@ -111,6 +132,11 @@ function wp_save_post_revision( $post_id ) {
return;
}
// Prevent saving post revisions if revisions should be saved on wp_after_insert_post.
if ( doing_action( 'post_updated' ) && has_action( 'wp_after_insert_post', 'wp_save_post_revision_on_insert' ) ) {
return;
}
$post = get_post( $post_id );
if ( ! $post ) {
@@ -361,15 +387,39 @@ function _wp_put_post_revision( $post = null, $autosave = false ) {
* Fires once a revision has been saved.
*
* @since 2.6.0
* @since 6.4.0 The post_id parameter was added.
*
* @param int $revision_id Post revision ID.
* @param int $post_id Post ID.
*/
do_action( '_wp_put_post_revision', $revision_id );
do_action( '_wp_put_post_revision', $revision_id, $post['post_parent'] );
}
return $revision_id;
}
/**
* Save the revisioned meta fields.
*
* @since 6.4.0
*
* @param int $revision_id The ID of the revision to save the meta to.
* @param int $post_id The ID of the post the revision is associated with.
*/
function wp_save_revisioned_meta_fields( $revision_id, $post_id ) {
$post_type = get_post_type( $post_id );
if ( ! $post_type ) {
return;
}
foreach ( wp_post_revision_meta_keys( $post_type ) 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.
*
@@ -463,6 +513,105 @@ function wp_restore_post_revision( $revision, $fields = null ) {
return $post_id;
}
/**
* Restore the revisioned meta values for a post.
*
* @since 6.4.0
*
* @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.
*/
function wp_restore_post_revision_meta( $post_id, $revision_id ) {
$post_type = get_post_type( $post_id );
if ( ! $post_type ) {
return;
}
// Restore revisioned meta fields.
foreach ( wp_post_revision_meta_keys( $post_type ) 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.
*
* @since 6.4.0
*
* @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.
*/
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 ) {
/**
* 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
*
* @param string $post_type The post type being revisioned.
* @return array An array of meta keys to be revisioned.
*/
function wp_post_revision_meta_keys( $post_type ) {
$registered_meta = array_merge(
get_registered_meta_keys( 'post' ),
get_registered_meta_keys( 'post', $post_type )
);
$wp_revisioned_meta_keys = array();
foreach ( $registered_meta as $name => $args ) {
if ( $args['revisions_enabled'] ) {
$wp_revisioned_meta_keys[ $name ] = true;
}
}
$wp_revisioned_meta_keys = array_keys( $wp_revisioned_meta_keys );
/**
* Filter the list of post meta keys to be revisioned.
*
* @since 6.4.0
*
* @param array $keys An array of meta fields to be revisioned.
* @param string $post_type The post type being revisioned.
*/
return apply_filters( 'wp_post_revision_meta_keys', $wp_revisioned_meta_keys, $post_type );
}
/**
* Check whether revisioned post meta fields have changed.
*
* @since 6.4.0
*
* @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.
* @return bool Whether the post has changed.
*/
function wp_check_revisioned_meta_fields_have_changed( $post_has_changed, WP_Post $last_revision, WP_Post $post ) {
foreach ( wp_post_revision_meta_keys( $post->post_type ) 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 +877,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;
}
@@ -768,7 +918,7 @@ function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) {
return $terms;
}
if ( empty( $_REQUEST['post_format'] ) || $post->ID != $post_id
if ( empty( $_REQUEST['post_format'] ) || $post->ID !== $post_id
|| 'post_format' !== $taxonomy || 'revision' === $post->post_type
) {
return $terms;
@@ -778,6 +928,7 @@ function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) {
$terms = array();
} else {
$term = get_term_by( 'slug', 'post-format-' . sanitize_key( $_REQUEST['post_format'] ), 'post_format' );
if ( $term ) {
$terms = array( $term ); // Can only have one post format.
}
@@ -804,13 +955,10 @@ function _wp_preview_post_thumbnail_filter( $value, $post_id, $meta_key ) {
return $value;
}
if ( empty( $_REQUEST['_thumbnail_id'] ) ||
empty( $_REQUEST['preview_id'] ) ||
$post->ID != $post_id ||
'_thumbnail_id' !== $meta_key ||
'revision' === $post->post_type ||
$post_id != $_REQUEST['preview_id'] ) {
if ( empty( $_REQUEST['_thumbnail_id'] ) || empty( $_REQUEST['preview_id'] )
|| $post->ID !== $post_id || $post_id !== (int) $_REQUEST['preview_id']
|| '_thumbnail_id' !== $meta_key || 'revision' === $post->post_type
) {
return $value;
}
@@ -948,3 +1096,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( $post->post_type ), true ) ||
'revision' === $post->post_type
) {
return $value;
}
$preview = wp_get_post_autosave( $post->ID );
if ( false === $preview ) {
return $value;
}
return get_post_meta( $preview->ID, $meta_key, $single );
}