Ticket #16215: 16215.10.diff
File 16215.10.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( 'pre_post_update', 'wp_save_post_revision', 10, 2 ); 253 add_action( 'pre_post_update', 'wp_save_post_revision', 10, 1 ); 254 add_action( 'post_updated', 'wp_save_post_revision', 10, 1 ); 254 255 add_action( 'publish_post', '_publish_post_hook', 5, 1 ); 255 256 add_action( 'transition_post_status', '_transition_post_status', 5, 3 ); 256 257 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 * 463 * @param int|object $post_id Post ID or post object 464 * @return true if success, false if problems 465 */ 466 function _wp_upgrade_revisions_of_post( $post ) { 467 global $wpdb; 468 469 $post = get_post( $post ); 470 if ( ! $post ) 471 return false; 472 473 if ( ! post_type_supports( $post->post_type, 'revisions' ) ) 474 return false; 475 476 $revisions = wp_get_post_revisions( $post->ID ); // array( 'order' => 'DESC', 'orderby' => 'date' ); // Always work from most recent to oldest 477 478 479 if ( ! $revisions ) 480 return true; 481 482 // Add post option exclusively 483 $lock = "revision-upgrade-{$post->ID}"; 484 $locked_at = number_format( microtime( true ), 10, '.', '' ); 485 $result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $locked_at ) ); 486 if ( ! $result ) { 487 // If we couldn't get a lock, see how old the previous lock is 488 $locked_at = get_option( $lock ); 489 if ( !$locked_at ) { 490 // Can't write to the lock, and can't read the lock. 491 // Something broken has happened 492 return false; 493 } 494 495 if ( $lock_at < number_format( microtime( true ), 10, '.', '' ) - 3600 ) { 496 // Lock is too old - try again 497 delete_option( $lock ); 498 return wp_upgrade_revisions_of_post( $post ); 499 } 500 501 // Lock is not too old: some other process may be upgrading this post. Bail. 502 return; 503 } else { 504 // If we could get a lock, re-"add" the option to fire all the correct filters. 505 add_option( $lock, $locked_at ); 506 } 507 508 $success = true; 509 510 reset( $revisions ); 511 do { 512 $this_revision = current( $revisions ); 513 $prev_revision = next( $revisions ); 514 515 //error_log($this_revision->post_name ); 516 517 518 $this_revision_version = _wp_get_post_revision_version( $this_revision ); 519 520 error_log($this_revision_version); 521 522 // Something terrible happened 523 if ( false === $this_revision_version ) 524 continue; 525 526 // 1 is the latest revision version, so we're already up to date 527 if ( 0 < $this_revision_version ) 528 continue; 529 530 // This revision is the oldest revision of the post. 531 // The correct post_author is probably $post->post_author, but that's only a good guess. 532 // Leave un-upgraded. 533 if ( ! $prev_revision ) { 534 continue; 535 } 536 537 $prev_revision_version = _wp_get_post_revision_version( $prev_revision ); 538 539 // If the previous revision is already up to date, it no longer has the information we need :( 540 if ( 0 < $prev_revision_version ) { 541 continue; 542 } 543 544 // Upgrade this revision 545 // Cast as object so that wp_update_post() handles slashing for us 546 $update = (object) array( 547 'ID' => $this_revision->ID, 548 'post_name' => preg_replace( '/^([\d-]+)(autosave|revision)-([\d-]+)$/', '$1$2-v1', $this_revision->post_name ), 549 'post_author' => $prev_revision->post_author, 550 ); 551 //error_log(json_encode($update)); 552 $result = wp_update_post( $update ); 553 if ( ! $result || is_wp_error( $result ) ) { 554 // Wilhelm! 555 $success = false; 556 break; 557 } 558 } while ( $prev_revision ); 559 560 delete_option( $lock ); 561 return true; 562 } 563 564 403 565 function _show_post_preview() { 404 566 405 567 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";