Ticket #16215: 16215.11.diff
File 16215.11.diff, 11.0 KB (added by , 12 years ago) |
---|
-
wp-includes/default-filters.php
250 250 add_action( 'plugins_loaded', 'wp_maybe_load_widgets', 0 ); 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 ); 256 256 add_action( 'transition_post_status', '_update_term_count_on_transition_post_status', 10, 3 ); -
wp-includes/post.php
3012 3012 * @return string unique slug for the post, based on $post_name (with a -1, -2, etc. suffix) 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 3018 3018 global $wpdb, $wp_rewrite; -
wp-includes/revision.php
52 52 $return['post_parent'] = $post['ID']; 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; 60 61 } … … 62 63 /** 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 68 71 * @subpackage Post_Revisions 69 72 * @since 2.6.0 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; 80 89 … … 82 91 if ( ! WP_POST_REVISIONS ) 83 92 return; 84 93 85 if ( ! $post = get_post( $post_id, ARRAY_A ) )94 if ( ! $post = get_post( $post_id, ARRAY_A ) ) 86 95 return; 87 96 88 97 if ( 'auto-draft' == $post['post_status'] ) 89 98 return; 90 99 91 if ( ! post_type_supports($post['post_type'], 'revisions') )100 if ( ! post_type_supports( $post['post_type'], 'revisions' ) ) 92 101 return; 93 102 94 // if new data is supplied, check that it is different from last saved revision, unless a plugin tells us to always save regardless 95 if ( apply_filters( 'wp_save_post_revision_check_for_changes', true, $post, $new_data ) && is_array( $new_data ) ) { 96 $post_has_changed = false; 97 foreach ( array_keys( _wp_post_revision_fields() ) as $field ) { 98 if ( normalize_whitespace( $new_data[ $field ] ) != normalize_whitespace( $post[ $field ] ) ) { 99 $post_has_changed = true; 100 break; 103 // compare the proposed update with the last stored revision, verify 104 // different, unless a plugin tells us to always save regardless 105 if ( $revisions = wp_get_post_revisions( $post_id ) ) { // grab the last revision 106 $last_revision = array_shift( $revisions ); 107 108 if ( $last_revision_array = get_post( $last_revision->ID, ARRAY_A ) ) { //if no previous revisions, save one for sure 109 110 if ( apply_filters( 'wp_save_post_revision_check_for_changes', true, $last_revision_array, $post ) && is_array( $post ) ) { 111 $post_has_changed = false; 112 113 foreach ( array_keys( _wp_post_revision_fields() ) as $field ) { 114 115 if ( normalize_whitespace( $post[ $field ] ) != normalize_whitespace( $last_revision_array[ $field ] ) ) { 116 $post_has_changed = true; 117 break; 118 119 } 120 } 121 122 //don't save revision if post unchanged 123 if( ! $post_has_changed ) 124 return; 101 125 } 102 126 } 103 //don't save revision if post unchanged104 if( ! $post_has_changed )105 return;106 127 } 107 128 108 129 $return = _wp_put_post_revision( $post ); 109 130 110 131 // WP_POST_REVISIONS = true (default), -1 111 if ( ! is_numeric( WP_POST_REVISIONS ) || WP_POST_REVISIONS < 0 )132 if ( ! is_numeric( WP_POST_REVISIONS ) || WP_POST_REVISIONS < 0 ) 112 133 return $return; 113 134 114 135 // all revisions and (possibly) one autosave 115 136 $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) ); 116 137 117 138 // WP_POST_REVISIONS = (int) (# of autosaves to save) 118 $delete = count( $revisions) - WP_POST_REVISIONS;139 $delete = count( $revisions ) - WP_POST_REVISIONS; 119 140 120 141 if ( $delete < 1 ) 121 142 return $return; … … 123 144 $revisions = array_slice( $revisions, 0, $delete ); 124 145 125 146 for ( $i = 0; isset($revisions[$i]); $i++ ) { 126 if ( false !== strpos( $revisions[ $i]->post_name, 'autosave' ) )147 if ( false !== strpos( $revisions[ $i ]->post_name, 'autosave' ) ) 127 148 continue; 128 wp_delete_post_revision( $revisions[ $i]->ID );149 wp_delete_post_revision( $revisions[ $i ]->ID ); 129 150 } 130 151 131 152 return $return; … … 400 421 return $post; 401 422 } 402 423 424 function _wp_get_post_revision_version( $post ) { 425 if ( is_array( $post ) ) { 426 if ( ! isset( $post['post_name'] ) ) { 427 return false; 428 } 429 430 $name = $post['post_name']; 431 } elseif ( is_object( $post ) ) { 432 if ( ! isset( $post->post_name ) ) { 433 return false; 434 } 435 436 $name = $post->post_name; 437 } else { 438 return false; 439 } 440 441 if ( ! preg_match( '/^([\d-]+)(?:autosave|revision)(?:-v)([\d-]+)$/', $name, $matches ) ) { 442 return 0; 443 } 444 445 if ( '1' === $matches[2] ) { 446 return 1; 447 } 448 449 return 0; 450 } 451 452 /** 453 * Upgrade the data 454 * 455 * @package WordPress 456 * @subpackage Post_Revisions 457 * @since 3.6.0 458 * 459 * @uses get_post() 460 * @uses post_type_supports() 461 * @uses wp_get_post_revisions() 462 * @uses wp_save_post_revision() 463 * 464 * @param int|object $post_id Post ID or post object 465 * @return true if success, false if problems 466 */ 467 function _wp_upgrade_revisions_of_post( $post ) { 468 global $wpdb; 469 470 $post = get_post( $post ); 471 if ( ! $post ) 472 return false; 473 474 //make sure we have a current revision, only adds one if missing 475 wp_save_post_revision( $post->ID ); 476 477 if ( ! post_type_supports( $post->post_type, 'revisions' ) ) 478 return false; 479 480 $revisions = wp_get_post_revisions( $post->ID ); // array( 'order' => 'DESC', 'orderby' => 'date' ); // Always work from most recent to oldest 481 482 483 if ( ! $revisions ) 484 return true; 485 486 // Add post option exclusively 487 $lock = "revision-upgrade-{$post->ID}"; 488 $locked_at = number_format( microtime( true ), 10, '.', '' ); 489 $result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $locked_at ) ); 490 if ( ! $result ) { 491 // If we couldn't get a lock, see how old the previous lock is 492 $locked_at = get_option( $lock ); 493 if ( !$locked_at ) { 494 // Can't write to the lock, and can't read the lock. 495 // Something broken has happened 496 return false; 497 } 498 499 if ( $lock_at < number_format( microtime( true ), 10, '.', '' ) - 3600 ) { 500 // Lock is too old - try again 501 delete_option( $lock ); 502 return wp_upgrade_revisions_of_post( $post ); 503 } 504 505 // Lock is not too old: some other process may be upgrading this post. Bail. 506 return; 507 } else { 508 // If we could get a lock, re-"add" the option to fire all the correct filters. 509 add_option( $lock, $locked_at ); 510 } 511 512 $success = true; 513 514 reset( $revisions ); 515 do { 516 $this_revision = current( $revisions ); 517 $prev_revision = next( $revisions ); 518 519 $this_revision_version = _wp_get_post_revision_version( $this_revision ); 520 521 error_log($this_revision_version); 522 523 // Something terrible happened 524 if ( false === $this_revision_version ) 525 continue; 526 527 // 1 is the latest revision version, so we're already up to date 528 if ( 0 < $this_revision_version ) 529 continue; 530 531 // This revision is the oldest revision of the post. 532 // The correct post_author is probably $post->post_author, but that's only a good guess. 533 // Leave un-upgraded. 534 if ( ! $prev_revision ) { 535 continue; 536 } 537 538 $prev_revision_version = _wp_get_post_revision_version( $prev_revision ); 539 540 // If the previous revision is already up to date, it no longer has the information we need :( 541 if ( 0 < $prev_revision_version ) { 542 continue; 543 } 544 545 // Upgrade this revision 546 // Cast as object so that wp_update_post() handles slashing for us 547 $update = (object) array( 548 'ID' => $this_revision->ID, 549 'post_name' => preg_replace( '/^([\d-]+)(autosave|revision)-([\d-]+)$/', '$1$2-v1', $this_revision->post_name ), 550 'post_author' => $prev_revision->post_author, 551 ); 552 //error_log(json_encode($update)); 553 $result = wp_update_post( $update ); 554 if ( ! $result || is_wp_error( $result ) ) { 555 // Wilhelm! 556 $success = false; 557 break; 558 } 559 } while ( $prev_revision ); 560 561 delete_option( $lock ); 562 return true; 563 } 564 565 403 566 function _show_post_preview() { 404 567 405 568 if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) { -
wp-admin/post.php
135 135 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?') ); 140 141 … … 153 154 exit(); 154 155 } 155 156 157 //upgrade any old bad revision data (#16215) 158 _wp_upgrade_revisions_of_post( $p ); 159 156 160 $post_type = $post->post_type; 157 161 if ( 'post' == $post_type ) { 158 162 $parent_file = "edit.php";