7 function get_attached_file( $attachment_id, $unfiltered = false ) {
8 $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
11 return apply_filters( 'get_attached_file', $file, $attachment_id );
14 function update_attached_file( $attachment_id, $file ) {
15 if ( !get_post( $attachment_id ) )
18 $old_file = get_attached_file( $attachment_id, true );
20 $file = apply_filters( 'update_attached_file', $file, $attachment_id );
23 return update_post_meta( $attachment_id, '_wp_attached_file', $file, $old_file );
25 return add_post_meta( $attachment_id, '_wp_attached_file', $file );
28 function &get_children($args = '', $output = OBJECT) {
29 global $post_cache, $wpdb, $blog_id;
31 if ( empty( $args ) ) {
32 if ( isset( $GLOBALS['post'] ) ) {
33 $args = 'post_parent=' . (int) $GLOBALS['post']->post_parent;
37 } elseif ( is_object( $args ) ) {
38 $args = 'post_parent=' . (int) $args->post_parent;
39 } elseif ( is_numeric( $args ) ) {
40 $args = 'post_parent=' . (int) $args;
44 'numberposts' => -1, 'post_type' => '',
45 'post_status' => '', 'post_parent' => 0
48 $r = wp_parse_args( $args, $defaults );
50 $children = get_posts( $r );
53 foreach ( $children as $key => $child ) {
54 $post_cache[$blog_id][$child->ID] =& $children[$key];
55 $kids[$child->ID] =& $children[$key];
61 if ( $output == OBJECT ) {
63 } elseif ( $output == ARRAY_A ) {
64 foreach ( $kids as $kid )
65 $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]);
67 } elseif ( $output == ARRAY_N ) {
68 foreach ( $kids as $kid )
69 $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID]));
76 // get extended entry info (<!--more-->)
77 function get_extended($post) {
78 //Match the new style more links
79 if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) {
80 list($main, $extended) = explode($matches[0], $post, 2);
86 // Strip leading and trailing whitespace
87 $main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main);
88 $extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended);
90 return array('main' => $main, 'extended' => $extended);
93 // Retrieves post data given a post ID or post object.
94 // Handles post caching.
95 function &get_post(&$post, $output = OBJECT, $filter = 'raw') {
96 global $post_cache, $wpdb, $blog_id;
99 if ( isset($GLOBALS['post']) )
100 $_post = & $GLOBALS['post'];
103 } elseif ( is_object($post) ) {
104 if ( 'page' == $post->post_type )
105 return get_page($post, $output, $filter);
106 if ( !isset($post_cache[$blog_id][$post->ID]) )
107 $post_cache[$blog_id][$post->ID] = &$post;
108 $_post = & $post_cache[$blog_id][$post->ID];
111 if ( isset($post_cache[$blog_id][$post]) )
112 $_post = & $post_cache[$blog_id][$post];
113 elseif ( $_post = wp_cache_get($post, 'pages') )
114 return get_page($_post, $output, $filter);
116 $query = "SELECT * FROM $wpdb->posts WHERE ID = '$post' LIMIT 1";
117 $_post = & $wpdb->get_row($query);
118 if ( 'page' == $_post->post_type )
119 return get_page($_post, $output, $filter);
120 $post_cache[$blog_id][$post] = & $_post;
124 if ( defined('WP_IMPORTING') )
125 unset($post_cache[$blog_id]);
127 $_post = sanitize_post($_post, $filter);
129 if ( $output == OBJECT ) {
131 } elseif ( $output == ARRAY_A ) {
132 return get_object_vars($_post);
133 } elseif ( $output == ARRAY_N ) {
134 return array_values(get_object_vars($_post));
140 function get_post_field( $field, $post, $context = 'display' ) {
142 $post = get_post( $post );
144 if ( is_wp_error($post) )
147 if ( !is_object($post) )
150 if ( !isset($post->$field) )
153 return sanitize_post_field($field, $post->$field, $post->ID, $context);
156 // Takes a post ID, returns its mime type.
157 function get_post_mime_type($ID = '') {
158 $post = & get_post($ID);
160 if ( is_object($post) )
161 return $post->post_mime_type;
166 function get_post_status($ID = '') {
167 $post = get_post($ID);
169 if ( is_object($post) ) {
170 if ( ('attachment' == $post->post_type) && $post->post_parent && ($post->ID != $post->post_parent) )
171 return get_post_status($post->post_parent);
173 return $post->post_status;
179 function get_post_type($post = false) {
180 global $wpdb, $posts;
182 if ( false === $post )
184 elseif ( (int) $post )
185 $post = get_post($post, OBJECT);
187 if ( is_object($post) )
188 return $post->post_type;
193 function get_posts($args) {
197 'numberposts' => 5, 'offset' => 0,
198 'category' => 0, 'orderby' => 'post_date',
199 'order' => 'DESC', 'include' => '',
200 'exclude' => '', 'meta_key' => '',
201 'meta_value' =>'', 'post_type' => 'post',
202 'post_status' => 'publish', 'post_parent' => 0
205 $r = wp_parse_args( $args, $defaults );
206 extract( $r, EXTR_SKIP );
208 $numberposts = (int) $numberposts;
209 $offset = (int) $offset;
210 $category = (int) $category;
211 $post_parent = (int) $post_parent;
214 if ( !empty($include) ) {
215 $offset = 0; //ignore offset, category, exclude, meta_key, and meta_value, post_parent if using include
221 $incposts = preg_split('/[\s,]+/',$include);
222 $numberposts = count($incposts); // only the number of posts included
223 if ( count($incposts) ) {
224 foreach ( $incposts as $incpost ) {
225 if (empty($inclusions))
226 $inclusions = ' AND ( ID = ' . intval($incpost) . ' ';
228 $inclusions .= ' OR ID = ' . intval($incpost) . ' ';
232 if (!empty($inclusions))
236 if ( !empty($exclude) ) {
237 $exposts = preg_split('/[\s,]+/',$exclude);
238 if ( count($exposts) ) {
239 foreach ( $exposts as $expost ) {
240 if (empty($exclusions))
241 $exclusions = ' AND ( ID <> ' . intval($expost) . ' ';
243 $exclusions .= ' AND ID <> ' . intval($expost) . ' ';
247 if (!empty($exclusions))
250 $query = "SELECT DISTINCT * FROM $wpdb->posts ";
251 $query .= empty( $category ) ? '' : ", $wpdb->term_relationships, $wpdb->term_taxonomy ";
252 $query .= empty( $meta_key ) ? '' : ", $wpdb->postmeta ";
253 $query .= " WHERE 1=1 ";
254 $query .= empty( $post_type ) ? '' : "AND post_type = '$post_type' ";
255 $query .= empty( $post_status ) ? '' : "AND post_status = '$post_status' ";
256 $query .= "$exclusions $inclusions " ;
257 $query .= empty( $category ) ? '' : "AND ($wpdb->posts.ID = $wpdb->term_relationships.object_id AND $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id AND $wpdb->term_taxonomy.term_id = " . $category. ") ";
258 $query .= empty( $post_parent ) ? '' : "AND $wpdb->posts.post_parent = '$post_parent' ";
259 $query .= empty( $meta_key ) | empty($meta_value) ? '' : " AND ($wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = '$meta_key' AND $wpdb->postmeta.meta_value = '$meta_value' )";
260 $query .= " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . ' ' . $order;
261 if ( 0 < $numberposts )
262 $query .= " LIMIT " . $offset . ',' . $numberposts;
264 $posts = $wpdb->get_results($query);
266 update_post_caches($posts);
272 // Post meta functions
275 function add_post_meta($post_id, $key, $value, $unique = false) {
276 global $wpdb, $post_meta_cache, $blog_id;
278 $post_id = (int) $post_id;
281 if ( $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = '$key' AND post_id = '$post_id'") ) {
286 $post_meta_cache[$blog_id][$post_id][$key][] = $value;
288 $value = maybe_serialize($value);
289 $value = $wpdb->escape($value);
291 $wpdb->query("INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value) VALUES ('$post_id','$key','$value')");
296 function delete_post_meta($post_id, $key, $value = '') {
297 global $wpdb, $post_meta_cache, $blog_id;
299 $post_id = (int) $post_id;
301 if ( empty($value) ) {
302 $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'");
304 $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'");
310 if ( empty($value) ) {
311 $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'");
312 unset($post_meta_cache[$blog_id][$post_id][$key]);
314 $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'");
315 $cache_key = $post_meta_cache[$blog_id][$post_id][$key];
316 if ($cache_key) foreach ( $cache_key as $index => $data )
317 if ( $data == $value )
318 unset($post_meta_cache[$blog_id][$post_id][$key][$index]);
321 unset($post_meta_cache[$blog_id][$post_id][$key]);
326 function get_post_meta($post_id, $key, $single = false) {
327 global $wpdb, $post_meta_cache, $blog_id;
329 $post_id = (int) $post_id;
331 if ( isset($post_meta_cache[$blog_id][$post_id][$key]) ) {
333 return maybe_unserialize( $post_meta_cache[$blog_id][$post_id][$key][0] );
335 return maybe_unserialize( $post_meta_cache[$blog_id][$post_id][$key] );
339 if ( !isset($post_meta_cache[$blog_id][$post_id]) )
340 update_postmeta_cache($post_id);
343 if ( isset($post_meta_cache[$blog_id][$post_id][$key][0]) )
344 return maybe_unserialize($post_meta_cache[$blog_id][$post_id][$key][0]);
348 return maybe_unserialize($post_meta_cache[$blog_id][$post_id][$key]);
352 function update_post_meta($post_id, $key, $value, $prev_value = '') {
353 global $wpdb, $post_meta_cache, $blog_id;
355 $post_id = (int) $post_id;
357 $original_value = $value;
358 $value = maybe_serialize($value);
359 $value = $wpdb->escape($value);
361 $original_prev = $prev_value;
362 $prev_value = maybe_serialize($prev_value);
363 $prev_value = $wpdb->escape($prev_value);
365 if (! $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = '$key' AND post_id = '$post_id'") ) {
369 if ( empty($prev_value) ) {
370 $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE meta_key = '$key' AND post_id = '$post_id'");
371 $cache_key = $post_meta_cache[$blog_id][$post_id][$key];
372 if ( !empty($cache_key) )
373 foreach ($cache_key as $index => $data)
374 $post_meta_cache[$blog_id][$post_id][$key][$index] = $original_value;
376 $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE meta_key = '$key' AND post_id = '$post_id' AND meta_value = '$prev_value'");
377 $cache_key = $post_meta_cache[$blog_id][$post_id][$key];
378 if ( !empty($cache_key) )
379 foreach ($cache_key as $index => $data)
380 if ( $data == $original_prev )
381 $post_meta_cache[$blog_id][$post_id][$key][$index] = $original_value;
388 function delete_post_meta_by_key($post_meta_key) {
389 global $wpdb, $post_meta_cache, $blog_id;
390 $post_meta_key = $wpdb->escape($post_meta_key);
391 if ( $wpdb->query("DELETE FROM $wpdb->postmeta WHERE meta_key = '$post_meta_key'") ) {
392 unset($post_meta_cache[$blog_id]); // not worth doing the work to iterate through the cache
399 function get_post_custom($post_id = 0) {
400 global $id, $post_meta_cache, $wpdb, $blog_id;
403 $post_id = (int) $id;
405 $post_id = (int) $post_id;
407 if ( !isset($post_meta_cache[$blog_id][$post_id]) )
408 update_postmeta_cache($post_id);
410 return $post_meta_cache[$blog_id][$post_id];
413 function get_post_custom_keys( $post_id = 0 ) {
414 $custom = get_post_custom( $post_id );
416 if ( !is_array($custom) )
419 if ( $keys = array_keys($custom) )
424 function get_post_custom_values( $key = '', $post_id = 0 ) {
425 $custom = get_post_custom($post_id);
427 return $custom[$key];
430 function sanitize_post($post, $context = 'display') {
432 if ( 'raw' == $context )
435 // TODO: Use array keys instead of hard coded list
436 $fields = array('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_date', 'post_date_gmt', 'post_parent', 'menu_order', 'post_mime_type', 'post_category');
439 if ( is_object($post) )
442 foreach ( $fields as $field ) {
444 $post->$field = sanitize_post_field($field, $post->$field, $post->ID, $context);
446 $post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context);
452 function sanitize_post_field($field, $value, $post_id, $context) {
453 $int_fields = array('ID', 'post_parent', 'menu_order');
454 if ( in_array($field, $int_fields) )
455 $value = (int) $value;
457 if ( 'raw' == $context )
461 if ( false !== strpos($field, 'post_') ) {
463 $field_no_prefix = str_replace('post_', '', $field);
466 if ( 'edit' == $context ) {
467 $format_to_edit = array('post_content', 'post_excerpt', 'post_title', 'post_password');
470 $value = apply_filters("edit_$field", $value, $post_id);
472 $value = apply_filters("${field_no_prefix}_edit_pre", $value, $post_id);
474 $value = apply_filters("edit_post_$field", $value, $post_id);
477 if ( in_array($field, $format_to_edit) ) {
478 if ( 'post_content' == $field )
479 $value = format_to_edit($value, user_can_richedit());
481 $value = format_to_edit($value);
483 $value = attribute_escape($value);
485 } else if ( 'db' == $context ) {
487 $value = apply_filters("pre_$field", $value);
488 $value = apply_filters("${field_no_prefix}_save_pre", $value);
490 $value = apply_filters("pre_post_$field", $value);
491 $value = apply_filters("${field}_pre", $value);
494 // Use display filters by default.
496 $value = apply_filters($field, $value, $post_id, $context);
498 $value = apply_filters("post_$field", $value, $post_id, $context);
501 if ( 'attribute' == $context )
502 $value = attribute_escape($value);
503 else if ( 'js' == $context )
504 $value = js_escape($value);
509 function wp_delete_post($postid = 0) {
510 global $wpdb, $wp_rewrite;
511 $postid = (int) $postid;
513 if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $postid") )
516 if ( 'attachment' == $post->post_type )
517 return wp_delete_attachment($postid);
519 do_action('delete_post', $postid);
521 // TODO delete for pluggable post taxonomies too
522 wp_delete_object_term_relationships($postid, array('category', 'post_tag'));
524 if ( 'page' == $post->post_type )
525 $wpdb->query("UPDATE $wpdb->posts SET post_parent = $post->post_parent WHERE post_parent = $postid AND post_type = 'page'");
527 $wpdb->query("UPDATE $wpdb->posts SET post_parent = $post->post_parent WHERE post_parent = $postid AND post_type = 'attachment'");
529 $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = $postid");
531 $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = $postid");
533 $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = $postid");
535 if ( 'page' == $post->post_type ) {
536 clean_page_cache($postid);
537 $wp_rewrite->flush_rules();
540 do_action('deleted_post', $postid);
545 function wp_get_post_categories( $post_id = 0, $args = array() ) {
546 $post_id = (int) $post_id;
548 $defaults = array('fields' => 'ids');
549 $args = wp_parse_args( $args, $defaults );
551 $cats = wp_get_object_terms($post_id, 'category', $args);
555 function wp_get_post_tags( $post_id = 0, $args = array() ) {
556 $post_id = (int) $post_id;
558 $defaults = array('fields' => 'all');
559 $args = wp_parse_args( $args, $defaults );
561 $tags = wp_get_object_terms($post_id, 'post_tag', $args);
566 function wp_get_recent_posts($num = 10) {
569 // Set the limit clause, if we got a limit
572 $limit = "LIMIT $num";
575 $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' ORDER BY post_date DESC $limit";
576 $result = $wpdb->get_results($sql,ARRAY_A);
578 return $result?$result:array();
581 function wp_get_single_post($postid = 0, $mode = OBJECT) {
584 $postid = (int) $postid;
586 $post = get_post($postid, $mode);
588 // Set categories and tags
589 if($mode == OBJECT) {
590 $post->post_category = wp_get_post_categories($postid);
591 $post->tags_input = wp_get_post_tags($postid, array('fields' => 'names'));
594 $post['post_category'] = wp_get_post_categories($postid);
595 $post['tags_input'] = wp_get_post_tags($postid, array('fields' => 'names'));
601 function wp_insert_post($postarr = array()) {
602 global $wpdb, $wp_rewrite, $allowedtags, $user_ID;
604 $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID,
605 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
606 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '');
608 $postarr = wp_parse_args($postarr, $defaults);
609 $postarr = sanitize_post($postarr, 'db');
611 // export array as variables
612 extract($postarr, EXTR_SKIP);
614 // Are we updating or creating?
618 $previous_status = get_post_field('post_status', $ID);
620 $previous_status = 'new';
623 if ( ('' == $post_content) && ('' == $post_title) && ('' == $post_excerpt) )
626 // Make sure we set a valid category
627 if (0 == count($post_category) || !is_array($post_category)) {
628 $post_category = array(get_option('default_category'));
631 if ( empty($post_author) )
632 $post_author = $user_ID;
634 if ( empty($post_status) )
635 $post_status = 'draft';
637 if ( empty($post_type) )
642 $post_ID = (int) $ID;
644 // Create a valid post name. Drafts are allowed to have an empty
646 if ( empty($post_name) ) {
647 if ( 'draft' != $post_status )
648 $post_name = sanitize_title($post_title);
650 $post_name = sanitize_title($post_name);
653 // If the post date is empty (due to having been new or a draft) and status is not 'draft', set date to now
654 if (empty($post_date)) {
655 if ( !in_array($post_status, array('draft', 'pending')) )
656 $post_date = current_time('mysql');
659 if (empty($post_date_gmt)) {
660 if ( !in_array($post_status, array('draft', 'pending')) )
661 $post_date_gmt = get_gmt_from_date($post_date);
664 if ( 'publish' == $post_status ) {
665 $now = gmdate('Y-m-d H:i:59');
666 if ( mysql2date('U', $post_date_gmt) > mysql2date('U', $now) )
667 $post_status = 'future';
670 if ( empty($comment_status) ) {
672 $comment_status = 'closed';
674 $comment_status = get_option('default_comment_status');
676 if ( empty($ping_status) )
677 $ping_status = get_option('default_ping_status');
679 if ( isset($to_ping) )
680 $to_ping = preg_replace('|\s+|', "\n", $to_ping);
684 if ( ! isset($pinged) )
687 if ( isset($post_parent) )
688 $post_parent = (int) $post_parent;
692 if ( isset($menu_order) )
693 $menu_order = (int) $menu_order;
697 if ( !isset($post_password) )
700 if ( 'draft' != $post_status ) {
701 $post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_type = '$post_type' AND ID != '$post_ID' AND post_parent = '$post_parent' LIMIT 1");
703 if ($post_name_check || in_array($post_name, $wp_rewrite->feeds) ) {
706 $alt_post_name = substr($post_name, 0, 200-(strlen($suffix)+1)). "-$suffix";
707 $post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_type = '$post_type' AND ID != '$post_ID' AND post_parent = '$post_parent' LIMIT 1");
709 } while ($post_name_check);
710 $post_name = $alt_post_name;
716 "UPDATE IGNORE $wpdb->posts SET
717 post_author = '$post_author',
718 post_date = '$post_date',
719 post_date_gmt = '$post_date_gmt',
720 post_content = '$post_content',
721 post_content_filtered = '$post_content_filtered',
722 post_title = '$post_title',
723 post_excerpt = '$post_excerpt',
724 post_status = '$post_status',
725 post_type = '$post_type',
726 comment_status = '$comment_status',
727 ping_status = '$ping_status',
728 post_password = '$post_password',
729 post_name = '$post_name',
730 to_ping = '$to_ping',
732 post_modified = '".current_time('mysql')."',
733 post_modified_gmt = '".current_time('mysql',1)."',
734 post_parent = '$post_parent',
735 menu_order = '$menu_order'
736 WHERE ID = $post_ID");
739 "INSERT IGNORE INTO $wpdb->posts
740 (post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_title, post_excerpt, post_status, post_type, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_mime_type)
742 ('$post_author', '$post_date', '$post_date_gmt', '$post_content', '$post_content_filtered', '$post_title', '$post_excerpt', '$post_status', '$post_type', '$comment_status', '$ping_status', '$post_password', '$post_name', '$to_ping', '$pinged', '$post_date', '$post_date_gmt', '$post_parent', '$menu_order', '$post_mime_type')");
743 $post_ID = (int) $wpdb->insert_id;
746 if ( empty($post_name) && 'draft' != $post_status ) {
747 $post_name = sanitize_title($post_title, $post_ID);
748 $wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" );
751 wp_set_post_categories( $post_ID, $post_category );
752 wp_set_post_tags( $post_ID, $tags_input );
754 if ( 'page' == $post_type ) {
755 clean_page_cache($post_ID);
757 clean_post_cache($post_ID);
762 $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post_ID) . "' WHERE ID = '$post_ID'");
764 $post = get_post($post_ID);
765 if ( !empty($page_template) )
766 $post->page_template = $page_template;
768 wp_transition_post_status($post_status, $previous_status, $post);
771 do_action('edit_post', $post_ID, $post);
773 do_action('save_post', $post_ID, $post);
774 do_action('wp_insert_post', $post_ID, $post);
779 function wp_update_post($postarr = array()) {
782 if ( is_object($postarr) )
783 $postarr = get_object_vars($postarr);
785 // First, get all of the original fields
786 $post = wp_get_single_post($postarr['ID'], ARRAY_A);
788 // Escape data pulled from DB.
789 $post = add_magic_quotes($post);
791 // Passed post category list overwrites existing category list if not empty.
792 if ( isset($postarr['post_category']) && is_array($postarr['post_category'])
793 && 0 != count($postarr['post_category']) )
794 $post_cats = $postarr['post_category'];
796 $post_cats = $post['post_category'];
798 // Drafts shouldn't be assigned a date unless explicitly done so by the user
799 if ( in_array($post['post_status'], array('draft', 'pending')) && empty($postarr['edit_date']) && empty($postarr['post_date']) &&
800 ('0000-00-00 00:00:00' == $post['post_date']) )
805 // Merge old and new fields with new fields overwriting old ones.
806 $postarr = array_merge($post, $postarr);
807 $postarr['post_category'] = $post_cats;
809 $postarr['post_date'] = '';
810 $postarr['post_date_gmt'] = '';
813 if ($postarr['post_type'] == 'attachment')
814 return wp_insert_attachment($postarr);
816 return wp_insert_post($postarr);
819 function wp_publish_post($post_id) {
822 $post = get_post($post_id);
827 if ( 'publish' == $post->post_status )
830 $wpdb->query( "UPDATE $wpdb->posts SET post_status = 'publish' WHERE ID = '$post_id'" );
832 $old_status = $post->post_status;
833 $post->post_status = 'publish';
834 wp_transition_post_status('publish', $old_status, $post);
836 do_action('edit_post', $post_id, $post);
837 do_action('save_post', $post_id, $post);
838 do_action('wp_insert_post', $post_id, $post);
841 function wp_add_post_tags($post_id = 0, $tags = '') {
842 return wp_set_post_tags($post_id, $tags, true);
845 function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) {
846 /* $append - true = don't delete existing tags, just add on, false = replace the tags with the new tags */
849 $post_id = (int) $post_id;
856 $tags = (is_array($tags)) ? $tags : explode( ',', $tags );
857 wp_set_object_terms($post_id, $tags, 'post_tag', $append);
860 function wp_set_post_categories($post_ID = 0, $post_categories = array()) {
863 $post_ID = (int) $post_ID;
864 // If $post_categories isn't already an array, make it one:
865 if (!is_array($post_categories) || 0 == count($post_categories) || empty($post_categories))
866 $post_categories = array(get_option('default_category'));
867 else if ( 1 == count($post_categories) && '' == $post_categories[0] )
870 $post_categories = array_map('intval', $post_categories);
871 $post_categories = array_unique($post_categories);
873 return wp_set_object_terms($post_ID, $post_categories, 'category');
874 } // wp_set_post_categories()
876 function wp_transition_post_status($new_status, $old_status, $post) {
877 if ( $new_status != $old_status ) {
878 do_action('transition_post_status', $new_status, $old_status, $post);
879 do_action("${old_status}_to_$new_status", $post);
881 do_action("${new_status}_$post->post_type", $post->ID, $post);
885 // Trackback and ping functions
888 function add_ping($post_id, $uri) { // Add a URL to those already pung
890 $pung = $wpdb->get_var("SELECT pinged FROM $wpdb->posts WHERE ID = $post_id");
892 $pung = preg_split('/\s/', $pung);
894 $new = implode("\n", $pung);
895 $new = apply_filters('add_ping', $new);
896 return $wpdb->query("UPDATE $wpdb->posts SET pinged = '$new' WHERE ID = $post_id");
899 function get_enclosed($post_id) { // Get enclosures already enclosed for a post
901 $custom_fields = get_post_custom( $post_id );
903 if ( !is_array( $custom_fields ) )
906 foreach ( $custom_fields as $key => $val ) {
907 if ( 'enclosure' != $key || !is_array( $val ) )
909 foreach( $val as $enc ) {
910 $enclosure = split( "\n", $enc );
911 $pung[] = trim( $enclosure[ 0 ] );
914 $pung = apply_filters('get_enclosed', $pung);
918 function get_pung($post_id) { // Get URLs already pung for a post
920 $pung = $wpdb->get_var("SELECT pinged FROM $wpdb->posts WHERE ID = $post_id");
922 $pung = preg_split('/\s/', $pung);
923 $pung = apply_filters('get_pung', $pung);
927 function get_to_ping($post_id) { // Get any URLs in the todo list
929 $to_ping = $wpdb->get_var("SELECT to_ping FROM $wpdb->posts WHERE ID = $post_id");
930 $to_ping = trim($to_ping);
931 $to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY);
932 $to_ping = apply_filters('get_to_ping', $to_ping);
936 // do trackbacks for a list of urls
937 // accepts a comma-separated list of trackback urls and a post id
938 function trackback_url_list($tb_list, $post_id) {
939 if (!empty($tb_list)) {
941 $postdata = wp_get_single_post($post_id, ARRAY_A);
943 // import postdata as variables
944 extract($postdata, EXTR_SKIP);
947 $excerpt = strip_tags($post_excerpt?$post_excerpt:$post_content);
949 if (strlen($excerpt) > 255) {
950 $excerpt = substr($excerpt,0,252) . '...';
953 $trackback_urls = explode(',', $tb_list);
954 foreach($trackback_urls as $tb_url) {
955 $tb_url = trim($tb_url);
956 trackback($tb_url, stripslashes($post_title), $excerpt, $post_id);
965 function get_all_page_ids() {
968 if ( ! $page_ids = wp_cache_get('all_page_ids', 'pages') ) {
969 $page_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'page'");
970 wp_cache_add('all_page_ids', $page_ids, 'pages');
977 // Retrieves page data given a page ID or page object.
978 // Handles page caching.
979 function &get_page(&$page, $output = OBJECT, $filter = 'raw') {
980 global $wpdb, $blog_id;
982 if ( empty($page) ) {
983 if ( isset( $GLOBALS['page'] ) && isset( $GLOBALS['page']->ID ) ) {
984 $_page = & $GLOBALS['page'];
985 wp_cache_add($_page->ID, $_page, 'pages');
987 // shouldn't we just return NULL at this point? ~ Mark
990 } elseif ( is_object($page) ) {
991 if ( 'post' == $page->post_type )
992 return get_post($page, $output, $filter);
993 wp_cache_add($page->ID, $page, 'pages');
997 // first, check the cache
998 if ( ! ( $_page = wp_cache_get($page, 'pages') ) ) {
999 // not in the page cache?
1000 if ( isset($GLOBALS['page']->ID) && ($page == $GLOBALS['page']->ID) ) { // for is_page() views
1001 // I don't think this code ever gets executed ~ Mark
1002 $_page = & $GLOBALS['page'];
1003 wp_cache_add($_page->ID, $_page, 'pages');
1004 } elseif ( isset($GLOBALS['post_cache'][$blog_id][$page]) ) { // it's actually a page, and is cached
1005 return get_post($page, $output, $filter);
1006 } else { // it's not in any caches, so off to the DB we go
1007 // Why are we using assignment for this query?
1008 $_page = & $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID= '$page' LIMIT 1");
1009 if ( 'post' == $_page->post_type )
1010 return get_post($_page, $output, $filter);
1011 // Potential issue: we're not checking to see if the post_type = 'page'
1012 // So all non-'post' posts will get cached as pages.
1013 wp_cache_add($_page->ID, $_page, 'pages');
1018 $_page = sanitize_post($_page, $filter);
1020 // at this point, one way or another, $_post contains the page object
1022 if ( $output == OBJECT ) {
1024 } elseif ( $output == ARRAY_A ) {
1025 return get_object_vars($_page);
1026 } elseif ( $output == ARRAY_N ) {
1027 return array_values(get_object_vars($_page));
1033 function get_page_by_path($page_path, $output = OBJECT) {
1035 $page_path = rawurlencode(urldecode($page_path));
1036 $page_path = str_replace('%2F', '/', $page_path);
1037 $page_path = str_replace('%20', ' ', $page_path);
1038 $page_paths = '/' . trim($page_path, '/');
1039 $leaf_path = sanitize_title(basename($page_paths));
1040 $page_paths = explode('/', $page_paths);
1041 foreach($page_paths as $pathdir)
1042 $full_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir);
1044 $pages = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = '$leaf_path' AND post_type='page'");
1046 if ( empty($pages) )
1049 foreach ($pages as $page) {
1050 $path = '/' . $leaf_path;
1052 while ($curpage->post_parent != 0) {
1053 $curpage = $wpdb->get_row("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = '$curpage->post_parent' and post_type='page'");
1054 $path = '/' . $curpage->post_name . $path;
1057 if ( $path == $full_path )
1058 return get_page($page->ID, $output);
1064 function get_page_by_title($page_title, $output = OBJECT) {
1066 $page_title = $wpdb->escape($page_title);
1067 $page = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$page_title' AND post_type='page'");
1069 return get_page($page, $output);
1074 function &get_page_children($page_id, $pages) {
1075 global $page_cache, $blog_id;
1077 if ( empty($pages) )
1078 $pages = &$page_cache[$blog_id];
1080 $page_list = array();
1081 foreach ( $pages as $page ) {
1082 if ( $page->post_parent == $page_id ) {
1083 $page_list[] = $page;
1084 if ( $children = get_page_children($page->ID, $pages) )
1085 $page_list = array_merge($page_list, $children);
1091 //fetches the pages returned as a FLAT list, but arranged in order of their hierarchy, i.e., child parents
1092 //immediately follow their parents
1093 function get_page_hierarchy($posts, $parent = 0) {
1094 $result = array ( );
1095 if ($posts) { foreach ($posts as $post) {
1096 if ($post->post_parent == $parent) {
1097 $result[$post->ID] = $post->post_name;
1098 $children = get_page_hierarchy($posts, $post->ID);
1099 $result += $children; //append $children to $result
1105 function get_page_uri($page_id) {
1106 $page = get_page($page_id);
1107 $uri = urldecode($page->post_name);
1109 // A page cannot be it's own parent.
1110 if ( $page->post_parent == $page->ID )
1113 while ($page->post_parent != 0) {
1114 $page = get_page($page->post_parent);
1115 $uri = urldecode($page->post_name) . "/" . $uri;
1121 function &get_pages($args = '') {
1125 'child_of' => 0, 'sort_order' => 'ASC',
1126 'sort_column' => 'post_title', 'hierarchical' => 1,
1127 'exclude' => '', 'include' => '',
1128 'meta_key' => '', 'meta_value' => '',
1132 $r = wp_parse_args( $args, $defaults );
1133 extract( $r, EXTR_SKIP );
1135 $key = md5( serialize( $r ) );
1136 if ( $cache = wp_cache_get( 'get_pages', 'page' ) )
1137 if ( isset( $cache[ $key ] ) )
1138 return apply_filters('get_pages', $cache[ $key ], $r );
1141 if ( !empty($include) ) {
1142 $child_of = 0; //ignore child_of, exclude, meta_key, and meta_value params if using include
1146 $hierarchical = false;
1147 $incpages = preg_split('/[\s,]+/',$include);
1148 if ( count($incpages) ) {
1149 foreach ( $incpages as $incpage ) {
1150 if (empty($inclusions))
1151 $inclusions = ' AND ( ID = ' . intval($incpage) . ' ';
1153 $inclusions .= ' OR ID = ' . intval($incpage) . ' ';
1157 if (!empty($inclusions))
1161 if ( !empty($exclude) ) {
1162 $expages = preg_split('/[\s,]+/',$exclude);
1163 if ( count($expages) ) {
1164 foreach ( $expages as $expage ) {
1165 if (empty($exclusions))
1166 $exclusions = ' AND ( ID <> ' . intval($expage) . ' ';
1168 $exclusions .= ' AND ID <> ' . intval($expage) . ' ';
1172 if (!empty($exclusions))
1176 if (!empty($authors)) {
1177 $post_authors = preg_split('/[\s,]+/',$authors);
1179 if ( count($post_authors) ) {
1180 foreach ( $post_authors as $post_author ) {
1181 //Do we have an author id or an author login?
1182 if ( 0 == intval($post_author) ) {
1183 $post_author = get_userdatabylogin($post_author);
1184 if ( empty($post_author) )
1186 if ( empty($post_author->ID) )
1188 $post_author = $post_author->ID;
1191 if ( '' == $author_query )
1192 $author_query = ' post_author = ' . intval($post_author) . ' ';
1194 $author_query .= ' OR post_author = ' . intval($post_author) . ' ';
1196 if ( '' != $author_query )
1197 $author_query = " AND ($author_query)";
1201 $query = "SELECT * FROM $wpdb->posts " ;
1202 $query .= ( empty( $meta_key ) ? "" : ", $wpdb->postmeta " ) ;
1203 $query .= " WHERE (post_type = 'page' AND post_status = 'publish') $exclusions $inclusions " ;
1204 $query .= ( empty( $meta_key ) | empty($meta_value) ? "" : " AND ($wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = '$meta_key' AND $wpdb->postmeta.meta_value = '$meta_value' )" ) ;
1205 $query .= $author_query;
1206 $query .= " ORDER BY " . $sort_column . " " . $sort_order ;
1208 $pages = $wpdb->get_results($query);
1210 if ( empty($pages) )
1211 return apply_filters('get_pages', array(), $r);
1214 update_page_cache($pages);
1216 if ( $child_of || $hierarchical )
1217 $pages = & get_page_children($child_of, $pages);
1219 $cache[ $key ] = $pages;
1220 wp_cache_set( 'get_pages', $cache, 'page' );
1222 $pages = apply_filters('get_pages', $pages, $r);
1227 function generate_page_uri_index() {
1230 //get pages in order of hierarchy, i.e. children after parents
1231 $posts = get_page_hierarchy($wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page'"));
1232 //now reverse it, because we need parents after children for rewrite rules to work properly
1233 $posts = array_reverse($posts, true);
1235 $page_uris = array();
1236 $page_attachment_uris = array();
1240 foreach ($posts as $id => $post) {
1243 $uri = get_page_uri($id);
1244 $attachments = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = '$id'");
1245 if ( $attachments ) {
1246 foreach ( $attachments as $attachment ) {
1247 $attach_uri = get_page_uri($attachment->ID);
1248 $page_attachment_uris[$attach_uri] = $attachment->ID;
1252 $page_uris[$uri] = $id;
1255 delete_option('page_uris');
1256 update_option('page_uris', $page_uris);
1258 if ( $page_attachment_uris ) {
1259 delete_option('page_attachment_uris');
1260 update_option('page_attachment_uris', $page_attachment_uris);
1266 // Attachment functions
1269 function is_local_attachment($url) {
1270 if (strpos($url, get_bloginfo('url')) === false)
1272 if (strpos($url, get_bloginfo('url') . '/?attachment_id=') !== false)
1274 if ( $id = url_to_postid($url) ) {
1275 $post = & get_post($id);
1276 if ( 'attachment' == $post->post_type )
1282 function wp_insert_attachment($object, $file = false, $parent = 0) {
1283 global $wpdb, $user_ID;
1285 $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID,
1286 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
1287 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '');
1289 $object = wp_parse_args($object, $defaults);
1290 if ( !empty($parent) )
1291 $object['post_parent'] = $parent;
1293 $object = sanitize_post($object, 'db');
1295 // export array as variables
1296 extract($object, EXTR_SKIP);
1298 // Make sure we set a valid category
1299 if (0 == count($post_category) || !is_array($post_category)) {
1300 $post_category = array(get_option('default_category'));
1303 if ( empty($post_author) )
1304 $post_author = $user_ID;
1306 $post_type = 'attachment';
1307 $post_status = 'inherit';
1309 // Are we updating or creating?
1311 if ( !empty($ID) ) {
1313 $post_ID = (int) $ID;
1316 // Create a valid post name.
1317 if ( empty($post_name) )
1318 $post_name = sanitize_title($post_title);
1320 $post_name = sanitize_title($post_name);
1323 $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_status = 'inherit' AND ID != '$post_ID' LIMIT 1");
1325 if ($post_name_check) {
1327 while ($post_name_check) {
1328 $alt_post_name = $post_name . "-$suffix";
1329 $post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_status = 'inherit' AND ID != '$post_ID' AND post_parent = '$post_parent' LIMIT 1");
1332 $post_name = $alt_post_name;
1335 if ( empty($post_date) )
1336 $post_date = current_time('mysql');
1337 if ( empty($post_date_gmt) )
1338 $post_date_gmt = current_time('mysql', 1);
1340 if ( empty($comment_status) ) {
1342 $comment_status = 'closed';
1344 $comment_status = get_option('default_comment_status');
1346 if ( empty($ping_status) )
1347 $ping_status = get_option('default_ping_status');
1349 if ( isset($to_ping) )
1350 $to_ping = preg_replace('|\s+|', "\n", $to_ping);
1354 if ( isset($post_parent) )
1355 $post_parent = (int) $post_parent;
1359 if ( isset($menu_order) )
1360 $menu_order = (int) $menu_order;
1364 if ( !isset($post_password) )
1365 $post_password = '';
1367 if ( ! isset($pinged) )
1372 "UPDATE $wpdb->posts SET
1373 post_author = '$post_author',
1374 post_date = '$post_date',
1375 post_date_gmt = '$post_date_gmt',
1376 post_content = '$post_content',
1377 post_content_filtered = '$post_content_filtered',
1378 post_title = '$post_title',
1379 post_excerpt = '$post_excerpt',
1380 post_status = '$post_status',
1381 post_type = '$post_type',
1382 comment_status = '$comment_status',
1383 ping_status = '$ping_status',
1384 post_password = '$post_password',
1385 post_name = '$post_name',
1386 to_ping = '$to_ping',
1388 post_modified = '".current_time('mysql')."',
1389 post_modified_gmt = '".current_time('mysql',1)."',
1390 post_parent = '$post_parent',
1391 menu_order = '$menu_order',
1392 post_mime_type = '$post_mime_type',
1394 WHERE ID = $post_ID");
1397 "INSERT INTO $wpdb->posts
1398 (post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_title, post_excerpt, post_status, post_type, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_mime_type, guid)
1400 ('$post_author', '$post_date', '$post_date_gmt', '$post_content', '$post_content_filtered', '$post_title', '$post_excerpt', '$post_status', '$post_type', '$comment_status', '$ping_status', '$post_password', '$post_name', '$to_ping', '$pinged', '$post_date', '$post_date_gmt', '$post_parent', '$menu_order', '$post_mime_type', '$guid')");
1401 $post_ID = (int) $wpdb->insert_id;
1404 if ( empty($post_name) ) {
1405 $post_name = sanitize_title($post_title, $post_ID);
1406 $wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" );
1409 wp_set_post_categories($post_ID, $post_category);
1412 update_attached_file( $post_ID, $file );
1414 clean_post_cache($post_ID);
1417 do_action('edit_attachment', $post_ID);
1419 do_action('add_attachment', $post_ID);
1425 function wp_delete_attachment($postid) {
1427 $postid = (int) $postid;
1429 if ( !$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = '$postid'") )
1432 if ( 'attachment' != $post->post_type )
1435 $meta = wp_get_attachment_metadata( $postid );
1436 $file = get_attached_file( $postid );
1438 // TODO delete for pluggable post taxonomies too
1439 wp_delete_object_term_relationships($postid, array('category', 'post_tag'));
1441 $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = '$postid'");
1443 $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = '$postid'");
1445 $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$postid'");
1447 if ( ! empty($meta['thumb']) ) {
1448 // Don't delete the thumb if another attachment uses it
1449 if (! $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")) {
1450 $thumbfile = str_replace(basename($file), $meta['thumb'], $file);
1451 $thumbfile = apply_filters('wp_delete_file', $thumbfile);
1452 @ unlink($thumbfile);
1456 $file = apply_filters('wp_delete_file', $file);
1458 if ( ! empty($file) )
1461 do_action('delete_attachment', $postid);
1466 function wp_get_attachment_metadata( $post_id, $unfiltered = false ) {
1467 $post_id = (int) $post_id;
1468 if ( !$post =& get_post( $post_id ) )
1471 $data = get_post_meta( $post->ID, '_wp_attachment_metadata', true );
1474 return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID );
1477 function wp_update_attachment_metadata( $post_id, $data ) {
1478 $post_id = (int) $post_id;
1479 if ( !$post =& get_post( $post_id ) )
1482 $old_data = wp_get_attachment_metadata( $post->ID, true );
1484 $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID );
1487 return update_post_meta( $post->ID, '_wp_attachment_metadata', $data, $old_data );
1489 return add_post_meta( $post->ID, '_wp_attachment_metadata', $data );
1492 function wp_get_attachment_url( $post_id = 0 ) {
1493 $post_id = (int) $post_id;
1494 if ( !$post =& get_post( $post_id ) )
1497 $url = get_the_guid( $post->ID );
1499 if ( 'attachment' != $post->post_type || !$url )
1502 return apply_filters( 'wp_get_attachment_url', $url, $post->ID );
1505 function wp_get_attachment_thumb_file( $post_id = 0 ) {
1506 $post_id = (int) $post_id;
1507 if ( !$post =& get_post( $post_id ) )
1509 if ( !$imagedata = wp_get_attachment_metadata( $post->ID ) )
1512 $file = get_attached_file( $post->ID );
1514 if ( !empty($imagedata['thumb']) && ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file)) && file_exists($thumbfile) )
1515 return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID );
1519 function wp_get_attachment_thumb_url( $post_id = 0 ) {
1520 $post_id = (int) $post_id;
1521 if ( !$post =& get_post( $post_id ) )
1523 if ( !$url = wp_get_attachment_url( $post->ID ) )
1526 if ( !$thumb = wp_get_attachment_thumb_file( $post->ID ) )
1529 $url = str_replace(basename($url), basename($thumb), $url);
1531 return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID );
1534 function wp_attachment_is_image( $post_id = 0 ) {
1535 $post_id = (int) $post_id;
1536 if ( !$post =& get_post( $post_id ) )
1539 if ( !$file = get_attached_file( $post->ID ) )
1542 $ext = preg_match('/\.([^.]+)$/', $file, $matches) ? strtolower($matches[1]) : false;
1544 $image_exts = array('jpg', 'jpeg', 'gif', 'png');
1546 if ( 'image/' == substr($post->post_mime_type, 0, 6) || $ext && 'import' == $post->post_mime_type && in_array($ext, $image_exts) )
1551 function wp_mime_type_icon( $mime = 0 ) {
1553 if ( is_numeric($mime) ) {
1554 $mime = (int) $mime;
1555 if ( !$post =& get_post( $mime ) )
1557 $post_id = (int) $post->ID;
1558 $mime = $post->post_mime_type;
1564 $icon_dir = apply_filters( 'icon_dir', get_template_directory() . '/images' );
1565 $icon_dir_uri = apply_filters( 'icon_dir_uri', get_template_directory_uri() . '/images' );
1568 substr($mime, 0, strpos($mime, '/')),
1569 substr($mime, strpos($mime, '/') + 1),
1570 str_replace('/', '_', $mime)
1573 $exts = array('jpg', 'gif', 'png');
1577 foreach ( $types as $type ) {
1578 foreach ( $exts as $ext ) {
1579 $src_file = "$icon_dir/$type.$ext";
1580 if ( file_exists($src_file) ) {
1581 $src = "$icon_dir_uri/$type.$ext";
1587 return apply_filters( 'wp_mime_type_icon', $src, $mime, $post_id ); // Last arg is 0 if function pass mime type.
1590 function wp_check_for_changed_slugs($post_id) {
1591 if ( !strlen($_POST['wp-old-slug']) )
1594 $post = &get_post($post_id);
1596 // we're only concerned with published posts
1597 if ( $post->post_status != 'publish' || $post->post_type != 'post' )
1600 // only bother if the slug has changed
1601 if ( $post->post_name == $_POST['wp-old-slug'] )
1604 $old_slugs = (array) get_post_meta($post_id, '_wp_old_slug');
1606 // if we haven't added this old slug before, add it now
1607 if ( !count($old_slugs) || !in_array($_POST['wp-old-slug'], $old_slugs) )
1608 add_post_meta($post_id, '_wp_old_slug', $_POST['wp-old-slug']);
1610 // if the new slug was used previously, delete it from the list
1611 if ( in_array($post->post_name, $old_slugs) )
1612 delete_post_meta($post_id, '_wp_old_slug', $post->post_name);
1618 * This function provides a standardized way to appropriately select on
1619 * the post_status of posts/pages. The function will return a piece of
1620 * SQL code that can be added to a WHERE clause; this SQL is constructed
1621 * to allow all published posts, and all private posts to which the user
1624 * @param string $post_type currently only supports 'post' or 'page'.
1625 * @return string SQL code that can be added to a where clause.
1627 function get_private_posts_cap_sql($post_type) {
1632 if ($post_type == 'post') {
1633 $cap = 'read_private_posts';
1635 } elseif ($post_type == 'page') {
1636 $cap = 'read_private_pages';
1637 // Dunno what it is, maybe plugins have their own post type?
1639 $cap = apply_filters('pub_priv_sql_capability', $cap);
1642 // We don't know what it is, filters don't change anything,
1643 // so set the SQL up to return nothing.
1648 $sql = '(post_status = \'publish\'';
1650 if (current_user_can($cap)) {
1651 // Does the user have the capability to view private posts? Guess so.
1652 $sql .= ' OR post_status = \'private\'';
1653 } elseif (is_user_logged_in()) {
1654 // Users can view their own private posts.
1655 $sql .= ' OR post_status = \'private\' AND post_author = \'' . $user_ID . '\'';
1663 function get_lastpostdate($timezone = 'server') {
1664 global $cache_lastpostdate, $pagenow, $wpdb, $blog_id;
1665 $add_seconds_blog = get_option('gmt_offset') * 3600;
1666 $add_seconds_server = date('Z');
1667 if ( !isset($cache_lastpostdate[$blog_id][$timezone]) ) {
1668 switch(strtolower($timezone)) {
1670 $lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
1673 $lastpostdate = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
1676 $lastpostdate = $wpdb->get_var("SELECT DATE_ADD(post_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
1679 $cache_lastpostdate[$blog_id][$timezone] = $lastpostdate;
1681 $lastpostdate = $cache_lastpostdate[$blog_id][$timezone];
1683 return apply_filters( 'get_lastpostdate', $lastpostdate );
1686 function get_lastpostmodified($timezone = 'server') {
1687 global $cache_lastpostmodified, $pagenow, $wpdb, $blog_id;
1688 $add_seconds_blog = get_option('gmt_offset') * 3600;
1689 $add_seconds_server = date('Z');
1690 if ( !isset($cache_lastpostmodified[$blog_id][$timezone]) ) {
1691 switch(strtolower($timezone)) {
1693 $lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
1696 $lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
1699 $lastpostmodified = $wpdb->get_var("SELECT DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
1702 $lastpostdate = get_lastpostdate($timezone);
1703 if ( $lastpostdate > $lastpostmodified ) {
1704 $lastpostmodified = $lastpostdate;
1706 $cache_lastpostmodified[$blog_id][$timezone] = $lastpostmodified;
1708 $lastpostmodified = $cache_lastpostmodified[$blog_id][$timezone];
1710 return apply_filters( 'get_lastpostmodified', $lastpostmodified );
1717 function update_post_cache(&$posts) {
1718 global $post_cache, $blog_id;
1723 for ($i = 0; $i < count($posts); $i++) {
1724 $post_cache[$blog_id][$posts[$i]->ID] = &$posts[$i];
1728 function clean_post_cache($id) {
1729 global $post_cache, $post_meta_cache, $post_term_cache, $blog_id;
1731 if ( isset( $post_cache[$blog_id][$id] ) )
1732 unset( $post_cache[$blog_id][$id] );
1734 if ( isset ($post_meta_cache[$blog_id][$id] ) )
1735 unset( $post_meta_cache[$blog_id][$id] );
1737 clean_object_term_cache($id, 'post');
1740 function update_page_cache(&$pages) {
1741 global $page_cache, $blog_id;
1746 for ($i = 0; $i < count($pages); $i++) {
1747 $page_cache[$blog_id][$pages[$i]->ID] = &$pages[$i];
1748 wp_cache_add($pages[$i]->ID, $pages[$i], 'pages');
1752 function clean_page_cache($id) {
1753 global $page_cache, $blog_id;
1755 if ( isset( $page_cache[$blog_id][$id] ) )
1756 unset( $page_cache[$blog_id][$id] );
1758 wp_cache_delete($id, 'pages');
1759 wp_cache_delete( 'all_page_ids', 'pages' );
1760 wp_cache_delete( 'get_pages', 'page' );
1763 function update_post_caches(&$posts) {
1765 global $wpdb, $blog_id;
1767 // No point in doing all this work if we didn't match any posts.
1771 // Get the categories for all the posts
1772 for ($i = 0; $i < count($posts); $i++) {
1773 $post_id_array[] = $posts[$i]->ID;
1774 $post_cache[$blog_id][$posts[$i]->ID] = &$posts[$i];
1777 $post_id_list = implode(',', $post_id_array);
1779 update_object_term_cache($post_id_list, 'post');
1781 update_postmeta_cache($post_id_list);
1784 function update_postmeta_cache($post_id_list = '') {
1785 global $wpdb, $post_meta_cache, $blog_id;
1787 // We should validate this comma-separated list for the upcoming SQL query
1788 $post_id_list = preg_replace('|[^0-9,]|', '', $post_id_list);
1790 if ( empty( $post_id_list ) )
1793 // we're marking each post as having its meta cached (with no keys... empty array), to prevent posts with no meta keys from being queried again
1794 // any posts that DO have keys will have this empty array overwritten with a proper array, down below
1795 $post_id_array = (array) explode(',', $post_id_list);
1796 $count = count( $post_id_array);
1797 for ( $i = 0; $i < $count; $i++ ) {
1798 $post_id = (int) $post_id_array[ $i ];
1799 if ( isset( $post_meta_cache[$blog_id][$post_id] ) ) { // If the meta is already cached
1800 unset( $post_id_array[ $i ] );
1803 $post_meta_cache[$blog_id][$post_id] = array();
1805 if ( count( $post_id_array ) == 0 )
1807 $post_id_list = join( ',', $post_id_array ); // with already cached stuff removeds
1809 // Get post-meta info
1810 if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE post_id IN($post_id_list) ORDER BY post_id, meta_key", ARRAY_A) ) {
1811 // Change from flat structure to hierarchical:
1812 if ( !isset($post_meta_cache) )
1813 $post_meta_cache[$blog_id] = array();
1815 foreach ($meta_list as $metarow) {
1816 $mpid = (int) $metarow['post_id'];
1817 $mkey = $metarow['meta_key'];
1818 $mval = $metarow['meta_value'];
1820 // Force subkeys to be array type:
1821 if ( !isset($post_meta_cache[$blog_id][$mpid]) || !is_array($post_meta_cache[$blog_id][$mpid]) )
1822 $post_meta_cache[$blog_id][$mpid] = array();
1823 if ( !isset($post_meta_cache[$blog_id][$mpid]["$mkey"]) || !is_array($post_meta_cache[$blog_id][$mpid]["$mkey"]) )
1824 $post_meta_cache[$blog_id][$mpid]["$mkey"] = array();
1826 // Add a value to the current pid/key:
1827 $post_meta_cache[$blog_id][$mpid][$mkey][] = $mval;
1836 function _transition_post_status($new_status, $old_status, $post) {
1839 if ( $old_status != 'publish' && $new_status == 'publish' ) {
1840 // Reset GUID if transitioning to publish.
1841 $wpdb->query("UPDATE $wpdb->posts SET guid = '" . get_permalink($post->ID) . "' WHERE ID = '$post->ID'");
1842 do_action('private_to_published', $post->ID); // Deprecated, use private_to_publish
1845 // Always clears the hook in case the post status bounced from future to draft.
1846 wp_clear_scheduled_hook('publish_future_post', $post->ID);
1849 function _future_post_hook($post_id, $post) {
1850 // Schedule publication.
1851 wp_clear_scheduled_hook( 'publish_future_post', $post->ID );
1852 wp_schedule_single_event(strtotime($post->post_date_gmt. ' GMT'), 'publish_future_post', array($post->ID));
1855 function _publish_post_hook($post_id) {
1858 if ( defined('XMLRPC_REQUEST') )
1859 do_action('xmlrpc_publish_post', $post_id);
1860 if ( defined('APP_REQUEST') )
1861 do_action('app_publish_post', $post_id);
1863 if ( defined('WP_IMPORTING') )
1866 $post = get_post($post_id);
1868 if ( get_option('default_pingback_flag') )
1869 $result = $wpdb->query("
1870 INSERT INTO $wpdb->postmeta
1871 (post_id,meta_key,meta_value)
1872 VALUES ('$post_id','_pingme','1')
1874 $result = $wpdb->query("
1875 INSERT INTO $wpdb->postmeta
1876 (post_id,meta_key,meta_value)
1877 VALUES ('$post_id','_encloseme','1')
1879 wp_schedule_single_event(time(), 'do_pings');
1882 function _save_post_hook($post_id, $post) {
1883 if ( $post->post_type == 'page' ) {
1884 if ( !empty($post->page_template) )
1885 if ( ! update_post_meta($post_id, '_wp_page_template', $post->page_template))
1886 add_post_meta($post_id, '_wp_page_template', $post->page_template, true);
1888 clean_page_cache($post_id);
1890 $wp_rewrite->flush_rules();
1892 clean_post_cache($post_id);