Make WordPress Core


Ignore:
File:
1 edited

Legend:

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

    r4364 r3350  
    44        global $wpdb;
    55        $user_id = (int) $user_id;
    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";
     6        $query = "SELECT ID, post_title FROM $wpdb->posts WHERE 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_type = 'post' AND 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_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) ORDER BY display_name" );
     37                $authors = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($editable)" );
    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;
    8986
    9087        $cat_ID = (int) $cat_ID;
     
    9693                $update = false;
    9794
    98         $cat_name = apply_filters('pre_category_name', $cat_name);
    99        
     95        $cat_name = wp_specialchars($cat_name);
     96
    10097        if (empty ($category_nicename))
    10198                $category_nicename = sanitize_title($cat_name);
    10299        else
    103100                $category_nicename = sanitize_title($category_nicename);
    104         $category_nicename = apply_filters('pre_category_nicename', $category_nicename);
    105101
    106102        if (empty ($category_description))
    107103                $category_description = '';
    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 )
     104
     105        if (empty ($category_parent))
    112106                $category_parent = 0;
    113107
    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 
    124108        if (!$update) {
    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')");
     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')");
    126110                $cat_ID = $wpdb->insert_id;
    127111        } else {
    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 
     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       
    131115        if ( $category_nicename == '' ) {
    132116                $category_nicename = sanitize_title($cat_name, $cat_ID );
     
    170154
    171155        // Don't delete the default cat.
    172         if ( $cat_ID == get_option('default_category') )
     156        if (1 == $cat_ID)
    173157                return 0;
    174158
    175         if ( $cat_ID == get_option('default_link_category') )
    176                 return 0;
    177 
    178159        $category = get_category($cat_ID);
    179160
     
    181162
    182163        // Delete the category.
    183         if ( !$wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'") )
    184                 return 0;
     164        $wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'");
    185165
    186166        // Update children to point to new parent.
    187167        $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'");
    188168
    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        
     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
    212172        wp_cache_delete($cat_ID, 'category');
    213173        wp_cache_delete('all_category_ids', 'category');
     
    234194
    235195        if ($post_id)
    236                 wp_set_post_categories($post_id, $cat_ids);
     196                wp_set_post_cats('', $post_id, $cat_ids);
    237197
    238198        return $cat_ids;
     
    270230
    271231        // FINALLY, delete user
    272         do_action('delete_user', $id);
    273 
    274232        $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
    275233        $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$id'");
     
    278236        wp_cache_delete($user->user_login, 'userlogins');
    279237
     238        do_action('delete_user', $id);
     239
    280240        return true;
    281241}
    282242
    283 function wp_revoke_user($id) {
    284         $id = (int) $id;
    285        
    286         $user = new WP_User($id);
    287         $user->remove_all_caps();       
     243function get_link($link_id, $output = OBJECT) {
     244        global $wpdb;
     245       
     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        }
    288257}
    289258
    290259function wp_insert_link($linkdata) {
    291260        global $wpdb, $current_user;
    292 
     261       
    293262        extract($linkdata);
    294263
    295264        $update = false;
    296 
    297265        if ( !empty($link_id) )
    298266                $update = true;
    299267
    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 
    308268        if ( empty($link_rating) )
    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);
     269                $link_rating = 0;       
    316270
    317271        if ( empty($link_target) )
    318                 $link_target = '';
    319         $link_target = apply_filters('pre_link_target', $link_target);
     272                $link_target = '';     
    320273
    321274        if ( empty($link_visible) )
    322275                $link_visible = 'Y';
    323         $link_visibile = preg_replace('/[^YNyn]/', '', $link_visible);
    324 
     276               
    325277        if ( empty($link_owner) )
    326278                $link_owner = $current_user->id;
    327         else
    328                 $link_owner = (int) $link_owner;
    329279
    330280        if ( empty($link_notes) )
    331281                $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         }
    350282
    351283        if ( $update ) {
    352284                $wpdb->query("UPDATE $wpdb->links SET link_url='$link_url',
    353285                        link_name='$link_name', link_image='$link_image',
    354                         link_target='$link_target',
     286                        link_target='$link_target', link_category='$link_category',
    355287                        link_visible='$link_visible', link_description='$link_description',
    356288                        link_rating='$link_rating', link_rel='$link_rel',
     
    358290                        WHERE link_id='$link_id'");
    359291        } else {
    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')");
     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')");
    361293                $link_id = $wpdb->insert_id;
    362294        }
    363 
    364         wp_set_link_cats($link_id, $link_category);
    365 
     295       
    366296        if ( $update )
    367297                do_action('edit_link', $link_id);
     
    376306
    377307        $link_id = (int) $linkdata['link_id'];
    378 
     308       
    379309        $link = get_link($link_id, ARRAY_A);
    380 
     310       
    381311        // Escape data pulled from DB.
    382312        $link = add_magic_quotes($link);
    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 
     313       
    391314        // Merge old and new fields with new fields overwriting old ones.
    392315        $linkdata = array_merge($link, $linkdata);
    393         $linkdata['link_category'] = $link_cats;
    394316
    395317        return wp_insert_link($linkdata);
     
    400322
    401323        do_action('delete_link', $link_id);
    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 
    415 function 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 
    431 function 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()
     324        return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");     
     325}
    483326
    484327function post_exists($title, $content = '', $post_date = '') {
Note: See TracChangeset for help on using the changeset viewer.