Post revisions:
- Always update the revision version when updating post authors. - Check if revisions have been updated and return early. - Update the revisions by direct query to avoid resetting post_modified. - Fix a bug where we may be comparing with an autosave but need to compare with the latest revision. Fixes #16215. git-svn-id: http://core.svn.wordpress.org/trunk@23849 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
85a3c6a82b
commit
7b831aabd3
|
@ -1424,7 +1424,7 @@ function wp_list_post_revisions( $post_id = 0, $args = null ) {
|
||||||
|
|
||||||
// Since 3.6 revisions include a copy of the current post data as a revision.
|
// Since 3.6 revisions include a copy of the current post data as a revision.
|
||||||
// The following removes that revision when $parent == false
|
// The following removes that revision when $parent == false
|
||||||
$parent_included = wp_first_revision_matches_current_version( $post_id );
|
$parent_included = _wp_last_revision_matches_current_post( $post_id );
|
||||||
if ( $parent_included && ! $parent )
|
if ( $parent_included && ! $parent )
|
||||||
array_pop( $revisions );
|
array_pop( $revisions );
|
||||||
elseif ( ! $parent_included && $parent )
|
elseif ( ! $parent_included && $parent )
|
||||||
|
|
|
@ -63,78 +63,70 @@ function _wp_post_revision_fields( $post = null, $autosave = false ) {
|
||||||
/**
|
/**
|
||||||
* Saves an already existing post as a post revision.
|
* Saves an already existing post as a post revision.
|
||||||
*
|
*
|
||||||
* Typically used immediately prior and after post updates.
|
* Typically used immediately after post updates.
|
||||||
* Prior to update checks for old revision data (latest revision != current post before update) and adds a copy of the current post as a revision if missing
|
* Adds a copy of the current post as a revision, so latest revision always matches current post
|
||||||
* After update adds a copy of the current post as a revision, so latest revision always matches current post
|
|
||||||
*
|
*
|
||||||
* @package WordPress
|
* @package WordPress
|
||||||
* @subpackage Post_Revisions
|
* @subpackage Post_Revisions
|
||||||
* @since 2.6.0
|
* @since 2.6.0
|
||||||
*
|
*
|
||||||
* @uses _wp_put_post_revision()
|
* @uses _wp_put_post_revision()
|
||||||
* @uses wp_first_revision_matches_current_version()
|
|
||||||
*
|
*
|
||||||
* @param int $post_id The ID of the post to save as a 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.
|
* @return mixed Null or 0 if error, new revision ID, if success.
|
||||||
*/
|
*/
|
||||||
function wp_save_post_revision( $post_id ) {
|
function wp_save_post_revision( $post_id ) {
|
||||||
//check to see if the post's first revision already matches the post data
|
|
||||||
//should be true before post update, _except_ for old data which
|
|
||||||
//doesn't include a copy of the current post data in revisions
|
|
||||||
if ( wp_first_revision_matches_current_version( $post_id ) )
|
|
||||||
return;
|
|
||||||
|
|
||||||
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
|
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if ( ! $post = get_post( $post_id, ARRAY_A ) )
|
if ( ! $post = get_post( $post_id ) )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if ( ! wp_revisions_enabled( (object) $post ) )
|
if ( ! post_type_supports( $post->post_type, 'revisions' ) )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if ( 'auto-draft' == $post['post_status'] )
|
if ( 'auto-draft' == $post->post_status )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if ( ! post_type_supports( $post['post_type'], 'revisions' ) )
|
if ( ! wp_revisions_enabled( $post ) )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// compare the proposed update with the last stored revision, verify
|
// Compare the proposed update with the last stored revision verifying that
|
||||||
// different, unless a plugin tells us to always save regardless
|
// they are different, unless a plugin tells us to always save regardless.
|
||||||
|
// If no previous revisions, save one
|
||||||
if ( $revisions = wp_get_post_revisions( $post_id ) ) {
|
if ( $revisions = wp_get_post_revisions( $post_id ) ) {
|
||||||
// grab the last revision
|
// grab the last revision, but not an autosave
|
||||||
$last_revision = array_shift( $revisions );
|
foreach ( $revisions as $revision ) {
|
||||||
|
if ( false !== strpos( $revision->post_name, "{$revision->post_parent}-revision" ) ) {
|
||||||
//if no previous revisions, save one for sure
|
$last_revision = $revision;
|
||||||
if ( $last_revision_array = get_post( $last_revision->ID, ARRAY_A ) ) {
|
break;
|
||||||
|
|
||||||
if ( apply_filters( 'wp_save_post_revision_check_for_changes', true, $last_revision_array, $post ) && is_array( $post ) ) {
|
|
||||||
$post_has_changed = false;
|
|
||||||
|
|
||||||
foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
|
|
||||||
|
|
||||||
if ( normalize_whitespace( $post[ $field ] ) != normalize_whitespace( $last_revision_array[ $field ] ) ) {
|
|
||||||
$post_has_changed = true;
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//don't save revision if post unchanged
|
|
||||||
if( ! $post_has_changed )
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', true, $last_revision, $post ) ) {
|
||||||
|
$post_has_changed = false;
|
||||||
|
|
||||||
|
foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
|
||||||
|
if ( normalize_whitespace( $post->$field ) != normalize_whitespace( $last_revision->$field ) ) {
|
||||||
|
$post_has_changed = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//don't save revision if post unchanged
|
||||||
|
if( ! $post_has_changed )
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$return = _wp_put_post_revision( $post );
|
$return = _wp_put_post_revision( $post );
|
||||||
|
|
||||||
$revisions_to_keep = wp_revisions_to_keep( (object) $post );
|
$revisions_to_keep = wp_revisions_to_keep( $post );
|
||||||
|
|
||||||
if ( $revisions_to_keep < 0 )
|
if ( $revisions_to_keep < 0 )
|
||||||
return $return;
|
return $return;
|
||||||
|
|
||||||
// all revisions and (possibly) one autosave
|
// all revisions and autosaves
|
||||||
$revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) );
|
$revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) );
|
||||||
|
|
||||||
$delete = count($revisions) - $revisions_to_keep;
|
$delete = count($revisions) - $revisions_to_keep;
|
||||||
|
@ -144,9 +136,10 @@ function wp_save_post_revision( $post_id ) {
|
||||||
|
|
||||||
$revisions = array_slice( $revisions, 0, $delete );
|
$revisions = array_slice( $revisions, 0, $delete );
|
||||||
|
|
||||||
for ( $i = 0; isset($revisions[$i]); $i++ ) {
|
for ( $i = 0; isset( $revisions[$i] ); $i++ ) {
|
||||||
if ( false !== strpos( $revisions[ $i ]->post_name, 'autosave' ) )
|
if ( false !== strpos( $revisions[ $i ]->post_name, 'autosave' ) )
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
wp_delete_post_revision( $revisions[ $i ]->ID );
|
wp_delete_post_revision( $revisions[ $i ]->ID );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -444,7 +437,6 @@ function wp_revisions_to_keep( $post ) {
|
||||||
return (int) apply_filters( 'wp_revisions_to_keep', $num, $post );
|
return (int) apply_filters( 'wp_revisions_to_keep', $num, $post );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function _set_preview($post) {
|
function _set_preview($post) {
|
||||||
|
|
||||||
if ( ! is_object($post) )
|
if ( ! is_object($post) )
|
||||||
|
@ -464,30 +456,14 @@ function _set_preview($post) {
|
||||||
return $post;
|
return $post;
|
||||||
}
|
}
|
||||||
|
|
||||||
function _wp_get_post_revision_version( $post ) {
|
function _wp_get_post_revision_version( $revision ) {
|
||||||
if ( is_array( $post ) ) {
|
if ( is_object( $revision ) )
|
||||||
if ( ! isset( $post['post_name'] ) ) {
|
$revision = get_object_vars( $revision );
|
||||||
return false;
|
elseif ( !is_array( $revision ) )
|
||||||
}
|
|
||||||
|
|
||||||
$name = $post['post_name'];
|
|
||||||
} elseif ( is_object( $post ) ) {
|
|
||||||
if ( ! isset( $post->post_name ) ) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$name = $post->post_name;
|
|
||||||
} else {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
if ( ! preg_match( '/^(\d+-)(?:autosave|revision)(?:-v)(\d+)$/', $name, $matches ) ) {
|
if ( preg_match( '/^\d+-(?:autosave|revision)-v(\d+)$/', $revision['post_name'], $matches ) )
|
||||||
return 0;
|
return (int) $matches[1];
|
||||||
}
|
|
||||||
|
|
||||||
if ( '1' === $matches[2] ) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -502,7 +478,6 @@ function _wp_get_post_revision_version( $post ) {
|
||||||
* @uses get_post()
|
* @uses get_post()
|
||||||
* @uses post_type_supports()
|
* @uses post_type_supports()
|
||||||
* @uses wp_get_post_revisions()
|
* @uses wp_get_post_revisions()
|
||||||
* @uses wp_save_post_revision()
|
|
||||||
*
|
*
|
||||||
* @param int|object $post_id Post ID or post object
|
* @param int|object $post_id Post ID or post object
|
||||||
* @return true if success, false if problems
|
* @return true if success, false if problems
|
||||||
|
@ -514,55 +489,50 @@ function _wp_upgrade_revisions_of_post( $post ) {
|
||||||
if ( ! $post )
|
if ( ! $post )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
//make sure we have a current revision, only adds one if missing
|
|
||||||
wp_save_post_revision( $post->ID );
|
|
||||||
|
|
||||||
if ( ! post_type_supports( $post->post_type, 'revisions' ) )
|
if ( ! post_type_supports( $post->post_type, 'revisions' ) )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
$revisions = wp_get_post_revisions( $post->ID ); // array( 'order' => 'DESC', 'orderby' => 'date' ); // Always work from most recent to oldest
|
$revisions = wp_get_post_revisions( $post->ID ); // array( 'order' => 'DESC', 'orderby' => 'date' ); // Always work from most recent to oldest
|
||||||
|
|
||||||
|
if ( ! $first = reset( $revisions ) )
|
||||||
|
return true;
|
||||||
|
|
||||||
if ( ! $revisions )
|
// Check if the revisions have already been updated
|
||||||
|
if ( preg_match( '/^\d+-(?:autosave|revision)-v\d+$/', $first->post_name ) )
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
// Add post option exclusively
|
// Add post option exclusively
|
||||||
$lock = "revision-upgrade-{$post->ID}";
|
$lock = "revision-upgrade-{$post->ID}";
|
||||||
$locked_at = number_format( microtime( true ), 10, '.', '' );
|
$now = time();
|
||||||
$result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $locked_at ) );
|
$result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $now ) );
|
||||||
if ( ! $result ) {
|
if ( ! $result ) {
|
||||||
// If we couldn't get a lock, see how old the previous lock is
|
// If we couldn't get a lock, see how old the previous lock is
|
||||||
$locked_at = get_option( $lock );
|
$locked = get_option( $lock );
|
||||||
if ( !$locked_at ) {
|
if ( ! $locked ) {
|
||||||
// Can't write to the lock, and can't read the lock.
|
// Can't write to the lock, and can't read the lock.
|
||||||
// Something broken has happened
|
// Something broken has happened
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $lock_at < number_format( microtime( true ), 10, '.', '' ) - 3600 ) {
|
if ( $locked > $now - 3600 ) {
|
||||||
// Lock is too old - try again
|
// Lock is not too old: some other process may be upgrading this post. Bail.
|
||||||
delete_option( $lock );
|
return false;
|
||||||
return wp_upgrade_revisions_of_post( $post );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lock is not too old: some other process may be upgrading this post. Bail.
|
// Lock is too old - update it (below) and continue
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
// If we could get a lock, re-"add" the option to fire all the correct filters.
|
|
||||||
add_option( $lock, $locked_at );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$success = true;
|
// If we could get a lock, re-"add" the option to fire all the correct filters.
|
||||||
|
update_option( $lock, $now );
|
||||||
|
|
||||||
reset( $revisions );
|
reset( $revisions );
|
||||||
|
|
||||||
do {
|
do {
|
||||||
$this_revision = current( $revisions );
|
$this_revision = current( $revisions );
|
||||||
$prev_revision = next( $revisions );
|
$prev_revision = next( $revisions );
|
||||||
|
|
||||||
$this_revision_version = _wp_get_post_revision_version( $this_revision );
|
$this_revision_version = _wp_get_post_revision_version( $this_revision );
|
||||||
|
|
||||||
error_log($this_revision_version);
|
|
||||||
|
|
||||||
// Something terrible happened
|
// Something terrible happened
|
||||||
if ( false === $this_revision_version )
|
if ( false === $this_revision_version )
|
||||||
continue;
|
continue;
|
||||||
|
@ -571,37 +541,34 @@ function _wp_upgrade_revisions_of_post( $post ) {
|
||||||
if ( 0 < $this_revision_version )
|
if ( 0 < $this_revision_version )
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// This revision is the oldest revision of the post.
|
// Always update the revision version
|
||||||
// The correct post_author is probably $post->post_author, but that's only a good guess.
|
$update = array(
|
||||||
// Leave un-upgraded.
|
'post_name' => preg_replace( '/^(\d+-(?:autosave|revision))[\d-]*$/', '$1-v1', $this_revision->post_name ),
|
||||||
if ( ! $prev_revision ) {
|
);
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$prev_revision_version = _wp_get_post_revision_version( $prev_revision );
|
// If this revision is the oldest revision of the post, i.e. no $prev_revision,
|
||||||
|
// the correct post_author is probably $post->post_author, but that's only a good guess.
|
||||||
|
// Update the revision version only and Leave the author as-is.
|
||||||
|
if ( $prev_revision ) {
|
||||||
|
$prev_revision_version = _wp_get_post_revision_version( $prev_revision );
|
||||||
|
|
||||||
// If the previous revision is already up to date, it no longer has the information we need :(
|
// If the previous revision is already up to date, it no longer has the information we need :(
|
||||||
if ( 0 < $prev_revision_version ) {
|
if ( $prev_revision_version < 1 )
|
||||||
continue;
|
$update['post_author'] = $prev_revision->post_author;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upgrade this revision
|
// Upgrade this revision
|
||||||
// Cast as object so that wp_update_post() handles slashing for us
|
$result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) );
|
||||||
$update = (object) array(
|
|
||||||
'ID' => $this_revision->ID,
|
if ( $result )
|
||||||
'post_name' => preg_replace( '/^(\d+-)(autosave|revision)-(\d+)$/', '$1$2-v1', $this_revision->post_name ),
|
wp_cache_delete( $this_revision->ID, 'posts' );
|
||||||
'post_author' => $prev_revision->post_author,
|
|
||||||
);
|
|
||||||
//error_log(json_encode($update));
|
|
||||||
$result = wp_update_post( $update );
|
|
||||||
if ( ! $result || is_wp_error( $result ) ) {
|
|
||||||
// Wilhelm!
|
|
||||||
$success = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} while ( $prev_revision );
|
} while ( $prev_revision );
|
||||||
|
|
||||||
delete_option( $lock );
|
delete_option( $lock );
|
||||||
|
|
||||||
|
// Add a copy of the post as latest revision.
|
||||||
|
wp_save_post_revision( $post->ID );
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -628,20 +595,37 @@ function _show_post_preview() {
|
||||||
* @param int|object $post Post ID or post object.
|
* @param int|object $post Post ID or post object.
|
||||||
* @return bool false if not a match, otherwise true.
|
* @return bool false if not a match, otherwise true.
|
||||||
*/
|
*/
|
||||||
function wp_first_revision_matches_current_version( $post ) {
|
function _wp_last_revision_matches_current_post( $post ) {
|
||||||
|
|
||||||
if ( ! $post = get_post( $post ) )
|
if ( ! $post = get_post( $post ) )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if ( ! $revisions = wp_get_post_revisions( $post->ID ) )
|
if ( ! $revisions = wp_get_post_revisions( $post->ID ) )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
$last_revision = array_shift( $revisions );
|
foreach ( $revisions as $revision ) {
|
||||||
|
if ( false !== strpos( $revision->post_name, "{$revision->post_parent}-revision" ) ) {
|
||||||
|
$last_revision = $revision;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ( ! ($last_revision->post_modified == $post->post_modified ) )
|
// No revisions yet, only autosaves
|
||||||
|
if ( ! isset( $last_revision ) )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return true;
|
$post_has_changed = false;
|
||||||
|
if ( $last_revision->post_modified == $post->post_modified ) {
|
||||||
|
foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
|
||||||
|
if ( normalize_whitespace( $post->$field ) != normalize_whitespace( $last_revision->$field ) ) {
|
||||||
|
$post_has_changed = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ! $post_has_changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -706,4 +690,4 @@ function wp_text_diff_with_count( $left_string, $right_string, $args = null ) {
|
||||||
$r .= "</table>";
|
$r .= "</table>";
|
||||||
|
|
||||||
return array( 'html' => $r, 'linesadded' => $linesadded, 'linesdeleted' => $linesdeleted );
|
return array( 'html' => $r, 'linesadded' => $linesadded, 'linesdeleted' => $linesdeleted );
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue