PageRenderTime 48ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/category-template.php

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