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