Make WordPress Core


Ignore:
Timestamp:
03/07/2013 03:32:26 PM (12 years ago)
Author:
westi
Message:

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

File:
1 edited

Legend:

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

    r23576 r23639  
    21322132    check_ajax_referer( 'revisions-ajax-nonce', 'nonce' );
    21332133
    2134     $compareto = isset( $_GET['compareto'] ) ? absint( $_GET['compareto'] ) : 0;
    2135     $showautosaves = isset( $_GET['showautosaves'] ) ? $_GET['showautosaves'] : '';
    2136     $showsplitview = isset( $_GET['showsplitview'] ) ? $_GET['showsplitview'] : '';
    2137     $postid = isset( $_GET['post_id'] ) ? absint( $_GET['post_id'] ) : '';
    2138 
    2139     $comparetwomode = ( '' == $postid ) ? false : true;
     2134    $compare_to = isset( $_GET['compare_to'] ) ? absint( $_GET['compare_to'] ) : 0;
     2135    $show_autosaves = isset( $_GET['show_autosaves'] ) ? $_GET['show_autosaves'] : '';
     2136    $show_split_view = isset( $_GET['show_split_view'] ) ? $_GET['show_split_view'] : '';
     2137    $post_id = isset( $_GET['post_id'] ) ? absint( $_GET['post_id'] ) : '';
     2138    $right_handle_at = isset( $_GET['right_handle_at'] ) ? $_GET['right_handle_at'] : 0;
     2139    $left_handle_at = isset( $_GET['left_handle_at'] ) ? $_GET['left_handle_at'] : 0;
     2140    $single_revision_id = isset( $_GET['single_revision_id'] ) ? $_GET['single_revision_id'] : 0;
     2141
     2142    $compare_two_mode = ( '' == $post_id ) ? false : true;
    21402143    //
    2141     //TODO: currently code returns all possible comparisons for the indicated 'compareto' revision
     2144    //TODO: currently code returns all possible comparisons for the indicated 'compare_to' revision
    21422145    //however, the front end prevents users from pulling the right handle past the left or the left pass the right,
    21432146    //so only the possible diffs need be generated
    21442147    //
    21452148    $alltherevisions = array();
    2146     if ( '' == $postid )
    2147         $postid = $compareto;
    2148 
    2149     if ( ! current_user_can( 'read_post', $postid ) )
     2149    if ( '' == $post_id )
     2150        $post_id = $compare_to;
     2151
     2152    if ( ! current_user_can( 'read_post', $post_id ) )
    21502153        continue;
    21512154
    2152     if ( ! $revisions = wp_get_post_revisions( $postid ) )
     2155    if ( ! $revisions = wp_get_post_revisions( $post_id ) )
    21532156        return;
    21542157
     2158    /* translators: revision date format, see http://php.net/date */
     2159    $datef = _x( 'j F, Y @ G:i:s', 'revision date format');
     2160
     2161    //single model fetch mode
     2162    if ( 0 != $single_revision_id ) {
     2163        $left_revision = get_post( $compare_to );
     2164        $right_revision = get_post( $single_revision_id );
     2165
     2166        if ( $compare_two_mode ) {
     2167            $compare_to_gravatar = get_avatar( $left_revision->post_author, 18 );
     2168            $compare_to_author = get_the_author_meta( 'display_name', $left_revision->post_author );
     2169            $compare_to_date = date_i18n( $datef, strtotime( $left_revision->post_modified ) );
     2170
     2171            $revision_from_date_author = sprintf(
     2172                '%s %s, %s %s (%s)',
     2173                $compare_to_gravatar,
     2174                $compare_to_author,
     2175                human_time_diff( strtotime( $left_revision->post_modified ), current_time( 'timestamp' ) ),
     2176                __( ' ago ' ),
     2177                $compare_to_date
     2178            );
     2179        }
     2180
     2181        //
     2182        //make sure the left revision is the most recent
     2183        //
     2184        if ( strtotime( $right_revision->post_modified_gmt ) < strtotime( $left_revision->post_modified_gmt ) ) {
     2185            $temp = $left_revision;
     2186            $left_revision = $right_revision;
     2187            $right_revision = $temp;
     2188        }
     2189
     2190        //
     2191        //compare from left to right, passed from application
     2192        //
     2193        $content='';
     2194        foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
     2195            $left_content = apply_filters( "_wp_post_revision_field_$field", $left_revision->$field, $field, $left_revision, 'left' );
     2196            $right_content = apply_filters( "_wp_post_revision_field_$field", $right_revision->$field, $field, $right_revision, 'right' );
     2197
     2198            add_filter( "_wp_post_revision_field_$field", 'wp_kses_post' );
     2199
     2200            $args = array();
     2201
     2202            if ( ! empty( $show_split_view ) )
     2203                 $args = array( 'show_split_view' => true );
     2204
     2205            $content .= wp_text_diff( $left_content, $right_content, $args );
     2206        }
     2207            $content = '' == $content ? __( 'No difference' ) : $content;
     2208            $alltherevisions = array (
     2209                'revisiondiff' => $content
     2210            );
     2211        echo json_encode( $alltherevisions );
     2212        exit();
     2213    }
    21552214
    21562215    //if we are comparing two revisions, the first 'revision' represented by the leftmost
    21572216    //slider position is the current revision, prepend a comparison to this revision
    2158     if ( $comparetwomode )
    2159         array_unshift( $revisions, get_post( $postid ) );
    2160 
    2161     $count = 1;
     2217    if ( $compare_two_mode )
     2218        array_unshift( $revisions, get_post( $post_id ) );
     2219       
     2220    $count = -1;
     2221
    21622222    foreach ( $revisions as $revision ) :
    2163     if ( 'true' != $showautosaves && wp_is_post_autosave( $revision ) )
     2223        if ( ! empty( $show_autosaves ) && wp_is_post_autosave( $revision ) )
     2224                continue;
     2225
     2226        $revision_from_date_author = '';
     2227        $count++;
     2228        // return blank data for diffs to the left of the left handle (for right handel model)
     2229        // or to the right of the right handle (for left handel model)
     2230        if ( ( 0 != $left_handle_at && $count <= $left_handle_at ) ||
     2231             ( 0 != $right_handle_at && $count > $right_handle_at )) {
     2232            $alltherevisions[] = array (
     2233                'ID' => $revision->ID,
     2234            );
     2235           
    21642236            continue;
    2165 
    2166     $revision_from_date_author = '';
    2167 
    2168 
    2169     $left_revision = get_post( $compareto );
    2170     $right_revision = get_post( $revision );
    2171 
    2172     $author = get_the_author_meta( 'display_name', $revision->post_author );
    2173     /* translators: revision date format, see http://php.net/date */
    2174     $datef = _x( 'j F, Y @ G:i:s', 'revision date format');
    2175 
    2176     $gravatar = get_avatar( $revision->post_author, 18 );
    2177 
    2178     $date = date_i18n( $datef, strtotime( $revision->post_modified ) );
    2179     $revision_date_author = sprintf(
    2180         '%s %s, %s %s (%s)',
    2181         $gravatar,
    2182         $author,
    2183         human_time_diff( strtotime( $revision->post_modified ), current_time( 'timestamp' ) ),
    2184         __( ' ago ' ),
    2185         $date
    2186     );
    2187 
    2188     if ( $comparetwomode ) {
    2189         $compareto_gravatar = get_avatar( $left_revision->post_author, 18 );
    2190         $compareto_author = get_the_author_meta( 'display_name', $left_revision->post_author );
    2191         $compareto_date = date_i18n( $datef, strtotime( $left_revision->post_modified ) );
    2192 
    2193         $revision_from_date_author = sprintf(
     2237        }
     2238
     2239        $gravatar = get_avatar( $revision->post_author, 18 );
     2240        $author = get_the_author_meta( 'display_name', $revision->post_author );
     2241        $date = date_i18n( $datef, strtotime( $revision->post_modified ) );
     2242        $revision_date_author = sprintf(
    21942243            '%s %s, %s %s (%s)',
    2195             $compareto_gravatar,
    2196             $compareto_author,
    2197             human_time_diff( strtotime( $left_revision->post_modified ), current_time( 'timestamp' ) ),
     2244            $gravatar,
     2245            $author,
     2246            human_time_diff( strtotime( $revision->post_modified ), current_time( 'timestamp' ) ),
    21982247            __( ' ago ' ),
    2199             $compareto_date
     2248            $date
    22002249        );
    2201     }
    2202 
    2203     $restoreaction = wp_nonce_url(
    2204         add_query_arg(
    2205             array( 'revision' => $revision->ID,
    2206                 'action' => 'restore' ),
    2207                 '/wp-admin/revision.php'
    2208         ),
    2209         "restore-post_{$compareto}|{$revision->ID}"
    2210     );
    2211 
    2212     //
    2213     //make sure the left revision is the most recent
    2214     //
    2215     if ( strtotime( $right_revision->post_modified_gmt ) < strtotime( $left_revision->post_modified_gmt ) ) {
    2216         $temp = $left_revision;
    2217         $left_revision = $right_revision;
    2218         $right_revision = $temp;
    2219     }
    2220 
    2221     //
    2222     //compare from left to right, passed from application
    2223     //
    2224     $content='';
    2225     foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
    2226         $left_content = apply_filters( "_wp_post_revision_field_$field", $left_revision->$field, $field, $left_revision, 'left' );
    2227         $right_content = apply_filters( "_wp_post_revision_field_$field", $right_revision->$field, $field, $right_revision, 'right' );
    2228 
    2229         add_filter( "_wp_post_revision_field_$field", 'wp_kses_post' );
    2230 
    2231         $args = array();
    2232 
    2233         if ( 'true' == $showsplitview )
    2234              $args = array( 'showsplitview' => 'true' );
    2235 
    2236         $content .= wp_text_diff( $left_content, $right_content, $args );
    2237     }
    2238 
    2239     //if we are comparing two revisions
    2240     //and we are on the matching revision
    2241     //add an error revision indicating unable to compare to self
    2242     if ( $comparetwomode && $compareto == $revision->ID )
     2250
     2251        $restoreaction = wp_nonce_url(
     2252            add_query_arg(
     2253                array( 'revision' => $revision->ID,
     2254                    'action' => 'restore' ),
     2255                    admin_url( 'revision.php' )
     2256            ),
     2257            "restore-post_{$compare_to}|{$revision->ID}"
     2258        );
     2259
    22432260        $alltherevisions[] = array (
    2244             'ID' => $revision->ID,
    2245             'revision_date_author' => $revision_date_author,
    2246             'revisiondiff' => sprintf('<div id="selfcomparisonerror">%s</div>', __( 'Cannot compare revision to itself' ) ),
    2247             'restoreaction' => urldecode( $restoreaction ),
    2248             'revision_from_date_author' => ''
    2249         );
    2250 
    2251     //add to the return data only if there is a difference
    2252     if ( '' != $content )
    2253         $alltherevisions[] = array (
    2254             'ID' => $revision->ID,
    2255             'revision_date_author' => $revision_date_author,
    2256             'revisiondiff' => $content,
    2257             'restoreaction' => urldecode( $restoreaction ),
    2258             'revision_from_date_author' => $revision_from_date_author
    2259         );
     2261                'ID' => $revision->ID,
     2262                'revision_date_author' => $revision_date_author,
     2263                'revision_from_date_author' => $revision_from_date_author,
     2264                'restoreaction' => urldecode( $restoreaction ),
     2265                'revision_toload' => true
     2266            );
    22602267
    22612268    endforeach;
Note: See TracChangeset for help on using the changeset viewer.