Create post_status=auto-draft when creating a new post item. status changes to draft on first auto-save. now we always have a real post ID to work with. see #11889. fixes #11145. fixes #11990

git-svn-id: http://svn.automattic.com/wordpress/trunk@12987 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
markjaquith 2010-02-06 10:07:57 +00:00
parent ec16dee2f3
commit 0f924eee99
10 changed files with 128 additions and 139 deletions

View File

@ -887,57 +887,54 @@ case 'autosave' : // The name of this action is hardcoded in edit_post()
$supplemental['session_expired'] = add_query_arg( 'interim-login', 1, wp_login_url() );
$id = $revision_id = 0;
if ( $_POST['post_ID'] < 0 ) {
$post_ID = (int) $_POST['post_ID'];
$_POST['ID'] = $post_ID;
$post = get_post($post_ID);
if ( 'auto-draft' == $post->post_status )
$_POST['post_status'] = 'draft';
$_POST['temp_ID'] = $_POST['post_ID'];
if ( $do_autosave ) {
$id = wp_write_post();
$data = $message;
}
} else {
$post_ID = (int) $_POST['post_ID'];
$_POST['ID'] = $post_ID;
$post = get_post($post_ID);
if ( $last = wp_check_post_lock( $post->ID ) ) {
$do_autosave = $do_lock = false;
if ( $last = wp_check_post_lock( $post->ID ) ) {
$do_autosave = $do_lock = false;
$last_user = get_userdata( $last );
$last_user_name = $last_user ? $last_user->display_name : __( 'Someone' );
$data = new WP_Error( 'locked', sprintf(
$_POST['post_type'] == 'page' ? __( 'Autosave disabled: %s is currently editing this page.' ) : __( 'Autosave disabled: %s is currently editing this post.' ),
esc_html( $last_user_name )
) );
$last_user = get_userdata( $last );
$last_user_name = $last_user ? $last_user->display_name : __( 'Someone' );
$data = new WP_Error( 'locked', sprintf(
$_POST['post_type'] == 'page' ? __( 'Autosave disabled: %s is currently editing this page.' ) : __( 'Autosave disabled: %s is currently editing this post.' ),
esc_html( $last_user_name )
) );
$supplemental['disable_autosave'] = 'disable';
}
if ( 'page' == $post->post_type ) {
if ( !current_user_can('edit_page', $post_ID) )
die(__('You are not allowed to edit this page.'));
} else {
if ( !current_user_can('edit_post', $post_ID) )
die(__('You are not allowed to edit this post.'));
}
if ( $do_autosave ) {
// Drafts are just overwritten by autosave
if ( 'draft' == $post->post_status ) {
$id = edit_post();
} else { // Non drafts are not overwritten. The autosave is stored in a special post revision.
$revision_id = wp_create_post_autosave( $post->ID );
if ( is_wp_error($revision_id) )
$id = $revision_id;
else
$id = $post->ID;
}
$data = $message;
} else {
$id = $post->ID;
}
$supplemental['disable_autosave'] = 'disable';
}
if ( $do_lock && $id && is_numeric($id) )
if ( 'page' == $post->post_type ) {
if ( !current_user_can('edit_page', $post_ID) )
die(__('You are not allowed to edit this page.'));
} else {
if ( !current_user_can('edit_post', $post_ID) )
die(__('You are not allowed to edit this post.'));
}
if ( $do_autosave ) {
// Drafts and auto-drafts are just overwritten by autosave
if ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) {
$id = edit_post();
} else { // Non drafts are not overwritten. The autosave is stored in a special post revision.
$revision_id = wp_create_post_autosave( $post->ID );
if ( is_wp_error($revision_id) )
$id = $revision_id;
else
$id = $post->ID;
}
$data = $message;
} else {
if ( '1' == $_POST['auto_draft'] )
$id = 0; // This tells us it didn't actually save
else
$id = $post->ID;
}
if ( $do_lock && $_POST['auto_draft'] != '1' && $id && is_numeric($id) )
wp_set_post_lock( $id );
if ( $nonce_age == 2 ) {
@ -961,18 +958,6 @@ case 'autosave' : // The name of this action is hardcoded in edit_post()
) );
$x->send();
break;
case 'autosave-generate-nonces' :
check_ajax_referer( 'autosave', 'autosavenonce' );
$ID = (int) $_POST['post_ID'];
$post_type = $_POST['post_type'];
$post_type_object = get_post_type_object($post_type);
if ( !$post_type_object )
die('0');
if ( current_user_can( $post_type_object->edit_cap, $ID ) )
die( json_encode( array( 'updateNonce' => wp_create_nonce( "update-{$post_type}_{$ID}" ), 'deleteURL' => str_replace( '&amp;', '&', wp_nonce_url( admin_url( $post_type . '.php?action=trash&post=' . $ID ), "trash-{$post_type}_{$ID}" ) ) ) ) );
do_action('autosave_generate_nonces');
die('0');
break;
case 'closed-postboxes' :
check_ajax_referer( 'closedpostboxes', 'closedpostboxesnonce' );
$closed = isset( $_POST['closed'] ) ? $_POST['closed'] : '';

View File

@ -60,28 +60,29 @@ if ( isset($_GET['message']) ) {
}
$notice = false;
if ( 0 == $post_ID ) {
$form_action = 'post';
$nonce_action = 'add-' . $post_type;
$temp_ID = -1 * time(); // don't change this formula without looking at wp_write_post()
$form_extra = "<input type='hidden' id='post_ID' name='temp_ID' value='" . esc_attr($temp_ID) . "' />";
$form_extra = '';
if ( 'auto-draft' == $post->post_status ) {
if ( 'edit' == $action )
$post->post_title = '';
$autosave = false;
$form_extra .= "<input type='hidden' id='auto_draft' name='auto_draft' value='1' />";
} else {
$form_action = 'editpost';
$nonce_action = 'update-' . $post_type . '_' . $post_ID;
$form_extra = "<input type='hidden' id='post_ID' name='post_ID' value='" . esc_attr($post_ID) . "' />";
$autosave = wp_get_post_autosave( $post_ID );
}
// Detect if there exists an autosave newer than the post and if that autosave is different than the post
if ( $autosave && mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) {
foreach ( _wp_post_revision_fields() as $autosave_field => $_autosave_field ) {
if ( normalize_whitespace( $autosave->$autosave_field ) != normalize_whitespace( $post->$autosave_field ) ) {
$notice = sprintf( __( 'There is an autosave of this post that is more recent than the version below. <a href="%s">View the autosave</a>' ), get_edit_post_link( $autosave->ID ) );
break;
}
$form_action = 'editpost';
$nonce_action = 'update-' . $post_type . '_' . $post_ID;
$form_extra .= "<input type='hidden' id='post_ID' name='post_ID' value='" . esc_attr($post_ID) . "' />";
// Detect if there exists an autosave newer than the post and if that autosave is different than the post
if ( $autosave && mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) {
foreach ( _wp_post_revision_fields() as $autosave_field => $_autosave_field ) {
if ( normalize_whitespace( $autosave->$autosave_field ) != normalize_whitespace( $post->$autosave_field ) ) {
$notice = sprintf( __( 'There is an autosave of this post that is more recent than the version below. <a href="%s">View the autosave</a>' ), get_edit_post_link( $autosave->ID ) );
break;
}
unset($autosave_field, $_autosave_field);
}
unset($autosave_field, $_autosave_field);
}
$post_type_object = get_post_type_object($post_type);
@ -197,7 +198,7 @@ $sample_permalink_html = get_sample_permalink_html($post->ID);
if ( !( 'pending' == $post->post_status && !current_user_can( $post_type_object->publish_cap ) ) ) { ?>
<div id="edit-slug-box">
<?php
if ( ! empty($post->ID) && ! empty($sample_permalink_html) ) :
if ( ! empty($post->ID) && ! empty($sample_permalink_html) && 'auto-draft' != $post->post_status ) :
echo $sample_permalink_html;
endif; ?>
</div>
@ -215,7 +216,7 @@ endif; ?>
<td class="autosave-info">
<span id="autosave">&nbsp;</span>
<?php
if ( $post_ID ) {
if ( 'auto-draft' != $post->post_status ) {
echo '<span id="last-edit">';
if ( $last_id = get_post_meta($post_ID, '_edit_last', true) ) {
$last_user = get_userdata($last_id);

View File

@ -71,8 +71,12 @@ switch ( $post->post_status ) {
_e('Pending Review');
break;
case 'draft':
case 'auto-draft':
_e('Draft');
break;
case 'auto-draft':
_e('Unsaved');
break;
}
?>
</span>
@ -80,7 +84,7 @@ switch ( $post->post_status ) {
<a href="#post_status" <?php if ( 'private' == $post->post_status ) { ?>style="display:none;" <?php } ?>class="edit-post-status hide-if-no-js" tabindex='4'><?php _e('Edit') ?></a>
<div id="post-status-select" class="hide-if-js">
<input type="hidden" name="hidden_post_status" id="hidden_post_status" value="<?php echo esc_attr($post->post_status); ?>" />
<input type="hidden" name="hidden_post_status" id="hidden_post_status" value="<?php echo esc_attr( ('auto-draft' == $post->post_status ) ? 'draft' : $post->post_status); ?>" />
<select name='post_status' id='post_status' tabindex='4'>
<?php if ( 'publish' == $post->post_status ) : ?>
<option<?php selected( $post->post_status, 'publish' ); ?> value='publish'><?php _e('Published') ?></option>
@ -90,7 +94,11 @@ switch ( $post->post_status ) {
<option<?php selected( $post->post_status, 'future' ); ?> value='future'><?php _e('Scheduled') ?></option>
<?php endif; ?>
<option<?php selected( $post->post_status, 'pending' ); ?> value='pending'><?php _e('Pending Review') ?></option>
<?php if ( 'auto-draft' == $post->post_status ) : ?>
<option<?php selected( $post->post_status, 'auto-draft' ); ?> value='draft'><?php _e('Draft') ?></option>
<?php else : ?>
<option<?php selected( $post->post_status, 'draft' ); ?> value='draft'><?php _e('Draft') ?></option>
<?php endif; ?>
</select>
<a href="#post_status" class="save-post-status hide-if-no-js button"><?php _e('OK'); ?></a>
<a href="#post_status" class="cancel-post-status hide-if-no-js"><?php _e('Cancel'); ?></a>

View File

@ -76,8 +76,6 @@ function _wp_translate_postdata( $update = false, $post_data = null ) {
if ( isset( $post_data['ID'] ) )
$post_id = $post_data['ID'];
elseif ( isset( $post_data['temp_ID'] ) )
$post_id = $post_data['temp_ID'];
else
$post_id = false;
$previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false;
@ -336,7 +334,8 @@ function bulk_edit_posts( $post_data = null ) {
*@param string A post type string, defaults to 'post'.
* @return object stdClass object containing all the default post data as attributes
*/
function get_default_post_to_edit( $post_type = 'post' ) {
function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) {
global $wpdb;
$post_title = '';
if ( !empty( $_REQUEST['post_title'] ) )
@ -350,26 +349,35 @@ function get_default_post_to_edit( $post_type = 'post' ) {
if ( !empty( $_REQUEST['excerpt'] ) )
$post_excerpt = esc_html( stripslashes( $_REQUEST['excerpt'] ));
$post->ID = 0;
if ( $create_in_db ) {
// Cleanup old auto-drafts more than 7 days old
$old_posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft' AND DATE_SUB( NOW(), INTERVAL 7 DAY ) > post_date" );
foreach ( (array) $old_posts as $delete )
wp_delete_post( $delete, true ); // Force delete
$post = get_post( wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) ) );
} else {
$post->ID = 0;
$post->post_author = '';
$post->post_date = '';
$post->post_date_gmt = '';
$post->post_password = '';
$post->post_type = $post_type;
$post->post_status = 'draft';
$post->to_ping = '';
$post->pinged = '';
$post->comment_status = get_option( 'default_comment_status' );
$post->ping_status = get_option( 'default_ping_status' );
$post->post_pingback = get_option( 'default_pingback_flag' );
$post->post_category = get_option( 'default_category' );
$post->page_template = 'default';
$post->post_parent = 0;
$post->menu_order = 0;
}
$post->post_content = apply_filters( 'default_content', $post_content );
$post->post_title = apply_filters( 'default_title', $post_title );
$post->post_excerpt = apply_filters( 'default_excerpt', $post_excerpt );
$post->post_name = '';
$post->post_author = '';
$post->post_date = '';
$post->post_date_gmt = '';
$post->post_password = '';
$post->post_status = 'draft';
$post->post_type = $post_type;
$post->to_ping = '';
$post->pinged = '';
$post->comment_status = get_option( 'default_comment_status' );
$post->ping_status = get_option( 'default_ping_status' );
$post->post_pingback = get_option( 'default_pingback_flag' );
$post->post_category = get_option( 'default_category' );
$post->post_content = apply_filters( 'default_content', $post_content);
$post->post_title = apply_filters( 'default_title', $post_title );
$post->post_excerpt = apply_filters( 'default_excerpt', $post_excerpt);
$post->page_template = 'default';
$post->post_parent = 0;
$post->menu_order = 0;
return $post;
}
@ -465,6 +473,7 @@ function wp_write_post() {
}
// Check for autosave collisions
// Does this need to be updated? ~ Mark
$temp_id = false;
if ( isset($_POST['temp_ID']) ) {
$temp_id = (int) $_POST['temp_ID'];
@ -473,7 +482,7 @@ function wp_write_post() {
foreach ( $draft_ids as $temp => $real )
if ( time() + $temp > 86400 ) // 1 day: $temp is equal to -1 * time( then )
unset($draft_ids[$temp]);
if ( isset($draft_ids[$temp_id]) ) { // Edit, don't write
$_POST['post_ID'] = $draft_ids[$temp_id];
unset($_POST['temp_ID']);
@ -513,6 +522,7 @@ function wp_write_post() {
add_meta( $post_ID );
// Reunite any orphaned attachments with their parent
// Does this need to be udpated? ~ Mark
if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) )
$draft_ids = array();
if ( $draft_temp_id = (int) array_search( $post_ID, $draft_ids ) )

View File

@ -50,7 +50,8 @@ When you&#8217;re promoted, just reload this page and you&#8217;ll be able to bl
// Show post form.
if ( current_user_can($post_type_object->edit_type_cap) ) {
$post = get_default_post_to_edit( $post_type );
$post = get_default_post_to_edit( $post_type, true );
$post_ID = $post->ID;
include('edit-form-advanced.php');
}

View File

@ -39,7 +39,7 @@ jQuery(document).ready( function($) {
// preview
$('#post-preview').click(function(){
if ( 1 > $('#post_ID').val() && notSaved ) {
if ( $('#auto_draft').val() == '1' && notSaved ) {
autosaveDelayPreview = true;
autosave();
return false;
@ -58,7 +58,7 @@ jQuery(document).ready( function($) {
if ( typeof tinyMCE != 'undefined' ) {
$('#title')[$.browser.opera ? 'keypress' : 'keydown'](function (e) {
if ( e.which == 9 && !e.shiftKey && !e.controlKey && !e.altKey ) {
if ( ($("#post_ID").val() < 1) && ($("#title").val().length > 0) ) { autosave(); }
if ( ($('#auto_draft').val() == '1') && ($("#title").val().length > 0) ) { autosave(); }
if ( tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden() && dotabkey ) {
e.preventDefault();
dotabkey = false;
@ -70,11 +70,10 @@ jQuery(document).ready( function($) {
}
// autosave new posts after a title is typed but not if Publish or Save Draft is clicked
if ( 0 > $('#post_ID').val() ) {
if ( '1' == $('#auto_draft').val() ) {
$('#title').blur( function() {
if ( !this.value || 0 < $('#post_ID').val() )
if ( !this.value || $('#auto_draft').val() != '1' )
return;
delayed_autosave();
});
}
@ -121,24 +120,19 @@ function autosave_parse_response(response) {
// called when autosaving pre-existing post
function autosave_saved(response) {
blockSave = false;
autosave_parse_response(response); // parse the ajax response
autosave_enable_buttons(); // re-enable disabled form buttons
}
// called when autosaving new post
function autosave_saved_new(response) {
blockSave = false;
var res = autosave_parse_response(response), tempID, postID;
// if no errors: update post_ID from the temporary value, grab new save-nonce for that new ID
if ( res && res.responses.length && !res.errors ) {
tempID = jQuery('#post_ID').val();
// An ID is sent only for real auto-saves, not for autosave=0 "keepalive" saves
postID = parseInt( res.responses[0].id, 10 );
autosave_update_post_ID( postID ); // disabled form buttons are re-enabled here
if ( tempID < 0 && postID > 0 ) { // update media buttons
notSaved = false;
jQuery('#media-buttons a').each(function(){
this.href = this.href.replace(tempID, postID);
});
}
if ( autosaveDelayPreview ) {
autosaveDelayPreview = false;
doPreview();
@ -151,22 +145,11 @@ function autosave_saved_new(response) {
function autosave_update_post_ID( postID ) {
if ( !isNaN(postID) && postID > 0 ) {
if ( postID == parseInt(jQuery('#post_ID').val(), 10) ) { return; } // no need to do this more than once
jQuery('#post_ID').attr({name: "post_ID"});
jQuery('#post_ID').val(postID);
// We need new nonces
jQuery.post(autosaveL10n.requestFile, {
action: "autosave-generate-nonces",
post_ID: postID,
autosavenonce: jQuery('#autosavenonce').val(),
post_type: jQuery('#post_type').val()
}, function(html) {
jQuery('#_wpnonce').val(html.updateNonce);
jQuery('#delete-action a.submitdelete').attr('href', html.deleteURL);
autosave_enable_buttons(); // re-enable disabled form buttons
jQuery('#delete-action a.submitdelete').fadeIn();
},
'json');
notSaved = false;
autosave_enable_buttons();
jQuery('#delete-action a.submitdelete').fadeIn();
jQuery('#hiddenaction').val('editpost');
jQuery('#auto_draft').val('0'); // No longer an auto-draft
}
}
@ -217,6 +200,7 @@ function delayed_autosave() {
autosave = function() {
// (bool) is rich editor enabled and active
blockSave = true;
var rich = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden(), post_data, doAutoSave, ed, origStatus, successCallback;
autosave_disable_buttons();
@ -281,6 +265,8 @@ autosave = function() {
if ( jQuery("#post_author").size() )
post_data["post_author"] = jQuery("#post_author").val();
post_data["user_ID"] = jQuery("#user-id").val();
if ( jQuery('#auto_draft').val() == '1' )
post_data["auto_draft"] = '1';
if ( doAutoSave ) {
autosaveLast = jQuery("#title").val()+jQuery("#content").val();
@ -288,15 +274,13 @@ autosave = function() {
post_data['autosave'] = 0;
}
if ( parseInt(post_data["post_ID"], 10) < 1 ) {
post_data["temp_ID"] = post_data["post_ID"];
if ( post_data["auto_draft"] == '1' ) {
successCallback = autosave_saved_new; // new post
} else {
successCallback = autosave_saved; // pre-existing post
}
autosaveOldMessage = jQuery('#autosave').html();
jQuery.ajax({
data: post_data,
beforeSend: doAutoSave ? autosave_loading : null,

File diff suppressed because one or more lines are too long

View File

@ -111,7 +111,7 @@ function get_permalink($id = 0, $leavename = false) {
$permalink = get_option('permalink_structure');
if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending')) ) {
if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
$unixtime = strtotime($post->post_date);
$category = '';

View File

@ -1976,7 +1976,7 @@ function wp_insert_post($postarr = array(), $wp_error = false) {
// Create a valid post name. Drafts and pending posts are allowed to have an empty
// post name.
if ( !isset($post_name) || empty($post_name) ) {
if ( !in_array( $post_status, array( 'draft', 'pending' ) ) )
if ( !in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) )
$post_name = sanitize_title($post_title);
else
$post_name = '';
@ -1989,7 +1989,7 @@ function wp_insert_post($postarr = array(), $wp_error = false) {
$post_date = current_time('mysql');
if ( empty($post_date_gmt) || '0000-00-00 00:00:00' == $post_date_gmt ) {
if ( !in_array( $post_status, array( 'draft', 'pending' ) ) )
if ( !in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) )
$post_date_gmt = get_gmt_from_date($post_date);
else
$post_date_gmt = '0000-00-00 00:00:00';
@ -2089,7 +2089,7 @@ function wp_insert_post($postarr = array(), $wp_error = false) {
$where = array( 'ID' => $post_ID );
}
if ( empty($data['post_name']) && !in_array( $data['post_status'], array( 'draft', 'pending' ) ) ) {
if ( empty($data['post_name']) && !in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
$data['post_name'] = sanitize_title($data['post_title'], $post_ID);
$wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
}
@ -2172,7 +2172,7 @@ function wp_update_post($postarr = array()) {
$post_cats = $post['post_category'];
// Drafts shouldn't be assigned a date unless explicitly done so by the user
if ( in_array($post['post_status'], array('draft', 'pending')) && empty($postarr['edit_date']) &&
if ( in_array($post['post_status'], array('draft', 'pending', 'auto-draft')) && empty($postarr['edit_date']) &&
('0000-00-00 00:00:00' == $post['post_date_gmt']) )
$clear_date = true;
else
@ -2276,7 +2276,7 @@ function check_and_publish_future_post($post_id) {
* @return string unique slug for the post, based on $post_name (with a -1, -2, etc. suffix)
*/
function wp_unique_post_slug($slug, $post_ID, $post_status, $post_type, $post_parent) {
if ( in_array( $post_status, array( 'draft', 'pending' ) ) )
if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) )
return $slug;
global $wpdb, $wp_rewrite;

View File

@ -99,7 +99,7 @@ function wp_default_scripts( &$scripts ) {
'l10n_print_after' => 'try{convertEntities(wpAjax);}catch(e){};'
) );
$scripts->add( 'autosave', "/wp-includes/js/autosave$suffix.js", array('schedule', 'wp-ajax-response'), '20091012' );
$scripts->add( 'autosave', "/wp-includes/js/autosave$suffix.js", array('schedule', 'wp-ajax-response'), '20100205' );
$scripts->add_data( 'autosave', 'group', 1 );
$scripts->add( 'wp-lists', "/wp-includes/js/wp-lists$suffix.js", array('wp-ajax-response'), '20091128' );