home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Internet & Communication / WordPress 1.5.1.dmg / wordpress / wp-includes / functions-post.php < prev    next >
Encoding:
PHP Script  |  2005-04-28  |  18.4 KB  |  609 lines

  1. <?php
  2.  
  3. /**** DB Functions ****/
  4.  
  5. /*
  6.  * generic function for inserting data into the posts table.
  7.  */
  8. function wp_insert_post($postarr = array()) {
  9.     global $wpdb, $post_default_category, $allowedtags;
  10.     
  11.     // export array as variables
  12.     extract($postarr);
  13.     
  14.     // Do some escapes for safety
  15.     $post_title = $wpdb->escape($post_title);
  16.     $post_name = sanitize_title($post_title);
  17.     $post_excerpt = $wpdb->escape($post_excerpt);
  18.     $post_content = $wpdb->escape($post_content);
  19.     $post_author = (int) $post_author;
  20.  
  21.     // Make sure we set a valid category
  22.     if (0 == count($post_category) || !is_array($post_category)) {
  23.         $post_category = array($post_default_category);
  24.     }
  25.  
  26.     $post_cat = $post_category[0];
  27.     
  28.     if (empty($post_date))
  29.         $post_date = current_time('mysql');
  30.     // Make sure we have a good gmt date:
  31.     if (empty($post_date_gmt)) 
  32.         $post_date_gmt = get_gmt_from_date($post_date);
  33.     if (empty($comment_status))
  34.         $comment_status = get_settings('default_comment_status');
  35.     if (empty($ping_status))
  36.         $ping_status = get_settings('default_ping_status');
  37.     if ( empty($post_parent) )
  38.         $post_parent = 0;
  39.  
  40.     if ('publish' == $post_status) {
  41.         $post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_status = 'publish' AND ID != '$post_ID' LIMIT 1");
  42.         if ($post_name_check) {
  43.             $suffix = 2;
  44.             while ($post_name_check) {
  45.                 $alt_post_name = $post_name . "-$suffix";
  46.                 $post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_status = 'publish' AND ID != '$post_ID' LIMIT 1");
  47.                 $suffix++;
  48.             }
  49.             $post_name = $alt_post_name;
  50.         }
  51.     }
  52.  
  53.     $sql = "INSERT INTO $wpdb->posts 
  54.         (post_author, post_date, post_date_gmt, post_modified, post_modified_gmt, post_content, post_title, post_excerpt, post_category, post_status, post_name, comment_status, ping_status, post_parent) 
  55.         VALUES ('$post_author', '$post_date', '$post_date_gmt', '$post_date', '$post_date_gmt', '$post_content', '$post_title', '$post_excerpt', '$post_cat', '$post_status', '$post_name', '$comment_status', '$ping_status', '$post_parent')";
  56.     
  57.     $result = $wpdb->query($sql);
  58.     $post_ID = $wpdb->insert_id;
  59.  
  60.     // Set GUID
  61.     $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'");
  62.     
  63.     wp_set_post_cats('', $post_ID, $post_category);
  64.     
  65.     if ($post_status == 'publish') {
  66.         do_action('publish_post', $post_ID);
  67.     }
  68.  
  69.     pingback($content, $post_ID);
  70.  
  71.     // Return insert_id if we got a good result, otherwise return zero.
  72.     return $result ? $post_ID : 0;
  73. }
  74.  
  75. function wp_get_single_post($postid = 0, $mode = OBJECT) {
  76.     global $wpdb;
  77.  
  78.     $sql = "SELECT * FROM $wpdb->posts WHERE ID=$postid";
  79.     $result = $wpdb->get_row($sql, $mode);
  80.     
  81.     // Set categories
  82.     if($mode == OBJECT) {
  83.         $result->post_category = wp_get_post_cats('',$postid);
  84.     } 
  85.     else {
  86.         $result['post_category'] = wp_get_post_cats('',$postid);
  87.     }
  88.  
  89.     return $result;
  90. }
  91.  
  92. function wp_get_recent_posts($num = 10) {
  93.     global $wpdb;
  94.  
  95.     // Set the limit clause, if we got a limit
  96.     if ($num) {
  97.         $limit = "LIMIT $num";
  98.     }
  99.  
  100.     $sql = "SELECT * FROM $wpdb->posts WHERE post_status IN ('publish', 'draft', 'private') ORDER BY post_date DESC $limit";
  101.     $result = $wpdb->get_results($sql,ARRAY_A);
  102.  
  103.     return $result?$result:array();
  104. }
  105.  
  106. function wp_update_post($postarr = array()) {
  107.     global $wpdb;
  108.  
  109.     // First get all of the original fields
  110.     extract(wp_get_single_post($postarr['ID'], ARRAY_A));    
  111.  
  112.     // Now overwrite any changed values being passed in
  113.     extract($postarr);
  114.  
  115.     // Make sure we set a valid category
  116.     if ( 0 == count($post_category) || !is_array($post_category) )
  117.         $post_category = array($post_default_category);
  118.  
  119.     // Do some escapes for safety
  120.     $post_title   = $wpdb->escape($post_title);
  121.     $post_excerpt = $wpdb->escape($post_excerpt);
  122.     $post_content = $wpdb->escape($post_content);
  123.  
  124.     $post_modified = current_time('mysql');
  125.     $post_modified_gmt = current_time('mysql', 1);
  126.  
  127.     $sql = "UPDATE $wpdb->posts 
  128.         SET post_content = '$post_content',
  129.         post_title = '$post_title',
  130.         post_category = $post_category[0],
  131.         post_status = '$post_status',
  132.         post_date = '$post_date',
  133.         post_date_gmt = '$post_date_gmt',
  134.         post_modified = '$post_modified',
  135.         post_modified_gmt = '$post_modified_gmt',
  136.         post_excerpt = '$post_excerpt',
  137.         ping_status = '$ping_status',
  138.         comment_status = '$comment_status'
  139.         WHERE ID = $ID";
  140.         
  141.     $result = $wpdb->query($sql);
  142.     $rows_affected = $wpdb->rows_affected;
  143.  
  144.     wp_set_post_cats('', $ID, $post_category);
  145.  
  146.     do_action('edit_post', $ID);
  147.  
  148.     return $rows_affected;
  149. }
  150.  
  151. function wp_get_post_cats($blogid = '1', $post_ID = 0) {
  152.     global $wpdb;
  153.     
  154.     $sql = "SELECT category_id 
  155.         FROM $wpdb->post2cat 
  156.         WHERE post_id = $post_ID 
  157.         ORDER BY category_id";
  158.  
  159.     $result = $wpdb->get_col($sql);
  160.  
  161.     return array_unique($result);
  162. }
  163.  
  164. function wp_set_post_cats($blogid = '1', $post_ID = 0, $post_categories = array()) {
  165.     global $wpdb;
  166.     // If $post_categories isn't already an array, make it one:
  167.     if (!is_array($post_categories)) {
  168.         if (!$post_categories) {
  169.             $post_categories = 1;
  170.         }
  171.         $post_categories = array($post_categories);
  172.     }
  173.  
  174.     $post_categories = array_unique($post_categories);
  175.  
  176.     // First the old categories
  177.     $old_categories = $wpdb->get_col("
  178.         SELECT category_id 
  179.         FROM $wpdb->post2cat 
  180.         WHERE post_id = $post_ID");
  181.     
  182.     if (!$old_categories) {
  183.         $old_categories = array();
  184.     } else {
  185.         $old_categories = array_unique($old_categories);
  186.     }
  187.  
  188.  
  189.     $oldies = printr($old_categories,1);
  190.     $newbies = printr($post_categories,1);
  191.  
  192.     // Delete any?
  193.     $delete_cats = array_diff($old_categories,$post_categories);
  194.  
  195.     if ($delete_cats) {
  196.         foreach ($delete_cats as $del) {
  197.             $wpdb->query("
  198.                 DELETE FROM $wpdb->post2cat 
  199.                 WHERE category_id = $del 
  200.                     AND post_id = $post_ID 
  201.                 ");
  202.         }
  203.     }
  204.  
  205.     // Add any?
  206.     $add_cats = array_diff($post_categories, $old_categories);
  207.  
  208.     if ($add_cats) {
  209.         foreach ($add_cats as $new_cat) {
  210.             $wpdb->query("
  211.                 INSERT INTO $wpdb->post2cat (post_id, category_id) 
  212.                 VALUES ($post_ID, $new_cat)");
  213.         }
  214.     }
  215. }    // wp_set_post_cats()
  216.  
  217. function wp_delete_post($postid = 0) {
  218.     global $wpdb;
  219.     $postid = (int) $postid;
  220.  
  221.     if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $postid") )
  222.         return $post;
  223.  
  224.     if ( 'static' == $post->post_status )
  225.         $wpdb->query("UPDATE $wpdb->posts SET post_parent = $post->post_parent WHERE post_parent = $postid AND post_status = 'static'");
  226.  
  227.     $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = $postid");
  228.     
  229.     $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = $postid");
  230.  
  231.     $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id = $postid");
  232.  
  233.     $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = $postid");
  234.     
  235.     return $post;
  236. }
  237.  
  238. /**** /DB Functions ****/
  239.  
  240. /**** Misc ****/
  241.  
  242. // get permalink from post ID
  243. function post_permalink($post_id = 0, $mode = '') { // $mode legacy
  244.     return get_permalink($post_id);
  245. }
  246.  
  247. // Get the name of a category from its ID
  248. function get_cat_name($cat_id) {
  249.     global $wpdb;
  250.     
  251.     $cat_id -= 0;     // force numeric
  252.     $name = $wpdb->get_var("SELECT cat_name FROM $wpdb->categories WHERE cat_ID=$cat_id");
  253.     
  254.     return $name;
  255. }
  256.  
  257. // Get the ID of a category from its name
  258. function get_cat_ID($cat_name='General') {
  259.     global $wpdb;
  260.     
  261.     $cid = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$cat_name'");
  262.  
  263.     return $cid?$cid:1;    // default to cat 1
  264. }
  265.  
  266. // Get author's preferred display name
  267. function get_author_name( $auth_id ) {
  268.     $authordata = get_userdata( $auth_id );
  269.  
  270.     switch( $authordata['user_idmode'] ) {
  271.         case 'nickname':
  272.             $authorname = $authordata['user_nickname'];
  273.             break;
  274.         case 'login':
  275.             $authorname = $authordata['user_login'];
  276.             break;
  277.         case 'firstname':
  278.             $authorname = $authordata['user_firstname'];
  279.             break;
  280.         case 'lastname':
  281.             $authorname = $authordata['user_lastname'];
  282.             break;
  283.         case 'namefl':
  284.             $authorname = $authordata['user_firstname'].' '.$authordata['user_lastname'];
  285.             break;
  286.         case 'namelf':
  287.             $authorname = $authordata['user_lastname'].' '.$authordata['user_firstname'];
  288.             break;
  289.         default:
  290.             $authorname = $authordata['user_nickname'];
  291.             break;
  292.     }
  293.  
  294.     return $authorname;
  295. }
  296.  
  297. // get extended entry info (<!--more-->)
  298. function get_extended($post) {
  299.     list($main,$extended) = explode('<!--more-->', $post, 2);
  300.  
  301.     // Strip leading and trailing whitespace
  302.     $main = preg_replace('/^[\s]*(.*)[\s]*$/','\\1',$main);
  303.     $extended = preg_replace('/^[\s]*(.*)[\s]*$/','\\1',$extended);
  304.  
  305.     return array('main' => $main, 'extended' => $extended);
  306. }
  307.  
  308. // do trackbacks for a list of urls
  309. // borrowed from edit.php
  310. // accepts a comma-separated list of trackback urls and a post id
  311. function trackback_url_list($tb_list, $post_id) {
  312.     if (!empty($tb_list)) {
  313.         // get post data
  314.         $postdata = wp_get_single_post($post_id, ARRAY_A);
  315.  
  316.         // import postdata as variables
  317.         extract($postdata);
  318.         
  319.         // form an excerpt
  320.         $excerpt = strip_tags($post_excerpt?$post_excerpt:$post_content);
  321.         
  322.         if (strlen($excerpt) > 255) {
  323.             $excerpt = substr($excerpt,0,252) . '...';
  324.         }
  325.         
  326.         $trackback_urls = explode(',', $tb_list);
  327.         foreach($trackback_urls as $tb_url) {
  328.             $tb_url = trim($tb_url);
  329.             trackback($tb_url, stripslashes($post_title), $excerpt, $post_id);
  330.         }
  331.     }
  332. }
  333.  
  334.  
  335. // query user capabilities
  336. // rather simplistic. shall evolve with future permission system overhaul
  337. // $blog_id and $category_id are there for future usage
  338.  
  339. /* returns true if $user_id can create a new post */
  340. function user_can_create_post($user_id, $blog_id = 1, $category_id = 'None') {
  341.     $author_data = get_userdata($user_id);
  342.     return ($author_data->user_level > 1);
  343. }
  344.  
  345. /* returns true if $user_id can create a new post */
  346. function user_can_create_draft($user_id, $blog_id = 1, $category_id = 'None') {
  347.     $author_data = get_userdata($user_id);
  348.     return ($author_data->user_level >= 1);
  349. }
  350.  
  351. /* returns true if $user_id can edit $post_id */
  352. function user_can_edit_post($user_id, $post_id, $blog_id = 1) {
  353.     $author_data = get_userdata($user_id);
  354.     $post = get_post($post_id);
  355.     $post_author_data = get_userdata($post->post_author);
  356.  
  357.     if ( (($user_id == $post_author_data->ID) && !($post->post_status == 'publish' &&  $author_data->user_level < 2))
  358.          || ($author_data->user_level > $post_author_data->user_level)
  359.          || ($author_data->user_level >= 10) ) {
  360.         return true;
  361.     } else {
  362.         return false;
  363.     }
  364. }
  365.  
  366. /* returns true if $user_id can delete $post_id */
  367. function user_can_delete_post($user_id, $post_id, $blog_id = 1) {
  368.     // right now if one can edit, one can delete
  369.     return user_can_edit_post($user_id, $post_id, $blog_id);
  370. }
  371.  
  372. /* returns true if $user_id can set new posts' dates on $blog_id */
  373. function user_can_set_post_date($user_id, $blog_id = 1, $category_id = 'None') {
  374.     $author_data = get_userdata($user_id);
  375.     return (($author_data->user_level > 4) && user_can_create_post($user_id, $blog_id, $category_id));
  376. }
  377.  
  378. /* returns true if $user_id can edit $post_id's date */
  379. function user_can_edit_post_date($user_id, $post_id, $blog_id = 1) {
  380.     $author_data = get_userdata($user_id);
  381.     return (($author_data->user_level > 4) && user_can_edit_post($user_id, $post_id, $blog_id));
  382. }
  383.  
  384. /* returns true if $user_id can edit $post_id's comments */
  385. function user_can_edit_post_comments($user_id, $post_id, $blog_id = 1) {
  386.     // right now if one can edit a post, one can edit comments made on it
  387.     return user_can_edit_post($user_id, $post_id, $blog_id);
  388. }
  389.  
  390. /* returns true if $user_id can delete $post_id's comments */
  391. function user_can_delete_post_comments($user_id, $post_id, $blog_id = 1) {
  392.     // right now if one can edit comments, one can delete comments
  393.     return user_can_edit_post_comments($user_id, $post_id, $blog_id);
  394. }
  395.  
  396. function user_can_edit_user($user_id, $other_user) {
  397.     $user  = get_userdata($user_id);
  398.     $other = get_userdata($other_user);
  399.     if ( $user->user_level > $other->user_level || $user->user_level > 8 || $user->ID == $other->ID )
  400.         return true;
  401.     else
  402.         return false;
  403. }
  404.  
  405. function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
  406.     global $wpdb;
  407.  
  408.     do_action('wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent);
  409.  
  410.     if ( preg_match_all('/&#(\d+);/', $comment . $author . $url, $chars) ) {
  411.         foreach ($chars[1] as $char) {
  412.             // If it's an encoded char in the normal ASCII set, reject
  413.             if ($char < 128)
  414.                 return true;
  415.         }
  416.     }
  417.  
  418.     $mod_keys = trim( get_settings('blacklist_keys') );
  419.     if ('' == $mod_keys )
  420.         return false; // If moderation keys are empty
  421.     $words = explode("\n", $mod_keys );
  422.  
  423.     foreach ($words as $word) {
  424.         $word = trim($word);
  425.  
  426.         // Skip empty lines
  427.         if ( empty($word) ) { continue; }
  428.  
  429.         // Do some escaping magic so that '#' chars in the 
  430.         // spam words don't break things:
  431.         $word = preg_quote($word, '#');
  432.         
  433.         $pattern = "#$word#i"; 
  434.         if ( preg_match($pattern, $author    ) ) return true;
  435.         if ( preg_match($pattern, $email     ) ) return true;
  436.         if ( preg_match($pattern, $url       ) ) return true;
  437.         if ( preg_match($pattern, $comment   ) ) return true;
  438.         if ( preg_match($pattern, $user_ip   ) ) return true;
  439.         if ( preg_match($pattern, $user_agent) ) return true;
  440.     }
  441.     
  442.     if ( isset($_SERVER['REMOTE_ADDR']) ) {
  443.         if ( wp_proxy_check($_SERVER['REMOTE_ADDR']) ) return true;
  444.     }
  445.  
  446.     return false;
  447. }
  448.  
  449. function wp_proxy_check($ipnum) {
  450.     if ( get_option('open_proxy_check') && isset($ipnum) ) {
  451.         $rev_ip = implode( '.', array_reverse( explode( '.', $ipnum ) ) );
  452.         $lookup = $rev_ip . '.opm.blitzed.org';
  453.         if ( $lookup != gethostbyname( $lookup ) )
  454.             return true;
  455.     }
  456.  
  457.     return false;
  458. }
  459.  
  460. function wp_new_comment( $commentdata, $spam = false ) {
  461.     global $wpdb;
  462.  
  463.     $commentdata = apply_filters('preprocess_comment', $commentdata);
  464.     extract($commentdata);
  465.  
  466.     $comment_post_ID = (int) $comment_post_ID;
  467.  
  468.     $user_id = apply_filters('pre_user_id', $user_ID);
  469.     $author  = apply_filters('pre_comment_author_name', $comment_author);
  470.     $email   = apply_filters('pre_comment_author_email', $comment_author_email);
  471.     $url     = apply_filters('pre_comment_author_url', $comment_author_url);
  472.     $comment = apply_filters('pre_comment_content', $comment_content);
  473.     $comment = apply_filters('post_comment_text', $comment); // Deprecated
  474.     $comment = apply_filters('comment_content_presave', $comment); // Deprecated
  475.  
  476.     $user_ip     = apply_filters('pre_comment_user_ip', $_SERVER['REMOTE_ADDR']);
  477.     $user_domain = apply_filters('pre_comment_user_domain', gethostbyaddr($user_ip) );
  478.     $user_agent  = apply_filters('pre_comment_user_agent', $_SERVER['HTTP_USER_AGENT']);
  479.  
  480.     $now     = current_time('mysql');
  481.     $now_gmt = current_time('mysql', 1);
  482.  
  483.     if ( $user_id ) {
  484.         $userdata = get_userdata($user_id);
  485.         $post_author = $wpdb->get_var("SELECT post_author FROM $wpdb->posts WHERE ID = '$comment_post_ID' LIMIT 1");
  486.     }
  487.  
  488.     // Simple duplicate check
  489.     $dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$author' ";
  490.     if ( $email ) $dupe .= "OR comment_author_email = '$email' ";
  491.     $dupe .= ") AND comment_content = '$comment' LIMIT 1";
  492.     if ( $wpdb->get_var($dupe) )
  493.         die( __('Duplicate comment detected; it looks as though you\'ve already said that!') );
  494.  
  495.     // Simple flood-protection
  496.     if ( $lasttime = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_author_IP = '$user_ip' OR comment_author_email = '$email' ORDER BY comment_date DESC LIMIT 1") ) {
  497.         $time_lastcomment = mysql2date('U', $lasttime);
  498.         $time_newcomment  = mysql2date('U', $now_gmt);
  499.         if ( ($time_newcomment - $time_lastcomment) < 15 ) {
  500.             do_action('comment_flood_trigger', $time_lastcomment, $time_newcomment);
  501.             die( __('Sorry, you can only post a new comment once every 15 seconds. Slow down cowboy.') );
  502.         }
  503.     }
  504.  
  505.     if ( $userdata && ( $user_id == $post_author || $userdata->user_level >= 9 ) ) {
  506.         $approved = 1;
  507.     } else {
  508.         if ( check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) )
  509.             $approved = 1;
  510.         else
  511.             $approved = 0;
  512.         if ( wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) )
  513.             $approved = 'spam';
  514.     }
  515.  
  516.     $approved = apply_filters('pre_comment_approved', $approved);
  517.  
  518.     $result = $wpdb->query("INSERT INTO $wpdb->comments 
  519.     (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_date_gmt, comment_content, comment_approved, comment_agent, comment_type, user_id)
  520.     VALUES 
  521.     ('$comment_post_ID', '$author', '$email', '$url', '$user_ip', '$now', '$now_gmt', '$comment', '$approved', '$user_agent', '$comment_type', '$user_id')
  522.     ");
  523.  
  524.     $comment_id = $wpdb->insert_id;
  525.     do_action('comment_post', $comment_id, $approved);
  526.  
  527.     if ( 'spam' !== $approved ) { // If it's spam save it silently for later crunching
  528.         if ( '0' == $approved )
  529.             wp_notify_moderator($comment_id);
  530.     
  531.         if ( get_settings('comments_notify') && $approved )
  532.             wp_notify_postauthor($comment_id, $comment_type);
  533.     }
  534.  
  535.     return $result;
  536. }
  537.  
  538. function do_trackbacks($post_id) {
  539.     global $wpdb;
  540.  
  541.     $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $post_id");
  542.     $to_ping = get_to_ping($post_id);
  543.     $pinged  = get_pung($post_id);
  544.  
  545.     if (empty($post->post_excerpt))
  546.         $excerpt = apply_filters('the_content', $post->post_content);
  547.     else
  548.         $excerpt = apply_filters('the_excerpt', $post->post_excerpt);
  549.     $excerpt = str_replace(']]>', ']]>', $excerpt);
  550.     $excerpt = strip_tags($excerpt);
  551.     $excerpt = substr($excerpt, 0, 252) . '...';
  552.  
  553.     $post_title = apply_filters('the_title', $post->post_title);
  554.     $post_title = strip_tags($post_title);
  555.  
  556.     if ($to_ping) : foreach ($to_ping as $tb_ping) :
  557.         $tb_ping = trim($tb_ping);
  558.         if ( !in_array($tb_ping, $pinged) )
  559.          trackback($tb_ping, $post_title, $excerpt, $post_id);
  560.     endforeach; endif;
  561. }
  562.  
  563. function get_pung($post_id) { // Get URIs already pung for a post
  564.     global $wpdb;
  565.     $pung = $wpdb->get_var("SELECT pinged FROM $wpdb->posts WHERE ID = $post_id");
  566.     $pung = trim($pung);
  567.     $pung = preg_split('/\s/', $pung);
  568.     return $pung;
  569. }
  570.  
  571. function get_enclosed($post_id) { // Get enclosures already enclosed for a post
  572.     global $wpdb;
  573.     $custom_fields = get_post_custom( $post_id );
  574.     $pung = array();
  575.     if( is_array( $custom_fields ) ) {
  576.         while( list( $key, $val ) = each( $custom_fields ) ) { 
  577.             if( $key == 'enclosure' ) {
  578.                 if (is_array($val)) {
  579.                     foreach($val as $enc) {
  580.                         $enclosure = split( "\n", $enc );
  581.                         $pung[] = trim( $enclosure[ 0 ] );
  582.                     }
  583.                 }
  584.             }
  585.         }
  586.     }
  587.     return $pung;
  588. }
  589.  
  590. function get_to_ping($post_id) { // Get any URIs in the todo list
  591.     global $wpdb;
  592.     $to_ping = $wpdb->get_var("SELECT to_ping FROM $wpdb->posts WHERE ID = $post_id");
  593.     $to_ping = trim($to_ping);
  594.     $to_ping = preg_split('/\s/', $to_ping);
  595.     return $to_ping;
  596. }
  597.  
  598. function add_ping($post_id, $uri) { // Add a URI to those already pung
  599.     global $wpdb;
  600.     $pung = $wpdb->get_var("SELECT pinged FROM $wpdb->posts WHERE ID = $post_id");
  601.     $pung = trim($pung);
  602.     $pung = preg_split('/\s/', $pung);
  603.     $pung[] = $uri;
  604.     $new = implode("\n", $pung);
  605.     return $wpdb->query("UPDATE $wpdb->posts SET pinged = '$new' WHERE ID = $post_id");
  606. }
  607.  
  608. ?>
  609.