PageRenderTime 59ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/category-template.php

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