+ $post->filter = 'sample';
+
+ $permalink = get_permalink($post, true);
+
+ // Replace custom post_type Token with generic pagename token for ease of use.
+ $permalink = str_replace("%$post->post_type%", '%pagename%', $permalink);
+
+ // Handle page hierarchy
+ if ( $ptype->hierarchical ) {
+ $uri = get_page_uri($post);
+ $uri = untrailingslashit($uri);
+ $uri = strrev( stristr( strrev( $uri ), '/' ) );
+ $uri = untrailingslashit($uri);
+ $uri = apply_filters( 'editable_slug', $uri );
+ if ( !empty($uri) )
+ $uri .= '/';
+ $permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink);
+ }
+
+ $permalink = array($permalink, apply_filters('editable_slug', $post->post_name));
+ $post->post_status = $original_status;
+ $post->post_date = $original_date;
+ $post->post_name = $original_name;
+ unset($post->filter);
+
+ return $permalink;
+}
+
+/**
+ * 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 ) {
+ $post = get_post( $id );
+ if ( ! $post )
+ return '';
+
+ 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";