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
This commit is contained in:
parent
e20fd21b39
commit
6f431038f2
|
@ -445,7 +445,7 @@ case 'add-cat' : // From Manage->Categories
|
||||||
$x->send();
|
$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(
|
$x = new WP_Ajax_Response( array(
|
||||||
'what' => 'cat',
|
'what' => 'cat',
|
||||||
'id' => new WP_Error( 'cat_exists', __('The category you are trying to create already exists.'), array( 'form-field' => 'cat_name' ) ),
|
'id' => new WP_Error( 'cat_exists', __('The category you are trying to create already exists.'), array( 'form-field' => 'cat_name' ) ),
|
||||||
|
|
|
@ -18,8 +18,10 @@
|
||||||
* @param unknown_type $cat_name
|
* @param unknown_type $cat_name
|
||||||
* @return unknown
|
* @return unknown
|
||||||
*/
|
*/
|
||||||
function category_exists($cat_name) {
|
function category_exists($cat_name, $parent = 0) {
|
||||||
$id = is_term($cat_name, 'category');
|
$id = is_term($cat_name, 'category', $parent);
|
||||||
|
global $wpdb;
|
||||||
|
error_log(var_export($wpdb->queries, true));
|
||||||
if ( is_array($id) )
|
if ( is_array($id) )
|
||||||
$id = $id['term_id'];
|
$id = $id['term_id'];
|
||||||
return $id;
|
return $id;
|
||||||
|
|
|
@ -834,9 +834,10 @@ function &get_terms($taxonomies, $args = '') {
|
||||||
*
|
*
|
||||||
* @param int|string $term The term to check
|
* @param int|string $term The term to check
|
||||||
* @param string $taxonomy The taxonomy name to use
|
* @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.
|
* @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;
|
global $wpdb;
|
||||||
|
|
||||||
$select = "SELECT term_id FROM $wpdb->terms as t WHERE ";
|
$select = "SELECT term_id FROM $wpdb->terms as t WHERE ";
|
||||||
|
@ -857,18 +858,30 @@ function is_term($term, $taxonomy = '') {
|
||||||
|
|
||||||
$where = 't.slug = %s';
|
$where = 't.slug = %s';
|
||||||
$else_where = 't.name = %s';
|
$else_where = 't.name = %s';
|
||||||
|
$where_fields = array($slug);
|
||||||
|
$else_where_fields = array($term);
|
||||||
if ( !empty($taxonomy) ) {
|
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 $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 $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) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue