Make WordPress Core

Ticket #16215: 16215.9.diff

File 16215.9.diff, 19.3 KB (added by adamsilverstein, 11 years ago)
  • wp-includes/default-filters.php

     
    250250add_action( 'plugins_loaded',             'wp_maybe_load_widgets',                    0    );
    251251add_action( 'plugins_loaded',             'wp_maybe_load_embeds',                     0    );
    252252add_action( 'shutdown',                   'wp_ob_end_flush_all',                      1    );
    253 add_action( 'pre_post_update',            'wp_save_post_revision',                   10, 2 );
     253add_action( 'pre_post_update',            '_wp_upgrade_revisions_of_post',           11, 1 );
     254add_action( 'pre_post_update',            'wp_save_post_revision',                   10, 1 );
     255add_action( 'post_updated',               'wp_save_post_revision',                   10, 1 );
    254256add_action( 'publish_post',               '_publish_post_hook',                       5, 1 );
    255257add_action( 'transition_post_status',     '_transition_post_status',                  5, 3 );
    256258add_action( 'transition_post_status',     '_update_term_count_on_transition_post_status', 10, 3 );
  • wp-includes/post-template.php

     
    13451345        if ( !in_array( $revision->post_type, array( 'post', 'page', 'revision' ) ) )
    13461346                return false;
    13471347
    1348         $author = get_the_author_meta( 'display_name', $revision->post_author );
     1348        $author = get_the_corrected_modified_author( $revision->ID );
    13491349        /* translators: revision date format, see http://php.net/date */
    13501350        $datef = _x( 'j F, Y @ G:i:s', 'revision date format');
    13511351
    1352         $gravatar = get_avatar( $revision->post_author, 24 );
     1352        $gravatar = get_avatar( $author['modified_author_id'], 24 );
    13531353
    13541354        $date = date_i18n( $datef, strtotime( $revision->post_modified ) );
    13551355        if ( $link && current_user_can( 'edit_post', $revision->ID ) && $link = get_edit_post_link( $revision->ID ) )
     
    13591359                /* translators: post revision title: 1: author avatar, 2: author name, 3: time ago, 4: date */
    13601360                _x( '%1$s %2$s, %3$s ago (%4$s)', 'post revision title' ),
    13611361                $gravatar,
    1362                 $author,
     1362                $author['modified_author_display_name'],
    13631363                human_time_diff( strtotime( $revision->post_modified ), current_time( 'timestamp' ) ),
    13641364                $date
    13651365        );
     
    14551455
    14561456        }
    14571457
    1458         if ( 'form-table' == $format ) : ?>
    1459 
    1460 <form action="revision.php" method="get">
    1461 
    1462 <div class="tablenav">
    1463         <div class="alignleft">
    1464                 <input type="submit" class="button-secondary" value="<?php esc_attr_e( 'Compare Revisions' ); ?>" />
    1465                 <input type="hidden" name="action" value="diff" />
    1466                 <input type="hidden" name="post_type" value="<?php echo esc_attr($post->post_type); ?>" />
    1467         </div>
    1468 </div>
    1469 
    1470 <br class="clear" />
    1471 
    1472 <table class="widefat post-revisions" cellspacing="0" id="post-revisions">
    1473         <col />
    1474         <col />
    1475         <col style="width: 33%" />
    1476         <col style="width: 33%" />
    1477         <col style="width: 33%" />
    1478 <thead>
    1479 <tr>
    1480         <th scope="col"><?php /* translators: column name in revisions */ _ex( 'Old', 'revisions column name' ); ?></th>
    1481         <th scope="col"><?php /* translators: column name in revisions */ _ex( 'New', 'revisions column name' ); ?></th>
    1482         <th scope="col"><?php /* translators: column name in revisions */ _ex( 'Date Created', 'revisions column name' ); ?></th>
    1483         <th scope="col"><?php _e( 'Author' ); ?></th>
    1484         <th scope="col" class="action-links"><?php _e( 'Actions' ); ?></th>
    1485 </tr>
    1486 </thead>
    1487 <tbody>
    1488 
    1489 <?php echo $rows; ?>
    1490 
    1491 </tbody>
    1492 </table>
    1493 
    1494 </form>
    1495 
    1496 <?php
    1497         else :
    14981458                echo "<ul class='post-revisions'>\n";
    14991459                echo $rows;
    15001460
     
    15241484                echo "</ul>";
    15251485                }
    15261486
    1527         endif;
    1528 
    15291487}
  • wp-includes/post.php

     
    28222822                $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
    28232823        }
    28242824
     2825        if ( 'revision' !== $post_type )
     2826                update_post_meta( $post_ID, '_edit_last', $user_ID );
     2827
    28252828        if ( is_object_in_taxonomy($post_type, 'category') )
    28262829                wp_set_post_categories( $post_ID, $post_category );
    28272830
  • wp-includes/revision.php

     
    5252        $return['post_parent']   = $post['ID'];
    5353        $return['post_status']   = 'inherit';
    5454        $return['post_type']     = 'revision';
    55         $return['post_name']     = $autosave ? "$post[ID]-autosave" : "$post[ID]-revision";
     55        $return['post_name']     = $autosave ? "$post[ID]-1-autosave" : "$post[ID]-1-revision"; // "1" is the revisioning system version
    5656        $return['post_date']     = isset($post['post_modified']) ? $post['post_modified'] : '';
    5757        $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 );
    5859
    5960        return $return;
    6061}
     
    6263/**
    6364 * Saves an already existing post as a post revision.
    6465 *
    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
    6669 *
    6770 * @package WordPress
    6871 * @subpackage Post_Revisions
    6972 * @since 2.6.0
    7073 *
    7174 * @uses _wp_put_post_revision()
     75 * @uses wp_first_revision_matches_current_version()
    7276 *
    7377 * @param int $post_id The ID of the post to save as a revision.
    7478 * @return mixed Null or 0 if error, new revision ID, if success.
    7579 */
    76 function wp_save_post_revision( $post_id, $new_data = null ) {
    77         // We do autosaves manually with wp_create_post_autosave()
     80function 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
    7887        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    7988                return;
    8089
     
    8291        if ( ! WP_POST_REVISIONS )
    8392                return;
    8493
    85         if ( !$post = get_post( $post_id, ARRAY_A ) )
     94        if ( ! $post = get_post( $post_id, ARRAY_A ) )
    8695                return;
    8796
    8897        if ( 'auto-draft' == $post['post_status'] )
    8998                return;
    9099
    91         if ( !post_type_supports($post['post_type'], 'revisions') )
     100        if ( ! post_type_supports( $post['post_type'], 'revisions' ) )
    92101                return;
    93102
    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;
    101125                        }
    102126                }
    103                 //don't save revision if post unchanged
    104                 if( ! $post_has_changed )
    105                         return;
    106127        }
    107128
    108129        $return = _wp_put_post_revision( $post );
    109130
    110131        // 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 )
    112133                return $return;
    113134
    114135        // all revisions and (possibly) one autosave
    115136        $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) );
    116137
    117138        // WP_POST_REVISIONS = (int) (# of autosaves to save)
    118         $delete = count($revisions) - WP_POST_REVISIONS;
     139        $delete = count( $revisions ) - WP_POST_REVISIONS;
    119140
    120141        if ( $delete < 1 )
    121142                return $return;
     
    123144        $revisions = array_slice( $revisions, 0, $delete );
    124145
    125146        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' ) )
    127148                        continue;
    128                 wp_delete_post_revision( $revisions[$i]->ID );
     149                wp_delete_post_revision( $revisions[ $i ]->ID );
    129150        }
    130151
    131152        return $return;
     
    142163 * @subpackage Post_Revisions
    143164 * @since 2.6.0
    144165 * @uses wp_get_post_revisions()
    145  * 
     166 *
    146167 * @param int $post_id The post ID.
    147168 * @param int $user_id optional The post author ID.
    148169 * @return object|bool The autosaved data or false on failure or when no autosave exists.
     
    393414        return $post;
    394415}
    395416
     417function _wp_get_post_revision_version( $post ) {
     418        if ( is_array( $post ) ) {
     419                if ( ! isset( $post['post_name'] ) ) {
     420                        return false;
     421                }
     422
     423                $name = $post['post_name'];
     424        } elseif ( is_object( $post ) ) {
     425                if ( ! isset( $post->post_name ) ) {
     426                        return false;
     427                }
     428
     429                $name = $post->post_name;
     430        } else {
     431                return false;
     432        }
     433
     434        if ( ! preg_match( '/^([\d-]+)(?:autosave|revision)(?:-\d+)*$/', $name, $matches ) ) {
     435                return false;
     436        }
     437
     438        $parts = explode( '-', trim( $matches[1], '-' ) );
     439
     440        if ( 2 !== count( $parts ) ) {
     441                return 0;
     442        }
     443
     444        return (int) $parts[1];
     445}
     446
     447/**
     448 * Upgrade the data
     449 *
     450 * @package WordPress
     451 * @subpackage Post_Revisions
     452 * @since 3.6.0
     453 *
     454 * @uses get_post()
     455 * @uses post_type_supports()
     456 * @uses wp_get_post_revisions()
     457*
     458 * @param int|object $post_id Post ID or post object
     459 * @return true if success, false if problems
     460 */
     461function _wp_upgrade_revisions_of_post( $post ) {
     462        global $wpdb;
     463
     464        $post = get_post( $post );
     465        if ( ! $post )
     466                return false;
     467
     468        if ( ! post_type_supports( $post->post_type, 'revisions' ) )
     469                return false;
     470
     471        $revisions = wp_get_post_revisions( $post->ID ); // array( 'order' => 'DESC', 'orderby' => 'date' ); // Always work from most recent to oldest
     472
     473        if ( ! $revisions )
     474                return true;
     475
     476        // Add post option exclusively
     477        $lock      = "revision-upgrade-{$post->ID}";
     478        $locked_at = time();
     479        $result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $locked_at ) );
     480        if ( ! $result ) {
     481                // If we couldn't get a lock, see how old the previous lock is
     482                $locked_at = get_option( $lock );
     483                if ( !$locked_at ) {
     484                        // Can't write to the lock, and can't read the lock.
     485                        // Something broken has happened
     486                        return false;
     487                }
     488
     489                if ( $lock_at < time() - 3600 ) {
     490                        // Lock is too old - try again
     491                        delete_option( $lock );
     492                        return wp_upgrade_revisions_of_post( $post );
     493                }
     494
     495                // Lock is not too old: some other process may be upgrading this post.  Bail.
     496                return;
     497        } else {
     498                // If we could get a lock, re-"add" the option to fire all the correct filters.
     499                add_option( $lock, $locked_at );
     500        }
     501
     502        $success = true;
     503
     504        reset( $revisions );
     505        do {
     506                $this_revision = current( $revisions );
     507                $prev_revision = next( $revisions );
     508
     509                $this_revision_version = _wp_get_post_revision_version( $this_revision );
     510
     511                // Something terrible happened
     512                if ( false === $this_revision_version )
     513                        continue;
     514
     515                // 1 is the latest revision version, so we're already up to date
     516                if ( 0 < $this_revision_version )
     517                        continue;
     518
     519                // This revision is the oldest revision of the post.
     520                // The correct post_author is probably $post->post_author, but that's only a good guess.
     521                // Leave un-upgraded.  Will be caught by get_modified_post_author() on display.
     522                if ( ! $prev_revision ) {
     523                        continue;
     524                }
     525
     526                $prev_revision_version = _wp_get_post_revision_version( $prev_revision );
     527
     528                // If the previous revision is already up to date, it no longer has the information we need :(
     529                if ( 0 < $prev_revision_version ) {
     530                        continue;
     531                }
     532
     533                // Upgrade this revision
     534
     535                // Cast as object so that wp_update_post() handles slashing for us
     536                $update = (object) array(
     537                        'ID'          => $this_revision->ID,
     538                        'post_name'   => preg_replace( '/^(\d+)(?:-0)?-/', '\\1-1-', $this_revision->post_name ),
     539                        'post_author' => $prev_revision->post_author,
     540                );
     541
     542                $result = wp_update_post( $update );
     543                if ( ! $result || is_wp_error( $result ) ) {
     544                        // Wilhelm!
     545                        $success = false;
     546                        break;
     547                }
     548        } while ( $prev_revision );
     549
     550        delete_option( $lock );
     551        return true;
     552}
     553
     554
    396555function _show_post_preview() {
    397556
    398557        if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) {
  • wp-includes/author-template.php

     
    7676}
    7777
    7878/**
     79 * Retrieve the author who last edited the current post.
     80 *
     81 * As of WordPress 3.6, returns the corrected display name and ID of the user who created the revision if called on a revision object.
     82 *
     83 * @since 3.6
     84 * @uses get_post_meta() Retrieves the ID of the author who last edited the current post.
     85 * @uses get_userdata() Retrieves the author's DB object.
     86 * @uses apply_filters() Calls 'the_modified_author' hook on the author display name.
     87 *
     88 * @param mixed $post_id Post ID, WP_Post, or falsey to use the current post.
     89 * @return array modified_author_display_name => The author's display name, modified_author_id => The author's user ID; empty if problems
     90 */
     91function get_the_corrected_modified_author( $post_id = 0 ) {
     92
     93        if ( ! $post = get_post( $post_id ) )
     94                return;
     95
     96        $unknown = false;
     97
     98        if ( 'revision' === $post->post_type ) {
     99                // _wp_get_post_revision_version() can return false
     100                $revision_version = _wp_get_post_revision_version( $post );
     101                if ( false === $revision_version ) {
     102                        // False means something horrible happened.  Just return something.
     103                        $modified_author_id = $post->post_author;
     104                } elseif ( 0 === $revision_version ) {
     105                        // Set as fallback
     106                        $modified_author_id = $post->post_author;
     107
     108                        $revisions = wp_get_post_revisions( $post->post_parent );
     109                        reset( $revisions );
     110                        do {
     111                                $this_revision = current( $revisions );
     112                                $prev_revision = next( $revisions ); // Ordered DESC
     113
     114                                if ( $post->ID == $this_revision->ID && $prev_revision ) {
     115                                        $prev_revision_version = _wp_get_post_revision_version( $prev_revision );
     116                                        if ( 0 === $prev_revision_version ) {
     117                                                $modified_author_id = $prev_revision->post_author;
     118                                        } else {
     119                                                $unknown = true;
     120                                        }
     121                                        break;
     122                                }
     123                        } while ( $prev_revision );
     124                } else {
     125                        // Everything is up to date
     126                        $modified_author_id = $post->post_author;
     127                }
     128        } else {
     129                $modified_author_id = get_post_meta( $post->ID, '_edit_last', true );
     130        }
     131
     132        if ( ! $modified_author_id )
     133                return;
     134
     135        $modified_author = get_userdata( $modified_author_id );
     136
     137        $display_name = $modified_author->display_name;
     138        if ( $unknown ) {
     139                $display_name = sprintf( _x( '%1$s?', 'Unknown revision author name: %1$s = display_name of best guess at author' ), $display_name );
     140        }
     141
     142        $toreturn = array (
     143                'modified_author_display_name' => apply_filters( 'the_modified_author', $display_name ),
     144                'modified_author_id' => $modified_author_id,
     145        );
     146
     147        return $toreturn;
     148}
     149
     150
     151
     152/**
    79153 * Display the name of the author who last edited the current post.
    80154 *
    81155 * @since 2.8
  • wp-admin/includes/ajax-actions.php

     
    22142214                }
    22152215
    22162216                if ( $compare_two_mode ) {
    2217                         $compare_to_gravatar = get_avatar( $left_revision->post_author, 24 );
    2218                         $compare_to_author = get_the_author_meta( 'display_name', $left_revision->post_author );
     2217                        $compare_to_author = get_the_corrected_modified_author( $left_revision->ID );
     2218                        $compare_to_gravatar = get_avatar( $compare_to_author['modified_author_id'], 24 );
    22192219                        $compare_to_date = date_i18n( $datef, strtotime( $left_revision->post_modified ) );
    22202220
    22212221                        $revision_from_date_author = sprintf(
    22222222                                /* translators: post revision title: 1: author avatar, 2: author name, 3: time ago, 4: date */
    22232223                                _x( '%1$s %2$s, %3$s ago (%4$s)', 'post revision title' ),
    22242224                                $compare_to_gravatar,
    2225                                 $compare_to_author,
     2225                                $compare_to_author['modified_author_display_name'],
    22262226                                human_time_diff( strtotime( $left_revision->post_modified ), current_time( 'timestamp' ) ),
    22272227                                $compare_to_date
    22282228                        );
    22292229                }
    22302230
    2231                 $gravatar = get_avatar( $revision->post_author, 24 );
    2232                 $author = get_the_author_meta( 'display_name', $revision->post_author );
     2231                $author = $author = get_the_corrected_modified_author( $revision->ID );
     2232                $gravatar = get_avatar( $author['modified_author_id'], 24 );
    22332233                $date = date_i18n( $datef, strtotime( $revision->post_modified ) );
    22342234                $revision_date_author = sprintf(
    22352235                        /* translators: post revision title: 1: author avatar, 2: author name, 3: time ago, 4: date */
    22362236                        _x( '%1$s %2$s, %3$s ago (%4$s)', 'post revision title' ),
    22372237                        $gravatar,
    2238                         $author,
     2238                        $author['modified_author_display_name'],
    22392239                        human_time_diff( strtotime( $revision->post_modified ), current_time( 'timestamp' ) ),
    22402240                        $date
    22412241                );
     
    22622262                $revision_date_author_short = sprintf(
    22632263                        '%s <strong>%s</strong><br />%s',
    22642264                        $gravatar,
    2265                         $author,
     2265                        $author['modified_author_display_name'],
    22662266                        $date_short
    22672267                );
    22682268
  • wp-admin/includes/post.php

     
    249249
    250250        add_meta( $post_ID );
    251251
    252         update_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
    253 
    254252        wp_update_post( $post_data );
    255253
    256254        // Now that we have an ID we can fix any attachment anchor hrefs
     
    565563
    566564        add_meta( $post_ID );
    567565
    568         add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
    569 
    570566        // Now that we have an ID we can fix any attachment anchor hrefs
    571567        _fix_attachment_links( $post_ID );
    572568
  • wp-admin/js/revisions.js

     
    479479                                                ( REVAPP._right_diff >= REVAPP._revisions.length ) ? '' : REVAPP._revisions.at( REVAPP._right_diff ).get( 'revision_date_author_short' ) );
    480480                                } else {
    481481                                        REVAPP.addTooltip ( $( 'a.ui-slider-handle' ),
    482                                                 ( REVAPP._right_diff >= REVAPP._revisions.length ) ? '' : REVAPP._revisions.at( REVAPP._right_diff ).get( 'revision_date_author_short' ) );
     482                                                ( REVAPP._right_diff > REVAPP._revisions.length ) ? '' : REVAPP._revisions.at( REVAPP._right_diff - 1 ).get( 'revision_date_author_short' ) );
    483483                                }
    484484
    485485                                //