Preview fixes: ensure autosave is run at least once on new posts before preview, properly show previews for pending and scheduled posts.

git-svn-id: http://svn.automattic.com/wordpress/trunk@9473 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
azaozz 2008-11-02 13:56:32 +00:00
parent 7396f548ef
commit 24fc4f0c3b
7 changed files with 58 additions and 84 deletions

View File

@ -1002,34 +1002,36 @@ function wp_create_post_autosave( $post_id ) {
*/
function post_preview() {
$_POST['post_category'] = explode(",", $_POST['catslist']);
$_POST['tags_input'] = explode(",", $_POST['tags_input']);
if($_POST['post_type'] == 'page' || empty($_POST['post_category']))
$post_ID = (int) $_POST['post_ID'];
if ( $post_ID < 1 )
wp_die( __('Preview not available. Please save as draft first.') );
if ( isset($_POST['catslist']) )
$_POST['post_category'] = explode(",", $_POST['catslist']);
if ( isset($_POST['tags_input']) )
$_POST['tags_input'] = explode(",", $_POST['tags_input']);
if ( $_POST['post_type'] == 'page' || empty($_POST['post_category']) )
unset($_POST['post_category']);
if($_POST['post_ID'] < 0) {
$_POST['post_status'] = 'draft';
$_POST['temp_ID'] = $_POST['post_ID'];
$id = wp_write_post();
$_POST['ID'] = $post_ID;
$post = get_post($post_ID);
if ( 'page' == $post->post_type ) {
if ( !current_user_can('edit_page', $post_ID) )
wp_die(__('You are not allowed to edit this page.'));
} else {
$post_ID = (int) $_POST['post_ID'];
$_POST['ID'] = $post_ID;
$post = get_post($post_ID);
if ( !current_user_can('edit_post', $post_ID) )
wp_die(__('You are not allowed to edit this post.'));
}
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 ( 'draft' == $post->post_status ) {
$id = edit_post();
} else { // Non drafts are not overwritten. The autosave is stored in a special post revision.
wp_create_post_autosave( $post->ID );
if ( 'draft' == $post->post_status ) {
$id = edit_post();
} else { // Non drafts are not overwritten. The autosave is stored in a special post revision.
$id = wp_create_post_autosave( $post->ID );
if ( ! is_wp_error($id) )
$id = $post->ID;
}
}
return $id;

View File

@ -108,8 +108,15 @@ jQuery(document).ready( function($) {
// preview
$('#post-preview').click(function(e){
if ( 1 > $('#post_ID').val() && autosaveFirst ) {
autosaveDelayPreview = true;
autosave();
return false;
}
$('input#wp-preview').val('dopreview');
$('form#post').attr('target', 'wp-preview').submit().attr('target', '');
$('input#wp-preview').val('');
return false;
});
});

View File

@ -263,9 +263,16 @@ jQuery(document).ready( function($) {
// preview
$('#post-preview').click(function(e){
if ( 1 > $('#post_ID').val() && autosaveFirst ) {
autosaveDelayPreview = true;
autosave();
return false;
}
$('input#wp-preview').val('dopreview');
$('form#post').attr('target', 'wp-preview').submit().attr('target', '');
$('input#wp-preview').val('');
return false;
});
});

View File

@ -176,19 +176,16 @@ case 'delete':
case 'preview':
check_admin_referer( 'autosave', 'autosavenonce' );
if ( empty($_POST['post_title']) )
wp_die( __('Please enter a title before previewing this page.') );
$id = post_preview();
if ( is_wp_error($id) )
wp_die( $id->get_error_message() );
if ( $_POST['post_status'] == 'publish' ) {
$nonce = wp_create_nonce('post_preview_' . $id);
$url = site_url('?wp_preview=' . $id . '&preview_nonce=' . $nonce);
if ( $_POST['post_status'] == 'draft' ) {
$url = get_option('home') . '/?page_id=' . $id . '&preview=true';
} else {
$url = site_url('?page_id=' . $id . '&preview=true');
$nonce = wp_create_nonce('post_preview_' . $id);
$url = get_option('home') . '/?wp_preview=' . $id . '&preview_nonce=' . $nonce;
}
wp_redirect($url);

View File

@ -223,19 +223,16 @@ case 'delete':
case 'preview':
check_admin_referer( 'autosave', 'autosavenonce' );
if ( empty($_POST['post_title']) )
wp_die( __('Please enter a title before previewing this post.') );
$id = post_preview();
if ( is_wp_error($id) )
wp_die( $id->get_error_message() );
if ( $_POST['post_status'] == 'publish' ) {
if ( $_POST['post_status'] == 'draft' ) {
$url = get_option('home') . '/?p=' . $id . '&preview=true';
} else {
$nonce = wp_create_nonce('post_preview_' . $id);
$url = get_option('home') . '/?wp_preview=' . $id . '&preview_nonce=' . $nonce;
} else {
$url = get_option('home') . '/?p=' . $id . '&preview=true';
}
wp_redirect($url);

View File

@ -1,8 +1,8 @@
var autosaveLast = '';
var autosavePeriodical;
var autosaveOldMessage = '';
var autosaveDelayURL = null;
var previewwin;
var autosaveDelayPreview = false;
var autosaveFirst = true;
jQuery(function($) {
autosaveLast = $('#post #title').val()+$('#post #content').val();
@ -10,16 +10,6 @@ jQuery(function($) {
//Disable autosave after the form has been submitted
$("#post").submit(function() { $.cancel(autosavePeriodical); });
// Autosave when the preview button is clicked.
$('#previewview a').click(function(e) {
autosave();
autosaveDelayURL = this.href;
previewwin = window.open('','_blank');
e.preventDefault();
return false;
});
});
function autosave_parse_response(response) {
@ -41,11 +31,10 @@ function autosave_parse_response(response) {
});
}
// if no errors: add preview link and slug UI
// if no errors: add slug UI
if ( !res.errors ) {
var postID = parseInt( res.responses[0].id );
if ( !isNaN(postID) && postID > 0 ) {
autosave_update_preview_link(postID);
autosave_update_slug(postID);
}
}
@ -69,10 +58,14 @@ function autosave_saved_new(response) {
var tempID = jQuery('#post_ID').val();
var postID = parseInt( res.responses[0].id );
autosave_update_post_ID( postID ); // disabled form buttons are re-enabled here
if ( tempID < 0 && postID > 0) // update media buttons
if ( tempID < 0 && postID > 0 ) // update media buttons
jQuery('#media-buttons a').each(function(){
this.href = this.href.replace(tempID, postID);
});
// activate preview
autosaveFirst = false;
if ( autosaveDelayPreview )
jQuery('#post-preview').click();
} else {
autosave_enable_buttons(); // re-enable disabled form buttons
}
@ -97,31 +90,6 @@ function autosave_update_post_ID( postID ) {
}
}
function autosave_update_preview_link(post_id) {
// Add preview button if not already there
if ( !jQuery('#previewview > *').size() ) {
var post_type = jQuery('#post_type').val();
var previewText = 'page' == post_type ? autosaveL10n.previewPageText : autosaveL10n.previewPostText;
jQuery.post(autosaveL10n.requestFile, {
action: "get-permalink",
post_id: post_id,
getpermalinknonce: jQuery('#getpermalinknonce').val()
}, function(permalink) {
jQuery('#previewview').html('<a class="button" target="_blank" href="'+permalink+'" tabindex="4">'+previewText+'</a>');
// Autosave when the preview button is clicked.
jQuery('#previewview a').click(function(e) {
autosave();
autosaveDelayURL = this.href;
previewwin = window.open('','_blank');
e.preventDefault();
return false;
});
});
}
}
function autosave_update_slug(post_id) {
// create slug area only if not already there
if ( jQuery.isFunction(make_slugedit_clickable) && !jQuery('#edit-slug-box > *').size() ) {
@ -147,10 +115,6 @@ function autosave_loading() {
function autosave_enable_buttons() {
jQuery("#submitpost :button:disabled, #submitpost :submit:disabled").attr('disabled', '');
if ( autosaveDelayURL ) {
previewwin.location = autosaveDelayURL;
autosaveDelayURL = null;
}
}
function autosave_disable_buttons() {
@ -223,7 +187,7 @@ var autosave = function() {
if(parseInt(post_data["post_ID"]) < 1) {
post_data["temp_ID"] = post_data["post_ID"];
var successCallback = autosave_saved_new;; // new post
var successCallback = autosave_saved_new; // new post
} else {
var successCallback = autosave_saved; // pre-existing post
}

View File

@ -75,7 +75,7 @@ function wp_default_scripts( &$scripts ) {
'broken' => __('An unidentified error has occurred.')
) );
$scripts->add( 'autosave', '/wp-includes/js/autosave.js', array('schedule', 'wp-ajax-response'), '20080701' );
$scripts->add( 'autosave', '/wp-includes/js/autosave.js', array('schedule', 'wp-ajax-response'), '20081102' );
$scripts->add( 'wp-lists', '/wp-includes/js/wp-lists.js', array('wp-ajax-response'), '20080729' );
$scripts->localize( 'wp-lists', 'wpListL10n', array(
@ -176,7 +176,7 @@ function wp_default_scripts( &$scripts ) {
'save' => __('Save'),
'cancel' => __('Cancel'),
) );
$scripts->add( 'post', '/wp-admin/js/post.js', array('suggest', 'jquery-ui-tabs', 'wp-lists', 'postbox', 'slug', 'settings-box'), '20081031' );
$scripts->add( 'post', '/wp-admin/js/post.js', array('suggest', 'jquery-ui-tabs', 'wp-lists', 'postbox', 'slug', 'settings-box'), '20081102' );
$scripts->localize( 'post', 'postL10n', array(
'tagsUsed' => __('Tags used on this post:'),
'add' => attribute_escape(__('Add')),
@ -195,7 +195,7 @@ function wp_default_scripts( &$scripts ) {
'savePending' => __('Save Pending'),
'saveDraft' => __('Save Draft')
) );
$scripts->add( 'page', '/wp-admin/js/page.js', array('jquery', 'slug', 'postbox', 'settings-box'), '20081031' );
$scripts->add( 'page', '/wp-admin/js/page.js', array('jquery', 'slug', 'postbox', 'settings-box'), '20081102' );
$scripts->localize( 'page', 'postL10n', array(
'cancel' => __('Cancel'),
'edit' => __('Edit'),