diff --git a/wp-admin/includes/taxonomy.php b/wp-admin/includes/taxonomy.php index 9769ea8c63..7423cf9214 100644 --- a/wp-admin/includes/taxonomy.php +++ b/wp-admin/includes/taxonomy.php @@ -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'); diff --git a/wp-includes/category.php b/wp-includes/category.php index 4d02277417..96eefa8805 100644 --- a/wp-includes/category.php +++ b/wp-includes/category.php @@ -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 ) { } } - ?> diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index e6ec49ea52..2abb7c4cbb 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -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. *