]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/category-template.php
Wordpress 2.5.1
[autoinstalls/wordpress.git] / wp-includes / category-template.php
1 <?php
2
3 function get_category_children($id, $before = '/', $after = '') {
4         if ( 0 == $id )
5                 return '';
6
7         $chain = '';
8         // TODO: consult hierarchy
9         $cat_ids = get_all_category_ids();
10         foreach ( $cat_ids as $cat_id ) {
11                 if ( $cat_id == $id )
12                         continue;
13
14                 $category = get_category($cat_id);
15                 if ( is_wp_error( $category ) )
16                         return $category;
17                 if ( $category->parent == $id ) {
18                         $chain .= $before.$category->term_id.$after;
19                         $chain .= get_category_children($category->term_id, $before, $after);
20                 }
21         }
22         return $chain;
23 }
24
25 function get_category_link($category_id) {
26         global $wp_rewrite;
27         $catlink = $wp_rewrite->get_category_permastruct();
28
29         if ( empty($catlink) ) {
30                 $file = get_option('home') . '/';
31                 $catlink = $file . '?cat=' . $category_id;
32         } else {
33                 $category = &get_category($category_id);
34                 if ( is_wp_error( $category ) )
35                         return $category;
36                 $category_nicename = $category->slug;
37
38                 if ( $parent = $category->parent )
39                         $category_nicename = get_category_parents($parent, false, '/', true) . $category_nicename;
40
41                 $catlink = str_replace('%category%', $category_nicename, $catlink);
42                 $catlink = get_option('home') . user_trailingslashit($catlink, 'category');
43         }
44         return apply_filters('category_link', $catlink, $category_id);
45 }
46
47 function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE){
48         $chain = '';
49         $parent = &get_category($id);
50         if ( is_wp_error( $parent ) )
51                 return $parent;
52
53         if ( $nicename )
54                 $name = $parent->slug;
55         else
56                 $name = $parent->cat_name;
57
58         if ( $parent->parent && ($parent->parent != $parent->term_id) )
59                 $chain .= get_category_parents($parent->parent, $link, $separator, $nicename);
60
61         if ( $link )
62                 $chain .= '<a href="' . get_category_link($parent->term_id) . '" title="' . sprintf(__("View all posts in %s"), $parent->cat_name) . '">'.$name.'</a>' . $separator;
63         else
64                 $chain .= $name.$separator;
65         return $chain;
66 }
67
68 function get_the_category($id = false) {
69         global $post;
70
71         $id = (int) $id;
72         if ( !$id )
73                 $id = (int) $post->ID;
74
75         $categories = get_object_term_cache($id, 'category');
76         if ( false === $categories )
77                 $categories = wp_get_object_terms($id, 'category');
78
79         if ( !empty($categories) )
80                 usort($categories, '_usort_terms_by_name');
81         else
82                 $categories = array();
83
84         foreach(array_keys($categories) as $key) {
85                 _make_cat_compat($categories[$key]);
86         }
87
88         return $categories;
89 }
90
91 function _usort_terms_by_name($a, $b) {
92         return strcmp($a->name, $b->name);
93 }
94
95 function _usort_terms_by_ID($a, $b) {
96         if ( $a->term_id > $b->term_id )
97                 return 1;
98         elseif ( $a->term_id < $b->term_id )
99                 return -1;
100         else
101                 return 0;
102 }
103
104 function get_the_category_by_ID($cat_ID) {
105         $cat_ID = (int) $cat_ID;
106         $category = &get_category($cat_ID);
107         if ( is_wp_error( $category ) )
108                 return $category;
109         return $category->name;
110 }
111
112 function get_the_category_list($separator = '', $parents='', $post_id = false) {
113         global $wp_rewrite;
114         $categories = get_the_category($post_id);
115         if (empty($categories))
116                 return apply_filters('the_category', __('Uncategorized'), $separator, $parents);
117
118         $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
119
120         $thelist = '';
121         if ( '' == $separator ) {
122                 $thelist .= '<ul class="post-categories">';
123                 foreach ( $categories as $category ) {
124                         $thelist .= "\n\t<li>";
125                         switch ( strtolower($parents) ) {
126                                 case 'multiple':
127                                         if ($category->parent)
128                                                 $thelist .= get_category_parents($category->parent, TRUE);
129                                         $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->name.'</a></li>';
130                                         break;
131                                 case 'single':
132                                         $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>';
133                                         if ($category->parent)
134                                                 $thelist .= get_category_parents($category->parent, FALSE);
135                                         $thelist .= $category->name.'</a></li>';
136                                         break;
137                                 case '':
138                                 default:
139                                         $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->cat_name.'</a></li>';
140                         }
141                 }
142                 $thelist .= '</ul>';
143         } else {
144                 $i = 0;
145                 foreach ( $categories as $category ) {
146                         if ( 0 < $i )
147                                 $thelist .= $separator . ' ';
148                         switch ( strtolower($parents) ) {
149                                 case 'multiple':
150                                         if ( $category->parent )
151                                                 $thelist .= get_category_parents($category->parent, TRUE);
152                                         $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->cat_name.'</a>';
153                                         break;
154                                 case 'single':
155                                         $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>';
156                                         if ( $category->parent )
157                                                 $thelist .= get_category_parents($category->parent, FALSE);
158                                         $thelist .= "$category->cat_name</a>";
159                                         break;
160                                 case '':
161                                 default:
162                                         $thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->name.'</a>';
163                         }
164                         ++$i;
165                 }
166         }
167         return apply_filters('the_category', $thelist, $separator, $parents);
168 }
169
170 /*
171  * in_category() - Checks whether the current post is within a particular category
172  *
173  * This function checks to see if the post is within the supplied category.  The categoy
174  * can be specified by number or name and will be checked as a name first to allow for categories with numeric names.
175  * Note: Prior to v2.5 of WordPress category names where not supported.
176  *
177  * @since 1.2.0
178  *
179  * @param int|string $category
180  * @return bool true if the post is in the supplied category
181 */
182 function in_category( $category ) { // Check if the current post is in the given category
183         global $post;
184
185         if ( empty($category) )
186                 return false;
187
188         $cat_ID = get_cat_ID($category);
189         if ( $cat_ID )
190                 $category = $cat_ID;
191
192         $categories = get_object_term_cache($post->ID, 'category');
193         if ( false === $categories )
194                 $categories = wp_get_object_terms($post->ID, 'category');
195         if ( array_key_exists($category, $categories) )
196                 return true;
197         else
198                 return false;
199 }
200
201 function the_category($separator = '', $parents='', $post_id = false) {
202         echo get_the_category_list($separator, $parents, $post_id);
203 }
204
205 function category_description($category = 0) {
206         global $cat;
207         if ( !$category )
208                 $category = $cat;
209
210         return get_term_field('description', $category, 'category');
211 }
212
213 function wp_dropdown_categories($args = '') {
214         $defaults = array(
215                 'show_option_all' => '', 'show_option_none' => '',
216                 'orderby' => 'ID', 'order' => 'ASC',
217                 'show_last_update' => 0, 'show_count' => 0,
218                 'hide_empty' => 1, 'child_of' => 0,
219                 'exclude' => '', 'echo' => 1,
220                 'selected' => 0, 'hierarchical' => 0,
221                 'name' => 'cat', 'class' => 'postform',
222                 'depth' => 0, 'tab_index' => 0
223         );
224
225         $defaults['selected'] = ( is_category() ) ? get_query_var('cat') : 0;
226
227         $r = wp_parse_args( $args, $defaults );
228         $r['include_last_update_time'] = $r['show_last_update'];
229         extract( $r );
230
231         $tab_index_attribute = '';
232         if ( (int) $tab_index > 0 )
233                 $tab_index_attribute = " tabindex=\"$tab_index\"";
234
235         $categories = get_categories($r);
236
237         $output = '';
238         if ( ! empty($categories) ) {
239                 $output = "<select name='$name' id='$name' class='$class' $tab_index_attribute>\n";
240
241                 if ( $show_option_all ) {
242                         $show_option_all = apply_filters('list_cats', $show_option_all);
243                         $output .= "\t<option value='0'>$show_option_all</option>\n";
244                 }
245
246                 if ( $show_option_none) {
247                         $show_option_none = apply_filters('list_cats', $show_option_none);
248                         $output .= "\t<option value='-1'>$show_option_none</option>\n";
249                 }
250
251                 if ( $hierarchical )
252                         $depth = $r['depth'];  // Walk the full depth.
253                 else
254                         $depth = -1; // Flat.
255
256                 $output .= walk_category_dropdown_tree($categories, $depth, $r);
257                 $output .= "</select>\n";
258         }
259
260         $output = apply_filters('wp_dropdown_cats', $output);
261
262         if ( $echo )
263                 echo $output;
264
265         return $output;
266 }
267
268 function wp_list_categories($args = '') {
269         $defaults = array(
270                 'show_option_all' => '', 'orderby' => 'name',
271                 'order' => 'ASC', 'show_last_update' => 0,
272                 'style' => 'list', 'show_count' => 0,
273                 'hide_empty' => 1, 'use_desc_for_title' => 1,
274                 'child_of' => 0, 'feed' => '', 'feed_type' => '',
275                 'feed_image' => '', 'exclude' => '',
276                 'hierarchical' => true, 'title_li' => __('Categories'),
277                 'echo' => 1, 'depth' => 0
278         );
279
280         $r = wp_parse_args( $args, $defaults );
281
282         if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
283                 $r['pad_counts'] = true;
284         }
285
286         if ( isset( $r['show_date'] ) ) {
287                 $r['include_last_update_time'] = $r['show_date'];
288         }
289
290         extract( $r );
291
292         $categories = get_categories($r);
293
294         $output = '';
295         if ( $title_li && 'list' == $style )
296                         $output = '<li class="categories">' . $r['title_li'] . '<ul>';
297
298         if ( empty($categories) ) {
299                 if ( 'list' == $style )
300                         $output .= '<li>' . __("No categories") . '</li>';
301                 else
302                         $output .= __("No categories");
303         } else {
304                 global $wp_query;
305
306                 if( !empty($show_option_all) )
307                         if ('list' == $style )
308                                 $output .= '<li><a href="' .  get_bloginfo('url')  . '">' . $show_option_all . '</a></li>';
309                         else
310                                 $output .= '<a href="' .  get_bloginfo('url')  . '">' . $show_option_all . '</a>';
311
312                 if ( is_category() )
313                         $r['current_category'] = $wp_query->get_queried_object_id();
314
315                 if ( $hierarchical )
316                         $depth = $r['depth'];
317                 else
318                         $depth = -1; // Flat.
319
320                 $output .= walk_category_tree($categories, $depth, $r);
321         }
322
323         if ( $title_li && 'list' == $style )
324                 $output .= '</ul></li>';
325
326         $output = apply_filters('wp_list_categories', $output);
327
328         if ( $echo )
329                 echo $output;
330         else
331                 return $output;
332 }
333
334 function wp_tag_cloud( $args = '' ) {
335         $defaults = array(
336                 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
337                 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
338                 'exclude' => '', 'include' => ''
339         );
340         $args = wp_parse_args( $args, $defaults );
341
342         $tags = get_tags( array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) ); // Always query top tags
343
344         if ( empty($tags) )
345                 return;
346
347         $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
348
349         if ( is_wp_error( $return ) )
350                 return false;
351
352         $return = apply_filters( 'wp_tag_cloud', $return, $args );
353
354         if ( 'array' == $args['format'] )
355                 return $return;
356
357         echo $return;
358 }
359
360 // $tags = prefetched tag array ( get_tags() )
361 // $args['format'] = 'flat' => whitespace separated, 'list' => UL, 'array' => array()
362 // $args['orderby'] = 'name', 'count'
363 function wp_generate_tag_cloud( $tags, $args = '' ) {
364         global $wp_rewrite;
365         $defaults = array(
366                 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
367                 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC'
368         );
369         $args = wp_parse_args( $args, $defaults );
370         extract($args);
371
372         if ( !$tags )
373                 return;
374         $counts = $tag_links = array();
375         foreach ( (array) $tags as $tag ) {
376                 $counts[$tag->name] = $tag->count;
377                 $tag_links[$tag->name] = get_tag_link( $tag->term_id );
378                 if ( is_wp_error( $tag_links[$tag->name] ) )
379                         return $tag_links[$tag->name];
380                 $tag_ids[$tag->name] = $tag->term_id;
381         }
382
383         $min_count = min($counts);
384         $spread = max($counts) - $min_count;
385         if ( $spread <= 0 )
386                 $spread = 1;
387         $font_spread = $largest - $smallest;
388         if ( $font_spread <= 0 )
389                 $font_spread = 1;
390         $font_step = $font_spread / $spread;
391
392         // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
393         if ( 'name' == $orderby )
394                 uksort($counts, 'strnatcasecmp');
395         else
396                 asort($counts);
397
398         if ( 'DESC' == $order )
399                 $counts = array_reverse( $counts, true );
400         elseif ( 'RAND' == $order ) {
401                 $keys = array_rand( $counts, count($counts) );
402                 foreach ( $keys as $key )
403                         $temp[$key] = $counts[$key];
404                 $counts = $temp;
405                 unset($temp);
406         }
407
408         $a = array();
409
410         $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
411
412         foreach ( $counts as $tag => $count ) {
413                 $tag_id = $tag_ids[$tag];
414                 $tag_link = clean_url($tag_links[$tag]);
415                 $tag = str_replace(' ', '&nbsp;', wp_specialchars( $tag ));
416                 $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . attribute_escape( sprintf( __ngettext('%d topic','%d topics',$count), $count ) ) . "'$rel style='font-size: " .
417                         ( $smallest + ( ( $count - $min_count ) * $font_step ) )
418                         . "$unit;'>$tag</a>";
419         }
420
421         switch ( $format ) :
422         case 'array' :
423                 $return =& $a;
424                 break;
425         case 'list' :
426                 $return = "<ul class='wp-tag-cloud'>\n\t<li>";
427                 $return .= join("</li>\n\t<li>", $a);
428                 $return .= "</li>\n</ul>\n";
429                 break;
430         default :
431                 $return = join("\n", $a);
432                 break;
433         endswitch;
434
435         return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
436 }
437
438 //
439 // Helper functions
440 //
441
442 function walk_category_tree() {
443         $walker = new Walker_Category;
444         $args = func_get_args();
445         return call_user_func_array(array(&$walker, 'walk'), $args);
446 }
447
448 function walk_category_dropdown_tree() {
449         $walker = new Walker_CategoryDropdown;
450         $args = func_get_args();
451         return call_user_func_array(array(&$walker, 'walk'), $args);
452 }
453
454 //
455 // Tags
456 //
457
458 function get_tag_link( $tag_id ) {
459         global $wp_rewrite;
460         $taglink = $wp_rewrite->get_tag_permastruct();
461
462         $tag = &get_term($tag_id, 'post_tag');
463         if ( is_wp_error( $tag ) )
464                 return $tag;
465         $slug = $tag->slug;
466
467         if ( empty($taglink) ) {
468                 $file = get_option('home') . '/';
469                 $taglink = $file . '?tag=' . $slug;
470         } else {
471                 $taglink = str_replace('%tag%', $slug, $taglink);
472                 $taglink = get_option('home') . user_trailingslashit($taglink, 'category');
473         }
474         return apply_filters('tag_link', $taglink, $tag_id);
475 }
476
477 function get_the_tags( $id = 0 ) {
478         return apply_filters( 'get_the_tags', get_the_terms($id, 'post_tag') );
479 }
480
481 function get_the_tag_list( $before = '', $sep = '', $after = '' ) {
482         return apply_filters( 'the_tags', get_the_term_list(0, 'post_tag', $before, $sep, $after) );
483 }
484
485 function the_tags( $before = 'Tags: ', $sep = ', ', $after = '' ) {
486         return the_terms( 0, 'post_tag', $before, $sep, $after );
487 }
488
489 function get_the_terms( $id = 0, $taxonomy ) {
490         global $post;
491
492         $id = (int) $id;
493
494         if ( ! $id && ! in_the_loop() )
495                 return false; // in-the-loop function
496
497         if ( !$id )
498                 $id = (int) $post->ID;
499
500         $terms = get_object_term_cache($id, $taxonomy);
501         if ( false === $terms )
502                 $terms = wp_get_object_terms($id, $taxonomy);
503
504         if ( empty( $terms ) )
505                 return false;
506
507         return $terms;
508 }
509
510 function get_the_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after = '' ) {
511         $terms = get_the_terms($id, $taxonomy);
512
513         if ( is_wp_error($terms) )
514                 return $terms;
515
516         if ( empty( $terms ) )
517                 return false;
518
519         foreach ( $terms as $term ) {
520                 $link = get_term_link($term, $taxonomy);
521                 if ( is_wp_error( $link ) )
522                         return $link;
523                 $term_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>';
524         }
525
526         $term_links = apply_filters( "term_links-$taxonomy", $term_links );
527
528         return $before . join($sep, $term_links) . $after;
529 }
530
531 function the_terms( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
532         $return = get_the_term_list($id, $taxonomy, $before, $sep, $after);
533         if ( is_wp_error( $return ) )
534                 return false;
535         else
536                 echo $return;
537 }
538
539 ?>