Make WordPress Core

Changeset 7907


Ignore:
Timestamp:
05/08/2008 05:25:07 PM (16 years ago)
Author:
ryan
Message:

Move autosave to post revisions. Props mdawaffe. see #6775

Location:
trunk
Files:
10 edited

Legend:

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

    r7777 r7907  
    462462    define( 'DOING_AUTOSAVE', true );
    463463
    464     $nonce_age = check_ajax_referer( 'autosave', 'autosavenonce');
     464    $nonce_age = check_ajax_referer( 'autosave', 'autosavenonce' );
    465465    global $current_user;
    466466
    467     $_POST['post_status'] = 'draft';
    468467    $_POST['post_category'] = explode(",", $_POST['catslist']);
    469468    $_POST['tags_input'] = explode(",", $_POST['tags_input']);
     
    479478    $supplemental = array();
    480479
    481     $id = 0;
     480    $id = $revision_id = 0;
    482481    if($_POST['post_ID'] < 0) {
     482        $_POST['post_status'] = 'draft';
    483483        $_POST['temp_ID'] = $_POST['post_ID'];
    484484        if ( $do_autosave ) {
     
    511511                die(__('You are not allowed to edit this post.'));
    512512        }
     513
    513514        if ( $do_autosave ) {
    514             $id = edit_post();
     515            // Drafts are just overwritten by autosave
     516            if ( 'draft' == $post->post_status ) {
     517                $id = edit_post();
     518            } else { // Non drafts are not overwritten.  The autosave is stored in a special post revision.
     519                $revision_id = wp_create_autosave( $post->ID );
     520                if ( is_wp_error($revision_id) )
     521                    $id = $revision_id;
     522                else
     523                    $id = $post->ID;
     524            }
    515525            $data = $message;
    516526        } else {
  • trunk/wp-admin/css/global.css

    r7538 r7907  
    195195    padding-right: 280px;
    196196}
     197
     198.wrap h2.long-header {
     199    padding-right: 0;
     200}
  • trunk/wp-admin/edit-form-advanced.php

    r7888 r7907  
    1  <?php
    2 $action = isset($action)? $action : '';
     1<?php
     2
     3$action = isset($action) ? $action : '';
    34if ( isset($_GET['message']) )
    45    $_GET['message'] = absint( $_GET['message'] );
     
    78$messages[3] = __('Custom field deleted.');
    89$messages[4] = __('Post updated.');
    9 $messages[5] = sprintf( __('Post restored to revision from %s'), wp_post_revision_time( $_GET['revision'] ) );
    10 ?>
    11 <?php if (isset($_GET['message'])) : ?>
    12 <div id="message" class="updated fade"><p><?php echo $messages[$_GET['message']]; ?></p></div>
    13 <?php endif; ?>
    14 
    15 <form name="post" action="post.php" method="post" id="post">
    16 <?php if ( (isset($mode) && 'bookmarklet' == $mode) || isset($_GET['popupurl']) ): ?>
    17 <input type="hidden" name="mode" value="bookmarklet" />
    18 <?php endif; ?>
    19 
    20 <div class="wrap">
    21 <h2><?php _e('Write Post') ?></h2>
    22 <?php
    23 
    24 if (!isset($post_ID) || 0 == $post_ID) {
     10$messages[5] = sprintf( __('Post restored to revision from %s'), wp_post_revision_title( $_GET['revision'], false ) );
     11
     12$notice = false;
     13$notices[1] = __( 'There is an autosave of this post that is more recent than the version below.  <a href="%s">View the autosave</a>.' );
     14
     15if ( !isset($post_ID) || 0 == $post_ID ) {
    2516    $form_action = 'post';
    2617    $temp_ID = -1 * time(); // don't change this formula without looking at wp_write_post()
    2718    $form_extra = "<input type='hidden' id='post_ID' name='temp_ID' value='$temp_ID' />";
    28     wp_nonce_field('add-post');
     19    $autosave = false;
    2920} else {
    3021    $post_ID = (int) $post_ID;
    3122    $form_action = 'editpost';
    3223    $form_extra = "<input type='hidden' id='post_ID' name='post_ID' value='$post_ID' />";
     24    $autosave = wp_get_autosave( $post_id );
     25    if ( $autosave && mysql2date( 'U', $autosave->post_modified_gmt ) > mysql2date( 'U', $post->post_modified_gmt ) )
     26        $notice = sprintf( $notices[1], get_edit_post_link( $autosave->ID ) );
     27}
     28
     29?>
     30<?php if ( $notice ) : ?>
     31<div id="notice" class="error"><p><?php echo $notice ?></p></div>
     32<?php endif; ?>
     33<?php if (isset($_GET['message'])) : ?>
     34<div id="message" class="updated fade"><p><?php echo $messages[$_GET['message']]; ?></p></div>
     35<?php endif; ?>
     36
     37<form name="post" action="post.php" method="post" id="post">
     38<?php if ( (isset($mode) && 'bookmarklet' == $mode) || isset($_GET['popupurl']) ): ?>
     39<input type="hidden" name="mode" value="bookmarklet" />
     40<?php endif; ?>
     41
     42<div class="wrap">
     43<h2><?php _e('Write Post') ?></h2>
     44<?php
     45
     46if ( !isset($post_ID) || 0 == $post_ID)
     47    wp_nonce_field('add-post');
     48else
    3349    wp_nonce_field('update-post_' .  $post_ID);
    34 }
    3550
    3651$form_pingback = '<input type="hidden" name="post_pingback" value="' . (int) get_option('default_pingback_flag') . '" id="post_pingback" />';
  • trunk/wp-admin/includes/post.php

    r7896 r7907  
    11<?php
    22
    3 // Update an existing post with values provided in $_POST.
    4 function edit_post() {
    5 
    6     $post_ID = (int) $_POST['post_ID'];
    7 
    8     if ( 'page' == $_POST['post_type'] ) {
    9         if ( !current_user_can( 'edit_page', $post_ID ) )
    10             wp_die( __('You are not allowed to edit this page.' ));
    11     } else {
    12         if ( !current_user_can( 'edit_post', $post_ID ) )
    13             wp_die( __('You are not allowed to edit this post.' ));
    14     }
    15 
    16     // Autosave shouldn't save too soon after a real save
    17     if ( 'autosave' == $_POST['action'] ) {
    18         $post =& get_post( $post_ID );
    19         $now = time();
    20         $then = strtotime($post->post_date_gmt . ' +0000');
    21         $delta = AUTOSAVE_INTERVAL / 2;
    22         if ( ($now - $then) < $delta )
    23             return $post_ID;
    24     }
    25 
    26     // Rename.
    27     $_POST['ID'] = (int) $_POST['post_ID'];
     3/**
     4 * _wp_translate_postdata() - Rename $_POST data from form names to DB post columns.
     5 *
     6 * Manipulates $_POST directly.
     7 *
     8 * @package WordPress
     9 * @since 2.6
     10 *
     11 * @param bool $update Are we updating a pre-existing post?
     12 * @return object|bool WP_Error on failure, true on success.
     13 */
     14function _wp_translate_postdata( $update = false ) {
     15    if ( $update )
     16        $_POST['ID'] = (int) $_POST['post_ID'];
    2817    $_POST['post_content'] = $_POST['content'];
    2918    $_POST['post_excerpt'] = $_POST['excerpt'];
     
    3322    if (!empty ( $_POST['post_author_override'] ) ) {
    3423        $_POST['post_author'] = (int) $_POST['post_author_override'];
    35     } else
     24    } else {
    3625        if (!empty ( $_POST['post_author'] ) ) {
    3726            $_POST['post_author'] = (int) $_POST['post_author'];
     
    3928            $_POST['post_author'] = (int) $_POST['user_ID'];
    4029        }
     30    }
    4131
    4232    if ( $_POST['post_author'] != $_POST['user_ID'] ) {
    4333        if ( 'page' == $_POST['post_type'] ) {
    44             if ( !current_user_can( 'edit_others_pages' ) )
    45                 wp_die( __('You are not allowed to edit pages as this user.' ));
     34            if ( !current_user_can( 'edit_others_pages' ) ) {
     35                return new WP_Error( 'edit_others_pages', $update ?
     36                    __( 'You are not allowed to edit pages as this user.' ) :
     37                    __( 'You are not allowed to create pages as this user.' )
     38                );
     39            }
    4640        } else {
    47             if ( !current_user_can( 'edit_others_posts' ) )
    48                 wp_die( __('You are not allowed to edit posts as this user.' ));
    49 
     41            if ( !current_user_can( 'edit_others_posts' ) ) {
     42                return new WP_Error( 'edit_others_posts', $update ?
     43                    __( 'You are not allowed to edit posts as this user.' ) :
     44                    __( 'You are not allowed to post as this user.' )
     45                );
     46            }
    5047        }
    5148    }
     
    6259
    6360    if ( 'page' == $_POST['post_type'] ) {
    64         if ('publish' == $_POST['post_status'] && !current_user_can( 'publish_pages' ))
     61        if ( 'publish' == $_POST['post_status'] && !current_user_can( 'publish_pages' ) )
    6562            $_POST['post_status'] = 'pending';
    6663    } else {
    67         if ('publish' == $_POST['post_status'] && !current_user_can( 'publish_posts' ))
     64        if ( 'publish' == $_POST['post_status'] && !current_user_can( 'publish_posts' ) )
    6865            $_POST['post_status'] = 'pending';
    6966    }
     
    7572        $_POST['ping_status'] = 'closed';
    7673
    77     foreach ( array ('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
     74    foreach ( array('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
    7875        if ( !empty( $_POST['hidden_' . $timeunit] ) && $_POST['hidden_' . $timeunit] != $_POST[$timeunit] ) {
    7976            $_POST['edit_date'] = '1';
     
    8279    }
    8380
    84     if (!empty ( $_POST['edit_date'] ) ) {
     81    if ( !empty( $_POST['edit_date'] ) ) {
    8582        $aa = $_POST['aa'];
    8683        $mm = $_POST['mm'];
     
    9390        $mn = ($mn > 59 ) ? $mn -60 : $mn;
    9491        $ss = ($ss > 59 ) ? $ss -60 : $ss;
    95         $_POST['post_date'] = "$aa-$mm-$jj $hh:$mn:$ss";
    96         $_POST['post_date_gmt'] = get_gmt_from_date( "$aa-$mm-$jj $hh:$mn:$ss" );
    97     }
     92        $_POST['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );
     93        $_POST['post_date_gmt'] = get_gmt_from_date( $_POST['post_date'] );
     94    }
     95
     96    return true;
     97}
     98
     99
     100// Update an existing post with values provided in $_POST.
     101function edit_post() {
     102
     103    $post_ID = (int) $_POST['post_ID'];
     104
     105    if ( 'page' == $_POST['post_type'] ) {
     106        if ( !current_user_can( 'edit_page', $post_ID ) )
     107            wp_die( __('You are not allowed to edit this page.' ));
     108    } else {
     109        if ( !current_user_can( 'edit_post', $post_ID ) )
     110            wp_die( __('You are not allowed to edit this post.' ));
     111    }
     112
     113    // Autosave shouldn't save too soon after a real save
     114    if ( 'autosave' == $_POST['action'] ) {
     115        $post =& get_post( $post_ID );
     116        $now = time();
     117        $then = strtotime($post->post_date_gmt . ' +0000');
     118        $delta = AUTOSAVE_INTERVAL / 2;
     119        if ( ($now - $then) < $delta )
     120            return $post_ID;
     121    }
     122
     123    $translated = _wp_translate_postdata( true );
     124    if ( is_wp_error($translated) )
     125        wp_die( $translated->get_error_message() );
    98126
    99127    // Meta Stuff
     
    237265    }
    238266
    239     // Rename.
    240     $_POST['post_content'] = $_POST['content'];
    241     $_POST['post_excerpt'] = $_POST['excerpt'];
    242     $_POST['post_parent'] = isset($_POST['parent_id'])? $_POST['parent_id'] : '';
    243     $_POST['to_ping'] = $_POST['trackback_url'];
    244 
    245     if (!empty ( $_POST['post_author_override'] ) ) {
    246         $_POST['post_author'] = (int) $_POST['post_author_override'];
    247     } else {
    248         if (!empty ( $_POST['post_author'] ) ) {
    249             $_POST['post_author'] = (int) $_POST['post_author'];
    250         } else {
    251             $_POST['post_author'] = (int) $_POST['user_ID'];
    252         }
    253 
    254     }
    255 
    256     if ( $_POST['post_author'] != $_POST['user_ID'] ) {
    257         if ( 'page' == $_POST['post_type'] ) {
    258             if ( !current_user_can( 'edit_others_pages' ) )
    259                 return new WP_Error( 'edit_others_pages', __( 'You are not allowed to create pages as this user.' ) );
    260         } else {
    261             if ( !current_user_can( 'edit_others_posts' ) )
    262                 return new WP_Error( 'edit_others_posts', __( 'You are not allowed to post as this user.' ) );
    263 
    264         }
    265     }
    266 
    267     // What to do based on which button they pressed
    268     if ( isset($_POST['saveasdraft']) && '' != $_POST['saveasdraft'] )
    269         $_POST['post_status'] = 'draft';
    270     if ( isset($_POST['saveasprivate']) && '' != $_POST['saveasprivate'] )
    271         $_POST['post_status'] = 'private';
    272     if ( isset($_POST['publish']) && ( '' != $_POST['publish'] ) && ( $_POST['post_status'] != 'private' ) )
    273         $_POST['post_status'] = 'publish';
    274     if ( isset($_POST['advanced']) && '' != $_POST['advanced'] )
    275         $_POST['post_status'] = 'draft';
    276 
    277     if ( 'page' == $_POST['post_type'] ) {
    278         if ('publish' == $_POST['post_status'] && !current_user_can( 'publish_pages' ) )
    279             $_POST['post_status'] = 'pending';
    280     } else {
    281         if ('publish' == $_POST['post_status'] && !current_user_can( 'publish_posts' ) )
    282             $_POST['post_status'] = 'pending';
    283     }
    284 
    285     if (!isset( $_POST['comment_status'] ))
    286         $_POST['comment_status'] = 'closed';
    287 
    288     if (!isset( $_POST['ping_status'] ))
    289         $_POST['ping_status'] = 'closed';
    290 
    291     foreach ( array ('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
    292         if ( !empty( $_POST['hidden_' . $timeunit] ) && $_POST['hidden_' . $timeunit] != $_POST[$timeunit] ) {
    293             $_POST['edit_date'] = '1';
    294             break;
    295         }
    296     }
    297 
    298     if (!empty ( $_POST['edit_date'] ) ) {
    299         $aa = $_POST['aa'];
    300         $mm = $_POST['mm'];
    301         $jj = $_POST['jj'];
    302         $hh = $_POST['hh'];
    303         $mn = $_POST['mn'];
    304         $ss = $_POST['ss'];
    305         $jj = ($jj > 31 ) ? 31 : $jj;
    306         $hh = ($hh > 23 ) ? $hh -24 : $hh;
    307         $mn = ($mn > 59 ) ? $mn -60 : $mn;
    308         $ss = ($ss > 59 ) ? $ss -60 : $ss;
    309         $_POST['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );
    310         $_POST['post_date_gmt'] = get_gmt_from_date( $_POST['post_date'] );
    311     }
     267    $translated = _wp_translate_postdata( false );
     268    if ( is_wp_error($translated) )
     269        return $translated;
    312270
    313271    // Create the post.
     
    688646}
    689647
    690 ?>
     648/**
     649 * wp_create_autosave() - creates autosave data for the specified post from $_POST data
     650 *
     651 * @package WordPress
     652 * @subpackage Post Revisions
     653 * @since 2.6
     654 *
     655 * @uses _wp_translate_postdata()
     656 * @uses _wp_revision_fields()
     657 */
     658function wp_create_autosave( $post_id ) {
     659    $translated = _wp_translate_postdata( true );
     660    if ( is_wp_error( $translated ) )
     661        return $translated;
     662
     663    // Only store one autosave.  If there is already an autosave, overwrite it.
     664    if ( $old_autosave = wp_get_autosave( $post_id ) ) {
     665        $new_autosave = _wp_revision_fields( $_POST, true );
     666        $new_autosave['ID'] = $old_autosave->ID;
     667        return wp_update_post( $new_autosave );
     668    }
     669
     670    // Otherwise create the new autosave as a special post revision
     671    return _wp_put_revision( $_POST, true );
     672}
  • trunk/wp-admin/revision.php

    r7747 r7907  
    33require_once('admin.php');
    44
    5 $parent_file = 'edit.php';
    6 $submenu_file = 'edit.php';
    7 
    8 wp_reset_vars(array('revision', 'diff', 'restore'));
    9 
     5wp_reset_vars(array('revision', 'left', 'right', 'action'));
    106$revision_id = absint($revision);
    117$diff        = absint($diff);
     8$left        = absint($left);
     9$right       = absint($right);
    1210
    13 if ( $diff ) {
    14     $restore = false;
    15     $revision = get_post( $revision_id );
    16     $post = 'revision' == $revision->post_type ? get_post( $revision->post_parent ) : get_post( $revision_id );
    17     $left_revision = get_post( $diff );
     11
     12$parent_file = $redirect = 'edit.php';
     13$submenu_file = 'edit.php';
     14$title = __( 'Post Revision' );
     15
     16
     17switch ( $action ) :
     18case 'delete' : // stubs
     19case 'edit' :
     20    $redirect = remove_query_arg( 'action' );
     21    break;
     22case 'restore' :
     23    if ( !current_user_can( 'edit_post', $revision->post_parent ) )
     24        break;
     25    if ( !$revision = wp_get_revision( $revision_id ) )
     26        break;
     27    if ( !$post = get_post( $revision->post_parent ) )
     28        break;
     29
     30    check_admin_referer( "restore-post_$post->ID|$revision->ID" );
     31
     32    wp_restore_revision( $revision->ID );
     33    $redirect = add_query_arg( array( 'message' => 5, 'revision' => $revision->ID ), get_edit_post_link( $post->ID, 'url' ) );
     34    break;
     35case 'diff' :
     36    if ( !$left_revision  = get_post( $left ) )
     37        break;
     38    if ( !$right_revision = get_post( $right ) )
     39        break;
     40
     41    if ( !current_user_can( 'edit_post', $left_revision->ID ) || !current_user_can( 'edit_post', $right_revision->ID ) )
     42        break;
    1843
    1944    // Don't allow reverse diffs?
    20     if ( strtotime($revision->post_modified_gmt) < strtotime($left_revision->post_modified_gmt) ) {
    21         wp_redirect( add_query_arg( array( 'diff' => $revision->ID, 'revision' => $diff ) ) );
    22         exit;
     45    if ( strtotime($right_revision->post_modified_gmt) < strtotime($left_revision->post_modified_gmt) ) {
     46        $redirect = add_query_arg( array( 'left' => $right, 'right' => $left ) );
     47        break;
    2348    }
    2449
    25     $h2 = __( 'Compare Revisions of &#8220;%1$s&#8221;' );
    26     $right = $revision->ID;
    27     $left  = $left_revision->ID;
     50    if ( $left_revision->ID == $right_revision->post_parent ) // right is a revision of left
     51        $post =& $left_revision;
     52    elseif ( $left_revision->post_parent == $right_revision->ID ) // left is a revision of right
     53        $post =& $right_revision;
     54    elseif ( $left_revision->post_parent == $right_revision->post_parent ) // both are revisions of common parent
     55        $post = get_post( $left_revision->post_parent );
     56    else
     57        break; // Don't diff two unrelated revisions
    2858
    2959    if (
    3060        // They're the same
    31         $left_revision->ID == $revision->ID
    32     ||
    33         // They don't have a comment parent (and we're not comparing a revision to it's post)
    34         ( $left_revision->ID != $revision->post_parent && $left_revision->post_parent != $revision->ID && $left_revision->post_parent != $revision->post_parent )
     61        $left_revision->ID == $right_revision->ID
    3562    ||
    3663        // Neither is a revision
    37         ( !wp_get_revision( $left_revision->ID ) && !wp_get_revision( $revision->ID ) )
    38     ) {
    39         wp_redirect( get_edit_post_link( $revision->ID, 'url' ) );
    40         exit();
    41     }
    42 } else {
    43     $revision = wp_get_revision( $revision_id );
    44     $post = get_post( $revision->post_parent );
    45     $h2 = __( 'Post Revision for &#8220;%1$s&#8221; created on %2$s' );
     64        ( !wp_get_revision( $left_revision->ID ) && !wp_get_revision( $right_revision->ID ) )
     65    )
     66        break;
     67   
     68    $post_title = '<a href="' . get_edit_post_link() . '">' . get_the_title() . '</a>';
     69    $h2 = sprintf( __( 'Compare Revisions of &#8220;%1$s&#8221;' ), $post_title );
     70
     71    $left  = $left_revision->ID;
     72    $right = $right_revision->ID;
     73
     74    $redirect = false;
     75    break;
     76case 'view' :
     77default :
     78    if ( !$revision = wp_get_revision( $revision_id ) )
     79        break;
     80    if ( !$post = get_post( $revision->post_parent ) )
     81        break;
     82
     83    if ( !current_user_can( 'edit_post', $revision->ID ) || !current_user_can( 'edit_post', $post->ID ) )
     84        break;
     85
     86    $post_title = '<a href="' . get_edit_post_link() . '">' . get_the_title() . '</a>';
     87    $revision_title = wp_post_revision_title( $revision, false );
     88    $h2 = sprintf( __( 'Post Revision for &#8220;%1$s&#8221; created on %2$s' ), $post_title, $revision_title );
     89
     90    // Sets up the diff radio buttons
     91    $left  = $revision->ID;
    4692    $right = $post->ID;
    47     $left  = $revision->ID;
    48 }
    4993
    50 if ( !$revision || !$post ) {
    51     wp_redirect("edit.php");
    52     exit();
    53 }
     94    $redirect = false;
     95    break;
     96endswitch;
    5497
    55 if ( $restore && current_user_can( 'edit_post', $revision->post_parent ) ) {
    56     check_admin_referer( "restore-post_$post->ID|$revision->ID" );
    57     wp_restore_revision( $revision->ID );
    58     wp_redirect( add_query_arg( array( 'message' => 5, 'revision' => $revision->ID ), get_edit_post_link( $post->ID, 'url' ) ) );
     98if ( $redirect ) {
     99    wp_redirect( $redirect );
    59100    exit;
    60101}
    61102
     103// Converts post_author ID# into name
    62104add_filter( '_wp_revision_field_post_author', 'get_author_name' );
    63 
    64 $title = __( 'Post Revision' );
    65105
    66106require_once( 'admin-header.php' );
    67107
    68 $post_title = '<a href="' . get_edit_post_link() . '">' . get_the_title() . '</a>';
    69 $revision_time = wp_post_revision_time( $revision );
    70108?>
    71109
    72110<div class="wrap">
    73111
    74 <h2 style="padding-right: 0"><?php printf( $h2, $post_title, $revision_time ); ?></h2>
     112<h2 class="long-header"><?php echo $h2; ?></h2>
    75113
    76114<table class="form-table ie-fixed">
    77115    <col class="th" />
    78 <?php if ( $diff ) : ?>
    79 
     116<?php if ( 'diff' == $action ) : ?>
    80117<tr id="revision">
    81118    <th scope="row"></th>
    82     <th scope="col" class="th-full"><?php printf( __('Older: %s'), wp_post_revision_time( $left_revision ) ); ?></td>
    83     <th scope="col" class="th-full"><?php printf( __('Newer: %s'), wp_post_revision_time( $revision ) ); ?></td>
     119    <th scope="col" class="th-full">
     120        <span class="alignleft"><?php printf( __('Older: %s'), wp_post_revision_title( $left_revision ) ); ?></span>
     121        <span class="alignright"><?php printf( __('Newer: %s'), wp_post_revision_title( $right_revision ) ); ?></span>
     122    </td>
    84123</tr>
    85 
    86124<?php endif;
    87125
    88 // use get_post_to_edit ?
     126// use get_post_to_edit filters?
    89127$identical = true;
    90128foreach ( _wp_revision_fields() as $field => $field_title ) :
    91     if ( !$diff )
     129    if ( 'diff' == $action ) {
     130        $left_content = apply_filters( "_wp_revision_field_$field", $left_revision->$field, $field );
     131        $right_content = apply_filters( "_wp_revision_field_$field", $right_revision->$field, $field );
     132        if ( !$content = wp_text_diff( $left_content, $right_content ) )
     133            continue; // There is no difference between left and right
     134        $identical = false;
     135    } else {
    92136        add_filter( "_wp_revision_field_$field", 'htmlspecialchars' );
    93     $content = apply_filters( "_wp_revision_field_$field", $revision->$field, $field );
    94     if ( $diff ) {
    95         $left_content = apply_filters( "_wp_revision_field_$field", $left_revision->$field, $field );
    96         if ( !$content = wp_text_diff( $left_content, $content ) )
    97             continue;
     137        $content = apply_filters( "_wp_revision_field_$field", $revision->$field, $field );
    98138    }
    99     $identical = false;
    100139    ?>
    101140
    102     <tr id="revision-field-<?php echo $field; ?>"?>
     141    <tr id="revision-field-<?php echo $field; ?>">
    103142        <th scope="row"><?php echo wp_specialchars( $field_title ); ?></th>
    104         <td colspan="2"><pre><?php echo $content; ?></pre></td>
     143        <td><pre><?php echo $content; ?></pre></td>
    105144    </tr>
    106145
     
    109148endforeach;
    110149
    111 if ( $diff && $identical ) :
     150if ( 'diff' == $action && $identical ) :
    112151
    113152    ?>
    114153
    115     <tr><td colspan="3"><div class="updated"><p><?php _e( 'These revisions are identical' ); ?></p></div></td></tr>
     154    <tr><td colspan="2"><div class="updated"><p><?php _e( 'These revisions are identical.' ); ?></p></div></td></tr>
    116155
    117156    <?php
     
    128167
    129168<?php
    130     wp_list_post_revisions( $post, array( 'format' => 'form-table', 'exclude' => $revision->ID, 'parent' => true, 'right' => $right, 'left' => $left ) );
    131169
    132     require_once( 'admin-footer.php' );
     170wp_list_post_revisions( $post, array( 'format' => 'form-table', 'parent' => true, 'right' => $right, 'left' => $left ) );
     171
     172require_once( 'admin-footer.php' );
  • trunk/wp-includes/js/autosave.js

    r7813 r7907  
    169169
    170170    var origStatus = jQuery('#original_post_status').val();
    171     if ( 'draft' != origStatus ) // autosave currently only turned on for drafts
    172         doAutoSave = false;
    173171
    174172    autosaveLast = jQuery("#title").val()+jQuery("#content").val();
  • trunk/wp-includes/pluggable.php

    r7886 r7907  
    13931393 */
    13941394function wp_text_diff( $left_string, $right_string, $args = null ) {
    1395     $defaults = array( 'title' => '' );
     1395    $defaults = array( 'title' => '', 'title_left' => '', 'title_right' => '' );
    13961396    $args = wp_parse_args( $args, $defaults );
    13971397
     
    14261426    $r .= "<col class='ltype' /><col class='content' /><col class='ltype' /><col class='content' />";
    14271427
     1428    if ( $args['title'] || $args['title_left'] || $args['title_right'] )
     1429        $r .= "<thead>";
    14281430    if ( $args['title'] )
    1429         $r .= "<thead><tr><th colspan='4'>$args[title]</th></tr></thead>\n";
     1431        $r .= "<tr class='diff-title'><th colspan='4'>$args[title]</th></tr>\n";
     1432    if ( $args['title_left'] || $args['title_right'] ) {
     1433        $r .= "<tr class='diff-sub-title'>\n";
     1434        $r .= "\t<td></td><th>$args[title_left]</th>\n";
     1435        $r .= "\t<td></td><th>$args[title_right]</th>\n";
     1436        $r .= "</tr>\n";
     1437    }
     1438    if ( $args['title'] || $args['title_left'] || $args['title_right'] )
     1439        $r .= "</thead>\n";
    14301440
    14311441    $r .= "<tbody>\n$diff\n</tbody>\n";
  • trunk/wp-includes/post-template.php

    r7883 r7907  
    567567
    568568/**
    569  * wp_post_revision_time() - returns formatted datetimestamp of a revision
     569 * wp_post_revision_title() - returns formatted datetimestamp of a revision (linked to that revisions's page)
    570570 *
    571571 * @package WordPress
     
    573573 * @since 2.6
    574574 *
    575  * @uses wp_get_revision()
    576575 * @uses date_i18n()
    577576 *
    578577 * @param int|object $revision revision ID or revision object
     578 * @param bool $link optional Link to revisions's page?
    579579 * @return string i18n formatted datetimestamp or localized 'Corrent Revision'
    580580 */
    581 function wp_post_revision_time( $revision ) {
    582     if ( !$revision = wp_get_revision( $revision ) ) {
    583         if ( $revision = get_post( $revision ) )
    584             return __( 'Current Revision' );
     581function wp_post_revision_title( $revision, $link = true ) {
     582    if ( !$revision = get_post( $revision ) )
    585583        return $revision;
    586     }
    587 
    588     $datef  = _c( 'j F, Y @ G:i|revision date format');
    589     return date_i18n( $datef, strtotime( $revision->post_date_gmt . ' +0000' ) );
     584
     585    if ( !in_array( $revision->post_type, array( 'post', 'page', 'revision' ) ) )
     586        return false;
     587
     588    $datef = _c( 'j F, Y @ G:i|revision date format');
     589    $autosavef = __( '%s [Autosave]' );
     590    $currentf  = __( '%s [Current Revision]' );
     591
     592    $date = date_i18n( $datef, strtotime( $revision->post_modified_gmt . ' +0000' ) );
     593    if ( $link && current_user_can( 'edit_post', $revision->ID ) && $link = get_edit_post_link( $revision->ID ) )
     594        $date = "<a href='$link'>$date</a>";
     595
     596    if ( 'revision' != $revision->post_type )
     597        $date = sprintf( $currentf, $date );
     598    elseif ( "{$revision->post_parent}-autosave" == $revision->post_name )
     599        $date = sprintf( $autosavef, $date );
     600
     601    return $date;
    590602}
    591603
     
    606618 *
    607619 * @uses wp_get_post_revisions()
    608  * @uses wp_post_revision_time()
     620 * @uses wp_post_revision_title()
    609621 * @uses get_edit_post_link()
    610622 * @uses get_author_name()
     
    631643    $class = false;
    632644    foreach ( $revisions as $revision ) {
    633         $date = wp_post_revision_time( $revision );
    634         if ( $link = get_edit_post_link( $revision->ID ) )
    635             $date = "<a href='$link'>$date</a>";
     645        $date = wp_post_revision_title( $revision );
    636646        $name = get_author_name( $revision->post_author );
    637647
    638648        if ( 'form-table' == $format ) {
    639649            if ( $left )
    640                 $old_checked = $left == $revision->ID ? ' checked="checked"' : '';
     650                $left_checked = $left == $revision->ID ? ' checked="checked"' : '';
    641651            else
    642                 $old_checked = $new_checked ? ' checked="checked"' : '';
    643             $new_checked = $right == $revision->ID ? ' checked="checked"' : '';
     652                $left_checked = $right_checked ? ' checked="checked"' : ''; // [sic] (the next one)
     653            $right_checked = $right == $revision->ID ? ' checked="checked"' : '';
    644654
    645655            $class = $class ? '' : " class='alternate'";
    646656
    647657            if ( $post->ID != $revision->ID && current_user_can( 'edit_post', $post->ID ) )
    648                 $actions = '<a href="' . wp_nonce_url( add_query_arg( array( 'revision' => $revision->ID, 'diff' => false, 'restore' => 'restore' ) ), "restore-post_$post->ID|$revision->ID" ) . '">' . __( 'Restore' ) . '</a>';
     658                $actions = '<a href="' . wp_nonce_url( add_query_arg( array( 'revision' => $revision->ID, 'diff' => false, 'action' => 'restore' ) ), "restore-post_$post->ID|$revision->ID" ) . '">' . __( 'Restore' ) . '</a>';
    649659            else
    650660                $actions = '';
    651661
    652662            $rows .= "<tr$class>\n";
    653             $rows .= "\t<th style='white-space: nowrap' scope='row'><input type='radio' name='diff' value='$revision->ID'$old_checked /><input type='radio' name='revision' value='$revision->ID'$new_checked />\n";
     663            $rows .= "\t<th style='white-space: nowrap' scope='row'><input type='radio' name='left' value='$revision->ID'$left_checked /><input type='radio' name='right' value='$revision->ID'$right_checked />\n";
    654664            $rows .= "\t<td>$date</td>\n";
    655665            $rows .= "\t<td>$name</td>\n";
     
    657667            $rows .= "</tr>\n";
    658668        } else {
    659             $rows .= "\t<li>" . sprintf( $titlef, $date, $name ). "</li>\n";
     669            $title = sprintf( $titlef, $date, $name );
     670            $rows .= "\t<li>$title</li>\n";
    660671        }
    661672    }
     
    668679    <div class="alignleft">
    669680        <input type="submit" class="button-secondary" value="<?php _e( 'Compare Revisions' ); ?>" />
     681        <input type="hidden" name="action" value="diff" />
    670682    </div>
    671683</div>
  • trunk/wp-includes/post.php

    r7900 r7907  
    8989
    9090    $children = get_posts( $r );
    91 
    9291    if ( !$children )
    9392        return false;
     
    29582957 * _wp_revision_fields() - determines which fields of posts are to be saved in revisions
    29592958 *
    2960  * Does two things. If passed a postn *array*, it will return a post array ready to be
     2959 * Does two things. If passed a post *array*, it will return a post array ready to be
    29612960 * insterted into the posts table as a post revision.
    2962  * Otherwise, returns an array whose keys are the post fields to be saved post revisions.
     2961 * Otherwise, returns an array whose keys are the post fields to be saved for post revisions.
    29632962 *
    29642963 * @package WordPress
     
    29672966 *
    29682967 * @param array $post optional a post array to be processed for insertion as a post revision
     2968 * @param bool $autosave optional Is the revision an autosave?
    29692969 * @return array post array ready to be inserted as a post revision or array of fields that can be versioned
    29702970 */
    2971 function _wp_revision_fields( $post = null ) {
     2971function _wp_revision_fields( $post = null, $autosave = false ) {
    29722972    static $fields = false;
    29732973
     
    29812981        );
    29822982
     2983        // Runs only once
     2984        $fields = apply_filters( '_wp_revision_fields', $fields );
     2985
    29832986        // WP uses these internally either in versioning or elsewhere - they cannot be versioned
    29842987        foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count' ) as $protect )
     
    29962999    $return['post_status']   = 'inherit';
    29973000    $return['post_type']     = 'revision';
    2998     $return['post_name']     = "$post[ID]-revision";
     3001    $return['post_name']     = $autosave ? "$post[ID]-autosave" : "$post[ID]-revision";
    29993002    $return['post_date']     = $post['post_modified'];
    30003003    $return['post_date_gmt'] = $post['post_modified_gmt'];
     
    30163019 */
    30173020function wp_save_revision( $post_id ) {
    3018     // TODO: rework autosave to use special type of post revision
     3021    // We do autosaves manually with wp_create_autosave()
    30193022    if ( @constant( 'DOING_AUTOSAVE' ) )
    30203023        return;
     
    30233026        return;
    30243027
    3025     // TODO: open this up for pages also
    3026     if ( 'post' != $post->post_type )
     3028    if ( !in_array( $post['post_type'], array( 'post', 'page' ) ) )
    30273029        return;
    30283030
     
    30313033
    30323034/**
    3033  * _wp_put_revision() - Inserts post data into the posts table as a post revision
     3035 * wp_get_autosave() - returns the autosaved data of the specified post.
     3036 *
     3037 * Returns a post object containing the information that was autosaved for the specified post.
    30343038 *
    30353039 * @package WordPress
     
    30373041 * @since 2.6
    30383042 *
     3043 * @param int $post_id The post ID
     3044 * @return object|bool the autosaved data or false on failure or when no autosave exists
     3045 */
     3046function wp_get_autosave( $post_id ) {
     3047    global $wpdb;
     3048    if ( !$post = get_post( $post_id ) )
     3049        return false;
     3050
     3051    $q = array(
     3052        'name' => "{$post->ID}-autosave",
     3053        'post_parent' => $post->ID,
     3054        'post_type' => 'revision',
     3055        'post_status' => 'inherit'
     3056    );
     3057
     3058    // Use WP_Query so that the result gets cached
     3059    $autosave_query = new WP_Query;
     3060
     3061    add_action( 'parse_query', '_wp_get_autosave_hack' );
     3062    $autosave = $autosave_query->query( $q );
     3063    remove_action( 'parse_query', '_wp_get_autosave_hack' );
     3064
     3065    if ( $autosave && is_array($autosave) && is_object($autosave[0]) )
     3066        return $autosave[0];
     3067
     3068    return false;
     3069}
     3070
     3071// Internally used to hack WP_Query into submission
     3072function _wp_get_autosave_hack( $query ) {
     3073    $query->is_single = false;
     3074}
     3075
     3076/**
     3077 * _wp_put_revision() - Inserts post data into the posts table as a post revision
     3078 *
     3079 * @package WordPress
     3080 * @subpackage Post Revisions
     3081 * @since 2.6
     3082 *
    30393083 * @uses wp_insert_post()
    30403084 *
    30413085 * @param int|object|array $post post ID, post object OR post array
     3086 * @param bool $autosave optional Is the revision an autosave?
    30423087 * @return mixed null or 0 if error, new revision ID if success
    30433088 */
    3044 function _wp_put_revision( $post = null ) {
     3089function _wp_put_revision( $post = null, $autosave = false ) {
    30453090    if ( is_object($post) )
    30463091        $post = get_object_vars( $post );
    30473092    elseif ( !is_array($post) )
    30483093        $post = get_post($post, ARRAY_A);
    3049 
    30503094    if ( !$post || empty($post['ID']) )
    30513095        return;
     
    30543098        return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
    30553099
    3056     $post = _wp_revision_fields( $post );
    3057 
    3058     if ( $revision_id = wp_insert_post( $post ) )
     3100    $post = _wp_revision_fields( $post, $autosave );
     3101
     3102    $revision_id = wp_insert_post( $post );
     3103    if ( is_wp_error($revision_id) )
     3104        return $revision_id;
     3105
     3106    if ( $revision_id )
    30593107        do_action( '_wp_put_revision', $revision_id );
    3060 
    30613108    return $revision_id;
    30623109}
     
    31283175    $update['ID'] = $revision['post_parent'];
    31293176
    3130     if ( $post_id = wp_update_post( $update ) )
     3177    $post_id = wp_update_post( $update );
     3178    if ( is_wp_error( $post_id ) )
     3179        return $post_id;
     3180
     3181    if ( $post_id )
    31313182        do_action( 'wp_restore_revision', $post_id, $revision['ID'] );
    31323183
     
    31543205        return $revision;
    31553206
    3156     if ( $delete = wp_delete_post( $revision->ID ) )
     3207    $delete = wp_delete_post( $revision->ID );
     3208    if ( is_wp_error( $delete ) )
     3209        return $delete;
     3210
     3211    if ( $delete )
    31573212        do_action( 'wp_delete_revision', $revision->ID, $revision );
    31583213
     
    31753230    if ( ( !$post = get_post( $post_id ) ) || empty( $post->ID ) )
    31763231        return array();
    3177 
    3178     if ( !$revisions = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'revision' ) ) )
     3232    if ( !$revisions = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'revision', 'post_status' => 'inherit' ) ) )
    31793233        return array();
    31803234    return $revisions;
    31813235}
    3182 
    3183 ?>
  • trunk/wp-includes/script-loader.php

    r7881 r7907  
    4848        ) );
    4949
    50         $this->add( 'autosave', '/wp-includes/js/autosave.js', array('schedule', 'wp-ajax-response'), '20080424' );
     50        $this->add( 'autosave', '/wp-includes/js/autosave.js', array('schedule', 'wp-ajax-response'), '20080507' );
    5151
    5252        $this->add( 'wp-ajax', '/wp-includes/js/wp-ajax.js', array('prototype'), '20070306');
Note: See TracChangeset for help on using the changeset viewer.