Skip to content

Commit

Permalink
Revisions: Add a way to filter the revisions considered for deletion.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
audrasjb committed Feb 7, 2023
1 parent 0c58126 commit fe3f12b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 29 additions & 0 deletions wp-includes/revision.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit fe3f12b

Please sign in to comment.