Make WordPress Core

Changeset 2937


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

Move some fxns to admin-db.

Location:
trunk/wp-admin
Files:
2 edited

Legend:

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

    r2900 r2937  
    8181}
    8282
     83function wp_insert_category($catarr) {
     84        global $wpdb;
     85
     86        extract($catarr);
     87
     88        $cat_ID = (int) $cat_ID;
     89
     90        // Are we updating or creating?
     91        if (!empty ($cat_ID)) {
     92                $update = true;
     93        } else {
     94                $update = false;
     95                $id_result = $wpdb->get_row("SHOW TABLE STATUS LIKE '$wpdb->categories'");
     96                $cat_ID = $id_result->Auto_increment;
     97        }
     98
     99        $cat_name = wp_specialchars($cat_name);
     100
     101        if (empty ($category_nicename))
     102                $category_nicename = sanitize_title($cat_name, $cat_ID);
     103        else
     104                $category_nicename = sanitize_title($category_nicename, $cat_ID);
     105
     106        if (empty ($category_description))
     107                $category_description = '';
     108
     109        if (empty ($category_parent))
     110                $category_parent = 0;
     111
     112        if (!$update)
     113                $query = "INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent) VALUES ('0', '$cat_name', '$category_nicename', '$category_description', '$cat')";
     114        else
     115                $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'";
     116
     117        $result = $wpdb->query($query);
     118
     119        if ($update) {
     120                do_action('edit_category', $cat_ID);
     121        } else {
     122                do_action('create_category', $rval);
     123                do_action('add_category', $rval);
     124        }
     125
     126        return $cat_ID;
     127}
     128
     129function wp_update_category($catarr) {
     130        global $wpdb;
     131
     132        $cat_ID = (int) $catarr['cat_ID'];
     133
     134        // First, get all of the original fields
     135        $category = get_category($cat_ID, ARRAY_A);
     136
     137        // Escape data pulled from DB.
     138        $category = add_magic_quotes($category);
     139
     140        // Merge old and new fields with new fields overwriting old ones.
     141        $catarr = array_merge($category, $catarr);
     142
     143        return wp_insert_category($catarr);
     144}
     145
     146function wp_delete_category($cat_ID) {
     147        global $wpdb;
     148
     149        $cat_ID = (int) $cat_ID;
     150
     151        // Don't delete the default cat.
     152        if (1 == $cat_ID)
     153                return 0;
     154
     155        $category = get_category($cat_ID);
     156
     157        $parent = $category->category_parent;
     158
     159        // Delete the category.
     160        $wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'");
     161
     162        // Update children to point to new parent.
     163        $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'");
     164
     165        // TODO: Only set categories to general if they're not in another category already
     166        $wpdb->query("UPDATE $wpdb->post2cat SET category_id='1' WHERE category_id='$cat_ID'");
     167
     168        do_action('delete_category', $cat_ID);
     169
     170        return 1;
     171}
     172
     173function wp_create_category($cat_name) {
     174        $cat_array = compact('cat_name');
     175        return wp_insert_category($cat_array);
     176}
     177
     178function wp_create_categories($categories, $post_id = '') {
     179        $cat_ids = array ();
     180        foreach ($categories as $category) {
     181                if ($id = category_exists($category))
     182                        $cat_ids[] = $id;
     183                else
     184                        if ($id = wp_create_category($category))
     185                                $cat_ids[] = $id;
     186        }
     187
     188        if ($post_id)
     189                wp_set_post_cats('', $post_id, $cat_ids);
     190
     191        return $cat_ids;
     192}
     193
     194function category_exists($cat_name) {
     195        global $wpdb;
     196        if (!$category_nicename = sanitize_title($cat_name))
     197                return 0;
     198
     199        return $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_nicename'");
     200}
     201
     202function wp_delete_user($id, $reassign = 'novalue') {
     203        global $wpdb;
     204
     205        $id = (int) $id;
     206
     207        if ($reassign == 'novalue') {
     208                $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
     209
     210                if ($post_ids) {
     211                        $post_ids = implode(',', $post_ids);
     212
     213                        // Delete comments, *backs
     214                        $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID IN ($post_ids)");
     215                        // Clean cats
     216                        $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id IN ($post_ids)");
     217                        // Clean post_meta
     218                        $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id IN ($post_ids)");
     219                        // Delete posts
     220                        $wpdb->query("DELETE FROM $wpdb->posts WHERE post_author = $id");
     221                }
     222
     223                // Clean links
     224                $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
     225        } else {
     226                $reassign = (int) $reassign;
     227                $wpdb->query("UPDATE $wpdb->posts SET post_author = {$reassign} WHERE post_author = {$id}");
     228                $wpdb->query("UPDATE $wpdb->links SET link_owner = {$reassign} WHERE link_owner = {$id}");
     229        }
     230
     231        // FINALLY, delete user
     232        $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
     233
     234        do_action('delete_user', $id);
     235
     236        return true;
     237}
     238
     239function get_link($link_id, $output = OBJECT) {
     240        global $wpdb;
     241       
     242        $link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$link_id'");
     243
     244        if ( $output == OBJECT ) {
     245                return $link;
     246        } elseif ( $output == ARRAY_A ) {
     247                return get_object_vars($link);
     248        } elseif ( $output == ARRAY_N ) {
     249                return array_values(get_object_vars($link));
     250        } else {
     251                return $link;
     252        }
     253}
     254
     255function wp_insert_link($linkdata) {
     256        global $wpdb, $current_user;
     257       
     258        extract($linkdata);
     259
     260        $update = false;
     261        if ( !empty($link_id) )
     262                $update = true;
     263
     264        if ( empty($link_rating) )
     265                $link_rating = 0;       
     266
     267        if ( empty($link_target) )
     268                $link_target = '';     
     269
     270        if ( empty($link_visible) )
     271                $link_visible = 'Y';
     272               
     273        if ( empty($link_owner) )
     274                $link_owner = $current_user->id;
     275       
     276        if ( $update ) {
     277                $wpdb->query("UPDATE $wpdb->links SET link_url='$link_url',
     278                        link_name='$link_name', link_image='$link_image',
     279                        link_target='$link_target', link_category='$link_category',
     280                        link_visible='$link_visible', link_description='$link_description',
     281                        link_rating='$link_rating', link_rel='$link_rel',
     282                        link_notes='$link_notes', link_rss = '$link_rss'
     283                        WHERE link_id='$link_id'");
     284        } else {
     285                $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')");
     286                $link_id = $wpdb->insert_id;
     287        }
     288       
     289        if ( $update )
     290                do_action('edit_link', $link_id);
     291        else
     292                do_action('add_link', $link_id);
     293
     294        return $link_id;
     295}
     296
     297function wp_update_link($linkdata) {
     298        global $wpdb;
     299
     300        $link_id = (int) $linkdata['link_id'];
     301       
     302        $link = get_link($link_id, ARRAY_A);
     303       
     304        // Escape data pulled from DB.
     305        $link = add_magic_quotes($link);
     306       
     307        // Merge old and new fields with new fields overwriting old ones.
     308        $linkdata = array_merge($link, $linkdata);
     309
     310        return wp_insert_link($linkdata);
     311}
     312
     313function wp_delete_link($link_id) {
     314        global $wpdb;
     315
     316    return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");
     317}
     318
     319function post_exists($title, $content = '', $post_date = '') {
     320        global $wpdb;
     321
     322        if (!empty ($post_date))
     323                $post_date = "AND post_date = '$post_date'";
     324
     325        if (!empty ($title))
     326                return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' $post_date");
     327        else
     328                if (!empty ($content))
     329                        return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' $post_date");
     330
     331        return 0;
     332}
     333
     334function comment_exists($comment_author, $comment_date) {
     335        global $wpdb;
     336
     337        return $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments
     338                        WHERE comment_author = '$comment_author' AND comment_date = '$comment_date'");
     339}
     340
    83341?>
  • 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.