WordPress.org

Make WordPress Core

Ticket #16215: 16215-13.patch

File 16215-13.patch, 4.4 KB (added by azaozz, 5 years ago)
  • wp-includes/revision.php

     
    514514        if ( ! $post )
    515515                return false;
    516516
    517         //make sure we have a current revision, only adds one if missing
    518         wp_save_post_revision( $post->ID );
    519 
    520517        if ( ! post_type_supports( $post->post_type, 'revisions' ) )
    521518                return false;
    522519
    523520        $revisions = wp_get_post_revisions( $post->ID ); // array( 'order' => 'DESC', 'orderby' => 'date' ); // Always work from most recent to oldest
    524521
     522        if ( ! $first = reset( $revisions ) )
     523                return true;
    525524
    526         if ( ! $revisions )
     525        // Check if the revisions have already been updated
     526        if ( preg_match( '/^\d+-(?:autosave|revision)-v\d+$/', $first->post_name ) )
    527527                return true;
    528528
    529529        // Add post option exclusively
    530530        $lock      = "revision-upgrade-{$post->ID}";
    531         $locked_at = number_format( microtime( true ), 10, '.', '' );
    532         $result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $locked_at ) );
     531        $now = number_format( microtime( true ), 10, '.', '' );
     532        $result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $now ) );
    533533        if ( ! $result ) {
    534534                // If we couldn't get a lock, see how old the previous lock is
    535                 $locked_at = get_option( $lock );
    536                 if ( !$locked_at ) {
     535                $locked = get_option( $lock );
     536                if ( ! $locked ) {
    537537                        // Can't write to the lock, and can't read the lock.
    538538                        // Something broken has happened
    539539                        return false;
    540540                }
    541541
    542                 if ( $lock_at < number_format( microtime( true ), 10, '.', '' ) - 3600 ) {
    543                         // Lock is too old - try again
    544                         delete_option( $lock );
    545                         return wp_upgrade_revisions_of_post( $post );
     542                if ( $locked > $now - 3600 ) {
     543                        // Lock is not too old: some other process may be upgrading this post.  Bail.
     544                        return false;
    546545                }
    547546
    548                 // Lock is not too old: some other process may be upgrading this post.  Bail.
    549                 return;
    550         } else {
    551                 // If we could get a lock, re-"add" the option to fire all the correct filters.
    552                 add_option( $lock, $locked_at );
     547                // Lock is too old - update it (below) and continue
    553548        }
    554549
     550        // If we could get a lock, re-"add" the option to fire all the correct filters.
     551        update_option( $lock, $now );
     552
    555553        $success = true;
    556554
    557555        reset( $revisions );
     
    571569                if ( 0 < $this_revision_version )
    572570                        continue;
    573571
    574                 // This revision is the oldest revision of the post.
    575                 // The correct post_author is probably $post->post_author, but that's only a good guess.
    576                 // Leave un-upgraded.
    577                 if ( ! $prev_revision ) {
    578                         continue;
    579                 }
     572                // Always update the revision version
     573                $update = array(
     574                        'post_name' => preg_replace( '/^(\d+-(?:autosave|revision))[\d-]*$/', '$1-v1', $this_revision->post_name ),
     575                );
    580576
    581                 $prev_revision_version = _wp_get_post_revision_version( $prev_revision );
     577                // If this revision is the oldest revision of the post, i.e. no $prev_revision,
     578                // the correct post_author is probably $post->post_author, but that's only a good guess.
     579                // Update the revision version only and Leave the author as-is
     580                if ( $prev_revision ) {
     581                        $prev_revision_version = _wp_get_post_revision_version( $prev_revision );
    582582
    583                 // If the previous revision is already up to date, it no longer has the information we need :(
    584                 if ( 0 < $prev_revision_version ) {
    585                         continue;
     583                        // If the previous revision is already up to date, it no longer has the information we need :(
     584                        if ( $prev_revision_version < 1 )
     585                                $update['post_author'] = $prev_revision->post_author;
    586586                }
    587587
    588588                // Upgrade this revision
    589                 // Cast as object so that wp_update_post() handles slashing for us
    590                 $update = (object) array(
    591                         'ID'          => $this_revision->ID,
    592                         'post_name'   => preg_replace( '/^(\d+-)(autosave|revision)-(\d+)$/', '$1$2-v1', $this_revision->post_name ),
    593                         'post_author' => $prev_revision->post_author,
    594                 );
    595                 //error_log(json_encode($update));
    596                 $result = wp_update_post( $update );
    597                 if ( ! $result || is_wp_error( $result ) ) {
     589                $result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) );
     590
     591                if ( $result ) {
     592                        wp_cache_delete( $this_revision->ID, 'posts' );
     593                } else {
    598594                        // Wilhelm!
    599595                        $success = false;
    600596                        break;
     
    602598        } while ( $prev_revision );
    603599
    604600        delete_option( $lock );
    605         return true;
     601        return $success;
    606602}
    607603
    608604