Changeset 7520 for trunk/wp-includes/taxonomy.php
- Timestamp:
- 03/26/2008 06:37:19 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/taxonomy.php
r7507 r7520 35 35 * @uses $wp_taxonomies 36 36 * 37 * @param array|string $object_type Name of the type of taxonomy object37 * @param array|string|object $object Name of the type of taxonomy object, or an object (row from posts) 38 38 * @return array The names of all taxonomy of $object_type. 39 39 */ 40 function get_object_taxonomies($object _type) {40 function get_object_taxonomies($object) { 41 41 global $wp_taxonomies; 42 43 if ( is_object($object) ) { 44 if ( $object->post_type == 'attachment' ) 45 return get_attachment_taxonomies($object); 46 $object = $object->post_type; 47 } 48 49 $object = (array) $object; 42 50 43 51 $taxonomies = array(); 44 52 foreach ( $wp_taxonomies as $taxonomy ) { 45 if ( in_array($object_type, (array) $taxonomy->object_type) )53 if ( array_intersect($object, (array) $taxonomy->object_type) ) 46 54 $taxonomies[] = $taxonomy->name; 47 55 } … … 120 128 121 129 /** 122 * register_taxonomy() - Create or modify a taxonomy object. 130 * register_taxonomy() - Create or modify a taxonomy object. Do not use before init. 123 131 * 124 132 * A simple function for creating or modifying a taxonomy object based on the parameters given. … … 126 134 * taxonomy name and another string for the object type. 127 135 * 128 * The function keeps a default set, allowing for the $args to be optional but allow the other129 * functions to still work. It is possible to overwrite the default set, which contains two130 * keys: hierarchical and update_count_callback.131 *132 136 * Nothing is returned, so expect error maybe or use is_taxonomy() to check whether taxonomy exists. 133 137 * … … 135 139 * hierarachical - has some defined purpose at other parts of the API and is a boolean value. 136 140 * update_count_callback - works much like a hook, in that it will be called when the count is updated. 141 * rewrite - false to prevent rewrite, or array('slug'=>$slug) to customize permastruct; default will use $taxonomy as slug 142 * query_var - false to prevent queries, or string to customize query var (?$query_var=$term); default will use $taxonomy as query var 137 143 * 138 144 * @package WordPress … … 140 146 * @since 2.3 141 147 * @uses $wp_taxonomies Inserts new taxonomy object into the list 148 * @uses $wp_rewrite Adds rewrite tags and permastructs 149 * @uses $wp Adds query vars 142 150 * 143 151 * @param string $taxonomy Name of taxonomy object … … 146 154 */ 147 155 function register_taxonomy( $taxonomy, $object_type, $args = array() ) { 148 global $wp_taxonomies, $wp_rewrite ;149 150 $defaults = array('hierarchical' => false, 'update_count_callback' => '' );156 global $wp_taxonomies, $wp_rewrite, $wp; 157 158 $defaults = array('hierarchical' => false, 'update_count_callback' => '', 'rewrite' => true, 'query_var' => true); 151 159 $args = wp_parse_args($args, $defaults); 152 160 153 if ( !empty( $args['rewrite'] ) ) { 161 if ( false !== $args['query_var'] ) { 162 if ( empty($args['query_var']) ) 163 $args['query_var'] = $taxonomy; 164 $args['query_var'] = sanitize_title_with_dashes($args['query_var']); 165 $wp->add_query_var($args['query_var']); 166 } 167 168 if ( false !== $args['rewrite'] ) { 154 169 if ( !is_array($args['rewrite']) ) 155 170 $args['rewrite'] = array(); 156 171 if ( !isset($args['rewrite']['slug']) ) 157 172 $args['rewrite']['slug'] = sanitize_title_with_dashes($taxonomy); 158 $wp_rewrite->add_rewrite_tag("%$taxonomy%", '([^/]+)', "taxonomy=$taxonomy&term=");159 $wp_rewrite->add_permastruct( "{$args['rewrite']['slug']}/%$taxonomy%");173 $wp_rewrite->add_rewrite_tag("%$taxonomy%", '([^/]+)', $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=$term"); 174 $wp_rewrite->add_permastruct($taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%"); 160 175 } 161 176 … … 1037 1052 $defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all'); 1038 1053 $args = wp_parse_args( $args, $defaults ); 1054 1055 $terms = array(); 1056 if ( count($taxonomies) > 1 ) { 1057 foreach ( $taxonomies as $index => $taxonomy ) { 1058 $t = get_taxonomy($taxonomy); 1059 if ( is_array($t->args) && $args != array_merge($args, $t->args) ) { 1060 unset($taxonomies[$index]); 1061 $terms = array_merge($terms, wp_get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args))); 1062 } 1063 } 1064 } else { 1065 $t = get_taxonomy($taxonomies[0]); 1066 if ( is_array($t->args) ) 1067 $args = array_merge($args, $t->args); 1068 } 1069 1039 1070 extract($args, EXTR_SKIP); 1040 1071 … … 1068 1099 1069 1100 if ( 'all' == $fields || 'all_with_object_id' == $fields ) { 1070 $terms = $wpdb->get_results($query);1101 $terms = array_merge($terms, $wpdb->get_results($query)); 1071 1102 update_term_cache($terms); 1072 1103 } else if ( 'ids' == $fields || 'names' == $fields ) { 1073 $terms = $wpdb->get_col($query);1104 $terms = array_merge($terms, $wpdb->get_col($query)); 1074 1105 } else if ( 'tt_ids' == $fields ) { 1075 1106 $terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) ORDER BY tr.term_taxonomy_id $order"); … … 1900 1931 } 1901 1932 1933 /** 1934 * get_term_link() - Generates a permalink for a taxonomy term archive 1935 * 1936 * @param object|int|string $term 1937 * @param string $taxonomy 1938 * @return string HTML link to taxonomy term archive 1939 */ 1940 function get_term_link( $term, $taxonomy ) { 1941 global $wp_rewrite; 1942 1943 $termlink = $wp_rewrite->get_extra_permastruct($taxonomy); 1944 1945 if ( !is_object($term) ) { 1946 if ( is_int($term) ) { 1947 $term = &get_term($term, $taxonomy); 1948 } else { 1949 $term = &get_term_by('slug', $term, $taxonomy); 1950 } 1951 } 1952 if ( is_wp_error( $term ) ) 1953 return $term; 1954 1955 $slug = $term->slug; 1956 1957 if ( empty($termlink) ) { 1958 $file = get_option('home') . '/'; 1959 $t = get_taxonomy($taxonomy); 1960 if ( $t->query_var ) 1961 $termlink = "$file?$t->query_var=$slug"; 1962 else 1963 $termlink = "$file?taxonomy=$taxonomy&term=$slug"; 1964 } else { 1965 $termlink = str_replace("%$taxonomy%", $slug, $termlink); 1966 $termlink = get_option('home') . user_trailingslashit($termlink, 'category'); 1967 } 1968 return apply_filters('term_link', $termlink, $term, $taxonomy); 1969 } 1970 1971 function the_taxonomies($args = array()) { 1972 $defaults = array( 1973 'post' => 0, 1974 'before' => '', 1975 'sep' => ' ', 1976 'after' => '', 1977 ); 1978 1979 $r = wp_parse_args( $args, $defaults ); 1980 extract( $r, EXTR_SKIP ); 1981 1982 echo $before . join($sep, get_the_taxonomies($post)) . $after; 1983 } 1984 1985 function get_the_taxonomies($post = 0) { 1986 if ( is_int($post) ) 1987 $post =& get_post($post); 1988 elseif ( !is_object($post) ) 1989 $post =& $GLOBALS['post']; 1990 1991 $taxonomies = array(); 1992 1993 if ( !$post ) 1994 return $taxonomies; 1995 1996 $_template = '%s: %l.'; 1997 1998 foreach ( get_object_taxonomies($post) as $taxonomy ) { 1999 $t = (array) get_taxonomy($taxonomy); 2000 if ( empty($t['label']) ) 2001 $t['label'] = $taxonomy; 2002 if ( empty($t['args']) ) 2003 $t['args'] = array(); 2004 if ( empty($t['template']) ) 2005 $t['template'] = $_template; 2006 2007 $terms = get_object_term_cache($post->ID, $taxonomy); 2008 if ( empty($terms) ) 2009 $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']); 2010 2011 $links = array(); 2012 2013 foreach ( $terms as $term ) 2014 $links[] = "<a href='" . attribute_escape(get_term_link($term, $taxonomy)) . "'>$term->name</a>"; 2015 2016 if ( $links ) 2017 $taxonomies[$taxonomy] = wp_sprintf($t['template'], $t['label'], $links, $terms); 2018 } 2019 return $taxonomies; 2020 } 2021 2022 function get_post_taxonomies($post = 0) { 2023 $post =& get_post($post); 2024 2025 return get_object_taxonomies($post); 2026 } 2027 1902 2028 ?>
Note: See TracChangeset
for help on using the changeset viewer.