root/tags/2.3.1/wp-includes/category-template.php

Revision 6125, 13.4 kB (checked in by ryan, 1 year ago)

Add checks for WP_Error. Props filosofo. see #4809

  • 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 = '') {
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, $term_cache, $blog_id;
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='') {
113     global $wp_rewrite;
114     $categories = get_the_category();
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 function in_category( $category ) { // Check if the current post is in the given category
171     global $post, $blog_id;
172
173     $categories = get_object_term_cache($post->ID, 'category');
174     if ( false === $categories )
175         $categories = wp_get_object_terms($post->ID, 'category');
176     if(array_key_exists($category, $categories))
177         return true;
178     else
179         return false;
180 }
181
182 function the_category($separator = '', $parents='') {
183     echo get_the_category_list($separator, $parents);
184 }
185
186 function category_description($category = 0) {
187     global $cat;
188     if ( !$category )
189         $category = $cat;
190
191     return get_term_field('description', $category, 'category');
192 }
193
194 function wp_dropdown_categories($args = '') {
195     $defaults = array(
196         'show_option_all' => '', 'show_option_none' => '',
197         'orderby' => 'ID', 'order' => 'ASC',
198         'show_last_update' => 0, 'show_count' => 0,
199         'hide_empty' => 1, 'child_of' => 0,
200         'exclude' => '', 'echo' => 1,
201         'selected' => 0, 'hierarchical' => 0,
202         'name' => 'cat', 'class' => 'postform'
203     );
204
205     $defaults['selected'] = ( is_category() ) ? get_query_var('cat') : 0;
206
207     $r = wp_parse_args( $args, $defaults );
208     $r['include_last_update_time'] = $r['show_last_update'];
209     extract( $r );
210
211     $categories = get_categories($r);
212
213     $output = '';
214     if ( ! empty($categories) ) {
215         $output = "<select name='$name' id='$name' class='$class'>\n";
216
217         if ( $show_option_all ) {
218             $show_option_all = apply_filters('list_cats', $show_option_all);
219             $output .= "\t<option value='0'>$show_option_all</option>\n";
220         }
221
222         if ( $show_option_none) {
223             $show_option_none = apply_filters('list_cats', $show_option_none);
224             $output .= "\t<option value='-1'>$show_option_none</option>\n";
225         }
226
227         if ( $hierarchical )
228             $depth = 0// Walk the full depth.
229         else
230             $depth = -1; // Flat.
231
232         $output .= walk_category_dropdown_tree($categories, $depth, $r);
233         $output .= "</select>\n";
234     }
235
236     $output = apply_filters('wp_dropdown_cats', $output);
237
238     if ( $echo )
239         echo $output;
240
241     return $output;
242 }
243
244 function wp_list_categories($args = '') {
245     $defaults = array(
246         'show_option_all' => '', 'orderby' => 'name',
247         'order' => 'ASC', 'show_last_update' => 0,
248         'style' => 'list', 'show_count' => 0,
249         'hide_empty' => 1, 'use_desc_for_title' => 1,
250         'child_of' => 0, 'feed' => '',
251         'feed_image' => '', 'exclude' => '',
252         'hierarchical' => true, 'title_li' => __('Categories'),
253         'echo' => 1
254     );
255
256     $r = wp_parse_args( $args, $defaults );
257
258     if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
259         $r['pad_counts'] = true;
260     }
261
262     if ( isset( $r['show_date'] ) ) {
263         $r['include_last_update_time'] = $r['show_date'];
264     }
265
266     extract( $r );
267
268     $categories = get_categories($r);
269
270     $output = '';
271     if ( $title_li && 'list' == $style )
272             $output = '<li class="categories">' . $r['title_li'] . '<ul>';
273
274     if ( empty($categories) ) {
275         if ( 'list' == $style )
276             $output .= '<li>' . __("No categories") . '</li>';
277         else
278             $output .= __("No categories");
279     } else {
280         global $wp_query;
281
282         if( !empty($show_option_all) )
283             if ('list' == $style )
284                 $output .= '<li><a href="' get_bloginfo('url')  . '">' . $show_option_all . '</a></li>';
285             else
286                 $output .= '<a href="' get_bloginfo('url')  . '">' . $show_option_all . '</a>';
287
288         if ( is_category() )
289             $r['current_category'] = $wp_query->get_queried_object_id();
290
291         if ( $hierarchical )
292             $depth = 0// Walk the full depth.
293         else
294             $depth = -1; // Flat.
295
296         $output .= walk_category_tree($categories, $depth, $r);
297     }
298
299     if ( $title_li && 'list' == $style )
300         $output .= '</ul></li>';
301
302     $output = apply_filters('wp_list_categories', $output);
303
304     if ( $echo )
305         echo $output;
306     else
307         return $output;
308 }
309
310 function wp_tag_cloud( $args = '' ) {
311     $defaults = array(
312         'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
313         'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
314         'exclude' => '', 'include' => ''
315     );
316     $args = wp_parse_args( $args, $defaults );
317
318     $tags = get_tags( array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) ); // Always query top tags
319
320     if ( empty($tags) )
321         return;
322
323     $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
324     if ( is_wp_error( $return ) )
325         return false;
326     else
327         echo apply_filters( 'wp_tag_cloud', $return, $args );
328 }
329
330 // $tags = prefetched tag array ( get_tags() )
331 // $args['format'] = 'flat' => whitespace separated, 'list' => UL, 'array' => array()
332 // $args['orderby'] = 'name', 'count'
333 function wp_generate_tag_cloud( $tags, $args = '' ) {
334     global $wp_rewrite;
335     $defaults = array(
336         'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
337         'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC'
338     );
339     $args = wp_parse_args( $args, $defaults );
340     extract($args);
341
342     if ( !$tags )
343         return;
344     $counts = $tag_links = array();
345     foreach ( (array) $tags as $tag ) {
346         $counts[$tag->name] = $tag->count;
347         $tag_links[$tag->name] = get_tag_link( $tag->term_id );
348         if ( is_wp_error( $tag_links[$tag->name] ) )
349             return $tag_links[$tag->name];
350         $tag_ids[$tag->name] = $tag->term_id;
351     }
352
353     $min_count = min($counts);
354     $spread = max($counts) - $min_count;
355     if ( $spread <= 0 )
356         $spread = 1;
357     $font_spread = $largest - $smallest;
358     if ( $font_spread <= 0 )
359         $font_spread = 1;
360     $font_step = $font_spread / $spread;
361
362     // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
363     if ( 'name' == $orderby )
364         uksort($counts, 'strnatcasecmp');
365     else
366         asort($counts);
367
368     if ( 'DESC' == $order )
369         $counts = array_reverse( $counts, true );
370
371     $a = array();
372
373     $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
374
375     foreach ( $counts as $tag => $count ) {
376         $tag_id = $tag_ids[$tag];
377         $tag_link = clean_url($tag_links[$tag]);
378         $tag = str_replace(' ', '&nbsp;', wp_specialchars( $tag ));
379         $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . attribute_escape( sprintf( __('%d topics'), $count ) ) . "'$rel style='font-size: " .
380             ( $smallest + ( ( $count - $min_count ) * $font_step ) )
381             . "$unit;'>$tag</a>";
382     }
383
384     switch ( $format ) :
385     case 'array' :
386         $return =& $a;
387         break;
388     case 'list' :
389         $return = "<ul class='wp-tag-cloud'>\n\t<li>";
390         $return .= join("</li>\n\t<li>", $a);
391         $return .= "</li>\n</ul>\n";
392         break;
393     default :
394         $return = join("\n", $a);
395         break;
396     endswitch;
397
398     return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
399 }
400
401 //
402 // Helper functions
403 //
404
405 function walk_category_tree() {
406     $walker = new Walker_Category;
407     $args = func_get_args();
408     return call_user_func_array(array(&$walker, 'walk'), $args);
409 }
410
411 function walk_category_dropdown_tree() {
412     $walker = new Walker_CategoryDropdown;
413     $args = func_get_args();
414     return call_user_func_array(array(&$walker, 'walk'), $args);
415 }
416
417 //
418 // Tags
419 //
420
421 function get_tag_link( $tag_id ) {
422     global $wp_rewrite;
423     $taglink = $wp_rewrite->get_tag_permastruct();
424
425     $tag = &get_term($tag_id, 'post_tag');
426     if ( is_wp_error( $tag ) )
427         return $tag;
428     $slug = $tag->slug;
429
430     if ( empty($taglink) ) {
431         $file = get_option('home') . '/';
432         $taglink = $file . '?tag=' . $slug;
433     } else {
434         $taglink = str_replace('%tag%', $slug, $taglink);
435         $taglink = get_option('home') . user_trailingslashit($taglink, 'category');
436     }
437     return apply_filters('tag_link', $taglink, $tag_id);
438 }
439
440 function get_the_tags( $id = 0 ) {
441     global $post;
442
443      $id = (int) $id;
444
445     if ( ! $id && ! in_the_loop() )
446         return false; // in-the-loop function
447
448     if ( !$id )
449         $id = (int) $post->ID;
450
451     $tags = get_object_term_cache