+/**
+ * Returns the HTML of the sample permalink slug editor.
+ *
+ * @since 2.5.0
+ *
+ * @param int|object $id Post ID or post object.
+ * @param string $new_title Optional. New title.
+ * @param string $new_slug Optional. New slug.
+ * @return string The HTML of the sample permalink slug editor.
+ */
+function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) {
+ global $wpdb;
+ $post = get_post($id);
+
+ list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
+
+ if ( 'publish' == get_post_status( $post ) ) {
+ $ptype = get_post_type_object($post->post_type);
+ $view_post = $ptype->labels->view_item;
+ $title = __('Click to edit this part of the permalink');
+ } else {
+ $title = __('Temporary permalink. Click to edit this part.');
+ }
+
+ if ( false === strpos($permalink, '%postname%') && false === strpos($permalink, '%pagename%') ) {
+ $return = '<strong>' . __('Permalink:') . "</strong>\n" . '<span id="sample-permalink" tabindex="-1">' . $permalink . "</span>\n";
+ if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) )
+ $return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __('Change Permalinks') . "</a></span>\n";
+ if ( isset( $view_post ) )
+ $return .= "<span id='view-post-btn'><a href='$permalink' class='button button-small'>$view_post</a></span>\n";
+
+ $return = apply_filters('get_sample_permalink_html', $return, $id, $new_title, $new_slug);
+
+ return $return;
+ }
+
+ if ( function_exists('mb_strlen') ) {
+ if ( mb_strlen($post_name) > 30 ) {
+ $post_name_abridged = mb_substr($post_name, 0, 14). '…' . mb_substr($post_name, -14);
+ } else {
+ $post_name_abridged = $post_name;
+ }
+ } else {
+ if ( strlen($post_name) > 30 ) {
+ $post_name_abridged = substr($post_name, 0, 14). '…' . substr($post_name, -14);
+ } else {
+ $post_name_abridged = $post_name;
+ }
+ }
+
+ $post_name_html = '<span id="editable-post-name" title="' . $title . '">' . $post_name_abridged . '</span>';
+ $display_link = str_replace(array('%pagename%','%postname%'), $post_name_html, $permalink);
+ $view_link = str_replace(array('%pagename%','%postname%'), $post_name, $permalink);
+ $return = '<strong>' . __('Permalink:') . "</strong>\n";
+ $return .= '<span id="sample-permalink" tabindex="-1">' . $display_link . "</span>\n";
+ $return .= '‎'; // Fix bi-directional text display defect in RTL languages.
+ $return .= '<span id="edit-slug-buttons"><a href="#post_name" class="edit-slug button button-small hide-if-no-js" onclick="editPermalink(' . $id . '); return false;">' . __('Edit') . "</a></span>\n";
+ $return .= '<span id="editable-post-name-full">' . $post_name . "</span>\n";
+ if ( isset($view_post) )
+ $return .= "<span id='view-post-btn'><a href='$view_link' class='button button-small'>$view_post</a></span>\n";
+
+ $return = apply_filters('get_sample_permalink_html', $return, $id, $new_title, $new_slug);
+
+ return $return;
+}
+
+/**
+ * Output HTML for the post thumbnail meta-box.
+ *
+ * @since 2.9.0
+ *
+ * @param int $thumbnail_id ID of the attachment used for thumbnail
+ * @param mixed $post The post ID or object associated with the thumbnail, defaults to global $post.
+ * @return string html
+ */
+function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {
+ global $content_width, $_wp_additional_image_sizes;
+
+ $post = get_post( $post );
+
+ $upload_iframe_src = esc_url( get_upload_iframe_src('image', $post->ID ) );
+ $set_thumbnail_link = '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set featured image' ) . '" href="%s" id="set-post-thumbnail" class="thickbox">%s</a></p>';
+ $content = sprintf( $set_thumbnail_link, $upload_iframe_src, esc_html__( 'Set featured image' ) );
+
+ if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
+ $old_content_width = $content_width;
+ $content_width = 266;
+ if ( !isset( $_wp_additional_image_sizes['post-thumbnail'] ) )
+ $thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) );
+ else
+ $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'post-thumbnail' );
+ if ( !empty( $thumbnail_html ) ) {
+ $ajax_nonce = wp_create_nonce( 'set_post_thumbnail-' . $post->ID );
+ $content = sprintf( $set_thumbnail_link, $upload_iframe_src, $thumbnail_html );
+ $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(\'' . $ajax_nonce . '\');return false;">' . esc_html__( 'Remove featured image' ) . '</a></p>';
+ }
+ $content_width = $old_content_width;
+ }
+
+ return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID );
+}
+
+/**
+ * Check to see if the post is currently being edited by another user.
+ *
+ * @since 2.5.0
+ *
+ * @param int $post_id ID of the post to check for editing
+ * @return bool|int False: not locked or locked by current user. Int: user ID of user with lock.
+ */
+function wp_check_post_lock( $post_id ) {
+ if ( !$post = get_post( $post_id ) )
+ return false;
+
+ if ( !$lock = get_post_meta( $post->ID, '_edit_lock', true ) )
+ return false;
+
+ $lock = explode( ':', $lock );
+ $time = $lock[0];
+ $user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );
+
+ $time_window = apply_filters( 'wp_check_post_lock_window', AUTOSAVE_INTERVAL * 2 );
+
+ if ( $time && $time > time() - $time_window && $user != get_current_user_id() )
+ return $user;
+ return false;
+}
+
+/**
+ * Mark the post as currently being edited by the current user
+ *
+ * @since 2.5.0
+ *
+ * @param int $post_id ID of the post to being edited
+ * @return bool|array Returns false if the post doesn't exist of there is no current user, or
+ * an array of the lock time and the user ID.
+ */
+function wp_set_post_lock( $post_id ) {
+ if ( !$post = get_post( $post_id ) )
+ return false;
+ if ( 0 == ($user_id = get_current_user_id()) )
+ return false;
+
+ $now = time();
+ $lock = "$now:$user_id";
+
+ update_post_meta( $post->ID, '_edit_lock', $lock );
+ return array( $now, $user_id );
+}
+
+/**
+ * Outputs the notice message to say that someone else is editing this post at the moment.
+ *
+ * @since 2.8.5
+ * @return none
+ */
+function _admin_notice_post_locked() {
+ $post = get_post();
+ $lock = explode( ':', get_post_meta( $post->ID, '_edit_lock', true ) );
+ $user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );
+ $last_user = get_userdata( $user );
+ $last_user_name = $last_user ? $last_user->display_name : __('Somebody');
+
+ switch ($post->post_type) {
+ case 'post':
+ $message = __( 'Warning: %s is currently editing this post' );
+ break;
+ case 'page':
+ $message = __( 'Warning: %s is currently editing this page' );
+ break;
+ default:
+ $message = __( 'Warning: %s is currently editing this.' );
+ }
+
+ $message = sprintf( $message, esc_html( $last_user_name ) );
+ echo "<div class='error'><p>$message</p></div>";
+}
+
+/**
+ * Creates autosave data for the specified post from $_POST data.
+ *
+ * @package WordPress
+ * @subpackage Post_Revisions
+ * @since 2.6.0
+ *
+ * @uses _wp_translate_postdata()
+ * @uses _wp_post_revision_fields()
+ *
+ * @return unknown
+ */
+function wp_create_post_autosave( $post_id ) {
+ $translated = _wp_translate_postdata( true );
+ if ( is_wp_error( $translated ) )
+ return $translated;
+
+ // Only store one autosave. If there is already an autosave, overwrite it.
+ if ( $old_autosave = wp_get_post_autosave( $post_id ) ) {
+ $new_autosave = _wp_post_revision_fields( $_POST, true );
+ $new_autosave['ID'] = $old_autosave->ID;
+ $new_autosave['post_author'] = get_current_user_id();
+ return wp_update_post( $new_autosave );
+ }
+
+ // _wp_put_post_revision() expects unescaped.
+ $_POST = stripslashes_deep($_POST);
+
+ // Otherwise create the new autosave as a special post revision
+ return _wp_put_post_revision( $_POST, 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.'));
+ }
+
+ 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;
+ }
+
+ 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;
+}