}
/**
- * Saves an already existing post as a post revision.
+ * Creates a revision for the current version of a post.
*
- * Typically used immediately after post updates.
- * Adds a copy of the current post as a revision, so latest revision always matches current post
+ * Typically used immediately after a post update, as every update is a revision,
+ * and the most recent revision always matches the current post.
*
* @since 2.6.0
*
- * @uses _wp_put_post_revision()
- *
- * @param int $post_id The ID of the post to save as a revision.
- * @return mixed Null or 0 if error, new revision ID, if success.
+ * @param int $post_id The ID of the post to save as a revision.
+ * @return null|int Null or 0 if error, new revision ID, if success.
*/
function wp_save_post_revision( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
*
* @since 3.6.0
*
- * @param bool $check_for_changes Whether to check for changes before saving a new revision.
- * Default true.
- * @param int $last_revision ID of the last revision.
- * @param int $post Post ID.
+ * @param bool $check_for_changes Whether to check for changes before saving a new revision.
+ * Default true.
+ * @param WP_Post $last_revision The the last revision post object.
+ * @param WP_Post $post The post object.
*
*/
if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', $check_for_changes = true, $last_revision, $post ) ) {
break;
}
}
+
+ /**
+ * Filter whether a post has changed.
+ *
+ * By default a revision is saved only if one of the revisioned fields has changed.
+ * This filter allows for additional checks to determine if there were changes.
+ *
+ * @since 4.1.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.
+ *
+ */
+ $post_has_changed = (bool) apply_filters( 'wp_save_post_revision_post_has_changed', $post_has_changed, $last_revision, $post );
+
//don't save revision if post unchanged
- if( ! $post_has_changed )
+ if( ! $post_has_changed ) {
return;
+ }
}
}
$return = _wp_put_post_revision( $post );
+ // If a limit for the number of revisions to keep has been set,
+ // delete the oldest ones.
$revisions_to_keep = wp_revisions_to_keep( $post );
if ( $revisions_to_keep < 0 )
return $return;
- // all revisions and autosaves
$revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) );
$delete = count($revisions) - $revisions_to_keep;
*
* @since 2.6.0
*
- * @uses wp_get_post_revisions()
- *
* @param int $post_id The post ID.
* @param int $user_id optional The post author ID.
* @return object|bool The autosaved data or false on failure or when no autosave exists.
* @since 2.6.0
* @access private
*
- * @uses wp_insert_post()
- *
* @param int|object|array $post Post ID, post object OR post array.
* @param bool $autosave Optional. Is the revision an autosave?
* @return mixed WP_Error or 0 if error, new revision ID if success.
*
* @since 2.6.0
*
- * @uses get_post()
- *
* @param int|object $post The post ID or object.
* @param string $output Optional. OBJECT, ARRAY_A, or ARRAY_N.
* @param string $filter Optional sanitation filter. @see sanitize_post().
*
* @since 2.6.0
*
- * @uses wp_get_post_revision()
- * @uses wp_update_post()
- *
* @param int|object $revision_id Revision ID or revision object.
* @param array $fields Optional. What fields to restore from. Defaults to all.
* @return mixed Null if error, false if no fields to restore, (int) post ID if success.
*
* @since 2.6.0
*
- * @uses wp_get_post_revision()
- * @uses wp_delete_post()
- *
* @param int|object $revision_id Revision ID or revision object.
* @return mixed Null or WP_Error if error, deleted post if success.
*/
*
* @since 2.6.0
*
- * @uses get_children()
- *
* @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
* @return array An array of revisions, or an empty array if none.
*/
*
* @since 3.6.0
*
- * @uses wp_revisions_to_keep()
- *
- * @param object $post The post object.
+ * @param WP_Post $post The post object.
* @return bool True if number of revisions to keep isn't zero, false otherwise.
*/
function wp_revisions_enabled( $post ) {
/**
* 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
+ * By default, an infinite number of revisions are kept.
*
- * @uses post_type_supports()
+ * The constant WP_POST_REVISIONS can be set in wp-config to specify the limit
+ * of revisions to keep.
*
- * @param object $post The post object.
+ * @since 3.6.0
+ *
+ * @param WP_Post $post The post object.
* @return int The number of revisions to keep.
*/
function wp_revisions_to_keep( $post ) {
* @since 3.6.0
* @access private
*
- * @uses wp_get_post_revisions()
- *
- * @param object $post Post object
+ * @param WP_Post $post Post object
* @param array $revisions Current revisions of the post
* @return bool true if the revisions were upgraded, false if problems
*/