WordPress.org

Make WordPress Core


Ignore:
File:
1 edited

Legend:

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

    r3350 r4364  
    44    global $wpdb;
    55    $user_id = (int) $user_id;
    6     $query = "SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'draft' AND post_author = $user_id ORDER BY ID DESC";
     6    $query = "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'draft' AND post_author = $user_id ORDER BY ID DESC";
    77    $query = apply_filters('get_users_drafts', $query);
    88    return $wpdb->get_results( $query );
     
    1515
    1616    $editable = get_editable_user_ids( $user_id );
    17    
     17
    1818    if( !$editable ) {
    1919        $other_drafts = '';
    2020    } else {
    2121        $editable = join(',', $editable);
    22         $other_drafts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'draft' AND post_author IN ($editable) AND post_author != '$user_id' ");
     22        $other_drafts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'draft' AND post_author IN ($editable) AND post_author != '$user_id' ");
    2323    }
    2424
     
    3535    } else {
    3636        $editable = join(',', $editable);
    37         $authors = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($editable)" );
     37        $authors = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($editable) ORDER BY display_name" );
    3838    }
    3939
     
    4343function get_editable_user_ids( $user_id, $exclude_zeros = true ) {
    4444    global $wpdb;
    45    
     45
    4646    $user = new WP_User( $user_id );
    47    
     47
    4848    if ( ! $user->has_cap('edit_others_posts') ) {
    4949        if ( $user->has_cap('edit_posts') || $exclude_zeros == false )
     
    5858    if ( $exclude_zeros )
    5959        $query .= " AND meta_value != '0'";
    60        
     60
    6161    return $wpdb->get_col( $query );
    6262}
     
    8484
    8585    extract($catarr);
     86
     87    if( trim( $cat_name ) == '' )
     88        return 0;
    8689
    8790    $cat_ID = (int) $cat_ID;
     
    9396        $update = false;
    9497
    95     $cat_name = wp_specialchars($cat_name);
    96 
     98    $cat_name = apply_filters('pre_category_name', $cat_name);
     99   
    97100    if (empty ($category_nicename))
    98101        $category_nicename = sanitize_title($cat_name);
    99102    else
    100103        $category_nicename = sanitize_title($category_nicename);
     104    $category_nicename = apply_filters('pre_category_nicename', $category_nicename);
    101105
    102106    if (empty ($category_description))
    103107        $category_description = '';
    104 
    105     if (empty ($category_parent))
     108    $category_description = apply_filters('pre_category_description', $category_description);
     109
     110    $category_parent = (int) $category_parent;
     111    if ( empty($category_parent) || !get_category( $category_parent ) || $category_parent == $cat_ID )
    106112        $category_parent = 0;
    107113
     114    if ( isset($posts_private) )
     115        $posts_private = (int) $posts_private;
     116    else
     117        $posts_private = 0;
     118
     119    if ( isset($links_private) )
     120        $links_private = (int) $links_private;
     121    else
     122        $links_private = 0;
     123
    108124    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')");
     125        $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')");
    110126        $cat_ID = $wpdb->insert_id;
    111127    } 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'");
    113     }
    114    
     128        $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'");
     129    }
     130
    115131    if ( $category_nicename == '' ) {
    116132        $category_nicename = sanitize_title($cat_name, $cat_ID );
     
    154170
    155171    // Don't delete the default cat.
    156     if (1 == $cat_ID)
     172    if ( $cat_ID == get_option('default_category') )
     173        return 0;
     174
     175    if ( $cat_ID == get_option('default_link_category') )
    157176        return 0;
    158177
     
    162181
    163182    // Delete the category.
    164     $wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'");
     183    if ( !$wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'") )
     184        return 0;
    165185
    166186    // Update children to point to new parent.
    167187    $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'");
    168188
    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 
     189    // Only set posts and links to the default category if they're not in another category already.
     190    $default_cat = get_option('default_category');
     191    $posts = $wpdb->get_col("SELECT post_id FROM $wpdb->post2cat WHERE category_id='$cat_ID'");
     192    if ( is_array($posts) ) foreach ($posts as $post_id) {
     193        $cats = wp_get_post_categories($post_id);
     194        if ( 1 == count($cats) )
     195            $cats = array($default_cat);
     196        else
     197            $cats = array_diff($cats, array($cat_ID));
     198        wp_set_post_categories($post_id, $cats);
     199    }
     200
     201    $default_link_cat = get_option('default_link_category');
     202    $links = $wpdb->get_col("SELECT link_id FROM $wpdb->link2cat WHERE category_id='$cat_ID'");
     203    if ( is_array($links) ) foreach ($links as $link_id) {
     204        $cats = wp_get_link_cats($link_id);
     205        if ( 1 == count($cats) )
     206            $cats = array($default_link_cat);
     207        else
     208            $cats = array_diff($cats, array($cat_ID));
     209        wp_set_link_cats($link_id, $cats);
     210    }
     211   
    172212    wp_cache_delete($cat_ID, 'category');
    173213    wp_cache_delete('all_category_ids', 'category');
     
    194234
    195235    if ($post_id)
    196         wp_set_post_cats('', $post_id, $cat_ids);
     236        wp_set_post_categories($post_id, $cat_ids);
    197237
    198238    return $cat_ids;
     
    230270
    231271    // FINALLY, delete user
     272    do_action('delete_user', $id);
     273
    232274    $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
    233275    $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$id'");
     
    236278    wp_cache_delete($user->user_login, 'userlogins');
    237279
    238     do_action('delete_user', $id);
    239 
    240280    return true;
    241281}
    242282
    243 function get_link($link_id, $output = OBJECT) {
    244     global $wpdb;
     283function wp_revoke_user($id) {
     284    $id = (int) $id;
    245285   
    246     $link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$link_id'");
    247 
    248     if ( $output == OBJECT ) {
    249         return $link;
    250     } elseif ( $output == ARRAY_A ) {
    251         return get_object_vars($link);
    252     } elseif ( $output == ARRAY_N ) {
    253         return array_values(get_object_vars($link));
    254     } else {
    255         return $link;
    256     }
     286    $user = new WP_User($id);
     287    $user->remove_all_caps();   
    257288}
    258289
    259290function wp_insert_link($linkdata) {
    260291    global $wpdb, $current_user;
    261    
     292
    262293    extract($linkdata);
    263294
    264295    $update = false;
     296
    265297    if ( !empty($link_id) )
    266298        $update = true;
    267299
     300    if( trim( $link_name ) == '' )
     301        return 0;
     302    $link_name = apply_filters('pre_link_name', $link_name);
     303
     304    if( trim( $link_url ) == '' )
     305        return 0;
     306    $link_url = apply_filters('pre_link_url', $link_url);
     307
    268308    if ( empty($link_rating) )
    269         $link_rating = 0;   
     309        $link_rating = 0;
     310    else
     311        $link_rating = (int) $link_rating;
     312
     313    if ( empty($link_image) )
     314        $link_image = '';
     315    $link_image = apply_filters('pre_link_image', $link_image);
    270316
    271317    if ( empty($link_target) )
    272         $link_target = ''; 
     318        $link_target = '';
     319    $link_target = apply_filters('pre_link_target', $link_target);
    273320
    274321    if ( empty($link_visible) )
    275322        $link_visible = 'Y';
    276        
     323    $link_visibile = preg_replace('/[^YNyn]/', '', $link_visible);
     324
    277325    if ( empty($link_owner) )
    278326        $link_owner = $current_user->id;
     327    else
     328        $link_owner = (int) $link_owner;
    279329
    280330    if ( empty($link_notes) )
    281331        $link_notes = '';
     332    $link_notes = apply_filters('pre_link_notes', $link_notes);
     333
     334    if ( empty($link_description) )
     335        $link_description = '';
     336    $link_description = apply_filters('pre_link_description', $link_description);
     337
     338    if ( empty($link_rss) )
     339        $link_rss = '';
     340    $link_rss = apply_filters('pre_link_rss', $link_rss);
     341
     342    if ( empty($link_rel) )
     343        $link_rel = '';
     344    $link_rel = apply_filters('pre_link_rel', $link_rel);
     345
     346    // Make sure we set a valid category
     347    if (0 == count($link_category) || !is_array($link_category)) {
     348        $link_category = array(get_option('default_link_category'));
     349    }
    282350
    283351    if ( $update ) {
    284352        $wpdb->query("UPDATE $wpdb->links SET link_url='$link_url',
    285353            link_name='$link_name', link_image='$link_image',
    286             link_target='$link_target', link_category='$link_category',
     354            link_target='$link_target',
    287355            link_visible='$link_visible', link_description='$link_description',
    288356            link_rating='$link_rating', link_rel='$link_rel',
     
    290358            WHERE link_id='$link_id'");
    291359    } 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')");
     360        $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')");
    293361        $link_id = $wpdb->insert_id;
    294362    }
    295    
     363
     364    wp_set_link_cats($link_id, $link_category);
     365
    296366    if ( $update )
    297367        do_action('edit_link', $link_id);
     
    306376
    307377    $link_id = (int) $linkdata['link_id'];
    308    
     378
    309379    $link = get_link($link_id, ARRAY_A);
    310    
     380
    311381    // Escape data pulled from DB.
    312382    $link = add_magic_quotes($link);
    313    
     383
     384    // Passed link category list overwrites existing category list if not empty.
     385    if ( isset($linkdata['link_category']) && is_array($linkdata['link_category'])
     386             && 0 != count($linkdata['link_category']) )
     387        $link_cats = $linkdata['link_category'];
     388    else
     389        $link_cats = $link['link_category'];
     390
    314391    // Merge old and new fields with new fields overwriting old ones.
    315392    $linkdata = array_merge($link, $linkdata);
     393    $linkdata['link_category'] = $link_cats;
    316394
    317395    return wp_insert_link($linkdata);
     
    322400
    323401    do_action('delete_link', $link_id);
    324     return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");
    325 }
     402   
     403    $categories = wp_get_link_cats($link_id);
     404    if( is_array( $categories ) ) {
     405        foreach ( $categories as $category ) {
     406            $wpdb->query("UPDATE $wpdb->categories SET link_count = link_count - 1 WHERE cat_ID = '$category'");
     407            wp_cache_delete($category, 'category');
     408        }
     409    }
     410
     411    $wpdb->query("DELETE FROM $wpdb->link2cat WHERE link_id = '$link_id'");
     412    return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");
     413}
     414
     415function wp_get_link_cats($link_ID = 0) {
     416    global $wpdb;
     417
     418    $sql = "SELECT category_id
     419        FROM $wpdb->link2cat
     420        WHERE link_id = $link_ID
     421        ORDER BY category_id";
     422
     423    $result = $wpdb->get_col($sql);
     424
     425    if ( !$result )
     426        $result = array();
     427
     428    return array_unique($result);
     429}
     430
     431function wp_set_link_cats($link_ID = 0, $link_categories = array()) {
     432    global $wpdb;
     433    // If $link_categories isn't already an array, make it one:
     434    if (!is_array($link_categories) || 0 == count($link_categories))
     435        $link_categories = array(get_option('default_link_category'));
     436
     437    $link_categories = array_unique($link_categories);
     438
     439    // First the old categories
     440    $old_categories = $wpdb->get_col("
     441        SELECT category_id
     442        FROM $wpdb->link2cat
     443        WHERE link_id = $link_ID");
     444
     445    if (!$old_categories) {
     446        $old_categories = array();
     447    } else {
     448        $old_categories = array_unique($old_categories);
     449    }
     450
     451    // Delete any?
     452    $delete_cats = array_diff($old_categories,$link_categories);
     453
     454    if ($delete_cats) {
     455        foreach ($delete_cats as $del) {
     456            $wpdb->query("
     457                DELETE FROM $wpdb->link2cat
     458                WHERE category_id = $del
     459                    AND link_id = $link_ID
     460                ");
     461        }
     462    }
     463
     464    // Add any?
     465    $add_cats = array_diff($link_categories, $old_categories);
     466
     467    if ($add_cats) {
     468        foreach ($add_cats as $new_cat) {
     469            $wpdb->query("
     470                INSERT INTO $wpdb->link2cat (link_id, category_id)
     471                VALUES ($link_ID, $new_cat)");
     472        }
     473    }
     474   
     475    // Update category counts.
     476    $all_affected_cats = array_unique(array_merge($link_categories, $old_categories));
     477    foreach ( $all_affected_cats as $cat_id ) {
     478        $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'");
     479        $wpdb->query("UPDATE $wpdb->categories SET link_count = '$count' WHERE cat_ID = '$cat_id'");
     480        wp_cache_delete($cat_id, 'category');
     481    }
     482}   // wp_set_link_cats()
    326483
    327484function post_exists($title, $content = '', $post_date = '') {
Note: See TracChangeset for help on using the changeset viewer.