Make WordPress Core


Ignore:
Timestamp:
07/24/2013 06:08:14 AM (11 years ago)
Author:
nacin
Message:

Revisions changes.

  • Eliminates the bloated Revisions meta box in favor of 'Revisions: #' in the publish box.
  • Adds ability to compare autosave to current post, when revisions are disabled.
  • Makes autosaves stand out visually, including "Restore This Autosave".

Also:

  • Adds missing capability check for restoring a revision.
  • When no revision matches the post's current modified time, avoid marking an autosave as 'current'.
  • Fixes wp_get_post_autosave() to return an autosave even when revisions are disabled.
  • Add 'check_enabled' arg to wp_get_post_revisions(); false avoids the wp_revisions_enabled() check.
  • Adds a responsive slider that is narrower for fewer versions. props markjaquith.

see #24804.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/revision.php

    r24761 r24790  
    3333
    3434    // If comparing revisions, make sure we're dealing with the right post parent.
    35     if ( $compare_from && $compare_from->post_parent !== $post->ID )
    36         return false;
    37     if ( $compare_to->post_parent !== $post->ID )
     35    // The parent post may be a 'revision' when revisions are disabled and we're looking at autosaves.
     36    if ( $compare_from && $compare_from->post_parent !== $post->ID && $compare_from->ID !== $post->ID )
     37        return false;
     38    if ( $compare_to->post_parent !== $post->ID && $compare_to->ID !== $post->ID )
    3839        return false;
    3940
     
    9293    $now_gmt = time();
    9394
    94     $revisions = wp_get_post_revisions( $post->ID, array( 'order' => 'ASC' ) );
     95    $revisions = wp_get_post_revisions( $post->ID, array( 'order' => 'ASC', 'check_enabled' => false ) );
     96    // If revisions are disabled, we only want autosaves and the current post.
     97    if ( ! wp_revisions_enabled( $post ) ) {
     98        foreach ( $revisions as $revision_id => $revision ) {
     99            if ( ! wp_is_post_autosave( $revision ) )
     100                unset( $revisions[ $revision_id ] );
     101        }
     102        $revisions = array( $post->ID => $post ) + $revisions;
     103    }
     104
    95105    $show_avatars = get_option( 'show_avatars' );
    96106
    97107    cache_users( wp_list_pluck( $revisions, 'post_author' ) );
     108
     109    $can_restore = current_user_can( 'edit_post', $post->ID );
    98110
    99111    foreach ( $revisions as $revision ) {
    100112        $modified = strtotime( $revision->post_modified );
    101113        $modified_gmt = strtotime( $revision->post_modified_gmt );
    102         $restore_link = str_replace( '&', '&', wp_nonce_url(
    103             add_query_arg(
    104                 array( 'revision' => $revision->ID,
    105                     'action' => 'restore' ),
    106                     admin_url( 'revision.php' )
    107             ),
    108             "restore-post_{$revision->ID}"
    109         ) );
     114        if ( $can_restore ) {
     115            $restore_link = str_replace( '&', '&', wp_nonce_url(
     116                add_query_arg(
     117                    array( 'revision' => $revision->ID,
     118                        'action' => 'restore' ),
     119                        admin_url( 'revision.php' )
     120                ),
     121                "restore-post_{$revision->ID}"
     122            ) );
     123        }
    110124
    111125        if ( ! isset( $authors[ $revision->post_author ] ) ) {
     
    117131        }
    118132
    119         $autosave = wp_is_post_autosave( $revision );
     133        $autosave = (bool) wp_is_post_autosave( $revision );
    120134        $current = ! $autosave && $revision->post_modified_gmt === $post->post_modified_gmt;
    121135        if ( $current && ! empty( $current_id ) ) {
     136            // If multiple revisions have the same post_modified_gmt, highest ID is current.
    122137            if ( $current_id < $revision->ID ) {
    123138                $revisions[ $current_id ]['current'] = false;
     
    139154            'autosave'   => $autosave,
    140155            'current'    => $current,
    141             'restoreUrl' => urldecode( $restore_link ),
     156            'restoreUrl' => $can_restore ? $restore_link : false,
    142157        );
    143158    }
     
    145160    // If a post has been saved since the last revision (no revisioned fields were changed)
    146161    // we may not have a "current" revision. Mark the latest revision as "current".
    147     if ( empty( $current_id ) )
    148         $revisions[ $revision->ID ]['current'] = true;
     162    if ( empty( $current_id ) ) {
     163        if ( $revisions[ $revision->ID ]['autosave'] ) {
     164            $revision = end( $revisions );
     165            while ( $revision['autosave'] ) {
     166                $revision = prev( $revisions );
     167            }
     168            $current_id = $revision['id'];
     169        } else {
     170            $current_id = $revision->ID;
     171        }
     172        $revisions[ $current_id ]['current'] = true;
     173    }
    149174
    150175    // Now, grab the initial diff
Note: See TracChangeset for help on using the changeset viewer.