Changeset 23823
- Timestamp:
- 03/27/2013 08:21:38 PM (12 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-admin/post.php
r23693 r23823 136 136 $p = $post_id; 137 137 138 138 139 if ( empty($post->ID) ) 139 140 wp_die( __('You attempted to edit an item that doesn’t exist. Perhaps it was deleted?') ); … … 153 154 exit(); 154 155 } 156 157 //upgrade any old bad revision data (#16215) 158 _wp_upgrade_revisions_of_post( $p ); 155 159 156 160 $post_type = $post->post_type; -
trunk/wp-includes/default-filters.php
r23805 r23823 251 251 add_action( 'plugins_loaded', 'wp_maybe_load_embeds', 0 ); 252 252 add_action( 'shutdown', 'wp_ob_end_flush_all', 1 ); 253 add_action( 'p re_post_update', 'wp_save_post_revision', 10, 2);253 add_action( 'post_updated', 'wp_save_post_revision', 10, 1 ); 254 254 add_action( 'publish_post', '_publish_post_hook', 5, 1 ); 255 255 add_action( 'transition_post_status', '_transition_post_status', 5, 3 ); -
trunk/wp-includes/post.php
r23740 r23823 3013 3013 */ 3014 3014 function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) { 3015 if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) )3015 if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) || ( 'inherit' == $post_status && 'revision' == $post_type ) ) 3016 3016 return $slug; 3017 3017 -
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.