Make WordPress Core

Changeset 13635


Ignore:
Timestamp:
03/09/2010 07:36:38 PM (15 years ago)
Author:
ryan
Message:

wp_get_shortlink() and pluggable shortlink generation. fixes #10640

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/edit-form-advanced.php

    r13217 r13635  
    193193<?php
    194194$sample_permalink_html = get_sample_permalink_html($post->ID);
     195$shortlink = wp_get_shortlink($post->ID, 'post');
     196if ( !empty($shortlink) )
     197    $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>';
     198
    195199if ( !( 'pending' == $post->post_status && !current_user_can( $post_type_object->publish_cap ) ) ) { ?>
    196200    <div id="edit-slug-box">
  • trunk/wp-includes/default-filters.php

    r13487 r13635  
    192192add_action( 'wp_head',             'rel_canonical'                        );
    193193add_action( 'wp_footer',           'wp_print_footer_scripts'              );
     194add_action( 'wp_head',             'wp_shortlink_wp_head'                 );
     195add_action( 'wp',                  'wp_shortlink_header'                  );
    194196
    195197// Feed Generator Tags
  • trunk/wp-includes/link-template.php

    r13557 r13635  
    20512051}
    20522052
     2053/**
     2054 * Return a shortlink for a post, page, attachment, or blog.
     2055 *
     2056 * Shortlinks are not supported by default. A plugin is required to get shortlink support.
     2057 * This function exists to provide a shortlink tag that all themes and plugins can target.  A plugin must hook in to
     2058 * provide the actual shortlinks.  Plugins can short circuit this function via the pre_get_shortlink filter or filter the output
     2059 * via the get_shortlink filter.
     2060 *
     2061 * @since 3.0.0.
     2062 *
     2063 * @param int $id A post or blog id.  Default is 0, which means the current post or blog.
     2064 * @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'.
     2065 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.
     2066 * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.
     2067 */
     2068function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {
     2069    // Allow plugins to short-circuit this function.
     2070    $shortlink = apply_filters('pre_get_shortlink', false, $id, $context, $allow_slugs);
     2071    if ( false !== $shortlink )
     2072        return $shortlink;
     2073
     2074    global $wp_query;
     2075    $post_id = 0;
     2076    if ( 'query' == $context && is_single() )
     2077        $post_id = $wp_query->get_queried_object_id();
     2078    elseif ( 'post' == $context )
     2079        $post_id = $id;
     2080
     2081    $shortlink = '';
     2082
     2083    // Return p= link for posts.
     2084    if ( !empty($post_id) ) {
     2085        $post = get_post($post_id);
     2086        if ( isset($post->post_type) && 'post' == $post->post_type )
     2087            $shortlink = home_url('?p=' . $post->ID);
     2088    }
     2089
     2090    return apply_filters('get_shortlink', $shortlink, $id, $context, $allow_slugs);
     2091}
     2092
     2093/**
     2094 *  Inject rel=sortlink into head if a shortlink is defined for the current page.
     2095 *
     2096 *  Attached to the wp_head action.
     2097 *
     2098 * @since 3.0.0
     2099 *
     2100 * @uses wp_get_shortlink()
     2101 */
     2102function wp_shortlink_wp_head() {
     2103    $shortlink = wp_get_shortlink(0, 'query');
     2104
     2105    if ( empty($shortlink) )
     2106        return;
     2107
     2108    echo '<link rel="shortlink" href="' . $shortlink . '" />';
     2109}
     2110
     2111/**
     2112 * Send a Link: rel=shortlink header if a shortlink is defined for the current page.
     2113 *
     2114 * Attached to the wp action.
     2115 *
     2116 * @since 3.0.0
     2117 *
     2118 * @uses wp_get_shortlink()
     2119 */
     2120function wp_shortlink_header() {
     2121    if ( headers_sent() )
     2122        return;
     2123
     2124    $shortlink = wp_get_shortlink(0, 'query');
     2125
     2126    if ( empty($shortlink) )
     2127        return;
     2128
     2129    header('Link: <' . $shortlink . '>; rel=shortlink');
     2130}
     2131
    20532132?>
Note: See TracChangeset for help on using the changeset viewer.