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 $post_ID = (int) $post_ID;
461 $sql = "SELECT category_id
463 WHERE post_id = '$post_ID'
464 ORDER BY category_id";
466 $result = $wpdb->get_col($sql);
471 return array_unique($result);
474 function wp_set_post_cats($blogid = '1', $post_ID = 0, $post_categories = array()) {
476 // If $post_categories isn't already an array, make it one:
477 if (!is_array($post_categories) || 0 == count($post_categories))
478 $post_categories = array(get_option('default_category'));
480 $post_categories = array_unique($post_categories);
482 // First the old categories
483 $old_categories = $wpdb->get_col("
486 WHERE post_id = $post_ID");
488 if (!$old_categories) {
489 $old_categories = array();
491 $old_categories = array_unique($old_categories);
495 $delete_cats = array_diff($old_categories,$post_categories);
498 foreach ($delete_cats as $del) {
500 DELETE FROM $wpdb->post2cat
501 WHERE category_id = $del
502 AND post_id = $post_ID
508 $add_cats = array_diff($post_categories, $old_categories);
511 foreach ($add_cats as $new_cat) {
513 INSERT INTO $wpdb->post2cat (post_id, category_id)
514 VALUES ($post_ID, $new_cat)");
518 // Update category counts.
519 $all_affected_cats = array_unique(array_merge($post_categories, $old_categories));
520 foreach ( $all_affected_cats as $cat_id ) {
521 $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'");
522 $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'");
523 wp_cache_delete($cat_id, 'category');
525 } // wp_set_post_cats()
527 function wp_delete_post($postid = 0) {
528 global $wpdb, $wp_rewrite;
529 $postid = (int) $postid;
531 if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $postid") )
534 if ( 'attachment' == $post->post_status )
535 return wp_delete_attachment($postid);
537 do_action('delete_post', $postid);
539 if ( 'publish' == $post->post_status) {
540 $categories = wp_get_post_cats('', $post->ID);
541 if( is_array( $categories ) ) {
542 foreach ( $categories as $cat_id ) {
543 $wpdb->query("UPDATE $wpdb->categories SET category_count = category_count - 1 WHERE cat_ID = '$cat_id'");
544 wp_cache_delete($cat_id, 'category');
549 if ( 'static' == $post->post_status )
550 $wpdb->query("UPDATE $wpdb->posts SET post_parent = $post->post_parent WHERE post_parent = $postid AND post_status = 'static'");
552 $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = $postid");
554 $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = $postid");
556 $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id = $postid");
558 $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = $postid");
560 if ( 'static' == $post->post_status ) {
561 wp_cache_delete('all_page_ids', 'pages');
562 $wp_rewrite->flush_rules();
568 /**** /DB Functions ****/
572 // get permalink from post ID
573 function post_permalink($post_id = 0, $mode = '') { // $mode legacy
574 return get_permalink($post_id);
577 // Get the name of a category from its ID
578 function get_cat_name($cat_id) {
581 $cat_id -= 0; // force numeric
582 $name = $wpdb->get_var("SELECT cat_name FROM $wpdb->categories WHERE cat_ID=$cat_id");
587 // Get the ID of a category from its name
588 function get_cat_ID($cat_name='General') {
591 $cid = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$cat_name'");
593 return $cid?$cid:1; // default to cat 1
596 // Get author's preferred display name
597 function get_author_name( $auth_id ) {
598 $authordata = get_userdata( $auth_id );
600 return $authordata->display_name;
603 // get extended entry info (<!--more-->)
604 function get_extended($post) {
605 list($main,$extended) = explode('<!--more-->', $post, 2);
607 // Strip leading and trailing whitespace
608 $main = preg_replace('/^[\s]*(.*)[\s]*$/','\\1',$main);
609 $extended = preg_replace('/^[\s]*(.*)[\s]*$/','\\1',$extended);
611 return array('main' => $main, 'extended' => $extended);
614 // do trackbacks for a list of urls
615 // borrowed from edit.php
616 // accepts a comma-separated list of trackback urls and a post id
617 function trackback_url_list($tb_list, $post_id) {
618 if (!empty($tb_list)) {
620 $postdata = wp_get_single_post($post_id, ARRAY_A);
622 // import postdata as variables
626 $excerpt = strip_tags($post_excerpt?$post_excerpt:$post_content);
628 if (strlen($excerpt) > 255) {
629 $excerpt = substr($excerpt,0,252) . '...';
632 $trackback_urls = explode(',', $tb_list);
633 foreach($trackback_urls as $tb_url) {
634 $tb_url = trim($tb_url);
635 trackback($tb_url, stripslashes($post_title), $excerpt, $post_id);
640 function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
643 do_action('wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent);
645 if ( preg_match_all('/&#(\d+);/', $comment . $author . $url, $chars) ) {
646 foreach ($chars[1] as $char) {
647 // If it's an encoded char in the normal ASCII set, reject
653 $mod_keys = trim( get_settings('blacklist_keys') );
654 if ('' == $mod_keys )
655 return false; // If moderation keys are empty
656 $words = explode("\n", $mod_keys );
658 foreach ($words as $word) {
662 if ( empty($word) ) { continue; }
664 // Do some escaping magic so that '#' chars in the
665 // spam words don't break things:
666 $word = preg_quote($word, '#');
668 $pattern = "#$word#i";
669 if ( preg_match($pattern, $author ) ) return true;
670 if ( preg_match($pattern, $email ) ) return true;
671 if ( preg_match($pattern, $url ) ) return true;
672 if ( preg_match($pattern, $comment ) ) return true;
673 if ( preg_match($pattern, $user_ip ) ) return true;
674 if ( preg_match($pattern, $user_agent) ) return true;
677 if ( isset($_SERVER['REMOTE_ADDR']) ) {
678 if ( wp_proxy_check($_SERVER['REMOTE_ADDR']) ) return true;
684 function wp_proxy_check($ipnum) {
685 if ( get_option('open_proxy_check') && isset($ipnum) ) {
686 $rev_ip = implode( '.', array_reverse( explode( '.', $ipnum ) ) );
687 $lookup = $rev_ip . '.sbl-xbl.spamhaus.org.';
688 if ( $lookup != gethostbyname( $lookup ) )
695 function do_trackbacks($post_id) {
698 $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $post_id");
699 $to_ping = get_to_ping($post_id);
700 $pinged = get_pung($post_id);
701 if ( empty($to_ping) ) {
702 $wpdb->query("UPDATE $wpdb->posts SET to_ping = '' WHERE ID = '$post_id'");
706 if (empty($post->post_excerpt))
707 $excerpt = apply_filters('the_content', $post->post_content);
709 $excerpt = apply_filters('the_excerpt', $post->post_excerpt);
710 $excerpt = str_replace(']]>', ']]>', $excerpt);
711 $excerpt = strip_tags($excerpt);
712 if ( function_exists('mb_strcut') ) // For international trackbacks
713 $excerpt = mb_strcut($excerpt, 0, 252, get_settings('blog_charset')) . '...';
715 $excerpt = substr($excerpt, 0, 252) . '...';
717 $post_title = apply_filters('the_title', $post->post_title);
718 $post_title = strip_tags($post_title);
720 if ($to_ping) : foreach ($to_ping as $tb_ping) :
721 $tb_ping = trim($tb_ping);
722 if ( !in_array($tb_ping, $pinged) ) {
723 trackback($tb_ping, $post_title, $excerpt, $post_id);
724 $pinged[] = $tb_ping;
726 $wpdb->query("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_ping', '')) WHERE ID = '$post_id'");
731 function get_pung($post_id) { // Get URIs already pung for a post
733 $pung = $wpdb->get_var("SELECT pinged FROM $wpdb->posts WHERE ID = $post_id");
735 $pung = preg_split('/\s/', $pung);
736 $pung = apply_filters('get_pung', $pung);
740 function get_enclosed($post_id) { // Get enclosures already enclosed for a post
742 $custom_fields = get_post_custom( $post_id );
744 if ( !is_array( $custom_fields ) )
747 foreach ( $custom_fields as $key => $val ) {
748 if ( 'enclosure' != $key || !is_array( $val ) )
750 foreach( $val as $enc ) {
751 $enclosure = split( "\n", $enc );
752 $pung[] = trim( $enclosure[ 0 ] );
755 $pung = apply_filters('get_enclosed', $pung);
759 function get_to_ping($post_id) { // Get any URIs in the todo list
761 $to_ping = $wpdb->get_var("SELECT to_ping FROM $wpdb->posts WHERE ID = $post_id");
762 $to_ping = trim($to_ping);
763 $to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY);
764 $to_ping = apply_filters('get_to_ping', $to_ping);
768 function add_ping($post_id, $uri) { // Add a URI to those already pung
770 $pung = $wpdb->get_var("SELECT pinged FROM $wpdb->posts WHERE ID = $post_id");
772 $pung = preg_split('/\s/', $pung);
774 $new = implode("\n", $pung);
775 $new = apply_filters('add_ping', $new);
776 return $wpdb->query("UPDATE $wpdb->posts SET pinged = '$new' WHERE ID = $post_id");
779 //fetches the pages returned as a FLAT list, but arranged in order of their hierarchy, i.e., child parents
780 //immediately follow their parents
781 function get_page_hierarchy($posts, $parent = 0) {
783 if ($posts) { foreach ($posts as $post) {
784 if ($post->post_parent == $parent) {
785 $result[$post->ID] = $post->post_name;
786 $children = get_page_hierarchy($posts, $post->ID);
787 $result += $children; //append $children to $result
793 function generate_page_rewrite_rules() {
796 //get pages in order of hierarchy, i.e. children after parents
797 $posts = get_page_hierarchy($wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_status = 'static'"));
798 //now reverse it, because we need parents after children for rewrite rules to work properly
799 $posts = array_reverse($posts, true);
801 $page_rewrite_rules = array();
802 $page_attachment_rewrite_rules = array();
806 foreach ($posts as $id => $post) {
809 $uri = get_page_uri($id);
810 $attachments = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_status = 'attachment' AND post_parent = '$id'");
811 if ( $attachments ) {
812 foreach ( $attachments as $attachment ) {
813 $attach_uri = get_page_uri($attachment->ID);
814 $page_attachment_rewrite_rules[$attach_uri] = $attachment->post_name;
818 $page_rewrite_rules[$uri] = $post;
821 update_option('page_uris', $page_rewrite_rules);
823 if ( $page_attachment_rewrite_rules )
824 update_option('page_attachment_uris', $page_attachment_rewrite_rules);
828 function get_post_status($ID = '') {
829 $post = get_post($ID);
831 if ( is_object($post) ) {
832 if ( ('attachment' == $post->post_status) && $post->post_parent && ($post->ID != $post->post_parent) )
833 return get_post_status($post->post_parent);
835 return $post->post_status;
841 // Takes a post ID, returns its mime type.
842 function get_post_mime_type($ID = '') {
843 $post = & get_post($ID);
845 if ( is_object($post) )
846 return $post->post_mime_type;
851 function get_attached_file($attachment_id) {
852 return get_post_meta($attachment_id, '_wp_attached_file', true);
855 function wp_mkdir_p($target) {
856 // from php.net/mkdir user contributed notes
857 if (file_exists($target)) {
858 if (! @ is_dir($target))
864 // Attempting to create the directory may clutter up our display.
865 if (@ mkdir($target)) {
866 $stat = @ stat(dirname($target));
867 $dir_perms = $stat['mode'] & 0007777; // Get the permission bits.
868 @ chmod($target, $dir_perms);
871 if ( is_dir(dirname($target)) )
875 // If the above failed, attempt to create the parent node, then try again.
876 if (wp_mkdir_p(dirname($target)))
877 return wp_mkdir_p($target);
882 // Returns an array containing the current upload directory's path and url, or an error message.
883 function wp_upload_dir() {
884 $siteurl = get_settings('siteurl');
885 //prepend ABSPATH to $dir and $siteurl to $url if they're not already there
886 $path = str_replace(ABSPATH, '', trim(get_settings('upload_path')));
887 $dir = ABSPATH . $path;
888 $url = trailingslashit($siteurl) . $path;
890 if ( $dir == ABSPATH ) { //the option was empty
891 $dir = ABSPATH . 'wp-content/uploads';
894 if ( defined('UPLOADS') ) {
895 $dir = ABSPATH . UPLOADS;
896 $url = trailingslashit($siteurl) . UPLOADS;
899 if ( get_settings('uploads_use_yearmonth_folders')) {
900 // Generate the yearly and monthly dirs
901 $time = current_time( 'mysql' );
902 $y = substr( $time, 0, 4 );
903 $m = substr( $time, 5, 2 );
904 $dir = $dir . "/$y/$m";
905 $url = $url . "/$y/$m";
908 // Make sure we have an uploads dir
909 if ( ! wp_mkdir_p( $dir ) ) {
910 $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), $dir);
911 return array('error' => $message);
914 $uploads = array('path' => $dir, 'url' => $url, 'error' => false);
915 return apply_filters('upload_dir', $uploads);
918 function wp_upload_bits($name, $type, $bits) {
920 return array('error' => __("Empty filename"));
922 $wp_filetype = wp_check_filetype($name);
923 if ( !$wp_filetype['ext'] )
924 return array('error' => __("Invalid file type"));
926 $upload = wp_upload_dir();
928 if ( $upload['error'] !== false )
933 $path_parts = pathinfo($filename);
934 $ext = $path_parts['extension'];
939 while ( file_exists($upload['path'] . "/$filename") ) {
940 if ( '' == "$number$ext" )
941 $filename = $filename . ++$number . $ext;
943 $filename = str_replace("$number$ext", ++$number . $ext, $filename);
946 $new_file = $upload['path'] . "/$filename";
947 if ( ! wp_mkdir_p( dirname($new_file) ) ) {
948 $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), dirname($new_file));
949 return array('error' => $message);
952 $ifp = @ fopen($new_file, 'wb');
954 return array('error' => "Could not write file $new_file.");
956 $success = @ fwrite($ifp, $bits);
958 // Set correct file permissions
959 $stat = @ stat(dirname($new_file));
960 $perms = $stat['mode'] & 0007777;
961 $perms = $perms & 0000666;
962 @ chmod($new_file, $perms);
965 $url = $upload['url'] . "/$filename";
967 return array('file' => $new_file, 'url' => $url, 'error' => false);
970 function wp_check_filetype($filename, $mimes = null) {
971 // Accepted MIME types are set here as PCRE unless provided.
972 $mimes = is_array($mimes) ? $mimes : apply_filters('upload_mimes', array (
973 'jpg|jpeg|jpe' => 'image/jpeg',
974 'gif' => 'image/gif',
975 'png' => 'image/png',
976 'bmp' => 'image/bmp',
977 'tif|tiff' => 'image/tiff',
978 'ico' => 'image/x-icon',
979 'asf|asx|wax|wmv|wmx' => 'video/asf',
980 'avi' => 'video/avi',
981 'mov|qt' => 'video/quicktime',
982 'mpeg|mpg|mpe' => 'video/mpeg',
983 'txt|c|cc|h' => 'text/plain',
984 'rtx' => 'text/richtext',
986 'htm|html' => 'text/html',
987 'mp3|mp4' => 'audio/mpeg',
988 'ra|ram' => 'audio/x-realaudio',
989 'wav' => 'audio/wav',
990 'ogg' => 'audio/ogg',
991 'mid|midi' => 'audio/midi',
992 'wma' => 'audio/wma',
993 'rtf' => 'application/rtf',
994 'js' => 'application/javascript',
995 'pdf' => 'application/pdf',
996 'doc' => 'application/msword',
997 'pot|pps|ppt' => 'application/vnd.ms-powerpoint',
998 'wri' => 'application/vnd.ms-write',
999 'xla|xls|xlt|xlw' => 'application/vnd.ms-excel',
1000 'mdb' => 'application/vnd.ms-access',
1001 'mpp' => 'application/vnd.ms-project',
1002 'swf' => 'application/x-shockwave-flash',
1003 'class' => 'application/java',
1004 'tar' => 'application/x-tar',
1005 'zip' => 'application/zip',
1006 'gz|gzip' => 'application/x-gzip',
1007 'exe' => 'application/x-msdownload'
1013 foreach ($mimes as $ext_preg => $mime_match) {
1014 $ext_preg = '!\.(' . $ext_preg . ')$!i';
1015 if ( preg_match($ext_preg, $filename, $ext_matches) ) {
1016 $type = $mime_match;
1017 $ext = $ext_matches[1];
1022 return compact('ext', 'type');