- File:
-
- 1 edited
-
trunk/wp-includes/category-template.php (modified) (22 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/category-template.php
r15220 r17443 11 11 * 12 12 * @since 1.0.0 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 ); 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; 39 28 } 40 29 … … 60 49 $name = $parent->slug; 61 50 else 62 $name = $parent-> cat_name;51 $name = $parent->name; 63 52 64 53 if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) { … … 68 57 69 58 if ( $link ) 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;59 $chain .= '<a href="' . get_category_link( $parent->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator; 71 60 else 72 61 $chain .= $name.$separator; … … 84 73 */ 85 74 function get_the_category( $id = false ) { 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 75 $categories = get_the_terms( $id, 'category' ); 76 if ( ! $categories ) 101 77 $categories = array(); 102 78 103 foreach ( (array) array_keys( $categories ) as $key ) { 79 $categories = array_values( $categories ); 80 81 foreach ( array_keys( $categories ) as $key ) { 104 82 _make_cat_compat( $categories[$key] ); 105 83 } 106 84 107 return $categories; 85 // Filter name is plural because we return alot of categories not just one 86 return apply_filters( 'get_the_categories', $categories ); 108 87 } 109 88 … … 203 182 case '': 204 183 default: 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>';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>'; 206 185 } 207 186 } … … 216 195 if ( $category->parent ) 217 196 $thelist .= get_category_parents( $category->parent, true, $separator ); 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>';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>'; 219 198 break; 220 199 case 'single': … … 222 201 if ( $category->parent ) 223 202 $thelist .= get_category_parents( $category->parent, false, $separator ); 224 $thelist .= "$category-> cat_name</a>";203 $thelist .= "$category->name</a>"; 225 204 break; 226 205 case '': … … 249 228 * @since 1.2.0 250 229 * 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 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) 255 232 * @return bool True if the current post is in any of the given categories. 256 233 */ 257 function in_category( $category, $ _post = null ) {234 function in_category( $category, $post = null ) { 258 235 if ( empty( $category ) ) 259 236 return false; 260 237 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; 238 return has_term( $category, 'category', $post ); 274 239 } 275 240 … … 490 455 $output = ''; 491 456 if ( $title_li && 'list' == $style ) 492 $output = '<li class="' . $class. '">' . $title_li . '<ul>';457 $output = '<li class="' . esc_attr( $class ) . '">' . $title_li . '<ul>'; 493 458 494 459 if ( empty( $categories ) ) { … … 500 465 } 501 466 } else { 502 global $wp_query;503 504 467 if( !empty( $show_option_all ) ) 505 468 if ( 'list' == $style ) … … 508 471 $output .= '<a href="' . get_bloginfo( 'url' ) . '">' . $show_option_all . '</a>'; 509 472 510 if ( empty( $r['current_category'] ) && ( is_category() || is_tax() ) ) 511 $r['current_category'] = $wp_query->get_queried_object_id(); 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 } 512 478 513 479 if ( $hierarchical ) … … 573 539 foreach ( $tags as $key => $tag ) { 574 540 if ( 'edit' == $args['link'] ) 575 $link = get_edit_tag_link( $tag->term_id, $ args['taxonomy']);541 $link = get_edit_tag_link( $tag->term_id, $tag->taxonomy ); 576 542 else 577 $link = get_term_link( intval($tag->term_id), $ args['taxonomy']);543 $link = get_term_link( intval($tag->term_id), $tag->taxonomy ); 578 544 if ( is_wp_error( $link ) ) 579 545 return false; … … 713 679 $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key; 714 680 $tag_name = $tags[ $key ]->name; 715 $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( $topic_count_text_callback($real_count ) ) . "' style='font-size: " .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: " . 716 682 ( $smallest + ( ( $count - $min_count ) * $font_step ) ) 717 683 . "$unit;'>$tag_name</a>"; … … 732 698 endswitch; 733 699 734 if ( $filter )700 if ( $filter ) 735 701 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args ); 736 else702 else 737 703 return $return; 704 } 705 706 /** 707 * Callback for comparing tags based on name 708 * 709 * @since 3.1.0 710 * @access private 711 */ 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 count 718 * 719 * @since 3.1.0 720 * @access private 721 */ 722 function _wp_tag_cloud_count_sort_cb( $a, $b ) { 723 return ( $a->count > $b->count ); 738 724 } 739 725 … … 778 764 } 779 765 766 /** 767 * Create HTML list of categories. 768 * 769 * @package WordPress 770 * @since 2.1.0 771 * @uses Walker 772 */ 773 class Walker_Category extends Walker { 774 /** 775 * @see Walker::$tree_type 776 * @since 2.1.0 777 * @var string 778 */ 779 var $tree_type = 'category'; 780 781 /** 782 * @see Walker::$db_fields 783 * @since 2.1.0 784 * @todo Decouple this 785 * @var array 786 */ 787 var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); 788 789 /** 790 * @see Walker::start_lvl() 791 * @since 2.1.0 792 * 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.0 808 * 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.0 824 * 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 $args 829 */ 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 else 839 $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 else 865 $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.0 899 * 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 WordPress 918 * @since 2.1.0 919 * @uses Walker 920 */ 921 class Walker_CategoryDropdown extends Walker { 922 /** 923 * @see Walker::$tree_type 924 * @since 2.1.0 925 * @var string 926 */ 927 var $tree_type = 'category'; 928 929 /** 930 * @see Walker::$db_fields 931 * @since 2.1.0 932 * @todo Decouple this 933 * @var array 934 */ 935 var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); 936 937 /** 938 * @see Walker::start_el() 939 * @since 2.1.0 940 * 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 780 965 // 781 966 // Tags … … 786 971 * 787 972 * @since 2.3.0 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' ); 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 798 984 if ( is_wp_error( $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 ); 985 return ''; 986 987 return $tag; 810 988 } 811 989 … … 876 1054 function term_description( $term = 0, $taxonomy = 'post_tag' ) { 877 1055 if ( !$term && ( is_tax() || is_tag() || is_category() ) ) { 878 global $wp_query; 879 $term = $wp_query->get_queried_object(); 1056 $term = get_queried_object(); 880 1057 $taxonomy = $term->taxonomy; 881 1058 $term = $term->term_id; … … 909 1086 910 1087 $terms = get_object_term_cache( $id, $taxonomy ); 911 if ( false === $terms ) 1088 if ( false === $terms ) { 912 1089 $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 ); 913 1094 914 1095 if ( empty( $terms ) ) … … 956 1137 * @since 2.5.0 957 1138 * 958 * @param int $id TermID.1139 * @param int $id Post ID. 959 1140 * @param string $taxonomy Taxonomy name. 960 1141 * @param string $before Optional. Before list. … … 963 1144 * @return null|bool False on WordPress error. Returns null when displaying. 964 1145 */ 965 function the_terms( $id , $taxonomy, $before = '', $sep = ', ', $after = '' ) {1146 function the_terms( $id = 0, $taxonomy, $before = '', $sep = ', ', $after = '' ) { 966 1147 $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after ); 967 1148 … … 970 1151 971 1152 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.0 1160 * 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 ); 972 1167 } 973 1168 … … 985 1180 * @since 2.6.0 986 1181 * 987 * @uses is_object_in_term()988 *989 1182 * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for. 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 ) 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 ) 1001 1208 return false; 1002 1209 1003 $r = is_object_in_term( $ _post->ID, 'post_tag', $tag);1210 $r = is_object_in_term( $post->ID, $taxonomy, $term ); 1004 1211 if ( is_wp_error( $r ) ) 1005 1212 return false; 1213 1006 1214 return $r; 1007 1215 }
Note: See TracChangeset
for help on using the changeset viewer.