- File:
-
- 1 edited
-
trunk/wp-includes/category-template.php (modified) (22 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/category-template.php
r17443 r15220 11 11 * 12 12 * @since 1.0.0 13 * @see get_term_link() 14 * 15 * @param int|object $category Category ID or object. 16 * @return string Link on success, empty string if category does not exist. 17 */ 18 function get_category_link( $category ) { 19 if ( ! is_object( $category ) ) 20 $category = (int) $category; 21 22 $category = get_term_link( $category, 'category' ); 23 24 if ( is_wp_error( $category ) ) 25 return ''; 26 27 return $category; 13 * @uses apply_filters() Calls 'category_link' filter on category link and category ID. 14 * 15 * @param int $category_id Category ID. 16 * @return string 17 */ 18 function get_category_link( $category_id ) { 19 global $wp_rewrite; 20 $catlink = $wp_rewrite->get_category_permastruct(); 21 22 if ( empty( $catlink ) ) { 23 $catlink = home_url('?cat=' . $category_id); 24 } else { 25 $category = &get_category( $category_id ); 26 if ( is_wp_error( $category ) ) 27 return $category; 28 $category_nicename = $category->slug; 29 30 if ( $category->parent == $category_id ) // recursive recursion 31 $category->parent = 0; 32 elseif ($category->parent != 0 ) 33 $category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename; 34 35 $catlink = str_replace( '%category%', $category_nicename, $catlink ); 36 $catlink = home_url( user_trailingslashit( $catlink, 'category' ) ); 37 } 38 return apply_filters( 'category_link', $catlink, $category_id ); 28 39 } 29 40 … … 49 60 $name = $parent->slug; 50 61 else 51 $name = $parent-> name;62 $name = $parent->cat_name; 52 63 53 64 if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) { … … 57 68 58 69 if ( $link ) 59 $chain .= '<a href="' . get_category_link( $parent->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent-> name ) ) . '">'.$name.'</a>' . $separator;70 $chain .= '<a href="' . get_category_link( $parent->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->cat_name ) ) . '">'.$name.'</a>' . $separator; 60 71 else 61 72 $chain .= $name.$separator; … … 73 84 */ 74 85 function get_the_category( $id = false ) { 75 $categories = get_the_terms( $id, 'category' ); 76 if ( ! $categories ) 86 global $post; 87 88 $id = (int) $id; 89 if ( !$id ) 90 $id = (int) $post->ID; 91 92 $categories = get_object_term_cache( $id, 'category' ); 93 if ( false === $categories ) { 94 $categories = wp_get_object_terms( $id, 'category' ); 95 wp_cache_add($id, $categories, 'category_relationships'); 96 } 97 98 if ( !empty( $categories ) ) 99 usort( $categories, '_usort_terms_by_name' ); 100 else 77 101 $categories = array(); 78 102 79 $categories = array_values( $categories ); 80 81 foreach ( array_keys( $categories ) as $key ) { 103 foreach ( (array) array_keys( $categories ) as $key ) { 82 104 _make_cat_compat( $categories[$key] ); 83 105 } 84 106 85 // Filter name is plural because we return alot of categories not just one 86 return apply_filters( 'get_the_categories', $categories ); 107 return $categories; 87 108 } 88 109 … … 182 203 case '': 183 204 default: 184 $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>';205 $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 206 } 186 207 } … … 195 216 if ( $category->parent ) 196 217 $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-> name.'</a>';218 $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 219 break; 199 220 case 'single': … … 201 222 if ( $category->parent ) 202 223 $thelist .= get_category_parents( $category->parent, false, $separator ); 203 $thelist .= "$category-> name</a>";224 $thelist .= "$category->cat_name</a>"; 204 225 break; 205 226 case '': … … 228 249 * @since 1.2.0 229 250 * 230 * @param int|string|array $category Category ID, name or slug, or array of said. 231 * @param int|object $_post Optional. Post to check instead of the current post. (since 2.7.0) 251 * @uses is_object_in_term() 252 * 253 * @param int|string|array $category. Category ID, name or slug, or array of said. 254 * @param int|post object Optional. Post to check instead of the current post. @since 2.7.0 232 255 * @return bool True if the current post is in any of the given categories. 233 256 */ 234 function in_category( $category, $ post = null ) {257 function in_category( $category, $_post = null ) { 235 258 if ( empty( $category ) ) 236 259 return false; 237 260 238 return has_term( $category, 'category', $post ); 261 if ( $_post ) { 262 $_post = get_post( $_post ); 263 } else { 264 $_post =& $GLOBALS['post']; 265 } 266 267 if ( !$_post ) 268 return false; 269 270 $r = is_object_in_term( $_post->ID, 'category', $category ); 271 if ( is_wp_error( $r ) ) 272 return false; 273 return $r; 239 274 } 240 275 … … 455 490 $output = ''; 456 491 if ( $title_li && 'list' == $style ) 457 $output = '<li class="' . esc_attr( $class ). '">' . $title_li . '<ul>';492 $output = '<li class="' . $class . '">' . $title_li . '<ul>'; 458 493 459 494 if ( empty( $categories ) ) { … … 465 500 } 466 501 } else { 502 global $wp_query; 503 467 504 if( !empty( $show_option_all ) ) 468 505 if ( 'list' == $style ) … … 471 508 $output .= '<a href="' . get_bloginfo( 'url' ) . '">' . $show_option_all . '</a>'; 472 509 473 if ( empty( $r['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) { 474 $current_term_object = get_queried_object(); 475 if ( $r['taxonomy'] == $current_term_object->taxonomy ) 476 $r['current_category'] = get_queried_object_id(); 477 } 510 if ( empty( $r['current_category'] ) && ( is_category() || is_tax() ) ) 511 $r['current_category'] = $wp_query->get_queried_object_id(); 478 512 479 513 if ( $hierarchical ) … … 539 573 foreach ( $tags as $key => $tag ) { 540 574 if ( 'edit' == $args['link'] ) 541 $link = get_edit_tag_link( $tag->term_id, $ tag->taxonomy);575 $link = get_edit_tag_link( $tag->term_id, $args['taxonomy'] ); 542 576 else 543 $link = get_term_link( intval($tag->term_id), $ tag->taxonomy);577 $link = get_term_link( intval($tag->term_id), $args['taxonomy'] ); 544 578 if ( is_wp_error( $link ) ) 545 579 return false; … … 679 713 $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key; 680 714 $tag_name = $tags[ $key ]->name; 681 $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( call_user_func( $topic_count_text_callback,$real_count ) ) . "' style='font-size: " .715 $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( $topic_count_text_callback( $real_count ) ) . "' style='font-size: " . 682 716 ( $smallest + ( ( $count - $min_count ) * $font_step ) ) 683 717 . "$unit;'>$tag_name</a>"; … … 698 732 endswitch; 699 733 700 if ( $filter )734 if ( $filter ) 701 735 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args ); 702 else736 else 703 737 return $return; 704 }705 706 /**707 * Callback for comparing tags based on name708 *709 * @since 3.1.0710 * @access private711 */712 function _wp_tag_cloud_name_sort_cb( $a, $b ) {713 return strnatcasecmp( $a->name, $b->name );714 }715 716 /**717 * Callback for comparing tags based on count718 *719 * @since 3.1.0720 * @access private721 */722 function _wp_tag_cloud_count_sort_cb( $a, $b ) {723 return ( $a->count > $b->count );724 738 } 725 739 … … 764 778 } 765 779 766 /**767 * Create HTML list of categories.768 *769 * @package WordPress770 * @since 2.1.0771 * @uses Walker772 */773 class Walker_Category extends Walker {774 /**775 * @see Walker::$tree_type776 * @since 2.1.0777 * @var string778 */779 var $tree_type = 'category';780 781 /**782 * @see Walker::$db_fields783 * @since 2.1.0784 * @todo Decouple this785 * @var array786 */787 var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');788 789 /**790 * @see Walker::start_lvl()791 * @since 2.1.0792 *793 * @param string $output Passed by reference. Used to append additional content.794 * @param int $depth Depth of category. Used for tab indentation.795 * @param array $args Will only append content if style argument value is 'list'.796 */797 function start_lvl(&$output, $depth, $args) {798 if ( 'list' != $args['style'] )799 return;800 801 $indent = str_repeat("\t", $depth);802 $output .= "$indent<ul class='children'>\n";803 }804 805 /**806 * @see Walker::end_lvl()807 * @since 2.1.0808 *809 * @param string $output Passed by reference. Used to append additional content.810 * @param int $depth Depth of category. Used for tab indentation.811 * @param array $args Will only append content if style argument value is 'list'.812 */813 function end_lvl(&$output, $depth, $args) {814 if ( 'list' != $args['style'] )815 return;816 817 $indent = str_repeat("\t", $depth);818 $output .= "$indent</ul>\n";819 }820 821 /**822 * @see Walker::start_el()823 * @since 2.1.0824 *825 * @param string $output Passed by reference. Used to append additional content.826 * @param object $category Category data object.827 * @param int $depth Depth of category in reference to parents.828 * @param array $args829 */830 function start_el(&$output, $category, $depth, $args) {831 extract($args);832 833 $cat_name = esc_attr( $category->name );834 $cat_name = apply_filters( 'list_cats', $cat_name, $category );835 $link = '<a href="' . esc_attr( get_term_link($category) ) . '" ';836 if ( $use_desc_for_title == 0 || empty($category->description) )837 $link .= 'title="' . esc_attr( sprintf(__( 'View all posts filed under %s' ), $cat_name) ) . '"';838 else839 $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';840 $link .= '>';841 $link .= $cat_name . '</a>';842 843 if ( !empty($feed_image) || !empty($feed) ) {844 $link .= ' ';845 846 if ( empty($feed_image) )847 $link .= '(';848 849 $link .= '<a href="' . get_term_feed_link( $category->term_id, $category->taxonomy, $feed_type ) . '"';850 851 if ( empty($feed) ) {852 $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';853 } else {854 $title = ' title="' . $feed . '"';855 $alt = ' alt="' . $feed . '"';856 $name = $feed;857 $link .= $title;858 }859 860 $link .= '>';861 862 if ( empty($feed_image) )863 $link .= $name;864 else865 $link .= "<img src='$feed_image'$alt$title" . ' />';866 867 $link .= '</a>';868 869 if ( empty($feed_image) )870 $link .= ')';871 }872 873 if ( !empty($show_count) )874 $link .= ' (' . intval($category->count) . ')';875 876 if ( !empty($show_date) )877 $link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);878 879 if ( 'list' == $args['style'] ) {880 $output .= "\t<li";881 $class = 'cat-item cat-item-' . $category->term_id;882 if ( !empty($current_category) ) {883 $_current_category = get_term( $current_category, $category->taxonomy );884 if ( $category->term_id == $current_category )885 $class .= ' current-cat';886 elseif ( $category->term_id == $_current_category->parent )887 $class .= ' current-cat-parent';888 }889 $output .= ' class="' . $class . '"';890 $output .= ">$link\n";891 } else {892 $output .= "\t$link<br />\n";893 }894 }895 896 /**897 * @see Walker::end_el()898 * @since 2.1.0899 *900 * @param string $output Passed by reference. Used to append additional content.901 * @param object $page Not used.902 * @param int $depth Depth of category. Not used.903 * @param array $args Only uses 'list' for whether should append to output.904 */905 function end_el(&$output, $page, $depth, $args) {906 if ( 'list' != $args['style'] )907 return;908 909 $output .= "</li>\n";910 }911 912 }913 914 /**915 * Create HTML dropdown list of Categories.916 *917 * @package WordPress918 * @since 2.1.0919 * @uses Walker920 */921 class Walker_CategoryDropdown extends Walker {922 /**923 * @see Walker::$tree_type924 * @since 2.1.0925 * @var string926 */927 var $tree_type = 'category';928 929 /**930 * @see Walker::$db_fields931 * @since 2.1.0932 * @todo Decouple this933 * @var array934 */935 var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');936 937 /**938 * @see Walker::start_el()939 * @since 2.1.0940 *941 * @param string $output Passed by reference. Used to append additional content.942 * @param object $category Category data object.943 * @param int $depth Depth of category. Used for padding.944 * @param array $args Uses 'selected', 'show_count', and 'show_last_update' keys, if they exist.945 */946 function start_el(&$output, $category, $depth, $args) {947 $pad = str_repeat(' ', $depth * 3);948 949 $cat_name = apply_filters('list_cats', $category->name, $category);950 $output .= "\t<option class=\"level-$depth\" value=\"".$category->term_id."\"";951 if ( $category->term_id == $args['selected'] )952 $output .= ' selected="selected"';953 $output .= '>';954 $output .= $pad.$cat_name;955 if ( $args['show_count'] )956 $output .= ' ('. $category->count .')';957 if ( $args['show_last_update'] ) {958 $format = 'Y-m-d';959 $output .= ' ' . gmdate($format, $category->last_update_timestamp);960 }961 $output .= "</option>\n";962 }963 }964 965 780 // 966 781 // Tags … … 971 786 * 972 787 * @since 2.3.0 973 * @see get_term_link() 974 * 975 * @param int|object $tag Tag ID or object. 976 * @return string Link on success, empty string if tag does not exist. 977 */ 978 function get_tag_link( $tag ) { 979 if ( ! is_object( $tag ) ) 980 $tag = (int) $tag; 981 982 $tag = get_term_link( $tag, 'post_tag' ); 983 788 * @uses apply_filters() Calls 'tag_link' with tag link and tag ID as parameters. 789 * 790 * @param int $tag_id Tag (term) ID. 791 * @return string 792 */ 793 function get_tag_link( $tag_id ) { 794 global $wp_rewrite; 795 $taglink = $wp_rewrite->get_tag_permastruct(); 796 797 $tag = &get_term( $tag_id, 'post_tag' ); 984 798 if ( is_wp_error( $tag ) ) 985 return ''; 986 987 return $tag; 799 return $tag; 800 $slug = $tag->slug; 801 802 if ( empty( $taglink ) ) { 803 $file = get_option( 'home' ) . '/'; 804 $taglink = $file . '?tag=' . $slug; 805 } else { 806 $taglink = str_replace( '%tag%', $slug, $taglink ); 807 $taglink = get_option( 'home' ) . user_trailingslashit( $taglink, 'category' ); 808 } 809 return apply_filters( 'tag_link', $taglink, $tag_id ); 988 810 } 989 811 … … 1054 876 function term_description( $term = 0, $taxonomy = 'post_tag' ) { 1055 877 if ( !$term && ( is_tax() || is_tag() || is_category() ) ) { 1056 $term = get_queried_object(); 878 global $wp_query; 879 $term = $wp_query->get_queried_object(); 1057 880 $taxonomy = $term->taxonomy; 1058 881 $term = $term->term_id; … … 1086 909 1087 910 $terms = get_object_term_cache( $id, $taxonomy ); 1088 if ( false === $terms ) {911 if ( false === $terms ) 1089 912 $terms = wp_get_object_terms( $id, $taxonomy ); 1090 wp_cache_add($id, $terms, $taxonomy . '_relationships');1091 }1092 1093 $terms = apply_filters( 'get_the_terms', $terms, $id, $taxonomy );1094 913 1095 914 if ( empty( $terms ) ) … … 1137 956 * @since 2.5.0 1138 957 * 1139 * @param int $id PostID.958 * @param int $id Term ID. 1140 959 * @param string $taxonomy Taxonomy name. 1141 960 * @param string $before Optional. Before list. … … 1144 963 * @return null|bool False on WordPress error. Returns null when displaying. 1145 964 */ 1146 function the_terms( $id = 0, $taxonomy, $before = '', $sep = ', ', $after = '' ) {965 function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) { 1147 966 $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after ); 1148 967 … … 1151 970 1152 971 echo apply_filters('the_terms', $term_list, $taxonomy, $before, $sep, $after); 1153 }1154 1155 1156 /**1157 * Check if the current post has any of given category.1158 *1159 * @since 3.1.01160 *1161 * @param string|int|array $tag Optional. The category name/term_id/slug or array of them to check for.1162 * @param int|object $post Optional. Post to check instead of the current post.1163 * @return bool True if the current post has any of the given categories (or any category, if no category specified).1164 */1165 function has_category( $category = '', $post = null ) {1166 return has_term( $category, 'category', $post );1167 972 } 1168 973 … … 1180 985 * @since 2.6.0 1181 986 * 987 * @uses is_object_in_term() 988 * 1182 989 * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for. 1183 * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0) 1184 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified). 1185 */ 1186 function has_tag( $tag = '', $post = null ) { 1187 return has_term( $tag, 'post_tag', $post ); 1188 } 1189 1190 /** 1191 * Check if the current post has any of given terms. 1192 * 1193 * The given terms are checked against the post's terms' term_ids, names and slugs. 1194 * Terms given as integers will only be checked against the post's terms' term_ids. 1195 * If no terms are given, determines if post has any terms. 1196 * 1197 * @since 3.1.0 1198 * 1199 * @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for. 1200 * @param string $taxonomy Taxonomy name 1201 * @param int|object $post Optional. Post to check instead of the current post. 1202 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified). 1203 */ 1204 function has_term( $term = '', $taxonomy = '', $post = null ) { 1205 $post = get_post($post); 1206 1207 if ( !$post ) 990 * @param int|post object Optional. Post to check instead of the current post. @since 2.7.0 991 * @return bool True if the current post has any of the the given tags (or any tag, if no tag specified). 992 */ 993 function has_tag( $tag = '', $_post = null ) { 994 if ( $_post ) { 995 $_post = get_post( $_post ); 996 } else { 997 $_post =& $GLOBALS['post']; 998 } 999 1000 if ( !$_post ) 1208 1001 return false; 1209 1002 1210 $r = is_object_in_term( $ post->ID, $taxonomy, $term);1003 $r = is_object_in_term( $_post->ID, 'post_tag', $tag ); 1211 1004 if ( is_wp_error( $r ) ) 1212 1005 return false; 1213 1214 1006 return $r; 1215 1007 }
Note: See TracChangeset
for help on using the changeset viewer.