WordPress.org

Make WordPress Core

Changeset 5271


Ignore:
Timestamp:
04/15/2007 02:26:26 AM (15 years ago)
Author:
ryan
Message:

tag_exists(), category_object_exists(), and some tag and cat create fix ups. see #3723

Location:
trunk
Files:
2 edited

Legend:

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

    r5184 r5271  
    9595    else
    9696        $update = false;
    97 
     97error_log("Type for $cat_ID is $type", 0);
    9898    $cat_name = apply_filters('pre_category_name', $cat_name);
    9999
     
    126126
    127127    // Let's check if we have this category already, if so just do an update
    128     if ( $cat_ID = category_exists( $category_nicename ) ) {
     128    if ( !$update && $cat_ID = category_object_exists( $category_nicename ) )
    129129        $update = true;
    130         $category = get_category($cat_ID);
    131         // If inserting a category that already exists, OR in the new type rather
    132         // than replacing the entire value.
    133         $type = $category->type | $type;
    134     }
    135130
    136131    if (!$update) {
     
    241236
    242237function wp_create_category($cat_name) {
    243     $cat_array = compact('cat_name');
    244     return wp_insert_category($cat_array);
     238    if ( $id = category_exists($cat_name) )
     239        return $id;
     240    $cat_array = array('cat_name' => $cat_name, 'type' => TAXONOMY_CATEGORY);
     241
     242    if ( $id = category_object_exists($cat_name) ) {
     243        $category = get_category($id);
     244        $cat_array['type'] = $category->type | $cat_array['type'];
     245        $cat_array['cat_ID'] = $id;
     246        return wp_update_category($cat_array);
     247    } else {
     248        return wp_insert_category($cat_array);
     249    }
    245250}
    246251
     
    261266}
    262267
     268function category_object_exists($cat_name) {
     269    global $wpdb;
     270    if (!$category_nicename = sanitize_title($cat_name))
     271        return 0;
     272
     273    return (int) $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_nicename'");
     274}
     275
    263276function category_exists($cat_name) {
    264277    global $wpdb;
     
    266279        return 0;
    267280
    268     return (int) $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_nicename'");
     281    return (int) $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_nicename' AND ( type & " . TAXONOMY_CATEGORY .  " != 0 )");
     282}
     283
     284function tag_exists($tag_name) {
     285    global $wpdb;
     286    if (! $tag_nicename = sanitize_title($tag_name))
     287        return 0;
     288
     289    return (int) $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$tag_nicename' AND ( type & " . TAXONOMY_TAG .  " != 0 )");
    269290}
    270291
    271292function wp_create_tag($tag_name) {
     293    if ( $id = tag_exists($tag_name) )
     294        return $id;
    272295    $tag_array = array('cat_name' => $tag_name, 'type' => TAXONOMY_TAG);
    273     return wp_insert_category($tag_array);
     296
     297    if ( $id = category_object_exists($tag_name) ) {
     298        error_log("$tag_name exists", 0);
     299        $category = get_category($id);
     300        $tag_array['type'] = $category->type | $tag_array['type'];
     301        $tag_array['cat_ID'] = $id;
     302        error_log("Type: {$tag_array['type']}", 0);
     303        $id = wp_update_category($tag_array);
     304        error_log("Tag id $id", 0);
     305        return $id;
     306    } else {
     307        return wp_insert_category($tag_array);
     308    }
    274309}
    275310
  • trunk/wp-includes/post.php

    r5248 r5271  
    809809        if ( !$tag_slug = sanitize_title( $tag ) )
    810810            continue; // discard
    811         if ( !$tag_id = category_exists( $tag ) )
     811        if ( !$tag_id = tag_exists( $tag ) )
    812812            $tag_id = wp_create_tag( $tag );
    813813        $tag_ids[] = $tag_id;
Note: See TracChangeset for help on using the changeset viewer.