From 2bdfce3c20c11f5157b46a006e26012be416e668 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Fri, 24 Nov 2006 16:37:44 +0000 Subject: [PATCH] cat_is_ancestor_of(). fixes #3387 git-svn-id: https://develop.svn.wordpress.org/trunk@4525 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/admin-db.php | 2 +- wp-includes/category.php | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/wp-admin/admin-db.php b/wp-admin/admin-db.php index 0e2b388cac..3d26d8eeb9 100644 --- a/wp-admin/admin-db.php +++ b/wp-admin/admin-db.php @@ -108,7 +108,7 @@ function wp_insert_category($catarr) { $category_description = apply_filters('pre_category_description', $category_description); $category_parent = (int) $category_parent; - if ( empty($category_parent) || !get_category( $category_parent ) || $category_parent == $cat_ID ) + if ( empty($category_parent) || !get_category( $category_parent ) || ($cat_ID && _cat_is_ancestor_of($cat_ID, $category_parent) ) ) $category_parent = 0; if ( isset($posts_private) ) diff --git a/wp-includes/category.php b/wp-includes/category.php index 7b3e7cae33..690c196ecf 100644 --- a/wp-includes/category.php +++ b/wp-includes/category.php @@ -225,6 +225,21 @@ function get_cat_name($cat_id) { return $category->cat_name; } +function cat_is_ancestor_of($cat1, $cat2) { + if ( is_int($cat1) ) + $cat1 = & get_category($cat1); + if ( is_int($cat2) ) + $cat2 = & get_category($cat2); + + if ( !$cat1->cat_ID || !$cat2->category_parent ) + return false; + + if ( $cat2->category_parent == $cat1->cat_ID ) + return true; + + return cat_is_ancestor_of($cat1, get_category($cat2->parent_category)); +} + // // Private //