]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/category-template.php
WordPress 4.3
[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 ) ) . '">'.$name.'</a>' . $separator;
60         else
61                 $chain .= $name.$separator;
62         return $chain;
63 }
64
65 /**
66  * Retrieve post categories.
67  *
68  * This tag may be used outside The Loop by passing a post id as the parameter.
69  *
70  * Note: This function only returns results from the default "category" taxonomy.
71  * For custom taxonomies use get_the_terms().
72  *
73  * @since 0.71
74  *
75  * @param int $id Optional, default to current post ID. The post ID.
76  * @return array Array of objects, one for each category assigned to the post.
77  */
78 function get_the_category( $id = false ) {
79         $categories = get_the_terms( $id, 'category' );
80         if ( ! $categories || is_wp_error( $categories ) )
81                 $categories = array();
82
83         $categories = array_values( $categories );
84
85         foreach ( array_keys( $categories ) as $key ) {
86                 _make_cat_compat( $categories[$key] );
87         }
88
89         /**
90          * Filter the array of categories to return for a post.
91          *
92          * @since 3.1.0
93          *
94          * @param array $categories An array of categories to return for the post.
95          */
96         return apply_filters( 'get_the_categories', $categories );
97 }
98
99 /**
100  * Sort categories by name.
101  *
102  * Used by usort() as a callback, should not be used directly. Can actually be
103  * used to sort any term object.
104  *
105  * @since 2.3.0
106  * @access private
107  *
108  * @param object $a
109  * @param object $b
110  * @return int
111  */
112 function _usort_terms_by_name( $a, $b ) {
113         return strcmp( $a->name, $b->name );
114 }
115
116 /**
117  * Sort categories by ID.
118  *
119  * Used by usort() as a callback, should not be used directly. Can actually be
120  * used to sort any term object.
121  *
122  * @since 2.3.0
123  * @access private
124  *
125  * @param object $a
126  * @param object $b
127  * @return int
128  */
129 function _usort_terms_by_ID( $a, $b ) {
130         if ( $a->term_id > $b->term_id )
131                 return 1;
132         elseif ( $a->term_id < $b->term_id )
133                 return -1;
134         else
135                 return 0;
136 }
137
138 /**
139  * Retrieve category name based on category ID.
140  *
141  * @since 0.71
142  *
143  * @param int $cat_ID Category ID.
144  * @return string|WP_Error Category name on success, WP_Error on failure.
145  */
146 function get_the_category_by_ID( $cat_ID ) {
147         $cat_ID = (int) $cat_ID;
148         $category = get_term( $cat_ID, 'category' );
149
150         if ( is_wp_error( $category ) )
151                 return $category;
152
153         return ( $category ) ? $category->name : '';
154 }
155
156 /**
157  * Retrieve category list in either HTML list or custom format.
158  *
159  * @since 1.5.1
160  *
161  * @global WP_Rewrite $wp_rewrite
162  *
163  * @param string $separator Optional, default is empty string. Separator for between the categories.
164  * @param string $parents Optional. How to display the parents.
165  * @param int $post_id Optional. Post ID to retrieve categories.
166  * @return string
167  */
168 function get_the_category_list( $separator = '', $parents='', $post_id = false ) {
169         global $wp_rewrite;
170         if ( ! is_object_in_taxonomy( get_post_type( $post_id ), 'category' ) ) {
171                 /** This filter is documented in wp-includes/category-template.php */
172                 return apply_filters( 'the_category', '', $separator, $parents );
173         }
174
175         $categories = get_the_category( $post_id );
176         if ( empty( $categories ) ) {
177                 /** This filter is documented in wp-includes/category-template.php */
178                 return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents );
179         }
180
181         $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
182
183         $thelist = '';
184         if ( '' == $separator ) {
185                 $thelist .= '<ul class="post-categories">';
186                 foreach ( $categories as $category ) {
187                         $thelist .= "\n\t<li>";
188                         switch ( strtolower( $parents ) ) {
189                                 case 'multiple':
190                                         if ( $category->parent )
191                                                 $thelist .= get_category_parents( $category->parent, true, $separator );
192                                         $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
193                                         break;
194                                 case 'single':
195                                         $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '"  ' . $rel . '>';
196                                         if ( $category->parent )
197                                                 $thelist .= get_category_parents( $category->parent, false, $separator );
198                                         $thelist .= $category->name.'</a></li>';
199                                         break;
200                                 case '':
201                                 default:
202                                         $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
203                         }
204                 }
205                 $thelist .= '</ul>';
206         } else {
207                 $i = 0;
208                 foreach ( $categories as $category ) {
209                         if ( 0 < $i )
210                                 $thelist .= $separator;
211                         switch ( strtolower( $parents ) ) {
212                                 case 'multiple':
213                                         if ( $category->parent )
214                                                 $thelist .= get_category_parents( $category->parent, true, $separator );
215                                         $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a>';
216                                         break;
217                                 case 'single':
218                                         $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>';
219                                         if ( $category->parent )
220                                                 $thelist .= get_category_parents( $category->parent, false, $separator );
221                                         $thelist .= "$category->name</a>";
222                                         break;
223                                 case '':
224                                 default:
225                                         $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a>';
226                         }
227                         ++$i;
228                 }
229         }
230
231         /**
232          * Filter the category or list of categories.
233          *
234          * @since 1.2.0
235          *
236          * @param array  $thelist   List of categories for the current post.
237          * @param string $separator Separator used between the categories.
238          * @param string $parents   How to display the category parents. Accepts 'multiple',
239          *                          'single', or empty.
240          */
241         return apply_filters( 'the_category', $thelist, $separator, $parents );
242 }
243
244 /**
245  * Check if the current post in within any of the given categories.
246  *
247  * The given categories are checked against the post's categories' term_ids, names and slugs.
248  * Categories given as integers will only be checked against the post's categories' term_ids.
249  *
250  * Prior to v2.5 of WordPress, category names were not supported.
251  * Prior to v2.7, category slugs were not supported.
252  * Prior to v2.7, only one category could be compared: in_category( $single_category ).
253  * Prior to v2.7, this function could only be used in the WordPress Loop.
254  * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
255  *
256  * @since 1.2.0
257  *
258  * @param int|string|array $category Category ID, name or slug, or array of said.
259  * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
260  * @return bool True if the current post is in any of the given categories.
261  */
262 function in_category( $category, $post = null ) {
263         if ( empty( $category ) )
264                 return false;
265
266         return has_category( $category, $post );
267 }
268
269 /**
270  * Display the category list for the post.
271  *
272  * @since 0.71
273  *
274  * @param string $separator Optional, default is empty string. Separator for between the categories.
275  * @param string $parents Optional. How to display the parents.
276  * @param int $post_id Optional. Post ID to retrieve categories.
277  */
278 function the_category( $separator = '', $parents='', $post_id = false ) {
279         echo get_the_category_list( $separator, $parents, $post_id );
280 }
281
282 /**
283  * Retrieve category description.
284  *
285  * @since 1.0.0
286  *
287  * @param int $category Optional. Category ID. Will use global category ID by default.
288  * @return string Category description, available.
289  */
290 function category_description( $category = 0 ) {
291         return term_description( $category, 'category' );
292 }
293
294 /**
295  * Display or retrieve the HTML dropdown list of categories.
296  *
297  * The 'hierarchical' argument, which is disabled by default, will override the
298  * depth argument, unless it is true. When the argument is false, it will
299  * display all of the categories. When it is enabled it will use the value in
300  * the 'depth' argument.
301  *
302  * @since 2.1.0
303  * @since 4.2.0 Introduced the `value_field` argument.
304  *
305  * @param string|array $args {
306  *     Optional. Array or string of arguments to generate a categories drop-down element.
307  *
308  *     @type string       $show_option_all   Text to display for showing all categories. Default empty.
309  *     @type string       $show_option_none  Text to display for showing no categories. Default empty.
310  *     @type string       $option_none_value Value to use when no category is selected. Default empty.
311  *     @type string       $orderby           Which column to use for ordering categories. See get_terms() for a list
312  *                                           of accepted values. Default 'id' (term_id).
313  *     @type string       $order             Whether to order terms in ascending or descending order. Accepts 'ASC'
314  *                                           or 'DESC'. Default 'ASC'.
315  *     @type bool         $pad_counts        See get_terms() for an argument description. Default false.
316  *     @type bool|int     $show_count        Whether to include post counts. Accepts 0, 1, or their bool equivalents.
317  *                                           Default 0.
318  *     @type bool|int     $hide_empty        Whether to hide categories that don't have any posts. Accepts 0, 1, or
319  *                                           their bool equivalents. Default 1.
320  *     @type int          $child_of          Term ID to retrieve child terms of. See get_terms(). Default 0.
321  *     @type array|string $exclude           Array or comma/space-separated string of term ids to exclude.
322  *                                           If `$include` is non-empty, `$exclude` is ignored. Default empty array.
323  *     @type bool|int     $echo              Whether to echo or return the generated markup. Accepts 0, 1, or their
324  *                                           bool equivalents. Default 1.
325  *     @type bool|int     $hierarchical      Whether to traverse the taxonomy hierarchy. Accepts 0, 1, or their bool
326  *                                           equivalents. Default 0.
327  *     @type int          $depth             Maximum depth. Default 0.
328  *     @type int          $tab_index         Tab index for the select element. Default 0 (no tabindex).
329  *     @type string       $name              Value for the 'name' attribute of the select element. Default 'cat'.
330  *     @type string       $id                Value for the 'id' attribute of the select element. Defaults to the value
331  *                                           of `$name`.
332  *     @type string       $class             Value for the 'class' attribute of the select element. Default 'postform'.
333  *     @type int|string   $selected          Value of the option that should be selected. Default 0.
334  *     @type string       $value_field       Term field that should be used to populate the 'value' attribute
335  *                                           of the option elements. Accepts any valid term field: 'term_id', 'name',
336  *                                           'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description',
337  *                                           'parent', 'count'. Default 'term_id'.
338  *     @type string       $taxonomy          Name of the category to retrieve. Default 'category'.
339  *     @type bool         $hide_if_empty     True to skip generating markup if no categories are found.
340  *                                           Default false (create select element even if no categories are found).
341  * }
342  * @return string HTML content only if 'echo' argument is 0.
343  */
344 function wp_dropdown_categories( $args = '' ) {
345         $defaults = array(
346                 'show_option_all' => '', 'show_option_none' => '',
347                 'orderby' => 'id', 'order' => 'ASC',
348                 'show_count' => 0,
349                 'hide_empty' => 1, 'child_of' => 0,
350                 'exclude' => '', 'echo' => 1,
351                 'selected' => 0, 'hierarchical' => 0,
352                 'name' => 'cat', 'id' => '',
353                 'class' => 'postform', 'depth' => 0,
354                 'tab_index' => 0, 'taxonomy' => 'category',
355                 'hide_if_empty' => false, 'option_none_value' => -1,
356                 'value_field' => 'term_id',
357         );
358
359         $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
360
361         // Back compat.
362         if ( isset( $args['type'] ) && 'link' == $args['type'] ) {
363                 _deprecated_argument( __FUNCTION__, '3.0', '' );
364                 $args['taxonomy'] = 'link_category';
365         }
366
367         $r = wp_parse_args( $args, $defaults );
368         $option_none_value = $r['option_none_value'];
369
370         if ( ! isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
371                 $r['pad_counts'] = true;
372         }
373
374         $tab_index = $r['tab_index'];
375
376         $tab_index_attribute = '';
377         if ( (int) $tab_index > 0 ) {
378                 $tab_index_attribute = " tabindex=\"$tab_index\"";
379         }
380
381         // Avoid clashes with the 'name' param of get_terms().
382         $get_terms_args = $r;
383         unset( $get_terms_args['name'] );
384         $categories = get_terms( $r['taxonomy'], $get_terms_args );
385
386         $name = esc_attr( $r['name'] );
387         $class = esc_attr( $r['class'] );
388         $id = $r['id'] ? esc_attr( $r['id'] ) : $name;
389
390         if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) {
391                 $output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
392         } else {
393                 $output = '';
394         }
395         if ( empty( $categories ) && ! $r['hide_if_empty'] && ! empty( $r['show_option_none'] ) ) {
396
397                 /**
398                  * Filter a taxonomy drop-down display element.
399                  *
400                  * A variety of taxonomy drop-down display elements can be modified
401                  * just prior to display via this filter. Filterable arguments include
402                  * 'show_option_none', 'show_option_all', and various forms of the
403                  * term name.
404                  *
405                  * @since 1.2.0
406                  *
407                  * @see wp_dropdown_categories()
408                  *
409                  * @param string $element Taxonomy element to list.
410                  */
411                 $show_option_none = apply_filters( 'list_cats', $r['show_option_none'] );
412                 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "' selected='selected'>$show_option_none</option>\n";
413         }
414
415         if ( ! empty( $categories ) ) {
416
417                 if ( $r['show_option_all'] ) {
418
419                         /** This filter is documented in wp-includes/category-template.php */
420                         $show_option_all = apply_filters( 'list_cats', $r['show_option_all'] );
421                         $selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : '';
422                         $output .= "\t<option value='0'$selected>$show_option_all</option>\n";
423                 }
424
425                 if ( $r['show_option_none'] ) {
426
427                         /** This filter is documented in wp-includes/category-template.php */
428                         $show_option_none = apply_filters( 'list_cats', $r['show_option_none'] );
429                         $selected = selected( $option_none_value, $r['selected'], false );
430                         $output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$selected>$show_option_none</option>\n";
431                 }
432
433                 if ( $r['hierarchical'] ) {
434                         $depth = $r['depth'];  // Walk the full depth.
435                 } else {
436                         $depth = -1; // Flat.
437                 }
438                 $output .= walk_category_dropdown_tree( $categories, $depth, $r );
439         }
440
441         if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) {
442                 $output .= "</select>\n";
443         }
444         /**
445          * Filter the taxonomy drop-down output.
446          *
447          * @since 2.1.0
448          *
449          * @param string $output HTML output.
450          * @param array  $r      Arguments used to build the drop-down.
451          */
452         $output = apply_filters( 'wp_dropdown_cats', $output, $r );
453
454         if ( $r['echo'] ) {
455                 echo $output;
456         }
457         return $output;
458 }
459
460 /**
461  * Display or retrieve the HTML list of categories.
462  *
463  * The list of arguments is below:
464  *     'show_option_all' (string) - Text to display for showing all categories.
465  *     'orderby' (string) default is 'ID' - What column to use for ordering the
466  * categories.
467  *     'order' (string) default is 'ASC' - What direction to order categories.
468  *     'show_count' (bool|int) default is 0 - Whether to show how many posts are
469  * in the category.
470  *     'hide_empty' (bool|int) default is 1 - Whether to hide categories that
471  * don't have any posts attached to them.
472  *     'use_desc_for_title' (bool|int) default is 1 - Whether to use the
473  * category description as the title attribute.
474  *     'feed' - See {@link get_categories()}.
475  *     'feed_type' - See {@link get_categories()}.
476  *     'feed_image' - See {@link get_categories()}.
477  *     'child_of' (int) default is 0 - See {@link get_categories()}.
478  *     'exclude' (string) - See {@link get_categories()}.
479  *     'exclude_tree' (string) - See {@link get_categories()}.
480  *     'echo' (bool|int) default is 1 - Whether to display or retrieve content.
481  *     'current_category' (int) - See {@link get_categories()}.
482  *     'hierarchical' (bool) - See {@link get_categories()}.
483  *     'title_li' (string) - See {@link get_categories()}.
484  *     'depth' (int) - The max depth.
485  *
486  * @since 2.1.0
487  *
488  * @param string|array $args Optional. Override default arguments.
489  * @return false|string HTML content only if 'echo' argument is 0.
490  */
491 function wp_list_categories( $args = '' ) {
492         $defaults = array(
493                 'show_option_all' => '', 'show_option_none' => __('No categories'),
494                 'orderby' => 'name', 'order' => 'ASC',
495                 'style' => 'list',
496                 'show_count' => 0, 'hide_empty' => 1,
497                 'use_desc_for_title' => 1, 'child_of' => 0,
498                 'feed' => '', 'feed_type' => '',
499                 'feed_image' => '', 'exclude' => '',
500                 'exclude_tree' => '', 'current_category' => 0,
501                 'hierarchical' => true, 'title_li' => __( 'Categories' ),
502                 'echo' => 1, 'depth' => 0,
503                 'taxonomy' => 'category'
504         );
505
506         $r = wp_parse_args( $args, $defaults );
507
508         if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] )
509                 $r['pad_counts'] = true;
510
511         if ( true == $r['hierarchical'] ) {
512                 $r['exclude_tree'] = $r['exclude'];
513                 $r['exclude'] = '';
514         }
515
516         if ( ! isset( $r['class'] ) )
517                 $r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy'];
518
519         if ( ! taxonomy_exists( $r['taxonomy'] ) ) {
520                 return false;
521         }
522
523         $show_option_all = $r['show_option_all'];
524         $show_option_none = $r['show_option_none'];
525
526         $categories = get_categories( $r );
527
528         $output = '';
529         if ( $r['title_li'] && 'list' == $r['style'] ) {
530                 $output = '<li class="' . esc_attr( $r['class'] ) . '">' . $r['title_li'] . '<ul>';
531         }
532         if ( empty( $categories ) ) {
533                 if ( ! empty( $show_option_none ) ) {
534                         if ( 'list' == $r['style'] ) {
535                                 $output .= '<li class="cat-item-none">' . $show_option_none . '</li>';
536                         } else {
537                                 $output .= $show_option_none;
538                         }
539                 }
540         } else {
541                 if ( ! empty( $show_option_all ) ) {
542
543                         $posts_page = '';
544
545                         // For taxonomies that belong only to custom post types, point to a valid archive.
546                         $taxonomy_object = get_taxonomy( $r['taxonomy'] );
547                         if ( ! in_array( 'post', $taxonomy_object->object_type ) && ! in_array( 'page', $taxonomy_object->object_type ) ) {
548                                 foreach ( $taxonomy_object->object_type as $object_type ) {
549                                         $_object_type = get_post_type_object( $object_type );
550
551                                         // Grab the first one.
552                                         if ( ! empty( $_object_type->has_archive ) ) {
553                                                 $posts_page = get_post_type_archive_link( $object_type );
554                                                 break;
555                                         }
556                                 }
557                         }
558
559                         // Fallback for the 'All' link is the front page.
560                         if ( ! $posts_page ) {
561                                 $posts_page = 'page' == get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) ? get_permalink( get_option( 'page_for_posts' ) ) : home_url( '/' );
562                         }
563
564                         $posts_page = esc_url( $posts_page );
565                         if ( 'list' == $r['style'] ) {
566                                 $output .= "<li class='cat-item-all'><a href='$posts_page'>$show_option_all</a></li>";
567                         } else {
568                                 $output .= "<a href='$posts_page'>$show_option_all</a>";
569                         }
570                 }
571
572                 if ( empty( $r['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) {
573                         $current_term_object = get_queried_object();
574                         if ( $current_term_object && $r['taxonomy'] === $current_term_object->taxonomy ) {
575                                 $r['current_category'] = get_queried_object_id();
576                         }
577                 }
578
579                 if ( $r['hierarchical'] ) {
580                         $depth = $r['depth'];
581                 } else {
582                         $depth = -1; // Flat.
583                 }
584                 $output .= walk_category_tree( $categories, $depth, $r );
585         }
586
587         if ( $r['title_li'] && 'list' == $r['style'] )
588                 $output .= '</ul></li>';
589
590         /**
591          * Filter the HTML output of a taxonomy list.
592          *
593          * @since 2.1.0
594          *
595          * @param string $output HTML output.
596          * @param array  $args   An array of taxonomy-listing arguments.
597          */
598         $html = apply_filters( 'wp_list_categories', $output, $args );
599
600         if ( $r['echo'] ) {
601                 echo $html;
602         } else {
603                 return $html;
604         }
605 }
606
607 /**
608  * Display tag cloud.
609  *
610  * The text size is set by the 'smallest' and 'largest' arguments, which will
611  * use the 'unit' argument value for the CSS text size unit. The 'format'
612  * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
613  * 'format' argument will separate tags with spaces. The list value for the
614  * 'format' argument will format the tags in a UL HTML list. The array value for
615  * the 'format' argument will return in PHP array type format.
616  *
617  * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
618  * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'.
619  *
620  * The 'number' argument is how many tags to return. By default, the limit will
621  * be to return the top 45 tags in the tag cloud list.
622  *
623  * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the
624  * text for the tooltip of the tag link.
625  *
626  * The 'topic_count_text_callback' argument is a function, which given the count
627  * of the posts with that tag returns a text for the tooltip of the tag link.
628  *
629  * The 'post_type' argument is used only when 'link' is set to 'edit'. It determines the post_type
630  * passed to edit.php for the popular tags edit links.
631  *
632  * The 'exclude' and 'include' arguments are used for the {@link get_tags()}
633  * function. Only one should be used, because only one will be used and the
634  * other ignored, if they are both set.
635  *
636  * @since 2.3.0
637  *
638  * @param array|string|null $args Optional. Override default arguments.
639  * @return void|array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument.
640  *                    Otherwise, this function outputs the tag cloud.
641  */
642 function wp_tag_cloud( $args = '' ) {
643         $defaults = array(
644                 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
645                 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
646                 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'post_type' => '', 'echo' => true
647         );
648         $args = wp_parse_args( $args, $defaults );
649
650         $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
651
652         if ( empty( $tags ) || is_wp_error( $tags ) )
653                 return;
654
655         foreach ( $tags as $key => $tag ) {
656                 if ( 'edit' == $args['link'] )
657                         $link = get_edit_term_link( $tag->term_id, $tag->taxonomy, $args['post_type'] );
658                 else
659                         $link = get_term_link( intval($tag->term_id), $tag->taxonomy );
660                 if ( is_wp_error( $link ) )
661                         return;
662
663                 $tags[ $key ]->link = $link;
664                 $tags[ $key ]->id = $tag->term_id;
665         }
666
667         $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
668
669         /**
670          * Filter the tag cloud output.
671          *
672          * @since 2.3.0
673          *
674          * @param string $return HTML output of the tag cloud.
675          * @param array  $args   An array of tag cloud arguments.
676          */
677         $return = apply_filters( 'wp_tag_cloud', $return, $args );
678
679         if ( 'array' == $args['format'] || empty($args['echo']) )
680                 return $return;
681
682         echo $return;
683 }
684
685 /**
686  * Default topic count scaling for tag links
687  *
688  * @param int $count number of posts with that tag
689  * @return int scaled count
690  */
691 function default_topic_count_scale( $count ) {
692         return round(log10($count + 1) * 100);
693 }
694
695 /**
696  * Generates a tag cloud (heatmap) from provided data.
697  *
698  * The text size is set by the 'smallest' and 'largest' arguments, which will
699  * use the 'unit' argument value for the CSS text size unit. The 'format'
700  * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
701  * 'format' argument will separate tags with spaces. The list value for the
702  * 'format' argument will format the tags in a UL HTML list. The array value for
703  * the 'format' argument will return in PHP array type format.
704  *
705  * The 'tag_cloud_sort' filter allows you to override the sorting.
706  * Passed to the filter: $tags array and $args array, has to return the $tags array
707  * after sorting it.
708  *
709  * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
710  * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or
711  * 'RAND'.
712  *
713  * The 'number' argument is how many tags to return. By default, the limit will
714  * be to return the entire tag cloud list.
715  *
716  * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the
717  * text for the tooltip of the tag link.
718  *
719  * The 'topic_count_text_callback' argument is a function, which given the count
720  * of the posts with that tag returns a text for the tooltip of the tag link.
721  *
722  * @todo Complete functionality.
723  * @since 2.3.0
724  *
725  * @param array $tags List of tags.
726  * @param string|array $args Optional, override default arguments.
727  * @return string|array Tag cloud as a string or an array, depending on 'format' argument.
728  */
729 function wp_generate_tag_cloud( $tags, $args = '' ) {
730         $defaults = array(
731                 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
732                 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
733                 'topic_count_text' => null, 'topic_count_text_callback' => null,
734                 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
735         );
736
737         $args = wp_parse_args( $args, $defaults );
738
739         $return = ( 'array' === $args['format'] ) ? array() : '';
740
741         if ( empty( $tags ) ) {
742                 return $return;
743         }
744
745         // Juggle topic count tooltips:
746         if ( isset( $args['topic_count_text'] ) ) {
747                 // First look for nooped plural support via topic_count_text.
748                 $translate_nooped_plural = $args['topic_count_text'];
749         } elseif ( ! empty( $args['topic_count_text_callback'] ) ) {
750                 // Look for the alternative callback style. Ignore the previous default.
751                 if ( $args['topic_count_text_callback'] === 'default_topic_count_text' ) {
752                         $translate_nooped_plural = _n_noop( '%s topic', '%s topics' );
753                 } else {
754                         $translate_nooped_plural = false;
755                 }
756         } elseif ( isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
757                 // If no callback exists, look for the old-style single_text and multiple_text arguments.
758                 $translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] );
759         } else {
760                 // This is the default for when no callback, plural, or argument is passed in.
761                 $translate_nooped_plural = _n_noop( '%s topic', '%s topics' );
762         }
763
764         /**
765          * Filter how the items in a tag cloud are sorted.
766          *
767          * @since 2.8.0
768          *
769          * @param array $tags Ordered array of terms.
770          * @param array $args An array of tag cloud arguments.
771          */
772         $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );
773         if ( empty( $tags_sorted ) ) {
774                 return $return;
775         }
776
777         if ( $tags_sorted !== $tags ) {
778                 $tags = $tags_sorted;
779                 unset( $tags_sorted );
780         } else {
781                 if ( 'RAND' === $args['order'] ) {
782                         shuffle( $tags );
783                 } else {
784                         // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
785                         if ( 'name' === $args['orderby'] ) {
786                                 uasort( $tags, '_wp_object_name_sort_cb' );
787                         } else {
788                                 uasort( $tags, '_wp_object_count_sort_cb' );
789                         }
790
791                         if ( 'DESC' === $args['order'] ) {
792                                 $tags = array_reverse( $tags, true );
793                         }
794                 }
795         }
796
797         if ( $args['number'] > 0 )
798                 $tags = array_slice( $tags, 0, $args['number'] );
799
800         $counts = array();
801         $real_counts = array(); // For the alt tag
802         foreach ( (array) $tags as $key => $tag ) {
803                 $real_counts[ $key ] = $tag->count;
804                 $counts[ $key ] = call_user_func( $args['topic_count_scale_callback'], $tag->count );
805         }
806
807         $min_count = min( $counts );
808         $spread = max( $counts ) - $min_count;
809         if ( $spread <= 0 )
810                 $spread = 1;
811         $font_spread = $args['largest'] - $args['smallest'];
812         if ( $font_spread < 0 )
813                 $font_spread = 1;
814         $font_step = $font_spread / $spread;
815
816         // Assemble the data that will be used to generate the tag cloud markup.
817         $tags_data = array();
818         foreach ( $tags as $key => $tag ) {
819                 $tag_id = isset( $tag->id ) ? $tag->id : $key;
820
821                 $count = $counts[ $key ];
822                 $real_count = $real_counts[ $key ];
823
824                 if ( $translate_nooped_plural ) {
825                         $title = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) );
826                 } else {
827                         $title = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args );
828                 }
829
830                 $tags_data[] = array(
831                         'id'         => $tag_id,
832                         'url'        => '#' != $tag->link ? $tag->link : '#',
833                         'name'       => $tag->name,
834                         'title'      => $title,
835                         'slug'       => $tag->slug,
836                         'real_count' => $real_count,
837                         'class'      => 'tag-link-' . $tag_id,
838                         'font_size'  => $args['smallest'] + ( $count - $min_count ) * $font_step,
839                 );
840         }
841
842         /**
843          * Filter the data used to generate the tag cloud.
844          *
845          * @since 4.3.0
846          *
847          * @param array $tags_data An array of term data for term used to generate the tag cloud.
848          */
849         $tags_data = apply_filters( 'wp_generate_tag_cloud_data', $tags_data );
850
851         $a = array();
852
853         // generate the output links array
854         foreach ( $tags_data as $key => $tag_data ) {
855                 $a[] = "<a href='" . esc_url( $tag_data['url'] ) . "' class='" . esc_attr( $tag_data['class'] ) . "' title='" . esc_attr( $tag_data['title'] ) . "' style='font-size: " . esc_attr( str_replace( ',', '.', $tag_data['font_size'] ) . $args['unit'] ) . ";'>" . esc_html( $tag_data['name'] ) . "</a>";
856         }
857
858         switch ( $args['format'] ) {
859                 case 'array' :
860                         $return =& $a;
861                         break;
862                 case 'list' :
863                         $return = "<ul class='wp-tag-cloud'>\n\t<li>";
864                         $return .= join( "</li>\n\t<li>", $a );
865                         $return .= "</li>\n</ul>\n";
866                         break;
867                 default :
868                         $return = join( $args['separator'], $a );
869                         break;
870         }
871
872         if ( $args['filter'] ) {
873                 /**
874                  * Filter the generated output of a tag cloud.
875                  *
876                  * The filter is only evaluated if a true value is passed
877                  * to the $filter argument in wp_generate_tag_cloud().
878                  *
879                  * @since 2.3.0
880                  *
881                  * @see wp_generate_tag_cloud()
882                  *
883                  * @param array|string $return String containing the generated HTML tag cloud output
884                  *                             or an array of tag links if the 'format' argument
885                  *                             equals 'array'.
886                  * @param array        $tags   An array of terms used in the tag cloud.
887                  * @param array        $args   An array of wp_generate_tag_cloud() arguments.
888                  */
889                 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
890         }
891
892         else
893                 return $return;
894 }
895
896 /**
897  * Callback for comparing objects based on name
898  *
899  * @since 3.1.0
900  * @access private
901  * @return int
902  */
903 function _wp_object_name_sort_cb( $a, $b ) {
904         return strnatcasecmp( $a->name, $b->name );
905 }
906
907 /**
908  * Callback for comparing objects based on count
909  *
910  * @since 3.1.0
911  * @access private
912  * @return bool
913  */
914 function _wp_object_count_sort_cb( $a, $b ) {
915         return ( $a->count > $b->count );
916 }
917
918 //
919 // Helper functions
920 //
921
922 /**
923  * Retrieve HTML list content for category list.
924  *
925  * @uses Walker_Category to create HTML list content.
926  * @since 2.1.0
927  * @see Walker_Category::walk() for parameters and return description.
928  * @return string
929  */
930 function walk_category_tree() {
931         $args = func_get_args();
932         // the user's options are the third parameter
933         if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) {
934                 $walker = new Walker_Category;
935         } else {
936                 $walker = $args[2]['walker'];
937         }
938         return call_user_func_array( array( $walker, 'walk' ), $args );
939 }
940
941 /**
942  * Retrieve HTML dropdown (select) content for category list.
943  *
944  * @uses Walker_CategoryDropdown to create HTML dropdown content.
945  * @since 2.1.0
946  * @see Walker_CategoryDropdown::walk() for parameters and return description.
947  * @return string
948  */
949 function walk_category_dropdown_tree() {
950         $args = func_get_args();
951         // the user's options are the third parameter
952         if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) {
953                 $walker = new Walker_CategoryDropdown;
954         } else {
955                 $walker = $args[2]['walker'];
956         }
957         return call_user_func_array( array( $walker, 'walk' ), $args );
958 }
959
960 /**
961  * Create HTML list of categories.
962  *
963  * @package WordPress
964  * @since 2.1.0
965  * @uses Walker
966  */
967 class Walker_Category extends Walker {
968         /**
969          * What the class handles.
970          *
971          * @see Walker::$tree_type
972          * @since 2.1.0
973          * @var string
974          */
975         public $tree_type = 'category';
976
977         /**
978          * Database fields to use.
979          *
980          * @see Walker::$db_fields
981          * @since 2.1.0
982          * @todo Decouple this
983          * @var array
984          */
985         public $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
986
987         /**
988          * Starts the list before the elements are added.
989          *
990          * @see Walker::start_lvl()
991          *
992          * @since 2.1.0
993          *
994          * @param string $output Passed by reference. Used to append additional content.
995          * @param int    $depth  Depth of category. Used for tab indentation.
996          * @param array  $args   An array of arguments. Will only append content if style argument value is 'list'.
997          *                       @see wp_list_categories()
998          */
999         public function start_lvl( &$output, $depth = 0, $args = array() ) {
1000                 if ( 'list' != $args['style'] )
1001                         return;
1002
1003                 $indent = str_repeat("\t", $depth);
1004                 $output .= "$indent<ul class='children'>\n";
1005         }
1006
1007         /**
1008          * Ends the list of after the elements are added.
1009          *
1010          * @see Walker::end_lvl()
1011          *
1012          * @since 2.1.0
1013          *
1014          * @param string $output Passed by reference. Used to append additional content.
1015          * @param int    $depth  Depth of category. Used for tab indentation.
1016          * @param array  $args   An array of arguments. Will only append content if style argument value is 'list'.
1017          *                       @wsee wp_list_categories()
1018          */
1019         public function end_lvl( &$output, $depth = 0, $args = array() ) {
1020                 if ( 'list' != $args['style'] )
1021                         return;
1022
1023                 $indent = str_repeat("\t", $depth);
1024                 $output .= "$indent</ul>\n";
1025         }
1026
1027         /**
1028          * Start the element output.
1029          *
1030          * @see Walker::start_el()
1031          *
1032          * @since 2.1.0
1033          *
1034          * @param string $output   Passed by reference. Used to append additional content.
1035          * @param object $category Category data object.
1036          * @param int    $depth    Depth of category in reference to parents. Default 0.
1037          * @param array  $args     An array of arguments. @see wp_list_categories()
1038          * @param int    $id       ID of the current category.
1039          */
1040         public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
1041                 /** This filter is documented in wp-includes/category-template.php */
1042                 $cat_name = apply_filters(
1043                         'list_cats',
1044                         esc_attr( $category->name ),
1045                         $category
1046                 );
1047
1048                 // Don't generate an element if the category name is empty.
1049                 if ( ! $cat_name ) {
1050                         return;
1051                 }
1052
1053                 $link = '<a href="' . esc_url( get_term_link( $category ) ) . '" ';
1054                 if ( $args['use_desc_for_title'] && ! empty( $category->description ) ) {
1055                         /**
1056                          * Filter the category description for display.
1057                          *
1058                          * @since 1.2.0
1059                          *
1060                          * @param string $description Category description.
1061                          * @param object $category    Category object.
1062                          */
1063                         $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
1064                 }
1065
1066                 $link .= '>';
1067                 $link .= $cat_name . '</a>';
1068
1069                 if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
1070                         $link .= ' ';
1071
1072                         if ( empty( $args['feed_image'] ) ) {
1073                                 $link .= '(';
1074                         }
1075
1076                         $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $args['feed_type'] ) ) . '"';
1077
1078                         if ( empty( $args['feed'] ) ) {
1079                                 $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
1080                         } else {
1081                                 $alt = ' alt="' . $args['feed'] . '"';
1082                                 $name = $args['feed'];
1083                                 $link .= empty( $args['title'] ) ? '' : $args['title'];
1084                         }
1085
1086                         $link .= '>';
1087
1088                         if ( empty( $args['feed_image'] ) ) {
1089                                 $link .= $name;
1090                         } else {
1091                                 $link .= "<img src='" . $args['feed_image'] . "'$alt" . ' />';
1092                         }
1093                         $link .= '</a>';
1094
1095                         if ( empty( $args['feed_image'] ) ) {
1096                                 $link .= ')';
1097                         }
1098                 }
1099
1100                 if ( ! empty( $args['show_count'] ) ) {
1101                         $link .= ' (' . number_format_i18n( $category->count ) . ')';
1102                 }
1103                 if ( 'list' == $args['style'] ) {
1104                         $output .= "\t<li";
1105                         $css_classes = array(
1106                                 'cat-item',
1107                                 'cat-item-' . $category->term_id,
1108                         );
1109
1110                         if ( ! empty( $args['current_category'] ) ) {
1111                                 $_current_category = get_term( $args['current_category'], $category->taxonomy );
1112                                 if ( $category->term_id == $args['current_category'] ) {
1113                                         $css_classes[] = 'current-cat';
1114                                 } elseif ( $category->term_id == $_current_category->parent ) {
1115                                         $css_classes[] = 'current-cat-parent';
1116                                 }
1117                         }
1118
1119                         /**
1120                          * Filter the list of CSS classes to include with each category in the list.
1121                          *
1122                          * @since 4.2.0
1123                          *
1124                          * @see wp_list_categories()
1125                          *
1126                          * @param array  $css_classes An array of CSS classes to be applied to each list item.
1127                          * @param object $category    Category data object.
1128                          * @param int    $depth       Depth of page, used for padding.
1129                          * @param array  $args        An array of wp_list_categories() arguments.
1130                          */
1131                         $css_classes = implode( ' ', apply_filters( 'category_css_class', $css_classes, $category, $depth, $args ) );
1132
1133                         $output .=  ' class="' . $css_classes . '"';
1134                         $output .= ">$link\n";
1135                 } else {
1136                         $output .= "\t$link<br />\n";
1137                 }
1138         }
1139
1140         /**
1141          * Ends the element output, if needed.
1142          *
1143          * @see Walker::end_el()
1144          *
1145          * @since 2.1.0
1146          *
1147          * @param string $output Passed by reference. Used to append additional content.
1148          * @param object $page   Not used.
1149          * @param int    $depth  Depth of category. Not used.
1150          * @param array  $args   An array of arguments. Only uses 'list' for whether should append to output. @see wp_list_categories()
1151          */
1152         public function end_el( &$output, $page, $depth = 0, $args = array() ) {
1153                 if ( 'list' != $args['style'] )
1154                         return;
1155
1156                 $output .= "</li>\n";
1157         }
1158
1159 }
1160
1161 /**
1162  * Create HTML dropdown list of Categories.
1163  *
1164  * @package WordPress
1165  * @since 2.1.0
1166  * @uses Walker
1167  */
1168 class Walker_CategoryDropdown extends Walker {
1169         /**
1170          * @see Walker::$tree_type
1171          * @since 2.1.0
1172          * @var string
1173          */
1174         public $tree_type = 'category';
1175
1176         /**
1177          * @see Walker::$db_fields
1178          * @since 2.1.0
1179          * @todo Decouple this
1180          * @var array
1181          */
1182         public $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
1183
1184         /**
1185          * Start the element output.
1186          *
1187          * @see Walker::start_el()
1188          * @since 2.1.0
1189          *
1190          * @param string $output   Passed by reference. Used to append additional content.
1191          * @param object $category Category data object.
1192          * @param int    $depth    Depth of category. Used for padding.
1193          * @param array  $args     Uses 'selected', 'show_count', and 'value_field' keys, if they exist.
1194          *                         See {@see wp_dropdown_categories()}.
1195          */
1196         public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
1197                 $pad = str_repeat('&nbsp;', $depth * 3);
1198
1199                 /** This filter is documented in wp-includes/category-template.php */
1200                 $cat_name = apply_filters( 'list_cats', $category->name, $category );
1201
1202                 if ( isset( $args['value_field'] ) && isset( $category->{$args['value_field']} ) ) {
1203                         $value_field = $args['value_field'];
1204                 } else {
1205                         $value_field = 'term_id';
1206                 }
1207
1208                 $output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $category->{$value_field} ) . "\"";
1209
1210                 if ( $category->{$value_field} == $args['selected'] )
1211                         $output .= ' selected="selected"';
1212                 $output .= '>';
1213                 $output .= $pad.$cat_name;
1214                 if ( $args['show_count'] )
1215                         $output .= '&nbsp;&nbsp;('. number_format_i18n( $category->count ) .')';
1216                 $output .= "</option>\n";
1217         }
1218 }
1219
1220 //
1221 // Tags
1222 //
1223
1224 /**
1225  * Retrieve the link to the tag.
1226  *
1227  * @since 2.3.0
1228  * @see get_term_link()
1229  *
1230  * @param int|object $tag Tag ID or object.
1231  * @return string Link on success, empty string if tag does not exist.
1232  */
1233 function get_tag_link( $tag ) {
1234         if ( ! is_object( $tag ) )
1235                 $tag = (int) $tag;
1236
1237         $tag = get_term_link( $tag, 'post_tag' );
1238
1239         if ( is_wp_error( $tag ) )
1240                 return '';
1241
1242         return $tag;
1243 }
1244
1245 /**
1246  * Retrieve the tags for a post.
1247  *
1248  * @since 2.3.0
1249  *
1250  * @param int $id Post ID.
1251  * @return array|false|WP_Error Array of tag objects on success, false on failure.
1252  */
1253 function get_the_tags( $id = 0 ) {
1254
1255         /**
1256          * Filter the array of tags for the given post.
1257          *
1258          * @since 2.3.0
1259          *
1260          * @see get_the_terms()
1261          *
1262          * @param array $terms An array of tags for the given post.
1263          */
1264         return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) );
1265 }
1266
1267 /**
1268  * Retrieve the tags for a post formatted as a string.
1269  *
1270  * @since 2.3.0
1271  *
1272  * @param string $before Optional. Before tags.
1273  * @param string $sep Optional. Between tags.
1274  * @param string $after Optional. After tags.
1275  * @param int $id Optional. Post ID. Defaults to the current post.
1276  * @return string|false|WP_Error A list of tags on success, false if there are no terms, WP_Error on failure.
1277  */
1278 function get_the_tag_list( $before = '', $sep = '', $after = '', $id = 0 ) {
1279
1280         /**
1281          * Filter the tags list for a given post.
1282          *
1283          * @since 2.3.0
1284          *
1285          * @param string $tag_list List of tags.
1286          * @param string $before   String to use before tags.
1287          * @param string $sep      String to use between the tags.
1288          * @param string $after    String to use after tags.
1289          * @param int    $id       Post ID.
1290          */
1291         return apply_filters( 'the_tags', get_the_term_list( $id, 'post_tag', $before, $sep, $after ), $before, $sep, $after, $id );
1292 }
1293
1294 /**
1295  * Retrieve the tags for a post.
1296  *
1297  * @since 2.3.0
1298  *
1299  * @param string $before Optional. Before list.
1300  * @param string $sep Optional. Separate items using this.
1301  * @param string $after Optional. After list.
1302  */
1303 function the_tags( $before = null, $sep = ', ', $after = '' ) {
1304         if ( null === $before )
1305                 $before = __('Tags: ');
1306         echo get_the_tag_list($before, $sep, $after);
1307 }
1308
1309 /**
1310  * Retrieve tag description.
1311  *
1312  * @since 2.8.0
1313  *
1314  * @param int $tag Optional. Tag ID. Will use global tag ID by default.
1315  * @return string Tag description, available.
1316  */
1317 function tag_description( $tag = 0 ) {
1318         return term_description( $tag );
1319 }
1320
1321 /**
1322  * Retrieve term description.
1323  *
1324  * @since 2.8.0
1325  *
1326  * @param int $term Optional. Term ID. Will use global term ID by default.
1327  * @param string $taxonomy Optional taxonomy name. Defaults to 'post_tag'.
1328  * @return string Term description, available.
1329  */
1330 function term_description( $term = 0, $taxonomy = 'post_tag' ) {
1331         if ( ! $term && ( is_tax() || is_tag() || is_category() ) ) {
1332                 $term = get_queried_object();
1333                 if ( $term ) {
1334                         $taxonomy = $term->taxonomy;
1335                         $term = $term->term_id;
1336                 }
1337         }
1338         $description = get_term_field( 'description', $term, $taxonomy );
1339         return is_wp_error( $description ) ? '' : $description;
1340 }
1341
1342 /**
1343  * Retrieve the terms of the taxonomy that are attached to the post.
1344  *
1345  * @since 2.5.0
1346  *
1347  * @param int|object $post Post ID or object.
1348  * @param string $taxonomy Taxonomy name.
1349  * @return array|false|WP_Error Array of term objects on success, false if there are no terms
1350  *                              or the post does not exist, WP_Error on failure.
1351  */
1352 function get_the_terms( $post, $taxonomy ) {
1353         if ( ! $post = get_post( $post ) )
1354                 return false;
1355
1356         $terms = get_object_term_cache( $post->ID, $taxonomy );
1357         if ( false === $terms ) {
1358                 $terms = wp_get_object_terms( $post->ID, $taxonomy );
1359                 wp_cache_add($post->ID, $terms, $taxonomy . '_relationships');
1360         }
1361
1362         /**
1363          * Filter the list of terms attached to the given post.
1364          *
1365          * @since 3.1.0
1366          *
1367          * @param array|WP_Error $terms    List of attached terms, or WP_Error on failure.
1368          * @param int            $post_id  Post ID.
1369          * @param string         $taxonomy Name of the taxonomy.
1370          */
1371         $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy );
1372
1373         if ( empty( $terms ) )
1374                 return false;
1375
1376         return $terms;
1377 }
1378
1379 /**
1380  * Retrieve a post's terms as a list with specified format.
1381  *
1382  * @since 2.5.0
1383  *
1384  * @param int $id Post ID.
1385  * @param string $taxonomy Taxonomy name.
1386  * @param string $before Optional. Before list.
1387  * @param string $sep Optional. Separate items using this.
1388  * @param string $after Optional. After list.
1389  * @return string|false|WP_Error A list of terms on success, false if there are no terms, WP_Error on failure.
1390  */
1391 function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
1392         $terms = get_the_terms( $id, $taxonomy );
1393
1394         if ( is_wp_error( $terms ) )
1395                 return $terms;
1396
1397         if ( empty( $terms ) )
1398                 return false;
1399
1400         $links = array();
1401
1402         foreach ( $terms as $term ) {
1403                 $link = get_term_link( $term, $taxonomy );
1404                 if ( is_wp_error( $link ) ) {
1405                         return $link;
1406                 }
1407                 $links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
1408         }
1409
1410         /**
1411          * Filter the term links for a given taxonomy.
1412          *
1413          * The dynamic portion of the filter name, `$taxonomy`, refers
1414          * to the taxonomy slug.
1415          *
1416          * @since 2.5.0
1417          *
1418          * @param array $links An array of term links.
1419          */
1420         $term_links = apply_filters( "term_links-$taxonomy", $links );
1421
1422         return $before . join( $sep, $term_links ) . $after;
1423 }
1424
1425 /**
1426  * Display the terms in a list.
1427  *
1428  * @since 2.5.0
1429  *
1430  * @param int $id Post ID.
1431  * @param string $taxonomy Taxonomy name.
1432  * @param string $before Optional. Before list.
1433  * @param string $sep Optional. Separate items using this.
1434  * @param string $after Optional. After list.
1435  * @return false|void False on WordPress error.
1436  */
1437 function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
1438         $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after );
1439
1440         if ( is_wp_error( $term_list ) )
1441                 return false;
1442
1443         /**
1444          * Filter the list of terms to display.
1445          *
1446          * @since 2.9.0
1447          *
1448          * @param array  $term_list List of terms to display.
1449          * @param string $taxonomy  The taxonomy name.
1450          * @param string $before    String to use before the terms.
1451          * @param string $sep       String to use between the terms.
1452          * @param string $after     String to use after the terms.
1453          */
1454         echo apply_filters( 'the_terms', $term_list, $taxonomy, $before, $sep, $after );
1455 }
1456
1457 /**
1458  * Check if the current post has any of given category.
1459  *
1460  * @since 3.1.0
1461  *
1462  * @param string|int|array $category Optional. The category name/term_id/slug or array of them to check for.
1463  * @param int|object $post Optional. Post to check instead of the current post.
1464  * @return bool True if the current post has any of the given categories (or any category, if no category specified).
1465  */
1466 function has_category( $category = '', $post = null ) {
1467         return has_term( $category, 'category', $post );
1468 }
1469
1470 /**
1471  * Check if the current post has any of given tags.
1472  *
1473  * The given tags are checked against the post's tags' term_ids, names and slugs.
1474  * Tags given as integers will only be checked against the post's tags' term_ids.
1475  * If no tags are given, determines if post has any tags.
1476  *
1477  * 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)
1478  * Prior to v2.7, this function could only be used in the WordPress Loop.
1479  * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
1480  *
1481  * @since 2.6.0
1482  *
1483  * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
1484  * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
1485  * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1486  */
1487 function has_tag( $tag = '', $post = null ) {
1488         return has_term( $tag, 'post_tag', $post );
1489 }
1490
1491 /**
1492  * Check if the current post has any of given terms.
1493  *
1494  * The given terms are checked against the post's terms' term_ids, names and slugs.
1495  * Terms given as integers will only be checked against the post's terms' term_ids.
1496  * If no terms are given, determines if post has any terms.
1497  *
1498  * @since 3.1.0
1499  *
1500  * @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for.
1501  * @param string $taxonomy Taxonomy name
1502  * @param int|object $post Optional. Post to check instead of the current post.
1503  * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1504  */
1505 function has_term( $term = '', $taxonomy = '', $post = null ) {
1506         $post = get_post($post);
1507
1508         if ( !$post )
1509                 return false;
1510
1511         $r = is_object_in_term( $post->ID, $taxonomy, $term );
1512         if ( is_wp_error( $r ) )
1513                 return false;
1514
1515         return $r;
1516 }