Make WordPress Core


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/link-template.php

    r15412 r17228  
    2828 * @uses $wp_rewrite
    2929 *
    30  * @param $string String a URL with or without a trailing slash.
    31  * @param $type_of_url String the type of URL being considered (e.g. single, category, etc) for use in the filter.
     30 * @param string $string URL with or without a trailing slash.
     31 * @param string $type_of_url The type of URL being considered (e.g. single, category, etc) for use in the filter.
    3232 * @return string
    3333 */
     
    4040
    4141    // Note that $type_of_url can be one of following:
    42     // single, single_trackback, single_feed, single_paged, feed, category, page, year, month, day, paged
     42    // single, single_trackback, single_feed, single_paged, feed, category, page, year, month, day, paged, post_type_archive
    4343    $string = apply_filters('user_trailingslashit', $string, $type_of_url);
    4444    return $string;
     
    186186    $slug = $post->post_name;
    187187
    188     $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
     188    $draft_or_pending = isset($post->post_status) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
    189189
    190190    $post_type = get_post_type_object($post->post_type);
    191191
    192     if ( !empty($post_link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) {
     192    if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) {
    193193        if ( ! $leavename ) {
    194194            if ( $post_type->hierarchical )
     
    272272        $post = &get_post($id);
    273273
     274    $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
     275
    274276    $link = $wp_rewrite->get_page_permastruct();
    275277
    276     if ( '' != $link && ( ( isset($post->post_status) && 'draft' != $post->post_status && 'pending' != $post->post_status ) || $sample ) ) {
    277         if ( ! $leavename )
     278    if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) {
     279        if ( ! $leavename ) {
    278280            $link = str_replace('%pagename%', get_page_uri($id), $link);
     281        }
     282
    279283        $link = home_url($link);
    280284        $link = user_trailingslashit($link, 'page');
     
    462466 * @return string
    463467 */
    464 function get_post_comments_feed_link($post_id = '', $feed = '') {
    465     global $id;
    466 
    467     if ( empty($post_id) )
    468         $post_id = (int) $id;
    469 
    470     if ( empty($feed) )
     468function get_post_comments_feed_link($post_id = 0, $feed = '') {
     469    $post_id = absint( $post_id );
     470
     471    if ( ! $post_id )
     472        $post_id = get_the_ID();
     473
     474    if ( empty( $feed ) )
    471475        $feed = get_default_feed();
    472476
     
    650654 *
    651655 * @param int $tag_id Tag ID
    652  * @return string
    653  */
    654 function get_edit_tag_link( $tag_id = 0, $taxonomy = 'post_tag' ) {
    655     global $post_type;
    656     $tax = get_taxonomy($taxonomy);
    657     if ( !current_user_can($tax->cap->edit_terms) )
    658         return;
    659 
    660     $tag = get_term($tag_id, $taxonomy);
    661 
    662     $location = admin_url('edit-tags.php?action=edit&taxonomy=' . $taxonomy . '&' . (!empty($post_type) ? 'post_type=' . $post_type .'&' : '') .'tag_ID=' . $tag->term_id);
    663     return apply_filters( 'get_edit_tag_link', $location );
     656 * @param string $taxonomy Taxonomy
     657 * @return string
     658 */
     659function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) {
     660    return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) );
    664661}
    665662
     
    673670 * @param string $after Optional. Display after edit link.
    674671 * @param int|object $tag Tag object or ID
    675  * @return string|null HTML content, if $echo is set to false.
     672 * @return string HTML content.
    676673 */
    677674function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
    678     $tax = get_taxonomy('post_tag');
     675    $link = edit_term_link( $link, '', '', false, $tag );
     676    echo $before . apply_filters( 'edit_tag_link', $link ) . $after;
     677}
     678
     679/**
     680 * Retrieve edit term url.
     681 *
     682 * @since 3.1.0
     683 *
     684 * @param int $term_id Term ID
     685 * @param string $taxonomy Taxonomy
     686 * @param string $object_type The object type
     687 * @return string
     688 */
     689function get_edit_term_link( $term_id, $taxonomy, $object_type = '' ) {
     690    $tax = get_taxonomy( $taxonomy );
     691    if ( !current_user_can( $tax->cap->edit_terms ) )
     692        return;
     693
     694    $term = get_term( $term_id, $taxonomy );
     695
     696    $args = array(
     697        'action' => 'edit',
     698        'taxonomy' => $taxonomy,
     699        'tag_ID' => $term->term_id,
     700    );
     701
     702    if ( $object_type )
     703        $args['post_type'] = $object_type;
     704
     705    $location = add_query_arg( $args, admin_url( 'edit-tags.php' ) );
     706
     707    return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type );
     708}
     709
     710/**
     711 * Display or retrieve edit term link with formatting.
     712 *
     713 * @since 3.1.0
     714 *
     715 * @param string $link Optional. Anchor text.
     716 * @param string $before Optional. Display before edit link.
     717 * @param string $after Optional. Display after edit link.
     718 * @param object $term Term object
     719 * @return string HTML content.
     720 */
     721function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) {
     722    if ( is_null( $term ) ) {
     723        $term = get_queried_object();
     724    }
     725
     726    $tax = get_taxonomy( $term->taxonomy );
    679727    if ( !current_user_can($tax->cap->edit_terms) )
    680728        return;
    681729
    682     $tag = get_term($tag, 'post_tag');
    683 
    684     if ( empty($link) )
     730    if ( empty( $link ) )
    685731        $link = __('Edit This');
    686732
    687     $link = '<a href="' . get_edit_tag_link( $tag->term_id ) . '" title="' . __( 'Edit Tag' ) . '">' . $link . '</a>';
    688     echo $before . apply_filters( 'edit_tag_link', $link, $tag->term_id ) . $after;
     733    $link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '" title="' . $link . '">' . $link . '</a>';
     734    $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after;
     735
     736    if ( $echo )
     737        echo $link;
     738    else
     739        return $link;
    689740}
    690741
     
    778829
    779830/**
     831 * Retrieve the permalink for a post type archive.
     832 *
     833 * @since 3.1.0
     834 *
     835 * @param string $post_type Post type
     836 * @return string
     837 */
     838function get_post_type_archive_link( $post_type ) {
     839    global $wp_rewrite;
     840    if ( ! $post_type_obj = get_post_type_object( $post_type ) )
     841        return false;
     842
     843    if ( ! $post_type_obj->has_archive )
     844        return false;
     845
     846    if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
     847        $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
     848        if ( $post_type_obj->rewrite['with_front'] )
     849            $struct = $wp_rewrite->front . $struct;
     850        $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
     851    } else {
     852        $link = home_url( '?post_type=' . $post_type );
     853    }
     854
     855    return apply_filters( 'post_type_archive_link', $link, $post_type );
     856}
     857
     858/**
     859 * Retrieve the permalink for a post type archive feed.
     860 *
     861 * @since 3.1.0
     862 *
     863 * @param string $post_type Post type
     864 * @param string $feed Optional. Feed type
     865 * @return string
     866 */
     867function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
     868    $default_feed = get_default_feed();
     869    if ( empty( $feed ) )
     870        $feed = $default_feed;
     871
     872    if ( ! $link = get_post_type_archive_link( $post_type ) )
     873        return false;
     874    $post_type_obj = get_post_type_object( $post_type );
     875    if ( $post_type_obj->rewrite['feeds'] && get_option( 'permalink_structure' ) ) {
     876        $link = trailingslashit($link);
     877        $link .= 'feed/';
     878        if ( $feed != $default_feed )
     879            $link .= "$feed/";
     880    } else {
     881        $link = add_query_arg( 'feed', $feed, $link );
     882    }
     883
     884    return apply_filters( 'post_type_archive_feed_link', $link, $feed );
     885}
     886
     887/**
    780888 * Retrieve edit posts link for post.
    781889 *
     
    847955function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
    848956    if ( ! empty( $deprecated ) )
    849         _deprecated_argument( __FUNCTION__, '3.0.0' );
     957        _deprecated_argument( __FUNCTION__, '3.0' );
    850958
    851959    if ( !$post = &get_post( $id ) )
     
    876984function get_edit_comment_link( $comment_id = 0 ) {
    877985    $comment = &get_comment( $comment_id );
    878     $post = &get_post( $comment->comment_post_ID );
    879 
    880     if ( $post->post_type == 'page' ) {
    881         if ( !current_user_can( 'edit_page', $post->ID ) )
    882             return;
    883     } else {
    884         if ( !current_user_can( 'edit_post', $post->ID ) )
    885             return;
    886     }
     986
     987    if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
     988        return;
    887989
    888990    $location = admin_url('comment.php?action=editcomment&amp;c=') . $comment->comment_ID;
     
    9011003 */
    9021004function edit_comment_link( $link = null, $before = '', $after = '' ) {
    903     global $comment, $post;
    904 
    905     if ( $post->post_type == 'page' ) {
    906         if ( !current_user_can( 'edit_page', $post->ID ) )
    907             return;
    908     } else {
    909         if ( !current_user_can( 'edit_post', $post->ID ) )
    910             return;
    911     }
     1005    global $comment;
     1006
     1007    if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
     1008        return;
    9121009
    9131010    if ( null === $link )
    9141011        $link = __('Edit This');
    9151012
    916     $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '" title="' . __( 'Edit comment' ) . '">' . $link . '</a>';
     1013    $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '" title="' . esc_attr__( 'Edit comment' ) . '">' . $link . '</a>';
    9171014    echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID ) . $after;
    9181015}
     
    9551052        $link = __('Edit This');
    9561053
    957     $link = '<a href="' . get_edit_bookmark_link( $link ) . '" title="' . __( 'Edit Link' ) . '">' . $link . '</a>';
     1054    $link = '<a href="' . get_edit_bookmark_link( $bookmark ) . '" title="' . esc_attr__( 'Edit Link' ) . '">' . $link . '</a>';
    9581055    echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
    9591056}
     
    9621059
    9631060/**
    964  * Retrieve previous post link that is adjacent to current post.
     1061 * Retrieve previous post that is adjacent to current post.
    9651062 *
    9661063 * @since 1.5.0
    9671064 *
    968  * @param bool $in_same_cat Optional. Whether link should be in same category.
     1065 * @param bool $in_same_cat Optional. Whether post should be in same category.
    9691066 * @param string $excluded_categories Optional. Excluded categories IDs.
    970  * @return string
     1067 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
    9711068 */
    9721069function get_previous_post($in_same_cat = false, $excluded_categories = '') {
     
    9751072
    9761073/**
    977  * Retrieve next post link that is adjacent to current post.
     1074 * Retrieve next post that is adjacent to current post.
    9781075 *
    9791076 * @since 1.5.0
    9801077 *
    981  * @param bool $in_same_cat Optional. Whether link should be in same category.
     1078 * @param bool $in_same_cat Optional. Whether post should be in same category.
    9821079 * @param string $excluded_categories Optional. Excluded categories IDs.
    983  * @return string
     1080 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
    9841081 */
    9851082function get_next_post($in_same_cat = false, $excluded_categories = '') {
     
    9881085
    9891086/**
    990  * Retrieve adjacent post link.
    991  *
    992  * Can either be next or previous post link.
     1087 * Retrieve adjacent post.
     1088 *
     1089 * Can either be next or previous post.
    9931090 *
    9941091 * @since 2.5.0
    9951092 *
    996  * @param bool $in_same_cat Optional. Whether link should be in same category.
     1093 * @param bool $in_same_cat Optional. Whether post should be in same category.
    9971094 * @param string $excluded_categories Optional. Excluded categories IDs.
    9981095 * @param bool $previous Optional. Whether to retrieve previous post.
    999  * @return string
     1096 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
    10001097 */
    10011098function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $previous = true) {
     
    11481245 * Retrieve boundary post.
    11491246 *
    1150  * Boundary being either the first or last post by publish date within the contraitns specified
     1247 * Boundary being either the first or last post by publish date within the constraints specified
    11511248 * by in same category or excluded categories.
    11521249 *
     
    11551252 * @param bool $in_same_cat Optional. Whether returned post should be in same category.
    11561253 * @param string $excluded_categories Optional. Excluded categories IDs.
    1157  * @param bool $previous Optional. Whether to retrieve first post.
     1254 * @param bool $start Optional. Whether to retrieve first or last post.
    11581255 * @return object
    11591256 */
     
    11881285    $order = $start ? 'ASC' : 'DESC';
    11891286
    1190     return get_posts( array('numberposts' => 1, 'no_found_rows' => true, 'order' => $order, 'orderby' => 'ID', 'category' => $categories) );
     1287    return get_posts( array('numberposts' => 1, 'category' => $categories, 'order' => $order, 'update_post_term_cache' => false, 'update_post_meta_cache' => false) );
    11911288}
    11921289
     
    12011298 * @param bool $in_same_cat Optional. Whether link should be in same category.
    12021299 * @param string $excluded_categories Optional. Excluded categories IDs.
    1203  * @param bool $start Optional, default is true. Whether display link to first post.
     1300 * @param bool $start Optional, default is true. Whether display link to first or last post.
    12041301 * @return string
    12051302 */
    12061303function get_boundary_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $start = true) {
    1207     $posts = get_boundary_post($in_same_cat,$excluded_categories,$start);
     1304    $posts = get_boundary_post($in_same_cat, $excluded_categories, $start);
    12081305    // If there is no post stop.
    12091306    if ( empty($posts) )
     
    14121509        }
    14131510
    1414         $request = preg_replace( '|page/\d+/?$|', '', $request);
     1511        $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request);
    14151512        $request = preg_replace( '|^index\.php|', '', $request);
    14161513        $request = ltrim($request, '/');
     
    14221519
    14231520        if ( $pagenum > 1 ) {
    1424             $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( 'page/' . $pagenum, 'paged' );
     1521            $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
    14251522        }
    14261523
     
    14921589    $nextpage = intval($paged) + 1;
    14931590
    1494     if ( !is_single() && ( empty($paged) || $nextpage <= $max_page) ) {
     1591    if ( !is_single() && ( $nextpage <= $max_page ) ) {
    14951592        $attr = apply_filters( 'next_posts_link_attributes', '' );
    14961593        return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label) . '</a>';
     
    18601957
    18611958    if ( empty( $blog_id ) || !is_multisite() )
    1862         $home = get_option( 'home' );
     1959        $url = get_option( 'home' );
    18631960    else
    1864         $home = get_blog_option( $blog_id, 'home' );
    1865 
    1866     $url = str_replace( 'http://', "$scheme://", $home );
     1961        $url = get_blog_option( $blog_id, 'home' );
     1962
     1963    if ( 'http' != $scheme )
     1964        $url = str_replace( 'http://', "$scheme://", $url );
    18671965
    18681966    if ( !empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false )
     
    19262024        $url = get_blog_option( $blog_id, 'siteurl' );
    19272025
    1928     $url = str_replace( 'http://', "{$scheme}://", $url );
     2026    if ( 'http' != $scheme )
     2027        $url = str_replace( 'http://', "{$scheme}://", $url );
    19292028
    19302029    if ( !empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false )
     
    20772176    }
    20782177
    2079     $url = 'http://' . $current_site->domain . $current_site->path;
    2080 
    2081     $url = str_replace( 'http://', "{$scheme}://", $url );
     2178    $url = $scheme . '://' . $current_site->domain . $current_site->path;
    20822179
    20832180    if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
     
    21122209        $scheme = is_ssl() && !is_admin() ? 'https' : 'http';
    21132210
    2114     $url = 'http://' . $current_site->domain . $current_site->path;
    2115 
    2116     $url = str_replace( 'http://', "$scheme://", $url );
     2211    $url = $scheme . '://' . $current_site->domain . $current_site->path;
    21172212
    21182213    if ( !empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false )
     
    21332228*/
    21342229function network_admin_url( $path = '', $scheme = 'admin' ) {
    2135     $url = network_site_url('wp-admin/', $scheme);
     2230    if ( ! is_multisite() )
     2231        return admin_url( $path, $scheme );
     2232
     2233    $url = network_site_url('wp-admin/network/', $scheme);
    21362234
    21372235    if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
     
    21392237
    21402238    return apply_filters('network_admin_url', $url, $path);
     2239}
     2240
     2241/**
     2242 * Retrieve the url to the admin area for the current user.
     2243 *
     2244 * @package WordPress
     2245 * @since 3.0.0
     2246 *
     2247 * @param string $path Optional path relative to the admin url
     2248 * @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.
     2249 * @return string Admin url link with optional path appended
     2250*/
     2251function user_admin_url( $path = '', $scheme = 'admin' ) {
     2252    $url = network_site_url('wp-admin/user/', $scheme);
     2253
     2254    if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
     2255        $url .= ltrim($path, '/');
     2256
     2257    return apply_filters('user_admin_url', $url, $path);
     2258}
     2259
     2260/**
     2261 * Retrieve the url to the admin area for either the current blog or the network depending on context.
     2262 *
     2263 * @package WordPress
     2264 * @since 3.1.0
     2265 *
     2266 * @param string $path Optional path relative to the admin url
     2267 * @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.
     2268 * @return string Admin url link with optional path appended
     2269*/
     2270function self_admin_url($path = '', $scheme = 'admin') {
     2271    if ( is_network_admin() )
     2272        return network_admin_url($path, $scheme);
     2273    elseif ( is_user_admin() )
     2274        return user_admin_url($path, $scheme);
     2275    else
     2276        return admin_url($path, $scheme);
     2277}
     2278
     2279/**
     2280 * Get the URL to the user's dashboard.
     2281 *
     2282 * If a user does not belong to any sites, the global user dashboard is used.  If the user belongs to the current site,
     2283 * the dashboard for the current site is returned.  If the user cannot edit the current site, the dashboard to the user's
     2284 * primary blog is returned.
     2285 *
     2286 * @since 3.1.0
     2287 *
     2288 * @param int $user_id User ID
     2289 * @param string $path Optional path relative to the dashboard.  Use only paths known to both blog and user admins.
     2290 * @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.
     2291 * @return string Dashboard url link with optional path appended
     2292 */
     2293function get_dashboard_url( $user_id, $path = '', $scheme = 'admin' ) {
     2294    $user_id = (int) $user_id;
     2295
     2296    $blogs = get_blogs_of_user( $user_id );
     2297    if ( empty($blogs) ) {
     2298        $url = user_admin_url( $path, $scheme );
     2299    } elseif ( ! is_multisite() ) {
     2300        $url = admin_url( $path, $scheme );
     2301    } else {
     2302        $current_blog = get_current_blog_id();
     2303        if ( $current_blog  && in_array($current_blog, array_keys($blogs)) ) {
     2304            $url = admin_url( $path, $scheme );
     2305        } else {
     2306            $active = get_active_blog_for_user( $user_id );
     2307            if ( $active )
     2308                $url = get_admin_url( $active->blog_id, $path, $scheme );
     2309            else
     2310                $url = user_admin_url( $path, $scheme );
     2311        }
     2312    }
     2313
     2314    return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme);
     2315}
     2316
     2317/**
     2318 * Get the URL to the user's profile editor.
     2319 *
     2320 * @since 3.1.0
     2321 *
     2322 * @param int $user User ID
     2323 * @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.
     2324 * @return string Dashboard url link with optional path appended
     2325 */
     2326function get_edit_profile_url( $user, $scheme = 'admin' ) {
     2327    $user = (int) $user;
     2328
     2329    if ( is_user_admin() )
     2330        $url = user_admin_url( 'profile.php', $scheme );
     2331    elseif ( is_network_admin() )
     2332        $url = network_admin_url( 'profile.php', $scheme );
     2333    else
     2334        $url = get_dashboard_url( $user, 'profile.php', $scheme );
     2335
     2336    return apply_filters( 'edit_profile_url', $url, $user, $scheme);
    21412337}
    21422338
     
    21702366 *
    21712367 * @param int $id A post or blog id.  Default is 0, which means the current post or blog.
    2172  * @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'.
     2368 * @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'.
    21732369 * @param bool $allow_slugs Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.
    21742370 * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.
     
    22292425 */
    22302426function wp_shortlink_header() {
    2231     if ( headers_sent() )
     2427    if ( headers_sent() )
    22322428        return;
    22332429
Note: See TracChangeset for help on using the changeset viewer.