Add get_term_by() and taxonomy registration bits. Move more category stuff to taxonomy. see #4189

git-svn-id: http://svn.automattic.com/wordpress/trunk@5525 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2007-05-23 10:32:33 +00:00
parent 557eafd82e
commit 42df47a478
2 changed files with 200 additions and 183 deletions

View File

@ -7,7 +7,7 @@ function get_all_category_ids() {
global $wpdb;
if ( ! $cat_ids = wp_cache_get('all_category_ids', 'category') ) {
$cat_ids = $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories");
$cat_ids = get_terms('category', 'get=ids&hierarchical=0&hide_empty=0');
wp_cache_add('all_category_ids', $cat_ids, 'category');
}
@ -15,169 +15,14 @@ function get_all_category_ids() {
}
function &get_categories($args = '') {
global $wpdb, $category_links;
$defaults = array(
'type' => 'post', 'child_of' => 0,
'orderby' => 'name', 'order' => 'ASC',
'hide_empty' => true, 'include_last_update_time' => false,
'hierarchical' => 1, 'exclude' => '',
'include' => '', 'number' => '',
'pad_counts' => false
);
$r = wp_parse_args( $args, $defaults );
if ( $r['orderby'] == 'count' ) {
$r['orderby'] = 'category_count';
} else {
$r['orderby'] = 'cat_' . $r['orderby'];
}
extract( $r );
$key = md5( serialize( $r ) );
if ( $cache = wp_cache_get( 'get_categories', 'category' ) )
if ( isset( $cache[ $key ] ) )
return apply_filters('get_categories', $cache[$key], $r);
$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) . ' ';
// TODO: Exclude children of excluded cats? Note: children are getting excluded
}
}
}
if (!empty($exclusions))
$exclusions .= ')';
$exclusions = apply_filters('list_cats_exclusions', $exclusions, $r );
$where .= $exclusions;
if ( $hide_empty && !$hierarchical ) {
if ( 'link' == $type )
$where .= ' AND link_count > 0';
else
$where .= ' AND category_count > 0';
} else {
$where .= ' AND ( type & ' . TAXONOMY_CATEGORY . ' != 0 ) ';
}
if ( !empty($number) )
$number = 'LIMIT ' . $number;
else
$number = '';
$categories = $wpdb->get_results("SELECT * FROM $wpdb->categories WHERE $where ORDER BY $orderby $order $number");
if ( empty($categories) )
return array();
// TODO: Integrate this into the main query.
if ( $include_last_update_time ) {
$stamps = $wpdb->get_results("SELECT category_id, UNIX_TIMESTAMP( MAX(post_date) ) AS ts FROM $wpdb->posts, $wpdb->post2cat, $wpdb->categories
WHERE post_status = 'publish' AND post_id = ID AND $where GROUP BY category_id");
global $cat_stamps;
foreach ($stamps as $stamp)
$cat_stamps[$stamp->category_id] = $stamp->ts;
function stamp_cat($cat) {
global $cat_stamps;
$cat->last_update_timestamp = $cat_stamps[$cat->cat_ID];
return $cat;
}
$categories = array_map('stamp_cat', $categories);
unset($cat_stamps);
}
if ( $child_of || $hierarchical ) {
$children = _get_category_hierarchy();
if ( ! empty($children) )
$categories = & _get_cat_children($child_of, $categories);
}
// Update category counts to include children.
if ( $pad_counts )
_pad_category_counts($type, $categories);
// Make sure we show empty categories that have children.
if ( $hierarchical && $hide_empty ) {
foreach ( $categories as $k => $category ) {
if ( ! $category->{'link' == $type ? 'link_count' : 'category_count'} ) {
$children = _get_cat_children($category->cat_ID, $categories);
foreach ( $children as $child )
if ( $child->{'link' == $type ? 'link_count' : 'category_count'} )
continue 2;
// It really is empty
unset($categories[$k]);
}
}
}
reset ( $categories );
$cache[ $key ] = $categories;
wp_cache_add( 'get_categories', $cache, 'category' );
$categories = apply_filters('get_categories', $categories, $r);
return $categories;
// TODO Add back compat fields into each object.
return get_terms('category', $args);
}
// Retrieves category data given a category ID or category object.
// Handles category caching.
function &get_category(&$category, $output = OBJECT) {
global $wpdb;
if ( empty($category) )
return null;
if ( is_object($category) ) {
wp_cache_add($category->cat_ID, $category, 'category');
$_category = $category;
} else {
$category = (int) $category;
if ( ! $_category = wp_cache_get($category, 'category') ) {
$_category = $wpdb->get_row("SELECT * FROM $wpdb->categories WHERE cat_ID = '$category' LIMIT 1");
wp_cache_add($category, $_category, 'category');
}
}
$_category = apply_filters('get_category', $_category);
if ( $output == OBJECT ) {
return $_category;
} elseif ( $output == ARRAY_A ) {
return get_object_vars($_category);
} elseif ( $output == ARRAY_N ) {
return array_values(get_object_vars($_category));
} else {
return $_category;
}
return get_term($category, 'category', $output);
}
function get_category_by_path($category_path, $full_match = true, $output = OBJECT) {
@ -192,7 +37,7 @@ function get_category_by_path($category_path, $full_match = true, $output = OBJE
foreach ( (array) $category_paths as $pathdir )
$full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir);
$categories = $wpdb->get_results("SELECT cat_ID, category_nicename, category_parent FROM $wpdb->categories WHERE category_nicename = '$leaf_path'");
$categories = get_terms('category', "slug=$leaf_path");
if ( empty($categories) )
return NULL;
@ -200,38 +45,34 @@ function get_category_by_path($category_path, $full_match = true, $output = OBJE
foreach ($categories as $category) {
$path = '/' . $leaf_path;
$curcategory = $category;
while ( ($curcategory->category_parent != 0) && ($curcategory->category_parent != $curcategory->cat_ID) ) {
$curcategory = $wpdb->get_row("SELECT cat_ID, category_nicename, category_parent FROM $wpdb->categories WHERE cat_ID = '$curcategory->category_parent'");
$path = '/' . $curcategory->category_nicename . $path;
while ( ($curcategory->parent != 0) && ($curcategory->parent != $curcategory->term_id) ) {
$curcategory = get_term($curcategory->parent);
$path = '/' . $curcategory->slug . $path;
}
if ( $path == $full_path )
return get_category($category->cat_ID, $output);
return get_category($category->term_id, $output);
}
// If full matching is not required, return the first cat that matches the leaf.
if ( ! $full_match )
return get_category($categories[0]->cat_ID, $output);
return get_category($categories[0]->term_id, $output);
return NULL;
}
function get_category_by_slug( $slug ) {
global $wpdb;
$slug = sanitize_title( $slug );
if ( empty( $slug ) )
return false;
$category = $wpdb->get_var( "SELECT * FROM $wpdb->categories WHERE category_nicename = '$slug' " );
return get_category( $category );
return get_term_by('slug', $slug, 'category');
}
// Get the ID of a category from its name
function get_cat_ID($cat_name='General') {
global $wpdb;
$cid = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$cat_name'");
return $cid?$cid:1; // default to cat 1
$cat = get_term_by('name', $cat_name, 'category');
if ($cat)
return $cat->term_id;
return 0;
}
// Deprecate
@ -252,13 +93,13 @@ function cat_is_ancestor_of($cat1, $cat2) {
if ( is_int($cat2) )
$cat2 = & get_category($cat2);
if ( !$cat1->cat_ID || !$cat2->category_parent )
if ( !$cat1->term_id || !$cat2->parent )
return false;
if ( $cat2->category_parent == $cat1->cat_ID )
if ( $cat2->parent == $cat1->term_id )
return true;
return cat_is_ancestor_of($cat1, get_category($cat2->parent_category));
return cat_is_ancestor_of($cat1, get_category($cat2->parent));
}
//

View File

@ -1,5 +1,32 @@
<?php
$wp_taxonomies =
array('category' => array('object_type' => 'post', 'hierarchical' => true),
'post_tag' => array('object_type' => 'post', 'hierarchical' => false),
'link_category' => array('object_type' => 'link', 'hierarchical' => false));
function is_taxonomy( $taxonomy ) {
global $wp_taxonomies;
return isset($wp_taxonomies[$taxonomy]);
}
function get_taxonomy( $taxonomy ) {
global $wp_taxonomies;
if ( ! is_taxonomy($taxonomy) )
return false;
return $wp_taxonomies[$taxonomy];
}
function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
global $wp_taxonomies;
$args['object_type'] = $object_type;
$wp_taxonomies[$taxonomy] = $args;
}
/**
* Adds a new term to the database. Optionally marks it as an alias of an existing term.
* @param int|string $term The term to add or update.
@ -9,6 +36,9 @@
function wp_insert_term( $term, $taxonomy, $args = array() ) {
global $wpdb;
if ( ! is_taxonomy($taxonomy) )
return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
$update = false;
if ( is_int($term) ) {
$update = true;
@ -257,21 +287,44 @@ function get_object_terms($object_id, $taxonomy, $args = array()) {
function &get_terms($taxonomies, $args = '') {
global $wpdb;
if ( !is_array($taxonomies) )
$single_taxonomy = false;
if ( !is_array($taxonomies) ) {
$single_taxonomy = true;
$taxonomies = array($taxonomies);
}
$in_taxonomies = "'" . implode("', '", $taxonomies) . "'";
$defaults = array('orderby' => 'name', 'order' => 'ASC',
'hide_empty' => true, 'exclude' => '', 'include' => '',
'number' => '', 'get' => 'everything');
'number' => '', 'get' => 'everything', 'slug' => '', 'parent' => '',
'hierarchical' => true, 'child_of' => 0);
$args = wp_parse_args( $args, $defaults );
$args['number'] = (int) $args['number'];
if ( ! $single_taxonomy ) {
$args['child_of'] = 0;
$args['hierarchical'] = false;
} else {
$tax = get_taxonomy($taxonomy);
if ( !$tax['hierarchical'] ) {
$args['child_of'] = 0;
$args['hierarchical'] = false;
}
}
extract($args);
$key = md5( serialize( $args ) . serialize( $taxonomies ) );
if ( $cache = wp_cache_get( 'get_terms', 'terms' ) ) {
if ( isset( $cache[ $key ] ) )
return apply_filters('get_terms', $cache[$key], $taxonomies, $args);
}
if ( 'count' == $orderby )
$orderby = 'tt.count';
else if ( 'name' == $orderby )
$orderby = 't.name';
else
$orderby = 't.term_id';
$where = '';
$inclusions = '';
@ -310,7 +363,17 @@ function &get_terms($taxonomies, $args = '') {
$exclusions = apply_filters('list_terms_exclusions', $exclusions, $args );
$where .= $exclusions;
if ( $hide_empty )
if ( !empty($slug) ) {
$slug = sanitize_title($slug);
$where = " AND t.slug = '$slug'";
}
if ( !empty($parent) ) {
$parent = (int) $parent;
$where = " AND tt.parent = '$parent'";
}
if ( $hide_empty && !$hierarchical )
$where .= ' AND tt.count > 0';
if ( !empty($number) )
@ -333,6 +396,37 @@ function &get_terms($taxonomies, $args = '') {
if ( empty($terms) )
return array();
if ( $child_of || $hierarchical ) {
$children = _get_term_hierarchy($taxonomies[0]);
if ( ! empty($children) )
$terms = & _get_term_children($child_of, $terms);
}
/*
// Update category counts to include children.
if ( $pad_counts )
_pad_category_counts($type, $categories);
// Make sure we show empty categories that have children.
if ( $hierarchical && $hide_empty ) {
foreach ( $categories as $k => $category ) {
if ( ! $category->{'link' == $type ? 'link_count' : 'category_count'} ) {
$children = _get_cat_children($category->cat_ID, $categories);
foreach ( $children as $child )
if ( $child->{'link' == $type ? 'link_count' : 'category_count'} )
continue 2;
// It really is empty
unset($categories[$k]);
}
}
}
reset ( $categories );
*/
$cache[ $key ] = $terms;
wp_cache_add( 'get_terms', $cache, 'terms' );
$terms = apply_filters('get_terms', $terms, $taxonomies, $args);
return $terms;
}
@ -344,17 +438,18 @@ function &get_term(&$term, $taxonomy, $output = OBJECT) {
return null;
if ( is_object($term) ) {
wp_cache_add($term->term_id, $term, "term:$taxonomy");
wp_cache_add($term->term_id, $term, $taxonomy);
$_term = $term;
} else {
$term = (int) $term;
if ( ! $_term = wp_cache_get($term, "term:$taxonomy") ) {
if ( ! $_term = wp_cache_get($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");
wp_cache_add($term, $_term, $taxonomy);
}
}
$_term = apply_filters('get_term', $_term, $taxonomy);
$_term = apply_filters("get_$taxonomy", $_term, $taxonomy);
if ( $output == OBJECT ) {
return $_term;
@ -367,4 +462,85 @@ function &get_term(&$term, $taxonomy, $output = OBJECT) {
}
}
function get_term_by($field, $value, $taxonomy, $output = OBJECT) {
global $wpdb;
if ( ! is_taxonomy($taxonomy) )
return false;
if ( 'slug' == $field ) {
$field = 't.slug';
$value = sanitize_title($field);
if ( empty($value) )
return false;
} else if ( 'name' == $field ) {
// Assume already escaped
$field = 't.name';
} else {
$field = 't.term_id';
$value = (int) $value;
}
$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 $field = '$value' LIMIT 1");
if ( !$term )
return false;
wp_cache_add($term->term_id, $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;
}
}
function _get_term_hierarchy($taxonomy) {
// TODO Make sure taxonomy is hierarchical
$children = get_option("{$taxonomy}_children");
if ( is_array($children) )
return $children;
$children = array();
$terms = get_terms('category', 'hide_empty=0&hierarchical=0');
foreach ( $terms as $term ) {
if ( $term->parent > 0 )
$children[$cterm->parent][] = $term->term_id;
}
update_option("{$taxonomy}_children", $children);
return $children;
}
function &_get_term_children($term_id, $terms) {
if ( empty($terms) )
return array();
$term_list = array();
$has_children = _get_term_hierarchy();
if ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) )
return array();
foreach ( $terms as $term ) {
if ( $term->term_id == $term_id )
continue;
if ( $term->parent == $term_id ) {
$term_list[] = $term;
if ( !isset($has_children[$term->term_id]) )
continue;
if ( $children = _get_cat_children($term->term_id, $terms) )
$term_list = array_merge($term_list, $children);
}
}
return $term_list;
}
?>