Make WordPress Core

Changeset 5521


Ignore:
Timestamp:
05/23/2007 03:57:20 AM (17 years ago)
Author:
ryan
Message:

Add get_terms() and get_term(). Move more of tagging to taxonomy. see #4189

Location:
trunk/wp-includes
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/category-template.php

    r5444 r5521  
    312312    $counts = $tag_links = array();
    313313    foreach ( (array) $tags as $tag ) {
    314         $counts[$tag->cat_name] = $tag->tag_count;
    315         $tag_links[$tag->cat_name] = get_tag_link( $tag->cat_ID );
     314        $counts[$tag->name] = $tag->count;
     315        $tag_links[$tag->name] = get_tag_link( $tag->term_id );
    316316    }
    317317
     
    385385function get_tag_link( $tag_id ) {
    386386    global $wp_rewrite;
    387     $catlink = $wp_rewrite->get_tag_permastruct();
    388 
    389     $category = &get_category($tag_id);
    390     $category_nicename = $category->category_nicename;
     387    $taglink = $wp_rewrite->get_tag_permastruct();
     388
     389    $tag = &get_term($tag_id, 'post_tag');
     390    $slug = $tag->slug;
    391391
    392392    if ( empty($catlink) ) {
    393393        $file = get_option('home') . '/';
    394         $catlink = $file . '?tag=' . $category_nicename;
     394        $taglink = $file . '?tag=' . $slug;
    395395    } else {
    396 
    397         $catlink = str_replace('%tag%', $category_nicename, $catlink);
    398         $catlink = get_option('home') . user_trailingslashit($catlink, 'category');
    399     }
    400     return apply_filters('tag_link', $catlink, $tag_id);
     396        $taglink = str_replace('%tag%', $slug, $taglink);
     397        $taglink = get_option('home') . user_trailingslashit($taglink, 'category');
     398    }
     399    return apply_filters('tag_link', $taglink, $tag_id);
    401400}
    402401
     
    427426    $tag_list = $before;
    428427    foreach ( $tags as $tag )
    429         $tag_links[] = '<a href="' . get_tag_link($tag->cat_ID) . '">' . $tag->cat_name . '</a>';
     428        $tag_links[] = '<a href="' . get_tag_link($tag->term_id) . '">' . $tag->slug . '</a>';
    430429
    431430    $tag_links = join( $sep, $tag_links );
  • trunk/wp-includes/category.php

    r5444 r5521  
    354354    global $wpdb, $category_links;
    355355
    356     $defaults = array('orderby' => 'name', 'order' => 'ASC',
    357         'hide_empty' => true, 'exclude' => '', 'include' => '',
    358         'number' => '');
    359     $args = wp_parse_args( $args, $defaults );
    360     if ( 'count' == $args['orderby'] )
    361         $args['orderby'] = 'tag_count';
    362     else
    363         $args['orderby'] = "cat_" . $args['orderby'];  // restricts order by to cat_ID and cat_name fields
    364     $args['number'] = (int) $args['number'];
    365     extract($args);
    366 
    367356    $key = md5( serialize( $args ) );
    368357    if ( $cache = wp_cache_get( 'get_tags', 'category' ) )
     
    370359            return apply_filters('get_tags', $cache[$key], $args);
    371360
    372     $where = 'cat_ID > 0';
    373     $inclusions = '';
    374     if ( !empty($include) ) {
    375         $child_of = 0; //ignore child_of and exclude params if using include
    376         $exclude = '';
    377         $incategories = preg_split('/[\s,]+/',$include);
    378         if ( count($incategories) ) {
    379             foreach ( $incategories as $incat ) {
    380                 if (empty($inclusions))
    381                     $inclusions = ' AND ( cat_ID = ' . intval($incat) . ' ';
    382                 else
    383                     $inclusions .= ' OR cat_ID = ' . intval($incat) . ' ';
    384             }
    385         }
    386     }
    387 
    388     if (!empty($inclusions))
    389         $inclusions .= ')';
    390     $where .= $inclusions;
    391 
    392     $exclusions = '';
    393     if ( !empty($exclude) ) {
    394         $excategories = preg_split('/[\s,]+/',$exclude);
    395         if ( count($excategories) ) {
    396             foreach ( $excategories as $excat ) {
    397                 if (empty($exclusions))
    398                     $exclusions = ' AND ( cat_ID <> ' . intval($excat) . ' ';
    399                 else
    400                     $exclusions .= ' AND cat_ID <> ' . intval($excat) . ' ';
    401             }
    402         }
    403     }
    404 
    405     if (!empty($exclusions))
    406         $exclusions .= ')';
    407     $exclusions = apply_filters('list_tags_exclusions', $exclusions, $args );
    408     $where .= $exclusions;
    409 
    410     if ( $hide_empty )
    411         $where .= ' AND tag_count > 0';
    412 
    413     $where .= ' AND ( type & ' . TAXONOMY_TAG . ' != 0 ) ';
    414 
    415     if ( !empty($number) )
    416         $number = 'LIMIT ' . $number;
    417     else
    418         $number = '';
    419 
    420     $tags = $wpdb->get_results("SELECT * FROM $wpdb->categories WHERE $where ORDER BY $orderby $order $number");
     361
     362    $tags = get_terms('post_tag');
    421363
    422364    if ( empty($tags) )
  • trunk/wp-includes/general-template.php

    r5450 r5521  
    161161
    162162    $cat = get_query_var('cat');
     163    $tag = get_query_var('tag_id');
    163164    $p = get_query_var('p');
    164165    $name = get_query_var('name');
     
    187188        $title = $wpdb->get_var("SELECT cat_name FROM $wpdb->categories WHERE category_nicename = '$category_name'");
    188189        $title = apply_filters('single_cat_title', $title);
     190    }
     191
     192    if ( !empty($tag) ) {
     193        $tag = get_term($tag, 'post_tag');
     194        if ( ! empty($tag->name) )
     195            $title = apply_filters('single_tag_title', $tag->slug);
    189196    }
    190197
  • trunk/wp-includes/query.php

    r5285 r5521  
    9595        return true;
    9696
    97     $cat_obj = $wp_query->get_queried_object();
    98     if ( $category == $cat_obj->category_nicename )
     97    $tag_obj = $wp_query->get_queried_object();
     98    if ( $slug == $tag_obj->slug )
    9999        return true;
    100100    return false;
     
    876876
    877877        if ( '' != $q['tag'] ) {
    878             $reqcat= get_category_by_slug( $q['tag'] );
    879             if ( !empty($reqcat) )
    880                 $reqcat = $reqcat->cat_ID;
     878            $reqtag = is_term( $q['tag'], 'post_tag' );
     879            if ( !empty($reqtag) )
     880                $reqtag = $reqtag['term_id'];
    881881            else
    882                 $reqcat = 0;
    883 
    884             $q['cat'] = $reqcat;
    885 
     882                $reqtag = 0;
     883
     884            $q['tag_id'] = $reqtag;
     885            // TODO: use term taxonomy
    886886            $tables = ", $wpdb->post2cat, $wpdb->categories";
    887             $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) LEFT JOIN $wpdb->categories ON ($wpdb->post2cat.category_id = $wpdb->categories.cat_ID) ";
    888             $whichcat = " AND category_id IN ({$q['cat']}) AND rel_type = 'tag' ";
     887            $join = " LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
     888            $whichcat = " AND $wpdb->term_taxonomy.term_id IN ({$q['tag_id']}) AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
    889889            $groupby = "{$wpdb->posts}.ID";
    890890        }
  • trunk/wp-includes/taxonomy.php

    r5515 r5521  
    126126                continue;
    127127            $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ('$object_id', '$id')");
     128            $wpdb->query("UPDATE $wpdb->term_taxonomy SET count = count + 1 WHERE term_taxonomy_id = $id");
    128129        }
    129130    }
     
    134135            $delete_terms = "'" . implode("', '", $delete_terms) . "'";
    135136            $wpdb->query("DELETE FROM $wpdb->term_relationships WHERE term_taxonomy_id IN ($delete_terms)");
     137            $wpdb->query("UPDATE $wpdb->term_taxonomy SET count = count - 1 WHERE term_taxonomy_id IN ($delete_terms)");
    136138        }
    137139    }
     
    152154
    153155    $taxonomies = "'" . implode("', '", $taxonomies) . "'";
    154     $object_ids = implode(', ', $object_ids);       
    155 
    156     if ( $taxonomy_data = $wpdb->get_results("SELECT t.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids)") ) {
     156    $object_ids = implode(', ', $object_ids);
     157
     158    if ( $taxonomy_data = $wpdb->get_results("SELECT t.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) ORDER BY t.name") ) {
    157159        if ($single_taxonomy && $single_object) {
    158160            // Just one kind of taxonomy for one object.
     
    175177    } else {
    176178        return array();
    177     }       
    178 }   
     179    }
     180}
     181
     182function &get_terms($taxonomies, $args = '') {
     183    global $wpdb;
     184
     185    if ( !is_array($taxonomies) )
     186        $taxonomies = array($taxonomies);
     187    $in_taxonomies = "'" . implode("', '", $taxonomies) . "'";
     188
     189    $defaults = array('orderby' => 'name', 'order' => 'ASC',
     190        'hide_empty' => true, 'exclude' => '', 'include' => '',
     191        'number' => '');
     192    $args = wp_parse_args( $args, $defaults );
     193    $args['number'] = (int) $args['number'];
     194    extract($args);
     195
     196    if ( 'count' == $orderby )
     197        $orderby = 'tt.count';
     198    else if ( 'name' == $orderby )
     199        $orderby = 't.name';
     200
     201    $where = '';
     202    $inclusions = '';
     203    if ( !empty($include) ) {
     204        $exclude = '';
     205        $interms = preg_split('/[\s,]+/',$include);
     206        if ( count($interms) ) {
     207            foreach ( $interms as $interm ) {
     208                if (empty($inclusions))
     209                    $inclusions = ' AND ( t.term_id = ' . intval($interm) . ' ';
     210                else
     211                    $inclusions .= ' OR t.term_id = ' . intval($interm) . ' ';
     212            }
     213        }
     214    }
     215
     216    if ( !empty($inclusions) )
     217        $inclusions .= ')';
     218    $where .= $inclusions;
     219
     220    $exclusions = '';
     221    if ( !empty($exclude) ) {
     222        $exterms = preg_split('/[\s,]+/',$exclude);
     223        if ( count($exterms) ) {
     224            foreach ( $exterms as $exterm ) {
     225                if (empty($exclusions))
     226                    $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
     227                else
     228                    $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
     229            }
     230        }
     231    }
     232
     233    if ( !empty($exclusions) )
     234        $exclusions .= ')';
     235    $exclusions = apply_filters('list_terms_exclusions', $exclusions, $args );
     236    $where .= $exclusions;
     237
     238    if ( $hide_empty )
     239        $where .= ' AND tt.count > 0';
     240
     241    if ( !empty($number) )
     242        $number = 'LIMIT ' . $number;
     243    else
     244        $number = '';
     245
     246    $terms = $wpdb->get_results("SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ($in_taxonomies) $where ORDER BY $orderby $order $number");
     247
     248    if ( empty($terms) )
     249        return array();
     250
     251    $terms = apply_filters('get_terms', $terms, $taxonomies, $args);
     252    return $terms;
     253}
     254
     255function &get_term(&$term, $taxonomy, $output = OBJECT) {
     256    global $wpdb;
     257
     258    if ( empty($term) )
     259        return null;
     260
     261    if ( is_object($term) ) {
     262        wp_cache_add($term->term_id, $term, "term:$taxonomy");
     263        $_term = $term;
     264    } else {
     265        $term = (int) $term;
     266        if ( ! $_term = wp_cache_get($term, "term:$taxonomy") ) {
     267            $_term = $wpdb->get_row("SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = '$taxonomy' AND t.term_id = '$term' LIMIT 1");
     268            wp_cache_add($term, $_term, "term:$taxonomy");
     269        }
     270    }
     271
     272    $_term = apply_filters('get_term', $_term, $taxonomy);
     273
     274    if ( $output == OBJECT ) {
     275        return $_term;
     276    } elseif ( $output == ARRAY_A ) {
     277        return get_object_vars($_term);
     278    } elseif ( $output == ARRAY_N ) {
     279        return array_values(get_object_vars($_term));
     280    } else {
     281        return $_term;
     282    }
     283}
    179284
    180285?>
Note: See TracChangeset for help on using the changeset viewer.