Revisions: Updates to the new Revisions UI.

Various Updates including:
 * i18n fixes
 * Added tracking of what revision ID was restored
 * async fetching of diffs so that slider works sooner even with many revisions

See #23497 props adamsilverstein, ethitter


git-svn-id: http://core.svn.wordpress.org/trunk@23639 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Westwood 2013-03-07 15:32:26 +00:00
parent 082e9342b7
commit 51db623107
6 changed files with 350 additions and 230 deletions

View File

@ -3722,10 +3722,25 @@ span#diff_left_count_inner,
.leftmodelloading #modelsloading, .leftmodelloading #modelsloading,
.rightmodelloading #modelsloading, .rightmodelloading #modelsloading,
.leftmodelloading #modelsloading .spinner, .leftmodelloading #modelsloading .spinner,
.rightmodelloading #modelsloading .spinner { .rightmodelloading #modelsloading .spinner,
{
display: inline; display: inline;
} }
.diff-loading {
margin-top: 50px;
width: 100%;
height: 200px;
}
.diff-loading .spinner
{
clear: both;
margin-left: auto;
margin-right: auto;
display: block;
float: none;
}
#difftitlefrom { #difftitlefrom {
float: left; float: left;
display: none; display: none;

View File

@ -2131,132 +2131,139 @@ function wp_ajax_nopriv_heartbeat() {
function wp_ajax_revisions_data() { function wp_ajax_revisions_data() {
check_ajax_referer( 'revisions-ajax-nonce', 'nonce' ); check_ajax_referer( 'revisions-ajax-nonce', 'nonce' );
$compareto = isset( $_GET['compareto'] ) ? absint( $_GET['compareto'] ) : 0; $compare_to = isset( $_GET['compare_to'] ) ? absint( $_GET['compare_to'] ) : 0;
$showautosaves = isset( $_GET['showautosaves'] ) ? $_GET['showautosaves'] : ''; $show_autosaves = isset( $_GET['show_autosaves'] ) ? $_GET['show_autosaves'] : '';
$showsplitview = isset( $_GET['showsplitview'] ) ? $_GET['showsplitview'] : ''; $show_split_view = isset( $_GET['show_split_view'] ) ? $_GET['show_split_view'] : '';
$postid = isset( $_GET['post_id'] ) ? absint( $_GET['post_id'] ) : ''; $post_id = isset( $_GET['post_id'] ) ? absint( $_GET['post_id'] ) : '';
$right_handle_at = isset( $_GET['right_handle_at'] ) ? $_GET['right_handle_at'] : 0;
$left_handle_at = isset( $_GET['left_handle_at'] ) ? $_GET['left_handle_at'] : 0;
$single_revision_id = isset( $_GET['single_revision_id'] ) ? $_GET['single_revision_id'] : 0;
$comparetwomode = ( '' == $postid ) ? false : true; $compare_two_mode = ( '' == $post_id ) ? false : true;
// //
//TODO: currently code returns all possible comparisons for the indicated 'compareto' revision //TODO: currently code returns all possible comparisons for the indicated 'compare_to' revision
//however, the front end prevents users from pulling the right handle past the left or the left pass the right, //however, the front end prevents users from pulling the right handle past the left or the left pass the right,
//so only the possible diffs need be generated //so only the possible diffs need be generated
// //
$alltherevisions = array(); $alltherevisions = array();
if ( '' == $postid ) if ( '' == $post_id )
$postid = $compareto; $post_id = $compare_to;
if ( ! current_user_can( 'read_post', $postid ) ) if ( ! current_user_can( 'read_post', $post_id ) )
continue; continue;
if ( ! $revisions = wp_get_post_revisions( $postid ) ) if ( ! $revisions = wp_get_post_revisions( $post_id ) )
return; return;
//if we are comparing two revisions, the first 'revision' represented by the leftmost
//slider position is the current revision, prepend a comparison to this revision
if ( $comparetwomode )
array_unshift( $revisions, get_post( $postid ) );
$count = 1;
foreach ( $revisions as $revision ) :
if ( 'true' != $showautosaves && wp_is_post_autosave( $revision ) )
continue;
$revision_from_date_author = '';
$left_revision = get_post( $compareto );
$right_revision = get_post( $revision );
$author = get_the_author_meta( 'display_name', $revision->post_author );
/* translators: revision date format, see http://php.net/date */ /* translators: revision date format, see http://php.net/date */
$datef = _x( 'j F, Y @ G:i:s', 'revision date format'); $datef = _x( 'j F, Y @ G:i:s', 'revision date format');
$gravatar = get_avatar( $revision->post_author, 18 ); //single model fetch mode
if ( 0 != $single_revision_id ) {
$left_revision = get_post( $compare_to );
$right_revision = get_post( $single_revision_id );
$date = date_i18n( $datef, strtotime( $revision->post_modified ) ); if ( $compare_two_mode ) {
$revision_date_author = sprintf( $compare_to_gravatar = get_avatar( $left_revision->post_author, 18 );
'%s %s, %s %s (%s)', $compare_to_author = get_the_author_meta( 'display_name', $left_revision->post_author );
$gravatar, $compare_to_date = date_i18n( $datef, strtotime( $left_revision->post_modified ) );
$author,
human_time_diff( strtotime( $revision->post_modified ), current_time( 'timestamp' ) ),
__( ' ago ' ),
$date
);
if ( $comparetwomode ) { $revision_from_date_author = sprintf(
$compareto_gravatar = get_avatar( $left_revision->post_author, 18 ); '%s %s, %s %s (%s)',
$compareto_author = get_the_author_meta( 'display_name', $left_revision->post_author ); $compare_to_gravatar,
$compareto_date = date_i18n( $datef, strtotime( $left_revision->post_modified ) ); $compare_to_author,
human_time_diff( strtotime( $left_revision->post_modified ), current_time( 'timestamp' ) ),
__( ' ago ' ),
$compare_to_date
);
}
$revision_from_date_author = sprintf( //
//make sure the left revision is the most recent
//
if ( strtotime( $right_revision->post_modified_gmt ) < strtotime( $left_revision->post_modified_gmt ) ) {
$temp = $left_revision;
$left_revision = $right_revision;
$right_revision = $temp;
}
//
//compare from left to right, passed from application
//
$content='';
foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
$left_content = apply_filters( "_wp_post_revision_field_$field", $left_revision->$field, $field, $left_revision, 'left' );
$right_content = apply_filters( "_wp_post_revision_field_$field", $right_revision->$field, $field, $right_revision, 'right' );
add_filter( "_wp_post_revision_field_$field", 'wp_kses_post' );
$args = array();
if ( ! empty( $show_split_view ) )
$args = array( 'show_split_view' => true );
$content .= wp_text_diff( $left_content, $right_content, $args );
}
$content = '' == $content ? __( 'No difference' ) : $content;
$alltherevisions = array (
'revisiondiff' => $content
);
echo json_encode( $alltherevisions );
exit();
}
//if we are comparing two revisions, the first 'revision' represented by the leftmost
//slider position is the current revision, prepend a comparison to this revision
if ( $compare_two_mode )
array_unshift( $revisions, get_post( $post_id ) );
$count = -1;
foreach ( $revisions as $revision ) :
if ( ! empty( $show_autosaves ) && wp_is_post_autosave( $revision ) )
continue;
$revision_from_date_author = '';
$count++;
// return blank data for diffs to the left of the left handle (for right handel model)
// or to the right of the right handle (for left handel model)
if ( ( 0 != $left_handle_at && $count <= $left_handle_at ) ||
( 0 != $right_handle_at && $count > $right_handle_at )) {
$alltherevisions[] = array (
'ID' => $revision->ID,
);
continue;
}
$gravatar = get_avatar( $revision->post_author, 18 );
$author = get_the_author_meta( 'display_name', $revision->post_author );
$date = date_i18n( $datef, strtotime( $revision->post_modified ) );
$revision_date_author = sprintf(
'%s %s, %s %s (%s)', '%s %s, %s %s (%s)',
$compareto_gravatar, $gravatar,
$compareto_author, $author,
human_time_diff( strtotime( $left_revision->post_modified ), current_time( 'timestamp' ) ), human_time_diff( strtotime( $revision->post_modified ), current_time( 'timestamp' ) ),
__( ' ago ' ), __( ' ago ' ),
$compareto_date $date
); );
}
$restoreaction = wp_nonce_url( $restoreaction = wp_nonce_url(
add_query_arg( add_query_arg(
array( 'revision' => $revision->ID, array( 'revision' => $revision->ID,
'action' => 'restore' ), 'action' => 'restore' ),
'/wp-admin/revision.php' admin_url( 'revision.php' )
), ),
"restore-post_{$compareto}|{$revision->ID}" "restore-post_{$compare_to}|{$revision->ID}"
); );
//
//make sure the left revision is the most recent
//
if ( strtotime( $right_revision->post_modified_gmt ) < strtotime( $left_revision->post_modified_gmt ) ) {
$temp = $left_revision;
$left_revision = $right_revision;
$right_revision = $temp;
}
//
//compare from left to right, passed from application
//
$content='';
foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
$left_content = apply_filters( "_wp_post_revision_field_$field", $left_revision->$field, $field, $left_revision, 'left' );
$right_content = apply_filters( "_wp_post_revision_field_$field", $right_revision->$field, $field, $right_revision, 'right' );
add_filter( "_wp_post_revision_field_$field", 'wp_kses_post' );
$args = array();
if ( 'true' == $showsplitview )
$args = array( 'showsplitview' => 'true' );
$content .= wp_text_diff( $left_content, $right_content, $args );
}
//if we are comparing two revisions
//and we are on the matching revision
//add an error revision indicating unable to compare to self
if ( $comparetwomode && $compareto == $revision->ID )
$alltherevisions[] = array ( $alltherevisions[] = array (
'ID' => $revision->ID, 'ID' => $revision->ID,
'revision_date_author' => $revision_date_author, 'revision_date_author' => $revision_date_author,
'revisiondiff' => sprintf('<div id="selfcomparisonerror">%s</div>', __( 'Cannot compare revision to itself' ) ), 'revision_from_date_author' => $revision_from_date_author,
'restoreaction' => urldecode( $restoreaction ), 'restoreaction' => urldecode( $restoreaction ),
'revision_from_date_author' => '' 'revision_toload' => true
); );
//add to the return data only if there is a difference
if ( '' != $content )
$alltherevisions[] = array (
'ID' => $revision->ID,
'revision_date_author' => $revision_date_author,
'revisiondiff' => $content,
'restoreaction' => urldecode( $restoreaction ),
'revision_from_date_author' => $revision_from_date_author
);
endforeach; endforeach;

View File

@ -6,16 +6,22 @@ window.wp = window.wp || {};
views : {}, views : {},
Model : Backbone.Model.extend({ Model : Backbone.Model.extend({
idAttribute : 'ID',
urlRoot : ajaxurl + '?action=revisions-data&compare_to=' + wpRevisionsSettings.post_id +
'&show_autosaves=false&show_split_view=true&nonce=' + wpRevisionsSettings.nonce,
defaults: { defaults: {
ID : 0, ID : 0,
revision_date_author : '', revision_date_author : '',
revisiondiff : '', revisiondiff : '<div class="diff-loading"><div class="spinner"></div></div>',
restoreaction: '', restoreaction : '',
diff_max : 0,
diff_count : 0,
diff_revision_to : 0,
revision_from_date_author : '', revision_from_date_author : '',
revision_toload : false
},
url : function() {
return this.urlRoot + '&single_revision_id=' + this.id;
} }
}), }),
app: _.extend({}, Backbone.Events), app: _.extend({}, Backbone.Events),
@ -30,42 +36,68 @@ window.wp = window.wp || {};
_left_diff : 0, _left_diff : 0,
_right_diff : 1, _right_diff : 1,
_autosaves : false, _autosaves : false,
_showsplitview : true, _show_split_view : true,
_compareoneortwo : 1, _compareoneortwo : 1,
left_model_loading : false, //keep track of model loads _left_model_loading : false, //keep track of model loads
right_model_loading : false, //disallow slider interaction, also repeat loads, while loading _right_model_loading : false, //disallow slider interaction, also repeat loads, while loading
//TODO add ability to arrive on specific revision //TODO add ability to arrive on specific revision
routes : { routes : {
"viewrevision/:revision": "viewrevision",
}, },
viewrevision : function( revision ) { viewrevision : function( revision ) {
//coming soon //coming soon
}, },
reload_toload_revisions : function( model_collection, reverse_direction ) {
var self = this;
var revisions_to_load = model_collection.where( { revision_toload : true } );
//console.log(revisions_to_load);
var delay=0;
_.each(revisions_to_load, function( the_model ) {
the_model.urlRoot = model_collection.url;
_.delay( function() {
the_model.fetch( {
update : true,
add : false,
remove : false,
//async : false,
success : function( model ) {
//console.log(model.get( 'ID' ) +'-'+self._revisions.at( self._right_diff ).get( 'ID' ));
if ( model.get( 'ID' ) === self._revisions.at( self._right_diff - 1 ).get( 'ID' ) ) { //reload if current model refreshed
//console.log('render');
self._revisionView.render();
}
}
} );
}, delay ) ;
delay = delay + 200; //stagger model loads by 200 ms to avoid hammering server with requests
}
);
},
start_left_model_loading : function() { start_left_model_loading : function() {
this.left_model_loading = true; this._left_model_loading = true;
$('.revisiondiffcontainer').addClass('leftmodelloading'); $('.revisiondiffcontainer').addClass('leftmodelloading');
}, },
stop_left_model_loading : function() { stop_left_model_loading : function() {
this.left_model_loading = false; this._left_model_loading = false;
$('.revisiondiffcontainer').removeClass('leftmodelloading'); $('.revisiondiffcontainer').removeClass('leftmodelloading');
}, },
start_right_model_loading : function() { start_right_model_loading : function() {
this.right_model_loading = true; this._right_model_loading = true;
$('.revisiondiffcontainer').addClass('rightmodelloading'); $('.revisiondiffcontainer').addClass('rightmodelloading');
}, },
stop_right_model_loading : function() { stop_right_model_loading : function() {
this.right_model_loading = false; this._right_model_loading = false;
$('.revisiondiffcontainer').removeClass('rightmodelloading'); $('.revisiondiffcontainer').removeClass('rightmodelloading');
}, },
reloadmodel : function() { reloadmodel : function() {
if ( 2 == this._compareoneortwo ) { if ( 2 === this._compareoneortwo ) {
this.reloadleftright(); this.reloadleftright();
} else { } else {
this.reloadmodelsingle(); this.reloadmodelsingle();
@ -74,9 +106,9 @@ window.wp = window.wp || {};
reloadmodelsingle : function() { reloadmodelsingle : function() {
var self = this; var self = this;
self._revisions.url = ajaxurl + '?action=revisions-data&compareto=' + wpRevisionsSettings.post_id + self._revisions.url = ajaxurl + '?action=revisions-data&compare_to=' + wpRevisionsSettings.post_id +
'&showautosaves=' + self.self_autosaves + '&show_autosaves=' + self._autosaves +
'&showsplitview=' + REVAPP._showsplitview + '&show_split_view=' + REVAPP._show_split_view +
'&nonce=' + wpRevisionsSettings.nonce; '&nonce=' + wpRevisionsSettings.nonce;
self.start_right_model_loading(); self.start_right_model_loading();
this._revisions.fetch({ //reload revision data this._revisions.fetch({ //reload revision data
@ -85,92 +117,89 @@ window.wp = window.wp || {};
var revisioncount = self._revisions.length; var revisioncount = self._revisions.length;
if ( self._right_diff > revisioncount ) //if right handle past rightmost, move if ( self._right_diff > revisioncount ) //if right handle past rightmost, move
self._right_diff = revisioncount; self._right_diff = revisioncount;
//TODO add a test for matchind left revision and push left, testing
//also reset the slider values here
self._revisionView.render(); self._revisionView.render();
$( '#slider' ).slider( 'option', 'max', revisioncount-1 ); //TODO test this self.reload_toload_revisions( self._revisions );
$( '#slider' ).slider( 'option', 'max', revisioncount-1 ); //TODO test this, autsaves changed
}, },
error : function () { error : function () {
self.stop_right_model_loading(); self.stop_right_model_loading();
window.console && console.log( 'Error loading revision data' ); //console.log( 'Error loading revision data' );
} }
}); });
}, },
reloadleftright : function() { reloadleft : function() {
var self = this; var self = this;
self.start_left_model_loading(); self.start_left_model_loading();
self.start_right_model_loading();
self._left_handle_revisions = new wp.revisions.Collection(); self._left_handle_revisions = new wp.revisions.Collection();
self._right_handle_revisions = new wp.revisions.Collection(); self._left_handle_revisions.url =
ajaxurl +
'?action=revisions-data&compare_to=' + self._revisions.at( self._right_diff - 1 ).get( 'ID' ) +
'&post_id=' + wpRevisionsSettings.post_id +
'&show_autosaves=' + self._autosaves +
'&show_split_view=' + self._show_split_view +
'&nonce=' + wpRevisionsSettings.nonce +
'&right_handle_at=' + ( self._right_diff );
if ( 0 == self._left_diff ) { self._left_handle_revisions.fetch({
success : function(){
self.stop_left_model_loading();
self.reload_toload_revisions( self._left_handle_revisions );
},
error : function () {
//console.log( 'Error loading revision data' );
self.stop_left_model_loading();
}
});
},
reloadright : function() {
var self = this;
self.start_right_model_loading();
self._right_handle_revisions = new wp.revisions.Collection();
if ( 0 === self._left_diff ) {
self._right_handle_revisions.url = self._right_handle_revisions.url =
ajaxurl + ajaxurl +
'?action=revisions-data&compareto=' + wpRevisionsSettings.post_id + '?action=revisions-data&compare_to=' + wpRevisionsSettings.post_id +
'&post_id=' + wpRevisionsSettings.post_id + '&post_id=' + wpRevisionsSettings.post_id +
'&showautosaves=' + self._autosaves + '&show_autosaves=' + self._autosaves +
'&showsplitview=' + self._showsplitview + '&show_split_view=' + self._show_split_view +
'&nonce=' + wpRevisionsSettings.nonce; '&nonce=' + wpRevisionsSettings.nonce;
} else { } else {
self._right_handle_revisions.url = self._right_handle_revisions.url =
ajaxurl + ajaxurl +
'?action=revisions-data&compareto=' + self._revisions.at( self._left_diff - 1 ).get( 'ID' ) + '?action=revisions-data&compare_to=' + self._revisions.at( self._left_diff - 1 ).get( 'ID' ) +
'&post_id=' + wpRevisionsSettings.post_id + '&post_id=' + wpRevisionsSettings.post_id +
'&showautosaves=' + self._autosaves + '&show_autosaves=' + self._autosaves +
'&showsplitview=' + self._showsplitview + '&show_split_view=' + self._show_split_view +
'&nonce=' + wpRevisionsSettings.nonce; '&nonce=' + wpRevisionsSettings.nonce +
'&left_handle_at=' + (self._left_diff ) ;
} }
self._left_handle_revisions.url =
ajaxurl +
'?action=revisions-data&compareto=' + self._revisions.at( self._right_diff - 1 ).get( 'ID' ) +
'&post_id=' + wpRevisionsSettings.post_id +
'&showautosaves=' + self._autosaves +
'&showsplitview=' + self._showsplitview +
'&nonce=' + wpRevisionsSettings.nonce;
self._left_handle_revisions.fetch({
xhr: function() {
var xhr = $.ajaxSettings.xhr();
xhr.onprogress = self.handleProgress;
return xhr;
},
handleProgress: function(evt){
var percentComplete = 0;
if (evt.lengthComputable) {
percentComplete = evt.loaded / evt.total;
window.console && console.log( Math.round( percentComplete * 100) + "%" );
}
},
success : function(){
self.stop_left_model_loading();
},
error : function () {
window.console && console.log( 'Error loading revision data' );
self.stop_left_model_loading();
}
});
self._right_handle_revisions.fetch({ self._right_handle_revisions.fetch({
success : function(){ success : function(){
self.stop_right_model_loading(); self.stop_right_model_loading();
self.reload_toload_revisions( self._right_handle_revisions );
}, },
error : function () { error : function ( response ) {
window.console && console.log( 'Error loading revision data' ); //console.log( 'Error loading revision data - ' + response.toSource() );
self.stop_right_model_loading(); self.stop_right_model_loading();
} }
}); });
},
reloadleftright : function() {
this.reloadleft();
this.reloadright();
}, },
/* /*
@ -194,22 +223,27 @@ window.wp = window.wp || {};
}, },
revisionDiffSetup : function() { revisionDiffSetup : function() {
var self = this, slider;
this._revisionView = new wp.revisions.views.View({ this._revisionView = new wp.revisions.views.View({
model : this._revisions model : this._revisions
}); });
this._revisionView.render(); this._revisionView.render();
$( '#diff_max, #diff_maxof' ).html( this._revisions.length );
$( '#diff_count' ).html( REVAPP._right_diff );
$( '#slider' ).slider( 'option', 'max', this._revisions.length - 1 );
this.reload_toload_revisions( this._revisions );
this._revisionsInteractions = new wp.revisions.views.Interact({ this._revisionsInteractions = new wp.revisions.views.Interact({
model : this._revisions model : this._revisions
}); });
this._revisionsInteractions.render(); this._revisionsInteractions.render();
/*
//Options hidden for now, moving to screen options
this._revisionsOptions = new wp.revisions.views.Options({ this._revisionsOptions = new wp.revisions.views.Options({
model : this._revisions model : this._revisions
}); });
this._revisionsOptions.render(); this._revisionsOptions.render();
*/
} }
}) })
@ -217,8 +251,12 @@ window.wp = window.wp || {};
wp.revisions.Collection = Backbone.Collection.extend({ wp.revisions.Collection = Backbone.Collection.extend({
model : wp.revisions.Model, model : wp.revisions.Model,
url : ajaxurl + '?action=revisions-data&compareto=' + wpRevisionsSettings.post_id + '&showautosaves=false&showsplitview=true&nonce=' + wpRevisionsSettings.nonce url : ajaxurl + '?action=revisions-data&compare_to=' + wpRevisionsSettings.post_id +
}); '&show_autosaves=false&show_split_view=true&nonce=' + wpRevisionsSettings.nonce,
initialize : function() {
}
} );
_.extend(wp.revisions.views, { _.extend(wp.revisions.views, {
// //
@ -242,7 +280,7 @@ window.wp = window.wp || {};
render : function() { render : function() {
var addhtml = ''; var addhtml = '';
//compare two revisions mode? //compare two revisions mode?
if ( 2 == REVAPP._compareoneortwo ) { if ( 2 === REVAPP._compareoneortwo ) {
this.comparetwochecked = 'checked'; this.comparetwochecked = 'checked';
if ( this.draggingleft ) { if ( this.draggingleft ) {
if ( this.model.at( REVAPP._left_diff ) ) { if ( this.model.at( REVAPP._left_diff ) ) {
@ -260,16 +298,21 @@ window.wp = window.wp || {};
) ); ) );
} }
} }
} else { //end compare two revisions mode, eg only one slider handel } else { //end compare two revisions mode, eg only one slider handle
this.comparetwochecked = ''; this.comparetwochecked = '';
if ( this.model.at( REVAPP._right_diff - 1 ) ) { if ( this.model.at( REVAPP._right_diff - 1 ) ) {
addhtml = this.template( _.extend( addhtml = this.template( _.extend(
this.model.at( REVAPP._right_diff-1 ).toJSON(), this.model.at( REVAPP._right_diff - 1 ).toJSON(),
{ comparetwochecked : this.comparetwochecked } //keep the checkmark checked { comparetwochecked : this.comparetwochecked } //keep the checkmark unchecked
) ); ) );
} }
} }
this.$el.html( addhtml ); this.$el.html( addhtml );
if ( this.model.length < 3 ) {
$( 'div#comparetworevisions' ).hide(); //don't allow compare two if fewer than three revisions
}
//console.log ( (this.model.at( REVAPP._right_diff - 1 )).url());
return this; return this;
}, },
@ -322,7 +365,7 @@ window.wp = window.wp || {};
}, },
// //
//toggle include autosaves //toggle include autosaves
// //
toggleshowautosaves : function() { toggleshowautosaves : function() {
var self = this; var self = this;
@ -331,11 +374,9 @@ window.wp = window.wp || {};
} else { } else {
REVAPP._autosaves = false ; REVAPP._autosaves = false ;
} }
//refresh the model data
//refresh the model data
REVAPP.reloadmodel(); REVAPP.reloadmodel();
//TODO check for two handle mode
}, },
// //
@ -345,10 +386,10 @@ window.wp = window.wp || {};
var self = this; var self = this;
if ( $( 'input#showsplitview' ).is( ':checked' ) ) { if ( $( 'input#showsplitview' ).is( ':checked' ) ) {
REVAPP._showsplitview = 'true'; REVAPP._show_split_view = 'true';
$('.revisiondiffcontainer').addClass('diffsplit'); $('.revisiondiffcontainer').addClass('diffsplit');
} else { } else {
REVAPP._showsplitview = ''; REVAPP._show_split_view = '';
$('.revisiondiffcontainer').removeClass('diffsplit'); $('.revisiondiffcontainer').removeClass('diffsplit');
} }
@ -364,8 +405,14 @@ window.wp = window.wp || {};
tagName : 'revisionvinteract', tagName : 'revisionvinteract',
className : 'revisionvinteract-container', className : 'revisionvinteract-container',
template : wp.template('revisionvinteract'), template : wp.template('revisionvinteract'),
_restoreword : '',
initialize : function() { initialize : function() {
this._restoreword = $( 'input#restore' ).attr( 'value' );
},
reset_restore_button : function() {
$( 'input#restore' ).attr( 'value', this._restoreword + ' ' + REVAPP._revisions.at( REVAPP._right_diff - 1 ).get( 'ID' ) );
}, },
render : function() { render : function() {
@ -375,12 +422,13 @@ window.wp = window.wp || {};
this.$el.html( addhtml ); this.$el.html( addhtml );
$( '#diff_max, #diff_maxof' ).html( this.model.length ); $( '#diff_max, #diff_maxof' ).html( this.model.length );
$( '#diff_count' ).html( REVAPP._right_diff ); $( '#diff_count' ).html( REVAPP._right_diff );
$( '#diff_left_count_inner' ).html( 0 == REVAPP._left_diff ? '' : 'revision' + REVAPP._left_diff ); $( '#diff_left_count_inner' ).html( 0 === REVAPP._left_diff ? '' : 'revision' + REVAPP._left_diff );
self.reset_restore_button();
var modelcount = REVAPP._revisions.length; var modelcount = REVAPP._revisions.length;
slider = $("#slider"); slider = $( "#slider" );
if ( 1 == REVAPP._compareoneortwo ) { if ( 1 === REVAPP._compareoneortwo ) {
//set up the slider with a single handle //set up the slider with a single handle
slider.slider({ slider.slider({
value : REVAPP._right_diff-1, value : REVAPP._right_diff-1,
@ -390,12 +438,13 @@ window.wp = window.wp || {};
//slide interactions for one handles slider //slide interactions for one handles slider
slide : function( event, ui ) { slide : function( event, ui ) {
if ( REVAPP.right_model_loading ) //left model stoll loading, prevent sliding left handle if ( REVAPP._right_model_loading ) //left model stoll loading, prevent sliding left handle
return false; return false;
REVAPP._right_diff =( ui.value+1 ); REVAPP._right_diff =( ui.value+1 );
$( '#diff_count' ).html( REVAPP._right_diff ); $( '#diff_count' ).html( REVAPP._right_diff );
REVAPP._revisionView.render(); REVAPP._revisionView.render();
self.reset_restore_button();
} }
}); });
$( '.revisiondiffcontainer' ).removeClass( 'comparetwo' ); $( '.revisiondiffcontainer' ).removeClass( 'comparetwo' );
@ -414,57 +463,58 @@ window.wp = window.wp || {};
switch ( index ) { switch ( index ) {
case 1: //left handle drag case 1: //left handle drag
if ( REVAPP.left_model_loading ) //left model stoll loading, prevent sliding left handle if ( REVAPP._left_model_loading ) //left model stoll loading, prevent sliding left handle
return false; return false;
if ( REVAPP._revisionView.model !== REVAPP._left_handle_revisions && if ( REVAPP._revisionView.model !== REVAPP._left_handle_revisions &&
null != REVAPP._left_handle_revisions ) null !== REVAPP._left_handle_revisions )
REVAPP._revisionView.model = REVAPP._left_handle_revisions; REVAPP._revisionView.model = REVAPP._left_handle_revisions;
REVAPP._revisionView.draggingleft = true; REVAPP._revisionView.draggingleft = true;
REVAPP._left_diff_start = ui.values[ 0 ];
break; break;
case 2: //right case 2: //right
if ( REVAPP.right_model_loading ) //right model stoll loading, prevent sliding right handle if ( REVAPP._right_model_loading ) //right model stoll loading, prevent sliding right handle
return false; return false;
//one extra spot at left end when comparing two //one extra spot at left end when comparing two
if ( REVAPP._revisionView.model !== REVAPP._right_handle_revisions && if ( REVAPP._revisionView.model !== REVAPP._right_handle_revisions &&
null != REVAPP._right_handle_revisions ) null !== REVAPP._right_handle_revisions )
REVAPP._revisionView.model = REVAPP._right_handle_revisions; REVAPP._revisionView.model = REVAPP._right_handle_revisions;
REVAPP._revisionView.draggingleft = false; REVAPP._revisionView.draggingleft = false;
REVAPP._right_diff = ui.values[1] - 1 ; REVAPP._right_diff_start = ui.values[ 1 ];
break; break;
} }
}, },
//when sliding in two handled mode change appropriate value //when sliding in two handled mode change appropriate value
slide : function( event, ui ) { slide : function( event, ui ) {
if ( ui.values[0] == ui.values[1] ) //prevent compare to self if ( ui.values[ 0 ] === ui.values[ 1 ] ) //prevent compare to self
return false; return false;
var index = $( ui.handle ).index(); //0 (left) or 1 (right) var index = $( ui.handle ).index(); //0 (left) or 1 (right)
switch ( index ) { switch ( index ) {
case 1: //left case 1: //left
if ( REVAPP.left_model_loading ) //left model stoll loading, prevent sliding left handle if ( REVAPP._left_model_loading ) //left model still loading, prevent sliding left handle
return false; return false;
REVAPP._left_diff = ui.values[0] - 1; //one extra spot at left end when comparing two REVAPP._left_diff = ui.values[ 0 ] - 1; //one extra spot at left end when comparing two
break; break;
case 2: //right case 2: //right
if ( REVAPP.right_model_loading ) //right model stoll loading, prevent sliding right handle if ( REVAPP._right_model_loading ) //right model still loading, prevent sliding right handle
return false; return false;
REVAPP._right_diff = ui.values[1] - 1 ; REVAPP._right_diff = ui.values[ 1 ] - 1 ;
break; break;
} }
$( '#diff_count' ).html( REVAPP._right_diff ); $( '#diff_count' ).html( REVAPP._right_diff );
if ( 0 == REVAPP._left_diff ) { if ( 0 === REVAPP._left_diff ) {
$( '.revisiondiffcontainer' ).addClass( 'currentversion' ); $( '.revisiondiffcontainer' ).addClass( 'currentversion' );
} else { } else {
@ -473,15 +523,31 @@ window.wp = window.wp || {};
} }
REVAPP._revisionView.render(); //render the diff view REVAPP._revisionView.render(); //render the diff view
self.reset_restore_button();
}, },
//when the user stops sliding in 2 handle mode, recalculate diffs //when the user stops sliding in 2 handle mode, recalculate diffs
stop : function( event, ui ) { stop : function( event, ui ) {
if ( 2 == REVAPP._compareoneortwo ) { if ( 2 === REVAPP._compareoneortwo ) {
//calculate and generate a diff for comparing to the left handle //calculate and generate a diff for comparing to the left handle
//and the right handle, swap out when dragging //and the right handle, swap out when dragging
if ( ! (REVAPP.left_model_loading && REVAPP.right_model.loading ) ) {
REVAPP.reloadleftright(); var index = $( ui.handle ).index(); //0 (left) or 1 (right)
switch ( index ) {
case 1: //left
//left handle dragged & changed, reload right handle model
if ( ! ( REVAPP._left_diff_start === ui.values[ 0 ] || REVAPP._left_model_loading ) )
REVAPP.reloadright();
break;
case 2: //right
//right handle dragged & changed, reload left handle model if changed
if ( ! ( REVAPP._right_diff_start === ui.values[ 1 ] || REVAPP._right_model_loading ) ) {
REVAPP.reloadleft();
}
break;
} }
} }
} }
@ -507,6 +573,7 @@ window.wp = window.wp || {};
$( '#diff_count' ).html( REVAPP._right_diff ); $( '#diff_count' ).html( REVAPP._right_diff );
$( '#slider' ).slider( 'value', REVAPP._right_diff - 1 ).trigger( 'slide' ); $( '#slider' ).slider( 'value', REVAPP._right_diff - 1 ).trigger( 'slide' );
this.reset_restore_button();
}, },
//go the the previous revision //go the the previous revision
@ -518,6 +585,7 @@ window.wp = window.wp || {};
$( '#diff_count' ).html( REVAPP._right_diff ); $( '#diff_count' ).html( REVAPP._right_diff );
$( '#slider' ).slider( 'value', REVAPP._right_diff - 1 ).trigger( 'slide' ); $( '#slider' ).slider( 'value', REVAPP._right_diff - 1 ).trigger( 'slide' );
this.reset_restore_button();
} }
}) })
}); });
@ -525,6 +593,6 @@ window.wp = window.wp || {};
//instantiate Revision Application //instantiate Revision Application
REVAPP = new wp.revisions.App(); REVAPP = new wp.revisions.App();
//TODO consider enable back button to step back thru states? //TODO consider enable back button to step back thru states?
Backbone.history.start(); //Backbone.history.start({pushState: true});
}(jQuery)); }(jQuery));

View File

@ -10,37 +10,45 @@
require_once('./admin.php'); require_once('./admin.php');
wp_reset_vars( array( 'revision', 'action' ) ); wp_reset_vars( array( 'revision', 'action' ) );
$revision_id = absint($revision); $revision_id = absint( $revision );
$redirect = 'edit.php'; $redirect = 'edit.php';
switch ( $action ) : switch ( $action ) :
case 'restore' : case 'restore' :
if ( !$revision = wp_get_post_revision( $revision_id ) ) if ( ! $revision = wp_get_post_revision( $revision_id ) )
break; break;
if ( !current_user_can( 'edit_post', $revision->post_parent ) ) if ( ! current_user_can( 'edit_post', $revision->post_parent ) )
break; break;
if ( !$post = get_post( $revision->post_parent ) ) if ( ! $post = get_post( $revision->post_parent ) )
break; break;
// Revisions disabled and we're not looking at an autosave // Revisions disabled and we're not looking at an autosave
if ( ( ! WP_POST_REVISIONS || !post_type_supports($post->post_type, 'revisions') ) && !wp_is_post_autosave( $revision ) ) { if ( ( ! WP_POST_REVISIONS || ! post_type_supports( $post->post_type, 'revisions' ) ) && ! wp_is_post_autosave( $revision ) ) {
$redirect = 'edit.php?post_type=' . $post->post_type; $redirect = 'edit.php?post_type=' . $post->post_type;
break; break;
} }
check_admin_referer( "restore-post_{$post->ID}|{$revision->ID}" ); check_admin_referer( "restore-post_{$post->ID}|{$revision->ID}" );
//store revision event in post meta
$restore_details = array(
'restored_revision_id' => $revision->ID,
'restored_by_user' => get_current_user_id(),
'restored_time' => time()
);
update_post_meta( $post->ID, '_post_restored_from', $restore_details );
wp_restore_post_revision( $revision->ID ); wp_restore_post_revision( $revision->ID );
$redirect = add_query_arg( array( 'message' => 5, 'revision' => $revision->ID ), get_edit_post_link( $post->ID, 'url' ) ); $redirect = add_query_arg( array( 'message' => 5, 'revision' => $revision->ID ), get_edit_post_link( $post->ID, 'url' ) );
break; break;
case 'view' : case 'view' :
case 'edit' : case 'edit' :
default : default :
if ( !$revision = wp_get_post_revision( $revision_id ) ) if ( ! $revision = wp_get_post_revision( $revision_id ) )
break; break;
if ( !$post = get_post( $revision->post_parent ) ) if ( ! $post = get_post( $revision->post_parent ) )
break; break;
if ( !current_user_can( 'read_post', $revision->ID ) || !current_user_can( 'read_post', $post->ID ) ) if ( ! current_user_can( 'read_post', $revision->ID ) || ! current_user_can( 'read_post', $post->ID ) )
break; break;
// Revisions disabled and we're not looking at an autosave // Revisions disabled and we're not looking at an autosave
@ -59,16 +67,16 @@ default :
endswitch; endswitch;
// Empty post_type means either malformed object found, or no valid parent was found. // Empty post_type means either malformed object found, or no valid parent was found.
if ( !$redirect && empty($post->post_type) ) if ( ! $redirect && empty( $post->post_type ) )
$redirect = 'edit.php'; $redirect = 'edit.php';
if ( !empty($redirect) ) { if ( ! empty( $redirect ) ) {
wp_redirect( $redirect ); wp_redirect( $redirect );
exit; exit;
} }
// This is so that the correct "Edit" menu item is selected. // This is so that the correct "Edit" menu item is selected.
if ( !empty($post->post_type) && 'post' != $post->post_type ) if ( ! empty( $post->post_type ) && 'post' != $post->post_type )
$parent_file = $submenu_file = 'edit.php?post_type=' . $post->post_type; $parent_file = $submenu_file = 'edit.php?post_type=' . $post->post_type;
else else
$parent_file = $submenu_file = 'edit.php'; $parent_file = $submenu_file = 'edit.php';
@ -83,10 +91,11 @@ require_once( './admin-header.php' );
<script type="text/javascript"> <script type="text/javascript">
var wpRevisionsSettings = <?php echo json_encode( array( 'post_id' => $post->ID, 'nonce' => wp_create_nonce( 'revisions-ajax-nonce' ) ) ); ?>; var wpRevisionsSettings = <?php echo json_encode( array( 'post_id' => $post->ID, 'nonce' => wp_create_nonce( 'revisions-ajax-nonce' ) ) ); ?>;
</script> </script>
<?php
$comparetworevisionslink = get_edit_post_link( $revision->ID );
?>
<div id="backbonerevisionsoptions"></div> <div id="backbonerevisionsoptions"></div>
<br class="clear"/>
<div class="wrap"> <div class="wrap">
<div class="icon32 icon32-posts-post" id="icon-edit"><br></div> <div class="icon32 icon32-posts-post" id="icon-edit"><br></div>
<div class="revisiondiffcontainer diffsplit currentversion rightmodelloading"> <div class="revisiondiffcontainer diffsplit currentversion rightmodelloading">
@ -94,10 +103,7 @@ var wpRevisionsSettings = <?php echo json_encode( array( 'post_id' => $post->ID,
<h2 class="long-header"><?php echo $h2; ?></h2> <h2 class="long-header"><?php echo $h2; ?></h2>
<div id="backbonerevisionsinteract"></div> <div id="backbonerevisionsinteract"></div>
<div id="backbonerevisionsdiff"></div> <div id="backbonerevisionsdiff"></div>
<hr /> <hr />
<?php
$comparetworevisionslink = get_edit_post_link( $revision->ID );
?>
</div> </div>
</div> </div>
@ -107,9 +113,8 @@ var wpRevisionsSettings = <?php echo json_encode( array( 'post_id' => $post->ID,
<div id="difftitlefrom">{{{ data.revision_from_date_author }}} <?php _e( '- compared to -' ); ?></div> <div id="difftitlefrom">{{{ data.revision_from_date_author }}} <?php _e( '- compared to -' ); ?></div>
<div id="difftitle">{{{ data.revision_date_author }}}</div> <div id="difftitle">{{{ data.revision_date_author }}}</div>
<div id="diffcancel"><input class="button" onClick="document.location='<?php echo get_edit_post_link( $post->ID ); ?>'" type="submit" id="cancel" value="<?php esc_attr_e( 'Cancel' )?>" /></div> <div id="diffcancel"><input class="button" onClick="document.location='<?php echo get_edit_post_link( $post->ID ); ?>'" type="submit" id="cancel" value="<?php esc_attr_e( 'Cancel' )?>" /></div>
<div id="diffrestore"><input class="button button-primary" onClick="document.location='{{{ data.restoreaction }}}'" type="submit" id="restore" value="<?php esc_attr_e( 'Restore' )?>" /></div> <div id="diffrestore"><input class="button button-primary" onClick="document.location='{{{ data.restoreaction }}}'" type="submit" id="restore" value="<?php esc_attr_e( 'Restore revision ID' )?>" /></div>
<div id="comparetworevisions"><input type="checkbox" id="comparetwo" value="comparetwo" {{{ data.comparetwochecked }}} name="comparetwo"/> <?php esc_attr_e( 'Compare two revisions' )?></div> <div id="comparetworevisions"><input type="checkbox" id="comparetwo" value="comparetwo" {{{ data.comparetwochecked }}} name="comparetwo"/> <label for="comparetwo"><?php esc_attr_e( 'Compare two revisions' ); ?></a></div> </div>
</div>
<div id="removedandadded"> <div id="removedandadded">
<div id="removed"><?php _e( 'Removed -' ); ?></div> <div id="removed"><?php _e( 'Removed -' ); ?></div>
<div id="added"><?php _e( 'Added +' ); ?></div> <div id="added"><?php _e( 'Added +' ); ?></div>
@ -119,8 +124,8 @@ var wpRevisionsSettings = <?php echo json_encode( array( 'post_id' => $post->ID,
<script id="tmpl-revisionvinteract" type="text/html"> <script id="tmpl-revisionvinteract" type="text/html">
<div id="diffheader"> <div id="diffheader">
<div id="diffprevious"><input class="button" type="submit" id="previous" value="Previous" /></div> <div id="diffprevious"><input class="button" type="submit" id="previous" value="<?php esc_attr_e( 'Previous' ); ?>" /></div>
<div id="diffnext"><input class="button" type="submit" id="next" value="Next" /></div> <div id="diffnext"><input class="button" type="submit" id="next" value="<?php esc_attr_e( 'Next' ); ?>" /></div>
<div id="diffslider"> <div id="diffslider">
<div id="revisioncount"> <div id="revisioncount">
<?php _e( 'Comparing' ); ?> <?php _e( 'Comparing' ); ?>

View File

@ -1719,7 +1719,7 @@ function wp_text_diff( $left_string, $right_string, $args = null ) {
$r = "<table class='diff'>\n"; $r = "<table class='diff'>\n";
if ( isset( $args[ 'showsplitview' ] ) && 'true' == $args[ 'showsplitview' ] ) { if ( ! empty( $args[ 'show_split_view' ] ) ) {
$r .= "<col class='content diffsplit left' /><col class='content diffsplit middle' /><col class='content diffsplit right' />"; $r .= "<col class='content diffsplit left' /><col class='content diffsplit middle' /><col class='content diffsplit right' />";
} else { } else {
$r .= "<col class='content' />"; $r .= "<col class='content' />";

View File

@ -1468,7 +1468,32 @@ function wp_list_post_revisions( $post_id = 0, $args = null ) {
else : else :
echo "<ul class='post-revisions'>\n"; echo "<ul class='post-revisions'>\n";
echo $rows; echo $rows;
//
// if the post was previously restored from a revision
// show the restore event details
//
if ( $restored_from_meta = get_post_meta( $post->ID, '_post_restored_from', true ) ) {
$author = get_the_author_meta( 'display_name', $restored_from_meta[ 'restored_by_user' ] );
/* translators: revision date format, see http://php.net/date */
$datef = _x( 'j F, Y @ G:i:s', 'revision date format');
$date = date_i18n( $datef, strtotime( $restored_from_meta[ 'restored_time' ] ) );
$timesince = human_time_diff( $restored_from_meta[ 'restored_time' ], current_time( 'timestamp' ) ) . __( ' ago ' );
?>
<hr />
<div id="revisions-meta-restored">
<?php
printf( 'Previously restored from Revision ID %d, %s by %s (%s)',
$restored_from_meta[ 'restored_revision_id'],
$timesince,
$author,
$date );
?>
</div>
<?php
echo "</ul>"; echo "</ul>";
}
endif; endif;
} }