From 404aa52ea2f6a68e78a353da506e338663b7ebc3 Mon Sep 17 00:00:00 2001 From: Mark Jaquith Date: Wed, 27 Mar 2013 18:11:56 +0000 Subject: [PATCH] Take revision control out of the realm of a pure constant. Make it filterable. * New filter: wp_revisions_to_keep props ethitter, SergeyBiryukov. fixes #22289. git-svn-id: https://develop.svn.wordpress.org/trunk@23818 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/revision.php | 2 +- wp-includes/class-wp-xmlrpc-server.php | 4 +- wp-includes/revision.php | 61 +++++++++++++++++++++----- 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/wp-admin/revision.php b/wp-admin/revision.php index 385d4ef79e..b173650154 100644 --- a/wp-admin/revision.php +++ b/wp-admin/revision.php @@ -48,7 +48,7 @@ default : break; // Revisions disabled and we're not looking at an autosave - if ( ( ! WP_POST_REVISIONS || !post_type_supports($post->post_type, 'revisions') ) && !wp_is_post_autosave( $revision ) ) { + if ( ! wp_revisions_enabled( $post ) && ! wp_is_post_autosave( $revision ) ) { $redirect = 'edit.php?post_type=' . $post->post_type; break; } diff --git a/wp-includes/class-wp-xmlrpc-server.php b/wp-includes/class-wp-xmlrpc-server.php index 6f4e6cf901..a651cbea77 100644 --- a/wp-includes/class-wp-xmlrpc-server.php +++ b/wp-includes/class-wp-xmlrpc-server.php @@ -3535,7 +3535,7 @@ class wp_xmlrpc_server extends IXR_Server { return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts.' ) ); // Check if revisions are enabled. - if ( ! WP_POST_REVISIONS || ! post_type_supports( $post->post_type, 'revisions' ) ) + if ( ! wp_revisions_enabled( $post ) ) return new IXR_Error( 401, __( 'Sorry, revisions are disabled.' ) ); $revisions = wp_get_post_revisions( $post_id ); @@ -3602,7 +3602,7 @@ class wp_xmlrpc_server extends IXR_Server { return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) ); // Check if revisions are disabled. - if ( ! WP_POST_REVISIONS || ! post_type_supports( $post->post_type, 'revisions' ) ) + if ( ! wp_revisions_enabled( $post ) ) return new IXR_Error( 401, __( 'Sorry, revisions are disabled.' ) ); $post = wp_restore_post_revision( $revision_id ); diff --git a/wp-includes/revision.php b/wp-includes/revision.php index b6e02b28ba..7970335415 100644 --- a/wp-includes/revision.php +++ b/wp-includes/revision.php @@ -78,17 +78,16 @@ function wp_save_post_revision( $post_id, $new_data = null ) { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; - // WP_POST_REVISIONS = 0, false - if ( ! WP_POST_REVISIONS ) + if ( ! $post = get_post( $post_id, ARRAY_A ) ) return; - if ( !$post = get_post( $post_id, ARRAY_A ) ) + if ( ! wp_revisions_enabled( (object) $post ) ) return; if ( 'auto-draft' == $post['post_status'] ) return; - if ( !post_type_supports($post['post_type'], 'revisions') ) + if ( ! post_type_supports( $post['post_type'], 'revisions' ) ) return; // if new data is supplied, check that it is different from last saved revision, unless a plugin tells us to always save regardless @@ -107,15 +106,15 @@ function wp_save_post_revision( $post_id, $new_data = null ) { $return = _wp_put_post_revision( $post ); - // WP_POST_REVISIONS = true (default), -1 - if ( !is_numeric( WP_POST_REVISIONS ) || WP_POST_REVISIONS < 0 ) + $revisions_to_keep = wp_revisions_to_keep( (object) $post ); + + if ( $revisions_to_keep < 0 ) return $return; // all revisions and (possibly) one autosave $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) ); - // WP_POST_REVISIONS = (int) (# of autosaves to save) - $delete = count($revisions) - WP_POST_REVISIONS; + $delete = count($revisions) - $revisions_to_keep; if ( $delete < 1 ) return $return; @@ -368,19 +367,61 @@ function wp_delete_post_revision( $revision_id ) { * @return array empty if no revisions */ function wp_get_post_revisions( $post_id = 0, $args = null ) { - if ( ( !$post = get_post( $post_id ) ) || empty( $post->ID ) ) + $post = get_post( $post_id ); + if ( ! $post || empty( $post->ID ) || ! wp_revisions_enabled( $post ) ) return array(); $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 ) ) + if ( ! $revisions = get_children( $args ) ) return array(); return $revisions; } +/** + * Determine if revisions are enabled for a given post. + * + * @since 3.6.0 + * + * @uses wp_revisions_to_keep() + * + * @param object $post + * @return bool + */ +function wp_revisions_enabled( $post ) { + return wp_revisions_to_keep( $post ) != 0; +} + +/** + * Determine how many revisions to retain for a given post. + * By default, an infinite number of revisions are stored if a post type supports revisions. + * + * @since 3.6.0 + * + * @uses post_type_supports() + * @uses apply_filters() Calls 'wp_revisions_to_keep' hook on the number of revisions. + * + * @param object $post + * @return int + */ +function wp_revisions_to_keep( $post ) { + $num = WP_POST_REVISIONS; + + if ( true === $num ) + $num = -1; + else + $num = intval( $num ); + + if ( ! post_type_supports( $post->post_type, 'revisions' ) ) + $num = 0; + + return (int) apply_filters( 'wp_revisions_to_keep', $num, $post ); +} + + function _set_preview($post) { if ( ! is_object($post) )