Further cleanup of revisions code. Probably not the last.

see #23901. props adamsilverstein, azaozz, ocean90.

git-svn-id: http://core.svn.wordpress.org/trunk@23898 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Mark Jaquith 2013-04-04 07:53:49 +00:00
parent 10d898ecbb
commit 2a38966a97
4 changed files with 819 additions and 826 deletions

View File

@ -2092,22 +2092,22 @@ function wp_ajax_heartbeat() {
function wp_ajax_revisions_data() {
check_ajax_referer( 'revisions-ajax-nonce', 'nonce' );
$compare_to = isset( $_GET['compare_to'] ) ? absint( $_GET['compare_to'] ) : 0;
$show_autosaves = isset( $_GET['show_autosaves'] ) ? $_GET['show_autosaves'] : '';
$show_split_view = isset( $_GET['show_split_view'] ) ? $_GET['show_split_view'] : '';
$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;
$compare_to = ! empty( $_GET['compare_to'] ) ? absint( $_GET['compare_to'] ) : 0;
$show_autosaves = ! empty( $_GET['show_autosaves'] );
$show_split_view = ! empty( $_GET['show_split_view'] );
$post_id = ! empty( $_GET['post_id'] ) ? absint( $_GET['post_id'] ) : 0;
$right_handle_at = ! empty( $_GET['right_handle_at'] ) ? (int) $_GET['right_handle_at'] : 0;
$left_handle_at = ! empty( $_GET['left_handle_at'] ) ? (int) $_GET['left_handle_at'] : 0;
$single_revision_id = ! empty( $_GET['single_revision_id'] ) ? absint( $_GET['single_revision_id'] ) : 0;
$compare_two_mode = (bool) $post_id;
$compare_two_mode = ( '' == $post_id ) ? false : true;
//
//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,
//so only the possible diffs need be generated
//
$alltherevisions = array();
if ( '' == $post_id )
$all_the_revisions = array();
if ( ! $post_id )
$post_id = $compare_to;
if ( ! current_user_can( 'read_post', $post_id ) )
@ -2116,33 +2116,26 @@ function wp_ajax_revisions_data() {
if ( ! $revisions = wp_get_post_revisions( $post_id ) )
return;
/* translators: revision date format, see http://php.net/date */
$datef = _x( 'j F, Y @ G:i:s', 'revision date format');
$left_revision = get_post( $compare_to );
//single model fetch mode
//return the diff of a single revision comparison
if ( 0 != $single_revision_id ) {
if ( $single_revision_id ) {
$right_revision = get_post( $single_revision_id );
if ( 0 == $compare_to )
if ( ! $compare_to )
$left_revision = get_post( $post_id );
// make sure the right revision is the most recent
if ( $compare_two_mode && $right_revision->ID < $left_revision->ID ) {
$temp = $left_revision;
$left_revision = $right_revision;
$right_revision = $temp;
}
// make sure the right revision is the most recent
if ( $compare_two_mode && $right_revision->ID < $left_revision->ID ) {
$temp = $left_revision;
$left_revision = $right_revision;
$right_revision = $temp;
}
$linesadded=0;
$linesdeleted=0;
//
$lines_added = $lines_deleted = 0;
$content = '';
//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' );
@ -2151,31 +2144,30 @@ function wp_ajax_revisions_data() {
$args = array();
if ( ! empty( $show_split_view ) )
if ( $show_split_view )
$args = array( 'show_split_view' => true );
// compare_to == 0 means first revision, so compare to a blank field to show whats changed
$diff = wp_text_diff_with_count( ( 0 == $compare_to) ? '' : $left_content, $right_content, $args );
$diff = wp_text_diff_with_count( ( 0 == $compare_to ) ? '' : $left_content, $right_content, $args );
if ( isset( $diff[ 'html' ] ) )
$content .= $diff[ 'html' ];
if ( isset( $diff[ 'linesadded' ] ) )
$linesadded = $linesadded + $diff[ 'linesadded' ];
if ( isset( $diff[ 'linesdeleted' ] ) )
$linesdeleted = $linesdeleted + $diff[ 'linesdeleted' ];
if ( isset( $diff[ 'lines_added' ] ) )
$lines_added = $lines_added + $diff[ 'lines_added' ];
if ( isset( $diff[ 'lines_deleted' ] ) )
$lines_deleted = $lines_deleted + $diff[ 'lines_deleted' ];
}
$content = '' == $content ? __( 'No difference' ) : $content;
$alltherevisions = array (
'revisiondiff' => $content,
'lines_deleted' => $linesdeleted,
'lines_added' => $linesadded
$all_the_revisions = array (
'diff' => $content,
'lines_deleted' => $lines_deleted,
'lines_added' => $lines_added
);
echo json_encode( $alltherevisions );
echo json_encode( $all_the_revisions );
exit();
} //end single model fetch
@ -2186,18 +2178,20 @@ function wp_ajax_revisions_data() {
$previous_revision_id = 0;
/* translators: revision date format, see http://php.net/date */
$datef = _x( 'j F, Y @ G:i:s', 'revision date format');
foreach ( $revisions as $revision ) :
//error_log( ( $show_autosaves ));
if ( empty( $show_autosaves ) && wp_is_post_autosave( $revision ) )
continue;
if ( ! $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 (
if ( ( 0 != $left_handle_at && $count < $left_handle_at ) ||
( 0 != $right_handle_at && $count > ( $right_handle_at - 2 ) ) ) {
$all_the_revisions[] = array (
'ID' => $revision->ID,
);
continue;
@ -2230,11 +2224,11 @@ function wp_ajax_revisions_data() {
$date
);
$autosavef = __( '%1$s [Autosave]' );
$currentf = __( '%1$s [Current Revision]' );
$autosavef = _x( '%1$s [Autosave]', 'post revision title extra' );
$currentf = _x( '%1$s [Current Revision]', 'post revision title extra' );
if ( ! $post = get_post( $post_id))
exit();
if ( ! $post = get_post( $post_id ) )
continue;
if ( $left_revision->post_modified === $post->post_modified )
$revision_from_date_author = sprintf( $currentf, $revision_from_date_author );
@ -2246,7 +2240,8 @@ function wp_ajax_revisions_data() {
elseif ( wp_is_post_autosave( $revision ) )
$revision_date_author = sprintf( $autosavef, $revision_date_author );
$date_short_format = __( 'j M @ G:i' );
/* translators: revision date short format, see http://php.net/date */
$date_short_format = _x( 'j M @ G:i', 'revision date short format');
$date_short = date_i18n( $date_short_format, strtotime( $revision->post_modified ) );
$revision_date_author_short = sprintf(
@ -2256,7 +2251,7 @@ function wp_ajax_revisions_data() {
$date_short
);
$restoreaction = wp_nonce_url(
$restore_link = wp_nonce_url(
add_query_arg(
array( 'revision' => $revision->ID,
'action' => 'restore' ),
@ -2264,20 +2259,22 @@ function wp_ajax_revisions_data() {
),
"restore-post_{$revision->ID}"
);
// if this is a left handled calculation swap data
if ( 0 != $right_handle_at ) {
$tmp = $revision_from_date_author;
$revision_from_date_author = $revision_date_author;
$revision_date_author = $tmp;
}
if ( ( $compare_two_mode || -1 !== $previous_revision_id ) ) {
$alltherevisions[] = array (
'ID' => $revision->ID,
'revision_date_author' => $revision_date_author,
'revision_from_date_author' => $revision_from_date_author,
'revision_date_author_short' => $revision_date_author_short,
'restoreaction' => urldecode( $restoreaction ),
'revision_toload' => true,
$all_the_revisions[] = array (
'ID' => $revision->ID,
'titleTo' => $revision_date_author,
'titleFrom' => $revision_from_date_author,
'titleTooltip' => $revision_date_author_short,
'restoreLink' => urldecode( $restore_link ),
'revision_toload' => true,
'previous_revision_id' => $previous_revision_id
);
}
@ -2285,6 +2282,6 @@ function wp_ajax_revisions_data() {
endforeach;
echo json_encode( $alltherevisions );
echo json_encode( $all_the_revisions );
exit();
}

File diff suppressed because it is too large Load Diff

View File

@ -79,12 +79,20 @@ wp_enqueue_script( 'revisions' );
require_once( './admin-header.php' );
//TODO - Some of the translations below split things into multiple strings that are contextually related and this makes it pretty impossible for RTL translation.
//TODO can we pass the context in a better way
$wpRevisionsSettings = array( 'post_id' => $post->ID,
'nonce' => wp_create_nonce( 'revisions-ajax-nonce' ),
'revision_id' => $revision_id );
wp_localize_script( 'revisions', 'wpRevisionsSettings', $wpRevisionsSettings );
$strings = array(
'diffFromTitle' => _x( 'From: %s', 'revision from title' ),
'diffToTitle' => _x( 'To: %s', 'revision to title' )
);
$settings = array(
'post_id' => $post->ID,
'nonce' => wp_create_nonce( 'revisions-ajax-nonce' ),
'revision_id' => $revision_id
);
$strings['settings'] = $settings;
wp_localize_script( 'revisions', 'wpRevisionsL10n', $strings );
$comparetworevisionslink = get_edit_post_link( $revision->ID );
?>
@ -112,25 +120,28 @@ $comparetworevisionslink = get_edit_post_link( $revision->ID );
</div>
<script id="tmpl-revision" type="text/html">
<div id="comparetworevisions">
<label>
<input type="checkbox" id="comparetwo" />
<?php esc_attr_e( 'Compare two revisions' ); ?>
</label>
</div>
<div id="diffsubheader" class="diff-left-hand-meta-row">
<div id="diff_from_current_revision">
<?php printf( '<b>%1$s</b> %2$s.' , __( 'From:' ), __( 'the current version' ) ); ?>
</div>
<div id="difftitlefrom">
<div class="diff-from-title"><?php _e( 'From:' ); ?></div>{{{ data.revision_from_date_author }}}
<div class="diff-from-title"><?php _e( 'From:' ); ?></div>{{{ data.titleFrom }}}
</div>
</div>
<div id="diffsubheader">
<div id="difftitle">
<div class="diff-to-title"><?php _e( 'To:' ); ?></div>{{{ data.revision_date_author }}}
<div class="diff-to-title"><?php _e( 'To:' ); ?></div>{{{ data.titleTo }}}
</div>
<div id="diffrestore">
<input class="button button-primary restore-button" onClick="document.location='{{{ data.restoreaction }}}'" type="submit" id="restore" value="<?php esc_attr_e( 'Restore This Revision' )?>" />
</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></label>
<input class="button button-primary" data-restore-link="{{{ data.restoreLink }}}" type="button" id="restore" value="<?php esc_attr_e( 'Restore This Revision' )?>" />
</div>
</div>
@ -138,10 +149,10 @@ $comparetworevisionslink = get_edit_post_link( $revision->ID );
<div id="removed"><?php _e( 'Removed -' ); ?></div>
<div id="added"><?php _e( 'Added +' ); ?></div>
</div
<div>{{{ data.revisiondiff }}}</div>
<div>{{{ data.diff }}}</div>
</script>
<script id="tmpl-revisionvinteract" type="text/html">
<script id="tmpl-revision-interact" type="text/html">
<div id="diffheader">
<div id="diffprevious"><input class="button" type="submit" id="previous" value="<?php esc_attr_e( 'Previous' ); ?>" />
</div>
@ -153,22 +164,10 @@ $comparetworevisionslink = get_edit_post_link( $revision->ID );
</div>
</div>
</script>
<script id="tmpl-revision-ticks" type="text/html">
<div class="revision-tick revision-toload{{{ data.revision_toload }}} revision-scopeofchanges-{{{ data.scope_of_changes }}}">
</div>
</script>
<?php
/*
TODO Convert these into screen options
<script id="tmpl-revisionoptions" type="text/html">
<div id="revisionoptions">
<div id="showsplitviewoption">
<input type='checkbox' id="show_split_view" checked="checked" value="1" /> <?php _e( 'Show split diff view' ); ?>
</div>
<div id="toggleshowautosavesoption">
<input type='checkbox' id="toggleshowautosaves" value="1" /> <?php _e( 'Show autosaves' ); ?>
</div>
</div>
</script>
*/
require_once( './admin-footer.php' );
require_once( './admin-footer.php' );

View File

@ -675,7 +675,7 @@ function wp_text_diff_with_count( $left_string, $right_string, $args = null ) {
$defaults = array( 'title' => '', 'title_left' => '', 'title_right' => '' );
$args = wp_parse_args( $args, $defaults );
if ( !class_exists( 'WP_Text_Diff_Renderer_Table' ) )
if ( ! class_exists( 'WP_Text_Diff_Renderer_Table' ) )
require( ABSPATH . WPINC . '/wp-diff.php' );
$left_string = normalize_whitespace( $left_string );
@ -685,8 +685,8 @@ function wp_text_diff_with_count( $left_string, $right_string, $args = null ) {
$right_lines = explode( "\n", $right_string) ;
$text_diff = new Text_Diff($left_lines, $right_lines );
$linesadded = $text_diff->countAddedLines();
$linesdeleted = $text_diff->countDeletedLines();
$lines_added = $text_diff->countAddedLines();
$lines_deleted = $text_diff->countDeletedLines();
$renderer = new WP_Text_Diff_Renderer_Table();
$diff = $renderer->render( $text_diff );
@ -718,5 +718,5 @@ function wp_text_diff_with_count( $left_string, $right_string, $args = null ) {
$r .= "<tbody>\n$diff\n</tbody>\n";
$r .= "</table>";
return array( 'html' => $r, 'linesadded' => $linesadded, 'linesdeleted' => $linesdeleted );
return array( 'html' => $r, 'lines_added' => $lines_added, 'lines_deleted' => $lines_deleted );
}