wp_get_shortlink() and pluggable shortlink generation. fixes #10640

git-svn-id: http://svn.automattic.com/wordpress/trunk@13635 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2010-03-09 19:36:38 +00:00
parent c42b0e50ea
commit fcbca58853
3 changed files with 85 additions and 0 deletions

View File

@ -192,6 +192,10 @@ $side_meta_boxes = do_meta_boxes($post_type, 'side', $post);
<div class="inside">
<?php
$sample_permalink_html = get_sample_permalink_html($post->ID);
$shortlink = wp_get_shortlink($post->ID, 'post');
if ( !empty($shortlink) )
$sample_permalink_html .= '<input id="shortlink" type="hidden" value="' . esc_attr($shortlink) . '" /><a href="#" class="button" onclick="prompt(&#39;URL:&#39;, jQuery(\'#shortlink\').val()); return false;">' . __('Get Shortlink') . '</a>';
if ( !( 'pending' == $post->post_status && !current_user_can( $post_type_object->publish_cap ) ) ) { ?>
<div id="edit-slug-box">
<?php

View File

@ -191,6 +191,8 @@ add_action( 'wp_head', 'wp_print_head_scripts', 9 );
add_action( 'wp_head', 'wp_generator' );
add_action( 'wp_head', 'rel_canonical' );
add_action( 'wp_footer', 'wp_print_footer_scripts' );
add_action( 'wp_head', 'wp_shortlink_wp_head' );
add_action( 'wp', 'wp_shortlink_header' );
// Feed Generator Tags
foreach ( array( 'rss2_head', 'commentsrss2_head', 'rss_head', 'rdf_header', 'atom_head', 'comments_atom_head', 'opml_head', 'app_head' ) as $action ) {

View File

@ -2050,4 +2050,83 @@ function wp_ajaxurl() {
add_action('wp_head', '_wp_ajaxurl', 1);
}
/**
* 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 && is_single() )
$post_id = $wp_query->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->post_type) && 'post' == $post->post_type )
$shortlink = home_url('?p=' . $post->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 '<link rel="shortlink" href="' . $shortlink . '" />';
}
/**
* 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: <' . $shortlink . '>; rel=shortlink');
}
?>