Make WordPress Core

Changeset 10905


Ignore:
Timestamp:
04/10/2009 08:58:25 PM (18 years ago)
Author:
ryan
Message:

Add option to check if term exists with given parent. Update ajax add-cat check to pass parent when checking if cat exists.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/admin-ajax.php

    r10903 r10905  
    446446        }
    447447
    448         if ( category_exists( trim( $_POST['cat_name'] ) ) ) {
     448        if ( category_exists( trim( $_POST['cat_name'] ), $_POST['category_parent'] ) ) {
    449449                $x = new WP_Ajax_Response( array(
    450450                        'what' => 'cat',
  • trunk/wp-admin/includes/taxonomy.php

    r10810 r10905  
    1919 * @return unknown
    2020 */
    21 function category_exists($cat_name) {
    22         $id = is_term($cat_name, 'category');
     21function category_exists($cat_name, $parent = 0) {
     22        $id = is_term($cat_name, 'category', $parent);
     23        global $wpdb;
     24                error_log(var_export($wpdb->queries, true));
    2325        if ( is_array($id) )
    2426                $id = $id['term_id'];
  • trunk/wp-includes/taxonomy.php

    r10813 r10905  
    835835 * @param int|string $term The term to check
    836836 * @param string $taxonomy The taxonomy name to use
     837 * @param int $parent ID of parent term under which to confine the exists search.
    837838 * @return mixed Get the term id or Term Object, if exists.
    838839 */
    839 function is_term($term, $taxonomy = '') {
     840function is_term($term, $taxonomy = '', $parent = 0) {
    840841        global $wpdb;
    841842
    … …  
    858859        $where = 't.slug = %s';
    859860        $else_where = 't.name = %s';
    860 
     861        $where_fields = array($slug);
     862        $else_where_fields = array($term);
    861863        if ( !empty($taxonomy) ) {
    862                 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) )
     864                $parent = (int) $parent;
     865                if ( $parent > 0 ) {
     866                        $where_fields[] = $parent;
     867                        $else_where_fields[] = $parent;
     868                        $where .= ' AND tt.parent = %d';
     869                        $else_where .= ' AND tt.parent = %d';
     870                }
     871
     872                $where_fields[] = $taxonomy;
     873                $else_where_fields[] = $taxonomy;
     874
     875                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) )
    863876                        return $result;
    864877
    865                 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);
    866         }
    867 
    868         if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where", $slug) ) )
     878                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);
     879        }
     880
     881        if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where", $where_fields) ) )
    869882                return $result;
    870883
    871         return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where", $term) );
     884        return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where", $else_where_fields) );
    872885}
    873886
Note: See TracChangeset for help on using the changeset viewer.