]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/category-template.php
Wordpress 3.3.1-scripts
[autoinstalls/wordpress.git] / wp-includes / category-template.php
1 <?php
2 /**
3  * Category Template Tags and API.
4  *
5  * @package WordPress
6  * @subpackage Template
7  */
8
9 /**
10  * Retrieve category link URL.
11  *
12  * @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;
28 }
29
30 /**
31  * Retrieve category parents with separator.
32  *
33  * @since 1.2.0
34  *
35  * @param int $id Category ID.
36  * @param bool $link Optional, default is false. Whether to format with link.
37  * @param string $separator Optional, default is '/'. How to separate categories.
38  * @param bool $nicename Optional, default is false. Whether to use nice name for display.
39  * @param array $visited Optional. Already linked to categories to prevent duplicates.
40  * @return string
41  */
42 function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
43         $chain = '';
44         $parent = &get_category( $id );
45         if ( is_wp_error( $parent ) )
46                 return $parent;
47
48         if ( $nicename )
49                 $name = $parent->slug;
50         else
51                 $name = $parent->name;
52
53         if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
54                 $visited[] = $parent->parent;
55                 $chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited );
56         }
57
58         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;
60         else
61                 $chain .= $name.$separator;
62         return $chain;
63 }
64
65 /**
66  * Retrieve post categories.
67  *
68  * @since 0.71
69  * @uses $post
70  *
71  * @param int $id Optional, default to current post ID. The post ID.
72  * @return array
73  */
74 function get_the_category( $id = false ) {
75         $categories = get_the_terms( $id, 'category' );
76         if ( ! $categories )
77                 $categories = array();
78
79         $categories = array_values( $categories );
80
81         foreach ( array_keys( $categories ) as $key ) {
82                 _make_cat_compat( $categories[$key] );
83         }
84
85         // Filter name is plural because we return alot of categories (possibly more than #13237) not just one
86         return apply_filters( 'get_the_categories', $categories );
87 }
88
89 /**
90  * Sort categories by name.
91  *
92  * Used by usort() as a callback, should not be used directly. Can actually be
93  * used to sort any term object.
94  *
95  * @since 2.3.0
96  * @access private
97  *
98  * @param object $a
99  * @param object $b
100  * @return int
101  */
102 function _usort_terms_by_name( $a, $b ) {
103         return strcmp( $a->name, $b->name );
104 }
105
106 /**
107  * Sort categories by ID.
108  *
109  * Used by usort() as a callback, should not be used directly. Can actually be
110  * used to sort any term object.
111  *
112  * @since 2.3.0
113  * @access private
114  *
115  * @param object $a
116  * @param object $b
117  * @return int
118  */
119 function _usort_terms_by_ID( $a, $b ) {
120         if ( $a->term_id > $b->term_id )
121                 return 1;
122         elseif ( $a->term_id < $b->term_id )
123                 return -1;
124         else
125                 return 0;
126 }
127
128 /**
129  * Retrieve category name based on category ID.
130  *
131  * @since 0.71
132  *
133  * @param int $cat_ID Category ID.
134  * @return string Category name.
135  */
136 function get_the_category_by_ID( $cat_ID ) {
137         $cat_ID = (int) $cat_ID;
138         $category = &get_category( $cat_ID );
139         if ( is_wp_error( $category ) )
140                 return $category;
141         return $category->name;
142 }
143
144 /**
145  * Retrieve category list in either HTML list or custom format.
146  *
147  * @since 1.5.1
148  *
149  * @param string $separator Optional, default is empty string. Separator for between the categories.
150  * @param string $parents Optional. How to display the parents.
151  * @param int $post_id Optional. Post ID to retrieve categories.
152  * @return string
153  */
154 function get_the_category_list( $separator = '', $parents='', $post_id = false ) {
155         global $wp_rewrite;
156         $categories = get_the_category( $post_id );
157         if ( !is_object_in_taxonomy( get_post_type( $post_id ), 'category' ) )
158                 return apply_filters( 'the_category', '', $separator, $parents );
159
160         if ( empty( $categories ) )
161                 return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents );
162
163         $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
164
165         $thelist = '';
166         if ( '' == $separator ) {
167                 $thelist .= '<ul class="post-categories">';
168                 foreach ( $categories as $category ) {
169                         $thelist .= "\n\t<li>";
170                         switch ( strtolower( $parents ) ) {
171                                 case 'multiple':
172                                         if ( $category->parent )
173                                                 $thelist .= get_category_parents( $category->parent, true, $separator );
174                                         $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>';
175                                         break;
176                                 case 'single':
177                                         $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>';
178                                         if ( $category->parent )
179                                                 $thelist .= get_category_parents( $category->parent, false, $separator );
180                                         $thelist .= $category->name.'</a></li>';
181                                         break;
182                                 case '':
183                                 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>';
185                         }
186                 }
187                 $thelist .= '</ul>';
188         } else {
189                 $i = 0;
190                 foreach ( $categories as $category ) {
191                         if ( 0 < $i )
192                                 $thelist .= $separator;
193                         switch ( strtolower( $parents ) ) {
194                                 case 'multiple':
195                                         if ( $category->parent )
196                                                 $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>';
198                                         break;
199                                 case 'single':
200                                         $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>';
201                                         if ( $category->parent )
202                                                 $thelist .= get_category_parents( $category->parent, false, $separator );
203                                         $thelist .= "$category->name</a>";
204                                         break;
205                                 case '':
206                                 default:
207                                         $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a>';
208                         }
209                         ++$i;
210                 }
211         }
212         return apply_filters( 'the_category', $thelist, $separator, $parents );
213 }
214
215
216 /**
217  * Check if the current post in within any of the given categories.
218  *
219  * The given categories are checked against the post's categories' term_ids, names and slugs.
220  * Categories given as integers will only be checked against the post's categories' term_ids.
221  *
222  * Prior to v2.5 of WordPress, category names were not supported.
223  * Prior to v2.7, category slugs were not supported.
224  * Prior to v2.7, only one category could be compared: in_category( $single_category ).
225  * Prior to v2.7, this function could only be used in the WordPress Loop.
226  * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
227  *
228  * @since 1.2.0
229  *
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)
232  * @return bool True if the current post is in any of the given categories.
233  */
234 function in_category( $category, $post = null ) {
235         if ( empty( $category ) )
236                 return false;
237
238         return has_term( $category, 'category', $post );
239 }
240
241 /**
242  * Display the category list for the post.
243  *
244  * @since 0.71
245  *
246  * @param string $separator Optional, default is empty string. Separator for between the categories.
247  * @param string $parents Optional. How to display the parents.
248  * @param int $post_id Optional. Post ID to retrieve categories.
249  */
250 function the_category( $separator = '', $parents='', $post_id = false ) {
251         echo get_the_category_list( $separator, $parents, $post_id );
252 }
253
254 /**
255  * Retrieve category description.
256  *
257  * @since 1.0.0
258  *
259  * @param int $category Optional. Category ID. Will use global category ID by default.
260  * @return string Category description, available.
261  */
262 function category_description( $category = 0 ) {
263         return term_description( $category, 'category' );
264 }
265
266 /**
267  * Display or retrieve the HTML dropdown list of categories.
268  *
269  * The list of arguments is below:
270  *     'show_option_all' (string) - Text to display for showing all categories.
271  *     'show_option_none' (string) - Text to display for showing no categories.
272  *     'orderby' (string) default is 'ID' - What column to use for ordering the
273  * categories.
274  *     'order' (string) default is 'ASC' - What direction to order categories.
275  *     'show_last_update' (bool|int) default is 0 - See {@link get_categories()}
276  *     'show_count' (bool|int) default is 0 - Whether to show how many posts are
277  * in the category.
278  *     'hide_empty' (bool|int) default is 1 - Whether to hide categories that
279  * don't have any posts attached to them.
280  *     'child_of' (int) default is 0 - See {@link get_categories()}.
281  *     'exclude' (string) - See {@link get_categories()}.
282  *     'echo' (bool|int) default is 1 - Whether to display or retrieve content.
283  *     'depth' (int) - The max depth.
284  *     'tab_index' (int) - Tab index for select element.
285  *     'name' (string) - The name attribute value for select element.
286  *     'id' (string) - The ID attribute value for select element. Defaults to name if omitted.
287  *     'class' (string) - The class attribute value for select element.
288  *     'selected' (int) - Which category ID is selected.
289  *     'taxonomy' (string) - The name of the taxonomy to retrieve. Defaults to category.
290  *
291  * The 'hierarchical' argument, which is disabled by default, will override the
292  * depth argument, unless it is true. When the argument is false, it will
293  * display all of the categories. When it is enabled it will use the value in
294  * the 'depth' argument.
295  *
296  * @since 2.1.0
297  *
298  * @param string|array $args Optional. Override default arguments.
299  * @return string HTML content only if 'echo' argument is 0.
300  */
301 function wp_dropdown_categories( $args = '' ) {
302         $defaults = array(
303                 'show_option_all' => '', 'show_option_none' => '',
304                 'orderby' => 'id', 'order' => 'ASC',
305                 'show_last_update' => 0, 'show_count' => 0,
306                 'hide_empty' => 1, 'child_of' => 0,
307                 'exclude' => '', 'echo' => 1,
308                 'selected' => 0, 'hierarchical' => 0,
309                 'name' => 'cat', 'id' => '',
310                 'class' => 'postform', 'depth' => 0,
311                 'tab_index' => 0, 'taxonomy' => 'category',
312                 'hide_if_empty' => false
313         );
314
315         $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
316
317         // Back compat.
318         if ( isset( $args['type'] ) && 'link' == $args['type'] ) {
319                 _deprecated_argument( __FUNCTION__, '3.0', '' );
320                 $args['taxonomy'] = 'link_category';
321         }
322
323         $r = wp_parse_args( $args, $defaults );
324
325         if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
326                 $r['pad_counts'] = true;
327         }
328
329         $r['include_last_update_time'] = $r['show_last_update'];
330         extract( $r );
331
332         $tab_index_attribute = '';
333         if ( (int) $tab_index > 0 )
334                 $tab_index_attribute = " tabindex=\"$tab_index\"";
335
336         $categories = get_terms( $taxonomy, $r );
337         $name = esc_attr( $name );
338         $class = esc_attr( $class );
339         $id = $id ? esc_attr( $id ) : $name;
340
341         if ( ! $r['hide_if_empty'] || ! empty($categories) )
342                 $output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
343         else
344                 $output = '';
345
346         if ( empty($categories) && ! $r['hide_if_empty'] && !empty($show_option_none) ) {
347                 $show_option_none = apply_filters( 'list_cats', $show_option_none );
348                 $output .= "\t<option value='-1' selected='selected'>$show_option_none</option>\n";
349         }
350
351         if ( ! empty( $categories ) ) {
352
353                 if ( $show_option_all ) {
354                         $show_option_all = apply_filters( 'list_cats', $show_option_all );
355                         $selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : '';
356                         $output .= "\t<option value='0'$selected>$show_option_all</option>\n";
357                 }
358
359                 if ( $show_option_none ) {
360                         $show_option_none = apply_filters( 'list_cats', $show_option_none );
361                         $selected = ( '-1' === strval($r['selected']) ) ? " selected='selected'" : '';
362                         $output .= "\t<option value='-1'$selected>$show_option_none</option>\n";
363                 }
364
365                 if ( $hierarchical )
366                         $depth = $r['depth'];  // Walk the full depth.
367                 else
368                         $depth = -1; // Flat.
369
370                 $output .= walk_category_dropdown_tree( $categories, $depth, $r );
371         }
372         if ( ! $r['hide_if_empty'] || ! empty($categories) )
373                 $output .= "</select>\n";
374
375
376         $output = apply_filters( 'wp_dropdown_cats', $output );
377
378         if ( $echo )
379                 echo $output;
380
381         return $output;
382 }
383
384 /**
385  * Display or retrieve the HTML list of categories.
386  *
387  * The list of arguments is below:
388  *     'show_option_all' (string) - Text to display for showing all categories.
389  *     'orderby' (string) default is 'ID' - What column to use for ordering the
390  * categories.
391  *     'order' (string) default is 'ASC' - What direction to order categories.
392  *     'show_last_update' (bool|int) default is 0 - See {@link
393  * walk_category_dropdown_tree()}
394  *     'show_count' (bool|int) default is 0 - Whether to show how many posts are
395  * in the category.
396  *     'hide_empty' (bool|int) default is 1 - Whether to hide categories that
397  * don't have any posts attached to them.
398  *     'use_desc_for_title' (bool|int) default is 1 - Whether to use the
399  * description instead of the category title.
400  *     'feed' - See {@link get_categories()}.
401  *     'feed_type' - See {@link get_categories()}.
402  *     'feed_image' - See {@link get_categories()}.
403  *     'child_of' (int) default is 0 - See {@link get_categories()}.
404  *     'exclude' (string) - See {@link get_categories()}.
405  *     'exclude_tree' (string) - See {@link get_categories()}.
406  *     'echo' (bool|int) default is 1 - Whether to display or retrieve content.
407  *     'current_category' (int) - See {@link get_categories()}.
408  *     'hierarchical' (bool) - See {@link get_categories()}.
409  *     'title_li' (string) - See {@link get_categories()}.
410  *     'depth' (int) - The max depth.
411  *
412  * @since 2.1.0
413  *
414  * @param string|array $args Optional. Override default arguments.
415  * @return string HTML content only if 'echo' argument is 0.
416  */
417 function wp_list_categories( $args = '' ) {
418         $defaults = array(
419                 'show_option_all' => '', 'show_option_none' => __('No categories'),
420                 'orderby' => 'name', 'order' => 'ASC',
421                 'show_last_update' => 0, 'style' => 'list',
422                 'show_count' => 0, 'hide_empty' => 1,
423                 'use_desc_for_title' => 1, 'child_of' => 0,
424                 'feed' => '', 'feed_type' => '',
425                 'feed_image' => '', 'exclude' => '',
426                 'exclude_tree' => '', 'current_category' => 0,
427                 'hierarchical' => true, 'title_li' => __( 'Categories' ),
428                 'echo' => 1, 'depth' => 0,
429                 'taxonomy' => 'category'
430         );
431
432         $r = wp_parse_args( $args, $defaults );
433
434         if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] )
435                 $r['pad_counts'] = true;
436
437         if ( isset( $r['show_date'] ) )
438                 $r['include_last_update_time'] = $r['show_date'];
439
440         if ( true == $r['hierarchical'] ) {
441                 $r['exclude_tree'] = $r['exclude'];
442                 $r['exclude'] = '';
443         }
444
445         if ( !isset( $r['class'] ) )
446                 $r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy'];
447
448         extract( $r );
449
450         if ( !taxonomy_exists($taxonomy) )
451                 return false;
452
453         $categories = get_categories( $r );
454
455         $output = '';
456         if ( $title_li && 'list' == $style )
457                         $output = '<li class="' . esc_attr( $class ) . '">' . $title_li . '<ul>';
458
459         if ( empty( $categories ) ) {
460                 if ( ! empty( $show_option_none ) ) {
461                         if ( 'list' == $style )
462                                 $output .= '<li>' . $show_option_none . '</li>';
463                         else
464                                 $output .= $show_option_none;
465                 }
466         } else {
467                 if ( ! empty( $show_option_all ) ) {
468                         $posts_page = ( 'page' == get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) ) ? get_permalink( get_option( 'page_for_posts' ) ) : home_url( '/' );
469                         $posts_page = esc_url( $posts_page );
470                         if ( 'list' == $style )
471                                 $output .= "<li><a href='$posts_page'>$show_option_all</a></li>";
472                         else
473                                 $output .= "<a href='$posts_page'>$show_option_all</a>";
474                 }
475
476                 if ( empty( $r['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) {
477                         $current_term_object = get_queried_object();
478                         if ( $r['taxonomy'] == $current_term_object->taxonomy )
479                                 $r['current_category'] = get_queried_object_id();
480                 }
481
482                 if ( $hierarchical )
483                         $depth = $r['depth'];
484                 else
485                         $depth = -1; // Flat.
486
487                 $output .= walk_category_tree( $categories, $depth, $r );
488         }
489
490         if ( $title_li && 'list' == $style )
491                 $output .= '</ul></li>';
492
493         $output = apply_filters( 'wp_list_categories', $output, $args );
494
495         if ( $echo )
496                 echo $output;
497         else
498                 return $output;
499 }
500
501 /**
502  * Display tag cloud.
503  *
504  * The text size is set by the 'smallest' and 'largest' arguments, which will
505  * use the 'unit' argument value for the CSS text size unit. The 'format'
506  * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
507  * 'format' argument will separate tags with spaces. The list value for the
508  * 'format' argument will format the tags in a UL HTML list. The array value for
509  * the 'format' argument will return in PHP array type format.
510  *
511  * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
512  * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'.
513  *
514  * The 'number' argument is how many tags to return. By default, the limit will
515  * be to return the top 45 tags in the tag cloud list.
516  *
517  * The 'topic_count_text_callback' argument is a function, which, given the count
518  * of the posts  with that tag, returns a text for the tooltip of the tag link.
519  *
520  * The 'exclude' and 'include' arguments are used for the {@link get_tags()}
521  * function. Only one should be used, because only one will be used and the
522  * other ignored, if they are both set.
523  *
524  * @since 2.3.0
525  *
526  * @param array|string $args Optional. Override default arguments.
527  * @return array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument.
528  */
529 function wp_tag_cloud( $args = '' ) {
530         $defaults = array(
531                 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
532                 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
533                 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true
534         );
535         $args = wp_parse_args( $args, $defaults );
536
537         $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
538
539         if ( empty( $tags ) || is_wp_error( $tags ) )
540                 return;
541
542         foreach ( $tags as $key => $tag ) {
543                 if ( 'edit' == $args['link'] )
544                         $link = get_edit_tag_link( $tag->term_id, $tag->taxonomy );
545                 else
546                         $link = get_term_link( intval($tag->term_id), $tag->taxonomy );
547                 if ( is_wp_error( $link ) )
548                         return false;
549
550                 $tags[ $key ]->link = $link;
551                 $tags[ $key ]->id = $tag->term_id;
552         }
553
554         $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
555
556         $return = apply_filters( 'wp_tag_cloud', $return, $args );
557
558         if ( 'array' == $args['format'] || empty($args['echo']) )
559                 return $return;
560
561         echo $return;
562 }
563
564 /**
565  * Default text for tooltip for tag links
566  *
567  * @param integer $count number of posts with that tag
568  * @return string text for the tooltip of a tag link.
569  */
570 function default_topic_count_text( $count ) {
571         return sprintf( _n('%s topic', '%s topics', $count), number_format_i18n( $count ) );
572 }
573
574 /**
575  * Default topic count scaling for tag links
576  *
577  * @param integer $count number of posts with that tag
578  * @return integer scaled count
579  */
580 function default_topic_count_scale( $count ) {
581         return round(log10($count + 1) * 100);
582 }
583
584
585 /**
586  * Generates a tag cloud (heatmap) from provided data.
587  *
588  * The text size is set by the 'smallest' and 'largest' arguments, which will
589  * use the 'unit' argument value for the CSS text size unit. The 'format'
590  * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
591  * 'format' argument will separate tags with spaces. The list value for the
592  * 'format' argument will format the tags in a UL HTML list. The array value for
593  * the 'format' argument will return in PHP array type format.
594  *
595  * The 'tag_cloud_sort' filter allows you to override the sorting.
596  * Passed to the filter: $tags array and $args array, has to return the $tags array
597  * after sorting it.
598  *
599  * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
600  * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or
601  * 'RAND'.
602  *
603  * The 'number' argument is how many tags to return. By default, the limit will
604  * be to return the entire tag cloud list.
605  *
606  * The 'topic_count_text_callback' argument is a function, which given the count
607  * of the posts  with that tag returns a text for the tooltip of the tag link.
608  *
609  * @todo Complete functionality.
610  * @since 2.3.0
611  *
612  * @param array $tags List of tags.
613  * @param string|array $args Optional, override default arguments.
614  * @return string
615  */
616 function wp_generate_tag_cloud( $tags, $args = '' ) {
617         global $wp_rewrite;
618         $defaults = array(
619                 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
620                 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
621                 'topic_count_text_callback' => 'default_topic_count_text',
622                 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
623         );
624
625         if ( !isset( $args['topic_count_text_callback'] ) && isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
626                 $body = 'return sprintf (
627                         _n(' . var_export($args['single_text'], true) . ', ' . var_export($args['multiple_text'], true) . ', $count),
628                         number_format_i18n( $count ));';
629                 $args['topic_count_text_callback'] = create_function('$count', $body);
630         }
631
632         $args = wp_parse_args( $args, $defaults );
633         extract( $args );
634
635         if ( empty( $tags ) )
636                 return;
637
638         $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );
639         if ( $tags_sorted != $tags  ) { // the tags have been sorted by a plugin
640                 $tags = $tags_sorted;
641                 unset($tags_sorted);
642         } else {
643                 if ( 'RAND' == $order ) {
644                         shuffle($tags);
645                 } else {
646                         // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
647                         if ( 'name' == $orderby )
648                                 uasort( $tags, '_wp_object_name_sort_cb' );
649                         else
650                                 uasort( $tags, '_wp_object_count_sort_cb' );
651
652                         if ( 'DESC' == $order )
653                                 $tags = array_reverse( $tags, true );
654                 }
655         }
656
657         if ( $number > 0 )
658                 $tags = array_slice($tags, 0, $number);
659
660         $counts = array();
661         $real_counts = array(); // For the alt tag
662         foreach ( (array) $tags as $key => $tag ) {
663                 $real_counts[ $key ] = $tag->count;
664                 $counts[ $key ] = $topic_count_scale_callback($tag->count);
665         }
666
667         $min_count = min( $counts );
668         $spread = max( $counts ) - $min_count;
669         if ( $spread <= 0 )
670                 $spread = 1;
671         $font_spread = $largest - $smallest;
672         if ( $font_spread < 0 )
673                 $font_spread = 1;
674         $font_step = $font_spread / $spread;
675
676         $a = array();
677
678         foreach ( $tags as $key => $tag ) {
679                 $count = $counts[ $key ];
680                 $real_count = $real_counts[ $key ];
681                 $tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
682                 $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
683                 $tag_name = $tags[ $key ]->name;
684                 $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: " .
685                         ( $smallest + ( ( $count - $min_count ) * $font_step ) )
686                         . "$unit;'>$tag_name</a>";
687         }
688
689         switch ( $format ) :
690         case 'array' :
691                 $return =& $a;
692                 break;
693         case 'list' :
694                 $return = "<ul class='wp-tag-cloud'>\n\t<li>";
695                 $return .= join( "</li>\n\t<li>", $a );
696                 $return .= "</li>\n</ul>\n";
697                 break;
698         default :
699                 $return = join( $separator, $a );
700                 break;
701         endswitch;
702
703         if ( $filter )
704                 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
705         else
706                 return $return;
707 }
708
709 /**
710  * Callback for comparing objects based on name
711  *
712  * @since 3.1.0
713  * @access private
714  */
715 function _wp_object_name_sort_cb( $a, $b ) {
716         return strnatcasecmp( $a->name, $b->name );
717 }
718
719 /**
720  * Callback for comparing objects based on count
721  *
722  * @since 3.1.0
723  * @access private
724  */
725 function _wp_object_count_sort_cb( $a, $b ) {
726         return ( $a->count > $b->count );
727 }
728
729 //
730 // Helper functions
731 //
732
733 /**
734  * Retrieve HTML list content for category list.
735  *
736  * @uses Walker_Category to create HTML list content.
737  * @since 2.1.0
738  * @see Walker_Category::walk() for parameters and return description.
739  */
740 function walk_category_tree() {
741         $args = func_get_args();
742         // the user's options are the third parameter
743         if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
744                 $walker = new Walker_Category;
745         else
746                 $walker = $args[2]['walker'];
747
748         return call_user_func_array(array( &$walker, 'walk' ), $args );
749 }
750
751 /**
752  * Retrieve HTML dropdown (select) content for category list.
753  *
754  * @uses Walker_CategoryDropdown to create HTML dropdown content.
755  * @since 2.1.0
756  * @see Walker_CategoryDropdown::walk() for parameters and return description.
757  */
758 function walk_category_dropdown_tree() {
759         $args = func_get_args();
760         // the user's options are the third parameter
761         if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
762                 $walker = new Walker_CategoryDropdown;
763         else
764                 $walker = $args[2]['walker'];
765
766         return call_user_func_array(array( &$walker, 'walk' ), $args );
767 }
768
769 /**
770  * Create HTML list of categories.
771  *
772  * @package WordPress
773  * @since 2.1.0
774  * @uses Walker
775  */
776 class Walker_Category extends Walker {
777         /**
778          * @see Walker::$tree_type
779          * @since 2.1.0
780          * @var string
781          */
782         var $tree_type = 'category';
783
784         /**
785          * @see Walker::$db_fields
786          * @since 2.1.0
787          * @todo Decouple this
788          * @var array
789          */
790         var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
791
792         /**
793          * @see Walker::start_lvl()
794          * @since 2.1.0
795          *
796          * @param string $output Passed by reference. Used to append additional content.
797          * @param int $depth Depth of category. Used for tab indentation.
798          * @param array $args Will only append content if style argument value is 'list'.
799          */
800         function start_lvl(&$output, $depth, $args) {
801                 if ( 'list' != $args['style'] )
802                         return;
803
804                 $indent = str_repeat("\t", $depth);
805                 $output .= "$indent<ul class='children'>\n";
806         }
807
808         /**
809          * @see Walker::end_lvl()
810          * @since 2.1.0
811          *
812          * @param string $output Passed by reference. Used to append additional content.
813          * @param int $depth Depth of category. Used for tab indentation.
814          * @param array $args Will only append content if style argument value is 'list'.
815          */
816         function end_lvl(&$output, $depth, $args) {
817                 if ( 'list' != $args['style'] )
818                         return;
819
820                 $indent = str_repeat("\t", $depth);
821                 $output .= "$indent</ul>\n";
822         }
823
824         /**
825          * @see Walker::start_el()
826          * @since 2.1.0
827          *
828          * @param string $output Passed by reference. Used to append additional content.
829          * @param object $category Category data object.
830          * @param int $depth Depth of category in reference to parents.
831          * @param array $args
832          */
833         function start_el(&$output, $category, $depth, $args) {
834                 extract($args);
835
836                 $cat_name = esc_attr( $category->name );
837                 $cat_name = apply_filters( 'list_cats', $cat_name, $category );
838                 $link = '<a href="' . esc_attr( get_term_link($category) ) . '" ';
839                 if ( $use_desc_for_title == 0 || empty($category->description) )
840                         $link .= 'title="' . esc_attr( sprintf(__( 'View all posts filed under %s' ), $cat_name) ) . '"';
841                 else
842                         $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
843                 $link .= '>';
844                 $link .= $cat_name . '</a>';
845
846                 if ( !empty($feed_image) || !empty($feed) ) {
847                         $link .= ' ';
848
849                         if ( empty($feed_image) )
850                                 $link .= '(';
851
852                         $link .= '<a href="' . get_term_feed_link( $category->term_id, $category->taxonomy, $feed_type ) . '"';
853
854                         if ( empty($feed) ) {
855                                 $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
856                         } else {
857                                 $title = ' title="' . $feed . '"';
858                                 $alt = ' alt="' . $feed . '"';
859                                 $name = $feed;
860                                 $link .= $title;
861                         }
862
863                         $link .= '>';
864
865                         if ( empty($feed_image) )
866                                 $link .= $name;
867                         else
868                                 $link .= "<img src='$feed_image'$alt$title" . ' />';
869
870                         $link .= '</a>';
871
872                         if ( empty($feed_image) )
873                                 $link .= ')';
874                 }
875
876                 if ( !empty($show_count) )
877                         $link .= ' (' . intval($category->count) . ')';
878
879                 if ( !empty($show_date) )
880                         $link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);
881
882                 if ( 'list' == $args['style'] ) {
883                         $output .= "\t<li";
884                         $class = 'cat-item cat-item-' . $category->term_id;
885                         if ( !empty($current_category) ) {
886                                 $_current_category = get_term( $current_category, $category->taxonomy );
887                                 if ( $category->term_id == $current_category )
888                                         $class .=  ' current-cat';
889                                 elseif ( $category->term_id == $_current_category->parent )
890                                         $class .=  ' current-cat-parent';
891                         }
892                         $output .=  ' class="' . $class . '"';
893                         $output .= ">$link\n";
894                 } else {
895                         $output .= "\t$link<br />\n";
896                 }
897         }
898
899         /**
900          * @see Walker::end_el()
901          * @since 2.1.0
902          *
903          * @param string $output Passed by reference. Used to append additional content.
904          * @param object $page Not used.
905          * @param int $depth Depth of category. Not used.
906          * @param array $args Only uses 'list' for whether should append to output.
907          */
908         function end_el(&$output, $page, $depth, $args) {
909                 if ( 'list' != $args['style'] )
910                         return;
911
912                 $output .= "</li>\n";
913         }
914
915 }
916
917 /**
918  * Create HTML dropdown list of Categories.
919  *
920  * @package WordPress
921  * @since 2.1.0
922  * @uses Walker
923  */
924 class Walker_CategoryDropdown extends Walker {
925         /**
926          * @see Walker::$tree_type
927          * @since 2.1.0
928          * @var string
929          */
930         var $tree_type = 'category';
931
932         /**
933          * @see Walker::$db_fields
934          * @since 2.1.0
935          * @todo Decouple this
936          * @var array
937          */
938         var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
939
940         /**
941          * @see Walker::start_el()
942          * @since 2.1.0
943          *
944          * @param string $output Passed by reference. Used to append additional content.
945          * @param object $category Category data object.
946          * @param int $depth Depth of category. Used for padding.
947          * @param array $args Uses 'selected', 'show_count', and 'show_last_update' keys, if they exist.
948          */
949         function start_el(&$output, $category, $depth, $args) {
950                 $pad = str_repeat('&nbsp;', $depth * 3);
951
952                 $cat_name = apply_filters('list_cats', $category->name, $category);
953                 $output .= "\t<option class=\"level-$depth\" value=\"".$category->term_id."\"";
954                 if ( $category->term_id == $args['selected'] )
955                         $output .= ' selected="selected"';
956                 $output .= '>';
957                 $output .= $pad.$cat_name;
958                 if ( $args['show_count'] )
959                         $output .= '&nbsp;&nbsp;('. $category->count .')';
960                 if ( $args['show_last_update'] ) {
961                         $format = 'Y-m-d';
962                         $output .= '&nbsp;&nbsp;' . gmdate($format, $category->last_update_timestamp);
963                 }
964                 $output .= "</option>\n";
965         }
966 }
967
968 //
969 // Tags
970 //
971
972 /**
973  * Retrieve the link to the tag.
974  *
975  * @since 2.3.0
976  * @see get_term_link()
977  *
978  * @param int|object $tag Tag ID or object.
979  * @return string Link on success, empty string if tag does not exist.
980  */
981 function get_tag_link( $tag ) {
982         if ( ! is_object( $tag ) )
983                 $tag = (int) $tag;
984
985         $tag = get_term_link( $tag, 'post_tag' );
986
987         if ( is_wp_error( $tag ) )
988                 return '';
989
990         return $tag;
991 }
992
993 /**
994  * Retrieve the tags for a post.
995  *
996  * @since 2.3.0
997  * @uses apply_filters() Calls 'get_the_tags' filter on the list of post tags.
998  *
999  * @param int $id Post ID.
1000  * @return array
1001  */
1002 function get_the_tags( $id = 0 ) {
1003         return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) );
1004 }
1005
1006 /**
1007  * Retrieve the tags for a post formatted as a string.
1008  *
1009  * @since 2.3.0
1010  * @uses apply_filters() Calls 'the_tags' filter on string list of tags.
1011  *
1012  * @param string $before Optional. Before tags.
1013  * @param string $sep Optional. Between tags.
1014  * @param string $after Optional. After tags.
1015  * @return string
1016  */
1017 function get_the_tag_list( $before = '', $sep = '', $after = '' ) {
1018         return apply_filters( 'the_tags', get_the_term_list( 0, 'post_tag', $before, $sep, $after ), $before, $sep, $after);
1019 }
1020
1021 /**
1022  * Retrieve the tags for a post.
1023  *
1024  * @since 2.3.0
1025  *
1026  * @param string $before Optional. Before list.
1027  * @param string $sep Optional. Separate items using this.
1028  * @param string $after Optional. After list.
1029  * @return string
1030  */
1031 function the_tags( $before = null, $sep = ', ', $after = '' ) {
1032         if ( null === $before )
1033                 $before = __('Tags: ');
1034         echo get_the_tag_list($before, $sep, $after);
1035 }
1036
1037 /**
1038  * Retrieve tag description.
1039  *
1040  * @since 2.8
1041  *
1042  * @param int $tag Optional. Tag ID. Will use global tag ID by default.
1043  * @return string Tag description, available.
1044  */
1045 function tag_description( $tag = 0 ) {
1046         return term_description( $tag );
1047 }
1048
1049 /**
1050  * Retrieve term description.
1051  *
1052  * @since 2.8
1053  *
1054  * @param int $term Optional. Term ID. Will use global term ID by default.
1055  * @return string Term description, available.
1056  */
1057 function term_description( $term = 0, $taxonomy = 'post_tag' ) {
1058         if ( !$term && ( is_tax() || is_tag() || is_category() ) ) {
1059                 $term = get_queried_object();
1060                 $taxonomy = $term->taxonomy;
1061                 $term = $term->term_id;
1062         }
1063         $description = get_term_field( 'description', $term, $taxonomy );
1064         return is_wp_error( $description ) ? '' : $description;
1065 }
1066
1067 /**
1068  * Retrieve the terms of the taxonomy that are attached to the post.
1069  *
1070  * @since 2.5.0
1071  *
1072  * @param int $id Post ID. Is not optional.
1073  * @param string $taxonomy Taxonomy name.
1074  * @return array|bool False on failure. Array of term objects on success.
1075  */
1076 function get_the_terms( $id = 0, $taxonomy ) {
1077         global $post;
1078
1079         $id = (int) $id;
1080
1081         if ( !$id ) {
1082                 if ( !$post->ID )
1083                         return false;
1084                 else
1085                         $id = (int) $post->ID;
1086         }
1087
1088         $terms = get_object_term_cache( $id, $taxonomy );
1089         if ( false === $terms ) {
1090                 $terms = wp_get_object_terms( $id, $taxonomy );
1091                 wp_cache_add($id, $terms, $taxonomy . '_relationships');
1092         }
1093
1094         $terms = apply_filters( 'get_the_terms', $terms, $id, $taxonomy );
1095
1096         if ( empty( $terms ) )
1097                 return false;
1098
1099         return $terms;
1100 }
1101
1102 /**
1103  * Retrieve a post's terms as a list with specified format.
1104  *
1105  * @since 2.5.0
1106  *
1107  * @param int $id Post ID.
1108  * @param string $taxonomy Taxonomy name.
1109  * @param string $before Optional. Before list.
1110  * @param string $sep Optional. Separate items using this.
1111  * @param string $after Optional. After list.
1112  * @return string
1113  */
1114 function get_the_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after = '' ) {
1115         $terms = get_the_terms( $id, $taxonomy );
1116
1117         if ( is_wp_error( $terms ) )
1118                 return $terms;
1119
1120         if ( empty( $terms ) )
1121                 return false;
1122
1123         foreach ( $terms as $term ) {
1124                 $link = get_term_link( $term, $taxonomy );
1125                 if ( is_wp_error( $link ) )
1126                         return $link;
1127                 $term_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>';
1128         }
1129
1130         $term_links = apply_filters( "term_links-$taxonomy", $term_links );
1131
1132         return $before . join( $sep, $term_links ) . $after;
1133 }
1134
1135 /**
1136  * Display the terms in a list.
1137  *
1138  * @since 2.5.0
1139  *
1140  * @param int $id Post ID.
1141  * @param string $taxonomy Taxonomy name.
1142  * @param string $before Optional. Before list.
1143  * @param string $sep Optional. Separate items using this.
1144  * @param string $after Optional. After list.
1145  * @return null|bool False on WordPress error. Returns null when displaying.
1146  */
1147 function the_terms( $id = 0, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
1148         $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after );
1149
1150         if ( is_wp_error( $term_list ) )
1151                 return false;
1152
1153         echo apply_filters('the_terms', $term_list, $taxonomy, $before, $sep, $after);
1154 }
1155
1156
1157 /**
1158  * Check if the current post has any of given category.
1159  *
1160  * @since 3.1.0
1161  *
1162  * @param string|int|array $tag Optional. The category name/term_id/slug or array of them to check for.
1163  * @param int|object $post Optional. Post to check instead of the current post.
1164  * @return bool True if the current post has any of the given categories (or any category, if no category specified).
1165  */
1166 function has_category( $category = '', $post = null ) {
1167         return has_term( $category, 'category', $post );
1168 }
1169
1170 /**
1171  * Check if the current post has any of given tags.
1172  *
1173  * The given tags are checked against the post's tags' term_ids, names and slugs.
1174  * Tags given as integers will only be checked against the post's tags' term_ids.
1175  * If no tags are given, determines if post has any tags.
1176  *
1177  * Prior to v2.7 of WordPress, tags given as integers would also be checked against the post's tags' names and slugs (in addition to term_ids)
1178  * Prior to v2.7, this function could only be used in the WordPress Loop.
1179  * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
1180  *
1181  * @since 2.6.0
1182  *
1183  * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
1184  * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
1185  * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1186  */
1187 function has_tag( $tag = '', $post = null ) {
1188         return has_term( $tag, 'post_tag', $post );
1189 }
1190
1191 /**
1192  * Check if the current post has any of given terms.
1193  *
1194  * The given terms are checked against the post's terms' term_ids, names and slugs.
1195  * Terms given as integers will only be checked against the post's terms' term_ids.
1196  * If no terms are given, determines if post has any terms.
1197  *
1198  * @since 3.1.0
1199  *
1200  * @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for.
1201  * @param string $taxonomy Taxonomy name
1202  * @param int|object $post Optional. Post to check instead of the current post.
1203  * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1204  */
1205 function has_term( $term = '', $taxonomy = '', $post = null ) {
1206         $post = get_post($post);
1207
1208         if ( !$post )
1209                 return false;
1210
1211         $r = is_object_in_term( $post->ID, $taxonomy, $term );
1212         if ( is_wp_error( $r ) )
1213                 return false;
1214
1215         return $r;
1216 }
1217
1218 ?>