+ /**
+ * Filters the URL to the plugins directory.
+ *
+ * @since 2.8.0
+ *
+ * @param string $url The complete URL to the plugins directory including scheme and path.
+ * @param string $path Path relative to the URL to the plugins directory. Blank string
+ * if no path is specified.
+ * @param string $plugin The plugin file path to be relative to. Blank string if no plugin
+ * is specified.
+ */
+ return apply_filters( 'plugins_url', $url, $path, $plugin );
+}
+
+/**
+ * Retrieves 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.
+ *
+ * @since 3.0.0
+ *
+ * @see set_url_scheme()
+ *
+ * @param string $path Optional. Path relative to the site URL. Default empty.
+ * @param string $scheme Optional. Scheme to give the site URL context. Accepts
+ * 'http', 'https', or 'relative'. Default null.
+ * @return string Site URL link with optional path appended.
+ */
+function network_site_url( $path = '', $scheme = null ) {
+ if ( ! is_multisite() )
+ return site_url($path, $scheme);
+
+ $current_site = get_current_site();
+
+ if ( 'relative' == $scheme )
+ $url = $current_site->path;
+ else
+ $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
+
+ if ( $path && is_string( $path ) )
+ $url .= ltrim( $path, '/' );
+
+ /**
+ * Filters the network site URL.
+ *
+ * @since 3.0.0
+ *
+ * @param string $url The complete network site URL including scheme and path.
+ * @param string $path Path relative to the network site URL. Blank string if
+ * no path is specified.
+ * @param string|null $scheme Scheme to give the URL context. Accepts 'http', 'https',
+ * 'relative' or null.
+ */
+ return apply_filters( 'network_site_url', $url, $path, $scheme );
+}
+
+/**
+ * Retrieves the home URL for the current network.
+ *
+ * Returns the home URL with the appropriate protocol, 'https' is_ssl()
+ * and 'http' otherwise. If `$scheme` is 'http' or 'https', `is_ssl()` is
+ * overridden.
+ *
+ * @since 3.0.0
+ *
+ * @param string $path Optional. Path relative to the home URL. Default empty.
+ * @param string $scheme Optional. Scheme to give the home URL context. Accepts
+ * 'http', 'https', or 'relative'. Default null.
+ * @return string Home URL link with optional path appended.
+ */
+function network_home_url( $path = '', $scheme = null ) {
+ if ( ! is_multisite() )
+ return home_url($path, $scheme);
+
+ $current_site = get_current_site();
+ $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 ( $path && is_string( $path ) )
+ $url .= ltrim( $path, '/' );
+
+ /**
+ * Filters the network home URL.
+ *
+ * @since 3.0.0
+ *
+ * @param string $url The complete network home URL including scheme and path.
+ * @param string $path Path relative to the network home URL. Blank string
+ * if no path is specified.
+ * @param string|null $orig_scheme Scheme to give the URL context. Accepts 'http', 'https',
+ * 'relative' or null.
+ */
+ return apply_filters( 'network_home_url', $url, $path, $orig_scheme);
+}
+
+/**
+ * Retrieves the URL to the admin area for the network.
+ *
+ * @since 3.0.0
+ *
+ * @param string $path Optional path relative to the admin URL. Default empty.
+ * @param string $scheme Optional. 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 ( $path && is_string( $path ) )
+ $url .= ltrim($path, '/');
+
+ /**
+ * Filters the network admin URL.
+ *
+ * @since 3.0.0
+ *
+ * @param string $url The complete network admin URL including scheme and path.
+ * @param string $path Path relative to the network admin URL. Blank string if
+ * no path is specified.
+ */
+ return apply_filters( 'network_admin_url', $url, $path );
+}
+
+/**
+ * Retrieves the URL to the admin area for the current user.
+ *
+ * @since 3.0.0
+ *
+ * @param string $path Optional. Path relative to the admin URL. Default empty.
+ * @param string $scheme Optional. 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 ( $path && is_string( $path ) )
+ $url .= ltrim($path, '/');
+
+ /**
+ * Filters the user admin URL for the current user.
+ *
+ * @since 3.1.0
+ *
+ * @param string $url The complete URL including scheme and path.
+ * @param string $path Path relative to the URL. Blank string if
+ * no path is specified.
+ */
+ return apply_filters( 'user_admin_url', $url, $path );
+}
+
+/**
+ * Retrieves the URL to the admin area for either the current site or the network depending on context.
+ *
+ * @since 3.1.0
+ *
+ * @param string $path Optional. Path relative to the admin URL. Default empty.
+ * @param string $scheme Optional. 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);
+}
+
+/**
+ * Sets the scheme for a URL.
+ *
+ * @since 3.4.0
+ * @since 4.4.0 The 'rest' scheme was added.
+ *
+ * @param string $url Absolute URL that includes a scheme
+ * @param string|null $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login',
+ * 'login_post', 'admin', 'relative', 'rest', 'rpc', or null. Default null.
+ * @return string $url URL with chosen scheme.
+ */
+function set_url_scheme( $url, $scheme = null ) {
+ $orig_scheme = $scheme;
+
+ if ( ! $scheme ) {
+ $scheme = is_ssl() ? 'https' : 'http';
+ } elseif ( $scheme === 'admin' || $scheme === 'login' || $scheme === 'login_post' || $scheme === 'rpc' ) {
+ $scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http';
+ } elseif ( $scheme !== 'http' && $scheme !== 'https' && $scheme !== 'relative' ) {
+ $scheme = is_ssl() ? 'https' : 'http';
+ }
+
+ $url = trim( $url );
+ if ( substr( $url, 0, 2 ) === '//' )
+ $url = 'http:' . $url;
+
+ if ( 'relative' == $scheme ) {
+ $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) );
+ if ( $url !== '' && $url[0] === '/' )
+ $url = '/' . ltrim($url , "/ \t\n\r\0\x0B" );
+ } else {
+ $url = preg_replace( '#^\w+://#', $scheme . '://', $url );
+ }
+
+ /**
+ * Filters the resulting URL after setting the scheme.
+ *
+ * @since 3.4.0
+ *
+ * @param string $url The complete URL including scheme and path.
+ * @param string $scheme Scheme applied to the URL. One of 'http', 'https', or 'relative'.
+ * @param string|null $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',
+ * 'login_post', 'admin', 'relative', 'rest', 'rpc', or null.
+ */
+ return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
+}
+
+/**
+ * Retrieves 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 site is returned.
+ *
+ * @since 3.1.0
+ *
+ * @param int $user_id Optional. User ID. Defaults to current user.
+ * @param string $path Optional path relative to the dashboard. Use only paths known to
+ * both site and user admins. Default empty.
+ * @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 = 0, $path = '', $scheme = 'admin' ) {
+ $user_id = $user_id ? (int) $user_id : get_current_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 );
+ }
+ }
+
+ /**
+ * Filters the dashboard URL for a user.
+ *
+ * @since 3.1.0
+ *
+ * @param string $url The complete URL including scheme and path.
+ * @param int $user_id The user ID.
+ * @param string $path Path relative to the URL. Blank string if no path is specified.
+ * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
+ * 'login_post', 'admin', 'relative' or null.
+ */
+ return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme);
+}
+
+/**
+ * Retrieves the URL to the user's profile editor.
+ *
+ * @since 3.1.0
+ *
+ * @param int $user_id Optional. User ID. Defaults to current user.
+ * @param string $scheme Optional. 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_id = 0, $scheme = 'admin' ) {
+ $user_id = $user_id ? (int) $user_id : get_current_user_id();
+
+ 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_id, 'profile.php', $scheme );
+
+ /**
+ * Filters the URL for a user's profile editor.
+ *
+ * @since 3.1.0
+ *
+ * @param string $url The complete URL including scheme and path.
+ * @param int $user_id The user ID.
+ * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
+ * 'login_post', 'admin', 'relative' or null.
+ */
+ return apply_filters( 'edit_profile_url', $url, $user_id, $scheme);
+}
+
+/**
+ * Returns the canonical URL for a post.
+ *
+ * When the post is the same as the current requested page the function will handle the
+ * pagination arguments too.
+ *
+ * @since 4.6.0
+ *
+ * @param int|WP_Post $post Optional. Post ID or object. Default is global `$post`.
+ * @return string|false The canonical URL, or false if the post does not exist or has not
+ * been published yet.
+ */
+function wp_get_canonical_url( $post = null ) {
+ $post = get_post( $post );
+
+ if ( ! $post ) {
+ return false;
+ }
+
+ if ( 'publish' !== $post->post_status ) {
+ return false;
+ }
+
+ $canonical_url = get_permalink( $post );
+
+ // If a canonical is being generated for the current page, make sure it has pagination if needed.
+ if ( $post->ID === get_queried_object_id() ) {
+ $page = get_query_var( 'page', 0 );
+ if ( $page >= 2 ) {
+ if ( '' == get_option( 'permalink_structure' ) ) {
+ $canonical_url = add_query_arg( 'page', $page, $canonical_url );
+ } else {
+ $canonical_url = trailingslashit( $canonical_url ) . user_trailingslashit( $page, 'single_paged' );
+ }
+ }
+
+ $cpage = get_query_var( 'cpage', 0 );
+ if ( $cpage ) {
+ $canonical_url = get_comments_pagenum_link( $cpage );
+ }
+ }
+
+ /**
+ * Filters the canonical URL for a post.
+ *
+ * @since 4.6.0
+ *
+ * @param string $string The post's canonical URL.
+ * @param WP_Post $post Post object.
+ */
+ return apply_filters( 'get_canonical_url', $canonical_url, $post );
+}
+
+/**
+ * Outputs rel=canonical for singular queries.
+ *
+ * @since 2.9.0
+ * @since 4.6.0 Adjusted to use wp_get_canonical_url().
+ */
+function rel_canonical() {
+ if ( ! is_singular() ) {
+ return;
+ }
+
+ $id = get_queried_object_id();
+
+ if ( 0 === $id ) {
+ return;
+ }
+
+ $url = wp_get_canonical_url( $id );
+
+ if ( ! empty( $url ) ) {
+ echo '<link rel="canonical" href="' . esc_url( $url ) . '" />' . "\n";
+ }
+}
+
+/**
+ * Returns a shortlink for a post, page, attachment, or site.
+ *
+ * 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 {@see 'pre_get_shortlink'} filter or filter the output via the {@see 'get_shortlink'}
+ * filter.
+ *
+ * @since 3.0.0.
+ *
+ * @param int $id Optional. A post or site id. Default is 0, which means the current post or site.
+ * @param string $context Optional. Whether the id is a 'site' 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 'post'.
+ * @param bool $allow_slugs Optional. Whether to allow post slugs in the shortlink. It is up to the plugin how
+ * and whether to honor this. Default true.
+ * @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 ) {
+ /**
+ * Filters whether to preempt generating a shortlink for the given post.
+ *
+ * Passing a truthy value to the filter will effectively short-circuit the
+ * shortlink-generation process, returning that value instead.
+ *
+ * @since 3.0.0
+ *
+ * @param bool|string $return Short-circuit return value. Either false or a URL string.
+ * @param int $id Post ID, or 0 for the current post.
+ * @param string $context The context for the link. One of 'post' or 'query',
+ * @param bool $allow_slugs Whether to allow post slugs in the shortlink.
+ */
+ $shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs );
+
+ if ( false !== $shortlink ) {
+ return $shortlink;
+ }
+
+ $post_id = 0;
+ if ( 'query' == $context && is_singular() ) {
+ $post_id = get_queried_object_id();
+ $post = get_post( $post_id );
+ } elseif ( 'post' == $context ) {
+ $post = get_post( $id );
+ if ( ! empty( $post->ID ) )
+ $post_id = $post->ID;
+ }
+
+ $shortlink = '';
+
+ // Return p= link for all public post types.
+ if ( ! empty( $post_id ) ) {
+ $post_type = get_post_type_object( $post->post_type );
+
+ if ( 'page' === $post->post_type && $post->ID == get_option( 'page_on_front' ) && 'page' == get_option( 'show_on_front' ) ) {
+ $shortlink = home_url( '/' );
+ } elseif ( $post_type->public ) {
+ $shortlink = home_url( '?p=' . $post_id );
+ }
+ }
+
+ /**
+ * Filters the shortlink for a post.
+ *
+ * @since 3.0.0
+ *
+ * @param string $shortlink Shortlink URL.
+ * @param int $id Post ID, or 0 for the current post.
+ * @param string $context The context for the link. One of 'post' or 'query',
+ * @param bool $allow_slugs Whether to allow post slugs in the shortlink. Not used by default.
+ */
+ return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs );
+}
+
+/**
+ * Injects rel=shortlink into the head if a shortlink is defined for the current page.
+ *
+ * Attached to the {@see 'wp_head'} action.
+ *
+ * @since 3.0.0
+ */
+function wp_shortlink_wp_head() {
+ $shortlink = wp_get_shortlink( 0, 'query' );
+
+ if ( empty( $shortlink ) )
+ return;
+
+ echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n";
+}
+
+/**
+ * Sends a Link: rel=shortlink header if a shortlink is defined for the current page.
+ *
+ * Attached to the {@see 'wp'} action.
+ *
+ * @since 3.0.0
+ */
+function wp_shortlink_header() {
+ if ( headers_sent() )
+ return;
+
+ $shortlink = wp_get_shortlink(0, 'query');
+
+ if ( empty($shortlink) )
+ return;
+
+ header('Link: <' . $shortlink . '>; rel=shortlink', false);
+}
+
+/**
+ * Displays the shortlink 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. Default empty.
+ * @param string $after Optional HTML to display after the link. Default empty.
+ */
+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>';
+
+ /**
+ * Filters the short link anchor tag for a post.
+ *
+ * @since 3.0.0
+ *
+ * @param string $link Shortlink anchor tag.
+ * @param string $shortlink Shortlink URL.
+ * @param string $text Shortlink's text.
+ * @param string $title Shortlink's title attribute.
+ */
+ $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
+ echo $before, $link, $after;
+ }