Introduce term_is_ancestor_of(). Finish taxonomy support for wp_insert_category(). Props garyc40. fixes #15581

git-svn-id: http://svn.automattic.com/wordpress/trunk@19678 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2012-01-04 22:44:19 +00:00
parent 5b9144b12d
commit 85304c2dc7
3 changed files with 28 additions and 20 deletions

View File

@ -117,7 +117,7 @@ function wp_insert_category($catarr, $wp_error = false) {
if ( $parent < 0 )
$parent = 0;
if ( empty($parent) || !category_exists( $parent ) || ($cat_ID && cat_is_ancestor_of($cat_ID, $parent) ) )
if ( empty( $parent ) || ! term_exists( $parent, $taxonomy ) || ( $cat_ID && term_is_ancestor_of( $cat_ID, $parent, $taxonomy ) ) )
$parent = 0;
$args = compact('name', 'slug', 'parent', 'description');

View File

@ -207,20 +207,9 @@ function get_cat_name( $cat_id ) {
* @return bool Whether $cat2 is child of $cat1
*/
function cat_is_ancestor_of( $cat1, $cat2 ) {
if ( ! isset($cat1->term_id) )
$cat1 = &get_category( $cat1 );
if ( ! isset($cat2->parent) )
$cat2 = &get_category( $cat2 );
if ( empty($cat1->term_id) || empty($cat2->parent) )
return false;
if ( $cat2->parent == $cat1->term_id )
return true;
return cat_is_ancestor_of( $cat1, get_category( $cat2->parent ) );
return term_is_ancestor_of( $cat1, $cat2, 'category' );
}
/**
* Sanitizes category data based on context.
*
@ -235,7 +224,6 @@ function sanitize_category( $category, $context = 'display' ) {
return sanitize_term( $category, 'category', $context );
}
/**
* Sanitizes data in single category key field.
*
@ -254,7 +242,6 @@ function sanitize_category_field( $field, $value, $cat_id, $context ) {
/* Tags */
/**
* Retrieves all post tags.
*
@ -277,7 +264,6 @@ function &get_tags( $args = '' ) {
return $tags;
}
/**
* Retrieve post tag by tag ID or tag object.
*
@ -301,10 +287,8 @@ function &get_tag( $tag, $output = OBJECT, $filter = 'raw' ) {
return get_term( $tag, 'post_tag', $output, $filter );
}
/* Cache */
/**
* Remove the category cache data based on ID.
*
@ -317,7 +301,6 @@ function clean_category_cache( $id ) {
clean_term_cache( $id, 'category' );
}
/**
* Update category structure to old pre 2.3 from new taxonomy structure.
*
@ -355,5 +338,4 @@ function _make_cat_compat( &$category ) {
}
}
?>

View File

@ -1494,6 +1494,32 @@ function term_exists($term, $taxonomy = '', $parent = 0) {
return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where", $else_where_fields) );
}
/**
* Check if a term is an ancestor of another term.
*
* You can use either an id or the term object for both parameters.
*
* @since 3.4.0
*
* @param int|object $term1 ID or object to check if this is the parent term.
* @param int|object $term2 The child term.
* @param string $taxonomy Taxonomy name that $term1 and $term2 belong to.
* @return bool Whether $term2 is child of $term1
*/
function term_is_ancestor_of( $term1, $term2, $taxonomy ) {
if ( ! isset( $term1->term_id ) )
$term1 = get_term( $term1, $taxonomy );
if ( ! isset( $term2->parent ) )
$term2 = get_term( $term2, $taxonomy );
if ( empty( $term1->term_id ) || empty( $term2->parent ) )
return false;
if ( $term2->parent == $term1->term_id )
return true;
return term_is_ancestor_of( $term1, get_term( $term2->parent, $taxonomy ), $taxonomy );
}
/**
* Sanitize Term all fields.
*