Make WordPress Core


Ignore:
Timestamp:
10/06/2005 05:26:06 PM (21 years ago)
Author:
ryan
Message:

Move some fxns to admin-db.

File:
1 edited

Legend:

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

    r2927 r2937  
    249249
    250250        return $category;
    251 }
    252 
    253 function wp_insert_category($catarr) {
    254         global $wpdb;
    255 
    256         extract($catarr);
    257 
    258         $cat_ID = (int) $cat_ID;
    259 
    260         // Are we updating or creating?
    261         if (!empty ($cat_ID)) {
    262                 $update = true;
    263         } else {
    264                 $update = false;
    265                 $id_result = $wpdb->get_row("SHOW TABLE STATUS LIKE '$wpdb->categories'");
    266                 $cat_ID = $id_result->Auto_increment;
    267         }
    268 
    269         $cat_name = wp_specialchars($cat_name);
    270 
    271         if (empty ($category_nicename))
    272                 $category_nicename = sanitize_title($cat_name, $cat_ID);
    273         else
    274                 $category_nicename = sanitize_title($category_nicename, $cat_ID);
    275 
    276         if (empty ($category_description))
    277                 $category_description = '';
    278 
    279         if (empty ($category_parent))
    280                 $category_parent = 0;
    281 
    282         if (!$update)
    283                 $query = "INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent) VALUES ('0', '$cat_name', '$category_nicename', '$category_description', '$cat')";
    284         else
    285                 $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'";
    286 
    287         $result = $wpdb->query($query);
    288 
    289         if ($update) {
    290                 do_action('edit_category', $cat_ID);
    291         } else {
    292                 do_action('create_category', $rval);
    293                 do_action('add_category', $rval);
    294         }
    295 
    296         return $cat_ID;
    297 }
    298 
    299 function wp_update_category($catarr) {
    300         global $wpdb;
    301 
    302         $cat_ID = (int) $catarr['cat_ID'];
    303 
    304         // First, get all of the original fields
    305         $category = get_category($cat_ID, ARRAY_A);
    306 
    307         // Escape data pulled from DB.
    308         $category = add_magic_quotes($category);
    309 
    310         // Merge old and new fields with new fields overwriting old ones.
    311         $catarr = array_merge($category, $catarr);
    312 
    313         return wp_insert_category($catarr);
    314 }
    315 
    316 function wp_delete_category($cat_ID) {
    317         global $wpdb;
    318 
    319         $cat_ID = (int) $cat_ID;
    320 
    321         // Don't delete the default cat.
    322         if (1 == $cat_ID)
    323                 return 0;
    324 
    325         $category = get_category($cat_ID);
    326 
    327         $parent = $category->category_parent;
    328 
    329         // Delete the category.
    330         $wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'");
    331 
    332         // Update children to point to new parent.
    333         $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'");
    334 
    335         // TODO: Only set categories to general if they're not in another category already
    336         $wpdb->query("UPDATE $wpdb->post2cat SET category_id='1' WHERE category_id='$cat_ID'");
    337 
    338         do_action('delete_category', $cat_ID);
    339 
    340         return 1;
    341 }
    342 
    343 function wp_create_category($cat_name) {
    344         $cat_array = compact('cat_name');
    345         return wp_insert_category($cat_array);
    346 }
    347 
    348 function wp_create_categories($categories, $post_id = '') {
    349         $cat_ids = array ();
    350         foreach ($categories as $category) {
    351                 if ($id = category_exists($category))
    352                         $cat_ids[] = $id;
    353                 else
    354                         if ($id = wp_create_category($category))
    355                                 $cat_ids[] = $id;
    356         }
    357 
    358         if ($post_id)
    359                 wp_set_post_cats('', $post_id, $cat_ids);
    360 
    361         return $cat_ids;
    362 }
    363 
    364 function category_exists($cat_name) {
    365         global $wpdb;
    366         if (!$category_nicename = sanitize_title($cat_name))
    367                 return 0;
    368 
    369         return $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_nicename'");
    370251}
    371252
     
    468349}
    469350
    470 function wp_delete_user($id, $reassign = 'novalue') {
    471         global $wpdb;
    472 
    473         $id = (int) $id;
    474 
    475         if ($reassign == 'novalue') {
    476                 $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
    477 
    478                 if ($post_ids) {
    479                         $post_ids = implode(',', $post_ids);
    480 
    481                         // Delete comments, *backs
    482                         $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID IN ($post_ids)");
    483                         // Clean cats
    484                         $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id IN ($post_ids)");
    485                         // Clean post_meta
    486                         $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id IN ($post_ids)");
    487                         // Delete posts
    488                         $wpdb->query("DELETE FROM $wpdb->posts WHERE post_author = $id");
    489                 }
    490 
    491                 // Clean links
    492                 $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
    493         } else {
    494                 $reassign = (int) $reassign;
    495                 $wpdb->query("UPDATE $wpdb->posts SET post_author = {$reassign} WHERE post_author = {$id}");
    496                 $wpdb->query("UPDATE $wpdb->links SET link_owner = {$reassign} WHERE link_owner = {$id}");
    497         }
    498 
    499         // FINALLY, delete user
    500         $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
    501 
    502         do_action('delete_user', $id);
    503 
    504         return true;
    505 }
    506 
    507 function get_link($link_id, $output = OBJECT) {
    508         global $wpdb;
    509        
    510         $link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$link_id'");
    511 
    512         if ( $output == OBJECT ) {
    513                 return $link;
    514         } elseif ( $output == ARRAY_A ) {
    515                 return get_object_vars($link);
    516         } elseif ( $output == ARRAY_N ) {
    517                 return array_values(get_object_vars($link));
    518         } else {
    519                 return $link;
    520         }
    521 }
    522351
    523352function get_link_to_edit($link_id) {
     
    575404                return wp_insert_link($_POST);
    576405        }
    577 }
    578 
    579 function wp_insert_link($linkdata) {
    580         global $wpdb, $current_user;
    581        
    582         extract($linkdata);
    583 
    584         $update = false;
    585         if ( !empty($link_id) )
    586                 $update = true;
    587 
    588         if ( empty($link_rating) )
    589                 $link_rating = 0;       
    590 
    591         if ( empty($link_target) )
    592                 $link_target = '';     
    593 
    594         if ( empty($link_visible) )
    595                 $link_visible = 'Y';
    596                
    597         if ( empty($link_owner) )
    598                 $link_owner = $current_user->id;
    599        
    600         if ( $update ) {
    601                 $wpdb->query("UPDATE $wpdb->links SET link_url='$link_url',
    602                         link_name='$link_name', link_image='$link_image',
    603                         link_target='$link_target', link_category='$link_category',
    604                         link_visible='$link_visible', link_description='$link_description',
    605                         link_rating='$link_rating', link_rel='$link_rel',
    606                         link_notes='$link_notes', link_rss = '$link_rss'
    607                         WHERE link_id='$link_id'");
    608         } else {
    609                 $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')");
    610                 $link_id = $wpdb->insert_id;
    611         }
    612        
    613         if ( $update )
    614                 do_action('edit_link', $link_id);
    615         else
    616                 do_action('add_link', $link_id);
    617 
    618         return $link_id;
    619 }
    620 
    621 function wp_update_link($linkdata) {
    622         global $wpdb;
    623 
    624         $link_id = (int) $linkdata['link_id'];
    625        
    626         $link = get_link($link_id, ARRAY_A);
    627        
    628         // Escape data pulled from DB.
    629         $link = add_magic_quotes($link);
    630        
    631         // Merge old and new fields with new fields overwriting old ones.
    632         $linkdata = array_merge($link, $linkdata);
    633 
    634         return wp_insert_link($linkdata);
    635 }
    636 
    637 function wp_delete_link($link_id) {
    638         global $wpdb;
    639 
    640     return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");
    641 }
    642 
    643 function post_exists($title, $content = '', $post_date = '') {
    644         global $wpdb;
    645 
    646         if (!empty ($post_date))
    647                 $post_date = "AND post_date = '$post_date'";
    648 
    649         if (!empty ($title))
    650                 return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' $post_date");
    651         else
    652                 if (!empty ($content))
    653                         return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' $post_date");
    654 
    655         return 0;
    656 }
    657 
    658 function comment_exists($comment_author, $comment_date) {
    659         global $wpdb;
    660 
    661         return $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments
    662                         WHERE comment_author = '$comment_author' AND comment_date = '$comment_date'");
    663406}
    664407
Note: See TracChangeset for help on using the changeset viewer.