From 0b795ff92beddcab417c0643d8cbb7e82f7b86b4 Mon Sep 17 00:00:00 2001 From: ryan Date: Wed, 23 May 2007 03:57:20 +0000 Subject: [PATCH] Add get_terms() and get_term(). Move more of tagging to taxonomy. see #4189 git-svn-id: http://svn.automattic.com/wordpress/trunk@5521 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/category-template.php | 21 +++--- wp-includes/category.php | 60 +--------------- wp-includes/general-template.php | 7 ++ wp-includes/query.php | 20 +++--- wp-includes/taxonomy.php | 113 ++++++++++++++++++++++++++++-- 5 files changed, 137 insertions(+), 84 deletions(-) diff --git a/wp-includes/category-template.php b/wp-includes/category-template.php index eea83db83c..ca4eb446ca 100644 --- a/wp-includes/category-template.php +++ b/wp-includes/category-template.php @@ -311,8 +311,8 @@ function wp_generate_tag_cloud( $tags, $args = '' ) { return; $counts = $tag_links = array(); foreach ( (array) $tags as $tag ) { - $counts[$tag->cat_name] = $tag->tag_count; - $tag_links[$tag->cat_name] = get_tag_link( $tag->cat_ID ); + $counts[$tag->name] = $tag->count; + $tag_links[$tag->name] = get_tag_link( $tag->term_id ); } $min_count = min($counts); @@ -384,20 +384,19 @@ function walk_category_dropdown_tree() { function get_tag_link( $tag_id ) { global $wp_rewrite; - $catlink = $wp_rewrite->get_tag_permastruct(); + $taglink = $wp_rewrite->get_tag_permastruct(); - $category = &get_category($tag_id); - $category_nicename = $category->category_nicename; + $tag = &get_term($tag_id, 'post_tag'); + $slug = $tag->slug; if ( empty($catlink) ) { $file = get_option('home') . '/'; - $catlink = $file . '?tag=' . $category_nicename; + $taglink = $file . '?tag=' . $slug; } else { - - $catlink = str_replace('%tag%', $category_nicename, $catlink); - $catlink = get_option('home') . user_trailingslashit($catlink, 'category'); + $taglink = str_replace('%tag%', $slug, $taglink); + $taglink = get_option('home') . user_trailingslashit($taglink, 'category'); } - return apply_filters('tag_link', $catlink, $tag_id); + return apply_filters('tag_link', $taglink, $tag_id); } function get_the_tags( $id = 0 ) { @@ -426,7 +425,7 @@ function the_tags( $before = 'Tags: ', $sep = ', ', $after = '' ) { $tag_list = $before; foreach ( $tags as $tag ) - $tag_links[] = '' . $tag->cat_name . ''; + $tag_links[] = '' . $tag->slug . ''; $tag_links = join( $sep, $tag_links ); $tag_links = apply_filters( 'the_tags', $tag_links ); diff --git a/wp-includes/category.php b/wp-includes/category.php index 37460c69cc..6a61decc96 100644 --- a/wp-includes/category.php +++ b/wp-includes/category.php @@ -353,71 +353,13 @@ function _get_category_hierarchy() { function &get_tags($args = '') { global $wpdb, $category_links; - $defaults = array('orderby' => 'name', 'order' => 'ASC', - 'hide_empty' => true, 'exclude' => '', 'include' => '', - 'number' => ''); - $args = wp_parse_args( $args, $defaults ); - if ( 'count' == $args['orderby'] ) - $args['orderby'] = 'tag_count'; - else - $args['orderby'] = "cat_" . $args['orderby']; // restricts order by to cat_ID and cat_name fields - $args['number'] = (int) $args['number']; - extract($args); - $key = md5( serialize( $args ) ); if ( $cache = wp_cache_get( 'get_tags', 'category' ) ) if ( isset( $cache[ $key ] ) ) return apply_filters('get_tags', $cache[$key], $args); - $where = 'cat_ID > 0'; - $inclusions = ''; - if ( !empty($include) ) { - $child_of = 0; //ignore child_of and exclude params if using include - $exclude = ''; - $incategories = preg_split('/[\s,]+/',$include); - if ( count($incategories) ) { - foreach ( $incategories as $incat ) { - if (empty($inclusions)) - $inclusions = ' AND ( cat_ID = ' . intval($incat) . ' '; - else - $inclusions .= ' OR cat_ID = ' . intval($incat) . ' '; - } - } - } - if (!empty($inclusions)) - $inclusions .= ')'; - $where .= $inclusions; - - $exclusions = ''; - if ( !empty($exclude) ) { - $excategories = preg_split('/[\s,]+/',$exclude); - if ( count($excategories) ) { - foreach ( $excategories as $excat ) { - if (empty($exclusions)) - $exclusions = ' AND ( cat_ID <> ' . intval($excat) . ' '; - else - $exclusions .= ' AND cat_ID <> ' . intval($excat) . ' '; - } - } - } - - if (!empty($exclusions)) - $exclusions .= ')'; - $exclusions = apply_filters('list_tags_exclusions', $exclusions, $args ); - $where .= $exclusions; - - if ( $hide_empty ) - $where .= ' AND tag_count > 0'; - - $where .= ' AND ( type & ' . TAXONOMY_TAG . ' != 0 ) '; - - if ( !empty($number) ) - $number = 'LIMIT ' . $number; - else - $number = ''; - - $tags = $wpdb->get_results("SELECT * FROM $wpdb->categories WHERE $where ORDER BY $orderby $order $number"); + $tags = get_terms('post_tag'); if ( empty($tags) ) return array(); diff --git a/wp-includes/general-template.php b/wp-includes/general-template.php index 3610bbb529..55a8fc4a5b 100644 --- a/wp-includes/general-template.php +++ b/wp-includes/general-template.php @@ -160,6 +160,7 @@ function wp_title($sep = '»', $display = true) { global $wpdb, $wp_locale, $wp_query; $cat = get_query_var('cat'); + $tag = get_query_var('tag_id'); $p = get_query_var('p'); $name = get_query_var('name'); $category_name = get_query_var('category_name'); @@ -188,6 +189,12 @@ function wp_title($sep = '»', $display = true) { $title = apply_filters('single_cat_title', $title); } + if ( !empty($tag) ) { + $tag = get_term($tag, 'post_tag'); + if ( ! empty($tag->name) ) + $title = apply_filters('single_tag_title', $tag->slug); + } + // If there's an author if ( !empty($author) ) { $title = get_userdata($author); diff --git a/wp-includes/query.php b/wp-includes/query.php index 67bfa16cea..1ac1e1ccec 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -94,8 +94,8 @@ function is_tag( $slug = '' ) { if ( empty( $slug ) ) return true; - $cat_obj = $wp_query->get_queried_object(); - if ( $category == $cat_obj->category_nicename ) + $tag_obj = $wp_query->get_queried_object(); + if ( $slug == $tag_obj->slug ) return true; return false; } @@ -875,17 +875,17 @@ class WP_Query { } if ( '' != $q['tag'] ) { - $reqcat= get_category_by_slug( $q['tag'] ); - if ( !empty($reqcat) ) - $reqcat = $reqcat->cat_ID; + $reqtag = is_term( $q['tag'], 'post_tag' ); + if ( !empty($reqtag) ) + $reqtag = $reqtag['term_id']; else - $reqcat = 0; - - $q['cat'] = $reqcat; + $reqtag = 0; + $q['tag_id'] = $reqtag; + // TODO: use term taxonomy $tables = ", $wpdb->post2cat, $wpdb->categories"; - $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) "; - $whichcat = " AND category_id IN ({$q['cat']}) AND rel_type = 'tag' "; + $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) "; + $whichcat = " AND $wpdb->term_taxonomy.term_id IN ({$q['tag_id']}) AND $wpdb->term_taxonomy.taxonomy = 'post_tag' "; $groupby = "{$wpdb->posts}.ID"; } diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index 366644c1b9..b2225f5e62 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -125,6 +125,7 @@ function wp_set_object_terms($object_id, $terms, $taxonomies, $append = false) { if ( $wpdb->get_var("SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = '$object_id' AND term_taxonomy_id = '$id'") ) continue; $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ('$object_id', '$id')"); + $wpdb->query("UPDATE $wpdb->term_taxonomy SET count = count + 1 WHERE term_taxonomy_id = $id"); } } @@ -133,6 +134,7 @@ function wp_set_object_terms($object_id, $terms, $taxonomies, $append = false) { if ( $delete_terms ) { $delete_terms = "'" . implode("', '", $delete_terms) . "'"; $wpdb->query("DELETE FROM $wpdb->term_relationships WHERE term_taxonomy_id IN ($delete_terms)"); + $wpdb->query("UPDATE $wpdb->term_taxonomy SET count = count - 1 WHERE term_taxonomy_id IN ($delete_terms)"); } } @@ -151,9 +153,9 @@ function get_object_terms($object_id, $taxonomy) { $object_ids = ($single_object = !is_array($object_id)) ? array($object_id) : $object_id; $taxonomies = "'" . implode("', '", $taxonomies) . "'"; - $object_ids = implode(', ', $object_ids); + $object_ids = implode(', ', $object_ids); - 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)") ) { + 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") ) { if ($single_taxonomy && $single_object) { // Just one kind of taxonomy for one object. return $taxonomy_data; @@ -174,7 +176,110 @@ function get_object_terms($object_id, $taxonomy) { } } else { return array(); - } -} + } +} + +function &get_terms($taxonomies, $args = '') { + global $wpdb; + + if ( !is_array($taxonomies) ) + $taxonomies = array($taxonomies); + $in_taxonomies = "'" . implode("', '", $taxonomies) . "'"; + + $defaults = array('orderby' => 'name', 'order' => 'ASC', + 'hide_empty' => true, 'exclude' => '', 'include' => '', + 'number' => ''); + $args = wp_parse_args( $args, $defaults ); + $args['number'] = (int) $args['number']; + extract($args); + + if ( 'count' == $orderby ) + $orderby = 'tt.count'; + else if ( 'name' == $orderby ) + $orderby = 't.name'; + + $where = ''; + $inclusions = ''; + if ( !empty($include) ) { + $exclude = ''; + $interms = preg_split('/[\s,]+/',$include); + if ( count($interms) ) { + foreach ( $interms as $interm ) { + if (empty($inclusions)) + $inclusions = ' AND ( t.term_id = ' . intval($interm) . ' '; + else + $inclusions .= ' OR t.term_id = ' . intval($interm) . ' '; + } + } + } + + if ( !empty($inclusions) ) + $inclusions .= ')'; + $where .= $inclusions; + + $exclusions = ''; + if ( !empty($exclude) ) { + $exterms = preg_split('/[\s,]+/',$exclude); + if ( count($exterms) ) { + foreach ( $exterms as $exterm ) { + if (empty($exclusions)) + $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' '; + else + $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' '; + } + } + } + + if ( !empty($exclusions) ) + $exclusions .= ')'; + $exclusions = apply_filters('list_terms_exclusions', $exclusions, $args ); + $where .= $exclusions; + + if ( $hide_empty ) + $where .= ' AND tt.count > 0'; + + if ( !empty($number) ) + $number = 'LIMIT ' . $number; + else + $number = ''; + + $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"); + + if ( empty($terms) ) + return array(); + + $terms = apply_filters('get_terms', $terms, $taxonomies, $args); + return $terms; +} + +function &get_term(&$term, $taxonomy, $output = OBJECT) { + global $wpdb; + + if ( empty($term) ) + return null; + + if ( is_object($term) ) { + wp_cache_add($term->term_id, $term, "term:$taxonomy"); + $_term = $term; + } else { + $term = (int) $term; + if ( ! $_term = wp_cache_get($term, "term:$taxonomy") ) { + $_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"); + wp_cache_add($term, $_term, "term:$taxonomy"); + } + } + + $_term = apply_filters('get_term', $_term, $taxonomy); + + if ( $output == OBJECT ) { + return $_term; + } elseif ( $output == ARRAY_A ) { + return get_object_vars($_term); + } elseif ( $output == ARRAY_N ) { + return array_values(get_object_vars($_term)); + } else { + return $_term; + } +} ?> \ No newline at end of file