PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/category-template.php

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