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_excerpt = apply_filters('excerpt_save_pre', $post_excerpt);
28 $post_title = apply_filters('title_save_pre', $post_title);
29 $post_category = apply_filters('category_save_pre', $post_category);
30 $post_status = apply_filters('status_save_pre', $post_status);
31 $post_name = apply_filters('name_save_pre', $post_name);
32 $comment_status = apply_filters('comment_status_pre', $comment_status);
33 $ping_status = apply_filters('ping_status_pre', $ping_status);
35 // Make sure we set a valid category
36 if (0 == count($post_category) || !is_array($post_category)) {
37 $post_category = array(get_option('default_category'));
39 $post_cat = $post_category[0];
41 if ( empty($post_author) )
42 $post_author = $user_ID;
44 if ( empty($post_status) )
45 $post_status = 'draft';
51 // Create a valid post name. Drafts are allowed to have an empty
53 if ( empty($post_name) ) {
54 if ( 'draft' != $post_status )
55 $post_name = sanitize_title($post_title);
57 $post_name = sanitize_title($post_name);
61 // If the post date is empty (due to having been new or a draft) and status is not 'draft', set date to now
62 if (empty($post_date)) {
63 if ( 'draft' != $post_status )
64 $post_date = current_time('mysql');
67 if (empty($post_date_gmt)) {
68 if ( 'draft' != $post_status )
69 $post_date_gmt = get_gmt_from_date($post_date);
72 if ( empty($comment_status) ) {
74 $comment_status = 'closed';
76 $comment_status = get_settings('default_comment_status');
78 if ( empty($ping_status) )
79 $ping_status = get_settings('default_ping_status');
80 if ( empty($post_pingback) )
81 $post_pingback = get_option('default_pingback_flag');
83 if ( isset($to_ping) )
84 $to_ping = preg_replace('|\s+|', "\n", $to_ping);
88 if ( ! isset($pinged) )
91 if ( isset($post_parent) )
92 $post_parent = (int) $post_parent;
96 if ( isset($menu_order) )
97 $menu_order = (int) $menu_order;
101 if ( !isset($post_password) )
104 if ( ('publish' == $post_status) || ('static' == $post_status) ) {
105 $post_name_check = ('publish' == $post_status)
106 ? $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_status = 'publish' AND ID != '$post_ID' LIMIT 1")
107 : $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");
109 if ($post_name_check) {
111 while ($post_name_check) {
112 $alt_post_name = $post_name . "-$suffix";
113 $post_name_check = ('publish' == $post_status)
114 ? $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")
115 : $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");
118 $post_name = $alt_post_name;
124 "UPDATE IGNORE $wpdb->posts SET
125 post_author = '$post_author',
126 post_date = '$post_date',
127 post_date_gmt = '$post_date_gmt',
128 post_content = '$post_content',
129 post_content_filtered = '$post_content_filtered',
130 post_title = '$post_title',
131 post_excerpt = '$post_excerpt',
132 post_status = '$post_status',
133 comment_status = '$comment_status',
134 ping_status = '$ping_status',
135 post_password = '$post_password',
136 post_name = '$post_name',
137 to_ping = '$to_ping',
139 post_modified = '".current_time('mysql')."',
140 post_modified_gmt = '".current_time('mysql',1)."',
141 post_parent = '$post_parent',
142 menu_order = '$menu_order'
143 WHERE ID = $post_ID");
146 "INSERT IGNORE INTO $wpdb->posts
147 (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)
149 ('$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')");
150 $post_ID = $wpdb->insert_id;
153 if ( empty($post_name) && 'draft' != $post_status ) {
154 $post_name = sanitize_title($post_title, $post_ID);
155 $wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" );
158 wp_set_post_cats('', $post_ID, $post_category);
160 if ( 'static' == $post_status ) {
161 clean_page_cache($post_ID);
162 wp_cache_delete($post_ID, 'pages');
164 clean_post_cache($post_ID);
169 $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'");
172 if ($previous_status != 'publish' && $post_status == 'publish') {
173 // Reset GUID if transitioning to publish.
174 $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'");
175 do_action('private_to_published', $post_ID);
178 do_action('edit_post', $post_ID);
181 if ($post_status == 'publish') {
182 do_action('publish_post', $post_ID);
184 if ( !defined('WP_IMPORTING') ) {
185 if ( $post_pingback )
186 $result = $wpdb->query("
187 INSERT INTO $wpdb->postmeta
188 (post_id,meta_key,meta_value)
189 VALUES ('$post_ID','_pingme','1')
191 $result = $wpdb->query("
192 INSERT INTO $wpdb->postmeta
193 (post_id,meta_key,meta_value)
194 VALUES ('$post_ID','_encloseme','1')
198 } else if ($post_status == 'static') {
199 wp_cache_delete('all_page_ids', 'pages');
200 $wp_rewrite->flush_rules();
202 if ( !empty($page_template) )
203 if ( ! update_post_meta($post_ID, '_wp_page_template', $page_template))
204 add_post_meta($post_ID, '_wp_page_template', $page_template, true);
207 do_action('save_post', $post_ID);
208 do_action('wp_insert_post', $post_ID);
213 function wp_insert_attachment($object, $file = false, $post_parent = 0) {
214 global $wpdb, $user_ID;
216 if ( is_object($object) )
217 $object = get_object_vars($object);
219 // Export array as variables
223 $post_content = apply_filters('content_save_pre', $post_content);
224 $post_excerpt = apply_filters('excerpt_save_pre', $post_excerpt);
225 $post_title = apply_filters('title_save_pre', $post_title);
226 $post_category = apply_filters('category_save_pre', $post_category);
227 $post_name = apply_filters('name_save_pre', $post_name);
228 $comment_status = apply_filters('comment_status_pre', $comment_status);
229 $ping_status = apply_filters('ping_status_pre', $ping_status);
230 $post_mime_type = apply_filters('post_mime_type_pre', $post_mime_type);
232 // Make sure we set a valid category
233 if (0 == count($post_category) || !is_array($post_category)) {
234 $post_category = array(get_option('default_category'));
236 $post_cat = $post_category[0];
238 if ( empty($post_author) )
239 $post_author = $user_ID;
241 $post_status = 'attachment';
243 // Are we updating or creating?
250 // Create a valid post name.
251 if ( empty($post_name) )
252 $post_name = sanitize_title($post_title);
254 $post_name = sanitize_title($post_name);
256 if (empty($post_date))
257 $post_date = current_time('mysql');
258 if (empty($post_date_gmt))
259 $post_date_gmt = current_time('mysql', 1);
261 if ( empty($comment_status) ) {
263 $comment_status = 'closed';
265 $comment_status = get_settings('default_comment_status');
267 if ( empty($ping_status) )
268 $ping_status = get_settings('default_ping_status');
269 if ( empty($post_pingback) )
270 $post_pingback = get_option('default_pingback_flag');
272 if ( isset($to_ping) )
273 $to_ping = preg_replace('|\s+|', "\n", $to_ping);
277 if ( isset($post_parent) )
278 $post_parent = (int) $post_parent;
282 if ( isset($menu_order) )
283 $menu_order = (int) $menu_order;
287 if ( !isset($post_password) )
290 if ( isset($to_ping) )
291 $to_ping = preg_replace('|\s+|', "\n", $to_ping);
295 if ( ! isset($pinged) )
300 "UPDATE $wpdb->posts SET
301 post_author = '$post_author',
302 post_date = '$post_date',
303 post_date_gmt = '$post_date_gmt',
304 post_content = '$post_content',
305 post_title = '$post_title',
306 post_excerpt = '$post_excerpt',
307 post_status = '$post_status',
308 comment_status = '$comment_status',
309 ping_status = '$ping_status',
310 post_password = '$post_password',
311 post_name = '$post_name',
312 to_ping = '$to_ping',
314 post_modified = '".current_time('mysql')."',
315 post_modified_gmt = '".current_time('mysql',1)."',
316 post_parent = '$post_parent',
317 menu_order = '$menu_order',
318 post_mime_type = '$post_mime_type',
320 WHERE ID = $post_ID");
323 "INSERT INTO $wpdb->posts
324 (post_author, post_date, post_date_gmt, post_content, 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)
326 ('$post_author', '$post_date', '$post_date_gmt', '$post_content', '$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')");
327 $post_ID = $wpdb->insert_id;
330 if ( empty($post_name) ) {
331 $post_name = sanitize_title($post_title, $post_ID);
332 $wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" );
335 wp_set_post_cats('', $post_ID, $post_category);
338 add_post_meta($post_ID, '_wp_attached_file', $file);
340 clean_post_cache($post_ID);
343 do_action('edit_attachment', $post_ID);
345 do_action('add_attachment', $post_ID);
351 function wp_delete_attachment($postid) {
353 $postid = (int) $postid;
355 if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $postid") )
358 if ( 'attachment' != $post->post_status )
361 $meta = get_post_meta($postid, '_wp_attachment_metadata', true);
362 $file = get_post_meta($postid, '_wp_attached_file', true);
364 $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = $postid");
366 $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = $postid");
368 $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id = $postid");
370 $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = $postid");
372 if ( ! empty($meta['thumb']) ) {
373 // Don't delete the thumb if another attachment uses it
374 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"))
375 @ unlink(str_replace(basename($file), $meta['thumb'], $file));
378 if ( ! empty($file) )
381 do_action('delete_attachment', $postid);
386 function wp_get_single_post($postid = 0, $mode = OBJECT) {
389 $post = get_post($postid, $mode);
392 if($mode == OBJECT) {
393 $post->post_category = wp_get_post_cats('',$postid);
396 $post['post_category'] = wp_get_post_cats('',$postid);
402 function wp_get_recent_posts($num = 10) {
405 // Set the limit clause, if we got a limit
407 $limit = "LIMIT $num";
410 $sql = "SELECT * FROM $wpdb->posts WHERE post_status IN ('publish', 'draft', 'private') ORDER BY post_date DESC $limit";
411 $result = $wpdb->get_results($sql,ARRAY_A);
413 return $result?$result:array();
416 function wp_update_post($postarr = array()) {
419 if ( is_object($postarr) )
420 $postarr = get_object_vars($postarr);
422 // First, get all of the original fields
423 $post = wp_get_single_post($postarr['ID'], ARRAY_A);
425 // Escape data pulled from DB.
426 $post = add_magic_quotes($post);
428 // Passed post category list overwrites existing category list if not empty.
429 if ( isset($postarr['post_category']) && is_array($postarr['post_category'])
430 && 0 != count($postarr['post_category']) )
431 $post_cats = $postarr['post_category'];
433 $post_cats = $post['post_category'];
435 // Drafts shouldn't be assigned a date unless explicitly done so by the user
436 if ( 'draft' == $post['post_status'] && empty($postarr['edit_date']) && empty($postarr['post_date']) &&
437 ('0000-00-00 00:00:00' == $post['post_date']) )
442 // Merge old and new fields with new fields overwriting old ones.
443 $postarr = array_merge($post, $postarr);
444 $postarr['post_category'] = $post_cats;
446 $postarr['post_date'] = '';
447 $postarr['post_date_gmt'] = '';
450 if ($postarr['post_status'] == 'attachment')
451 return wp_insert_attachment($postarr);
453 return wp_insert_post($postarr);
456 function wp_get_post_cats($blogid = '1', $post_ID = 0) {
459 $sql = "SELECT category_id
461 WHERE post_id = $post_ID
462 ORDER BY category_id";
464 $result = $wpdb->get_col($sql);
469 return array_unique($result);
472 function wp_set_post_cats($blogid = '1', $post_ID = 0, $post_categories = array()) {
474 // If $post_categories isn't already an array, make it one:
475 if (!is_array($post_categories) || 0 == count($post_categories))
476 $post_categories = array(get_option('default_category'));
478 $post_categories = array_unique($post_categories);
480 // First the old categories
481 $old_categories = $wpdb->get_col("
484 WHERE post_id = $post_ID");
486 if (!$old_categories) {
487 $old_categories = array();
489 $old_categories = array_unique($old_categories);
493 $delete_cats = array_diff($old_categories,$post_categories);
496 foreach ($delete_cats as $del) {
498 DELETE FROM $wpdb->post2cat
499 WHERE category_id = $del
500 AND post_id = $post_ID
506 $add_cats = array_diff($post_categories, $old_categories);
509 foreach ($add_cats as $new_cat) {
511 INSERT INTO $wpdb->post2cat (post_id, category_id)
512 VALUES ($post_ID, $new_cat)");
516 // Update category counts.
517 $all_affected_cats = array_unique(array_merge($post_categories, $old_categories));
518 foreach ( $all_affected_cats as $cat_id ) {
519 $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'");
520 $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'");
521 wp_cache_delete($cat_id, 'category');
523 } // wp_set_post_cats()
525 function wp_delete_post($postid = 0) {
526 global $wpdb, $wp_rewrite;
527 $postid = (int) $postid;
529 if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $postid") )
532 if ( 'attachment' == $post->post_status )
533 return wp_delete_attachment($postid);
535 do_action('delete_post', $postid);
537 if ( 'publish' == $post->post_status) {
538 $categories = wp_get_post_cats('', $post->ID);
539 if( is_array( $categories ) ) {
540 foreach ( $categories as $cat_id ) {
541 $wpdb->query("UPDATE $wpdb->categories SET category_count = category_count - 1 WHERE cat_ID = '$cat_id'");
542 wp_cache_delete($cat_id, 'category');
547 if ( 'static' == $post->post_status )
548 $wpdb->query("UPDATE $wpdb->posts SET post_parent = $post->post_parent WHERE post_parent = $postid AND post_status = 'static'");
550 $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = $postid");
552 $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = $postid");
554 $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id = $postid");
556 $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = $postid");
558 if ( 'static' == $post->post_status ) {
559 wp_cache_delete('all_page_ids', 'pages');
560 $wp_rewrite->flush_rules();
566 /**** /DB Functions ****/
570 // get permalink from post ID
571 function post_permalink($post_id = 0, $mode = '') { // $mode legacy
572 return get_permalink($post_id);
575 // Get the name of a category from its ID
576 function get_cat_name($cat_id) {
579 $cat_id -= 0; // force numeric
580 $name = $wpdb->get_var("SELECT cat_name FROM $wpdb->categories WHERE cat_ID=$cat_id");
585 // Get the ID of a category from its name
586 function get_cat_ID($cat_name='General') {
589 $cid = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$cat_name'");
591 return $cid?$cid:1; // default to cat 1
594 // Get author's preferred display name
595 function get_author_name( $auth_id ) {
596 $authordata = get_userdata( $auth_id );
598 return $authordata->display_name;
601 // get extended entry info (<!--more-->)
602 function get_extended($post) {
603 list($main,$extended) = explode('<!--more-->', $post, 2);
605 // Strip leading and trailing whitespace
606 $main = preg_replace('/^[\s]*(.*)[\s]*$/','\\1',$main);
607 $extended = preg_replace('/^[\s]*(.*)[\s]*$/','\\1',$extended);
609 return array('main' => $main, 'extended' => $extended);
612 // do trackbacks for a list of urls
613 // borrowed from edit.php
614 // accepts a comma-separated list of trackback urls and a post id
615 function trackback_url_list($tb_list, $post_id) {
616 if (!empty($tb_list)) {
618 $postdata = wp_get_single_post($post_id, ARRAY_A);
620 // import postdata as variables
624 $excerpt = strip_tags($post_excerpt?$post_excerpt:$post_content);
626 if (strlen($excerpt) > 255) {
627 $excerpt = substr($excerpt,0,252) . '...';
630 $trackback_urls = explode(',', $tb_list);
631 foreach($trackback_urls as $tb_url) {
632 $tb_url = trim($tb_url);
633 trackback($tb_url, stripslashes($post_title), $excerpt, $post_id);
638 function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
641 do_action('wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent);
643 if ( preg_match_all('/&#(\d+);/', $comment . $author . $url, $chars) ) {
644 foreach ($chars[1] as $char) {
645 // If it's an encoded char in the normal ASCII set, reject
651 $mod_keys = trim( get_settings('blacklist_keys') );
652 if ('' == $mod_keys )
653 return false; // If moderation keys are empty
654 $words = explode("\n", $mod_keys );
656 foreach ($words as $word) {
660 if ( empty($word) ) { continue; }
662 // Do some escaping magic so that '#' chars in the
663 // spam words don't break things:
664 $word = preg_quote($word, '#');
666 $pattern = "#$word#i";
667 if ( preg_match($pattern, $author ) ) return true;
668 if ( preg_match($pattern, $email ) ) return true;
669 if ( preg_match($pattern, $url ) ) return true;
670 if ( preg_match($pattern, $comment ) ) return true;
671 if ( preg_match($pattern, $user_ip ) ) return true;
672 if ( preg_match($pattern, $user_agent) ) return true;
675 if ( isset($_SERVER['REMOTE_ADDR']) ) {
676 if ( wp_proxy_check($_SERVER['REMOTE_ADDR']) ) return true;
682 function wp_proxy_check($ipnum) {
683 if ( get_option('open_proxy_check') && isset($ipnum) ) {
684 $rev_ip = implode( '.', array_reverse( explode( '.', $ipnum ) ) );
685 $lookup = $rev_ip . '.opm.blitzed.org.';
686 if ( $lookup != gethostbyname( $lookup ) )
693 function do_trackbacks($post_id) {
696 $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $post_id");
697 $to_ping = get_to_ping($post_id);
698 $pinged = get_pung($post_id);
699 if ( empty($to_ping) ) {
700 $wpdb->query("UPDATE $wpdb->posts SET to_ping = '' WHERE ID = '$post_id'");
704 if (empty($post->post_excerpt))
705 $excerpt = apply_filters('the_content', $post->post_content);
707 $excerpt = apply_filters('the_excerpt', $post->post_excerpt);
708 $excerpt = str_replace(']]>', ']]>', $excerpt);
709 $excerpt = strip_tags($excerpt);
710 if ( function_exists('mb_strcut') ) // For international trackbacks
711 $excerpt = mb_strcut($excerpt, 0, 252, get_settings('blog_charset')) . '...';
713 $excerpt = substr($excerpt, 0, 252) . '...';
715 $post_title = apply_filters('the_title', $post->post_title);
716 $post_title = strip_tags($post_title);
718 if ($to_ping) : foreach ($to_ping as $tb_ping) :
719 $tb_ping = trim($tb_ping);
720 if ( !in_array($tb_ping, $pinged) ) {
721 trackback($tb_ping, $post_title, $excerpt, $post_id);
722 $pinged[] = $tb_ping;
724 $wpdb->query("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_ping', '')) WHERE ID = '$post_id'");
729 function get_pung($post_id) { // Get URIs already pung for a post
731 $pung = $wpdb->get_var("SELECT pinged FROM $wpdb->posts WHERE ID = $post_id");
733 $pung = preg_split('/\s/', $pung);
734 $pung = apply_filters('get_pung', $pung);
738 function get_enclosed($post_id) { // Get enclosures already enclosed for a post
740 $custom_fields = get_post_custom( $post_id );
742 if ( !is_array( $custom_fields ) )
745 foreach ( $custom_fields as $key => $val ) {
746 if ( 'enclosure' != $key || !is_array( $val ) )
748 foreach( $val as $enc ) {
749 $enclosure = split( "\n", $enc );
750 $pung[] = trim( $enclosure[ 0 ] );
753 $pung = apply_filters('get_enclosed', $pung);
757 function get_to_ping($post_id) { // Get any URIs in the todo list
759 $to_ping = $wpdb->get_var("SELECT to_ping FROM $wpdb->posts WHERE ID = $post_id");
760 $to_ping = trim($to_ping);
761 $to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY);
762 $to_ping = apply_filters('get_to_ping', $to_ping);
766 function add_ping($post_id, $uri) { // Add a URI to those already pung
768 $pung = $wpdb->get_var("SELECT pinged FROM $wpdb->posts WHERE ID = $post_id");
770 $pung = preg_split('/\s/', $pung);
772 $new = implode("\n", $pung);
773 $new = apply_filters('add_ping', $new);
774 return $wpdb->query("UPDATE $wpdb->posts SET pinged = '$new' WHERE ID = $post_id");
777 //fetches the pages returned as a FLAT list, but arranged in order of their hierarchy, i.e., child parents
778 //immediately follow their parents
779 function get_page_hierarchy($posts, $parent = 0) {
781 if ($posts) { foreach ($posts as $post) {
782 if ($post->post_parent == $parent) {
783 $result[$post->ID] = $post->post_name;
784 $children = get_page_hierarchy($posts, $post->ID);
785 $result += $children; //append $children to $result
791 function generate_page_rewrite_rules() {
794 //get pages in order of hierarchy, i.e. children after parents
795 $posts = get_page_hierarchy($wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_status = 'static'"));
796 //now reverse it, because we need parents after children for rewrite rules to work properly
797 $posts = array_reverse($posts, true);
799 $page_rewrite_rules = array();
800 $page_attachment_rewrite_rules = array();
804 foreach ($posts as $id => $post) {
807 $uri = get_page_uri($id);
808 $attachments = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_status = 'attachment' AND post_parent = '$id'");
809 if ( $attachments ) {
810 foreach ( $attachments as $attachment ) {
811 $attach_uri = get_page_uri($attachment->ID);
812 $page_attachment_rewrite_rules[$attach_uri] = $attachment->post_name;
816 $page_rewrite_rules[$uri] = $post;
819 update_option('page_uris', $page_rewrite_rules);
821 if ( $page_attachment_rewrite_rules )
822 update_option('page_attachment_uris', $page_attachment_rewrite_rules);
826 function get_post_status($ID = '') {
827 $post = get_post($ID);
829 if ( is_object($post) ) {
830 if ( ('attachment' == $post->post_status) && $post->post_parent && ($post->ID != $post->post_parent) )
831 return get_post_status($post->post_parent);
833 return $post->post_status;
839 // Takes a post ID, returns its mime type.
840 function get_post_mime_type($ID = '') {
841 $post = & get_post($ID);
843 if ( is_object($post) )
844 return $post->post_mime_type;
849 function get_attached_file($attachment_id) {
850 return get_post_meta($attachment_id, '_wp_attached_file', true);
853 function wp_mkdir_p($target) {
854 // from php.net/mkdir user contributed notes
855 if (file_exists($target)) {
856 if (! @ is_dir($target))
862 // Attempting to create the directory may clutter up our display.
863 if (@ mkdir($target)) {
864 $stat = @ stat(dirname($target));
865 $dir_perms = $stat['mode'] & 0007777; // Get the permission bits.
866 @ chmod($target, $dir_perms);
869 if ( is_dir(dirname($target)) )
873 // If the above failed, attempt to create the parent node, then try again.
874 if (wp_mkdir_p(dirname($target)))
875 return wp_mkdir_p($target);
880 // Returns an array containing the current upload directory's path and url, or an error message.
881 function wp_upload_dir() {
882 $siteurl = get_settings('siteurl');
883 //prepend ABSPATH to $dir and $siteurl to $url if they're not already there
884 $path = str_replace(ABSPATH, '', trim(get_settings('upload_path')));
885 $dir = ABSPATH . $path;
886 $url = trailingslashit($siteurl) . $path;
888 if ( $dir == ABSPATH ) { //the option was empty
889 $dir = ABSPATH . 'wp-content/uploads';
892 if ( defined('UPLOADS') ) {
893 $dir = ABSPATH . UPLOADS;
894 $url = trailingslashit($siteurl) . UPLOADS;
897 if ( get_settings('uploads_use_yearmonth_folders')) {
898 // Generate the yearly and monthly dirs
899 $time = current_time( 'mysql' );
900 $y = substr( $time, 0, 4 );
901 $m = substr( $time, 5, 2 );
902 $dir = $dir . "/$y/$m";
903 $url = $url . "/$y/$m";
906 // Make sure we have an uploads dir
907 if ( ! wp_mkdir_p( $dir ) ) {
908 $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), $dir);
909 return array('error' => $message);
912 $uploads = array('path' => $dir, 'url' => $url, 'error' => false);
913 return apply_filters('upload_dir', $uploads);
916 function wp_upload_bits($name, $type, $bits) {
918 return array('error' => "Empty filename");
920 $upload = wp_upload_dir();
922 if ( $upload['error'] !== false )
927 $path_parts = pathinfo($filename);
928 $ext = $path_parts['extension'];
933 while ( file_exists($upload['path'] . "/$filename") ) {
934 if ( '' == "$number$ext" )
935 $filename = $filename . ++$number . $ext;
937 $filename = str_replace("$number$ext", ++$number . $ext, $filename);
940 $new_file = $upload['path'] . "/$filename";
941 if ( ! wp_mkdir_p( dirname($new_file) ) ) {
942 $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), dirname($new_file));
943 return array('error' => $message);
946 $ifp = @ fopen($new_file, 'wb');
948 return array('error' => "Could not write file $new_file.");
950 $success = @ fwrite($ifp, $bits);
952 // Set correct file permissions
953 $stat = @ stat(dirname($new_file));
954 $perms = $stat['mode'] & 0007777;
955 $perms = $perms & 0000666;
956 @ chmod($new_file, $perms);
959 $url = $upload['url'] . "/$filename";
961 return array('file' => $new_file, 'url' => $url, 'error' => false);