Changeset 23823 for trunk/wp-includes/revision.php
- Timestamp:
- 03/27/2013 08:21:38 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/revision.php
r23818 r23823 53 53 $return['post_status'] = 'inherit'; 54 54 $return['post_type'] = 'revision'; 55 $return['post_name'] = $autosave ? "$post[ID]-autosave " : "$post[ID]-revision";55 $return['post_name'] = $autosave ? "$post[ID]-autosave-v1" : "$post[ID]-revision-v1"; // "1" is the revisioning system version 56 56 $return['post_date'] = isset($post['post_modified']) ? $post['post_modified'] : ''; 57 57 $return['post_date_gmt'] = isset($post['post_modified_gmt']) ? $post['post_modified_gmt'] : ''; 58 $return['post_author'] = get_post_meta( $post['ID'], '_edit_last', true ); 58 59 59 60 return $return; … … 63 64 * Saves an already existing post as a post revision. 64 65 * 65 * Typically used immediately prior to post updates. 66 * Typically used immediately prior and after post updates. 67 * 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 68 * After update adds a copy of the current post as a revision, so latest revision always matches current post 66 69 * 67 70 * @package WordPress … … 70 73 * 71 74 * @uses _wp_put_post_revision() 75 * @uses wp_first_revision_matches_current_version() 72 76 * 73 77 * @param int $post_id The ID of the post to save as a revision. 74 78 * @return mixed Null or 0 if error, new revision ID, if success. 75 79 */ 76 function wp_save_post_revision( $post_id, $new_data = null ) { 77 // We do autosaves manually with wp_create_post_autosave() 80 function wp_save_post_revision( $post_id ) { 81 //check to see if the post's first revision already matches the post data 82 //should be true before post update, _except_ for old data which 83 //doesn't include a copy of the current post data in revisions 84 if ( wp_first_revision_matches_current_version( $post_id ) ) 85 return; 86 78 87 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 79 88 return; … … 91 100 return; 92 101 93 // if new data is supplied, check that it is different from last saved revision, unless a plugin tells us to always save regardless 94 if ( apply_filters( 'wp_save_post_revision_check_for_changes', true, $post, $new_data ) && is_array( $new_data ) ) { 95 $post_has_changed = false; 96 foreach ( array_keys( _wp_post_revision_fields() ) as $field ) { 97 if ( normalize_whitespace( $new_data[ $field ] ) != normalize_whitespace( $post[ $field ] ) ) { 98 $post_has_changed = true; 99 break; 102 // compare the proposed update with the last stored revision, verify 103 // different, unless a plugin tells us to always save regardless 104 if ( $revisions = wp_get_post_revisions( $post_id ) ) { 105 // grab the last revision 106 $last_revision = array_shift( $revisions ); 107 108 //if no previous revisions, save one for sure 109 if ( $last_revision_array = get_post( $last_revision->ID, ARRAY_A ) ) { 110 111 if ( apply_filters( 'wp_save_post_revision_check_for_changes', true, $last_revision_array, $post ) && is_array( $post ) ) { 112 $post_has_changed = false; 113 114 foreach ( array_keys( _wp_post_revision_fields() ) as $field ) { 115 116 if ( normalize_whitespace( $post[ $field ] ) != normalize_whitespace( $last_revision_array[ $field ] ) ) { 117 $post_has_changed = true; 118 break; 119 120 } 121 } 122 123 //don't save revision if post unchanged 124 if( ! $post_has_changed ) 125 return; 100 126 } 101 127 } 102 //don't save revision if post unchanged103 if( ! $post_has_changed )104 return;105 128 } 106 129 … … 123 146 124 147 for ( $i = 0; isset($revisions[$i]); $i++ ) { 125 if ( false !== strpos( $revisions[ $i]->post_name, 'autosave' ) )148 if ( false !== strpos( $revisions[ $i ]->post_name, 'autosave' ) ) 126 149 continue; 127 wp_delete_post_revision( $revisions[ $i]->ID );150 wp_delete_post_revision( $revisions[ $i ]->ID ); 128 151 } 129 152 … … 441 464 return $post; 442 465 } 466 467 function _wp_get_post_revision_version( $post ) { 468 if ( is_array( $post ) ) { 469 if ( ! isset( $post['post_name'] ) ) { 470 return false; 471 } 472 473 $name = $post['post_name']; 474 } elseif ( is_object( $post ) ) { 475 if ( ! isset( $post->post_name ) ) { 476 return false; 477 } 478 479 $name = $post->post_name; 480 } else { 481 return false; 482 } 483 484 if ( ! preg_match( '/^(\d+-)(?:autosave|revision)(?:-v)(\d+)$/', $name, $matches ) ) { 485 return 0; 486 } 487 488 if ( '1' === $matches[2] ) { 489 return 1; 490 } 491 492 return 0; 493 } 494 495 /** 496 * Upgrade the data 497 * 498 * @package WordPress 499 * @subpackage Post_Revisions 500 * @since 3.6.0 501 * 502 * @uses get_post() 503 * @uses post_type_supports() 504 * @uses wp_get_post_revisions() 505 * @uses wp_save_post_revision() 506 * 507 * @param int|object $post_id Post ID or post object 508 * @return true if success, false if problems 509 */ 510 function _wp_upgrade_revisions_of_post( $post ) { 511 global $wpdb; 512 513 $post = get_post( $post ); 514 if ( ! $post ) 515 return false; 516 517 //make sure we have a current revision, only adds one if missing 518 wp_save_post_revision( $post->ID ); 519 520 if ( ! post_type_supports( $post->post_type, 'revisions' ) ) 521 return false; 522 523 $revisions = wp_get_post_revisions( $post->ID ); // array( 'order' => 'DESC', 'orderby' => 'date' ); // Always work from most recent to oldest 524 525 526 if ( ! $revisions ) 527 return true; 528 529 // Add post option exclusively 530 $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 ) ); 533 if ( ! $result ) { 534 // If we couldn't get a lock, see how old the previous lock is 535 $locked_at = get_option( $lock ); 536 if ( !$locked_at ) { 537 // Can't write to the lock, and can't read the lock. 538 // Something broken has happened 539 return false; 540 } 541 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 ); 546 } 547 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 ); 553 } 554 555 $success = true; 556 557 reset( $revisions ); 558 do { 559 $this_revision = current( $revisions ); 560 $prev_revision = next( $revisions ); 561 562 $this_revision_version = _wp_get_post_revision_version( $this_revision ); 563 564 error_log($this_revision_version); 565 566 // Something terrible happened 567 if ( false === $this_revision_version ) 568 continue; 569 570 // 1 is the latest revision version, so we're already up to date 571 if ( 0 < $this_revision_version ) 572 continue; 573 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 } 580 581 $prev_revision_version = _wp_get_post_revision_version( $prev_revision ); 582 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; 586 } 587 588 // 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 ) ) { 598 // Wilhelm! 599 $success = false; 600 break; 601 } 602 } while ( $prev_revision ); 603 604 delete_option( $lock ); 605 return true; 606 } 607 443 608 444 609 function _show_post_preview() {
Note: See TracChangeset
for help on using the changeset viewer.