PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/category-template.php

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