Changeset 7520

Show
Ignore:
Timestamp:
03/26/08 06:37:19 (4 months ago)
Author:
ryan
Message:

Taxonomy links and template tags from andy. see #6357

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wp-admin/includes/media.php

    r7510 r7520  
    425425 
    426426    return wp_iframe( 'media_upload_library_form', $errors ); 
    427 } 
    428  
    429 function get_attachment_taxonomies($attachment) { 
    430     if ( is_int( $attachment ) ) 
    431         $attachment = get_post($attachment); 
    432     else if ( is_array($attachment) ) 
    433         $attachment = (object) $attachment; 
    434  
    435     if ( ! is_object($attachment) ) 
    436         return array(); 
    437  
    438     $filename = basename($attachment->guid); 
    439  
    440     $objects = array('attachment'); 
    441  
    442     if ( false !== strpos($filename, '.') ) 
    443         $objects[] = 'attachment:' . substr($filename, strrpos($filename, '.') + 1); 
    444     if ( !empty($attachment->post_mime_type) ) { 
    445         $objects[] = 'attachment:' . $attachment->post_mime_type; 
    446         if ( false !== strpos($attachment->post_mime_type, '/') ) 
    447             foreach ( explode('/', $attachment->post_mime_type) as $token ) 
    448                 if ( !empty($token) ) 
    449                     $objects[] = "attachment:$token"; 
    450     } 
    451  
    452     $taxonomies = array(); 
    453     foreach ( $objects as $object ) 
    454         if ( $taxes = get_object_taxonomies($object) ) 
    455             $taxonomies = array_merge($taxonomies, $taxes); 
    456  
    457     return array_unique($taxonomies); 
    458427} 
    459428 
  • trunk/wp-content/themes/default/image.php

    r7222 r7520  
    2424                        This entry was posted on <?php the_time('l, F jS, Y') ?> at <?php the_time() ?> 
    2525                        and is filed under <?php the_category(', ') ?>. 
     26                        <?php the_taxonomies(); ?> 
    2627                        You can follow any responses to this entry through the <?php post_comments_feed_link('RSS 2.0'); ?> feed. 
    2728 
  • trunk/wp-includes/category-template.php

    r7505 r7520  
    476476 
    477477function get_the_tags( $id = 0 ) { 
     478    return apply_filters( 'get_the_tags', get_the_terms($id, 'post_tag') ); 
     479} 
     480 
     481function get_the_tag_list( $before = '', $sep = '', $after = '' ) { 
     482    return apply_filters( 'the_tags', get_the_term_list(0, 'post_tag', $before, $sep, $after) ); 
     483} 
     484 
     485function the_tags( $before = 'Tags: ', $sep = ', ', $after = '' ) { 
     486    return the_terms( 0, 'post_tag', $before, $sep, $after ); 
     487} 
     488 
     489function get_the_terms( $id = 0, $taxonomy ) { 
    478490    global $post; 
    479491 
     
    486498        $id = (int) $post->ID; 
    487499 
    488     $tags = get_object_term_cache($id, 'post_tag'); 
    489     if ( false === $tags ) 
    490         $tags = wp_get_object_terms($id, 'post_tag'); 
    491  
    492     $tags = apply_filters( 'get_the_tags', $tags ); 
    493     if ( empty( $tags ) ) 
     500    $terms = get_object_term_cache($id, $taxonomy); 
     501    if ( false === $terms ) 
     502        $terms = wp_get_object_terms($id, $taxonomy); 
     503 
     504    if ( empty( $terms ) ) 
    494505        return false; 
    495     return $tags; 
    496 
    497  
    498 function get_the_tag_list( $before = '', $sep = '', $after = '' ) { 
    499     $tags = get_the_tags(); 
    500  
    501     if ( empty( $tags ) ) 
     506 
     507    return $terms; 
     508
     509 
     510function get_the_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after = '' ) { 
     511    $terms = get_the_terms($id, $taxonomy); 
     512 
     513    if ( is_wp_error($terms) ) 
     514        return $terms; 
     515 
     516    if ( empty( $terms ) ) 
    502517        return false; 
    503518 
    504     $tag_list = $before; 
    505     foreach ( $tags as $tag ) { 
    506         $link = get_tag_link($tag->term_id); 
     519    foreach ( $terms as $term ) { 
     520        $link = get_term_link($term, $taxonomy); 
    507521        if ( is_wp_error( $link ) ) 
    508522            return $link; 
    509         $tag_links[] = '<a href="' . $link . '" rel="tag">' . $tag->name . '</a>'; 
    510     } 
    511  
    512     $tag_links = join( $sep, $tag_links ); 
    513     $tag_links = apply_filters( 'the_tags', $tag_links ); 
    514     $tag_list .= $tag_links; 
    515  
    516     $tag_list .= $after; 
    517  
    518     return $tag_list; 
    519 
    520  
    521 function the_tags( $before = 'Tags: ', $sep = ', ', $after = '' ) { 
    522     $return = get_the_tag_list($before, $sep, $after); 
     523        $term_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>'; 
     524    } 
     525 
     526    $term_links = apply_filters( "term_links-$taxonomy", $term_links ); 
     527 
     528    return $before . join($sep, $term_links) . $after; 
     529
     530 
     531function the_terms( $id, $taxonomy, $before = '', $sep = '', $after = '' ) { 
     532    $return = get_the_term_list($id, $taxonomy, $before, $sep, $after); 
    523533    if ( is_wp_error( $return ) ) 
    524534        return false; 
  • trunk/wp-includes/classes.php

    r7503 r7520  
    1515 
    1616    function add_query_var($qv) { 
    17         $this->public_query_vars[] = $qv; 
     17        if ( !in_array($qv, $this->public_query_vars) ) 
     18            $this->public_query_vars[] = $qv; 
    1819    } 
    1920 
  • trunk/wp-includes/formatting.php

    r7497 r7520  
    13771377    $result = array_shift($args); 
    13781378    if ( count($args) == 1 ) 
    1379         $result .= $l['between_two'] . array_shift($args); 
     1379        $result .= $l['between_only_two'] . array_shift($args); 
    13801380    // Loop when more than two args 
    13811381    while ( count($args) ) { 
  • trunk/wp-includes/media.php

    r7516 r7520  
    439439} 
    440440 
     441function get_attachment_taxonomies($attachment) { 
     442    if ( is_int( $attachment ) ) 
     443        $attachment = get_post($attachment); 
     444    else if ( is_array($attachment) ) 
     445        $attachment = (object) $attachment; 
     446 
     447    if ( ! is_object($attachment) ) 
     448        return array(); 
     449 
     450    $filename = basename($attachment->guid); 
     451 
     452    $objects = array('attachment'); 
     453 
     454    if ( false !== strpos($filename, '.') ) 
     455        $objects[] = 'attachment:' . substr($filename, strrpos($filename, '.') + 1); 
     456    if ( !empty($attachment->post_mime_type) ) { 
     457        $objects[] = 'attachment:' . $attachment->post_mime_type; 
     458        if ( false !== strpos($attachment->post_mime_type, '/') ) 
     459            foreach ( explode('/', $attachment->post_mime_type) as $token ) 
     460                if ( !empty($token) ) 
     461                    $objects[] = "attachment:$token"; 
     462    } 
     463 
     464    $taxonomies = array(); 
     465    foreach ( $objects as $object ) 
     466        if ( $taxes = get_object_taxonomies($object) ) 
     467            $taxonomies = array_merge($taxonomies, $taxes); 
     468 
     469    return array_unique($taxonomies); 
     470} 
     471 
    441472?> 
  • trunk/wp-includes/query.php

    r7511 r7520  
    681681            if ( empty($qv['taxonomy']) || empty($qv['term']) ) { 
    682682                $this->is_tax = false; 
     683                foreach ( $GLOBALS['wp_taxonomies'] as $t ) { 
     684                    if ( isset($t->query_var) && '' != $qv[$t->query_var] ) { 
     685                        $this->is_tax = true; 
     686                        break; 
     687                    } 
     688                } 
    683689            } else { 
    684690                $this->is_tax = true; 
     
    11471153        // Taxonomies 
    11481154        if ( $this->is_tax ) { 
    1149             $terms = get_terms($q['taxonomy'], array('slug'=>$q['term'])); 
    1150             foreach ( $terms as $term ) 
    1151                 $term_ids[] = $term->term_id; 
    1152             $post_ids = get_objects_in_term($term_ids, $q['taxonomy']); 
    1153  
    1154             if ( count($post_ids) ) { 
    1155                 $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") "; 
    1156                 $post_type = 'any'; 
    1157                 $q['post_status'] = 'publish'; 
    1158                 $post_status_join = true; 
     1155            if ( '' != $q['taxonomy'] ) { 
     1156                $taxonomy = $q['taxonomy']; 
     1157                $tt[$taxonomy] = $q['term']; 
     1158                $terms = get_terms($q['taxonomy'], array('slug'=>$q['term'])); 
    11591159            } else { 
    1160                 $whichcat = " AND 0 = 1"; 
     1160                foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) { 
     1161                    if ( isset($t->query_var) && '' != $q[$t->query_var] ) { 
     1162                        $terms = get_terms($taxonomy, array('slug'=>$q[$t->query_var])); 
     1163                        if ( !is_wp_error($terms) ) 
     1164                            break; 
     1165                    } 
     1166                } 
     1167            } 
     1168            if ( is_wp_error($terms) || empty($terms) ) { 
     1169                $whichcat = " AND 0 "; 
     1170            } else { 
     1171                foreach ( $terms as $term ) 
     1172                    $term_ids[] = $term->term_id; 
     1173                $post_ids = get_objects_in_term($term_ids, $taxonomy); 
     1174                if ( !is_wp_error($post_ids) && count($post_ids) ) { 
     1175                    $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") "; 
     1176                    $post_type = 'any'; 
     1177                    $q['post_status'] = 'publish'; 
     1178                    $post_status_join = true; 
     1179                } else { 
     1180                    $whichcat = " AND 0 "; 
     1181                } 
    11611182            } 
    11621183        } 
     
    12971318            } 
    12981319            if ( $post_status_join ) { 
    1299                 $join .= " INNER JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) "; 
     1320                $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) "; 
    13001321                foreach ( $statuswheres as $index => $statuswhere ) 
    13011322                    $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))"; 
  • trunk/wp-includes/rewrite.php

    r7493 r7520  
    462462    } 
    463463 
     464    function get_extra_permastruct($name) { 
     465        if ( isset($this->extra_permastructs[$name]) ) 
     466            return $this->extra_permastructs[$name]; 
     467        return false; 
     468    } 
     469 
    464470    function get_author_permastruct() { 
    465471        if (isset($this->author_structure)) { 
     
    833839 
    834840        // Extra permastructs 
    835         $extra_rewrite = array(); 
    836841        foreach ( $this->extra_permastructs as $permastruct ) 
    837             $extra_rewrite = array_merge($extra_rewrite, $this->generate_rewrite_rules($permastruct, EP_NONE)); 
     842            $this->extra_rules_top = array_merge($this->extra_rules_top, $this->generate_rewrite_rules($permastruct, EP_NONE)); 
    838843 
    839844        // Put them together. 
    840845        if ( $this->use_verbose_page_rules ) 
    841             $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $default_feeds, $page_rewrite, $root_rewrite, $comments_rewrite, $search_rewrite, $category_rewrite, $tag_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $extra_rewrite, $this->extra_rules); 
     846            $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $default_feeds, $page_rewrite, $root_rewrite, $comments_rewrite, $search_rewrite, $category_rewrite, $tag_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $this->extra_rules); 
    842847        else 
    843             $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $default_feeds, $root_rewrite, $comments_rewrite, $search_rewrite, $category_rewrite, $tag_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $extra_rewrite, $page_rewrite, $this->extra_rules); 
     848            $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $default_feeds, $root_rewrite, $comments_rewrite, $search_rewrite, $category_rewrite, $tag_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules); 
    844849 
    845850        do_action_ref_array('generate_rewrite_rules', array(&$this)); 
     
    955960    } 
    956961 
    957     function add_permastruct($struct, $with_front = true) { 
     962    function add_permastruct($name, $struct, $with_front = true) { 
    958963        if ( $with_front ) 
    959964            $struct = $this->front . $struct; 
    960         $this->extra_permastructs[] = $struct; 
     965        $this->extra_permastructs[$name] = $struct; 
    961966    } 
    962967 
  • 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?>