Make WordPress Core


Ignore:
File:
1 edited

Legend:

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

    r15220 r17443  
    1111 *
    1212 * @since 1.0.0
    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  */
    18 function 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 );
     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 */
     18function 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;
    3928}
    4029
     
    6049        $name = $parent->slug;
    6150    else
    62         $name = $parent->cat_name;
     51        $name = $parent->name;
    6352
    6453    if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
     
    6857
    6958    if ( $link )
    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;
     59        $chain .= '<a href="' . get_category_link( $parent->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
    7160    else
    7261        $chain .= $name.$separator;
     
    8473 */
    8574function get_the_category( $id = false ) {
    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
     75    $categories = get_the_terms( $id, 'category' );
     76    if ( ! $categories )
    10177        $categories = array();
    10278
    103     foreach ( (array) array_keys( $categories ) as $key ) {
     79    $categories = array_values( $categories );
     80
     81    foreach ( array_keys( $categories ) as $key ) {
    10482        _make_cat_compat( $categories[$key] );
    10583    }
    10684
    107     return $categories;
     85    // Filter name is plural because we return alot of categories not just one
     86    return apply_filters( 'get_the_categories', $categories );
    10887}
    10988
     
    203182                case '':
    204183                default:
    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>';
     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>';
    206185            }
    207186        }
     
    216195                    if ( $category->parent )
    217196                        $thelist .= get_category_parents( $category->parent, true, $separator );
    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>';
     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>';
    219198                    break;
    220199                case 'single':
     
    222201                    if ( $category->parent )
    223202                        $thelist .= get_category_parents( $category->parent, false, $separator );
    224                     $thelist .= "$category->cat_name</a>";
     203                    $thelist .= "$category->name</a>";
    225204                    break;
    226205                case '':
     
    249228 * @since 1.2.0
    250229 *
    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
     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)
    255232 * @return bool True if the current post is in any of the given categories.
    256233 */
    257 function in_category( $category, $_post = null ) {
     234function in_category( $category, $post = null ) {
    258235    if ( empty( $category ) )
    259236        return false;
    260237
    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;
     238    return has_term( $category, 'category', $post );
    274239}
    275240
     
    490455    $output = '';
    491456    if ( $title_li && 'list' == $style )
    492             $output = '<li class="' . $class . '">' . $title_li . '<ul>';
     457            $output = '<li class="' . esc_attr( $class ) . '">' . $title_li . '<ul>';
    493458
    494459    if ( empty( $categories ) ) {
     
    500465        }
    501466    } else {
    502         global $wp_query;
    503 
    504467        if( !empty( $show_option_all ) )
    505468            if ( 'list' == $style )
     
    508471                $output .= '<a href="' .  get_bloginfo( 'url' )  . '">' . $show_option_all . '</a>';
    509472
    510         if ( empty( $r['current_category'] ) && ( is_category() || is_tax() ) )
    511             $r['current_category'] = $wp_query->get_queried_object_id();
     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        }
    512478
    513479        if ( $hierarchical )
     
    573539    foreach ( $tags as $key => $tag ) {
    574540        if ( 'edit' == $args['link'] )
    575             $link = get_edit_tag_link( $tag->term_id, $args['taxonomy'] );
     541            $link = get_edit_tag_link( $tag->term_id, $tag->taxonomy );
    576542        else
    577             $link = get_term_link( intval($tag->term_id), $args['taxonomy'] );
     543            $link = get_term_link( intval($tag->term_id), $tag->taxonomy );
    578544        if ( is_wp_error( $link ) )
    579545            return false;
     
    713679        $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
    714680        $tag_name = $tags[ $key ]->name;
    715         $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( $topic_count_text_callback( $real_count ) ) . "' style='font-size: " .
     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: " .
    716682            ( $smallest + ( ( $count - $min_count ) * $font_step ) )
    717683            . "$unit;'>$tag_name</a>";
     
    732698    endswitch;
    733699
    734     if ( $filter )
     700    if ( $filter )
    735701        return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
    736     else
     702    else
    737703        return $return;
     704}
     705
     706/**
     707 * Callback for comparing tags based on name
     708 *
     709 * @since 3.1.0
     710 * @access private
     711 */
     712function _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 */
     722function _wp_tag_cloud_count_sort_cb( $a, $b ) {
     723    return ( $a->count > $b->count );
    738724}
    739725
     
    778764}
    779765
     766/**
     767 * Create HTML list of categories.
     768 *
     769 * @package WordPress
     770 * @since 2.1.0
     771 * @uses Walker
     772 */
     773class 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 */
     921class 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
    780965//
    781966// Tags
     
    786971 *
    787972 * @since 2.3.0
    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  */
    793 function 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' );
     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 */
     978function get_tag_link( $tag ) {
     979    if ( ! is_object( $tag ) )
     980        $tag = (int) $tag;
     981
     982    $tag = get_term_link( $tag, 'post_tag' );
     983
    798984    if ( is_wp_error( $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 );
     985        return '';
     986
     987    return $tag;
    810988}
    811989
     
    8761054function term_description( $term = 0, $taxonomy = 'post_tag' ) {
    8771055    if ( !$term && ( is_tax() || is_tag() || is_category() ) ) {
    878         global $wp_query;
    879         $term = $wp_query->get_queried_object();
     1056        $term = get_queried_object();
    8801057        $taxonomy = $term->taxonomy;
    8811058        $term = $term->term_id;
     
    9091086
    9101087    $terms = get_object_term_cache( $id, $taxonomy );
    911     if ( false === $terms )
     1088    if ( false === $terms ) {
    9121089        $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 );
    9131094
    9141095    if ( empty( $terms ) )
     
    9561137 * @since 2.5.0
    9571138 *
    958  * @param int $id Term ID.
     1139 * @param int $id Post ID.
    9591140 * @param string $taxonomy Taxonomy name.
    9601141 * @param string $before Optional. Before list.
     
    9631144 * @return null|bool False on WordPress error. Returns null when displaying.
    9641145 */
    965 function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
     1146function the_terms( $id = 0, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
    9661147    $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after );
    9671148
     
    9701151
    9711152    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 */
     1165function has_category( $category = '', $post = null ) {
     1166    return has_term( $category, 'category', $post );
    9721167}
    9731168
     
    9851180 * @since 2.6.0
    9861181 *
    987  * @uses is_object_in_term()
    988  *
    9891182 * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
    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  */
    993 function has_tag( $tag = '', $_post = null ) {
    994     if ( $_post ) {
    995         $_post = get_post( $_post );
    996     } else {
    997         $_post =& $GLOBALS['post'];
    998     }
    999 
    1000     if ( !$_post )
     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 */
     1186function 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 */
     1204function has_term( $term = '', $taxonomy = '', $post = null ) {
     1205    $post = get_post($post);
     1206
     1207    if ( !$post )
    10011208        return false;
    10021209
    1003     $r = is_object_in_term( $_post->ID, 'post_tag', $tag );
     1210    $r = is_object_in_term( $post->ID, $taxonomy, $term );
    10041211    if ( is_wp_error( $r ) )
    10051212        return false;
     1213
    10061214    return $r;
    10071215}
Note: See TracChangeset for help on using the changeset viewer.