]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/category-template.php
Wordpress 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 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                         if ( 'list' == $style )
469                                 $output .= '<li><a href="' .  get_bloginfo( 'url' )  . '">' . $show_option_all . '</a></li>';
470                         else
471                                 $output .= '<a href="' .  get_bloginfo( 'url' )  . '">' . $show_option_all . '</a>';
472
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                 }
478
479                 if ( $hierarchical )
480                         $depth = $r['depth'];
481                 else
482                         $depth = -1; // Flat.
483
484                 $output .= walk_category_tree( $categories, $depth, $r );
485         }
486
487         if ( $title_li && 'list' == $style )
488                 $output .= '</ul></li>';
489
490         $output = apply_filters( 'wp_list_categories', $output, $args );
491
492         if ( $echo )
493                 echo $output;
494         else
495                 return $output;
496 }
497
498 /**
499  * Display tag cloud.
500  *
501  * The text size is set by the 'smallest' and 'largest' arguments, which will
502  * use the 'unit' argument value for the CSS text size unit. The 'format'
503  * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
504  * 'format' argument will separate tags with spaces. The list value for the
505  * 'format' argument will format the tags in a UL HTML list. The array value for
506  * the 'format' argument will return in PHP array type format.
507  *
508  * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
509  * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'.
510  *
511  * The 'number' argument is how many tags to return. By default, the limit will
512  * be to return the top 45 tags in the tag cloud list.
513  *
514  * The 'topic_count_text_callback' argument is a function, which, given the count
515  * of the posts  with that tag, returns a text for the tooltip of the tag link.
516  *
517  * The 'exclude' and 'include' arguments are used for the {@link get_tags()}
518  * function. Only one should be used, because only one will be used and the
519  * other ignored, if they are both set.
520  *
521  * @since 2.3.0
522  *
523  * @param array|string $args Optional. Override default arguments.
524  * @return array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument.
525  */
526 function wp_tag_cloud( $args = '' ) {
527         $defaults = array(
528                 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
529                 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
530                 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true
531         );
532         $args = wp_parse_args( $args, $defaults );
533
534         $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
535
536         if ( empty( $tags ) )
537                 return;
538
539         foreach ( $tags as $key => $tag ) {
540                 if ( 'edit' == $args['link'] )
541                         $link = get_edit_tag_link( $tag->term_id, $tag->taxonomy );
542                 else
543                         $link = get_term_link( intval($tag->term_id), $tag->taxonomy );
544                 if ( is_wp_error( $link ) )
545                         return false;
546
547                 $tags[ $key ]->link = $link;
548                 $tags[ $key ]->id = $tag->term_id;
549         }
550
551         $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
552
553         $return = apply_filters( 'wp_tag_cloud', $return, $args );
554
555         if ( 'array' == $args['format'] || empty($args['echo']) )
556                 return $return;
557
558         echo $return;
559 }
560
561 /**
562  * Default text for tooltip for tag links
563  *
564  * @param integer $count number of posts with that tag
565  * @return string text for the tooltip of a tag link.
566  */
567 function default_topic_count_text( $count ) {
568         return sprintf( _n('%s topic', '%s topics', $count), number_format_i18n( $count ) );
569 }
570
571 /**
572  * Default topic count scaling for tag links
573  *
574  * @param integer $count number of posts with that tag
575  * @return integer scaled count
576  */
577 function default_topic_count_scale( $count ) {
578         return round(log10($count + 1) * 100);
579 }
580
581
582 /**
583  * Generates a tag cloud (heatmap) from provided data.
584  *
585  * The text size is set by the 'smallest' and 'largest' arguments, which will
586  * use the 'unit' argument value for the CSS text size unit. The 'format'
587  * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
588  * 'format' argument will separate tags with spaces. The list value for the
589  * 'format' argument will format the tags in a UL HTML list. The array value for
590  * the 'format' argument will return in PHP array type format.
591  *
592  * The 'tag_cloud_sort' filter allows you to override the sorting.
593  * Passed to the filter: $tags array and $args array, has to return the $tags array
594  * after sorting it.
595  *
596  * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
597  * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or
598  * 'RAND'.
599  *
600  * The 'number' argument is how many tags to return. By default, the limit will
601  * be to return the entire tag cloud list.
602  *
603  * The 'topic_count_text_callback' argument is a function, which given the count
604  * of the posts  with that tag returns a text for the tooltip of the tag link.
605  *
606  * @todo Complete functionality.
607  * @since 2.3.0
608  *
609  * @param array $tags List of tags.
610  * @param string|array $args Optional, override default arguments.
611  * @return string
612  */
613 function wp_generate_tag_cloud( $tags, $args = '' ) {
614         global $wp_rewrite;
615         $defaults = array(
616                 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
617                 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
618                 'topic_count_text_callback' => 'default_topic_count_text',
619                 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
620         );
621
622         if ( !isset( $args['topic_count_text_callback'] ) && isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
623                 $body = 'return sprintf (
624                         _n(' . var_export($args['single_text'], true) . ', ' . var_export($args['multiple_text'], true) . ', $count),
625                         number_format_i18n( $count ));';
626                 $args['topic_count_text_callback'] = create_function('$count', $body);
627         }
628
629         $args = wp_parse_args( $args, $defaults );
630         extract( $args );
631
632         if ( empty( $tags ) )
633                 return;
634
635         $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );
636         if ( $tags_sorted != $tags  ) { // the tags have been sorted by a plugin
637                 $tags = $tags_sorted;
638                 unset($tags_sorted);
639         } else {
640                 if ( 'RAND' == $order ) {
641                         shuffle($tags);
642                 } else {
643                         // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
644                         if ( 'name' == $orderby )
645                                 uasort( $tags, create_function('$a, $b', 'return strnatcasecmp($a->name, $b->name);') );
646                         else
647                                 uasort( $tags, create_function('$a, $b', 'return ($a->count > $b->count);') );
648
649                         if ( 'DESC' == $order )
650                                 $tags = array_reverse( $tags, true );
651                 }
652         }
653
654         if ( $number > 0 )
655                 $tags = array_slice($tags, 0, $number);
656
657         $counts = array();
658         $real_counts = array(); // For the alt tag
659         foreach ( (array) $tags as $key => $tag ) {
660                 $real_counts[ $key ] = $tag->count;
661                 $counts[ $key ] = $topic_count_scale_callback($tag->count);
662         }
663
664         $min_count = min( $counts );
665         $spread = max( $counts ) - $min_count;
666         if ( $spread <= 0 )
667                 $spread = 1;
668         $font_spread = $largest - $smallest;
669         if ( $font_spread < 0 )
670                 $font_spread = 1;
671         $font_step = $font_spread / $spread;
672
673         $a = array();
674
675         foreach ( $tags as $key => $tag ) {
676                 $count = $counts[ $key ];
677                 $real_count = $real_counts[ $key ];
678                 $tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
679                 $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
680                 $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: " .
682                         ( $smallest + ( ( $count - $min_count ) * $font_step ) )
683                         . "$unit;'>$tag_name</a>";
684         }
685
686         switch ( $format ) :
687         case 'array' :
688                 $return =& $a;
689                 break;
690         case 'list' :
691                 $return = "<ul class='wp-tag-cloud'>\n\t<li>";
692                 $return .= join( "</li>\n\t<li>", $a );
693                 $return .= "</li>\n</ul>\n";
694                 break;
695         default :
696                 $return = join( $separator, $a );
697                 break;
698         endswitch;
699
700         if ( $filter )
701                 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
702         else
703                 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 );
724 }
725
726 //
727 // Helper functions
728 //
729
730 /**
731  * Retrieve HTML list content for category list.
732  *
733  * @uses Walker_Category to create HTML list content.
734  * @since 2.1.0
735  * @see Walker_Category::walk() for parameters and return description.
736  */
737 function walk_category_tree() {
738         $args = func_get_args();
739         // the user's options are the third parameter
740         if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
741                 $walker = new Walker_Category;
742         else
743                 $walker = $args[2]['walker'];
744
745         return call_user_func_array(array( &$walker, 'walk' ), $args );
746 }
747
748 /**
749  * Retrieve HTML dropdown (select) content for category list.
750  *
751  * @uses Walker_CategoryDropdown to create HTML dropdown content.
752  * @since 2.1.0
753  * @see Walker_CategoryDropdown::walk() for parameters and return description.
754  */
755 function walk_category_dropdown_tree() {
756         $args = func_get_args();
757         // the user's options are the third parameter
758         if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
759                 $walker = new Walker_CategoryDropdown;
760         else
761                 $walker = $args[2]['walker'];
762
763         return call_user_func_array(array( &$walker, 'walk' ), $args );
764 }
765
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
965 //
966 // Tags
967 //
968
969 /**
970  * Retrieve the link to the tag.
971  *
972  * @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
984         if ( is_wp_error( $tag ) )
985                 return '';
986
987         return $tag;
988 }
989
990 /**
991  * Retrieve the tags for a post.
992  *
993  * @since 2.3.0
994  * @uses apply_filters() Calls 'get_the_tags' filter on the list of post tags.
995  *
996  * @param int $id Post ID.
997  * @return array
998  */
999 function get_the_tags( $id = 0 ) {
1000         return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) );
1001 }
1002
1003 /**
1004  * Retrieve the tags for a post formatted as a string.
1005  *
1006  * @since 2.3.0
1007  * @uses apply_filters() Calls 'the_tags' filter on string list of tags.
1008  *
1009  * @param string $before Optional. Before tags.
1010  * @param string $sep Optional. Between tags.
1011  * @param string $after Optional. After tags.
1012  * @return string
1013  */
1014 function get_the_tag_list( $before = '', $sep = '', $after = '' ) {
1015         return apply_filters( 'the_tags', get_the_term_list( 0, 'post_tag', $before, $sep, $after ), $before, $sep, $after);
1016 }
1017
1018 /**
1019  * Retrieve the tags for a post.
1020  *
1021  * @since 2.3.0
1022  *
1023  * @param string $before Optional. Before list.
1024  * @param string $sep Optional. Separate items using this.
1025  * @param string $after Optional. After list.
1026  * @return string
1027  */
1028 function the_tags( $before = null, $sep = ', ', $after = '' ) {
1029         if ( null === $before )
1030                 $before = __('Tags: ');
1031         echo get_the_tag_list($before, $sep, $after);
1032 }
1033
1034 /**
1035  * Retrieve tag description.
1036  *
1037  * @since 2.8
1038  *
1039  * @param int $tag Optional. Tag ID. Will use global tag ID by default.
1040  * @return string Tag description, available.
1041  */
1042 function tag_description( $tag = 0 ) {
1043         return term_description( $tag );
1044 }
1045
1046 /**
1047  * Retrieve term description.
1048  *
1049  * @since 2.8
1050  *
1051  * @param int $term Optional. Term ID. Will use global term ID by default.
1052  * @return string Term description, available.
1053  */
1054 function term_description( $term = 0, $taxonomy = 'post_tag' ) {
1055         if ( !$term && ( is_tax() || is_tag() || is_category() ) ) {
1056                 $term = get_queried_object();
1057                 $taxonomy = $term->taxonomy;
1058                 $term = $term->term_id;
1059         }
1060         $description = get_term_field( 'description', $term, $taxonomy );
1061         return is_wp_error( $description ) ? '' : $description;
1062 }
1063
1064 /**
1065  * Retrieve the terms of the taxonomy that are attached to the post.
1066  *
1067  * This function can only be used within the loop.
1068  *
1069  * @since 2.5.0
1070  *
1071  * @param int $id Post ID. Is not optional.
1072  * @param string $taxonomy Taxonomy name.
1073  * @return array|bool False on failure. Array of term objects on success.
1074  */
1075 function get_the_terms( $id = 0, $taxonomy ) {
1076         global $post;
1077
1078         $id = (int) $id;
1079
1080         if ( !$id ) {
1081                 if ( !$post->ID )
1082                         return false;
1083                 else
1084                         $id = (int) $post->ID;
1085         }
1086
1087         $terms = get_object_term_cache( $id, $taxonomy );
1088         if ( false === $terms ) {
1089                 $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 );
1094
1095         if ( empty( $terms ) )
1096                 return false;
1097
1098         return $terms;
1099 }
1100
1101 /**
1102  * Retrieve a post's terms as a list with specified format.
1103  *
1104  * @since 2.5.0
1105  *
1106  * @param int $id Post ID.
1107  * @param string $taxonomy Taxonomy name.
1108  * @param string $before Optional. Before list.
1109  * @param string $sep Optional. Separate items using this.
1110  * @param string $after Optional. After list.
1111  * @return string
1112  */
1113 function get_the_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after = '' ) {
1114         $terms = get_the_terms( $id, $taxonomy );
1115
1116         if ( is_wp_error( $terms ) )
1117                 return $terms;
1118
1119         if ( empty( $terms ) )
1120                 return false;
1121
1122         foreach ( $terms as $term ) {
1123                 $link = get_term_link( $term, $taxonomy );
1124                 if ( is_wp_error( $link ) )
1125                         return $link;
1126                 $term_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>';
1127         }
1128
1129         $term_links = apply_filters( "term_links-$taxonomy", $term_links );
1130
1131         return $before . join( $sep, $term_links ) . $after;
1132 }
1133
1134 /**
1135  * Display the terms in a list.
1136  *
1137  * @since 2.5.0
1138  *
1139  * @param int $id Post ID.
1140  * @param string $taxonomy Taxonomy name.
1141  * @param string $before Optional. Before list.
1142  * @param string $sep Optional. Separate items using this.
1143  * @param string $after Optional. After list.
1144  * @return null|bool False on WordPress error. Returns null when displaying.
1145  */
1146 function the_terms( $id = 0, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
1147         $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after );
1148
1149         if ( is_wp_error( $term_list ) )
1150                 return false;
1151
1152         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 );
1167 }
1168
1169 /**
1170  * Check if the current post has any of given tags.
1171  *
1172  * The given tags are checked against the post's tags' term_ids, names and slugs.
1173  * Tags given as integers will only be checked against the post's tags' term_ids.
1174  * If no tags are given, determines if post has any tags.
1175  *
1176  * 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)
1177  * Prior to v2.7, this function could only be used in the WordPress Loop.
1178  * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
1179  *
1180  * @since 2.6.0
1181  *
1182  * @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 )
1208                 return false;
1209
1210         $r = is_object_in_term( $post->ID, $taxonomy, $term );
1211         if ( is_wp_error( $r ) )
1212                 return false;
1213
1214         return $r;
1215 }
1216
1217 ?>