Make WordPress Core

Ticket #16215: 16215.4.diff

File 16215.4.diff, 8.4 KB (added by mdawaffe, 12 years ago)

uses post_name for revision_version

  • wp-admin/includes/post.php

     
    241241
    242242        add_meta( $post_ID );
    243243
    244         update_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
    245 
    246244        wp_update_post( $post_data );
    247245
    248246        // Now that we have an ID we can fix any attachment anchor hrefs
     
    568566
    569567        add_meta( $post_ID );
    570568
    571         add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
    572 
    573569        // Now that we have an ID we can fix any attachment anchor hrefs
    574570        _fix_attachment_links( $post_ID );
    575571
  • 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-includes/post-template.php

     
    13861386                        continue;
    13871387
    13881388                $date = wp_post_revision_title( $revision );
    1389                 $name = get_the_author_meta( 'display_name', $revision->post_author );
     1389                $name = get_the_modified_author( $revision );
    13901390
    13911391                if ( 'form-table' == $format ) {
    13921392                        if ( $left )
  • wp-includes/post.php

     
    29002900                $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
    29012901        }
    29022902
     2903        if ( 'revision' !== $post_type )
     2904                update_post_meta( $post_ID, '_edit_last', $user_ID );
     2905
    29032906        if ( is_object_in_taxonomy($post_type, 'category') )
    29042907                wp_set_post_categories( $post_ID, $post_category );
    29052908
     
    49164919        $return['post_parent']   = $post['ID'];
    49174920        $return['post_status']   = 'inherit';
    49184921        $return['post_type']     = 'revision';
    4919         $return['post_name']     = $autosave ? "$post[ID]-autosave" : "$post[ID]-revision";
     4922        $return['post_name']     = $autosave ? "$post[ID]-1-autosave" : "$post[ID]-1-revision"; // "1" is the revisioning system version
    49204923        $return['post_date']     = isset($post['post_modified']) ? $post['post_modified'] : '';
    49214924        $return['post_date_gmt'] = isset($post['post_modified_gmt']) ? $post['post_modified_gmt'] : '';
    49224925
     4926        $return['post_author']   = get_post_meta( $post['ID'], '_edit_last', true );
     4927
    49234928        return $return;
    49244929}
    49254930
     
    52385243        return $revisions;
    52395244}
    52405245
     5246function _wp_get_post_revision_version( $post ) {
     5247        if ( is_array( $post ) ) {
     5248                if ( ! isset( $post['post_name'] ) ) {
     5249                        return false;
     5250                }
     5251
     5252                $name = $post['post_name'];
     5253        } elseif ( is_object( $post ) ) {
     5254                if ( ! isset( $post->post_name ) ) {
     5255                        return false;
     5256                }
     5257
     5258                $name = $post->post_name;
     5259        } else {
     5260                return false;
     5261        }
     5262
     5263        if ( ! preg_match( '/^([\d-]+)(?:autosave|revision)(?:-\d+)*$/', $name, $matches ) ) {
     5264                return false;
     5265        }
     5266
     5267        $parts = explode( '-', trim( $matches[1], '-' ) );
     5268
     5269        if ( 2 !== count( $parts ) ) {
     5270                return 0;
     5271        }
     5272
     5273        return (int) $parts[1];
     5274}
     5275
     5276function _wp_upgrade_revisions_of_post( $post ) {
     5277        global $wpdb;
     5278
     5279        $post = get_post( $post );
     5280        if ( ! $post )
     5281                return false;
     5282
     5283        if ( ! post_type_supports( $post->post_type, 'revisions' ) )
     5284                return false;
     5285
     5286        $revisions = wp_get_post_revisions( $post->ID ); // array( 'order' => 'DESC', 'orderby' => 'date' ); // Always work from most recent to oldest.
     5287
     5288        if ( ! $revisions )
     5289                return true;
     5290
     5291        // Add post option exclusively
     5292        $lock      = "revision-upgrade-{$post->ID}";
     5293        $locked_at = time();
     5294        $result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $locked_at ) );
     5295        if ( ! $result ) {
     5296                // If we couldn't get a lock, see how old the previous lock is
     5297                $locked_at = get_option( $lock );
     5298                if ( !$locked_at ) {
     5299                        // Can't write to the lock, and can't read the lock.
     5300                        // Something broken has happened
     5301                        return false;
     5302                }
     5303
     5304                if ( $lock_at < time() - 3600 ) {
     5305                        // Lock is too old - try again
     5306                        delete_option( $lock );
     5307                        return wp_upgrade_revisions_of_post( $post );
     5308                }
     5309
     5310                // Lock is not too old: some other process may be upgrading this post.  Bail.
     5311                return;
     5312        } else {
     5313                // If we could get a lock, re-"add" the option to fire all the correct filters.
     5314                add_option( $lock, $locked_at );
     5315        }
     5316
     5317        $success = true;
     5318
     5319        reset( $revisions );
     5320        do {
     5321                $this_revision = current( $revisions );
     5322                $prev_revision = next( $revisions );
     5323
     5324                $this_revision_version = _wp_get_post_revision_version( $this_revision );
     5325
     5326                // Something terrible happened
     5327                if ( false === $this_revision_version )
     5328                        continue;
     5329
     5330                // 1 is the latest revision version, so we're already up to date
     5331                if ( 0 < $this_revision_version )
     5332                        continue;
     5333
     5334                // This revision is the oldest revision of the post.
     5335                // The correct post_author is probably $post->post_author, but that's only a good guess.
     5336                // Leave un-upgraded.  Will be caught by get_modified_post_author() on display.
     5337                if ( ! $prev_revision ) {
     5338                        continue;
     5339                }
     5340
     5341                $prev_revision_version = _wp_get_post_revision_version( $prev_revision );
     5342
     5343                // If the previous revision is already up to date, it no longer has the information we need :(
     5344                if ( 0 < $prev_revision_version ) {
     5345                        continue;
     5346                }
     5347
     5348                // Upgrade this revision
     5349
     5350                // Cast as object so that wp_update_post() handles slashing for us
     5351                $update = (object) array(
     5352                        'ID'          => $this_revision->ID,
     5353                        'post_name'   => preg_replace( '/^(\d+)(?:-0)?-/', '\\1-1-', $this_revision->post_name ),
     5354                        'post_author' => $prev_revision->post_author,
     5355                );
     5356
     5357                $result = wp_update_post( $update );
     5358                if ( ! $result || is_wp_error( $result ) ) {
     5359                        // Wilhelm!
     5360                        $success = false;
     5361                        break;
     5362                }
     5363        } while ( $prev_revision );
     5364
     5365        delete_option( $lock );
     5366        return true;
     5367}
     5368
    52415369function _set_preview($post) {
    52425370
    52435371        if ( ! is_object($post) )