+
+
+ post_type )->public ) {
+ $preview_link = set_url_scheme( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) );
+
+ if ( 'publish' == $post->post_status || $user->ID != $post->post_author ) {
+ // Latest content is in autosave
+ $nonce = wp_create_nonce( 'post_preview_' . $post->ID );
+ $preview_link = add_query_arg( array( 'preview_id' => $post->ID, 'preview_nonce' => $nonce ), $preview_link );
+ }
+ } else {
+ $preview_link = '';
+ }
- if ( !add_post_meta( $post->ID, '_edit_lock', $now, true ) )
- update_post_meta( $post->ID, '_edit_lock', $now );
- if ( !add_post_meta( $post->ID, '_edit_last', $current_user->ID, true ) )
- update_post_meta( $post->ID, '_edit_last', $current_user->ID );
+ $preview_link = apply_filters( 'preview_post_link', $preview_link );
+ $override = apply_filters( 'override_post_lock', true, $post, $user );
+ $tab_last = $override ? '' : ' wp-tab-last';
+
+ ?>
+
+
ID, 64 ); ?>
+
+ display_name ) );
+ ?>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ ID;
+ $new_autosave['post_author'] = $post_author;
+
+ // If the new autosave is the same content as the post, delete the old autosave.
+ $post = get_post( $post_id );
+ $autosave_is_different = false;
+ foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
+ if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) {
+ $autosave_is_different = true;
+ break;
+ }
+ }
+
+ if ( ! $autosave_is_different ) {
+ wp_delete_post_revision( $old_autosave->ID );
+ return;
+ }
+
+ return wp_update_post( $new_autosave );
+ }
+
+ // _wp_put_post_revision() expects unescaped.
+ $post_data = wp_unslash( $_POST );
+
+ // Otherwise create the new autosave as a special post revision
+ return _wp_put_post_revision( $post_data, true );
}
-?>
+/**
+ * Save draft or manually autosave for showing preview.
+ *
+ * @package WordPress
+ * @since 2.7.0
+ *
+ * @uses get_post_status()
+ * @uses edit_post()
+ * @uses get_post()
+ * @uses current_user_can()
+ * @uses wp_die()
+ * @uses wp_create_post_autosave()
+ * @uses add_query_arg()
+ * @uses wp_create_nonce()
+ *
+ * @return str URL to redirect to show the preview
+ */
+function post_preview() {
+
+ $post_ID = (int) $_POST['post_ID'];
+ $status = get_post_status( $post_ID );
+ if ( 'auto-draft' == $status )
+ wp_die( __('Preview not available. Please save as a 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']);
+
+ $_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 {
+ if ( ! current_user_can('edit_post', $post_ID) )
+ wp_die( __('You are not allowed to edit this post.') );
+ }
+
+ $user_id = get_current_user_id();
+ $locked = wp_check_post_lock( $post->ID );
+ if ( ! $locked && 'draft' == $post->post_status && $user_id == $post->post_author ) {
+ $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;
+ }
+
+ if ( is_wp_error($id) )
+ wp_die( $id->get_error_message() );
+
+ if ( ! $locked && $_POST['post_status'] == 'draft' && $user_id == $post->post_author ) {
+ $url = add_query_arg( 'preview', 'true', get_permalink($id) );
+ } else {
+ $nonce = wp_create_nonce('post_preview_' . $id);
+ $args = array(
+ 'preview' => 'true',
+ 'preview_id' => $id,
+ 'preview_nonce' => $nonce,
+ );
+
+ if ( isset( $_POST['post_format'] ) )
+ $args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
+
+ $url = add_query_arg( $args, get_permalink($id) );
+ }
+
+ return apply_filters( 'preview_post_link', $url );
+}