Make WordPress Core

Changeset 24790


Ignore:
Timestamp:
07/24/2013 06:08:14 AM (12 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.

Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/css/wp-admin.css

    r24775 r24790  
    35403540    height: 0.8em;
    35413541    top: 7px;
    3542     width: 70%;
     3542    max-width: 70%;
    35433543    -moz-box-sizing: border-box;
    35443544    -webkit-box-sizing: border-box;
     
    36423642    color: #777;
    36433643}
     3644
     3645.revisions-controls .author-card.autosave {
     3646    color: #d54e21;
     3647}
     3648
    36443649.revisions-controls .author-card .author-name {
    36453650    font-weight: bold;
     
    36643669}
    36653670
    3666 .wp-slider {
    3667     width: 70%;
     3671.revisions-controls .wp-slider {
     3672    max-width: 70%;
    36683673    margin: 0 auto;
    36693674    top: -3px;
  • trunk/wp-admin/edit-form-advanced.php

    r24787 r24790  
    110110require_once('./includes/meta-boxes.php');
    111111
     112
     113$publish_callback_args = null;
     114if ( post_type_supports($post_type, 'revisions') && 'auto-draft' != $post->post_status ) {
     115    $revisions = wp_get_post_revisions( $post_ID );
     116
     117    // Check if the revisions have been upgraded
     118    if ( ! empty( $revisions ) && _wp_get_post_revision_version( end( $revisions ) ) < 1 )
     119        _wp_upgrade_revisions_of_post( $post, $revisions );
     120
     121    // We should aim to show the revisions metabox only when there are revisions.
     122    if ( count( $revisions ) > 1 ) {
     123        reset( $revisions ); // Reset pointer for key()
     124        $publish_callback_args = array( 'revisions_count' => count( $revisions ), 'revision_id' => key( $revisions ) );
     125        // add_meta_box('revisionsdiv', __('Revisions'), 'post_revisions_meta_box', null, 'normal', 'core');
     126    }
     127}
     128
    112129if ( 'attachment' == $post_type ) {
    113130    wp_enqueue_script( 'image-edit' );
     
    116133    add_action( 'edit_form_after_title', 'edit_form_image_editor' );
    117134} else {
    118     add_meta_box( 'submitdiv', __( 'Publish' ), 'post_submit_meta_box', null, 'side', 'core' );
     135    add_meta_box( 'submitdiv', __( 'Publish' ), 'post_submit_meta_box', null, 'side', 'core', $publish_callback_args );
    119136}
    120137
     
    171188    if ( is_super_admin() || current_user_can( $post_type_object->cap->edit_others_posts ) )
    172189        add_meta_box('authordiv', __('Author'), 'post_author_meta_box', null, 'normal', 'core');
    173 }
    174 
    175 if ( post_type_supports($post_type, 'revisions') && 'auto-draft' != $post->post_status ) {
    176     $revisions = wp_get_post_revisions( $post_ID );
    177 
    178     // Check if the revisions have been upgraded
    179     if ( ! empty( $revisions ) && _wp_get_post_revision_version( end( $revisions ) ) < 1 )
    180         _wp_upgrade_revisions_of_post( $post, $revisions );
    181 
    182     // We should aim to show the revisions metabox only when there are revisions.
    183     if ( count( $revisions ) > 1 )
    184         add_meta_box('revisionsdiv', __('Revisions'), 'post_revisions_meta_box', null, 'normal', 'core');
    185190}
    186191
  • trunk/wp-admin/includes/ajax-actions.php

    r24713 r24790  
    21002100
    21012101    // Really just pre-loading the cache here.
    2102     if ( ! $revisions = wp_get_post_revisions( $post->ID ) )
     2102    if ( ! $revisions = wp_get_post_revisions( $post->ID, array( 'check_enabled' => false ) ) )
    21032103        wp_send_json_error();
    21042104
  • trunk/wp-admin/includes/meta-boxes.php

    r24522 r24790  
    1010 * @param object $post
    1111 */
    12 function post_submit_meta_box($post) {
     12function post_submit_meta_box($post, $args = array() ) {
    1313    global $action;
    1414
     
    171171    $date = date_i18n( $datef, strtotime( current_time('mysql') ) );
    172172}
     173
     174if ( ! empty( $args['args']['revisions_count'] ) ) :
     175    $revisions_to_keep = wp_revisions_to_keep( $post );
     176?>
     177<div class="misc-pub-section num-revisions">
     178<?php
     179    if ( $revisions_to_keep > 0 && $revisions_to_keep <= $args['args']['revisions_count'] ) {
     180        echo '<span title="' . esc_attr( sprintf( __( 'Your site is configured to keep only the last %s revisions.' ),
     181            number_format_i18n( $revisions_to_keep ) ) ) . '">';
     182        printf( __( 'Revisions: %s' ), '<b>' . number_format_i18n( $args['args']['revisions_count'] ) . '+</b>' );
     183        echo '</span>';
     184    } else {
     185        printf( 'Revisions: %s', '<b>' . number_format_i18n( $args['args']['revisions_count'] ) . '</b>' );
     186    }
     187?>
     188    <a class="hide-if-no-js" href="<?php echo esc_url( get_edit_post_link( $args['args']['revision_id'] ) ); ?>"><?php _ex( 'Browse', 'revisions' ); ?></a>
     189</div>
     190<?php endif;
    173191
    174192if ( $can_publish ) : // Contributors don't get to choose the date of publish ?>
  • 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( '&amp;', '&', 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( '&amp;', '&', 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
  • trunk/wp-admin/js/revisions.js

    r24768 r24790  
    652652            tickCount = this.model.revisions.length - 1;
    653653            tickWidth = 1 / tickCount;
     654            this.$el.css('width', ( this.model.revisions.length * 50 ) + 'px');
    654655
    655656            _(tickCount).times( function( index ){
    656                 this.$el.append( '<div style="' + this.direction + ': ' + ( 100 * tickWidth * index ) + '%"></div>' ); }, this );
     657                this.$el.append( '<div style="' + this.direction + ': ' + ( 100 * tickWidth * index ) + '%"></div>' );
     658            }, this );
    657659        }
    658660    });
     
    832834
    833835        ready: function() {
     836            this.$el.css('width', ( this.model.revisions.length * 50 ) + 'px');
    834837            this.$el.slider( _.extend( this.model.toJSON(), {
    835838                start: this.start,
  • trunk/wp-admin/revision.php

    r24763 r24790  
    3232        break;
    3333
    34     // Revisions disabled (previously checked autosavegs && ! wp_is_post_autosave( $revision ))
     34    // Revisions disabled (previously checked autosaves && ! wp_is_post_autosave( $revision ))
    3535    if ( ! wp_revisions_enabled( $post ) ) {
    3636        $redirect = 'edit.php?post_type=' . $post->post_type;
     
    134134    <div class="author-card">
    135135    <# if ( 'undefined' !== typeof data && 'undefined' !== typeof data.author ) { #>
    136         {{{ data.author.avatar }}}
    137         <div class="author-info">
    138                
    139             <span class="byline"><?php printf( __( 'Revision by %s' ),
    140                 '<span class="author-name">{{ data.author.name }}</span>' ); ?></span>
    141             <span class="time-ago">{{ data.timeAgo }}</span>
    142             <span class="date">({{ data.dateShort }})</span>
    143         </div>
     136            <div class="author-card<# if ( data.autosave ) { #> autosave<# } #>">
     137                {{{ data.author.avatar }}}
     138                <div class="author-info">
     139                <# if ( data.autosave ) { #>
     140                    <span class="byline"><?php printf( __( 'Autosave by %s' ),
     141                        '<span class="author-name">{{ data.author.name }}</span>' ); ?></span>
     142                <# } else if ( data.current ) { #>
     143                    <span class="byline"><?php printf( __( 'Current Revision by %s' ),
     144                        '<span class="author-name">{{ data.author.name }}</span>' ); ?></span>
     145                <# } else { #>
     146                    <span class="byline"><?php printf( __( 'Revision by %s' ),
     147                        '<span class="author-name">{{ data.author.name }}</span>' ); ?></span>
     148                <# } #>
     149                    <span class="time-ago">{{ data.timeAgo }}</span>
     150                    <span class="date">({{ data.dateShort }})</span>
     151                </div>
     152            </div>
    144153    <# } #>
    145154    </div>
     
    167176            <strong><?php _ex( 'From:', 'Followed by post revision info' ); ?></strong>
    168177        <# if ( 'undefined' !== typeof data.from ) { #>
    169             <div class="author-card">
     178            <div class="author-card<# if ( data.from.attributes.autosave ) { #> autosave<# } #>">
    170179                {{{ data.from.attributes.author.avatar }}}
    171180                <div class="author-info">
     181                <# if ( data.from.attributes.autosave ) { #>
     182                    <span class="byline"><?php printf( __( 'Autosave by %s' ),
     183                        '<span class="author-name">{{ data.from.attributes.author.name }}</span>' ); ?></span>
     184                <# } else if ( data.from.attributes.current ) { #>
     185                    <span class="byline"><?php printf( __( 'Current Revision by %s' ),
     186                        '<span class="author-name">{{ data.from.attributes.author.name }}</span>' ); ?></span>
     187                <# } else { #>
    172188                    <span class="byline"><?php printf( __( 'Revision by %s' ),
    173189                        '<span class="author-name">{{ data.from.attributes.author.name }}</span>' ); ?></span>
     190                <# } #>
    174191                    <span class="time-ago">{{ data.from.attributes.timeAgo }}</span>
    175192                    <span class="date">({{ data.from.attributes.dateShort }})</span>
     
    184201            <strong><?php _ex( 'To:', 'Followed by post revision info' ); ?></strong>
    185202        <# if ( 'undefined' !== typeof data.to ) { #>
    186             <div class="author-card">
     203            <div class="author-card<# if ( data.to.attributes.autosave ) { #> autosave<# } #>">
    187204                {{{ data.to.attributes.author.avatar }}}
    188205                <div class="author-info">
     206                <# if ( data.to.attributes.autosave ) { #>
     207                    <span class="byline"><?php printf( __( 'Autosave by %s' ),
     208                        '<span class="author-name">{{ data.to.attributes.author.name }}</span>' ); ?></span>
     209                <# } else if ( data.to.attributes.current ) { #>
     210                    <span class="byline"><?php printf( __( 'Current Revision by %s' ),
     211                        '<span class="author-name">{{ data.to.attributes.author.name }}</span>' ); ?></span>
     212                <# } else { #>
    189213                    <span class="byline"><?php printf( __( 'Revision by %s' ),
    190214                        '<span class="author-name">{{ data.to.attributes.author.name }}</span>' ); ?></span>
     215                <# } #>
    191216                    <span class="time-ago">{{ data.to.attributes.timeAgo }}</span>
    192217                    <span class="date">({{ data.to.attributes.dateShort }})</span>
    193218                </div>
    194219        <# } #>
     220        <# if ( data.to.attributes.restoreUrl ) { #>
    195221            <input
    196222            <# if ( data.to.attributes.current ) { #>
    197223                disabled="disabled"
    198224            <# } #>
    199             type="button" class="restore-revision button button-primary" data-restore-link="{{{ data.restoreLink }}}" value="<?php esc_attr_e( 'Restore This Revision' ); ?>" />
     225            <# if ( data.to.attributes.autosave ) { #>
     226                type="button" class="restore-revision button button-primary" value="<?php esc_attr_e( 'Restore This Autosave' ); ?>" />
     227            <# } else { #>
     228                type="button" class="restore-revision button button-primary" value="<?php esc_attr_e( 'Restore This Revision' ); ?>" />
     229            <# } #>
     230        <# } #>
    200231        </div>
    201232    </div>
  • trunk/wp-includes/revision.php

    r24520 r24790  
    158158 */
    159159function wp_get_post_autosave( $post_id, $user_id = 0 ) {
    160     $revisions = wp_get_post_revisions($post_id);
     160    $revisions = wp_get_post_revisions( $post_id, array( 'check_enabled' => false ) );
    161161
    162162    foreach ( $revisions as $revision ) {
     
    370370function wp_get_post_revisions( $post_id = 0, $args = null ) {
    371371    $post = get_post( $post_id );
    372     if ( ! $post || empty( $post->ID ) || ! wp_revisions_enabled( $post ) )
     372    if ( ! $post || empty( $post->ID ) )
    373373        return array();
    374374
    375     $defaults = array( 'order' => 'DESC', 'orderby' => 'date' );
     375    $defaults = array( 'order' => 'DESC', 'orderby' => 'date', 'check_enabled' => true );
    376376    $args = wp_parse_args( $args, $defaults );
     377
     378    if ( $args['check_enabled'] && ! wp_revisions_enabled( $post ) )
     379        return array();
     380
    377381    $args = array_merge( $args, array( 'post_parent' => $post->ID, 'post_type' => 'revision', 'post_status' => 'inherit' ) );
    378382
Note: See TracChangeset for help on using the changeset viewer.