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