Revisions: Add a way to filter the revisions considered for deletion.

This changeset introduces a new filter for `wp_save_post_revision()`. `wp_save_post_revision_revisions_before_deletion` passes the revisions to be considered for deletion, and the new revision's post ID.

This allows extenders to exclude specific revisions from being considered for deletion.

Props jhned, costdev, audrasjb, adamsilverstein, mukesh27.
Fixes #57320.

Built from https://develop.svn.wordpress.org/trunk@55254


git-svn-id: http://core.svn.wordpress.org/trunk@54787 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
audrasjb 2023-02-07 12:39:17 +00:00
parent 0c5812699c
commit fe3f12b10f
2 changed files with 30 additions and 1 deletions

View File

@ -200,6 +200,35 @@ function wp_save_post_revision( $post_id ) {
$revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) );
/**
* Filters the revisions to be considered for deletion.
*
* @since 6.2.0
*
* @param WP_Post[]|int[] $revisions Array of revision objects or IDs,
* or an empty array if none.
* @param int $post_id The ID of the post to save as a revision.
*/
$filtered_revisions = apply_filters(
'wp_save_post_revision_revisions_before_deletion',
$revisions,
$post_id
);
if ( is_array( $filtered_revisions ) ) {
$revisions = $filtered_revisions;
} else {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: %s: The filter name. */
__( 'The "%s" filter should return an array.' ),
'wp_save_post_revision_revisions_before_deletion'
),
'6.2.0'
);
}
$delete = count( $revisions ) - $revisions_to_keep;
if ( $delete < 1 ) {

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.2-alpha-55253';
$wp_version = '6.2-alpha-55254';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.