From a8a396d656ca6444101f0db147b84639abd51f52 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Fri, 23 May 2008 15:27:56 +0000 Subject: [PATCH] Allow defining the number of revisions to save. Props mdawaffe. see #6775 git-svn-id: https://develop.svn.wordpress.org/trunk@7987 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/post.php | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/wp-includes/post.php b/wp-includes/post.php index af9b7527cb..9a99a2a4a5 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -3018,6 +3018,7 @@ function wp_save_revision( $post_id ) { if ( @constant( 'DOING_AUTOSAVE' ) ) return; + // WP_POST_REVISIONS = 0, false if ( !constant('WP_POST_REVISIONS') ) return; @@ -3027,7 +3028,30 @@ function wp_save_revision( $post_id ) { if ( !in_array( $post['post_type'], array( 'post', 'page' ) ) ) return; - return _wp_put_revision( $post ); + $return = _wp_put_revision( $post ); + + // WP_POST_REVISIONS = true (default), -1 + if ( !is_numeric( WP_POST_REVISIONS ) || WP_POST_REVISIONS < 0 ) + return $return; + + // all revisions and (possibly) one autosave + $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) ); + + // WP_POST_REVISIONS = (int) (# of autasaves to save) + $delete = count($revisions) - WP_POST_REVISIONS; + + if ( $delete < 1 ) + return $return; + + $revisions = array_slice( $revisions, 0, $delete ); + + for ( $i = 0; isset($revisions[$i]); $i++ ) { + if ( false !== strpos( $revisions[$i]->post_name, 'autosave' ) ) + continue; + wp_delete_revision( $revisions[$i]->ID ); + } + + return $return; } /** @@ -3225,12 +3249,17 @@ function wp_delete_revision( $revision_id ) { * @param int|object $post_id post ID or post object * @return array empty if no revisions */ -function wp_get_post_revisions( $post_id = 0 ) { +function wp_get_post_revisions( $post_id = 0, $args = null ) { if ( !constant('WP_POST_REVISIONS') ) return array(); if ( ( !$post = get_post( $post_id ) ) || empty( $post->ID ) ) return array(); - if ( !$revisions = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'revision', 'post_status' => 'inherit' ) ) ) + + $defaults = array( 'order' => 'DESC', 'orderby' => 'date' ); + $args = wp_parse_args( $args, $defaults ); + $args = array_merge( $args, array( 'post_parent' => $post->ID, 'post_type' => 'revision', 'post_status' => 'inherit' ) ); + + if ( !$revisions = get_children( $args ) ) return array(); return $revisions; }