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: https://develop.svn.wordpress.org/trunk@10905 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2009-04-10 20:58:25 +00:00
parent 46490b01a5
commit f7df1f116e
3 changed files with 24 additions and 9 deletions

View File

@ -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' ) ),

View File

@ -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;

View File

@ -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) );
}
/**