<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[13635] trunk: wp_get_shortlink() and pluggable shortlink generation.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/13635">13635</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2010-03-09 19:36:38 +0000 (Tue, 09 Mar 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>wp_get_shortlink() and pluggable shortlink generation. fixes <a href="http://trac.wordpress.org/ticket/10640">#10640</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadmineditformadvancedphp">trunk/wp-admin/edit-form-advanced.php</a></li>
<li><a href="#trunkwpincludesdefaultfiltersphp">trunk/wp-includes/default-filters.php</a></li>
<li><a href="#trunkwpincludeslinktemplatephp">trunk/wp-includes/link-template.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadmineditformadvancedphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/edit-form-advanced.php (13634 => 13635)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/edit-form-advanced.php        2010-03-09 19:10:07 UTC (rev 13634)
+++ trunk/wp-admin/edit-form-advanced.php        2010-03-09 19:36:38 UTC (rev 13635)
</span><span class="lines">@@ -192,6 +192,10 @@
</span><span class="cx"> &lt;div class=&quot;inside&quot;&gt;
</span><span class="cx"> &lt;?php
</span><span class="cx"> $sample_permalink_html = get_sample_permalink_html($post-&gt;ID);
</span><ins>+$shortlink = wp_get_shortlink($post-&gt;ID, 'post');
+if ( !empty($shortlink) )
+    $sample_permalink_html .= '&lt;input id=&quot;shortlink&quot; type=&quot;hidden&quot; value=&quot;' . esc_attr($shortlink) . '&quot; /&gt;&lt;a href=&quot;#&quot; class=&quot;button&quot; onclick=&quot;prompt(&amp;#39;URL:&amp;#39;, jQuery(\'#shortlink\').val()); return false;&quot;&gt;' . __('Get Shortlink') . '&lt;/a&gt;';
+
</ins><span class="cx"> if ( !( 'pending' == $post-&gt;post_status &amp;&amp; !current_user_can( $post_type_object-&gt;publish_cap ) ) ) { ?&gt;
</span><span class="cx">         &lt;div id=&quot;edit-slug-box&quot;&gt;
</span><span class="cx"> &lt;?php
</span></span></pre></div>
<a id="trunkwpincludesdefaultfiltersphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/default-filters.php (13634 => 13635)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/default-filters.php        2010-03-09 19:10:07 UTC (rev 13634)
+++ trunk/wp-includes/default-filters.php        2010-03-09 19:36:38 UTC (rev 13635)
</span><span class="lines">@@ -191,6 +191,8 @@
</span><span class="cx"> add_action( 'wp_head',             'wp_generator'                         );
</span><span class="cx"> add_action( 'wp_head',             'rel_canonical'                        );
</span><span class="cx"> add_action( 'wp_footer',           'wp_print_footer_scripts'              );
</span><ins>+add_action( 'wp_head',             'wp_shortlink_wp_head'                 );
+add_action( 'wp',                  'wp_shortlink_header'                  );
</ins><span class="cx"> 
</span><span class="cx"> // Feed Generator Tags
</span><span class="cx"> foreach ( array( 'rss2_head', 'commentsrss2_head', 'rss_head', 'rdf_header', 'atom_head', 'comments_atom_head', 'opml_head', 'app_head' ) as $action ) {
</span></span></pre></div>
<a id="trunkwpincludeslinktemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/link-template.php (13634 => 13635)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/link-template.php        2010-03-09 19:10:07 UTC (rev 13634)
+++ trunk/wp-includes/link-template.php        2010-03-09 19:36:38 UTC (rev 13635)
</span><span class="lines">@@ -2050,4 +2050,83 @@
</span><span class="cx">         add_action('wp_head', '_wp_ajaxurl', 1);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+/**
+ * Return a shortlink for a post, page, attachment, or blog.
+ *
+ * Shortlinks are not supported by default. A plugin is required to get shortlink support.
+ * This function exists to provide a shortlink tag that all themes and plugins can target.  A plugin must hook in to
+ * provide the actual shortlinks.  Plugins can short circuit this function via the pre_get_shortlink filter or filter the output
+ * via the get_shortlink filter.
+ *
+ * @since 3.0.0.
+ * 
+ * @param int $id A post or blog id.  Default is 0, which means the current post or blog.
+ * @param string $contex Whether the id is a 'blog' id, 'post' id, or 'media' id.  If 'post', the post_type of the post is consulted.  If 'query', the current query is consulted to determine the id and context. Default is 'post'.
+ * @param bool $allow_slugs Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.
+ * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.
+ */
+function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {
+        // Allow plugins to short-circuit this function.
+        $shortlink = apply_filters('pre_get_shortlink', false, $id, $context, $allow_slugs);
+        if ( false !== $shortlink )
+                return $shortlink;
+
+        global $wp_query;
+        $post_id = 0;
+        if ( 'query' == $context &amp;&amp; is_single() )
+                $post_id = $wp_query-&gt;get_queried_object_id();
+        elseif ( 'post' == $context )
+                $post_id = $id;
+
+        $shortlink = '';
+
+        // Return p= link for posts.
+        if ( !empty($post_id) ) {
+                $post = get_post($post_id);
+                if ( isset($post-&gt;post_type) &amp;&amp; 'post' == $post-&gt;post_type )
+                        $shortlink = home_url('?p=' . $post-&gt;ID);
+        }
+
+        return apply_filters('get_shortlink', $shortlink, $id, $context, $allow_slugs);
+}
+
+/**
+ *  Inject rel=sortlink into head if a shortlink is defined for the current page.
+ *
+ *  Attached to the wp_head action.
+ *
+ * @since 3.0.0
+ *
+ * @uses wp_get_shortlink()
+ */
+function wp_shortlink_wp_head() {
+        $shortlink = wp_get_shortlink(0, 'query');
+
+        if ( empty($shortlink) )
+                return;
+
+        echo '&lt;link rel=&quot;shortlink&quot; href=&quot;' . $shortlink . '&quot; /&gt;';
+}
+
+/**
+ * Send a Link: rel=shortlink header if a shortlink is defined for the current page.
+ *
+ * Attached to the wp action.
+ *
+ * @since 3.0.0
+ *
+ * @uses wp_get_shortlink()
+ */
+function wp_shortlink_header() {
+    if ( headers_sent() )
+                return;
+
+        $shortlink = wp_get_shortlink(0, 'query');
+
+        if ( empty($shortlink) )
+                return;
+
+        header('Link: &lt;' . $shortlink . '&gt;; rel=shortlink');
+}
+
</ins><span class="cx"> ?&gt;
</span></span></pre>
</div>
</div>

</body>
</html>