Make WordPress Core


Ignore:
Timestamp:
06/26/2013 09:06:50 PM (13 years ago)
Author:
markjaquith
Message:

Cleanup of the revisions screen, both on the PHP API side, and the JS.

  • Much simpler PHP API
  • Cleaner and more Backbone-y JS API
  • Consequently, does batch queries; this now scales up to hundreds of revisions

Currently missing, but much easier considering the cleaned up base:

  • Compare two mode
  • RTL

props koopersmith, nacin, adamsilverstein, ocean90. see #24425

File:
1 edited

Legend:

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

    r24406 r24520  
    20832083}
    20842084
    2085 function wp_ajax_revisions_data() {
    2086     check_ajax_referer( 'revisions-ajax-nonce', 'nonce' );
    2087 
    2088     $compare_to = ! empty( $_GET['compare_to'] ) ? absint( $_GET['compare_to'] ) : 0;
    2089     $show_autosaves = ! empty( $_GET['show_autosaves'] );
    2090     $show_split_view = ! empty( $_GET['show_split_view'] );
    2091     $post_id = ! empty( $_GET['post_id'] ) ? absint( $_GET['post_id'] ) : 0;
    2092     $right_handle_at = ! empty( $_GET['right_handle_at'] ) ? (int) $_GET['right_handle_at'] : 0;
    2093     $left_handle_at = ! empty( $_GET['left_handle_at'] ) ? (int) $_GET['left_handle_at'] : 0;
    2094     $single_revision_id = ! empty( $_GET['single_revision_id'] ) ? absint( $_GET['single_revision_id'] ) : 0;
    2095     $compare_two_mode = (bool) $post_id;
    2096 
    2097     $all_the_revisions = array();
    2098     if ( ! $post_id )
    2099         $post_id = $compare_to;
    2100 
    2101     if ( ! current_user_can( 'read_post', $post_id ) )
    2102         continue;
    2103 
    2104     if ( ! $revisions = wp_get_post_revisions( $post_id ) )
    2105         return;
    2106 
    2107     $left_revision = get_post( $compare_to );
    2108 
    2109     // single model fetch mode
    2110     // return the diff of a single revision comparison
    2111     if ( $single_revision_id ) {
    2112         $right_revision = get_post( $single_revision_id );
    2113 
    2114         if ( ! $compare_to )
    2115             $left_revision = get_post( $post_id );
    2116 
    2117         // make sure the right revision is the most recent, except on oldest revision
    2118         if ( $compare_to && $right_revision->post_date < $left_revision->post_date ) {
    2119             $temp = $left_revision;
    2120             $left_revision = $right_revision;
    2121             $right_revision = $temp;
    2122         }
    2123 
    2124         $lines_added = $lines_deleted = 0;
    2125         $content = '';
    2126         // compare from left to right, passed from application
    2127         foreach ( _wp_post_revision_fields() as $field => $field_value ) {
    2128             $left_content = apply_filters( "_wp_post_revision_field_$field", $left_revision->$field, $field, $left_revision, 'left' );
    2129             $right_content = apply_filters( "_wp_post_revision_field_$field", $right_revision->$field, $field, $right_revision, 'right' );
    2130 
    2131             add_filter( "_wp_post_revision_field_$field", 'htmlspecialchars' );
    2132 
    2133             $args = array();
    2134 
    2135             if ( $show_split_view )
    2136                  $args = array( 'show_split_view' => true );
    2137 
    2138             // compare_to == 0 means first revision, so compare to a blank field to show whats changed
    2139             $diff = wp_text_diff_with_count( ( 0 == $compare_to ) ? '' : $left_content, $right_content, $args );
    2140 
    2141             if ( isset( $diff[ 'html' ] ) ) {
    2142                 $content .= sprintf( '<div class="diff-label">%s</div>', $field_value );
    2143                 $content .= $diff[ 'html' ];
    2144             }
    2145 
    2146             if ( isset( $diff[ 'lines_added' ] ) )
    2147                 $lines_added = $lines_added + $diff[ 'lines_added' ];
    2148 
    2149             if ( isset( $diff[ 'lines_deleted' ] ) )
    2150                 $lines_deleted = $lines_deleted + $diff[ 'lines_deleted' ];
    2151         }
    2152         $content = '' == $content ? __( 'No difference' ) : $content;
    2153 
    2154         $all_the_revisions = array (
    2155             'diff'         => $content,
    2156             'linesDeleted' => $lines_deleted,
    2157             'linesAdded'   => $lines_added
     2085function wp_ajax_get_revision_diffs() {
     2086    require ABSPATH . 'wp-admin/includes/revision.php';
     2087
     2088    // check_ajax_referer( 'revisions-ajax-nonce', 'nonce' );
     2089
     2090    if ( ! $post = get_post( (int) $_REQUEST['post_id'] ) )
     2091        wp_send_json_error();
     2092
     2093    if ( ! current_user_can( 'read_post', $post->ID ) )
     2094        wp_send_json_error();
     2095
     2096    // Really just pre-loading the cache here.
     2097    if ( ! $revisions = wp_get_post_revisions( $post->ID ) )
     2098        wp_send_json_error();
     2099
     2100    $return = array();
     2101    @set_time_limit( count( $_REQUEST['compare'] ) );
     2102
     2103    foreach ( $_REQUEST['compare'] as $compare_key ) {
     2104        list( $compare_from, $compare_to ) = explode( ':', $compare_key ); // from:to
     2105
     2106        $return[] = array(
     2107            'id' => $compare_key,
     2108            'fields' => wp_get_revision_ui_diff( $post, $compare_from, $compare_to ),
    21582109        );
    2159 
    2160         echo json_encode( $all_the_revisions );
    2161         exit();
    2162     } // end single model fetch
    2163 
    2164     $count = -1;
    2165 
    2166     // reverse the list to start with oldest revision
    2167     $revisions = array_reverse( $revisions );
    2168 
    2169     $previous_revision_id = 0;
    2170 
    2171     /* translators: revision date format, see http://php.net/date */
    2172     $datef = _x( 'j F, Y @ G:i:s', 'revision date format');
    2173 
    2174     foreach ( $revisions as $revision ) :
    2175         if ( ! $show_autosaves && wp_is_post_autosave( $revision ) )
    2176             continue;
    2177 
    2178         $revision_from_date_author = '';
    2179         $is_current_revision = false;
    2180         $count++;
    2181 
    2182         /**
    2183         * return blank data for diffs to the left of the left handle (for right handel model)
    2184         * or to the right of the right handle (for left handel model)
    2185         * and visa versa in RTL mode
    2186         */
    2187         if( ! is_rtl() ) {
    2188             if ( ( ( 0 != $left_handle_at && $count < $left_handle_at ) ||
    2189                  ( 0 != $right_handle_at && $count > ( $right_handle_at - 2 ) ) ) ) {
    2190                 $all_the_revisions[] = array (
    2191                     'ID' => $revision->ID,
    2192                 );
    2193                 continue;
    2194             }
    2195         } else { // is_rtl
    2196             if ( ( 0 != $left_handle_at && $count > ( $left_handle_at - 1 ) ||
    2197                  ( 0 != $left_handle_at && $count < $right_handle_at ) ) ) {
    2198                 $all_the_revisions[] = array (
    2199                     'ID' => $revision->ID,
    2200                 );
    2201                 continue;
    2202             }
    2203         }
    2204 
    2205         if ( $compare_two_mode ) {
    2206             $compare_to_gravatar = get_avatar( $left_revision->post_author, 24 );
    2207             $compare_to_author = get_the_author_meta( 'display_name', $left_revision->post_author );
    2208             $compare_to_date = date_i18n( $datef, strtotime( $left_revision->post_modified ) );
    2209 
    2210             $revision_from_date_author = sprintf(
    2211                 /* translators: post revision title: 1: author avatar, 2: author name, 3: time ago, 4: date */
    2212                 _x( '%1$s %2$s, %3$s ago (%4$s)', 'post revision title' ),
    2213                 $compare_to_gravatar,
    2214                 $compare_to_author,
    2215                 human_time_diff( strtotime( $left_revision->post_modified ), current_time( 'timestamp' ) ),
    2216                 $compare_to_date
    2217             );
    2218         }
    2219 
    2220         $gravatar = get_avatar( $revision->post_author, 24 );
    2221         $author = get_the_author_meta( 'display_name', $revision->post_author );
    2222         $date = date_i18n( $datef, strtotime( $revision->post_modified ) );
    2223         $revision_date_author = sprintf(
    2224             /* translators: post revision title: 1: author avatar, 2: author name, 3: time ago, 4: date */
    2225             _x( '%1$s %2$s, %3$s ago (%4$s)', 'post revision title' ),
    2226             $gravatar,
    2227             $author,
    2228             human_time_diff( strtotime( $revision->post_modified ), current_time( 'timestamp' ) ),
    2229             $date
    2230         );
    2231 
    2232         /* translators: 1: date */
    2233         $autosavef = _x( '%1$s [Autosave]', 'post revision title extra' );
    2234         /* translators: 1: date */
    2235         $currentf  = _x( '%1$s [Current Revision]', 'post revision title extra' );
    2236 
    2237         if ( ! $post = get_post( $post_id ) )
    2238             continue;
    2239 
    2240         if ( $left_revision->post_modified === $post->post_modified )
    2241             $revision_from_date_author = sprintf( $currentf, $revision_from_date_author );
    2242         elseif ( wp_is_post_autosave( $left_revision ) )
    2243             $revision_from_date_author = sprintf( $autosavef, $revision_from_date_author );
    2244 
    2245         if ( $revision->post_modified === $post->post_modified ) {
    2246             $revision_date_author = sprintf( $currentf, $revision_date_author );
    2247             $is_current_revision = true;
    2248         } elseif ( wp_is_post_autosave( $revision ) ) {
    2249             $revision_date_author = sprintf( $autosavef, $revision_date_author );
    2250         }
    2251 
    2252         /* translators: revision date short format, see http://php.net/date */
    2253         $date_short_format = _x( 'j M @ G:i', 'revision date short format');
    2254         $date_short = date_i18n( $date_short_format, strtotime( $revision->post_modified ) );
    2255 
    2256         $revision_date_author_short = sprintf(
    2257             '%s <strong>%s</strong><br />%s',
    2258             $gravatar,
    2259             $author,
    2260             $date_short
    2261         );
    2262 
    2263         $restore_link = wp_nonce_url(
    2264             add_query_arg(
    2265                 array( 'revision' => $revision->ID,
    2266                     'action' => 'restore' ),
    2267                     admin_url( 'revision.php' )
    2268             ),
    2269             "restore-post_{$revision->ID}"
    2270         );
    2271 
    2272         // if this is a left handled calculation swap data
    2273         if ( 0 != $right_handle_at ) {
    2274             $tmp = $revision_from_date_author;
    2275             $revision_from_date_author = $revision_date_author;
    2276             $revision_date_author = $tmp;
    2277         }
    2278 
    2279         if ( ( $compare_two_mode || -1 !== $previous_revision_id ) ) {
    2280             $all_the_revisions[] = array (
    2281                 'ID'           => $revision->ID,
    2282                 'titleTo'      => $revision_date_author,
    2283                 'titleFrom'    => $revision_from_date_author,
    2284                 'titleTooltip' => $revision_date_author_short,
    2285                 'restoreLink'  => urldecode( $restore_link ),
    2286                 'previousID'   => $previous_revision_id,
    2287                 'isCurrent'    => $is_current_revision,
    2288             );
    2289         }
    2290         $previous_revision_id = $revision->ID;
    2291 
    2292     endforeach;
    2293 
    2294     // in RTL + single handle mode, reverse the revision direction
    2295     if ( is_rtl() && $compare_two_mode )
    2296         $all_the_revisions = array_reverse( $all_the_revisions );
    2297 
    2298     echo json_encode( $all_the_revisions );
    2299     exit();
    2300 }
     2110    }
     2111    wp_send_json_success( $return );
     2112}
Note: See TracChangeset for help on using the changeset viewer.