Make WordPress Core


Ignore:
Timestamp:
03/26/2008 06:37:19 AM (17 years ago)
Author:
ryan
Message:

Taxonomy links and template tags from andy. see #6357

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/taxonomy.php

    r7507 r7520  
    3535 * @uses $wp_taxonomies
    3636 *
    37  * @param array|string $object_type Name of the type of taxonomy object
     37 * @param array|string|object $object Name of the type of taxonomy object, or an object (row from posts)
    3838 * @return array The names of all taxonomy of $object_type.
    3939 */
    40 function get_object_taxonomies($object_type) {
     40function get_object_taxonomies($object) {
    4141    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;
    4250
    4351    $taxonomies = array();
    4452    foreach ( $wp_taxonomies as $taxonomy ) {
    45         if ( in_array($object_type, (array) $taxonomy->object_type) )
     53        if ( array_intersect($object, (array) $taxonomy->object_type) )
    4654            $taxonomies[] = $taxonomy->name;
    4755    }
     
    120128
    121129/**
    122  * register_taxonomy() - Create or modify a taxonomy object.
     130 * register_taxonomy() - Create or modify a taxonomy object. Do not use before init.
    123131 *
    124132 * A simple function for creating or modifying a taxonomy object based on the parameters given.
     
    126134 * taxonomy name and another string for the object type.
    127135 *
    128  * The function keeps a default set, allowing for the $args to be optional but allow the other
    129  * functions to still work. It is possible to overwrite the default set, which contains two
    130  * keys: hierarchical and update_count_callback.
    131  *
    132136 * Nothing is returned, so expect error maybe or use is_taxonomy() to check whether taxonomy exists.
    133137 *
     
    135139 * hierarachical - has some defined purpose at other parts of the API and is a boolean value.
    136140 * 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
    137143 *
    138144 * @package WordPress
     
    140146 * @since 2.3
    141147 * @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
    142150 *
    143151 * @param string $taxonomy Name of taxonomy object
     
    146154 */
    147155function 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);
    151159    $args = wp_parse_args($args, $defaults);
    152160
    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'] ) {
    154169        if ( !is_array($args['rewrite']) )
    155170            $args['rewrite'] = array();
    156171        if ( !isset($args['rewrite']['slug']) )
    157172            $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%");
    160175    }
    161176
     
    10371052    $defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
    10381053    $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
    10391070    extract($args, EXTR_SKIP);
    10401071
     
    10681099
    10691100    if ( 'all' == $fields || 'all_with_object_id' == $fields ) {
    1070         $terms = $wpdb->get_results($query);
     1101        $terms = array_merge($terms, $wpdb->get_results($query));
    10711102        update_term_cache($terms);
    10721103    } else if ( 'ids' == $fields || 'names' == $fields ) {
    1073         $terms = $wpdb->get_col($query);
     1104        $terms = array_merge($terms, $wpdb->get_col($query));
    10741105    } else if ( 'tt_ids' == $fields ) {
    10751106        $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");
     
    19001931}
    19011932
     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 */
     1940function 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
     1971function 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
     1985function 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
     2022function get_post_taxonomies($post = 0) {
     2023    $post =& get_post($post);
     2024
     2025    return get_object_taxonomies($post);
     2026}
     2027
    19022028?>
Note: See TracChangeset for help on using the changeset viewer.