3 /**** DB Functions ****/
6 * generic function for inserting data into the posts table.
8 function wp_insert_post($postarr = array()) {
9 global $wpdb, $wp_rewrite, $allowedtags, $user_ID;
11 if ( is_object($postarr) )
12 $postarr = get_object_vars($postarr);
14 // export array as variables
17 // Are we updating or creating?
21 $post = & get_post($ID);
22 $previous_status = $post->post_status;
26 $post_content = apply_filters('content_save_pre', $post_content);
27 $post_content_filtered = apply_filters('content_filtered_save_pre', $post_content_filtered);
28 $post_excerpt = apply_filters('excerpt_save_pre', $post_excerpt);
29 $post_title = apply_filters('title_save_pre', $post_title);
30 $post_category = apply_filters('category_save_pre', $post_category);
31 $post_status = apply_filters('status_save_pre', $post_status);
32 $post_name = apply_filters('name_save_pre', $post_name);
33 $comment_status = apply_filters('comment_status_pre', $comment_status);
34 $ping_status = apply_filters('ping_status_pre', $ping_status);
36 // Make sure we set a valid category
37 if (0 == count($post_category) || !is_array($post_category)) {
38 $post_category = array(get_option('default_category'));
40 $post_cat = $post_category[0];
42 if ( empty($post_author) )
43 $post_author = $user_ID;
45 if ( empty($post_status) )
46 $post_status = 'draft';
52 // Create a valid post name. Drafts are allowed to have an empty
54 if ( empty($post_name) ) {
55 if ( 'draft' != $post_status )
56 $post_name = sanitize_title($post_title);
58 $post_name = sanitize_title($post_name);
62 // If the post date is empty (due to having been new or a draft) and status is not 'draft', set date to now
63 if (empty($post_date)) {
64 if ( 'draft' != $post_status )
65 $post_date = current_time('mysql');
68 if (empty($post_date_gmt)) {
69 if ( 'draft' != $post_status )
70 $post_date_gmt = get_gmt_from_date($post_date);
73 if ( empty($comment_status) ) {
75 $comment_status = 'closed';
77 $comment_status = get_settings('default_comment_status');
79 if ( empty($ping_status) )
80 $ping_status = get_settings('default_ping_status');
81 if ( empty($post_pingback) )
82 $post_pingback = get_option('default_pingback_flag');
84 if ( isset($to_ping) )
85 $to_ping = preg_replace('|\s+|', "\n", $to_ping);
89 if ( ! isset($pinged) )
92 if ( isset($post_parent) )
93 $post_parent = (int) $post_parent;
97 if ( isset($menu_order) )
98 $menu_order = (int) $menu_order;
102 if ( !isset($post_password) )
105 if ( ('publish' == $post_status) || ('static' == $post_status) ) {
106 $post_name_check = ('publish' == $post_status)
107 ? $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_status = 'publish' AND ID != '$post_ID' LIMIT 1")
108 : $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_status = 'static' AND ID != '$post_ID' AND post_parent = '$post_parent' LIMIT 1");
110 if ($post_name_check) {
112 while ($post_name_check) {
113 $alt_post_name = $post_name . "-$suffix";
114 $post_name_check = ('publish' == $post_status)
115 ? $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")
116 : $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_status = 'static' AND ID != '$post_ID' AND post_parent = '$post_parent' LIMIT 1");
119 $post_name = $alt_post_name;
125 "UPDATE IGNORE $wpdb->posts SET
126 post_author = '$post_author',
127 post_date = '$post_date',
128 post_date_gmt = '$post_date_gmt',
129 post_content = '$post_content',
130 post_content_filtered = '$post_content_filtered',
131 post_title = '$post_title',
132 post_excerpt = '$post_excerpt',
133 post_status = '$post_status',
134 comment_status = '$comment_status',
135 ping_status = '$ping_status',
136 post_password = '$post_password',
137 post_name = '$post_name',
138 to_ping = '$to_ping',
140 post_modified = '".current_time('mysql')."',
141 post_modified_gmt = '".current_time('mysql',1)."',
142 post_parent = '$post_parent',
143 menu_order = '$menu_order'
144 WHERE ID = $post_ID");
147 "INSERT IGNORE INTO $wpdb->posts
148 (post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_mime_type)
150 ('$post_author', '$post_date', '$post_date_gmt', '$post_content', '$post_content_filtered', '$post_title', '$post_excerpt', '$post_status', '$comment_status', '$ping_status', '$post_password', '$post_name', '$to_ping', '$pinged', '$post_date', '$post_date_gmt', '$post_parent', '$menu_order', '$post_mime_type')");
151 $post_ID = $wpdb->insert_id;
154 if ( empty($post_name) && 'draft' != $post_status ) {
155 $post_name = sanitize_title($post_title, $post_ID);
156 $wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" );
159 wp_set_post_cats('', $post_ID, $post_category);
161 if ( 'static' == $post_status ) {
162 clean_page_cache($post_ID);
163 wp_cache_delete($post_ID, 'pages');
165 clean_post_cache($post_ID);
170 $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'");
173 if ($previous_status != 'publish' && $post_status == 'publish') {
174 // Reset GUID if transitioning to publish.
175 $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'");
176 do_action('private_to_published', $post_ID);
179 do_action('edit_post', $post_ID);
182 if ($post_status == 'publish') {
183 do_action('publish_post', $post_ID);
185 if ( !defined('WP_IMPORTING') ) {
186 if ( $post_pingback )
187 $result = $wpdb->query("
188 INSERT INTO $wpdb->postmeta
189 (post_id,meta_key,meta_value)
190 VALUES ('$post_ID','_pingme','1')
192 $result = $wpdb->query("
193 INSERT INTO $wpdb->postmeta
194 (post_id,meta_key,meta_value)
195 VALUES ('$post_ID','_encloseme','1')
199 } else if ($post_status == 'static') {
200 wp_cache_delete('all_page_ids', 'pages');
201 $wp_rewrite->flush_rules();
203 if ( !empty($page_template) )
204 if ( ! update_post_meta($post_ID, '_wp_page_template', $page_template))
205 add_post_meta($post_ID, '_wp_page_template', $page_template, true);
208 do_action('save_post', $post_ID);
209 do_action('wp_insert_post', $post_ID);
214 function wp_insert_attachment($object, $file = false, $post_parent = 0) {
215 global $wpdb, $user_ID;
217 if ( is_object($object) )
218 $object = get_object_vars($object);
220 // Export array as variables
224 $post_content = apply_filters('content_save_pre', $post_content);
225 $post_content_filtered = apply_filters('content_filtered_save_pre', $post_content_filtered);
226 $post_excerpt = apply_filters('excerpt_save_pre', $post_excerpt);
227 $post_title = apply_filters('title_save_pre', $post_title);
228 $post_category = apply_filters('category_save_pre', $post_category);
229 $post_name = apply_filters('name_save_pre', $post_name);
230 $comment_status = apply_filters('comment_status_pre', $comment_status);
231 $ping_status = apply_filters('ping_status_pre', $ping_status);
232 $post_mime_type = apply_filters('post_mime_type_pre', $post_mime_type);
234 // Make sure we set a valid category
235 if (0 == count($post_category) || !is_array($post_category)) {
236 $post_category = array(get_option('default_category'));
238 $post_cat = $post_category[0];
240 if ( empty($post_author) )
241 $post_author = $user_ID;
243 $post_status = 'attachment';
245 // Are we updating or creating?
252 // Create a valid post name.
253 if ( empty($post_name) )
254 $post_name = sanitize_title($post_title);
256 $post_name = sanitize_title($post_name);
258 if (empty($post_date))
259 $post_date = current_time('mysql');
260 if (empty($post_date_gmt))
261 $post_date_gmt = current_time('mysql', 1);
263 if ( empty($comment_status) ) {
265 $comment_status = 'closed';
267 $comment_status = get_settings('default_comment_status');
269 if ( empty($ping_status) )
270 $ping_status = get_settings('default_ping_status');
271 if ( empty($post_pingback) )
272 $post_pingback = get_option('default_pingback_flag');
274 if ( isset($to_ping) )
275 $to_ping = preg_replace('|\s+|', "\n", $to_ping);
279 if ( isset($post_parent) )
280 $post_parent = (int) $post_parent;
284 if ( isset($menu_order) )
285 $menu_order = (int) $menu_order;
289 if ( !isset($post_password) )
292 if ( isset($to_ping) )
293 $to_ping = preg_replace('|\s+|', "\n", $to_ping);
297 if ( ! isset($pinged) )
302 "UPDATE $wpdb->posts SET
303 post_author = '$post_author',
304 post_date = '$post_date',
305 post_date_gmt = '$post_date_gmt',
306 post_content = '$post_content',
307 post_content_filtered = '$post_content_filtered',
308 post_title = '$post_title',
309 post_excerpt = '$post_excerpt',
310 post_status = '$post_status',
311 comment_status = '$comment_status',
312 ping_status = '$ping_status',
313 post_password = '$post_password',
314 post_name = '$post_name',
315 to_ping = '$to_ping',
317 post_modified = '".current_time('mysql')."',
318 post_modified_gmt = '".current_time('mysql',1)."',
319 post_parent = '$post_parent',
320 menu_order = '$menu_order',
321 post_mime_type = '$post_mime_type',
323 WHERE ID = $post_ID");
326 "INSERT INTO $wpdb->posts
327 (post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_mime_type, guid)
329 ('$post_author', '$post_date', '$post_date_gmt', '$post_content', '$post_content_filtered', '$post_title', '$post_excerpt', '$post_status', '$comment_status', '$ping_status', '$post_password', '$post_name', '$to_ping', '$pinged', '$post_date', '$post_date_gmt', '$post_parent', '$menu_order', '$post_mime_type', '$guid')");
330 $post_ID = $wpdb->insert_id;
333 if ( empty($post_name) ) {
334 $post_name = sanitize_title($post_title, $post_ID);
335 $wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" );
338 wp_set_post_cats('', $post_ID, $post_category);
341 add_post_meta($post_ID, '_wp_attached_file', $file);
343 clean_post_cache($post_ID);
346 do_action('edit_attachment', $post_ID);
348 do_action('add_attachment', $post_ID);
354 function wp_delete_attachment($postid) {
356 $postid = (int) $postid;
358 if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = '$postid'") )
361 if ( 'attachment' != $post->post_status )
364 $meta = get_post_meta($postid, '_wp_attachment_metadata', true);
365 $file = get_post_meta($postid, '_wp_attached_file', true);
367 $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = '$postid'");
369 $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = '$postid'");
371 $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id = '$postid'");
373 $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$postid'");
375 if ( ! empty($meta['thumb']) ) {
376 // Don't delete the thumb if another attachment uses it
377 if (! $foo = $wpdb->get_row("SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE '%".$wpdb->escape($meta['thumb'])."%' AND post_id <> '$postid'"))
378 @ unlink(str_replace(basename($file), $meta['thumb'], $file));
381 if ( ! empty($file) )
384 do_action('delete_attachment', $postid);
389 function wp_get_single_post($postid = 0, $mode = OBJECT) {
392 $post = get_post($postid, $mode);
395 if($mode == OBJECT) {
396 $post->post_category = wp_get_post_cats('',$postid);
399 $post['post_category'] = wp_get_post_cats('',$postid);
405 function wp_get_recent_posts($num = 10) {
408 // Set the limit clause, if we got a limit
411 $limit = "LIMIT $num";
414 $sql = "SELECT * FROM $wpdb->posts WHERE post_status IN ('publish', 'draft', 'private') ORDER BY post_date DESC $limit";
415 $result = $wpdb->get_results($sql,ARRAY_A);
417 return $result?$result:array();
420 function wp_update_post($postarr = array()) {
423 if ( is_object($postarr) )
424 $postarr = get_object_vars($postarr);
426 // First, get all of the original fields
427 $post = wp_get_single_post($postarr['ID'], ARRAY_A);
429 // Escape data pulled from DB.
430 $post = add_magic_quotes($post);
432 // Passed post category list overwrites existing category list if not empty.
433 if ( isset($postarr['post_category']) && is_array($postarr['post_category'])
434 && 0 != count($postarr['post_category']) )
435 $post_cats = $postarr['post_category'];
437 $post_cats = $post['post_category'];
439 // Drafts shouldn't be assigned a date unless explicitly done so by the user
440 if ( 'draft' == $post['post_status'] && empty($postarr['edit_date']) && empty($postarr['post_date']) &&
441 ('0000-00-00 00:00:00' == $post['post_date']) )
446 // Merge old and new fields with new fields overwriting old ones.
447 $postarr = array_merge($post, $postarr);
448 $postarr['post_category'] = $post_cats;
450 $postarr['post_date'] = '';
451 $postarr['post_date_gmt'] = '';
454 if ($postarr['post_status'] == 'attachment')
455 return wp_insert_attachment($postarr);
457 return wp_insert_post($postarr);
460 function wp_get_post_cats($blogid = '1', $post_ID = 0) {
463 $post_ID = (int) $post_ID;
465 $sql = "SELECT category_id
467 WHERE post_id = '$post_ID'
468 ORDER BY category_id";
470 $result = $wpdb->get_col($sql);
475 return array_unique($result);
478 function wp_set_post_cats($blogid = '1', $post_ID = 0, $post_categories = array()) {
481 $post_ID = (int) $post_ID;
483 // If $post_categories isn't already an array, make it one:
484 if (!is_array($post_categories) || 0 == count($post_categories))
485 $post_categories = array(get_option('default_category'));
487 $post_categories = array_unique($post_categories);
489 // First the old categories
490 $old_categories = $wpdb->get_col("
493 WHERE post_id = '$post_ID'");
495 if (!$old_categories) {
496 $old_categories = array();
498 $old_categories = array_unique($old_categories);
502 $delete_cats = array_diff($old_categories,$post_categories);
505 foreach ($delete_cats as $del) {
507 DELETE FROM $wpdb->post2cat
508 WHERE category_id = '$del'
509 AND post_id = '$post_ID'
515 $add_cats = array_diff($post_categories, $old_categories);
518 foreach ($add_cats as $new_cat) {
519 $new_cat = (int) $new_cat;
520 if ( !empty($new_cat) )
522 INSERT INTO $wpdb->post2cat (post_id, category_id)
523 VALUES ('$post_ID', '$new_cat')");
527 // Update category counts.
528 $all_affected_cats = array_unique(array_merge($post_categories, $old_categories));
529 foreach ( $all_affected_cats as $cat_id ) {
530 $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->post2cat, $wpdb->posts WHERE $wpdb->posts.ID=$wpdb->post2cat.post_id AND post_status='publish' AND category_id = '$cat_id'");
531 $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'");
532 wp_cache_delete($cat_id, 'category');
534 } // wp_set_post_cats()
536 function wp_delete_post($postid = 0) {
537 global $wpdb, $wp_rewrite;
538 $postid = (int) $postid;
540 if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $postid") )
543 if ( 'attachment' == $post->post_status )
544 return wp_delete_attachment($postid);
546 do_action('delete_post', $postid);
548 if ( 'publish' == $post->post_status) {
549 $categories = wp_get_post_cats('', $post->ID);
550 if( is_array( $categories ) ) {
551 foreach ( $categories as $cat_id ) {
552 $wpdb->query("UPDATE $wpdb->categories SET category_count = category_count - 1 WHERE cat_ID = '$cat_id'");
553 wp_cache_delete($cat_id, 'category');
558 if ( 'static' == $post->post_status )
559 $wpdb->query("UPDATE $wpdb->posts SET post_parent = $post->post_parent WHERE post_parent = $postid AND post_status = 'static'");
561 $wpdb->query("UPDATE $wpdb->posts SET post_parent = $post->post_parent WHERE post_parent = $postid AND post_status = 'attachment'");
563 $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = $postid");
565 $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = $postid");
567 $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id = $postid");
569 $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = $postid");
571 if ( 'static' == $post->post_status ) {
572 wp_cache_delete('all_page_ids', 'pages');
573 $wp_rewrite->flush_rules();
579 /**** /DB Functions ****/
583 // get permalink from post ID
584 function post_permalink($post_id = 0, $mode = '') { // $mode legacy
585 return get_permalink($post_id);
588 // Get the name of a category from its ID
589 function get_cat_name($cat_id) {
592 $cat_id -= 0; // force numeric
593 $name = $wpdb->get_var("SELECT cat_name FROM $wpdb->categories WHERE cat_ID=$cat_id");
598 // Get the ID of a category from its name
599 function get_cat_ID($cat_name='General') {
602 $cid = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$cat_name'");
604 return $cid?$cid:1; // default to cat 1
607 // Get author's preferred display name
608 function get_author_name( $auth_id ) {
609 $authordata = get_userdata( $auth_id );
611 return $authordata->display_name;
614 // get extended entry info (<!--more-->)
615 function get_extended($post) {
616 list($main,$extended) = explode('<!--more-->', $post, 2);
618 // Strip leading and trailing whitespace
619 $main = preg_replace('/^[\s]*(.*)[\s]*$/','\\1',$main);
620 $extended = preg_replace('/^[\s]*(.*)[\s]*$/','\\1',$extended);
622 return array('main' => $main, 'extended' => $extended);
625 // do trackbacks for a list of urls
626 // borrowed from edit.php
627 // accepts a comma-separated list of trackback urls and a post id
628 function trackback_url_list($tb_list, $post_id) {
629 if (!empty($tb_list)) {
631 $postdata = wp_get_single_post($post_id, ARRAY_A);
633 // import postdata as variables
637 $excerpt = strip_tags($post_excerpt?$post_excerpt:$post_content);
639 if (strlen($excerpt) > 255) {
640 $excerpt = substr($excerpt,0,252) . '...';
643 $trackback_urls = explode(',', $tb_list);
644 foreach($trackback_urls as $tb_url) {
645 $tb_url = trim($tb_url);
646 trackback($tb_url, stripslashes($post_title), $excerpt, $post_id);
651 function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
654 do_action('wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent);
656 if ( preg_match_all('/&#(\d+);/', $comment . $author . $url, $chars) ) {
657 foreach ($chars[1] as $char) {
658 // If it's an encoded char in the normal ASCII set, reject
664 $mod_keys = trim( get_settings('blacklist_keys') );
665 if ('' == $mod_keys )
666 return false; // If moderation keys are empty
667 $words = explode("\n", $mod_keys );
669 foreach ($words as $word) {
673 if ( empty($word) ) { continue; }
675 // Do some escaping magic so that '#' chars in the
676 // spam words don't break things:
677 $word = preg_quote($word, '#');
679 $pattern = "#$word#i";
680 if ( preg_match($pattern, $author ) ) return true;
681 if ( preg_match($pattern, $email ) ) return true;
682 if ( preg_match($pattern, $url ) ) return true;
683 if ( preg_match($pattern, $comment ) ) return true;
684 if ( preg_match($pattern, $user_ip ) ) return true;
685 if ( preg_match($pattern, $user_agent) ) return true;
688 if ( isset($_SERVER['REMOTE_ADDR']) ) {
689 if ( wp_proxy_check($_SERVER['REMOTE_ADDR']) ) return true;
695 function wp_proxy_check($ipnum) {
696 if ( get_option('open_proxy_check') && isset($ipnum) ) {
697 $ipnum = preg_replace( '/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*/', '$1', $ipnum );
698 $rev_ip = implode( '.', array_reverse( explode( '.', $ipnum ) ) );
699 $lookup = $rev_ip . '.sbl-xbl.spamhaus.org.';
700 if ( $lookup != gethostbyname( $lookup ) )
707 function do_trackbacks($post_id) {
710 $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $post_id");
711 $to_ping = get_to_ping($post_id);
712 $pinged = get_pung($post_id);
713 if ( empty($to_ping) ) {
714 $wpdb->query("UPDATE $wpdb->posts SET to_ping = '' WHERE ID = '$post_id'");
718 if (empty($post->post_excerpt))
719 $excerpt = apply_filters('the_content', $post->post_content);
721 $excerpt = apply_filters('the_excerpt', $post->post_excerpt);
722 $excerpt = str_replace(']]>', ']]>', $excerpt);
723 $excerpt = strip_tags($excerpt);
724 if ( function_exists('mb_strcut') ) // For international trackbacks
725 $excerpt = mb_strcut($excerpt, 0, 252, get_settings('blog_charset')) . '...';
727 $excerpt = substr($excerpt, 0, 252) . '...';
729 $post_title = apply_filters('the_title', $post->post_title);
730 $post_title = strip_tags($post_title);
732 if ($to_ping) : foreach ($to_ping as $tb_ping) :
733 $tb_ping = trim($tb_ping);
734 if ( !in_array($tb_ping, $pinged) ) {
735 trackback($tb_ping, $post_title, $excerpt, $post_id);
736 $pinged[] = $tb_ping;
738 $wpdb->query("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_ping', '')) WHERE ID = '$post_id'");
743 function get_pung($post_id) { // Get URIs already pung for a post
745 $pung = $wpdb->get_var("SELECT pinged FROM $wpdb->posts WHERE ID = $post_id");
747 $pung = preg_split('/\s/', $pung);
748 $pung = apply_filters('get_pung', $pung);
752 function get_enclosed($post_id) { // Get enclosures already enclosed for a post
754 $custom_fields = get_post_custom( $post_id );
756 if ( !is_array( $custom_fields ) )
759 foreach ( $custom_fields as $key => $val ) {
760 if ( 'enclosure' != $key || !is_array( $val ) )
762 foreach( $val as $enc ) {
763 $enclosure = split( "\n", $enc );
764 $pung[] = trim( $enclosure[ 0 ] );
767 $pung = apply_filters('get_enclosed', $pung);
771 function get_to_ping($post_id) { // Get any URIs in the todo list
773 $to_ping = $wpdb->get_var("SELECT to_ping FROM $wpdb->posts WHERE ID = $post_id");
774 $to_ping = trim($to_ping);
775 $to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY);
776 $to_ping = apply_filters('get_to_ping', $to_ping);
780 function add_ping($post_id, $uri) { // Add a URI to those already pung
782 $pung = $wpdb->get_var("SELECT pinged FROM $wpdb->posts WHERE ID = $post_id");
784 $pung = preg_split('/\s/', $pung);
786 $new = implode("\n", $pung);
787 $new = apply_filters('add_ping', $new);
788 return $wpdb->query("UPDATE $wpdb->posts SET pinged = '$new' WHERE ID = $post_id");
791 //fetches the pages returned as a FLAT list, but arranged in order of their hierarchy, i.e., child parents
792 //immediately follow their parents
793 function get_page_hierarchy($posts, $parent = 0) {
795 if ($posts) { foreach ($posts as $post) {
796 if ($post->post_parent == $parent) {
797 $result[$post->ID] = $post->post_name;
798 $children = get_page_hierarchy($posts, $post->ID);
799 $result += $children; //append $children to $result
805 function generate_page_rewrite_rules() {
808 //get pages in order of hierarchy, i.e. children after parents
809 $posts = get_page_hierarchy($wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_status = 'static'"));
810 //now reverse it, because we need parents after children for rewrite rules to work properly
811 $posts = array_reverse($posts, true);
813 $page_rewrite_rules = array();
814 $page_attachment_rewrite_rules = array();
818 foreach ($posts as $id => $post) {
821 $uri = get_page_uri($id);
822 $attachments = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_status = 'attachment' AND post_parent = '$id'");
823 if ( $attachments ) {
824 foreach ( $attachments as $attachment ) {
825 $attach_uri = get_page_uri($attachment->ID);
826 $page_attachment_rewrite_rules[$attach_uri] = $attachment->post_name;
830 $page_rewrite_rules[$uri] = $post;
833 update_option('page_uris', $page_rewrite_rules);
835 if ( $page_attachment_rewrite_rules )
836 update_option('page_attachment_uris', $page_attachment_rewrite_rules);
840 function get_post_status($ID = '') {
841 $post = get_post($ID);
843 if ( is_object($post) ) {
844 if ( ('attachment' == $post->post_status) && $post->post_parent && ($post->ID != $post->post_parent) )
845 return get_post_status($post->post_parent);
847 return $post->post_status;
853 // Takes a post ID, returns its mime type.
854 function get_post_mime_type($ID = '') {
855 $post = & get_post($ID);
857 if ( is_object($post) )
858 return $post->post_mime_type;
863 function get_attached_file($attachment_id) {
864 return get_post_meta($attachment_id, '_wp_attached_file', true);
867 function wp_mkdir_p($target) {
868 // from php.net/mkdir user contributed notes
869 if (file_exists($target)) {
870 if (! @ is_dir($target))
876 // Attempting to create the directory may clutter up our display.
877 if (@ mkdir($target)) {
878 $stat = @ stat(dirname($target));
879 $dir_perms = $stat['mode'] & 0007777; // Get the permission bits.
880 @ chmod($target, $dir_perms);
883 if ( is_dir(dirname($target)) )
887 // If the above failed, attempt to create the parent node, then try again.
888 if (wp_mkdir_p(dirname($target)))
889 return wp_mkdir_p($target);
894 // Returns an array containing the current upload directory's path and url, or an error message.
895 function wp_upload_dir() {
896 $siteurl = get_settings('siteurl');
897 //prepend ABSPATH to $dir and $siteurl to $url if they're not already there
898 $path = str_replace(ABSPATH, '', trim(get_settings('upload_path')));
899 $dir = ABSPATH . $path;
900 $url = trailingslashit($siteurl) . $path;
902 if ( $dir == ABSPATH ) { //the option was empty
903 $dir = ABSPATH . 'wp-content/uploads';
906 if ( defined('UPLOADS') ) {
907 $dir = ABSPATH . UPLOADS;
908 $url = trailingslashit($siteurl) . UPLOADS;
911 if ( get_settings('uploads_use_yearmonth_folders')) {
912 // Generate the yearly and monthly dirs
913 $time = current_time( 'mysql' );
914 $y = substr( $time, 0, 4 );
915 $m = substr( $time, 5, 2 );
916 $dir = $dir . "/$y/$m";
917 $url = $url . "/$y/$m";
920 // Make sure we have an uploads dir
921 if ( ! wp_mkdir_p( $dir ) ) {
922 $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), $dir);
923 return array('error' => $message);
926 $uploads = array('path' => $dir, 'url' => $url, 'error' => false);
927 return apply_filters('upload_dir', $uploads);
930 function wp_upload_bits($name, $type, $bits) {
932 return array('error' => __("Empty filename"));
934 $wp_filetype = wp_check_filetype($name);
935 if ( !$wp_filetype['ext'] )
936 return array('error' => __("Invalid file type"));
938 $upload = wp_upload_dir();
940 if ( $upload['error'] !== false )
945 $path_parts = pathinfo($filename);
946 $ext = $path_parts['extension'];
951 while ( file_exists($upload['path'] . "/$filename") ) {
952 if ( '' == "$number$ext" )
953 $filename = $filename . ++$number . $ext;
955 $filename = str_replace("$number$ext", ++$number . $ext, $filename);
958 $new_file = $upload['path'] . "/$filename";
959 if ( ! wp_mkdir_p( dirname($new_file) ) ) {
960 $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), dirname($new_file));
961 return array('error' => $message);
964 $ifp = @ fopen($new_file, 'wb');
966 return array('error' => "Could not write file $new_file.");
968 $success = @ fwrite($ifp, $bits);
970 // Set correct file permissions
971 $stat = @ stat(dirname($new_file));
972 $perms = $stat['mode'] & 0007777;
973 $perms = $perms & 0000666;
974 @ chmod($new_file, $perms);
977 $url = $upload['url'] . "/$filename";
979 return array('file' => $new_file, 'url' => $url, 'error' => false);
982 function wp_check_filetype($filename, $mimes = null) {
983 // Accepted MIME types are set here as PCRE unless provided.
984 $mimes = is_array($mimes) ? $mimes : apply_filters('upload_mimes', array (
985 'jpg|jpeg|jpe' => 'image/jpeg',
986 'gif' => 'image/gif',
987 'png' => 'image/png',
988 'bmp' => 'image/bmp',
989 'tif|tiff' => 'image/tiff',
990 'ico' => 'image/x-icon',
991 'asf|asx|wax|wmv|wmx' => 'video/asf',
992 'avi' => 'video/avi',
993 'mov|qt' => 'video/quicktime',
994 'mpeg|mpg|mpe' => 'video/mpeg',
995 'txt|c|cc|h' => 'text/plain',
996 'rtx' => 'text/richtext',
998 'htm|html' => 'text/html',
999 'mp3|mp4' => 'audio/mpeg',
1000 'ra|ram' => 'audio/x-realaudio',
1001 'wav' => 'audio/wav',
1002 'ogg' => 'audio/ogg',
1003 'mid|midi' => 'audio/midi',
1004 'wma' => 'audio/wma',
1005 'rtf' => 'application/rtf',
1006 'js' => 'application/javascript',
1007 'pdf' => 'application/pdf',
1008 'doc' => 'application/msword',
1009 'pot|pps|ppt' => 'application/vnd.ms-powerpoint',
1010 'wri' => 'application/vnd.ms-write',
1011 'xla|xls|xlt|xlw' => 'application/vnd.ms-excel',
1012 'mdb' => 'application/vnd.ms-access',
1013 'mpp' => 'application/vnd.ms-project',
1014 'swf' => 'application/x-shockwave-flash',
1015 'class' => 'application/java',
1016 'tar' => 'application/x-tar',
1017 'zip' => 'application/zip',
1018 'gz|gzip' => 'application/x-gzip',
1019 'exe' => 'application/x-msdownload'
1025 foreach ($mimes as $ext_preg => $mime_match) {
1026 $ext_preg = '!\.(' . $ext_preg . ')$!i';
1027 if ( preg_match($ext_preg, $filename, $ext_matches) ) {
1028 $type = $mime_match;
1029 $ext = $ext_matches[1];
1034 return compact('ext', 'type');