<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[23849] trunk/wp-includes: Post revisions:</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/23849">23849</a></dd>
<dt>Author</dt> <dd>azaozz</dd>
<dt>Date</dt> <dd>2013-03-29 05:28:44 +0000 (Fri, 29 Mar 2013)</dd>
</dl>
<h3>Log Message</h3>
<pre>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 <a href="http://core.trac.wordpress.org/ticket/16215">#16215</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesposttemplatephp">trunk/wp-includes/post-template.php</a></li>
<li><a href="#trunkwpincludesrevisionphp">trunk/wp-includes/revision.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesposttemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/post-template.php (23848 => 23849)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/post-template.php        2013-03-29 05:24:12 UTC (rev 23848)
+++ trunk/wp-includes/post-template.php        2013-03-29 05:28:44 UTC (rev 23849)
</span><span class="lines">@@ -1424,7 +1424,7 @@
</span><span class="cx">
</span><span class="cx">         // Since 3.6 revisions include a copy of the current post data as a revision.
</span><span class="cx">         // The following removes that revision when $parent == false
</span><del>-        $parent_included = wp_first_revision_matches_current_version( $post_id );
</del><ins>+        $parent_included = _wp_last_revision_matches_current_post( $post_id );
</ins><span class="cx">         if ( $parent_included && ! $parent )
</span><span class="cx">                 array_pop( $revisions );
</span><span class="cx">         elseif ( ! $parent_included && $parent )
</span></span></pre></div>
<a id="trunkwpincludesrevisionphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/revision.php (23848 => 23849)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/revision.php        2013-03-29 05:24:12 UTC (rev 23848)
+++ trunk/wp-includes/revision.php        2013-03-29 05:28:44 UTC (rev 23849)
</span><span class="lines">@@ -63,78 +63,70 @@
</span><span class="cx"> /**
</span><span class="cx"> * Saves an already existing post as a post revision.
</span><span class="cx"> *
</span><del>- * Typically used immediately prior and 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
- * After update adds a copy of the current post as a revision, so latest revision always matches current post
</del><ins>+ * Typically used immediately after post updates.
+ * Adds a copy of the current post as a revision, so latest revision always matches current post
</ins><span class="cx"> *
</span><span class="cx"> * @package WordPress
</span><span class="cx"> * @subpackage Post_Revisions
</span><span class="cx"> * @since 2.6.0
</span><span class="cx"> *
</span><span class="cx"> * @uses _wp_put_post_revision()
</span><del>- * @uses wp_first_revision_matches_current_version()
</del><span class="cx"> *
</span><span class="cx"> * @param int $post_id The ID of the post to save as a revision.
</span><span class="cx"> * @return mixed Null or 0 if error, new revision ID, if success.
</span><span class="cx"> */
</span><span class="cx"> function wp_save_post_revision( $post_id ) {
</span><del>-        //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;
-
</del><span class="cx">         if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
</span><span class="cx">                 return;
</span><span class="cx">
</span><del>-        if ( ! $post = get_post( $post_id, ARRAY_A ) )
</del><ins>+        if ( ! $post = get_post( $post_id ) )
</ins><span class="cx">                 return;
</span><span class="cx">
</span><del>-        if ( ! wp_revisions_enabled( (object) $post ) )
</del><ins>+        if ( ! post_type_supports( $post->post_type, 'revisions' ) )
</ins><span class="cx">                 return;
</span><span class="cx">
</span><del>-        if ( 'auto-draft' == $post['post_status'] )
</del><ins>+        if ( 'auto-draft' == $post->post_status )
</ins><span class="cx">                 return;
</span><span class="cx">
</span><del>-        if ( ! post_type_supports( $post['post_type'], 'revisions' ) )
</del><ins>+        if ( ! wp_revisions_enabled( $post ) )
</ins><span class="cx">                 return;
</span><span class="cx">
</span><del>-        // compare the proposed update with the last stored revision, verify
-        // different, unless a plugin tells us to always save regardless
</del><ins>+        // Compare the proposed update with the last stored revision verifying that
+        // they are different, unless a plugin tells us to always save regardless.
+        // If no previous revisions, save one
</ins><span class="cx">         if ( $revisions = wp_get_post_revisions( $post_id ) ) {
</span><del>-                // grab the last revision
-                $last_revision = array_shift( $revisions );
</del><ins>+                // grab the last revision, but not an autosave
+                foreach ( $revisions as $revision ) {
+                        if ( false !== strpos( $revision->post_name, "{$revision->post_parent}-revision" ) ) {
+                                $last_revision = $revision;
+                                break;
+                        }
+                }
</ins><span class="cx">
</span><del>-                //if no previous revisions, save one for sure
-                if ( $last_revision_array = get_post( $last_revision->ID, ARRAY_A ) ) {
</del><ins>+                if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', true, $last_revision, $post ) ) {
+                        $post_has_changed = false;
</ins><span class="cx">
</span><del>-                        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;
-
-                                        }
</del><ins>+                        foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
+                                if ( normalize_whitespace( $post->$field ) != normalize_whitespace( $last_revision->$field ) ) {
+                                        $post_has_changed = true;
+                                        break;
</ins><span class="cx">                                 }
</span><del>-
-                                //don't save revision if post unchanged
-                                if( ! $post_has_changed )
-                                        return;
</del><span class="cx">                         }
</span><ins>+
+                        //don't save revision if post unchanged
+                        if( ! $post_has_changed )
+                                return;
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         $return = _wp_put_post_revision( $post );
</span><span class="cx">
</span><del>-        $revisions_to_keep = wp_revisions_to_keep( (object) $post );
</del><ins>+        $revisions_to_keep = wp_revisions_to_keep( $post );
</ins><span class="cx">
</span><span class="cx">         if ( $revisions_to_keep < 0 )
</span><span class="cx">                 return $return;
</span><span class="cx">
</span><del>-        // all revisions and (possibly) one autosave
</del><ins>+        // all revisions and autosaves
</ins><span class="cx">         $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) );
</span><span class="cx">
</span><span class="cx">         $delete = count($revisions) - $revisions_to_keep;
</span><span class="lines">@@ -144,9 +136,10 @@
</span><span class="cx">
</span><span class="cx">         $revisions = array_slice( $revisions, 0, $delete );
</span><span class="cx">
</span><del>-        for ( $i = 0; isset($revisions[$i]); $i++ ) {
</del><ins>+        for ( $i = 0; isset( $revisions[$i] ); $i++ ) {
</ins><span class="cx">                 if ( false !== strpos( $revisions[ $i ]->post_name, 'autosave' ) )
</span><span class="cx">                         continue;
</span><ins>+
</ins><span class="cx">                 wp_delete_post_revision( $revisions[ $i ]->ID );
</span><span class="cx">         }
</span><span class="cx">
</span><span class="lines">@@ -444,7 +437,6 @@
</span><span class="cx">         return (int) apply_filters( 'wp_revisions_to_keep', $num, $post );
</span><span class="cx"> }
</span><span class="cx">
</span><del>-
</del><span class="cx"> function _set_preview($post) {
</span><span class="cx">
</span><span class="cx">         if ( ! is_object($post) )
</span><span class="lines">@@ -464,31 +456,15 @@
</span><span class="cx">         return $post;
</span><span class="cx"> }
</span><span class="cx">
</span><del>-function _wp_get_post_revision_version( $post ) {
-        if ( is_array( $post ) ) {
-                if ( ! isset( $post['post_name'] ) ) {
-                        return false;
-                }
-
-                $name = $post['post_name'];
-        } elseif ( is_object( $post ) ) {
-                if ( ! isset( $post->post_name ) ) {
-                        return false;
-                }
-
-                $name = $post->post_name;
-        } else {
</del><ins>+function _wp_get_post_revision_version( $revision ) {
+        if ( is_object( $revision ) )
+                $revision = get_object_vars( $revision );
+        elseif ( !is_array( $revision ) )
</ins><span class="cx">                 return false;
</span><del>-        }
</del><span class="cx">
</span><del>-        if ( ! preg_match( '/^(\d+-)(?:autosave|revision)(?:-v)(\d+)$/', $name, $matches ) ) {
-                return 0;
-        }
</del><ins>+        if ( preg_match( '/^\d+-(?:autosave|revision)-v(\d+)$/', $revision['post_name'], $matches ) )
+                return (int) $matches[1];
</ins><span class="cx">
</span><del>-        if ( '1' === $matches[2] ) {
-                return 1;
-        }
-
</del><span class="cx">         return 0;
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -502,7 +478,6 @@
</span><span class="cx"> * @uses get_post()
</span><span class="cx"> * @uses post_type_supports()
</span><span class="cx"> * @uses wp_get_post_revisions()
</span><del>- * @uses wp_save_post_revision()
</del><span class="cx"> *
</span><span class="cx"> * @param int|object $post_id Post ID or post object
</span><span class="cx"> * @return true if success, false if problems
</span><span class="lines">@@ -514,55 +489,50 @@
</span><span class="cx">         if ( ! $post )
</span><span class="cx">                 return false;
</span><span class="cx">
</span><del>-        //make sure we have a current revision, only adds one if missing
-        wp_save_post_revision( $post->ID );
-
</del><span class="cx">         if ( ! post_type_supports( $post->post_type, 'revisions' ) )
</span><span class="cx">                 return false;
</span><span class="cx">
</span><span class="cx">         $revisions = wp_get_post_revisions( $post->ID ); // array( 'order' => 'DESC', 'orderby' => 'date' ); // Always work from most recent to oldest
</span><span class="cx">
</span><ins>+        if ( ! $first = reset( $revisions ) )
+                return true;
</ins><span class="cx">
</span><del>-        if ( ! $revisions )
</del><ins>+        // Check if the revisions have already been updated
+        if ( preg_match( '/^\d+-(?:autosave|revision)-v\d+$/', $first->post_name ) )
</ins><span class="cx">                 return true;
</span><span class="cx">
</span><span class="cx">         // Add post option exclusively
</span><del>-        $lock = "revision-upgrade-{$post->ID}";
-        $locked_at = number_format( microtime( true ), 10, '.', '' );
-        $result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $locked_at ) );
</del><ins>+        $lock = "revision-upgrade-{$post->ID}";
+        $now = time();
+        $result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $now ) );
</ins><span class="cx">         if ( ! $result ) {
</span><span class="cx">                 // If we couldn't get a lock, see how old the previous lock is
</span><del>-                $locked_at = get_option( $lock );
-                if ( !$locked_at ) {
</del><ins>+                $locked = get_option( $lock );
+                if ( ! $locked ) {
</ins><span class="cx">                         // Can't write to the lock, and can't read the lock.
</span><span class="cx">                         // Something broken has happened
</span><span class="cx">                         return false;
</span><span class="cx">                 }
</span><span class="cx">
</span><del>-                if ( $lock_at < number_format( microtime( true ), 10, '.', '' ) - 3600 ) {
-                        // Lock is too old - try again
-                        delete_option( $lock );
-                        return wp_upgrade_revisions_of_post( $post );
</del><ins>+                if ( $locked > $now - 3600 ) {
+                        // Lock is not too old: some other process may be upgrading this post. Bail.
+                        return false;
</ins><span class="cx">                 }
</span><span class="cx">
</span><del>-                // Lock is not too old: some other process may be upgrading this post. Bail.
-                return;
-        } else {
-                // If we could get a lock, re-"add" the option to fire all the correct filters.
-                add_option( $lock, $locked_at );
</del><ins>+                // Lock is too old - update it (below) and continue
</ins><span class="cx">         }
</span><span class="cx">
</span><del>-        $success = true;
</del><ins>+        // If we could get a lock, re-"add" the option to fire all the correct filters.
+        update_option( $lock, $now );
</ins><span class="cx">
</span><span class="cx">         reset( $revisions );
</span><ins>+
</ins><span class="cx">         do {
</span><span class="cx">                 $this_revision = current( $revisions );
</span><span class="cx">                 $prev_revision = next( $revisions );
</span><span class="cx">
</span><span class="cx">                 $this_revision_version = _wp_get_post_revision_version( $this_revision );
</span><span class="cx">
</span><del>-                error_log($this_revision_version);
-
</del><span class="cx">                 // Something terrible happened
</span><span class="cx">                 if ( false === $this_revision_version )
</span><span class="cx">                         continue;
</span><span class="lines">@@ -571,37 +541,34 @@
</span><span class="cx">                 if ( 0 < $this_revision_version )
</span><span class="cx">                         continue;
</span><span class="cx">
</span><del>-                // This revision is the oldest revision of the post.
-                // The correct post_author is probably $post->post_author, but that's only a good guess.
-                // Leave un-upgraded.
-                if ( ! $prev_revision ) {
-                        continue;
-                }
</del><ins>+                // Always update the revision version
+                $update = array(
+                        'post_name' => preg_replace( '/^(\d+-(?:autosave|revision))[\d-]*$/', '$1-v1', $this_revision->post_name ),
+                );
</ins><span class="cx">
</span><del>-                $prev_revision_version = _wp_get_post_revision_version( $prev_revision );
</del><ins>+                // 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 );
</ins><span class="cx">
</span><del>-                // If the previous revision is already up to date, it no longer has the information we need :(
-                if ( 0 < $prev_revision_version ) {
-                        continue;
</del><ins>+                        // If the previous revision is already up to date, it no longer has the information we need :(
+                        if ( $prev_revision_version < 1 )
+                                $update['post_author'] = $prev_revision->post_author;
</ins><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 // Upgrade this revision
</span><del>-                // Cast as object so that wp_update_post() handles slashing for us
-                $update = (object) array(
-                        'ID' => $this_revision->ID,
-                        'post_name' => preg_replace( '/^(\d+-)(autosave|revision)-(\d+)$/', '$1$2-v1', $this_revision->post_name ),
-                        '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;
-                }
</del><ins>+                $result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) );
+
+                if ( $result )
+                        wp_cache_delete( $this_revision->ID, 'posts' );
+
</ins><span class="cx">         } while ( $prev_revision );
</span><span class="cx">
</span><span class="cx">         delete_option( $lock );
</span><ins>+
+        // Add a copy of the post as latest revision.
+        wp_save_post_revision( $post->ID );
</ins><span class="cx">         return true;
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -628,20 +595,37 @@
</span><span class="cx"> * @param int|object $post Post ID or post object.
</span><span class="cx"> * @return bool false if not a match, otherwise true.
</span><span class="cx"> */
</span><del>-function wp_first_revision_matches_current_version( $post ) {
-
</del><ins>+function _wp_last_revision_matches_current_post( $post ) {
</ins><span class="cx">         if ( ! $post = get_post( $post ) )
</span><span class="cx">                 return false;
</span><span class="cx">
</span><span class="cx">         if ( ! $revisions = wp_get_post_revisions( $post->ID ) )
</span><span class="cx">                 return false;
</span><span class="cx">
</span><del>-        $last_revision = array_shift( $revisions );
</del><ins>+        foreach ( $revisions as $revision ) {
+                if ( false !== strpos( $revision->post_name, "{$revision->post_parent}-revision" ) ) {
+                        $last_revision = $revision;
+                        break;
+                }
+        }
</ins><span class="cx">
</span><del>-        if ( ! ($last_revision->post_modified == $post->post_modified ) )
</del><ins>+        // No revisions yet, only autosaves
+        if ( ! isset( $last_revision ) )
</ins><span class="cx">                 return false;
</span><span class="cx">
</span><del>-        return true;
</del><ins>+        $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;
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><span class="lines">@@ -706,4 +690,4 @@
</span><span class="cx">         $r .= "</table>";
</span><span class="cx">
</span><span class="cx">         return array( 'html' => $r, 'linesadded' => $linesadded, 'linesdeleted' => $linesdeleted );
</span><del>-        }
</del><ins>+}
</ins></span></pre>
</div>
</div>
</body>
</html>