+/**
+ * Retrieve the site url for the current network.
+ *
+ * Returns the site url with the appropriate protocol, 'https' if
+ * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
+ * overridden.
+ *
+ * @package WordPress
+ * @since 3.0.0
+ *
+ * @param string $path Optional. Path relative to the site url.
+ * @param string $scheme Optional. Scheme to give the site url context. Currently 'http','https', 'login', 'login_post', or 'admin'.
+ * @return string Site url link with optional path appended.
+*/
+function network_site_url( $path = '', $scheme = null ) {
+ global $current_site;
+
+ if ( !is_multisite() )
+ return site_url($path, $scheme);
+
+ $orig_scheme = $scheme;
+ if ( !in_array($scheme, array('http', 'https')) ) {
+ if ( ( 'login_post' == $scheme || 'rpc' == $scheme ) && ( force_ssl_login() || force_ssl_admin() ) )
+ $scheme = 'https';
+ elseif ( ('login' == $scheme) && ( force_ssl_admin() ) )
+ $scheme = 'https';
+ elseif ( ('admin' == $scheme) && force_ssl_admin() )
+ $scheme = 'https';
+ else
+ $scheme = ( is_ssl() ? 'https' : 'http' );
+ }
+
+ $url = 'http://' . $current_site->domain . $current_site->path;
+
+ $url = str_replace( 'http://', "{$scheme}://", $url );
+
+ if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
+ $url .= ltrim($path, '/');
+
+ return apply_filters('network_site_url', $url, $path, $orig_scheme);
+}
+
+/**
+ * Retrieve the home url for the current network.
+ *
+ * Returns the home url with the appropriate protocol, 'https' if
+ * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
+ * overridden.
+ *
+ * @package WordPress
+ * @since 3.0.0
+ *
+ * @param string $path (optional) Path relative to the home url.
+ * @param string $scheme (optional) Scheme to give the home url context. Currently 'http','https'
+ * @return string Home url link with optional path appended.
+*/
+function network_home_url( $path = '', $scheme = null ) {
+ global $current_site;
+
+ if ( !is_multisite() )
+ return home_url($path, $scheme);
+
+ $orig_scheme = $scheme;
+
+ if ( !in_array($scheme, array('http', 'https')) )
+ $scheme = is_ssl() && !is_admin() ? 'https' : 'http';
+
+ $url = 'http://' . $current_site->domain . $current_site->path;
+
+ $url = str_replace( 'http://', "$scheme://", $url );
+
+ if ( !empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false )
+ $url .= ltrim( $path, '/' );
+
+ return apply_filters( 'network_home_url', $url, $path, $orig_scheme);
+}
+
+/**
+ * Retrieve the url to the admin area for the network.
+ *
+ * @package WordPress
+ * @since 3.0.0
+ *
+ * @param string $path Optional path relative to the admin url
+ * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.
+ * @return string Admin url link with optional path appended
+*/
+function network_admin_url( $path = '', $scheme = 'admin' ) {
+ $url = network_site_url('wp-admin/', $scheme);
+
+ if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
+ $url .= ltrim($path, '/');
+
+ return apply_filters('network_admin_url', $url, $path);
+}
+
+/**
+ * Output rel=canonical for singular queries
+ *
+ * @package WordPress
+ * @since 2.9.0
+*/
+function rel_canonical() {
+ if ( !is_singular() )
+ return;
+
+ global $wp_the_query;
+ if ( !$id = $wp_the_query->get_queried_object_id() )
+ return;
+
+ $link = get_permalink( $id );
+ echo "<link rel='canonical' href='$link' />\n";
+}
+
+/**
+ * Return a shortlink for a post, page, attachment, or blog.
+ *
+ * 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. Default shortlink support is limited to providing ?p= style links for posts.
+ * 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 = get_post($id);
+ $post_id = $post->ID;
+ }
+
+ $shortlink = '';
+
+ // Return p= link for posts.
+ if ( !empty($post_id) && '' != get_option('permalink_structure') ) {
+ $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='" . esc_url_raw( $shortlink ) . "' />\n";
+}
+
+/**
+ * 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', false);
+}
+
+/**
+ * Display the Short Link for a Post
+ *
+ * Must be called from inside "The Loop"
+ *
+ * Call like the_shortlink(__('Shortlinkage FTW'))
+ *
+ * @since 3.0.0
+ *
+ * @param string $text Optional The link text or HTML to be displayed. Defaults to 'This is the short link.'
+ * @param string $title Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.
+ * @param string $before Optional HTML to display before the link.
+ * @param string $before Optional HTML to display after the link.
+ */
+function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
+ global $post;
+
+ if ( empty( $text ) )
+ $text = __('This is the short link.');
+
+ if ( empty( $title ) )
+ $title = the_title_attribute( array( 'echo' => FALSE ) );
+
+ $shortlink = wp_get_shortlink( $post->ID );
+
+ if ( !empty( $shortlink ) ) {
+ $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';
+ $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
+ echo $before, $link, $after;
+ }
+}
+