Make WordPress Core


Ignore:
Timestamp:
02/28/2013 03:14:34 PM (12 years ago)
Author:
westi
Message:

Revisions: First pass an implementing a new UI/UX for reviewing the revisions of posts. See #23497 props adamsilverstein for the initial patch.

This implements a new revisions ui using Backbone and preserves all the old methods of "integration" so the change should be transparent to plugins using revisi
ons with CPTs.

This is the first pass and so there are a number of things still to be resolved, more details in the ticket. Feedback welcomed.

File:
1 edited

Legend:

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

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