From 6f431038f2375d42ed3a21696ae9e0a3cd8e7e2b Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 10 Apr 2009 20:58:25 +0000 Subject: [PATCH] Add option to check if term exists with given parent. Update ajax add-cat check to pass parent when checking if cat exists. git-svn-id: http://svn.automattic.com/wordpress/trunk@10905 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/admin-ajax.php | 2 +- wp-admin/includes/taxonomy.php | 6 ++++-- wp-includes/taxonomy.php | 25 +++++++++++++++++++------ 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/wp-admin/admin-ajax.php b/wp-admin/admin-ajax.php index 4fdfc00d6..2720bbf93 100644 --- a/wp-admin/admin-ajax.php +++ b/wp-admin/admin-ajax.php @@ -445,7 +445,7 @@ case 'add-cat' : // From Manage->Categories $x->send(); } - if ( category_exists( trim( $_POST['cat_name'] ) ) ) { + if ( category_exists( trim( $_POST['cat_name'] ), $_POST['category_parent'] ) ) { $x = new WP_Ajax_Response( array( 'what' => 'cat', 'id' => new WP_Error( 'cat_exists', __('The category you are trying to create already exists.'), array( 'form-field' => 'cat_name' ) ), diff --git a/wp-admin/includes/taxonomy.php b/wp-admin/includes/taxonomy.php index 16519f133..902b06633 100644 --- a/wp-admin/includes/taxonomy.php +++ b/wp-admin/includes/taxonomy.php @@ -18,8 +18,10 @@ * @param unknown_type $cat_name * @return unknown */ -function category_exists($cat_name) { - $id = is_term($cat_name, 'category'); +function category_exists($cat_name, $parent = 0) { + $id = is_term($cat_name, 'category', $parent); + global $wpdb; + error_log(var_export($wpdb->queries, true)); if ( is_array($id) ) $id = $id['term_id']; return $id; diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index 1518ed94d..e321d8d2f 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -834,9 +834,10 @@ function &get_terms($taxonomies, $args = '') { * * @param int|string $term The term to check * @param string $taxonomy The taxonomy name to use + * @param int $parent ID of parent term under which to confine the exists search. * @return mixed Get the term id or Term Object, if exists. */ -function is_term($term, $taxonomy = '') { +function is_term($term, $taxonomy = '', $parent = 0) { global $wpdb; $select = "SELECT term_id FROM $wpdb->terms as t WHERE "; @@ -857,18 +858,30 @@ function is_term($term, $taxonomy = '') { $where = 't.slug = %s'; $else_where = 't.name = %s'; - + $where_fields = array($slug); + $else_where_fields = array($term); if ( !empty($taxonomy) ) { - if ( $result = $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s", $slug, $taxonomy), ARRAY_A) ) + $parent = (int) $parent; + if ( $parent > 0 ) { + $where_fields[] = $parent; + $else_where_fields[] = $parent; + $where .= ' AND tt.parent = %d'; + $else_where .= ' AND tt.parent = %d'; + } + + $where_fields[] = $taxonomy; + $else_where_fields[] = $taxonomy; + + if ( $result = $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s", $where_fields), ARRAY_A) ) return $result; - return $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $else_where AND tt.taxonomy = %s", $term, $taxonomy), ARRAY_A); + return $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $else_where AND tt.taxonomy = %s", $else_where_fields), ARRAY_A); } - if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where", $slug) ) ) + if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where", $where_fields) ) ) return $result; - return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where", $term) ); + return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where", $else_where_fields) ); } /**