Changeset 2889

Show
Ignore:
Timestamp:
09/18/05 19:44:14 (3 years ago)
Author:
ryan
Message:

Link love. Add get_link(), get_link_to_edit(), add_link(), edit_link(), wp_insert_link(), wp_update_link(), wp_delete_link()

Files:

Legend:

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

    r2886 r2889  
    11<?php 
     2 
    23 
    34// Creates a new post from the "Write Post" form using $_POST information. 
     
    56    global $user_ID; 
    67 
    7     if ( ! current_user_can('edit_posts')
    8         die( __('You are not allowed to create posts or drafts on this blog.') ); 
     8    if (!current_user_can('edit_posts')
     9        die(__('You are not allowed to create posts or drafts on this blog.')); 
    910 
    1011    // Rename. 
    11     $_POST['post_content'] = $_POST['content']; 
    12     $_POST['post_excerpt'] = $_POST['excerpt']; 
     12    $_POST['post_content'] = $_POST['content']; 
     13    $_POST['post_excerpt'] = $_POST['excerpt']; 
    1314    $_POST['post_parent'] = $_POST['parent_id']; 
    1415    $_POST['to_ping'] = $_POST['trackback_url']; 
    1516 
    16     if (! empty($_POST['post_author_override'])) { 
     17    if (!empty ($_POST['post_author_override'])) { 
    1718        $_POST['$post_author'] = (int) $_POST['post_author_override']; 
    18     } else if (! empty($_POST['post_author'])) { 
    19         $_POST['post_author'] = (int) $_POST['post_author']; 
    20     } else { 
    21         $_POST['post_author'] = (int) $_POST['user_ID']; 
    22     } 
    23  
    24     if ( ($_POST['post_author'] != $_POST['user_ID']) && ! current_user_can('edit_others_posts') ) 
    25         die( __('You cannot post as this user.') ); 
    26      
     19    } else 
     20        if (!empty ($_POST['post_author'])) { 
     21            $_POST['post_author'] = (int) $_POST['post_author']; 
     22        } else { 
     23            $_POST['post_author'] = (int) $_POST['user_ID']; 
     24        } 
     25 
     26    if (($_POST['post_author'] != $_POST['user_ID']) && !current_user_can('edit_others_posts')) 
     27        die(__('You cannot post as this user.')); 
     28 
    2729    // What to do based on which button they pressed 
    28     if ('' != $_POST['saveasdraft']) $_POST['post_status'] = 'draft'; 
    29     if ('' != $_POST['saveasprivate']) $_POST['post_status'] = 'private'; 
    30     if ('' != $_POST['publish']) $_POST['post_status'] = 'publish'; 
    31     if ('' != $_POST['advanced']) $_POST['post_status'] = 'draft'; 
    32     if ('' != $_POST['savepage']) $_POST['post_status'] = 'static'; 
    33  
    34     if ( 'publish' == $_POST['post_status'] && ! current_user_can('publish_posts') ) 
     30    if ('' != $_POST['saveasdraft']) 
    3531        $_POST['post_status'] = 'draft'; 
    36  
    37     if ( !empty($_POST['edit_date']) ) { 
     32    if ('' != $_POST['saveasprivate']) 
     33        $_POST['post_status'] = 'private'; 
     34    if ('' != $_POST['publish']) 
     35        $_POST['post_status'] = 'publish'; 
     36    if ('' != $_POST['advanced']) 
     37        $_POST['post_status'] = 'draft'; 
     38    if ('' != $_POST['savepage']) 
     39        $_POST['post_status'] = 'static'; 
     40 
     41    if ('publish' == $_POST['post_status'] && !current_user_can('publish_posts')) 
     42        $_POST['post_status'] = 'draft'; 
     43 
     44    if (!empty ($_POST['edit_date'])) { 
    3845        $aa = $_POST['aa']; 
    3946        $mm = $_POST['mm']; 
     
    4350        $ss = $_POST['ss']; 
    4451        $jj = ($jj > 31) ? 31 : $jj; 
    45         $hh = ($hh > 23) ? $hh - 24 : $hh; 
    46         $mn = ($mn > 59) ? $mn - 60 : $mn; 
    47         $ss = ($ss > 59) ? $ss - 60 : $ss; 
     52        $hh = ($hh > 23) ? $hh -24 : $hh; 
     53        $mn = ($mn > 59) ? $mn -60 : $mn; 
     54        $ss = ($ss > 59) ? $ss -60 : $ss; 
    4855        $_POST['post_date'] = "$aa-$mm-$jj $hh:$mn:$ss"; 
    4956        $_POST['post_date_gmt'] = get_gmt_from_date("$aa-$mm-$jj $hh:$mn:$ss"); 
    50     }  
     57    } 
    5158 
    5259    // Create the post. 
     
    6370    $post_ID = (int) $_POST['post_ID']; 
    6471 
    65     if ( ! current_user_can('edit_post', $post_ID)
    66         die( __('You are not allowed to edit this post.') ); 
     72    if (!current_user_can('edit_post', $post_ID)
     73        die(__('You are not allowed to edit this post.')); 
    6774 
    6875    // Rename. 
    6976    $_POST['ID'] = (int) $_POST['post_ID']; 
    70     $_POST['post_content'] = $_POST['content']; 
    71     $_POST['post_excerpt'] = $_POST['excerpt']; 
     77    $_POST['post_content'] = $_POST['content']; 
     78    $_POST['post_excerpt'] = $_POST['excerpt']; 
    7279    $_POST['post_parent'] = $_POST['parent_id']; 
    7380    $_POST['to_ping'] = $_POST['trackback_url']; 
    7481 
    75     if (! empty($_POST['post_author_override'])) { 
     82    if (!empty ($_POST['post_author_override'])) { 
    7683        $_POST['$post_author'] = (int) $_POST['post_author_override']; 
    77     } else if (! empty($_POST['post_author'])) { 
    78         $_POST['post_author'] = (int) $_POST['post_author']; 
    79     } else { 
    80         $_POST['post_author'] = (int) $_POST['user_ID']; 
    81     } 
    82  
    83     if ( ($_POST['post_author'] != $_POST['user_ID']) && ! current_user_can('edit_others_posts') ) 
    84         die( __('You cannot post as this user.') ); 
     84    } else 
     85        if (!empty ($_POST['post_author'])) { 
     86            $_POST['post_author'] = (int) $_POST['post_author']; 
     87        } else { 
     88            $_POST['post_author'] = (int) $_POST['user_ID']; 
     89        } 
     90 
     91    if (($_POST['post_author'] != $_POST['user_ID']) && !current_user_can('edit_others_posts')) 
     92        die(__('You cannot post as this user.')); 
    8593 
    8694    // What to do based on which button they pressed 
    87     if ('' != $_POST['saveasdraft']) $_POST['post_status'] = 'draft'; 
    88     if ('' != $_POST['saveasprivate']) $_POST['post_status'] = 'private'; 
    89     if ('' != $_POST['publish']) $_POST['post_status'] = 'publish'; 
    90     if ('' != $_POST['advanced']) $_POST['post_status'] = 'draft'; 
    91     if ('' != $_POST['savepage']) $_POST['post_status'] = 'static'; 
    92  
    93     if ( 'publish' == $_POST['post_status'] && ! current_user_can('publish_posts') ) 
     95    if ('' != $_POST['saveasdraft']) 
    9496        $_POST['post_status'] = 'draft'; 
    95      
    96     if ( !isset($_POST['comment_status']) ) 
     97    if ('' != $_POST['saveasprivate']) 
     98        $_POST['post_status'] = 'private'; 
     99    if ('' != $_POST['publish']) 
     100        $_POST['post_status'] = 'publish'; 
     101    if ('' != $_POST['advanced']) 
     102        $_POST['post_status'] = 'draft'; 
     103    if ('' != $_POST['savepage']) 
     104        $_POST['post_status'] = 'static'; 
     105 
     106    if ('publish' == $_POST['post_status'] && !current_user_can('publish_posts')) 
     107        $_POST['post_status'] = 'draft'; 
     108 
     109    if (!isset ($_POST['comment_status'])) 
    97110        $_POST['comment_status'] = 'closed'; 
    98111 
    99     if ( !isset($_POST['ping_status'])
     112    if (!isset ($_POST['ping_status'])
    100113        $_POST['ping_status'] = 'closed'; 
    101      
    102     if ( !empty($_POST['edit_date']) ) { 
     114 
     115    if (!empty ($_POST['edit_date'])) { 
    103116        $aa = $_POST['aa']; 
    104117        $mm = $_POST['mm']; 
     
    108121        $ss = $_POST['ss']; 
    109122        $jj = ($jj > 31) ? 31 : $jj; 
    110         $hh = ($hh > 23) ? $hh - 24 : $hh; 
    111         $mn = ($mn > 59) ? $mn - 60 : $mn; 
    112         $ss = ($ss > 59) ? $ss - 60 : $ss; 
     123        $hh = ($hh > 23) ? $hh -24 : $hh; 
     124        $mn = ($mn > 59) ? $mn -60 : $mn; 
     125        $ss = ($ss > 59) ? $ss -60 : $ss; 
    113126        $_POST['post_date'] = "$aa-$mm-$jj $hh:$mn:$ss"; 
    114127        $_POST['post_date_gmt'] = get_gmt_from_date("$aa-$mm-$jj $hh:$mn:$ss"); 
    115     }  
     128    } 
    116129 
    117130    wp_update_post($_POST); 
    118131 
    119132    // Meta Stuff 
    120     if ($_POST['meta']) : 
    121         foreach ($_POST['meta'] as $key => $value) : 
    122             update_meta($key, $value['key'], $value['value']); 
    123        endforeach; 
     133    if ($_POST['meta']) 
     134        : foreach ($_POST['meta'] as $key => $value) 
     135            : update_meta($key, $value['key'], $value['value']); 
     136    endforeach; 
    124137    endif; 
    125138 
    126     if ($_POST['deletemeta']) : 
    127         foreach ($_POST['deletemeta'] as $key => $value) : 
    128             delete_meta($key); 
    129        endforeach; 
     139    if ($_POST['deletemeta']) 
     140        : foreach ($_POST['deletemeta'] as $key => $value) 
     141            : delete_meta($key); 
     142    endforeach; 
    130143    endif; 
    131144 
    132145    add_meta($post_ID); 
    133      
     146 
    134147    return $post_ID; 
    135148} 
     
    141154    $comment_post_ID = (int) $_POST['comment_post_ID']; 
    142155 
    143     if ( ! current_user_can('edit_post', $comment_post_ID)
    144         die( __('You are not allowed to edit comments on this post, so you cannot edit this comment.') ); 
     156    if (!current_user_can('edit_post', $comment_post_ID)
     157        die(__('You are not allowed to edit comments on this post, so you cannot edit this comment.')); 
    145158 
    146159    $_POST['comment_author'] = $_POST['newcomment_author']; 
    147     $_POST['comment_author_email'] = $_POST['newcomment_author_email']; 
     160    $_POST['comment_author_email'] = $_POST['newcomment_author_email']; 
    148161    $_POST['comment_author_url'] = $_POST['newcomment_author_url']; 
    149162    $_POST['comment_approved'] = $_POST['comment_status']; 
    150163    $_POST['comment_content'] = $_POST['content']; 
    151164    $_POST['comment_ID'] = (int) $_POST['comment_ID']; 
    152   
    153     if ( !empty($_POST['edit_date']) ) { 
     165 
     166    if (!empty ($_POST['edit_date'])) { 
    154167        $aa = $_POST['aa']; 
    155168        $mm = $_POST['mm']; 
     
    159172        $ss = $_POST['ss']; 
    160173        $jj = ($jj > 31) ? 31 : $jj; 
    161         $hh = ($hh > 23) ? $hh - 24 : $hh; 
    162         $mn = ($mn > 59) ? $mn - 60 : $mn; 
    163         $ss = ($ss > 59) ? $ss - 60 : $ss; 
     174        $hh = ($hh > 23) ? $hh -24 : $hh; 
     175        $mn = ($mn > 59) ? $mn -60 : $mn; 
     176        $ss = ($ss > 59) ? $ss -60 : $ss; 
    164177        $_POST['comment_date'] = "$aa-$mm-$jj $hh:$mn:$ss"; 
    165178    } 
     
    182195 
    183196    if ($post->post_status == 'static') 
    184         $post->page_template = get_post_meta($id, '_wp_page_template', true);   
     197        $post->page_template = get_post_meta($id, '_wp_page_template', true); 
    185198 
    186199    return $post; 
     
    234247 
    235248    // Are we updating or creating? 
    236     if ( !empty($cat_ID) ) { 
     249    if (!empty ($cat_ID)) { 
    237250        $update = true; 
    238251    } else { 
     
    244257    $cat_name = wp_specialchars($cat_name); 
    245258 
    246     if ( empty($category_nicename)
     259    if (empty ($category_nicename)
    247260        $category_nicename = sanitize_title($cat_name, $cat_ID); 
    248261    else 
    249262        $category_nicename = sanitize_title($category_nicename, $cat_ID); 
    250263 
    251     if ( empty($category_description)
     264    if (empty ($category_description)
    252265        $category_description = ''; 
    253266 
    254     if ( empty($category_parent)
     267    if (empty ($category_parent)
    255268        $category_parent = 0; 
    256269 
    257     if ( !$update) 
     270    if (!$update) 
    258271        $query = "INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent) VALUES ('0', '$cat_name', '$category_nicename', '$category_description', '$cat')"; 
    259272    else 
     
    262275    $result = $wpdb->query($query); 
    263276 
    264     if ( $update ) { 
     277    if ($update) { 
    265278        do_action('edit_category', $cat_ID); 
    266279    } else { 
     
    276289 
    277290    $cat_ID = (int) $catarr['cat_ID']; 
    278      
     291 
    279292    // First, get all of the original fields 
    280     $category = get_category($cat_ID, ARRAY_A);     
     293    $category = get_category($cat_ID, ARRAY_A); 
    281294 
    282295    // Escape data pulled from DB. 
     
    295308 
    296309    // Don't delete the default cat. 
    297     if ( 1 == $cat_ID
     310    if (1 == $cat_ID
    298311        return 0; 
    299312 
     
    321334} 
    322335 
    323  
    324336function wp_create_categories($categories, $post_id = '') { 
    325     $cat_ids = array(); 
     337    $cat_ids = array (); 
    326338    foreach ($categories as $category) { 
    327         if ( $id = category_exists($category)
     339        if ($id = category_exists($category)
    328340            $cat_ids[] = $id; 
    329         else if ( $id = wp_create_category($category) ) 
    330             $cat_ids[] = $id;                
    331     } 
    332      
    333     if ( $post_id ) 
     341        else 
     342            if ($id = wp_create_category($category)) 
     343                $cat_ids[] = $id; 
     344    } 
     345 
     346    if ($post_id) 
    334347        wp_set_post_cats('', $post_id, $cat_ids); 
    335          
     348 
    336349    return $cat_ids; 
    337350} 
     
    339352function category_exists($cat_name) { 
    340353    global $wpdb; 
    341     if ( !$category_nicename = sanitize_title($cat_name)
     354    if (!$category_nicename = sanitize_title($cat_name)
    342355        return 0; 
    343          
     356 
    344357    return $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_nicename'"); 
    345358} 
     
    348361 
    349362function add_user() { 
    350     return update_user();   
    351 } 
    352  
    353 function update_user($user_id = 0) { 
    354      
    355     if ( $user_id != 0 ) { 
     363    return edit_user(); 
     364} 
     365 
     366function edit_user($user_id = 0) { 
     367 
     368    if ($user_id != 0) { 
    356369        $update = true; 
    357370        $user->ID = $user_id; 
     
    362375        $user = ''; 
    363376    } 
    364      
    365     if ( isset($_POST['user_login'])
     377 
     378    if (isset ($_POST['user_login'])
    366379        $user->user_login = wp_specialchars(trim($_POST['user_login'])); 
    367380 
    368381    $pass1 = $pass2 = ''; 
    369     if ( isset($_POST['pass1'])
     382    if (isset ($_POST['pass1'])
    370383        $pass1 = $_POST['pass1']; 
    371     if ( isset($_POST['pass2'])
     384    if (isset ($_POST['pass2'])
    372385        $pass2 = $_POST['pass2']; 
    373386 
    374     if ( isset($_POST['email'])
     387    if (isset ($_POST['email'])
    375388        $user->user_email = wp_specialchars(trim($_POST['email'])); 
    376     if ( isset($_POST['url']) ) { 
     389    if (isset ($_POST['url'])) { 
    377390        $user->user_url = wp_specialchars(trim($_POST['url'])); 
    378         $user->user_url = preg_match('/^(https?|ftps?|mailto|news|gopher):/is', $user->user_url) ? $user->user_url : 'http://' . $user->user_url;  
    379     } 
    380     if ( isset($_POST['first_name'])
     391        $user->user_url = preg_match('/^(https?|ftps?|mailto|news|gopher):/is', $user->user_url) ? $user->user_url : 'http://'.$user->user_url; 
     392    } 
     393    if (isset ($_POST['first_name'])
    381394        $user->first_name = wp_specialchars(trim($_POST['first_name'])); 
    382     if ( isset($_POST['last_name'])
     395    if (isset ($_POST['last_name'])
    383396        $user->last_name = wp_specialchars(trim($_POST['last_name'])); 
    384     if ( isset($_POST['nickname'])
     397    if (isset ($_POST['nickname'])
    385398        $user->nickname = wp_specialchars(trim($_POST['nickname'])); 
    386     if ( isset($_POST['display_name'])
     399    if (isset ($_POST['display_name'])
    387400        $user->display_name = wp_specialchars(trim($_POST['display_name'])); 
    388     if ( isset($_POST['description'])
     401    if (isset ($_POST['description'])
    389402        $user->description = wp_specialchars(trim($_POST['description'])); 
    390     if ( isset($_POST['jabber'])
     403    if (isset ($_POST['jabber'])
    391404        $user->jabber = wp_specialchars(trim($_POST['jabber'])); 
    392     if ( isset($_POST['aim'])
     405    if (isset ($_POST['aim'])
    393406        $user->aim = wp_specialchars(trim($_POST['aim'])); 
    394     if ( isset($_POST['yim'])
     407    if (isset ($_POST['yim'])
    395408        $user->yim = wp_specialchars(trim($_POST['yim'])); 
    396409 
    397     $errors = array(); 
    398          
     410    $errors = array (); 
     411 
    399412    /* checking that username has been typed */ 
    400413    if ($user->user_login == '') 
     
    402415 
    403416    /* checking the password has been typed twice */ 
    404     do_action('check_passwords', array($user->user_login, &$pass1, &$pass2)); 
    405      
    406     if ( !$update ) { 
    407         if ( $pass1 == '' || $pass2 == ''
     417    do_action('check_passwords', array ($user->user_login, & $pass1, & $pass2)); 
     418 
     419    if (!$update) { 
     420        if ($pass1 == '' || $pass2 == ''
    408421            $errors['pass'] = __('<strong>ERROR</strong>: Please enter your password twice.'); 
    409422    } else { 
    410         if ( ( empty($pass1) && !empty($pass2) ) || ( empty($pass2) && !empty($pass1) )
     423        if ((empty ($pass1) && !empty ($pass2)) || (empty ($pass2) && !empty ($pass1))
    411424            $errors['pass'] = __("<strong>ERROR</strong>: you typed your new password only once."); 
    412425    } 
    413      
     426 
    414427    /* checking the password has been typed twice the same */ 
    415428    if ($pass1 != $pass2) 
    416429        $errors['pass'] = __('<strong>ERROR</strong>: Please type the same password in the two password fields.'); 
    417430 
    418     if ( !empty($pass1)
     431    if (!empty ($pass1)
    419432        $user->user_pass = $pass1; 
    420      
    421     if ( !$update && username_exists( $user_login )
     433 
     434    if (!$update && username_exists($user_login)
    422435        $errors['user_login'] = __('<strong>ERROR</strong>: This username is already registered, please choose another one.'); 
    423436 
    424437    /* checking e-mail address */ 
    425     if (empty($user->user_email)) { 
     438    if (empty ($user->user_email)) { 
    426439        $errors['user_email'] = __("<strong>ERROR</strong>: please type an e-mail address"); 
    427     } else if (!is_email($user->user_email)) { 
    428         $errors['user_email'] = __("<strong>ERROR</strong>: the email address isn't correct"); 
    429     } 
    430  
    431     if ( count($errors) != 0 ) 
     440    } else 
     441        if (!is_email($user->user_email)) { 
     442            $errors['user_email'] = __("<strong>ERROR</strong>: the email address isn't correct"); 
     443        } 
     444 
     445    if (count($errors) != 0) 
    432446        return $errors; 
    433      
    434     if ( $update ) { 
     447 
     448    if ($update) { 
    435449        $user_id = wp_update_user(get_object_vars($user)); 
    436450    } else { 
     
    438452        wp_new_user_notification($user_id); 
    439453    } 
    440      
     454 
    441455    return $errors; 
    442456} 
     
    446460 
    447461    $id = (int) $id; 
    448      
    449     if($reassign == 'novalue') { 
     462 
     463    if ($reassign == 'novalue') { 
    450464        $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id"); 
    451      
     465 
    452466        if ($post_ids) { 
    453467            $post_ids = implode(',', $post_ids); 
    454              
     468 
    455469            // Delete comments, *backs 
    456470            $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID IN ($post_ids)"); 
     
    462476            $wpdb->query("DELETE FROM $wpdb->posts WHERE post_author = $id"); 
    463477        } 
    464      
     478 
    465479        // Clean links 
    466480        $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id"); 
    467481    } else { 
    468         $reassign = (int)$reassign; 
     482        $reassign = (int) $reassign; 
    469483        $wpdb->query("UPDATE $wpdb->posts SET post_author = {$reassign} WHERE post_author = {$id}"); 
    470484        $wpdb->query("UPDATE $wpdb->links SET link_owner = {$reassign} WHERE link_owner = {$id}"); 
     
    479493} 
    480494 
     495function get_link($link_id, $output = OBJECT) { 
     496    global $wpdb; 
     497     
     498    $link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$link_id'"); 
     499 
     500    if ( $output == OBJECT ) { 
     501        return $link; 
     502    } elseif ( $output == ARRAY_A ) { 
     503        return get_object_vars($link); 
     504    } elseif ( $output == ARRAY_N ) { 
     505        return array_values(get_object_vars($link)); 
     506    } else { 
     507        return $link; 
     508    } 
     509} 
     510 
     511function get_link_to_edit($link_id) { 
     512    $link = get_link($link_id); 
     513     
     514    $link->link_url = wp_specialchars($link->link_url, 1); 
     515    $link->link_name = wp_specialchars($link->link_name, 1); 
     516    $link->link_description = wp_specialchars($link->link_description); 
     517    $link->link_notes = wp_specialchars($link->link_notes); 
     518    $link->link_rss = wp_specialchars($link->link_rss); 
     519     
     520    return $link; 
     521} 
     522 
     523function add_link() { 
     524    return edit_link();  
     525} 
     526 
     527function edit_link($link_id = '') { 
     528    if (!current_user_can('manage_links')) 
     529        die(__("Cheatin' uh ?")); 
     530 
     531    $_POST['link_url'] = wp_specialchars($_POST['link_url']); 
     532    //$link_url = preg_match('/^(https?|ftps?|mailto|news|gopher):/is', $link_url) ? $link_url : 'http://'.$link_url; 
     533    $_POST['link_name'] = wp_specialchars($_POST['link_name']); 
     534    $_POST['link_image'] = wp_specialchars($_POST['link_image']); 
     535    $_POST['link_rss'] = wp_specialchars($_POST['link_rss']); 
     536    $auto_toggle = get_autotoggle($_POST['link_category']); 
     537     
     538    // if we are in an auto toggle category and this one is visible then we 
     539    // need to make the others invisible before we add this new one. 
     540    // FIXME Add category toggle func. 
     541    //if (($auto_toggle == 'Y') && ($link_visible == 'Y')) { 
     542    //  $wpdb->query("UPDATE $wpdb->links set link_visible = 'N' WHERE link_category = $link_category"); 
     543    //} 
     544 
     545    if ( !empty($link_id) ) { 
     546        $_POST['link_id'] = $link_id; 
     547        return wp_update_link($_POST); 
     548    } else { 
     549        return wp_insert_link($_POST); 
     550    } 
     551} 
     552 
     553function wp_insert_link($linkdata) { 
     554    global $wpdb; 
     555     
     556    extract($linkdata); 
     557 
     558    $update = false; 
     559    if ( !empty($link_id) ) 
     560        $update = true; 
     561 
     562    if ( empty($link_rating) ) 
     563        $link_rating = 0;    
     564 
     565    if ( empty($link_target) ) 
     566        $link_target = '';   
     567 
     568    if ( empty($link_visible) ) 
     569        $link_visible = 'Y';     
     570     
     571    if ( $update ) { 
     572        $wpdb->query("UPDATE $wpdb->links SET link_url='$link_url', 
     573            link_name='$link_name', link_image='$link_image', 
     574            link_target='$link_target', link_category='$link_category', 
     575            link_visible='$link_visible', link_description='$link_description', 
     576            link_rating='$link_rating', link_rel='$link_rel', 
     577            link_notes='$link_notes', link_rss = '$link_rss' 
     578            WHERE link_id='$link_id'"); 
     579    } else { 
     580        $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')"); 
     581        $link_id = $wpdb->insert_id; 
     582    } 
     583     
     584    if ( $update ) 
     585        do_action('edit_link', $link_id); 
     586    else 
     587        do_action('add_link', $link_id); 
     588 
     589    return $link_id; 
     590} 
     591 
     592function wp_update_link($linkdata) { 
     593    global $wpdb; 
     594 
     595    $link_id = (int) $linkdata['link_id']; 
     596     
     597    $link = get_link($link_id, ARRAY_A); 
     598     
     599    // Escape data pulled from DB. 
     600    $link = add_magic_quotes($link); 
     601     
     602    // Merge old and new fields with new fields overwriting old ones. 
     603    $linkdata = array_merge($link, $linkdata); 
     604 
     605    return wp_insert_link($linkdata); 
     606} 
     607 
     608function wp_delete_link($link_id) { 
     609    global $wpdb; 
     610 
     611    return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");  
     612} 
     613 
    481614function post_exists($title, $content = '', $post_date = '') { 
    482615    global $wpdb; 
    483      
    484     if ( !empty($post_date)
     616 
     617    if (!empty ($post_date)
    485618        $post_date = "AND post_date = '$post_date'"; 
    486619 
    487     if ( ! empty($title)
     620    if (!empty ($title)
    488621        return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' $post_date"); 
    489     else if ( ! empty($content) ) 
    490         return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' $post_date"); 
     622    else 
     623        if (!empty ($content)) 
     624            return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' $post_date"); 
    491625 
    492626    return 0; 
     
    497631 
    498632    return $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments 
    499         WHERE comment_author = '$comment_author' AND comment_date = '$comment_date'"); 
    500 } 
    501  
    502 function url_shorten ($url) { 
     633           WHERE comment_author = '$comment_author' AND comment_date = '$comment_date'"); 
     634} 
     635 
     636function url_shorten($url) { 
    503637    $short_url = str_replace('http://', '', stripslashes($url)); 
    504638    $short_url = str_replace('www.', '', $short_url); 
     
    506640        $short_url = substr($short_url, 0, -1); 
    507641    if (strlen($short_url) > 35) 
    508         $short_url = substr($short_url, 0, 32).'...'; 
     642        $short_url = substr($short_url, 0, 32).'...'; 
    509643    return $short_url; 
    510644} 
    511645 
    512646function selected($selected, $current) { 
    513     if ($selected == $current) echo ' selected="selected"'; 
     647    if ($selected == $current) 
     648        echo ' selected="selected"'; 
    514649} 
    515650 
    516651function checked($checked, $current) { 
    517     if ($checked == $current) echo ' checked="checked"'; 
    518 
    519  
    520 function return_categories_list( $parent = 0 ) { 
     652    if ($checked == $current) 
     653        echo ' checked="checked"'; 
     654
     655 
     656function return_categories_list($parent = 0) { 
    521657    global $wpdb; 
    522658    return $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent ORDER BY category_count DESC"); 
     
    524660 
    525661function get_nested_categories($default = 0, $parent = 0) { 
    526  global $post_ID, $mode, $wpdb; 
    527  
    528  if ($post_ID) { 
    529    $checked_categories = $wpdb->get_col(" 
    530      SELECT category_id 
    531      FROM $wpdb->categories, $wpdb->post2cat 
    532      WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$post_ID' 
    533      "); 
    534  
    535    if(count($checked_categories) == 0) 
    536    { 
    537      // No selected categories, strange 
    538      $checked_categories[] = $default; 
    539    } 
    540  
    541  } else { 
    542    $checked_categories[] = $default; 
    543  } 
    544  
    545  $cats = return_categories_list($parent); 
    546  $result = array(); 
    547  
    548     if ( is_array( $cats ) ) { 
    549         foreach($cats as $cat) { 
     662    global $post_ID, $mode, $wpdb; 
     663 
     664    if ($post_ID) { 
     665        $checked_categories = $wpdb->get_col(" 
     666             SELECT category_id 
     667             FROM $wpdb->categories, $wpdb->post2cat 
     668             WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$post_ID' 
     669             "); 
     670 
     671        if (count($checked_categories) == 0) { 
     672            // No selected categories, strange 
     673            $checked_categories[] = $default; 
     674        } 
     675 
     676    } else { 
     677        $checked_categories[] = $default; 
     678    } 
     679 
     680    $cats = return_categories_list($parent); 
     681    $result = array (); 
     682 
     683    if (is_array($cats)) { 
     684        foreach ($cats as $cat) { 
    550685            $result[$cat]['children'] = get_nested_categories($default, $cat); 
    551686            $result[$cat]['cat_ID'] = $cat; 
     
    559694 
    560695function write_nested_categories($categories) { 
    561  foreach($categories as $category) { 
    562    echo '<label for="category-', $category['cat_ID'], '" class="selectit"><input value="', $category['cat_ID'], 
    563      '" type="checkbox" name="post_category[]" id="category-', $category['cat_ID'], '"', 
    564      ($category['checked'] ? ' checked="checked"' : ""), '/> ', wp_specialchars($category['cat_name']), "</label>\n"; 
    565  
    566    if(isset($category['children'])) { 
    567      echo "\n<span class='cat-nest'>\n"; 
    568      write_nested_categories($category['children']); 
    569      echo "</span>\n"; 
    570    } 
    571  } 
     696    foreach ($categories as $category) { 
     697        echo '<label for="category-', $category['cat_ID'], '" class="selectit"><input value="', $category['cat_ID'], '" type="checkbox" name="post_category[]" id="category-', $category['cat_ID'], '"', ($category['checked'] ? ' checked="checked"' : ""), '/> ', wp_specialchars($category['cat_name']), "</label>\n"; 
     698 
     699        if (isset ($category['children'])) { 
     700            echo "\n<span class='cat-nest'>\n"; 
     701            write_nested_categories($category['children']); 
     702            echo "</span>\n"; 
     703        } 
     704    } 
    572705} 
    573706 
    574707function dropdown_categories($default = 0) { 
    575  write_nested_categories(get_nested_categories($default)); 
    576 }  
     708   write_nested_categories(get_nested_categories($default)); 
     709} 
    577710 
    578711// Dandy new recursive multiple category stuff. 
     
    580713    global $wpdb, $class; 
    581714 
    582     if ( !$categories
     715    if (!$categories
    583716        $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY cat_name"); 
    584717 
     
    589722                $count = $wpdb->get_var("SELECT COUNT(post_id) FROM $wpdb->post2cat WHERE category_id = $category->cat_ID"); 
    590723                $pad = str_repeat('&#8212; ', $level); 
    591                 if ( current_user_can('manage_categories')
    592                     $edit = "<a href='categories.php?action=edit&amp;cat_ID=$category->cat_ID' class='edit'>" . __('Edit') . "</a></td><td><a href='categories.php?action=delete&amp;cat_ID=$category->cat_ID' onclick=\"return deleteSomething( 'cat', $category->cat_ID, '" . sprintf(__("You are about to delete the category &quot;%s&quot;.  All of its posts will go to the default category.\\n&quot;OK&quot; to delete, &quot;Cancel&quot; to stop."), wp_specialchars($category->cat_name, 1))  . "' );\" class='delete'>" .  __('Delete') . "</a>"; 
     724                if (current_user_can('manage_categories')
     725                    $edit = "<a href='categories.php?action=edit&amp;cat_ID=$category->cat_ID' class='edit'>".__('Edit')."</a></td><td><a href='categories.php?action=delete&amp;cat_ID=$category->cat_ID' onclick=\"return deleteSomething( 'cat', $category->cat_ID, '".sprintf(__("You are about to delete the category &quot;%s&quot;.  All of its posts will go to the default category.\\n&quot;OK&quot; to delete, &quot;Cancel&quot; to stop."), wp_specialchars($category->cat_name, 1))."' );\" class='delete'>".__('Delete')."</a>"; 
    593726                else 
    594727                    $edit = ''; 
    595                  
     728 
    596729                $class = ('alternate' == $class) ? '' : 'alternate'; 
    597730                echo "<tr id='cat-$category->cat_ID' class='$class'><th scope='row'>$category->cat_ID</th><td>$pad $category->cat_name</td> 
    598                 <td>$category->category_description</td> 
    599                 <td>$count</td> 
    600                 <td>$edit</td> 
    601                 </tr>"; 
    602                 cat_rows($category->cat_ID, $level + 1, $categories); 
     731                               <td>$category->category_description</td> 
     732                               <td>$count</td> 
     733                               <td>$edit</td> 
     734                               </tr>"; 
     735                cat_rows($category->cat_ID, $level +1, $categories); 
    603736            } 
    604737        } 
     
    608741} 
    609742 
    610 function page_rows( $parent = 0, $level = 0, $pages = 0 ) { 
     743function page_rows($parent = 0, $level = 0, $pages = 0) { 
    611744    global $wpdb, $class, $post; 
    612745    if (!$pages) 
     
    614747 
    615748    if ($pages) { 
    616         foreach ($pages as $post) { start_wp(); 
     749        foreach ($pages as $post) { 
     750            start_wp(); 
    617751            if ($post->post_parent == $parent) { 
    618752                $post->post_title = wp_specialchars($post->post_title); 
     
    634768 
    635769<?php 
    636                 page_rows($id, $level + 1, $pages); 
     770 
     771                page_rows($id, $level +1, $pages); 
    637772            } 
    638773        } 
     
    648783    } 
    649784    if ($categories) { 
    650         foreach ($categories as $category) { if ($currentcat != $category->cat_ID && $parent == $category->category_parent) { 
    651             $count = $wpdb->get_var("SELECT COUNT(post_id) FROM $wpdb->post2cat WHERE category_id = $category->cat_ID"); 
    652             $pad = str_repeat('&#8211; ', $level); 
    653             $category->cat_name = wp_specialchars($category->cat_name); 
    654             echo "\n\t<option value='$category->cat_ID'"; 
    655             if ($currentparent == $category->cat_ID) 
    656                 echo " selected='selected'"; 
    657             echo ">$pad$category->cat_name</option>"; 
    658             wp_dropdown_cats($currentcat, $currentparent, $category->cat_ID, $level + 1, $categories); 
    659         } } 
     785        foreach ($categories as $category) { 
     786            if ($currentcat != $category->cat_ID && $parent == $category->category_parent) { 
     787                $count = $wpdb->get_var("SELECT COUNT(post_id) FROM $wpdb->post2cat WHERE category_id = $category->cat_ID"); 
     788                $pad = str_repeat('&#8211; ', $level); 
     789                $category->cat_name = wp_specialchars($category->cat_name); 
     790                echo "\n\t<option value='$category->cat_ID'"; 
     791                if ($currentparent == $category->cat_ID) 
     792                    echo " selected='selected'"; 
     793                echo ">$pad$category->cat_name</option>"; 
     794                wp_dropdown_cats($currentcat, $currentparent, $category->cat_ID, $level +1, $categories); 
     795            } 
     796        } 
    660797    } else { 
    661798        return false; 
     
    665802function wp_create_thumbnail($file, $max_side, $effect = '') { 
    666803 
    667     // 1 = GIF, 2 = JPEG, 3 = PNG 
    668  
    669     if(file_exists($file)) { 
    670         $type = getimagesize($file); 
    671          
    672         // if the associated function doesn't exist - then it's not 
    673         // handle. duh. i hope. 
    674          
    675         if(!function_exists('imagegif') && $type[2] == 1) { 
    676             $error = __('Filetype not supported. Thumbnail not created.'); 
    677         }elseif(!function_exists('imagejpeg') && $type[2] == 2) { 
    678             $error = __('Filetype not supported. Thumbnail not created.'); 
    679         }elseif(!function_exists('imagepng') && $type[2] == 3) { 
    680             $error = __('Filetype not supported. Thumbnail not created.'); 
    681         } else { 
    682          
    683             // create the initial copy from the original file 
    684             if($type[2] == 1) { 
    685                 $image = imagecreatefromgif($file); 
    686             } elseif($type[2] == 2) { 
    687                 $image = imagecreatefromjpeg($file); 
    688             } elseif($type[2] == 3) { 
    689                 $image = imagecreatefrompng($file); 
    690             } 
    691              
     804        // 1 = GIF, 2 = JPEG, 3 = PNG 
     805 
     806    if (file_exists($file)) { 
     807        $type = getimagesize($file); 
     808 
     809        // if the associated function doesn't exist - then it's not 
     810        // handle. duh. i hope. 
     811 
     812        if (!function_exists('imagegif') && $type[2] == 1) { 
     813            $error = __('Filetype not supported. Thumbnail not created.'); 
     814        } 
     815        elseif (!function_exists('imagejpeg') && $type[2] == 2) { 
     816            $error = __('Filetype not supported. Thumbnail not created.'); 
     817        } 
     818        elseif (!function_exists('imagepng') && $type[2] == 3) { 
     819            $error = __('Filetype not supported. Thumbnail not created.'); 
     820        } else { 
     821 
     822            // create the initial copy from the original file 
     823            if ($type[2] == 1) { 
     824                $image = imagecreatefromgif($file); 
     825            } 
     826            elseif ($type[2] == 2) { 
     827                $image = imagecreatefromjpeg($file); 
     828            } 
     829            elseif ($type[2] == 3) { 
     830                $image = imagecreatefrompng($file); 
     831            } 
     832 
    692833            if (function_exists('imageantialias')) 
    693                 imageantialias($image, TRUE); 
    694              
    695             $image_attr = getimagesize($file); 
    696              
    697             // figure out the longest side 
    698              
    699             if($image_attr[0] > $image_attr[1]) { 
    700                 $image_width = $image_attr[0]; 
    701                 $image_height = $image_attr[1]; 
    702                 $image_new_width = $max_side; 
    703                  
    704                 $image_ratio = $image_width/$image_new_width; 
    705                 $image_new_height = $image_height/$image_ratio; 
    706                 //width is > height 
    707             } else { 
    708                 $image_width = $image_attr[0]; 
    709                 $image_height = $image_attr[1]; 
    710                 $image_new_height = $max_side; 
    711                  
    712                 $image_ratio = $image_height/$image_new_height; 
    713                 $image_new_width = $image_width/$image_ratio; 
    714                 //height > width 
    715             } 
    716              
    717             $thumbnail = imagecreatetruecolor($image_new_width, $image_new_height); 
    718             @imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1]); 
    719              
    720             // move the thumbnail to it's final destination 
    721              
    722             $path = explode('/', $file); 
    723             $thumbpath = substr($file, 0, strrpos($file, '/')) . '/thumb-' . $path[count($path)-1]; 
    724              
    725             if($type[2] == 1) { 
    726                 if(!imagegif($thumbnail, $thumbpath)) { 
    727                     $error = __("Thumbnail path invalid"); 
    728                 } 
    729             } elseif($type[2] == 2) { 
    730                 if(!imagejpeg($thumbnail, $thumbpath)) { 
    731                     $error = __("Thumbnail path invalid"); 
    732                 } 
    733             } elseif($type[2] == 3) { 
    734                 if(!imagepng($thumbnail, $thumbpath)) { 
    735                     $error = __("Thumbnail path invalid"); 
    736                 } 
    737             } 
    738              
    739         } 
    740     } 
    741      
    742     if(!empty($error)) 
    743     { 
    744         return $error; 
    745     } 
    746     else 
    747     { 
    748         return 1; 
    749     } 
     834                imageantialias($image, TRUE); 
     835 
     836            $image_attr = getimagesize($file); 
     837 
     838            // figure out the longest side 
     839 
     840            if ($image_attr[0] > $image_attr[1]) { 
     841                $image_width = $image_attr[0]; 
     842                $image_height = $image_attr[1]; 
     843                $image_new_width = $max_side; 
     844 
     845                $image_ratio = $image_width / $image_new_width; 
     846                $image_new_height = $image_height / $image_ratio; 
     847                //width is > height 
     848            } else { 
     849                $image_width = $image_attr[0]; 
     850                $image_height = $image_attr[1]; 
     851                $image_new_height = $max_side; 
     852 
     853                $image_ratio = $image_height / $image_new_height; 
     854                $image_new_width = $image_width / $image_ratio; 
     855                //height > width 
     856            } 
     857 
     858            $thumbnail = imagecreatetruecolor($image_new_width, $image_new_height); 
     859            @ imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1]); 
     860 
     861            // move the thumbnail to it's final destination 
     862 
     863            $path = explode('/', $file); 
     864            $thumbpath = substr($file, 0, strrpos($file, '/')).'/thumb-'.$p