Make WordPress Core


Ignore:
File:
1 edited

Legend:

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

    r17443 r15220  
    1111 *
    1212 * @since 1.0.0
    13  * @see get_term_link()
    14  *
    15  * @param int|object $category Category ID or object.
    16  * @return string Link on success, empty string if category does not exist.
    17  */
    18 function get_category_link( $category ) {
    19     if ( ! is_object( $category ) )
    20         $category = (int) $category;
    21 
    22     $category = get_term_link( $category, 'category' );
    23 
    24     if ( is_wp_error( $category ) )
    25         return '';
    26 
    27     return $category;
     13 * @uses apply_filters() Calls 'category_link' filter on category link and category ID.
     14 *
     15 * @param int $category_id Category ID.
     16 * @return string
     17 */
     18function get_category_link( $category_id ) {
     19    global $wp_rewrite;
     20    $catlink = $wp_rewrite->get_category_permastruct();
     21
     22    if ( empty( $catlink ) ) {
     23        $catlink = home_url('?cat=' . $category_id);
     24    } else {
     25        $category = &get_category( $category_id );
     26        if ( is_wp_error( $category ) )
     27            return $category;
     28        $category_nicename = $category->slug;
     29
     30        if ( $category->parent == $category_id ) // recursive recursion
     31            $category->parent = 0;
     32        elseif ($category->parent != 0 )
     33            $category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename;
     34
     35        $catlink = str_replace( '%category%', $category_nicename, $catlink );
     36        $catlink = home_url( user_trailingslashit( $catlink, 'category' ) );
     37    }
     38    return apply_filters( 'category_link', $catlink, $category_id );
    2839}
    2940
     
    4960        $name = $parent->slug;
    5061    else
    51         $name = $parent->name;
     62        $name = $parent->cat_name;
    5263
    5364    if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
     
    5768
    5869    if ( $link )
    59         $chain .= '<a href="' . get_category_link( $parent->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
     70        $chain .= '<a href="' . get_category_link( $parent->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->cat_name ) ) . '">'.$name.'</a>' . $separator;
    6071    else
    6172        $chain .= $name.$separator;
     
    7384 */
    7485function get_the_category( $id = false ) {
    75     $categories = get_the_terms( $id, 'category' );
    76     if ( ! $categories )
     86    global $post;
     87
     88    $id = (int) $id;
     89    if ( !$id )
     90        $id = (int) $post->ID;
     91
     92    $categories = get_object_term_cache( $id, 'category' );
     93    if ( false === $categories ) {
     94        $categories = wp_get_object_terms( $id, 'category' );
     95        wp_cache_add($id, $categories, 'category_relationships');
     96    }
     97
     98    if ( !empty( $categories ) )
     99        usort( $categories, '_usort_terms_by_name' );
     100    else
    77101        $categories = array();
    78102
    79     $categories = array_values( $categories );
    80 
    81     foreach ( array_keys( $categories ) as $key ) {
     103    foreach ( (array) array_keys( $categories ) as $key ) {
    82104        _make_cat_compat( $categories[$key] );
    83105    }
    84106
    85     // Filter name is plural because we return alot of categories not just one
    86     return apply_filters( 'get_the_categories', $categories );
     107    return $categories;
    87108}
    88109
     
    182203                case '':
    183204                default:
    184                     $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
     205                    $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->cat_name.'</a></li>';
    185206            }
    186207        }
     
    195216                    if ( $category->parent )
    196217                        $thelist .= get_category_parents( $category->parent, true, $separator );
    197                     $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a>';
     218                    $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->cat_name.'</a>';
    198219                    break;
    199220                case 'single':
     
    201222                    if ( $category->parent )
    202223                        $thelist .= get_category_parents( $category->parent, false, $separator );
    203                     $thelist .= "$category->name</a>";
     224                    $thelist .= "$category->cat_name</a>";
    204225                    break;
    205226                case '':
     
    228249 * @since 1.2.0
    229250 *
    230  * @param int|string|array $category Category ID, name or slug, or array of said.
    231  * @param int|object $_post Optional. Post to check instead of the current post. (since 2.7.0)
     251 * @uses is_object_in_term()
     252 *
     253 * @param int|string|array $category. Category ID, name or slug, or array of said.
     254 * @param int|post object Optional.  Post to check instead of the current post. @since 2.7.0
    232255 * @return bool True if the current post is in any of the given categories.
    233256 */
    234 function in_category( $category, $post = null ) {
     257function in_category( $category, $_post = null ) {
    235258    if ( empty( $category ) )
    236259        return false;
    237260
    238     return has_term( $category, 'category', $post );
     261    if ( $_post ) {
     262        $_post = get_post( $_post );
     263    } else {
     264        $_post =& $GLOBALS['post'];
     265    }
     266
     267    if ( !$_post )
     268        return false;
     269
     270    $r = is_object_in_term( $_post->ID, 'category', $category );
     271    if ( is_wp_error( $r ) )
     272        return false;
     273    return $r;
    239274}
    240275
     
    455490    $output = '';
    456491    if ( $title_li && 'list' == $style )
    457             $output = '<li class="' . esc_attr( $class ) . '">' . $title_li . '<ul>';
     492            $output = '<li class="' . $class . '">' . $title_li . '<ul>';
    458493
    459494    if ( empty( $categories ) ) {
     
    465500        }
    466501    } else {
     502        global $wp_query;
     503
    467504        if( !empty( $show_option_all ) )
    468505            if ( 'list' == $style )
     
    471508                $output .= '<a href="' .  get_bloginfo( 'url' )  . '">' . $show_option_all . '</a>';
    472509
    473         if ( empty( $r['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) {
    474             $current_term_object = get_queried_object();
    475             if ( $r['taxonomy'] == $current_term_object->taxonomy )
    476                 $r['current_category'] = get_queried_object_id();
    477         }
     510        if ( empty( $r['current_category'] ) && ( is_category() || is_tax() ) )
     511            $r['current_category'] = $wp_query->get_queried_object_id();
    478512
    479513        if ( $hierarchical )
     
    539573    foreach ( $tags as $key => $tag ) {
    540574        if ( 'edit' == $args['link'] )
    541             $link = get_edit_tag_link( $tag->term_id, $tag->taxonomy );
     575            $link = get_edit_tag_link( $tag->term_id, $args['taxonomy'] );
    542576        else
    543             $link = get_term_link( intval($tag->term_id), $tag->taxonomy );
     577            $link = get_term_link( intval($tag->term_id), $args['taxonomy'] );
    544578        if ( is_wp_error( $link ) )
    545579            return false;
     
    679713        $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
    680714        $tag_name = $tags[ $key ]->name;
    681         $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( call_user_func( $topic_count_text_callback, $real_count ) ) . "' style='font-size: " .
     715        $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( $topic_count_text_callback( $real_count ) ) . "' style='font-size: " .
    682716            ( $smallest + ( ( $count - $min_count ) * $font_step ) )
    683717            . "$unit;'>$tag_name</a>";
     
    698732    endswitch;
    699733
    700     if ( $filter )
     734    if ( $filter )
    701735        return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
    702     else
     736    else
    703737        return $return;
    704 }
    705 
    706 /**
    707  * Callback for comparing tags based on name
    708  *
    709  * @since 3.1.0
    710  * @access private
    711  */
    712 function _wp_tag_cloud_name_sort_cb( $a, $b ) {
    713     return strnatcasecmp( $a->name, $b->name );
    714 }
    715 
    716 /**
    717  * Callback for comparing tags based on count
    718  *
    719  * @since 3.1.0
    720  * @access private
    721  */
    722 function _wp_tag_cloud_count_sort_cb( $a, $b ) {
    723     return ( $a->count > $b->count );
    724738}
    725739
     
    764778}
    765779
    766 /**
    767  * Create HTML list of categories.
    768  *
    769  * @package WordPress
    770  * @since 2.1.0
    771  * @uses Walker
    772  */
    773 class Walker_Category extends Walker {
    774     /**
    775      * @see Walker::$tree_type
    776      * @since 2.1.0
    777      * @var string
    778      */
    779     var $tree_type = 'category';
    780 
    781     /**
    782      * @see Walker::$db_fields
    783      * @since 2.1.0
    784      * @todo Decouple this
    785      * @var array
    786      */
    787     var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
    788 
    789     /**
    790      * @see Walker::start_lvl()
    791      * @since 2.1.0
    792      *
    793      * @param string $output Passed by reference. Used to append additional content.
    794      * @param int $depth Depth of category. Used for tab indentation.
    795      * @param array $args Will only append content if style argument value is 'list'.
    796      */
    797     function start_lvl(&$output, $depth, $args) {
    798         if ( 'list' != $args['style'] )
    799             return;
    800 
    801         $indent = str_repeat("\t", $depth);
    802         $output .= "$indent<ul class='children'>\n";
    803     }
    804 
    805     /**
    806      * @see Walker::end_lvl()
    807      * @since 2.1.0
    808      *
    809      * @param string $output Passed by reference. Used to append additional content.
    810      * @param int $depth Depth of category. Used for tab indentation.
    811      * @param array $args Will only append content if style argument value is 'list'.
    812      */
    813     function end_lvl(&$output, $depth, $args) {
    814         if ( 'list' != $args['style'] )
    815             return;
    816 
    817         $indent = str_repeat("\t", $depth);
    818         $output .= "$indent</ul>\n";
    819     }
    820 
    821     /**
    822      * @see Walker::start_el()
    823      * @since 2.1.0
    824      *
    825      * @param string $output Passed by reference. Used to append additional content.
    826      * @param object $category Category data object.
    827      * @param int $depth Depth of category in reference to parents.
    828      * @param array $args
    829      */
    830     function start_el(&$output, $category, $depth, $args) {
    831         extract($args);
    832 
    833         $cat_name = esc_attr( $category->name );
    834         $cat_name = apply_filters( 'list_cats', $cat_name, $category );
    835         $link = '<a href="' . esc_attr( get_term_link($category) ) . '" ';
    836         if ( $use_desc_for_title == 0 || empty($category->description) )
    837             $link .= 'title="' . esc_attr( sprintf(__( 'View all posts filed under %s' ), $cat_name) ) . '"';
    838         else
    839             $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
    840         $link .= '>';
    841         $link .= $cat_name . '</a>';
    842 
    843         if ( !empty($feed_image) || !empty($feed) ) {
    844             $link .= ' ';
    845 
    846             if ( empty($feed_image) )
    847                 $link .= '(';
    848 
    849             $link .= '<a href="' . get_term_feed_link( $category->term_id, $category->taxonomy, $feed_type ) . '"';
    850 
    851             if ( empty($feed) ) {
    852                 $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
    853             } else {
    854                 $title = ' title="' . $feed . '"';
    855                 $alt = ' alt="' . $feed . '"';
    856                 $name = $feed;
    857                 $link .= $title;
    858             }
    859 
    860             $link .= '>';
    861 
    862             if ( empty($feed_image) )
    863                 $link .= $name;
    864             else
    865                 $link .= "<img src='$feed_image'$alt$title" . ' />';
    866 
    867             $link .= '</a>';
    868 
    869             if ( empty($feed_image) )
    870                 $link .= ')';
    871         }
    872 
    873         if ( !empty($show_count) )
    874             $link .= ' (' . intval($category->count) . ')';
    875 
    876         if ( !empty($show_date) )
    877             $link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);
    878 
    879         if ( 'list' == $args['style'] ) {
    880             $output .= "\t<li";
    881             $class = 'cat-item cat-item-' . $category->term_id;
    882             if ( !empty($current_category) ) {
    883                 $_current_category = get_term( $current_category, $category->taxonomy );
    884                 if ( $category->term_id == $current_category )
    885                     $class .=  ' current-cat';
    886                 elseif ( $category->term_id == $_current_category->parent )
    887                     $class .=  ' current-cat-parent';
    888             }
    889             $output .=  ' class="' . $class . '"';
    890             $output .= ">$link\n";
    891         } else {
    892             $output .= "\t$link<br />\n";
    893         }
    894     }
    895 
    896     /**
    897      * @see Walker::end_el()
    898      * @since 2.1.0
    899      *
    900      * @param string $output Passed by reference. Used to append additional content.
    901      * @param object $page Not used.
    902      * @param int $depth Depth of category. Not used.
    903      * @param array $args Only uses 'list' for whether should append to output.
    904      */
    905     function end_el(&$output, $page, $depth, $args) {
    906         if ( 'list' != $args['style'] )
    907             return;
    908 
    909         $output .= "</li>\n";
    910     }
    911 
    912 }
    913 
    914 /**
    915  * Create HTML dropdown list of Categories.
    916  *
    917  * @package WordPress
    918  * @since 2.1.0
    919  * @uses Walker
    920  */
    921 class Walker_CategoryDropdown extends Walker {
    922     /**
    923      * @see Walker::$tree_type
    924      * @since 2.1.0
    925      * @var string
    926      */
    927     var $tree_type = 'category';
    928 
    929     /**
    930      * @see Walker::$db_fields
    931      * @since 2.1.0
    932      * @todo Decouple this
    933      * @var array
    934      */
    935     var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
    936 
    937     /**
    938      * @see Walker::start_el()
    939      * @since 2.1.0
    940      *
    941      * @param string $output Passed by reference. Used to append additional content.
    942      * @param object $category Category data object.
    943      * @param int $depth Depth of category. Used for padding.
    944      * @param array $args Uses 'selected', 'show_count', and 'show_last_update' keys, if they exist.
    945      */
    946     function start_el(&$output, $category, $depth, $args) {
    947         $pad = str_repeat('&nbsp;', $depth * 3);
    948 
    949         $cat_name = apply_filters('list_cats', $category->name, $category);
    950         $output .= "\t<option class=\"level-$depth\" value=\"".$category->term_id."\"";
    951         if ( $category->term_id == $args['selected'] )
    952             $output .= ' selected="selected"';
    953         $output .= '>';
    954         $output .= $pad.$cat_name;
    955         if ( $args['show_count'] )
    956             $output .= '&nbsp;&nbsp;('. $category->count .')';
    957         if ( $args['show_last_update'] ) {
    958             $format = 'Y-m-d';
    959             $output .= '&nbsp;&nbsp;' . gmdate($format, $category->last_update_timestamp);
    960         }
    961         $output .= "</option>\n";
    962     }
    963 }
    964 
    965780//
    966781// Tags
     
    971786 *
    972787 * @since 2.3.0
    973  * @see get_term_link()
    974  *
    975  * @param int|object $tag Tag ID or object.
    976  * @return string Link on success, empty string if tag does not exist.
    977  */
    978 function get_tag_link( $tag ) {
    979     if ( ! is_object( $tag ) )
    980         $tag = (int) $tag;
    981 
    982     $tag = get_term_link( $tag, 'post_tag' );
    983 
     788 * @uses apply_filters() Calls 'tag_link' with tag link and tag ID as parameters.
     789 *
     790 * @param int $tag_id Tag (term) ID.
     791 * @return string
     792 */
     793function get_tag_link( $tag_id ) {
     794    global $wp_rewrite;
     795    $taglink = $wp_rewrite->get_tag_permastruct();
     796
     797    $tag = &get_term( $tag_id, 'post_tag' );
    984798    if ( is_wp_error( $tag ) )
    985         return '';
    986 
    987     return $tag;
     799        return $tag;
     800    $slug = $tag->slug;
     801
     802    if ( empty( $taglink ) ) {
     803        $file = get_option( 'home' ) . '/';
     804        $taglink = $file . '?tag=' . $slug;
     805    } else {
     806        $taglink = str_replace( '%tag%', $slug, $taglink );
     807        $taglink = get_option( 'home' ) . user_trailingslashit( $taglink, 'category' );
     808    }
     809    return apply_filters( 'tag_link', $taglink, $tag_id );
    988810}
    989811
     
    1054876function term_description( $term = 0, $taxonomy = 'post_tag' ) {
    1055877    if ( !$term && ( is_tax() || is_tag() || is_category() ) ) {
    1056         $term = get_queried_object();
     878        global $wp_query;
     879        $term = $wp_query->get_queried_object();
    1057880        $taxonomy = $term->taxonomy;
    1058881        $term = $term->term_id;
     
    1086909
    1087910    $terms = get_object_term_cache( $id, $taxonomy );
    1088     if ( false === $terms ) {
     911    if ( false === $terms )
    1089912        $terms = wp_get_object_terms( $id, $taxonomy );
    1090         wp_cache_add($id, $terms, $taxonomy . '_relationships');
    1091     }
    1092 
    1093     $terms = apply_filters( 'get_the_terms', $terms, $id, $taxonomy );
    1094913
    1095914    if ( empty( $terms ) )
     
    1137956 * @since 2.5.0
    1138957 *
    1139  * @param int $id Post ID.
     958 * @param int $id Term ID.
    1140959 * @param string $taxonomy Taxonomy name.
    1141960 * @param string $before Optional. Before list.
     
    1144963 * @return null|bool False on WordPress error. Returns null when displaying.
    1145964 */
    1146 function the_terms( $id = 0, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
     965function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
    1147966    $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after );
    1148967
     
    1151970
    1152971    echo apply_filters('the_terms', $term_list, $taxonomy, $before, $sep, $after);
    1153 }
    1154 
    1155 
    1156 /**
    1157  * Check if the current post has any of given category.
    1158  *
    1159  * @since 3.1.0
    1160  *
    1161  * @param string|int|array $tag Optional. The category name/term_id/slug or array of them to check for.
    1162  * @param int|object $post Optional. Post to check instead of the current post.
    1163  * @return bool True if the current post has any of the given categories (or any category, if no category specified).
    1164  */
    1165 function has_category( $category = '', $post = null ) {
    1166     return has_term( $category, 'category', $post );
    1167972}
    1168973
     
    1180985 * @since 2.6.0
    1181986 *
     987 * @uses is_object_in_term()
     988 *
    1182989 * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
    1183  * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
    1184  * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
    1185  */
    1186 function has_tag( $tag = '', $post = null ) {
    1187     return has_term( $tag, 'post_tag', $post );
    1188 }
    1189 
    1190 /**
    1191  * Check if the current post has any of given terms.
    1192  *
    1193  * The given terms are checked against the post's terms' term_ids, names and slugs.
    1194  * Terms given as integers will only be checked against the post's terms' term_ids.
    1195  * If no terms are given, determines if post has any terms.
    1196  *
    1197  * @since 3.1.0
    1198  *
    1199  * @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for.
    1200  * @param string $taxonomy Taxonomy name
    1201  * @param int|object $post Optional. Post to check instead of the current post.
    1202  * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
    1203  */
    1204 function has_term( $term = '', $taxonomy = '', $post = null ) {
    1205     $post = get_post($post);
    1206 
    1207     if ( !$post )
     990 * @param int|post object Optional.  Post to check instead of the current post. @since 2.7.0
     991 * @return bool True if the current post has any of the the given tags (or any tag, if no tag specified).
     992 */
     993function has_tag( $tag = '', $_post = null ) {
     994    if ( $_post ) {
     995        $_post = get_post( $_post );
     996    } else {
     997        $_post =& $GLOBALS['post'];
     998    }
     999
     1000    if ( !$_post )
    12081001        return false;
    12091002
    1210     $r = is_object_in_term( $post->ID, $taxonomy, $term );
     1003    $r = is_object_in_term( $_post->ID, 'post_tag', $tag );
    12111004    if ( is_wp_error( $r ) )
    12121005        return false;
    1213 
    12141006    return $r;
    12151007}
Note: See TracChangeset for help on using the changeset viewer.