Preview for both drafts and published posts/pages

git-svn-id: http://svn.automattic.com/wordpress/trunk@9451 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
azaozz 2008-10-31 22:47:07 +00:00
parent 0018370f9d
commit 9fba444f3b
11 changed files with 154 additions and 9 deletions

View File

@ -170,11 +170,17 @@ switch ( $post->post_status ) {
<input type="submit" name="save" id="save-post" value="<?php echo attribute_escape( __('Save Pending') ); ?>" tabindex="4" class="button button-highlighted" />
<?php } ?>
<noscript>
<?php if ( 'publish' == $post->post_status ) { ?>
<a class="preview button" href="<?php echo clean_url(get_permalink($post->ID)); ?>" target="_blank" tabindex="4"><?php _e('View Post'); ?></a>
<?php } else { ?>
<a class="preview button" href="<?php echo clean_url(apply_filters('preview_post_link', add_query_arg('preview', 'true', get_permalink($post->ID)))); ?>" target="_blank" tabindex="4"><?php _e('Preview'); ?></a>
<?php } ?>
</noscript>
<a class="preview button hide-if-no-js" href="#" id="post-preview" tabindex="4"><?php _e('Preview'); ?></a>
<input type="hidden" name="wp-preview" id="wp-preview" value="" />
</div>
<div class="clear"></div>
</div>

View File

@ -160,11 +160,17 @@ switch ( $post->post_status ) {
<input type="submit" name="save" id="save-post" value="<?php echo attribute_escape( __('Save Pending') ); ?>" tabindex="4" class="button button-highlighted" />
<?php } ?>
<noscript>
<?php if ( 'publish' == $post->post_status ) { ?>
<a class="preview button" href="<?php echo clean_url(get_permalink($post->ID)); ?>" target="_blank" tabindex="4"><?php _e('View Post'); ?></a>
<?php } else { ?>
<a class="preview button" href="<?php echo clean_url(apply_filters('preview_post_link', add_query_arg('preview', 'true', get_permalink($post->ID)))); ?>" target="_blank" tabindex="4"><?php _e('Preview'); ?></a>
<?php } ?>
</noscript>
<a class="preview button hide-if-no-js" href="#" id="post-preview" tabindex="4"><?php _e('Preview'); ?></a>
<input type="hidden" name="wp-preview" id="wp-preview" value="" />
</div>
<div class="clear"></div>
</div>

View File

@ -987,7 +987,56 @@ function wp_create_post_autosave( $post_id ) {
}
/**
* wp_tiny_mce() - adds the TinyMCE editor used on the Write and Edit screens.
* Save draft or manually autosave for showing preview.
*
* @package WordPress
* @since 2.7
*
* @uses wp_write_post()
* @uses edit_post()
* @uses get_post()
* @uses current_user_can()
* @uses wp_create_post_autosave()
*
* @return int|object the saved post id or wp_error object
*/
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']))
unset($_POST['post_category']);
if($_POST['post_ID'] < 0) {
$_POST['post_status'] = 'draft';
$_POST['temp_ID'] = $_POST['post_ID'];
$id = wp_write_post();
} else {
$post_ID = (int) $_POST['post_ID'];
$_POST['ID'] = $post_ID;
$post = get_post($post_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 ( '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 );
$id = $post->ID;
}
}
return $id;
}
/**
* Adds the TinyMCE editor used on the Write and Edit screens.
*
* Has option to output a trimmed down version used in Press This.
*

View File

@ -24,7 +24,6 @@ switchEditors = {
saveCallback : function(el, content, body) {
this.I(el).style.color = '#fff';
if ( tinyMCE.activeEditor.isHidden() )
content = this.I(el).value;
else

View File

@ -105,4 +105,11 @@ jQuery(document).ready( function($) {
return false;
});
// preview
$('#post-preview').click(function(e){
$('input#wp-preview').val('dopreview');
$('form#post').attr('target', 'wp-preview').submit().attr('target', '');
$('input#wp-preview').val('');
});
});

View File

@ -260,6 +260,14 @@ jQuery(document).ready( function($) {
return s;
}
});
// preview
$('#post-preview').click(function(e){
$('input#wp-preview').val('dopreview');
$('form#post').attr('target', 'wp-preview').submit().attr('target', '');
$('input#wp-preview').val('');
});
});
(function($){
@ -314,7 +322,7 @@ jQuery(document).ready( function($) {
return false;
}
}
};
})(jQuery);

View File

@ -67,9 +67,10 @@ function redirect_page($page_ID) {
wp_redirect($location);
}
if (isset($_POST['deletepost'])) {
$action = "delete";
}
if (isset($_POST['deletepost']))
$action = "delete";
elseif ( isset($_POST['wp-preview']) && 'dopreview' == $_POST['wp-preview'] )
$action = 'preview';
switch($action) {
case 'post':
@ -172,6 +173,28 @@ case 'delete':
exit();
break;
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);
} else {
$url = site_url('?page_id=' . $id . '&preview=true');
}
wp_redirect($url);
exit();
break;
default:
wp_redirect('edit-pages.php');
exit();

View File

@ -80,6 +80,8 @@ function redirect_post($post_ID = '') {
if ( isset( $_POST['deletepost'] ) )
$action = 'delete';
elseif ( isset($_POST['wp-preview']) && 'dopreview' == $_POST['wp-preview'] )
$action = 'preview';
switch($action) {
case 'postajaxpost':
@ -218,6 +220,28 @@ case 'delete':
exit();
break;
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' ) {
$nonce = wp_create_nonce('post_preview_' . $id);
$url = site_url('?wp_preview=' . $id . '&preview_nonce=' . $nonce);
} else {
$url = site_url('?p=' . $id . '&preview=true');
}
wp_redirect($url);
exit();
break;
default:
wp_redirect('edit.php');
exit();

View File

@ -197,5 +197,6 @@ add_action('template_redirect', 'wp_old_slug_redirect');
add_action('edit_post', 'wp_check_for_changed_slugs');
add_action('edit_form_advanced', 'wp_remember_old_slug');
add_action('init', 'wp_user_settings', 9);
add_action('init', '_show_post_preview');
?>

View File

@ -3551,3 +3551,25 @@ function wp_get_post_revisions( $post_id = 0, $args = null ) {
return array();
return $revisions;
}
function _show_post_preview() {
if ( isset($_GET['wp_preview']) && isset($_GET['preview_nonce']) ) {
$post_ID = (int) $_GET['wp_preview'];
if ( false == wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $post_ID ) )
wp_die( __('You do not have permission to preview drafts.') );
$q = array(
'name' => "{$post_ID}-autosave",
'post_parent' => $post_ID,
'post_type' => 'revision',
'post_status' => 'inherit'
);
add_action( 'parse_query', '_wp_get_post_autosave_hack' );
query_posts($q);
remove_action( 'parse_query', '_wp_get_post_autosave_hack' );
}
}

View File

@ -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'), '20081018' );
$scripts->add( 'post', '/wp-admin/js/post.js', array('suggest', 'jquery-ui-tabs', 'wp-lists', 'postbox', 'slug', 'settings-box'), '20081031' );
$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'), '20080925' );
$scripts->add( 'page', '/wp-admin/js/page.js', array('jquery', 'slug', 'postbox', 'settings-box'), '20081031' );
$scripts->localize( 'page', 'postL10n', array(
'cancel' => __('Cancel'),
'edit' => __('Edit'),