root/trunk/wp-includes/category-template.php

Revision 8668, 16.8 kB (checked in by ryan, 2 days ago)

Tag cloud fixes from DD32. Implements number arg. see #6015

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2
3 function get_category_children( $id, $before = '/', $after = '', $visited = array() ) {
4     if ( 0 == $id )
5         return '';
6
7     $chain = '';
8     // TODO: consult hierarchy
9     $cat_ids = get_all_category_ids();
10     foreach ( (array) $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 && !in_array( $category->term_id, $visited ) ) {
18             $visited[] = $category->term_id;
19             $chain .= $before.$category->term_id.$after;
20             $chain .= get_category_children( $category->term_id, $before, $after );
21         }
22     }
23     return $chain;
24 }
25
26 function get_category_link( $category_id ) {
27     global $wp_rewrite;
28     $catlink = $wp_rewrite->get_category_permastruct();
29
30     if ( empty( $catlink ) ) {
31         $file = get_option( 'home' ) . '/';
32         $catlink = $file . '?cat=' . $category_id;
33     } else {
34         $category = &get_category( $category_id );
35         if ( is_wp_error( $category ) )
36             return $category;
37         $category_nicename = $category->slug;
38
39         if ( $parent = $category->parent )
40             $category_nicename = get_category_parents( $parent, false, '/', true ) . $category_nicename;
41
42         $catlink = str_replace( '%category%', $category_nicename, $catlink );
43         $catlink = get_option( 'home' ) . user_trailingslashit( $catlink, 'category' );
44     }
45     return apply_filters( 'category_link', $catlink, $category_id );
46 }
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
54     if ( $nicename )
55         $name = $parent->slug;
56     else
57         $name = $parent->cat_name;
58
59     if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
60         $visited[] = $parent->parent;
61         $chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited );
62     }
63
64     if ( $link )
65         $chain .= '<a href="' . get_category_link( $parent->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $parent->cat_name ) . '">'.$name.'</a>' . $separator;
66     else
67         $chain .= $name.$separator;
68     return $chain;
69 }
70
71 function get_the_category( $id = false ) {
72     global $post;
73
74     $id = (int) $id;
75     if ( !$id )
76         $id = (int) $post->ID;
77
78     $categories = get_object_term_cache( $id, 'category' );
79     if ( false === $categories )
80         $categories = wp_get_object_terms( $id, 'category' );
81
82     if ( !empty( $categories ) )
83         usort( $categories, '_usort_terms_by_name' );
84     else
85         $categories = array();
86
87     foreach ( (array) array_keys( $categories ) as $key ) {
88         _make_cat_compat( $categories[$key] );
89     }
90
91     return $categories;
92 }
93
94 function _usort_terms_by_name( $a, $b ) {
95     return strcmp( $a->name, $b->name );
96 }
97
98 function _usort_terms_by_ID( $a, $b ) {
99     if ( $a->term_id > $b->term_id )
100         return 1;
101     elseif ( $a->term_id < $b->term_id )
102         return -1;
103     else
104         return 0;
105 }
106
107 function get_the_category_by_ID( $cat_ID ) {
108     $cat_ID = (int) $cat_ID;
109     $category = &get_category( $cat_ID );
110     if ( is_wp_error( $category ) )
111         return $category;
112     return $category->name;
113 }
114
115 function get_the_category_list( $separator = '', $parents='', $post_id = false ) {
116     global $wp_rewrite;
117     $categories = get_the_category( $post_id );
118     if ( empty( $categories ) )
119         return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents );
120
121     $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
122
123     $thelist = '';
124     if ( '' == $separator ) {
125         $thelist .= '<ul class="post-categories">';
126         foreach ( $categories as $category ) {
127             $thelist .= "\n\t<li>";
128             switch ( strtolower( $parents ) ) {
129                 case 'multiple':
130                     if ( $category->parent )
131                         $thelist .= get_category_parents( $category->parent, TRUE );
132                     $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>' . $category->name.'</a></li>';
133                     break;
134                 case 'single':
135                     $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>';
136                     if ( $category->parent )
137                         $thelist .= get_category_parents( $category->parent, FALSE );
138                     $thelist .= $category->name.'</a></li>';
139                     break;
140                 case '':
141                 default:
142                     $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>' . $category->cat_name.'</a></li>';
143             }
144         }
145         $thelist .= '</ul>';
146     } else {
147         $i = 0;
148         foreach ( $categories as $category ) {
149             if ( 0 < $i )
150                 $thelist .= $separator . ' ';
151             switch ( strtolower( $parents ) ) {
152                 case 'multiple':
153                     if ( $category->parent )
154                         $thelist .= get_category_parents( $category->parent, TRUE );
155                     $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>' . $category->cat_name.'</a>';
156                     break;
157                 case 'single':
158                     $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>';
159                     if ( $category->parent )
160                         $thelist .= get_category_parents( $category->parent, FALSE );
161                     $thelist .= "$category->cat_name</a>";
162                     break;
163                 case '':
164                 default:
165                     $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>' . $category->name.'</a>';
166             }
167             ++$i;
168         }
169     }
170     return apply_filters( 'the_category', $thelist, $separator, $parents );
171 }
172
173 /*
174  * in_category() - Checks whether the current post is within a particular category
175  *
176  * This function checks to see if the post is within the supplied category.  The categoy
177  * can be specified by number or name and will be checked as a name first to allow for categories with numeric names.
178  * Note: Prior to v2.5 of WordPress category names where not supported.
179  *
180  * @since 1.2.0
181  *
182  * @param int|string $category
183  * @return bool true if the post is in the supplied category
184 */
185 function in_category( $category ) { // Check if the current post is in the given category
186     global $post;
187
188     if ( empty( $category ) )
189         return false;
190
191     // If category is not an int, check to see if it's a name
192     if ( ! is_int( $category ) ) {
193         $cat_ID = get_cat_ID( $category );
194         if ( $cat_ID )
195             $category = $cat_ID;
196     }
197
198     $categories = get_object_term_cache( $post->ID, 'category' );
199     if ( false === $categories )
200         $categories = wp_get_object_terms( $post->ID, 'category' );
201     if ( array_key_exists( $category, $categories ) )
202         return true;
203     else
204         return false;
205 }
206
207 function the_category( $separator = '', $parents='', $post_id = false ) {
208     echo get_the_category_list( $separator, $parents, $post_id );
209 }
210
211 function category_description( $category = 0 ) {
212     global $cat;
213     if ( !$category )
214         $category = $cat;
215
216     return get_term_field( 'description', $category, 'category' );
217 }
218
219 function wp_dropdown_categories( $args = '' ) {
220     $defaults = array(
221         'show_option_all' => '', 'show_option_none' => '',
222         'orderby' => 'ID', 'order' => 'ASC',
223         'show_last_update' => 0, 'show_count' => 0,
224         'hide_empty' => 1, 'child_of' => 0,
225         'exclude' => '', 'echo' => 1,
226         'selected' => 0, 'hierarchical' => 0,
227         'name' => 'cat', 'class' => 'postform',
228         'depth' => 0, 'tab_index' => 0
229     );
230
231     $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
232
233     $r = wp_parse_args( $args, $defaults );
234     $r['include_last_update_time'] = $r['show_last_update'];
235     extract( $r );
236
237     $tab_index_attribute = '';
238     if ( (int) $tab_index > 0 )
239         $tab_index_attribute = " tabindex=\"$tab_index\"";
240
241     $categories = get_categories( $r );
242
243     $output = '';
244     if ( ! empty( $categories ) ) {
245         $output = "<select name='$name' id='$name' class='$class' $tab_index_attribute>\n";
246
247         if ( $show_option_all ) {
248             $show_option_all = apply_filters( 'list_cats', $show_option_all );
249             $output .= "\t<option value='0'>$show_option_all</option>\n";
250         }
251
252         if ( $show_option_none ) {
253             $show_option_none = apply_filters( 'list_cats', $show_option_none );
254             $output .= "\t<option value='-1'>$show_option_none</option>\n";
255         }
256
257         if ( $hierarchical )
258             $depth = $r['depth'];  // Walk the full depth.
259         else
260             $depth = -1; // Flat.
261
262         $output .= walk_category_dropdown_tree( $categories, $depth, $r );
263         $output .= "</select>\n";
264     }
265
266     $output = apply_filters( 'wp_dropdown_cats', $output );
267
268     if ( $echo )
269         echo $output;
270
271     return $output;
272 }
273
274 function wp_list_categories( $args = '' ) {
275     $defaults = array(
276         'show_option_all' => '', 'orderby' => 'name',
277         'order' => 'ASC', 'show_last_update' => 0,
278         'style' => 'list', 'show_count' => 0,
279         'hide_empty' => 1, 'use_desc_for_title' => 1,
280         'child_of' => 0, 'feed' => '', 'feed_type' => '',
281         'feed_image' => '', 'exclude' => '', 'current_category' => 0,
282         'hierarchical' => true, 'title_li' => __( 'Categories' ),
283         'echo' => 1, 'depth' => 0
284     );
285
286     $r = wp_parse_args( $args, $defaults );
287
288     if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
289         $r['pad_counts'] = true;
290     }
291
292     if ( isset( $r['show_date'] ) ) {
293         $r['include_last_update_time'] = $r['show_date'];
294     }
295
296     extract( $r );
297
298     $categories = get_categories( $r );
299
300     $output = '';
301     if ( $title_li && 'list' == $style )
302             $output = '<li class="categories">' . $r['title_li'] . '<ul>';
303
304     if ( empty( $categories ) ) {
305         if ( 'list' == $style )
306             $output .= '<li>' . __( "No categories" ) . '</li>';
307         else
308             $output .= __( "No categories" );
309     } else {
310         global $wp_query;
311
312         if( !empty( $show_option_all ) )
313             if ( 'list' == $style )
314                 $output .= '<li><a href="' get_bloginfo( 'url' )  . '">' . $show_option_all . '</a></li>';
315             else
316                 $output .= '<a href="' get_bloginfo( 'url' )  . '">' . $show_option_all . '</a>';
317
318         if ( empty( $r['current_category'] ) && is_category() )
319             $r['current_category'] = $wp_query->get_queried_object_id();
320
321         if ( $hierarchical )
322             $depth = $r['depth'];
323         else
324             $depth = -1; // Flat.
325
326         $output .= walk_category_tree( $categories, $depth, $r );
327     }
328
329     if ( $title_li && 'list' == $style )
330         $output .= '</ul></li>';
331
332     $output = apply_filters( 'wp_list_categories', $output );
333
334     if ( $echo )
335         echo $output;
336     else
337         return $output;
338 }
339
340 function wp_tag_cloud( $args = '' ) {
341     $defaults = array(
342         'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
343         'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
344         'exclude' => '', 'include' => ''
345     );
346     $args = wp_parse_args( $args, $defaults );
347
348     $tags = get_tags( array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
349
350     if ( empty( $tags ) )
351         return;
352
353     foreach ( $tags as $key => $tag ) {
354         $link = get_tag_link( $tag->term_id );
355         if ( is_wp_error( $link ) )
356             return false;
357
358         $tags[ $key ]->link = $link;
359         $tags[ $key ]->id = $tag->term_id;
360     }
361
362     $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
363
364     $return = apply_filters( 'wp_tag_cloud', $return, $args );
365
366     if ( 'array' == $args['format'] )
367         return $return;
368
369     echo $return;
370 }
371
372 /**
373  * Generates a tag cloud (heatmap) from provided data
374  *
375  * TODO: Complete
376  *
377  * @since 2.6
378  *
379  * $tags = array of objects with the properties 'name', 'link', 'id', and 'count'
380  * $args['format'] = 'flat' => whitespace separated, 'list' => UL, 'array' => array()
381  * $args['orderby'] = 'name', 'count'
382 */
383 function wp_generate_tag_cloud( $tags, $args = '' ) {
384     global $wp_rewrite;
385     $defaults = array(
386         'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
387         'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
388         'single_text' => '%d topic', 'multiple_text' => '%d topics'
389     );
390     $args = wp_parse_args( $args, $defaults );
391     extract( $args );
392
393     if ( empty( $tags ) )
394         return;
395
396     // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
397     if ( 'name' == $orderby )
398         uasort( $tags, create_function('$a, $b', 'return strnatcasecmp($a->name, $b->name);') );
399     else
400         uasort( $tags, create_function('$a, $b', 'return ($a->count < $b->count);') );
401
402     if ( 'DESC' == $order )
403         $tags = array_reverse( $tags, true );
404     elseif ( 'RAND' == $order ) {
405         $keys = array_rand( $tags, count( $tags ) );
406         foreach ( $keys as $key )
407             $temp[$key] = $tags[$key];
408         $tags = $temp;
409         unset( $temp );
410     }
411
412     if ( $number > 0 )
413         $tags = array_slice($tags, 0, $number);
414
415     $counts = array();
416     foreach ( (array) $tags as $key => $tag )
417         $counts[ $key ] = $tag->count;
418
419     $min_count = min( $counts );
420     $spread = max( $counts ) - $min_count;
421     if ( $spread <= 0 )
422         $spread = 1;
423     $font_spread = $largest - $smallest;
424     if ( $font_spread <= 0 )
425         $font_spread = 1;
426     $font_step = $font_spread / $spread;
427
428     $a = array();
429
430     $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
431
432     foreach ( $tags as $key => $tag ) {
433         $count = $counts[ $key ];
434         $tag_link = clean_url( $tag->link );
435         $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
436         $tag_name = $tags[ $key ]->name;
437         $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . attribute_escape( sprintf( __ngettext( $single_text, $multiple_text, $count ), $count ) ) . "'$rel style='font-size: " .
438             ( $smallest + ( ( $count - $min_count ) * $font_step ) )
439             . "$unit;'>$tag_name</a>";
440     }
441
442     switch ( $format ) :
443     case 'array' :
444         $return =&