Make WordPress Core


Ignore:
Timestamp:
02/27/2006 04:57:30 AM (19 years ago)
Author:
ryan
Message:

Bookmark/link rework. #2499

File:
1 edited

Legend:

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

    r3528 r3570  
    106106        $category_parent = 0;
    107107
     108    if ( isset($posts_private) )
     109        $posts_private = (int) $posts_private;
     110    else
     111        $posts_private = 0;
     112
     113    if ( isset($links_private) )
     114        $links_private = (int) $links_private;
     115    else
     116        $links_private = 0;
     117
    108118    if (!$update) {
    109         $wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent) VALUES ('0', '$cat_name', '$category_nicename', '$category_description', '$category_parent')");
     119        $wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent, links_private, posts_private) VALUES ('0', '$cat_name', '$category_nicename', '$category_description', '$category_parent', '$links_private', '$posts_private')");
    110120        $cat_ID = $wpdb->insert_id;
    111121    } else {
    112         $wpdb->query ("UPDATE $wpdb->categories SET cat_name = '$cat_name', category_nicename = '$category_nicename', category_description = '$category_description', category_parent = '$category_parent' WHERE cat_ID = '$cat_ID'");
     122        $wpdb->query ("UPDATE $wpdb->categories SET cat_name = '$cat_name', category_nicename = '$category_nicename', category_description = '$category_description', category_parent = '$category_parent', links_private = '$links_private', posts_private = '$posts_private' WHERE cat_ID = '$cat_ID'");
    113123    }
    114124
     
    154164
    155165    // Don't delete the default cat.
    156     if (1 == $cat_ID)
     166    if ( $cat_ID == get_option('default_category') )
     167        return 0;
     168
     169    if ( $cat_ID == get_option('default_link_category') )
    157170        return 0;
    158171
     
    167180    $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'");
    168181
    169     // TODO: Only set categories to general if they're not in another category already
    170     $wpdb->query("UPDATE $wpdb->post2cat SET category_id='1' WHERE category_id='$cat_ID'");
    171 
     182    // Only set posts and links to the default category if they're not in another category already.
     183    $default_cat = get_option('default_category');
     184    $posts = $wpdb->get_col("SELECT post_id FROM $wpdb->post2cat WHERE category_id='$cat_ID'");
     185    if ( is_array($posts) ) foreach ($posts as $post_id) {
     186        $cats = wp_get_post_cats('', $post_id);
     187        if ( 1 == count($cats) )
     188            $cats = array($default_cat);
     189        else
     190            $cats = array_diff($cats, array($cat_ID));
     191        wp_set_post_cats('', $post_id, $cats);
     192    }
     193
     194    $default_link_cat = get_option('default_link_category');
     195    $links = $wpdb->get_col("SELECT link_id FROM $wpdb->link2cat WHERE category_id='$cat_ID'");
     196    if ( is_array($links) ) foreach ($links as $link_id) {
     197        $cats = wp_get_link_cats($link_id);
     198        if ( 1 == count($cats) )
     199            $cats = array($default_link_cat);
     200        else
     201            $cats = array_diff($cats, array($cat_ID));
     202        wp_set_link_cats($link_id, $cats);
     203    }
     204   
    172205    wp_cache_delete($cat_ID, 'category');
    173206    wp_cache_delete('all_category_ids', 'category');
     
    245278
    246279    $link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$link_id'");
     280    $link->link_category = wp_get_link_cats($link_id);
    247281
    248282    if ( $output == OBJECT ) {
     
    281315        $link_notes = '';
    282316
     317    // Make sure we set a valid category
     318    if (0 == count($link_category) || !is_array($link_category)) {
     319        $link_category = array(get_option('default_category'));
     320    }
     321
    283322    if ( $update ) {
    284323        $wpdb->query("UPDATE $wpdb->links SET link_url='$link_url',
    285324            link_name='$link_name', link_image='$link_image',
    286             link_target='$link_target', link_category='$link_category',
     325            link_target='$link_target',
    287326            link_visible='$link_visible', link_description='$link_description',
    288327            link_rating='$link_rating', link_rel='$link_rel',
     
    290329            WHERE link_id='$link_id'");
    291330    } else {
    292         $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_image, link_target, link_category, link_description, link_visible, link_owner, link_rating, link_rel, link_notes, link_rss) VALUES('$link_url','$link_name', '$link_image', '$link_target', '$link_category', '$link_description', '$link_visible', '$link_owner', '$link_rating', '$link_rel', '$link_notes', '$link_rss')");
     331        $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_image, link_target, link_description, link_visible, link_owner, link_rating, link_rel, link_notes, link_rss) VALUES('$link_url','$link_name', '$link_image', '$link_target', '$link_description', '$link_visible', '$link_owner', '$link_rating', '$link_rel', '$link_notes', '$link_rss')");
    293332        $link_id = $wpdb->insert_id;
    294333    }
     334
     335    wp_set_link_cats($link_id, $link_category);
    295336
    296337    if ( $update )
     
    312353    $link = add_magic_quotes($link);
    313354
     355    // Passed link category list overwrites existing category list if not empty.
     356    if ( isset($linkdata['link_category']) && is_array($linkdata['link_category'])
     357             && 0 != count($linkdata['link_category']) )
     358        $link_cats = $linkdata['link_category'];
     359    else
     360        $link_cats = $link['link_category'];
     361
    314362    // Merge old and new fields with new fields overwriting old ones.
    315363    $linkdata = array_merge($link, $linkdata);
     364    $linkdata['link_category'] = $link_cats;
    316365
    317366    return wp_insert_link($linkdata);
     
    322371
    323372    do_action('delete_link', $link_id);
     373   
     374    $categories = wp_get_link_cats($link_id);
     375    if( is_array( $categories ) ) {
     376        foreach ( $categories as $category ) {
     377            $wpdb->query("UPDATE $wpdb->categories SET link_count = link_count - 1 WHERE cat_ID = '$category'");
     378            wp_cache_delete($category, 'category');
     379        }
     380    }
     381
     382    $wpdb->query("DELETE FROM $wpdb->link2cat WHERE link_id = '$link_id'");
    324383    return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");
    325384}
     385
     386function wp_get_link_cats($link_ID = 0) {
     387    global $wpdb;
     388
     389    $sql = "SELECT category_id
     390        FROM $wpdb->link2cat
     391        WHERE link_id = $link_ID
     392        ORDER BY category_id";
     393
     394    $result = $wpdb->get_col($sql);
     395
     396    if ( !$result )
     397        $result = array();
     398
     399    return array_unique($result);
     400}
     401
     402function wp_set_link_cats($link_ID = 0, $link_categories = array()) {
     403    global $wpdb;
     404    // If $link_categories isn't already an array, make it one:
     405    if (!is_array($link_categories) || 0 == count($link_categories))
     406        $link_categories = array(get_option('default_category'));
     407
     408    $link_categories = array_unique($link_categories);
     409
     410    // First the old categories
     411    $old_categories = $wpdb->get_col("
     412        SELECT category_id
     413        FROM $wpdb->link2cat
     414        WHERE link_id = $link_ID");
     415
     416    if (!$old_categories) {
     417        $old_categories = array();
     418    } else {
     419        $old_categories = array_unique($old_categories);
     420    }
     421
     422    // Delete any?
     423    $delete_cats = array_diff($old_categories,$link_categories);
     424
     425    if ($delete_cats) {
     426        foreach ($delete_cats as $del) {
     427            $wpdb->query("
     428                DELETE FROM $wpdb->link2cat
     429                WHERE category_id = $del
     430                    AND link_id = $link_ID
     431                ");
     432        }
     433    }
     434
     435    // Add any?
     436    $add_cats = array_diff($link_categories, $old_categories);
     437
     438    if ($add_cats) {
     439        foreach ($add_cats as $new_cat) {
     440            $wpdb->query("
     441                INSERT INTO $wpdb->link2cat (link_id, category_id)
     442                VALUES ($link_ID, $new_cat)");
     443        }
     444    }
     445   
     446    // Update category counts.
     447    $all_affected_cats = array_unique(array_merge($link_categories, $old_categories));
     448    foreach ( $all_affected_cats as $cat_id ) {
     449        $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->link2cat, $wpdb->links WHERE $wpdb->links.link_id = $wpdb->link2cat.link_id AND category_id = '$cat_id'");
     450        $wpdb->query("UPDATE $wpdb->categories SET link_count = '$count' WHERE cat_ID = '$cat_id'");
     451        wp_cache_delete($cat_id, 'category');
     452    }
     453}   // wp_set_link_cats()
    326454
    327455function post_exists($title, $content = '', $post_date = '') {
Note: See TracChangeset for help on using the changeset viewer.