+/**
+ * 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. See set_url_scheme().
+ * @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);
+
+ if ( 'relative' == $scheme )
+ $url = $current_site->path;
+ else
+ $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
+
+ if ( ! empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false )
+ $url .= ltrim( $path, '/' );
+
+ return apply_filters( 'network_site_url', $url, $path, $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', or 'relative'.
+ * @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', 'relative' ) ) )
+ $scheme = is_ssl() && ! is_admin() ? 'https' : 'http';
+
+ if ( 'relative' == $scheme )
+ $url = $current_site->path;
+ else
+ $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
+
+ 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' ) {
+ if ( ! is_multisite() )
+ return admin_url( $path, $scheme );
+
+ $url = network_site_url('wp-admin/network/', $scheme);
+
+ if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
+ $url .= ltrim($path, '/');
+
+ return apply_filters('network_admin_url', $url, $path);
+}
+
+/**
+ * Retrieve the url to the admin area for the current user.
+ *
+ * @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 user_admin_url( $path = '', $scheme = 'admin' ) {
+ $url = network_site_url('wp-admin/user/', $scheme);
+
+ if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
+ $url .= ltrim($path, '/');
+
+ return apply_filters('user_admin_url', $url, $path);
+}
+
+/**
+ * Retrieve the url to the admin area for either the current blog or the network depending on context.
+ *
+ * @package WordPress
+ * @since 3.1.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 self_admin_url($path = '', $scheme = 'admin') {
+ if ( is_network_admin() )
+ return network_admin_url($path, $scheme);
+ elseif ( is_user_admin() )
+ return user_admin_url($path, $scheme);
+ else
+ return admin_url($path, $scheme);
+}
+
+/**
+ * Set the scheme for a URL
+ *
+ * @since 3.4.0
+ *
+ * @param string $url Absolute url that includes a scheme
+ * @param string $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'.
+ * @return string $url URL with chosen scheme.
+ */
+function set_url_scheme( $url, $scheme = null ) {
+ $orig_scheme = $scheme;
+ if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
+ 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' );
+ }
+
+ if ( 'relative' == $scheme )
+ $url = preg_replace( '#^.+://[^/]*#', '', $url );
+ else
+ $url = preg_replace( '#^.+://#', $scheme . '://', $url );
+
+ return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
+}
+
+/**
+ * Get the URL to the user's dashboard.
+ *
+ * If a user does not belong to any site, the global user dashboard is used. If the user belongs to the current site,
+ * the dashboard for the current site is returned. If the user cannot edit the current site, the dashboard to the user's
+ * primary blog is returned.
+ *
+ * @since 3.1.0
+ *
+ * @param int $user_id User ID
+ * @param string $path Optional path relative to the dashboard. Use only paths known to both blog and user admins.
+ * @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 Dashboard url link with optional path appended.
+ */
+function get_dashboard_url( $user_id, $path = '', $scheme = 'admin' ) {
+ $user_id = (int) $user_id;
+
+ $blogs = get_blogs_of_user( $user_id );
+ if ( ! is_super_admin() && empty($blogs) ) {
+ $url = user_admin_url( $path, $scheme );
+ } elseif ( ! is_multisite() ) {
+ $url = admin_url( $path, $scheme );
+ } else {
+ $current_blog = get_current_blog_id();
+ if ( $current_blog && ( is_super_admin( $user_id ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) {
+ $url = admin_url( $path, $scheme );
+ } else {
+ $active = get_active_blog_for_user( $user_id );
+ if ( $active )
+ $url = get_admin_url( $active->blog_id, $path, $scheme );
+ else
+ $url = user_admin_url( $path, $scheme );
+ }
+ }
+
+ return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme);
+}
+
+/**
+ * Get the URL to the user's profile editor.
+ *
+ * @since 3.1.0
+ *
+ * @param int $user User ID
+ * @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 Dashboard url link with optional path appended.
+ */
+function get_edit_profile_url( $user, $scheme = 'admin' ) {
+ $user = (int) $user;
+
+ if ( is_user_admin() )
+ $url = user_admin_url( 'profile.php', $scheme );
+ elseif ( is_network_admin() )
+ $url = network_admin_url( 'profile.php', $scheme );
+ else
+ $url = get_dashboard_url( $user, 'profile.php', $scheme );
+
+ return apply_filters( 'edit_profile_url', $url, $user, $scheme);
+}
+
+/**
+ * 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 );
+
+ if ( $page = get_query_var('cpage') )
+ $link = get_comments_pagenum_link( $page );
+
+ 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 $context 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=shortlink 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( $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 $after Optional HTML to display after the link.
+ */
+function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
+ $post = get_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;
+ }
+}