Make WordPress Core


Ignore:
Timestamp:
04/04/2013 07:53:49 AM (13 years ago)
Author:
markjaquith
Message:

Further cleanup of revisions code. Probably not the last.

see #23901. props adamsilverstein, azaozz, ocean90.

File:
1 edited

Legend:

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

    r23890 r23898  
    20932093    check_ajax_referer( 'revisions-ajax-nonce', 'nonce' );
    20942094
    2095     $compare_to = isset( $_GET['compare_to'] ) ? absint( $_GET['compare_to'] ) : 0;
    2096     $show_autosaves = isset( $_GET['show_autosaves'] ) ? $_GET['show_autosaves'] : '';
    2097     $show_split_view = isset( $_GET['show_split_view'] ) ? $_GET['show_split_view'] : '';
    2098     $post_id = isset( $_GET['post_id'] ) ? absint( $_GET['post_id'] ) : '';
    2099     $right_handle_at = isset( $_GET['right_handle_at'] ) ? $_GET['right_handle_at'] : 0;
    2100     $left_handle_at = isset( $_GET['left_handle_at'] ) ? $_GET['left_handle_at'] : 0;
    2101     $single_revision_id = isset( $_GET['single_revision_id'] ) ? $_GET['single_revision_id'] : 0;
    2102 
    2103     $compare_two_mode = ( '' == $post_id ) ? false : true;
     2095    $compare_to = ! empty( $_GET['compare_to'] ) ? absint( $_GET['compare_to'] ) : 0;
     2096    $show_autosaves = ! empty( $_GET['show_autosaves'] );
     2097    $show_split_view = ! empty( $_GET['show_split_view'] );
     2098    $post_id = ! empty( $_GET['post_id'] ) ? absint( $_GET['post_id'] ) : 0;
     2099    $right_handle_at = ! empty( $_GET['right_handle_at'] ) ? (int) $_GET['right_handle_at'] : 0;
     2100    $left_handle_at = ! empty( $_GET['left_handle_at'] ) ? (int) $_GET['left_handle_at'] : 0;
     2101    $single_revision_id = ! empty( $_GET['single_revision_id'] ) ? absint( $_GET['single_revision_id'] ) : 0;
     2102    $compare_two_mode = (bool) $post_id;
     2103
    21042104    //
    21052105    //TODO: currently code returns all possible comparisons for the indicated 'compare_to' revision
     
    21072107    //so only the possible diffs need be generated
    21082108    //
    2109     $alltherevisions = array();
    2110     if ( '' == $post_id )
     2109    $all_the_revisions = array();
     2110    if ( ! $post_id )
    21112111        $post_id = $compare_to;
    21122112
     
    21172117        return;
    21182118
    2119     /* translators: revision date format, see http://php.net/date */
    2120     $datef = _x( 'j F, Y @ G:i:s', 'revision date format');
    2121 
    21222119    $left_revision = get_post( $compare_to );
    21232120
    21242121    //single model fetch mode
    21252122    //return the diff of a single revision comparison
    2126     if ( 0 != $single_revision_id ) {
     2123    if ( $single_revision_id ) {
    21272124        $right_revision = get_post( $single_revision_id );
    21282125
    2129     if ( 0 == $compare_to )
     2126        if ( ! $compare_to )
    21302127            $left_revision = get_post( $post_id );
    21312128
    2132     // make sure the right revision is the most recent
    2133     if ( $compare_two_mode && $right_revision->ID < $left_revision->ID ) {
    2134         $temp = $left_revision;
    2135         $left_revision = $right_revision;
    2136         $right_revision = $temp;
    2137     }
    2138 
    2139         $linesadded=0;
    2140         $linesdeleted=0;
    2141 
    2142         //
     2129        // make sure the right revision is the most recent
     2130        if ( $compare_two_mode && $right_revision->ID < $left_revision->ID ) {
     2131            $temp = $left_revision;
     2132            $left_revision = $right_revision;
     2133            $right_revision = $temp;
     2134        }
     2135
     2136        $lines_added = $lines_deleted = 0;
     2137        $content = '';
    21432138        //compare from left to right, passed from application
    2144         //
    2145         $content='';
    21462139        foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
    21472140            $left_content = apply_filters( "_wp_post_revision_field_$field", $left_revision->$field, $field, $left_revision, 'left' );
     
    21522145            $args = array();
    21532146
    2154             if ( ! empty( $show_split_view ) )
     2147            if ( $show_split_view )
    21552148                 $args = array( 'show_split_view' => true );
    21562149
    21572150            // compare_to == 0 means first revision, so compare to a blank field to show whats changed
    2158             $diff = wp_text_diff_with_count( ( 0 == $compare_to) ? '' : $left_content, $right_content, $args );
     2151            $diff = wp_text_diff_with_count( ( 0 == $compare_to ) ? '' : $left_content, $right_content, $args );
    21592152
    21602153            if ( isset( $diff[ 'html' ] ) )
    21612154                $content .= $diff[ 'html' ];
    21622155
    2163             if ( isset( $diff[ 'linesadded' ] ) )
    2164                 $linesadded = $linesadded + $diff[ 'linesadded' ];
    2165 
    2166             if ( isset( $diff[ 'linesdeleted' ] ) )
    2167                 $linesdeleted = $linesdeleted + $diff[ 'linesdeleted' ];
    2168 
    2169 
     2156            if ( isset( $diff[ 'lines_added' ] ) )
     2157                $lines_added = $lines_added + $diff[ 'lines_added' ];
     2158
     2159            if ( isset( $diff[ 'lines_deleted' ] ) )
     2160                $lines_deleted = $lines_deleted + $diff[ 'lines_deleted' ];
    21702161        }
    21712162        $content = '' == $content ? __( 'No difference' ) : $content;
    21722163
    2173         $alltherevisions = array (
    2174             'revisiondiff' => $content,
    2175             'lines_deleted' => $linesdeleted,
    2176             'lines_added' => $linesadded
     2164        $all_the_revisions = array (
     2165            'diff'          => $content,
     2166            'lines_deleted' => $lines_deleted,
     2167            'lines_added'   => $lines_added
    21772168        );
    2178         echo json_encode( $alltherevisions );
     2169
     2170        echo json_encode( $all_the_revisions );
    21792171        exit();
    21802172    } //end single model fetch
     
    21872179    $previous_revision_id = 0;
    21882180
     2181    /* translators: revision date format, see http://php.net/date */
     2182    $datef = _x( 'j F, Y @ G:i:s', 'revision date format');
     2183
    21892184    foreach ( $revisions as $revision ) :
    2190         //error_log( ( $show_autosaves  ));
    2191         if ( empty( $show_autosaves ) && wp_is_post_autosave( $revision ) )
    2192                 continue;
     2185        if ( ! $show_autosaves && wp_is_post_autosave( $revision ) )
     2186            continue;
    21932187
    21942188        $revision_from_date_author = '';
     
    21962190        // return blank data for diffs to the left of the left handle (for right handel model)
    21972191        // or to the right of the right handle (for left handel model)
    2198         if ( ( 0 != $left_handle_at && $count <= $left_handle_at ) ||
    2199              ( 0 != $right_handle_at && $count > $right_handle_at )) {
    2200             $alltherevisions[] = array (
     2192        if ( ( 0 != $left_handle_at && $count < $left_handle_at ) ||
     2193             ( 0 != $right_handle_at && $count > ( $right_handle_at - 2 ) ) ) {
     2194            $all_the_revisions[] = array (
    22012195                'ID' => $revision->ID,
    22022196            );
     
    22312225        );
    22322226
    2233         $autosavef = __( '%1$s [Autosave]' );
    2234         $currentf  = __( '%1$s [Current Revision]' );
    2235 
    2236         if ( ! $post = get_post( $post_id))
    2237             exit();
     2227        $autosavef = _x( '%1$s [Autosave]', 'post revision title extra' );
     2228        $currentf  = _x( '%1$s [Current Revision]', 'post revision title extra' );
     2229
     2230        if ( ! $post = get_post( $post_id ) )
     2231            continue;
    22382232
    22392233        if ( $left_revision->post_modified === $post->post_modified )
     
    22472241            $revision_date_author = sprintf( $autosavef, $revision_date_author );
    22482242
    2249         $date_short_format = __( 'j M @ G:i' );
     2243        /* translators: revision date short format, see http://php.net/date */
     2244        $date_short_format = _x( 'j M @ G:i', 'revision date short format');
    22502245        $date_short = date_i18n( $date_short_format, strtotime( $revision->post_modified ) );
    22512246
     
    22572252        );
    22582253
    2259         $restoreaction = wp_nonce_url(
     2254        $restore_link = wp_nonce_url(
    22602255            add_query_arg(
    22612256                array( 'revision' => $revision->ID,
     
    22652260            "restore-post_{$revision->ID}"
    22662261        );
     2262
    22672263        // if this is a left handled calculation swap data
    22682264        if ( 0 != $right_handle_at ) {
     
    22712267            $revision_date_author = $tmp;
    22722268        }
     2269
    22732270        if ( ( $compare_two_mode || -1 !== $previous_revision_id ) ) {
    2274             $alltherevisions[] = array (
    2275                 'ID' => $revision->ID,
    2276                 'revision_date_author' => $revision_date_author,
    2277                 'revision_from_date_author' => $revision_from_date_author,
    2278                 'revision_date_author_short' => $revision_date_author_short,
    2279                 'restoreaction' => urldecode( $restoreaction ),
    2280                 'revision_toload' => true,
     2271            $all_the_revisions[] = array (
     2272                'ID'                   => $revision->ID,
     2273                'titleTo'              => $revision_date_author,
     2274                'titleFrom'            => $revision_from_date_author,
     2275                'titleTooltip'        => $revision_date_author_short,
     2276                'restoreLink'          => urldecode( $restore_link ),
     2277                'revision_toload'      => true,
    22812278                'previous_revision_id' => $previous_revision_id
    22822279            );
     
    22862283    endforeach;
    22872284
    2288     echo json_encode( $alltherevisions );
     2285    echo json_encode( $all_the_revisions );
    22892286    exit();
    22902287}
Note: See TracChangeset for help on using the changeset viewer.