Make WordPress Core

Ticket #16215: 16215.7.diff

File 16215.7.diff, 10.8 KB (added by adamsilverstein, 12 years ago)

updated against trunk

  • wp-includes/post-template.php

     
    14031403                        continue;
    14041404
    14051405                $date = wp_post_revision_title( $revision );
    1406                 $name = get_the_author_meta( 'display_name', $revision->post_author );
     1406                $name = get_the_modified_author( $revision );
    14071407
    14081408                if ( 'form-table' == $format ) {
    14091409                        if ( $left )
  • 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}
     
    142143 * @subpackage Post_Revisions
    143144 * @since 2.6.0
    144145 * @uses wp_get_post_revisions()
    145  * 
     146 *
    146147 * @param int $post_id The post ID.
    147148 * @param int $user_id optional The post author ID.
    148149 * @return object|bool The autosaved data or false on failure or when no autosave exists.
     
    393394        return $post;
    394395}
    395396
     397function _wp_get_post_revision_version( $post ) {
     398        if ( is_array( $post ) ) {
     399                if ( ! isset( $post['post_name'] ) ) {
     400                        return false;
     401                }
     402
     403                $name = $post['post_name'];
     404        } elseif ( is_object( $post ) ) {
     405                if ( ! isset( $post->post_name ) ) {
     406                        return false;
     407                }
     408
     409                $name = $post->post_name;
     410        } else {
     411                return false;
     412        }
     413
     414        if ( ! preg_match( '/^([\d-]+)(?:autosave|revision)(?:-\d+)*$/', $name, $matches ) ) {
     415                return false;
     416        }
     417
     418        $parts = explode( '-', trim( $matches[1], '-' ) );
     419
     420        if ( 2 !== count( $parts ) ) {
     421                return 0;
     422        }
     423
     424        return (int) $parts[1];
     425}
     426
     427function _wp_upgrade_revisions_of_post( $post ) {
     428        global $wpdb;
     429
     430        $post = get_post( $post );
     431        if ( ! $post )
     432                return false;
     433
     434        if ( ! post_type_supports( $post->post_type, 'revisions' ) )
     435                return false;
     436
     437        $revisions = wp_get_post_revisions( $post->ID ); // array( 'order' => 'DESC', 'orderby' => 'date' ); // Always work from most recent to oldest
     438
     439        if ( ! $revisions )
     440                return true;
     441
     442        // Add post option exclusively
     443        $lock      = "revision-upgrade-{$post->ID}";
     444        $locked_at = time();
     445        $result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $locked_at ) );
     446        if ( ! $result ) {
     447                // If we couldn't get a lock, see how old the previous lock is
     448                $locked_at = get_option( $lock );
     449                if ( !$locked_at ) {
     450                        // Can't write to the lock, and can't read the lock.
     451                        // Something broken has happened
     452                        return false;
     453                }
     454
     455                if ( $lock_at < time() - 3600 ) {
     456                        // Lock is too old - try again
     457                        delete_option( $lock );
     458                        return wp_upgrade_revisions_of_post( $post );
     459                }
     460
     461                // Lock is not too old: some other process may be upgrading this post.  Bail.
     462                return;
     463        } else {
     464                // If we could get a lock, re-"add" the option to fire all the correct filters.
     465                add_option( $lock, $locked_at );
     466        }
     467
     468        $success = true;
     469
     470        reset( $revisions );
     471        do {
     472                $this_revision = current( $revisions );
     473                $prev_revision = next( $revisions );
     474
     475                $this_revision_version = _wp_get_post_revision_version( $this_revision );
     476
     477                // Something terrible happened
     478                if ( false === $this_revision_version )
     479                        continue;
     480
     481                // 1 is the latest revision version, so we're already up to date
     482                if ( 0 < $this_revision_version )
     483                        continue;
     484
     485                // This revision is the oldest revision of the post.
     486                // The correct post_author is probably $post->post_author, but that's only a good guess.
     487                // Leave un-upgraded.  Will be caught by get_modified_post_author() on display.
     488                if ( ! $prev_revision ) {
     489                        continue;
     490                }
     491
     492                $prev_revision_version = _wp_get_post_revision_version( $prev_revision );
     493
     494                // If the previous revision is already up to date, it no longer has the information we need :(
     495                if ( 0 < $prev_revision_version ) {
     496                        continue;
     497                }
     498
     499                // Upgrade this revision
     500
     501                // Cast as object so that wp_update_post() handles slashing for us
     502                $update = (object) array(
     503                        'ID'          => $this_revision->ID,
     504                        'post_name'   => preg_replace( '/^(\d+)(?:-0)?-/', '\\1-1-', $this_revision->post_name ),
     505                        'post_author' => $prev_revision->post_author,
     506                );
     507
     508                $result = wp_update_post( $update );
     509                if ( ! $result || is_wp_error( $result ) ) {
     510                        // Wilhelm!
     511                        $success = false;
     512                        break;
     513                }
     514        } while ( $prev_revision );
     515
     516        delete_option( $lock );
     517        return true;
     518}
     519
     520
    396521function _show_post_preview() {
    397522
    398523        if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) {
  • wp-includes/author-template.php

     
    6161/**
    6262 * Retrieve the author who last edited the current post.
    6363 *
     64 * As of WordPress 3.6, returns the display name of the user who created the revision if called on a revision object.
     65 *
    6466 * @since 2.8
    65  * @uses $post The current post's DB object.
    6667 * @uses get_post_meta() Retrieves the ID of the author who last edited the current post.
    6768 * @uses get_userdata() Retrieves the author's DB object.
    6869 * @uses apply_filters() Calls 'the_modified_author' hook on the author display name.
     70 *
     71 * @param mixed $post_id Post ID, WP_Post, or falsey to use the current post.
    6972 * @return string The author's display name.
    7073 */
    71 function get_the_modified_author() {
    72         if ( $last_id = get_post_meta( get_post()->ID, '_edit_last', true) ) {
    73                 $last_user = get_userdata($last_id);
    74                 return apply_filters('the_modified_author', $last_user->display_name);
     74function get_the_modified_author( $post_id = 0 ) {
     75        $post = get_post( $post_id );
     76        if ( ! $post )
     77                return;
     78
     79        $unknown = false;
     80
     81        if ( 'revision' === $post->post_type ) {
     82                // _wp_get_post_revision_version() can return false
     83                $revision_version = _wp_get_post_revision_version( $post );
     84                if ( false === $revision_version ) {
     85                        // False means something horrible happened.  Just return something.
     86                        $modified_author_id = $post->post_author;
     87                } elseif ( 0 === $revision_version ) {
     88                        // Set as fallback
     89                        $modified_author_id = $post->post_author;
     90
     91                        $revisions = wp_get_post_revisions( $post->post_parent );
     92                        reset( $revisions );
     93                        do {
     94                                $this_revision = current( $revisions );
     95                                $prev_revision = next( $revisions ); // Ordered DESC
     96
     97                                if ( $post->ID == $this_revision->ID && $prev_revision ) {
     98                                        $prev_revision_version = _wp_get_post_revision_version( $prev_revision );
     99                                        if ( 0 === $prev_revision_version ) {
     100                                                $modified_author_id = $prev_revision->post_author;
     101                                        } else {
     102                                                $unknown = true;
     103                                        }
     104                                        break;
     105                                }
     106                        } while ( $prev_revision );
     107                } else {
     108                        // Everything is up to date
     109                        $modified_author_id = $post->post_author;
     110                }
     111        } else {
     112                $modified_author_id = get_post_meta( $post->ID, '_edit_last', true );
    75113        }
     114
     115        if ( ! $modified_author_id )
     116                return;
     117
     118        $modified_author = get_userdata( $modified_author_id );
     119
     120        $display_name = $modified_author->display_name;
     121        if ( $unknown ) {
     122                $display_name = sprintf( _x( '%1$s?', 'Unknown revision author name: %1$s = display_name of best guess at author' ), $display_name );
     123        }
     124
     125        return apply_filters( 'the_modified_author', $display_name );
    76126}
    77127
    78128/**
  • wp-admin/includes/ajax-actions.php

     
    21302130
    21312131                if ( $compare_two_mode ) {
    21322132                        $compare_to_gravatar = get_avatar( $left_revision->post_author, 18 );
    2133                         $compare_to_author = get_the_author_meta( 'display_name', $left_revision->post_author );
     2133                        $compare_to_author = get_the_modified_author( $left_revision->ID );
    21342134                        $compare_to_date = date_i18n( $datef, strtotime( $left_revision->post_modified ) );
    21352135
    21362136                        $revision_from_date_author = sprintf(
     
    21792179
    21802180        //if we are comparing two revisions, the first 'revision' represented by the leftmost
    21812181        //slider position is the current revision, prepend a comparison to this revision
    2182         if ( $compare_two_mode ) 
     2182        if ( $compare_two_mode )
    21832183                array_unshift( $revisions, get_post( $post_id ) );
    2184                
     2184
    21852185        $count = -1;
    21862186
    21872187        foreach ( $revisions as $revision ) :
     
    21922192                $count++;
    21932193                // return blank data for diffs to the left of the left handle (for right handel model)
    21942194                // or to the right of the right handle (for left handel model)
    2195                 if ( ( 0 != $left_handle_at && $count <= $left_handle_at ) || 
    2196                          ( 0 != $right_handle_at && $count > $right_handle_at )) { 
     2195                if ( ( 0 != $left_handle_at && $count <= $left_handle_at ) ||
     2196                         ( 0 != $right_handle_at && $count > $right_handle_at )) {
    21972197                        $alltherevisions[] = array (
    21982198                                'ID' => $revision->ID,
    21992199                        );
    2200                        
     2200
    22012201                        continue;
    22022202                }
    22032203
    22042204                $gravatar = get_avatar( $revision->post_author, 18 );
    2205                 $author = get_the_author_meta( 'display_name', $revision->post_author );
     2205                $author = get_the_modified_author( $revision->ID );
    22062206                $date = date_i18n( $datef, strtotime( $revision->post_modified ) );
    22072207                $revision_date_author = sprintf(
    22082208                        /* translators: post revision title: 1: author avatar, 2: author name, 3: time ago, 4: date */
  • 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