From 7c063b0b500e06dba7c0816926957b7d88fefc05 Mon Sep 17 00:00:00 2001 From: azaozz Date: Tue, 4 Nov 2008 13:00:12 +0000 Subject: [PATCH] Preview fixes: different method of showing previews for published posts, now uses the proper templates. Fixes #8052 git-svn-id: http://svn.automattic.com/wordpress/trunk@9509 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/post.php | 14 ++++++++++++-- wp-admin/page.php | 12 +----------- wp-admin/post.php | 12 +----------- wp-includes/post.php | 37 +++++++++++++++++++++++-------------- wp-includes/query.php | 3 +++ 5 files changed, 40 insertions(+), 38 deletions(-) diff --git a/wp-admin/includes/post.php b/wp-admin/includes/post.php index 5c410ea097..e6cc1a6e7e 100644 --- a/wp-admin/includes/post.php +++ b/wp-admin/includes/post.php @@ -998,7 +998,7 @@ function wp_create_post_autosave( $post_id ) { * @uses current_user_can() * @uses wp_create_post_autosave() * - * @return int|object the saved post id or wp_error object + * @return str URL to redirect to show the preview */ function post_preview() { @@ -1034,7 +1034,17 @@ function post_preview() { $id = $post->ID; } - return $id; + if ( is_wp_error($id) ) + wp_die( $id->get_error_message() ); + + if ( $_POST['post_status'] == 'draft' ) { + $url = add_query_arg( 'preview', 'true', get_permalink($id) ); + } else { + $nonce = wp_create_nonce('post_preview_' . $id); + $url = add_query_arg( array( 'preview' => 'true', 'preview_id' => $id, 'preview_nonce' => $nonce ), get_permalink($id) ); + } + + return $url; } /** diff --git a/wp-admin/page.php b/wp-admin/page.php index 4dc16c4ec8..7455d52ff5 100644 --- a/wp-admin/page.php +++ b/wp-admin/page.php @@ -176,17 +176,7 @@ case 'delete': case 'preview': check_admin_referer( 'autosave', 'autosavenonce' ); - $id = post_preview(); - - if ( is_wp_error($id) ) - wp_die( $id->get_error_message() ); - - if ( $_POST['post_status'] == 'draft' ) { - $url = get_option('home') . '/?page_id=' . $id . '&preview=true'; - } else { - $nonce = wp_create_nonce('post_preview_' . $id); - $url = get_option('home') . '/?wp_preview=' . $id . '&preview_nonce=' . $nonce; - } + $url = post_preview(); wp_redirect($url); exit(); diff --git a/wp-admin/post.php b/wp-admin/post.php index bfcdba867a..fd1c4973c1 100644 --- a/wp-admin/post.php +++ b/wp-admin/post.php @@ -223,17 +223,7 @@ case 'delete': case 'preview': check_admin_referer( 'autosave', 'autosavenonce' ); - $id = post_preview(); - - if ( is_wp_error($id) ) - wp_die( $id->get_error_message() ); - - 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; - } + $url = post_preview(); wp_redirect($url); exit(); diff --git a/wp-includes/post.php b/wp-includes/post.php index eab3d9f7b4..33e6a891ea 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -3552,24 +3552,33 @@ function wp_get_post_revisions( $post_id = 0, $args = null ) { return $revisions; } +function _set_preview($post) { + + if ( ! is_object($post) ) + return $post; + + $preview = wp_get_post_autosave($post->ID); + + if ( ! is_object($preview) ) + return $post; + + $preview = sanitize_post($preview); + + $post->post_content = $preview->post_content; + $post->post_title = $preview->post_title; + $post->post_excerpt = $preview->post_excerpt; + + return $post; +} + function _show_post_preview() { - if ( isset($_GET['wp_preview']) && isset($_GET['preview_nonce']) ) { - $post_ID = (int) $_GET['wp_preview']; + if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) { + $id = (int) $_GET['preview_id']; - if ( false == wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $post_ID ) || ! current_user_can('edit_post', $post_ID) ) + if ( false == wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $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' ); - + add_filter('the_preview', '_set_preview'); } } diff --git a/wp-includes/query.php b/wp-includes/query.php index 6699997e94..7202996ad9 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -2294,6 +2294,9 @@ class WP_Query { } } } + + if ( $this->is_preview && current_user_can( "edit_{$post_type}", $this->posts[0]->ID ) ) + $this->posts[0] = apply_filters('the_preview', $this->posts[0]); } // Put sticky posts at the top of the posts array