]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/post.php
WordPress 4.1.3-scripts
[autoinstalls/wordpress.git] / wp-includes / post.php
1 <?php
2 /**
3  * Post functions and post utility function.
4  *
5  * @package WordPress
6  * @subpackage Post
7  * @since 1.5.0
8  */
9
10 //
11 // Post Type Registration
12 //
13
14 /**
15  * Creates the initial post types when 'init' action is fired.
16  *
17  * @since 2.9.0
18  */
19 function create_initial_post_types() {
20         register_post_type( 'post', array(
21                 'labels' => array(
22                         'name_admin_bar' => _x( 'Post', 'add new on admin bar' ),
23                 ),
24                 'public'  => true,
25                 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
26                 '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
27                 'capability_type' => 'post',
28                 'map_meta_cap' => true,
29                 'hierarchical' => false,
30                 'rewrite' => false,
31                 'query_var' => false,
32                 'delete_with_user' => true,
33                 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
34         ) );
35
36         register_post_type( 'page', array(
37                 'labels' => array(
38                         'name_admin_bar' => _x( 'Page', 'add new on admin bar' ),
39                 ),
40                 'public' => true,
41                 'publicly_queryable' => false,
42                 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
43                 '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
44                 'capability_type' => 'page',
45                 'map_meta_cap' => true,
46                 'hierarchical' => true,
47                 'rewrite' => false,
48                 'query_var' => false,
49                 'delete_with_user' => true,
50                 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
51         ) );
52
53         register_post_type( 'attachment', array(
54                 'labels' => array(
55                         'name' => _x('Media', 'post type general name'),
56                         'name_admin_bar' => _x( 'Media', 'add new from admin bar' ),
57                         'add_new' => _x( 'Add New', 'add new media' ),
58                         'edit_item' => __( 'Edit Media' ),
59                         'view_item' => __( 'View Attachment Page' ),
60                 ),
61                 'public' => true,
62                 'show_ui' => true,
63                 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
64                 '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
65                 'capability_type' => 'post',
66                 'capabilities' => array(
67                         'create_posts' => 'upload_files',
68                 ),
69                 'map_meta_cap' => true,
70                 'hierarchical' => false,
71                 'rewrite' => false,
72                 'query_var' => false,
73                 'show_in_nav_menus' => false,
74                 'delete_with_user' => true,
75                 'supports' => array( 'title', 'author', 'comments' ),
76         ) );
77         add_post_type_support( 'attachment:audio', 'thumbnail' );
78         add_post_type_support( 'attachment:video', 'thumbnail' );
79
80         register_post_type( 'revision', array(
81                 'labels' => array(
82                         'name' => __( 'Revisions' ),
83                         'singular_name' => __( 'Revision' ),
84                 ),
85                 'public' => false,
86                 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
87                 '_edit_link' => 'revision.php?revision=%d', /* internal use only. don't use this when registering your own post type. */
88                 'capability_type' => 'post',
89                 'map_meta_cap' => true,
90                 'hierarchical' => false,
91                 'rewrite' => false,
92                 'query_var' => false,
93                 'can_export' => false,
94                 'delete_with_user' => true,
95                 'supports' => array( 'author' ),
96         ) );
97
98         register_post_type( 'nav_menu_item', array(
99                 'labels' => array(
100                         'name' => __( 'Navigation Menu Items' ),
101                         'singular_name' => __( 'Navigation Menu Item' ),
102                 ),
103                 'public' => false,
104                 '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
105                 'hierarchical' => false,
106                 'rewrite' => false,
107                 'delete_with_user' => false,
108                 'query_var' => false,
109         ) );
110
111         register_post_status( 'publish', array(
112                 'label'       => _x( 'Published', 'post' ),
113                 'public'      => true,
114                 '_builtin'    => true, /* internal use only. */
115                 'label_count' => _n_noop( 'Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>' ),
116         ) );
117
118         register_post_status( 'future', array(
119                 'label'       => _x( 'Scheduled', 'post' ),
120                 'protected'   => true,
121                 '_builtin'    => true, /* internal use only. */
122                 'label_count' => _n_noop('Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>' ),
123         ) );
124
125         register_post_status( 'draft', array(
126                 'label'       => _x( 'Draft', 'post' ),
127                 'protected'   => true,
128                 '_builtin'    => true, /* internal use only. */
129                 'label_count' => _n_noop( 'Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>' ),
130         ) );
131
132         register_post_status( 'pending', array(
133                 'label'       => _x( 'Pending', 'post' ),
134                 'protected'   => true,
135                 '_builtin'    => true, /* internal use only. */
136                 'label_count' => _n_noop( 'Pending <span class="count">(%s)</span>', 'Pending <span class="count">(%s)</span>' ),
137         ) );
138
139         register_post_status( 'private', array(
140                 'label'       => _x( 'Private', 'post' ),
141                 'private'     => true,
142                 '_builtin'    => true, /* internal use only. */
143                 'label_count' => _n_noop( 'Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>' ),
144         ) );
145
146         register_post_status( 'trash', array(
147                 'label'       => _x( 'Trash', 'post' ),
148                 'internal'    => true,
149                 '_builtin'    => true, /* internal use only. */
150                 'label_count' => _n_noop( 'Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>' ),
151                 'show_in_admin_status_list' => true,
152         ) );
153
154         register_post_status( 'auto-draft', array(
155                 'label'    => 'auto-draft',
156                 'internal' => true,
157                 '_builtin' => true, /* internal use only. */
158         ) );
159
160         register_post_status( 'inherit', array(
161                 'label'    => 'inherit',
162                 'internal' => true,
163                 '_builtin' => true, /* internal use only. */
164                 'exclude_from_search' => false,
165         ) );
166 }
167 add_action( 'init', 'create_initial_post_types', 0 ); // highest priority
168
169 /**
170  * Retrieve attached file path based on attachment ID.
171  *
172  * By default the path will go through the 'get_attached_file' filter, but
173  * passing a true to the $unfiltered argument of get_attached_file() will
174  * return the file path unfiltered.
175  *
176  * The function works by getting the single post meta name, named
177  * '_wp_attached_file' and returning it. This is a convenience function to
178  * prevent looking up the meta name and provide a mechanism for sending the
179  * attached filename through a filter.
180  *
181  * @since 2.0.0
182  *
183  * @param int  $attachment_id Attachment ID.
184  * @param bool $unfiltered    Optional. Whether to apply filters. Default false.
185  * @return string|bool The file path to where the attached file should be, false otherwise.
186  */
187 function get_attached_file( $attachment_id, $unfiltered = false ) {
188         $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
189         // If the file is relative, prepend upload dir.
190         if ( $file && 0 !== strpos($file, '/') && !preg_match('|^.:\\\|', $file) && ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) )
191                 $file = $uploads['basedir'] . "/$file";
192         if ( $unfiltered )
193                 return $file;
194
195         /**
196          * Filter the attached file based on the given ID.
197          *
198          * @since 2.1.0
199          *
200          * @param string $file          Path to attached file.
201          * @param int    $attachment_id Attachment ID.
202          */
203         return apply_filters( 'get_attached_file', $file, $attachment_id );
204 }
205
206 /**
207  * Update attachment file path based on attachment ID.
208  *
209  * Used to update the file path of the attachment, which uses post meta name
210  * '_wp_attached_file' to store the path of the attachment.
211  *
212  * @since 2.1.0
213  *
214  * @param int    $attachment_id Attachment ID.
215  * @param string $file          File path for the attachment.
216  * @return bool True on success, false on failure.
217  */
218 function update_attached_file( $attachment_id, $file ) {
219         if ( !get_post( $attachment_id ) )
220                 return false;
221
222         /**
223          * Filter the path to the attached file to update.
224          *
225          * @since 2.1.0
226          *
227          * @param string $file          Path to the attached file to update.
228          * @param int    $attachment_id Attachment ID.
229          */
230         $file = apply_filters( 'update_attached_file', $file, $attachment_id );
231
232         if ( $file = _wp_relative_upload_path( $file ) )
233                 return update_post_meta( $attachment_id, '_wp_attached_file', $file );
234         else
235                 return delete_post_meta( $attachment_id, '_wp_attached_file' );
236 }
237
238 /**
239  * Return relative path to an uploaded file.
240  *
241  * The path is relative to the current upload dir.
242  *
243  * @since 2.9.0
244  *
245  * @param string $path Full path to the file.
246  * @return string Relative path on success, unchanged path on failure.
247  */
248 function _wp_relative_upload_path( $path ) {
249         $new_path = $path;
250
251         $uploads = wp_upload_dir();
252         if ( 0 === strpos( $new_path, $uploads['basedir'] ) ) {
253                         $new_path = str_replace( $uploads['basedir'], '', $new_path );
254                         $new_path = ltrim( $new_path, '/' );
255         }
256
257         /**
258          * Filter the relative path to an uploaded file.
259          *
260          * @since 2.9.0
261          *
262          * @param string $new_path Relative path to the file.
263          * @param string $path     Full path to the file.
264          */
265         return apply_filters( '_wp_relative_upload_path', $new_path, $path );
266 }
267
268 /**
269  * Retrieve all children of the post parent ID.
270  *
271  * Normally, without any enhancements, the children would apply to pages. In the
272  * context of the inner workings of WordPress, pages, posts, and attachments
273  * share the same table, so therefore the functionality could apply to any one
274  * of them. It is then noted that while this function does not work on posts, it
275  * does not mean that it won't work on posts. It is recommended that you know
276  * what context you wish to retrieve the children of.
277  *
278  * Attachments may also be made the child of a post, so if that is an accurate
279  * statement (which needs to be verified), it would then be possible to get
280  * all of the attachments for a post. Attachments have since changed since
281  * version 2.5, so this is most likely unaccurate, but serves generally as an
282  * example of what is possible.
283  *
284  * The arguments listed as defaults are for this function and also of the
285  * {@link get_posts()} function. The arguments are combined with the
286  * get_children defaults and are then passed to the {@link get_posts()}
287  * function, which accepts additional arguments. You can replace the defaults in
288  * this function, listed below and the additional arguments listed in the
289  * {@link get_posts()} function.
290  *
291  * The 'post_parent' is the most important argument and important attention
292  * needs to be paid to the $args parameter. If you pass either an object or an
293  * integer (number), then just the 'post_parent' is grabbed and everything else
294  * is lost. If you don't specify any arguments, then it is assumed that you are
295  * in The Loop and the post parent will be grabbed for from the current post.
296  *
297  * The 'post_parent' argument is the ID to get the children. The 'numberposts'
298  * is the amount of posts to retrieve that has a default of '-1', which is
299  * used to get all of the posts. Giving a number higher than 0 will only
300  * retrieve that amount of posts.
301  *
302  * The 'post_type' and 'post_status' arguments can be used to choose what
303  * criteria of posts to retrieve. The 'post_type' can be anything, but WordPress
304  * post types are 'post', 'pages', and 'attachments'. The 'post_status'
305  * argument will accept any post status within the write administration panels.
306  *
307  * @internal Claims made in the long description might be inaccurate.
308  * @since 2.0.0
309  *
310  * @see get_posts()
311  *
312  * @param mixed  $args   Optional. User defined arguments for replacing the defaults. Default empty.
313  * @param string $output Optional. Constant for return type. Accepts OBJECT, ARRAY_A, ARRAY_N.
314  *                       Default OBJECt.
315  * @return array Array of children, where the type of each element is determined by $output parameter.
316  *               Empty array on failure.
317  */
318 function get_children( $args = '', $output = OBJECT ) {
319         $kids = array();
320         if ( empty( $args ) ) {
321                 if ( isset( $GLOBALS['post'] ) ) {
322                         $args = array('post_parent' => (int) $GLOBALS['post']->post_parent );
323                 } else {
324                         return $kids;
325                 }
326         } elseif ( is_object( $args ) ) {
327                 $args = array('post_parent' => (int) $args->post_parent );
328         } elseif ( is_numeric( $args ) ) {
329                 $args = array('post_parent' => (int) $args);
330         }
331
332         $defaults = array(
333                 'numberposts' => -1, 'post_type' => 'any',
334                 'post_status' => 'any', 'post_parent' => 0,
335         );
336
337         $r = wp_parse_args( $args, $defaults );
338
339         $children = get_posts( $r );
340
341         if ( ! $children )
342                 return $kids;
343
344         if ( ! empty( $r['fields'] ) )
345                 return $children;
346
347         update_post_cache($children);
348
349         foreach ( $children as $key => $child )
350                 $kids[$child->ID] = $children[$key];
351
352         if ( $output == OBJECT ) {
353                 return $kids;
354         } elseif ( $output == ARRAY_A ) {
355                 foreach ( (array) $kids as $kid )
356                         $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]);
357                 return $weeuns;
358         } elseif ( $output == ARRAY_N ) {
359                 foreach ( (array) $kids as $kid )
360                         $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID]));
361                 return $babes;
362         } else {
363                 return $kids;
364         }
365 }
366
367 /**
368  * Get extended entry info (<!--more-->).
369  *
370  * There should not be any space after the second dash and before the word
371  * 'more'. There can be text or space(s) after the word 'more', but won't be
372  * referenced.
373  *
374  * The returned array has 'main', 'extended', and 'more_text' keys. Main has the text before
375  * the `<!--more-->`. The 'extended' key has the content after the
376  * `<!--more-->` comment. The 'more_text' key has the custom "Read More" text.
377  *
378  * @since 1.0.0
379  *
380  * @param string $post Post content.
381  * @return array Post before ('main'), after ('extended'), and custom readmore ('more_text').
382  */
383 function get_extended( $post ) {
384         //Match the new style more links.
385         if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) {
386                 list($main, $extended) = explode($matches[0], $post, 2);
387                 $more_text = $matches[1];
388         } else {
389                 $main = $post;
390                 $extended = '';
391                 $more_text = '';
392         }
393
394         //  leading and trailing whitespace.
395         $main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main);
396         $extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended);
397         $more_text = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $more_text);
398
399         return array( 'main' => $main, 'extended' => $extended, 'more_text' => $more_text );
400 }
401
402 /**
403  * Retrieves post data given a post ID or post object.
404  *
405  * See {@link sanitize_post()} for optional $filter values. Also, the parameter
406  * $post, must be given as a variable, since it is passed by reference.
407  *
408  * @since 1.5.1
409  *
410  * @param int|WP_Post $post   Optional. Post ID or post object. Defaults to global $post.
411  * @param string      $output Optional, default is Object. Accepts OBJECT, ARRAY_A, or ARRAY_N.
412  *                            Default OBJECT.
413  * @param string      $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',
414  *                            or 'display'. Default 'raw'.
415  * @return WP_Post|null WP_Post on success or null on failure.
416  */
417 function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) {
418         if ( empty( $post ) && isset( $GLOBALS['post'] ) )
419                 $post = $GLOBALS['post'];
420
421         if ( is_a( $post, 'WP_Post' ) ) {
422                 $_post = $post;
423         } elseif ( is_object( $post ) ) {
424                 if ( empty( $post->filter ) ) {
425                         $_post = sanitize_post( $post, 'raw' );
426                         $_post = new WP_Post( $_post );
427                 } elseif ( 'raw' == $post->filter ) {
428                         $_post = new WP_Post( $post );
429                 } else {
430                         $_post = WP_Post::get_instance( $post->ID );
431                 }
432         } else {
433                 $_post = WP_Post::get_instance( $post );
434         }
435
436         if ( ! $_post )
437                 return null;
438
439         $_post = $_post->filter( $filter );
440
441         if ( $output == ARRAY_A )
442                 return $_post->to_array();
443         elseif ( $output == ARRAY_N )
444                 return array_values( $_post->to_array() );
445
446         return $_post;
447 }
448
449 /**
450  * WordPress Post class.
451  *
452  * @since 3.5.0
453  *
454  */
455 final class WP_Post {
456
457         /**
458          * Post ID.
459          *
460          * @var int
461          */
462         public $ID;
463
464         /**
465          * ID of post author.
466          *
467          * A numeric string, for compatibility reasons.
468          *
469          * @var string
470          */
471         public $post_author = 0;
472
473         /**
474          * The post's local publication time.
475          *
476          * @var string
477          */
478         public $post_date = '0000-00-00 00:00:00';
479
480         /**
481          * The post's GMT publication time.
482          *
483          * @var string
484          */
485         public $post_date_gmt = '0000-00-00 00:00:00';
486
487         /**
488          * The post's content.
489          *
490          * @var string
491          */
492         public $post_content = '';
493
494         /**
495          * The post's title.
496          *
497          * @var string
498          */
499         public $post_title = '';
500
501         /**
502          * The post's excerpt.
503          *
504          * @var string
505          */
506         public $post_excerpt = '';
507
508         /**
509          * The post's status.
510          *
511          * @var string
512          */
513         public $post_status = 'publish';
514
515         /**
516          * Whether comments are allowed.
517          *
518          * @var string
519          */
520         public $comment_status = 'open';
521
522         /**
523          * Whether pings are allowed.
524          *
525          * @var string
526          */
527         public $ping_status = 'open';
528
529         /**
530          * The post's password in plain text.
531          *
532          * @var string
533          */
534         public $post_password = '';
535
536         /**
537          * The post's slug.
538          *
539          * @var string
540          */
541         public $post_name = '';
542
543         /**
544          * URLs queued to be pinged.
545          *
546          * @var string
547          */
548         public $to_ping = '';
549
550         /**
551          * URLs that have been pinged.
552          *
553          * @var string
554          */
555         public $pinged = '';
556
557         /**
558          * The post's local modified time.
559          *
560          * @var string
561          */
562         public $post_modified = '0000-00-00 00:00:00';
563
564         /**
565          * The post's GMT modified time.
566          *
567          * @var string
568          */
569         public $post_modified_gmt = '0000-00-00 00:00:00';
570
571         /**
572          * A utility DB field for post content.
573          *
574          *
575          * @var string
576          */
577         public $post_content_filtered = '';
578
579         /**
580          * ID of a post's parent post.
581          *
582          * @var int
583          */
584         public $post_parent = 0;
585
586         /**
587          * The unique identifier for a post, not necessarily a URL, used as the feed GUID.
588          *
589          * @var string
590          */
591         public $guid = '';
592
593         /**
594          * A field used for ordering posts.
595          *
596          * @var int
597          */
598         public $menu_order = 0;
599
600         /**
601          * The post's type, like post or page.
602          *
603          * @var string
604          */
605         public $post_type = 'post';
606
607         /**
608          * An attachment's mime type.
609          *
610          * @var string
611          */
612         public $post_mime_type = '';
613
614         /**
615          * Cached comment count.
616          *
617          * A numeric string, for compatibility reasons.
618          *
619          * @var string
620          */
621         public $comment_count = 0;
622
623         /**
624          * Stores the post object's sanitization level.
625          *
626          * Does not correspond to a DB field.
627          *
628          * @var string
629          */
630         public $filter;
631
632         /**
633          * Retrieve WP_Post instance.
634          *
635          * @static
636          * @access public
637          *
638          * @param int $post_id Post ID.
639          * @return WP_Post|bool Post object, false otherwise.
640          */
641         public static function get_instance( $post_id ) {
642                 global $wpdb;
643
644                 $post_id = (int) $post_id;
645                 if ( ! $post_id )
646                         return false;
647
648                 $_post = wp_cache_get( $post_id, 'posts' );
649
650                 if ( ! $_post ) {
651                         $_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id ) );
652
653                         if ( ! $_post )
654                                 return false;
655
656                         $_post = sanitize_post( $_post, 'raw' );
657                         wp_cache_add( $_post->ID, $_post, 'posts' );
658                 } elseif ( empty( $_post->filter ) ) {
659                         $_post = sanitize_post( $_post, 'raw' );
660                 }
661
662                 return new WP_Post( $_post );
663         }
664
665         /**
666          * Constructor.
667          *
668          * @param WP_Post $post Post object.
669          */
670         public function __construct( $post ) {
671                 foreach ( get_object_vars( $post ) as $key => $value )
672                         $this->$key = $value;
673         }
674
675         /**
676          * Isset-er.
677          *
678          * @param string $key Property to check if set.
679          * @return bool
680          */
681         public function __isset( $key ) {
682                 if ( 'ancestors' == $key )
683                         return true;
684
685                 if ( 'page_template' == $key )
686                         return ( 'page' == $this->post_type );
687
688                 if ( 'post_category' == $key )
689                    return true;
690
691                 if ( 'tags_input' == $key )
692                    return true;
693
694                 return metadata_exists( 'post', $this->ID, $key );
695         }
696
697         /**
698          * Getter.
699          *
700          * @param string $key Key to get.
701          * @return array|mixed
702          */
703         public function __get( $key ) {
704                 if ( 'page_template' == $key && $this->__isset( $key ) ) {
705                         return get_post_meta( $this->ID, '_wp_page_template', true );
706                 }
707
708                 if ( 'post_category' == $key ) {
709                         if ( is_object_in_taxonomy( $this->post_type, 'category' ) )
710                                 $terms = get_the_terms( $this, 'category' );
711
712                         if ( empty( $terms ) )
713                                 return array();
714
715                         return wp_list_pluck( $terms, 'term_id' );
716                 }
717
718                 if ( 'tags_input' == $key ) {
719                         if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) )
720                                 $terms = get_the_terms( $this, 'post_tag' );
721
722                         if ( empty( $terms ) )
723                                 return array();
724
725                         return wp_list_pluck( $terms, 'name' );
726                 }
727
728                 // Rest of the values need filtering.
729                 if ( 'ancestors' == $key )
730                         $value = get_post_ancestors( $this );
731                 else
732                         $value = get_post_meta( $this->ID, $key, true );
733
734                 if ( $this->filter )
735                         $value = sanitize_post_field( $key, $value, $this->ID, $this->filter );
736
737                 return $value;
738         }
739
740         /**
741          * {@Missing Summary}
742          *
743          * @param string $filter Filter.
744          * @return $this|array|bool|object|WP_Post
745          */
746         public function filter( $filter ) {
747                 if ( $this->filter == $filter )
748                         return $this;
749
750                 if ( $filter == 'raw' )
751                         return self::get_instance( $this->ID );
752
753                 return sanitize_post( $this, $filter );
754         }
755
756         /**
757          * Convert object to array.
758          *
759          * @return array Object as array.
760          */
761         public function to_array() {
762                 $post = get_object_vars( $this );
763
764                 foreach ( array( 'ancestors', 'page_template', 'post_category', 'tags_input' ) as $key ) {
765                         if ( $this->__isset( $key ) )
766                                 $post[ $key ] = $this->__get( $key );
767                 }
768
769                 return $post;
770         }
771 }
772
773 /**
774  * Retrieve ancestors of a post.
775  *
776  * @since 2.5.0
777  *
778  * @param int|WP_Post $post Post ID or post object.
779  * @return array Ancestor IDs or empty array if none are found.
780  */
781 function get_post_ancestors( $post ) {
782         $post = get_post( $post );
783
784         if ( ! $post || empty( $post->post_parent ) || $post->post_parent == $post->ID )
785                 return array();
786
787         $ancestors = array();
788
789         $id = $ancestors[] = $post->post_parent;
790
791         while ( $ancestor = get_post( $id ) ) {
792                 // Loop detection: If the ancestor has been seen before, break.
793                 if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors ) )
794                         break;
795
796                 $id = $ancestors[] = $ancestor->post_parent;
797         }
798
799         return $ancestors;
800 }
801
802 /**
803  * Retrieve data from a post field based on Post ID.
804  *
805  * Examples of the post field will be, 'post_type', 'post_status', 'post_content',
806  * etc and based off of the post object property or key names.
807  *
808  * The context values are based off of the taxonomy filter functions and
809  * supported values are found within those functions.
810  *
811  * @since 2.3.0
812  *
813  * @see sanitize_post_field()
814  *
815  * @param string      $field   Post field name.
816  * @param int|WP_Post $post    Post ID or post object.
817  * @param string      $context Optional. How to filter the field. Accepts 'raw', 'edit', 'db',
818  *                             or 'display'. Default 'display'.
819  * @return string The value of the post field on success, empty string on failure.
820  */
821 function get_post_field( $field, $post, $context = 'display' ) {
822         $post = get_post( $post );
823
824         if ( !$post )
825                 return '';
826
827         if ( !isset($post->$field) )
828                 return '';
829
830         return sanitize_post_field($field, $post->$field, $post->ID, $context);
831 }
832
833 /**
834  * Retrieve the mime type of an attachment based on the ID.
835  *
836  * This function can be used with any post type, but it makes more sense with
837  * attachments.
838  *
839  * @since 2.0.0
840  *
841  * @param int|WP_Post $ID Optional. Post ID or post object. Default empty.
842  * @return string|false The mime type on success, false on failure.
843  */
844 function get_post_mime_type( $ID = '' ) {
845         $post = get_post($ID);
846
847         if ( is_object($post) )
848                 return $post->post_mime_type;
849
850         return false;
851 }
852
853 /**
854  * Retrieve the post status based on the Post ID.
855  *
856  * If the post ID is of an attachment, then the parent post status will be given
857  * instead.
858  *
859  * @since 2.0.0
860  *
861  * @param int|WP_Post $ID Optional. Post ID or post object. Default empty.
862  * @return string|false Post status on success, false on failure.
863  */
864 function get_post_status( $ID = '' ) {
865         $post = get_post($ID);
866
867         if ( !is_object($post) )
868                 return false;
869
870         if ( 'attachment' == $post->post_type ) {
871                 if ( 'private' == $post->post_status )
872                         return 'private';
873
874                 // Unattached attachments are assumed to be published.
875                 if ( ( 'inherit' == $post->post_status ) && ( 0 == $post->post_parent) )
876                         return 'publish';
877
878                 // Inherit status from the parent.
879                 if ( $post->post_parent && ( $post->ID != $post->post_parent ) ) {
880                         $parent_post_status = get_post_status( $post->post_parent );
881                         if ( 'trash' == $parent_post_status ) {
882                                 return get_post_meta( $post->post_parent, '_wp_trash_meta_status', true );
883                         } else {
884                                 return $parent_post_status;
885                         }
886                 }
887
888         }
889
890         return $post->post_status;
891 }
892
893 /**
894  * Retrieve all of the WordPress supported post statuses.
895  *
896  * Posts have a limited set of valid status values, this provides the
897  * post_status values and descriptions.
898  *
899  * @since 2.5.0
900  *
901  * @return array List of post statuses.
902  */
903 function get_post_statuses() {
904         $status = array(
905                 'draft'   => __( 'Draft' ),
906                 'pending' => __( 'Pending Review' ),
907                 'private' => __( 'Private' ),
908                 'publish' => __( 'Published' )
909         );
910
911         return $status;
912 }
913
914 /**
915  * Retrieve all of the WordPress support page statuses.
916  *
917  * Pages have a limited set of valid status values, this provides the
918  * post_status values and descriptions.
919  *
920  * @since 2.5.0
921  *
922  * @return array List of page statuses.
923  */
924 function get_page_statuses() {
925         $status = array(
926                 'draft'   => __( 'Draft' ),
927                 'private' => __( 'Private' ),
928                 'publish' => __( 'Published' )
929         );
930
931         return $status;
932 }
933
934 /**
935  * Register a post status. Do not use before init.
936  *
937  * A simple function for creating or modifying a post status based on the
938  * parameters given. The function will accept an array (second optional
939  * parameter), along with a string for the post status name.
940  *
941  * Arguments prefixed with an _underscore shouldn't be used by plugins and themes.
942  *
943  * @since 3.0.0
944  * @uses $wp_post_statuses Inserts new post status object into the list
945  *
946  * @param string $post_status Name of the post status.
947  * @param array|string $args {
948  *     Optional. Array or string of post status arguments.
949  *
950  *     @type bool|string $label                     A descriptive name for the post status marked
951  *                                                  for translation. Defaults to value of $post_status.
952  *     @type bool|array  $label_count               Descriptive text to use for nooped plurals.
953  *                                                  Default array of $label, twice
954  *     @type bool        $exclude_from_search       Whether to exclude posts with this post status
955  *                                                  from search results. Default is value of $internal.
956  *     @type bool        $_builtin                  Whether the status is built-in. Core-use only.
957  *                                                  Default false.
958  *     @type bool        $public                    Whether posts of this status should be shown
959  *                                                  in the front end of the site. Default true.
960  *     @type bool        $internal                  Whether the status is for internal use only.
961  *                                                  Default false.
962  *     @type bool        $protected                 Whether posts with this status should be protected.
963  *                                                  Default false.
964  *     @type bool        $private                   Whether posts with this status should be private.
965  *                                                  Default false.
966  *     @type bool        $publicly_queryable        Whether posts with this status should be publicly-
967  *                                                  queryable. Default is value of $public.
968  *     @type bool        $show_in_admin_all_list    Whether to include posts in the edit listing for
969  *                                                  their post type. Default is value of $internal.
970  *     @type bool        $show_in_admin_status_list Show in the list of statuses with post counts at
971  *                                                  the top of the edit listings,
972  *                                                  e.g. All (12) | Published (9) | My Custom Status (2)
973  *                                                  Default is value of $internal.
974  * }
975  */
976 function register_post_status( $post_status, $args = array() ) {
977         global $wp_post_statuses;
978
979         if (!is_array($wp_post_statuses))
980                 $wp_post_statuses = array();
981
982         // Args prefixed with an underscore are reserved for internal use.
983         $defaults = array(
984                 'label' => false,
985                 'label_count' => false,
986                 'exclude_from_search' => null,
987                 '_builtin' => false,
988                 'public' => null,
989                 'internal' => null,
990                 'protected' => null,
991                 'private' => null,
992                 'publicly_queryable' => null,
993                 'show_in_admin_status_list' => null,
994                 'show_in_admin_all_list' => null,
995         );
996         $args = wp_parse_args($args, $defaults);
997         $args = (object) $args;
998
999         $post_status = sanitize_key($post_status);
1000         $args->name = $post_status;
1001
1002         // Set various defaults.
1003         if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private )
1004                 $args->internal = true;
1005
1006         if ( null === $args->public  )
1007                 $args->public = false;
1008
1009         if ( null === $args->private  )
1010                 $args->private = false;
1011
1012         if ( null === $args->protected  )
1013                 $args->protected = false;
1014
1015         if ( null === $args->internal  )
1016                 $args->internal = false;
1017
1018         if ( null === $args->publicly_queryable )
1019                 $args->publicly_queryable = $args->public;
1020
1021         if ( null === $args->exclude_from_search )
1022                 $args->exclude_from_search = $args->internal;
1023
1024         if ( null === $args->show_in_admin_all_list )
1025                 $args->show_in_admin_all_list = !$args->internal;
1026
1027         if ( null === $args->show_in_admin_status_list )
1028                 $args->show_in_admin_status_list = !$args->internal;
1029
1030         if ( false === $args->label )
1031                 $args->label = $post_status;
1032
1033         if ( false === $args->label_count )
1034                 $args->label_count = array( $args->label, $args->label );
1035
1036         $wp_post_statuses[$post_status] = $args;
1037
1038         return $args;
1039 }
1040
1041 /**
1042  * Retrieve a post status object by name.
1043  *
1044  * @since 3.0.0
1045  *
1046  * @global array $wp_post_statuses List of post statuses.
1047  *
1048  * @see register_post_status()
1049  *
1050  * @param string $post_status The name of a registered post status.
1051  * @return object A post status object.
1052  */
1053 function get_post_status_object( $post_status ) {
1054         global $wp_post_statuses;
1055
1056         if ( empty($wp_post_statuses[$post_status]) )
1057                 return null;
1058
1059         return $wp_post_statuses[$post_status];
1060 }
1061
1062 /**
1063  * Get a list of post statuses.
1064  *
1065  * @since 3.0.0
1066  *
1067  * @global array $wp_post_statuses List of post statuses.
1068  *
1069  * @see register_post_status()
1070  *
1071  * @param array|string $args     Optional. Array or string of post status arguments to compare against
1072  *                               properties of the global `$wp_post_statuses objects`. Default empty array.
1073  * @param string       $output   Optional. The type of output to return, either 'names' or 'objects'. Default 'names'.
1074  * @param string       $operator Optional. The logical operation to perform. 'or' means only one element
1075  *                               from the array needs to match; 'and' means all elements must match.
1076  *                               Default 'and'.
1077  * @return array A list of post status names or objects.
1078  */
1079 function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) {
1080         global $wp_post_statuses;
1081
1082         $field = ('names' == $output) ? 'name' : false;
1083
1084         return wp_filter_object_list($wp_post_statuses, $args, $operator, $field);
1085 }
1086
1087 /**
1088  * Whether the post type is hierarchical.
1089  *
1090  * A false return value might also mean that the post type does not exist.
1091  *
1092  * @since 3.0.0
1093  *
1094  * @see get_post_type_object()
1095  *
1096  * @param string $post_type Post type name
1097  * @return bool Whether post type is hierarchical.
1098  */
1099 function is_post_type_hierarchical( $post_type ) {
1100         if ( ! post_type_exists( $post_type ) )
1101                 return false;
1102
1103         $post_type = get_post_type_object( $post_type );
1104         return $post_type->hierarchical;
1105 }
1106
1107 /**
1108  * Check if a post type is registered.
1109  *
1110  * @since 3.0.0
1111  *
1112  * @see get_post_type_object()
1113  *
1114  * @param string $post_type Post type name.
1115  * @return bool Whether post type is registered.
1116  */
1117 function post_type_exists( $post_type ) {
1118         return (bool) get_post_type_object( $post_type );
1119 }
1120
1121 /**
1122  * Retrieve the post type of the current post or of a given post.
1123  *
1124  * @since 2.1.0
1125  *
1126  * @param int|WP_Post $post Optional. Post ID or post object. Default is global $post.
1127  * @return string|false Post type on success, false on failure.
1128  */
1129 function get_post_type( $post = null ) {
1130         if ( $post = get_post( $post ) )
1131                 return $post->post_type;
1132
1133         return false;
1134 }
1135
1136 /**
1137  * Retrieve a post type object by name.
1138  *
1139  * @since 3.0.0
1140  *
1141  * @global array $wp_post_types List of post types.
1142  *
1143  * @see register_post_type()
1144  *
1145  * @param string $post_type The name of a registered post type.
1146  * @return object A post type object.
1147  */
1148 function get_post_type_object( $post_type ) {
1149         global $wp_post_types;
1150
1151         if ( empty($wp_post_types[$post_type]) )
1152                 return null;
1153
1154         return $wp_post_types[$post_type];
1155 }
1156
1157 /**
1158  * Get a list of all registered post type objects.
1159  *
1160  * @since 2.9.0
1161  *
1162  * @global array $wp_post_types List of post types.
1163  *
1164  * @see register_post_type() for accepted arguments.
1165  *
1166  * @param array|string $args     Optional. An array of key => value arguments to match against
1167  *                               the post type objects. Default empty array.
1168  * @param string       $output   Optional. The type of output to return. Accepts post type 'names'
1169  *                               or 'objects'. Default 'names'.
1170  * @param string       $operator Optional. The logical operation to perform. 'or' means only one
1171  *                               element from the array needs to match; 'and' means all elements
1172  *                               must match. Accepts 'or' or 'and'. Default 'and'.
1173  * @return array A list of post type names or objects.
1174  */
1175 function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) {
1176         global $wp_post_types;
1177
1178         $field = ('names' == $output) ? 'name' : false;
1179
1180         return wp_filter_object_list($wp_post_types, $args, $operator, $field);
1181 }
1182
1183 /**
1184  * Register a post type. Do not use before init.
1185  *
1186  * A function for creating or modifying a post type based on the
1187  * parameters given. The function will accept an array (second optional
1188  * parameter), along with a string for the post type name.
1189  *
1190  * @since 2.9.0
1191  *
1192  * @global array      $wp_post_types List of post types.
1193  * @global WP_Rewrite $wp_rewrite    Used for default feeds.
1194  * @global WP         $wp            Used to add query vars.
1195  *
1196  * @param string $post_type Post type key, must not exceed 20 characters.
1197  * @param array|string $args {
1198  *     Array or string of arguments for registering a post type.
1199  *
1200  *     @type string      $label                Name of the post type shown in the menu. Usually plural.
1201  *                                             Default is value of $labels['name'].
1202  *     @type array       $labels               An array of labels for this post type. If not set, post
1203  *                                             labels are inherited for non-hierarchical types and page
1204  *                                             labels for hierarchical ones. {@see get_post_type_labels()}.
1205  *     @type string      $description          A short descriptive summary of what the post type is.
1206  *                                             Default empty.
1207  *     @type bool        $public               Whether a post type is intended for use publicly either via
1208  *                                             the admin interface or by front-end users. While the default
1209  *                                             settings of $exclude_from_search, $publicly_queryable, $show_ui,
1210  *                                             and $show_in_nav_menus are inherited from public, each does not
1211  *                                             rely on this relationship and controls a very specific intention.
1212  *                                             Default false.
1213  *     @type bool        $hierarchical         Whether the post type is hierarchical (e.g. page). Default false.
1214  *     @type bool        $exclude_from_search  Whether to exclude posts with this post type from front end search
1215  *                                             results. Default is the opposite value of $public.
1216  *     @type bool        $publicly_queryable   Whether queries can be performed on the front end for the post type
1217  *                                             as part of {@see parse_request()}. Endpoints would include:
1218  *                                             * ?post_type={post_type_key}
1219  *                                             * ?{post_type_key}={single_post_slug}
1220  *                                             * ?{post_type_query_var}={single_post_slug}
1221  *                                             If not set, the default is inherited from $public.
1222  *     @type bool        $show_ui              Whether to generate a default UI for managing this post type in the
1223  *                                             admin. Default is value of $public.
1224  *     @type bool        $show_in_menu         Where to show the post type in the admin menu. To work, $show_ui
1225  *                                             must be true. If true, the post type is shown in its own top level
1226  *                                             menu. If false, no menu is shown. If a string of an existing top
1227  *                                             level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post
1228  *                                             type will be placed as a sub-menu of that.
1229  *                                             Default is value of $show_ui.
1230  *     @type bool        $show_in_nav_menus    Makes this post type available for selection in navigation menus.
1231  *                                             Default is value $public.
1232  *     @type bool        $show_in_admin_bar    Makes this post type available via the admin bar. Default is value
1233  *                                             of $show_in_menu.
1234  *     @type int         $menu_position        The position in the menu order the post type should appear. To work,
1235  *                                             $show_in_menu must be true. Default null (at the bottom).
1236  *     @type string      $menu_icon            The url to the icon to be used for this menu. Pass a base64-encoded
1237  *                                             SVG using a data URI, which will be colored to match the color scheme
1238  *                                             -- this should begin with 'data:image/svg+xml;base64,'. Pass the name
1239  *                                             of a Dashicons helper class to use a font icon, e.g.
1240  *                                             'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty
1241  *                                             so an icon can be added via CSS. Defaults to use the posts icon.
1242  *     @type string      $capability_type      The string to use to build the read, edit, and delete capabilities.
1243  *                                             May be passed as an array to allow for alternative plurals when using
1244  *                                             this argument as a base to construct the capabilities, e.g.
1245  *                                             array('story', 'stories'). Default 'post'.
1246  *     @type array       $capabilities         Array of capabilities for this post type. $capability_type is used
1247  *                                             as a base to construct capabilities by default.
1248  *                                             {@see get_post_type_capabilities()}.
1249  *     @type bool        $map_meta_cap         Whether to use the internal default meta capability handling.
1250  *                                             Default false.
1251  *     @type array       $supports             An alias for calling {@see add_post_type_support()} directly.
1252  *                                             Defaults to array containing 'title' & 'editor'.
1253  *     @type callback    $register_meta_box_cb Provide a callback function that sets up the meta boxes for the
1254  *                                             edit form. Do remove_meta_box() and add_meta_box() calls in the
1255  *                                             callback. Default null.
1256  *     @type array       $taxonomies           An array of taxonomy identifiers that will be registered for the
1257  *                                             post type. Taxonomies can be registered later with
1258  *                                             {@see register_taxonomy()} or {@see register_taxonomy_for_object_type()}.
1259  *                                             Default empty array.
1260  *     @type bool|string $has_archive          Whether there should be post type archives, or if a string, the
1261  *                                             archive slug to use. Will generate the proper rewrite rules if
1262  *                                             $rewrite is enabled. Default false.
1263  *     @type bool|array  $rewrite              {
1264  *         Triggers the handling of rewrites for this post type. To prevent rewrite, set to false.
1265  *         Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be
1266  *         passed with any of these keys:
1267  *
1268  *         @type string $slug       Customize the permastruct slug. Defaults to $post_type key.
1269  *         @type bool   $with_front Whether the permastruct should be prepended with WP_Rewrite::$front.
1270  *                                  Default true.
1271  *         @type bool   $feeds      Whether the feed permastruct should be built for this post type.
1272  *                                  Default is value of $has_archive.
1273  *         @type bool   $pages      Whether the permastruct should provide for pagination. Default true.
1274  *         @type const  $ep_mask    Endpoint mask to assign. If not specified and permalink_epmask is set,
1275  *                                  inherits from $permalink_epmask. If not specified and permalink_epmask
1276  *                                  is not set, defaults to EP_PERMALINK.
1277  *     }
1278  *     @type string|bool $query_var            Sets the query_var key for this post type. Defaults to $post_type
1279  *                                             key. If false, a post type cannot be loaded at
1280  *                                             ?{query_var}={post_slug}. If specified as a string, the query
1281  *                                             ?{query_var_string}={post_slug} will be valid.
1282  *     @type bool        $can_export           Whether to allow this post type to be exported. Default true.
1283  *     @type bool        $delete_with_user     Whether to delete posts of this type when deleting a user. If true,
1284  *                                             posts of this type belonging to the user will be moved to trash
1285  *                                             when then user is deleted. If false, posts of this type belonging
1286  *                                             to the user will *not* be trashed or deleted. If not set (the default),
1287  *                                             posts are trashed if post_type_supports('author'). Otherwise posts
1288  *                                             are not trashed or deleted. Default null.
1289  *     @type bool        $_builtin             FOR INTERNAL USE ONLY! True if this post type is a native or
1290  *                                             "built-in" post_type. Default false.
1291  *     @type string      $_edit_link           FOR INTERNAL USE ONLY! URL segment to use for edit link of
1292  *                                             this post type. Default 'post.php?post=%d'.
1293  * }
1294  * @return object|WP_Error The registered post type object, or an error object.
1295  */
1296 function register_post_type( $post_type, $args = array() ) {
1297         global $wp_post_types, $wp_rewrite, $wp;
1298
1299         if ( ! is_array( $wp_post_types ) )
1300                 $wp_post_types = array();
1301
1302         // Args prefixed with an underscore are reserved for internal use.
1303         $defaults = array(
1304                 'labels'               => array(),
1305                 'description'          => '',
1306                 'public'               => false,
1307                 'hierarchical'         => false,
1308                 'exclude_from_search'  => null,
1309                 'publicly_queryable'   => null,
1310                 'show_ui'              => null,
1311                 'show_in_menu'         => null,
1312                 'show_in_nav_menus'    => null,
1313                 'show_in_admin_bar'    => null,
1314                 'menu_position'        => null,
1315                 'menu_icon'            => null,
1316                 'capability_type'      => 'post',
1317                 'capabilities'         => array(),
1318                 'map_meta_cap'         => null,
1319                 'supports'             => array(),
1320                 'register_meta_box_cb' => null,
1321                 'taxonomies'           => array(),
1322                 'has_archive'          => false,
1323                 'rewrite'              => true,
1324                 'query_var'            => true,
1325                 'can_export'           => true,
1326                 'delete_with_user'     => null,
1327                 '_builtin'             => false,
1328                 '_edit_link'           => 'post.php?post=%d',
1329         );
1330         $args = wp_parse_args( $args, $defaults );
1331         $args = (object) $args;
1332
1333         $post_type = sanitize_key( $post_type );
1334         $args->name = $post_type;
1335
1336         if ( strlen( $post_type ) > 20 ) {
1337                 _doing_it_wrong( __FUNCTION__, __( 'Post types cannot exceed 20 characters in length' ), '4.0' );
1338                 return new WP_Error( 'post_type_too_long', __( 'Post types cannot exceed 20 characters in length' ) );
1339         }
1340
1341         // If not set, default to the setting for public.
1342         if ( null === $args->publicly_queryable )
1343                 $args->publicly_queryable = $args->public;
1344
1345         // If not set, default to the setting for public.
1346         if ( null === $args->show_ui )
1347                 $args->show_ui = $args->public;
1348
1349         // If not set, default to the setting for show_ui.
1350         if ( null === $args->show_in_menu || ! $args->show_ui )
1351                 $args->show_in_menu = $args->show_ui;
1352
1353         // If not set, default to the whether the full UI is shown.
1354         if ( null === $args->show_in_admin_bar )
1355                 $args->show_in_admin_bar = (bool) $args->show_in_menu;
1356
1357         // If not set, default to the setting for public.
1358         if ( null === $args->show_in_nav_menus )
1359                 $args->show_in_nav_menus = $args->public;
1360
1361         // If not set, default to true if not public, false if public.
1362         if ( null === $args->exclude_from_search )
1363                 $args->exclude_from_search = !$args->public;
1364
1365         // Back compat with quirky handling in version 3.0. #14122.
1366         if ( empty( $args->capabilities ) && null === $args->map_meta_cap && in_array( $args->capability_type, array( 'post', 'page' ) ) )
1367                 $args->map_meta_cap = true;
1368
1369         // If not set, default to false.
1370         if ( null === $args->map_meta_cap )
1371                 $args->map_meta_cap = false;
1372
1373         $args->cap = get_post_type_capabilities( $args );
1374         unset( $args->capabilities );
1375
1376         if ( is_array( $args->capability_type ) )
1377                 $args->capability_type = $args->capability_type[0];
1378
1379         if ( ! empty( $args->supports ) ) {
1380                 add_post_type_support( $post_type, $args->supports );
1381                 unset( $args->supports );
1382         } elseif ( false !== $args->supports ) {
1383                 // Add default features
1384                 add_post_type_support( $post_type, array( 'title', 'editor' ) );
1385         }
1386
1387         if ( false !== $args->query_var && ! empty( $wp ) ) {
1388                 if ( true === $args->query_var )
1389                         $args->query_var = $post_type;
1390                 else
1391                         $args->query_var = sanitize_title_with_dashes( $args->query_var );
1392                 $wp->add_query_var( $args->query_var );
1393         }
1394
1395         if ( false !== $args->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
1396                 if ( ! is_array( $args->rewrite ) )
1397                         $args->rewrite = array();
1398                 if ( empty( $args->rewrite['slug'] ) )
1399                         $args->rewrite['slug'] = $post_type;
1400                 if ( ! isset( $args->rewrite['with_front'] ) )
1401                         $args->rewrite['with_front'] = true;
1402                 if ( ! isset( $args->rewrite['pages'] ) )
1403                         $args->rewrite['pages'] = true;
1404                 if ( ! isset( $args->rewrite['feeds'] ) || ! $args->has_archive )
1405                         $args->rewrite['feeds'] = (bool) $args->has_archive;
1406                 if ( ! isset( $args->rewrite['ep_mask'] ) ) {
1407                         if ( isset( $args->permalink_epmask ) )
1408                                 $args->rewrite['ep_mask'] = $args->permalink_epmask;
1409                         else
1410                                 $args->rewrite['ep_mask'] = EP_PERMALINK;
1411                 }
1412
1413                 if ( $args->hierarchical )
1414                         add_rewrite_tag( "%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&pagename=" );
1415                 else
1416                         add_rewrite_tag( "%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=" );
1417
1418                 if ( $args->has_archive ) {
1419                         $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive;
1420                         if ( $args->rewrite['with_front'] )
1421                                 $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
1422                         else
1423                                 $archive_slug = $wp_rewrite->root . $archive_slug;
1424
1425                         add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$post_type", 'top' );
1426                         if ( $args->rewrite['feeds'] && $wp_rewrite->feeds ) {
1427                                 $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
1428                                 add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
1429                                 add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
1430                         }
1431                         if ( $args->rewrite['pages'] )
1432                                 add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&paged=$matches[1]', 'top' );
1433                 }
1434
1435                 $permastruct_args = $args->rewrite;
1436                 $permastruct_args['feed'] = $permastruct_args['feeds'];
1437                 add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $permastruct_args );
1438         }
1439
1440         // Register the post type meta box if a custom callback was specified.
1441         if ( $args->register_meta_box_cb )
1442                 add_action( 'add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1 );
1443
1444         $args->labels = get_post_type_labels( $args );
1445         $args->label = $args->labels->name;
1446
1447         $wp_post_types[ $post_type ] = $args;
1448
1449         add_action( 'future_' . $post_type, '_future_post_hook', 5, 2 );
1450
1451         foreach ( $args->taxonomies as $taxonomy ) {
1452                 register_taxonomy_for_object_type( $taxonomy, $post_type );
1453         }
1454
1455         /**
1456          * Fires after a post type is registered.
1457          *
1458          * @since 3.3.0
1459          *
1460          * @param string $post_type Post type.
1461          * @param object $args      Arguments used to register the post type.
1462          */
1463         do_action( 'registered_post_type', $post_type, $args );
1464
1465         return $args;
1466 }
1467
1468 /**
1469  * Build an object with all post type capabilities out of a post type object
1470  *
1471  * Post type capabilities use the 'capability_type' argument as a base, if the
1472  * capability is not set in the 'capabilities' argument array or if the
1473  * 'capabilities' argument is not supplied.
1474  *
1475  * The capability_type argument can optionally be registered as an array, with
1476  * the first value being singular and the second plural, e.g. array('story, 'stories')
1477  * Otherwise, an 's' will be added to the value for the plural form. After
1478  * registration, capability_type will always be a string of the singular value.
1479  *
1480  * By default, seven keys are accepted as part of the capabilities array:
1481  *
1482  * - edit_post, read_post, and delete_post are meta capabilities, which are then
1483  *   generally mapped to corresponding primitive capabilities depending on the
1484  *   context, which would be the post being edited/read/deleted and the user or
1485  *   role being checked. Thus these capabilities would generally not be granted
1486  *   directly to users or roles.
1487  *
1488  * - edit_posts - Controls whether objects of this post type can be edited.
1489  * - edit_others_posts - Controls whether objects of this type owned by other users
1490  *   can be edited. If the post type does not support an author, then this will
1491  *   behave like edit_posts.
1492  * - publish_posts - Controls publishing objects of this post type.
1493  * - read_private_posts - Controls whether private objects can be read.
1494  *
1495  * These four primitive capabilities are checked in core in various locations.
1496  * There are also seven other primitive capabilities which are not referenced
1497  * directly in core, except in map_meta_cap(), which takes the three aforementioned
1498  * meta capabilities and translates them into one or more primitive capabilities
1499  * that must then be checked against the user or role, depending on the context.
1500  *
1501  * - read - Controls whether objects of this post type can be read.
1502  * - delete_posts - Controls whether objects of this post type can be deleted.
1503  * - delete_private_posts - Controls whether private objects can be deleted.
1504  * - delete_published_posts - Controls whether published objects can be deleted.
1505  * - delete_others_posts - Controls whether objects owned by other users can be
1506  *   can be deleted. If the post type does not support an author, then this will
1507  *   behave like delete_posts.
1508  * - edit_private_posts - Controls whether private objects can be edited.
1509  * - edit_published_posts - Controls whether published objects can be edited.
1510  *
1511  * These additional capabilities are only used in map_meta_cap(). Thus, they are
1512  * only assigned by default if the post type is registered with the 'map_meta_cap'
1513  * argument set to true (default is false).
1514  *
1515  * @since 3.0.0
1516  *
1517  * @see register_post_type()
1518  * @see map_meta_cap()
1519  *
1520  * @param object $args Post type registration arguments.
1521  * @return object object with all the capabilities as member variables.
1522  */
1523 function get_post_type_capabilities( $args ) {
1524         if ( ! is_array( $args->capability_type ) )
1525                 $args->capability_type = array( $args->capability_type, $args->capability_type . 's' );
1526
1527         // Singular base for meta capabilities, plural base for primitive capabilities.
1528         list( $singular_base, $plural_base ) = $args->capability_type;
1529
1530         $default_capabilities = array(
1531                 // Meta capabilities
1532                 'edit_post'          => 'edit_'         . $singular_base,
1533                 'read_post'          => 'read_'         . $singular_base,
1534                 'delete_post'        => 'delete_'       . $singular_base,
1535                 // Primitive capabilities used outside of map_meta_cap():
1536                 'edit_posts'         => 'edit_'         . $plural_base,
1537                 'edit_others_posts'  => 'edit_others_'  . $plural_base,
1538                 'publish_posts'      => 'publish_'      . $plural_base,
1539                 'read_private_posts' => 'read_private_' . $plural_base,
1540         );
1541
1542         // Primitive capabilities used within map_meta_cap():
1543         if ( $args->map_meta_cap ) {
1544                 $default_capabilities_for_mapping = array(
1545                         'read'                   => 'read',
1546                         'delete_posts'           => 'delete_'           . $plural_base,
1547                         'delete_private_posts'   => 'delete_private_'   . $plural_base,
1548                         'delete_published_posts' => 'delete_published_' . $plural_base,
1549                         'delete_others_posts'    => 'delete_others_'    . $plural_base,
1550                         'edit_private_posts'     => 'edit_private_'     . $plural_base,
1551                         'edit_published_posts'   => 'edit_published_'   . $plural_base,
1552                 );
1553                 $default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping );
1554         }
1555
1556         $capabilities = array_merge( $default_capabilities, $args->capabilities );
1557
1558         // Post creation capability simply maps to edit_posts by default:
1559         if ( ! isset( $capabilities['create_posts'] ) )
1560                 $capabilities['create_posts'] = $capabilities['edit_posts'];
1561
1562         // Remember meta capabilities for future reference.
1563         if ( $args->map_meta_cap )
1564                 _post_type_meta_capabilities( $capabilities );
1565
1566         return (object) $capabilities;
1567 }
1568
1569 /**
1570  * Store or return a list of post type meta caps for map_meta_cap().
1571  *
1572  * @since 3.1.0
1573  * @access private
1574  *
1575  * @param null|array $capabilities Post type meta capabilities.
1576  */
1577 function _post_type_meta_capabilities( $capabilities = null ) {
1578         static $meta_caps = array();
1579         if ( null === $capabilities )
1580                 return $meta_caps;
1581         foreach ( $capabilities as $core => $custom ) {
1582                 if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) )
1583                         $meta_caps[ $custom ] = $core;
1584         }
1585 }
1586
1587 /**
1588  * Build an object with all post type labels out of a post type object
1589  *
1590  * Accepted keys of the label array in the post type object:
1591  *
1592  * - name - general name for the post type, usually plural. The same and overridden
1593  *          by $post_type_object->label. Default is Posts/Pages
1594  * - singular_name - name for one object of this post type. Default is Post/Page
1595  * - add_new - Default is Add New for both hierarchical and non-hierarchical types.
1596  *             When internationalizing this string, please use a gettext context
1597  *             {@link http://codex.wordpress.org/I18n_for_WordPress_Developers#Disambiguation_by_context}
1598  *             matching your post type. Example: `_x( 'Add New', 'product' );`.
1599  * - add_new_item - Default is Add New Post/Add New Page.
1600  * - edit_item - Default is Edit Post/Edit Page.
1601  * - new_item - Default is New Post/New Page.
1602  * - view_item - Default is View Post/View Page.
1603  * - search_items - Default is Search Posts/Search Pages.
1604  * - not_found - Default is No posts found/No pages found.
1605  * - not_found_in_trash - Default is No posts found in Trash/No pages found in Trash.
1606  * - parent_item_colon - This string isn't used on non-hierarchical types. In hierarchical
1607  *                       ones the default is 'Parent Page:'.
1608  * - all_items - String for the submenu. Default is All Posts/All Pages.
1609  * - menu_name - Default is the same as `name`.
1610  *
1611  * Above, the first default value is for non-hierarchical post types (like posts)
1612  * and the second one is for hierarchical post types (like pages).
1613  *
1614  * @since 3.0.0
1615  * @access private
1616  *
1617  * @param object $post_type_object Post type object.
1618  * @return object object with all the labels as member variables.
1619  */
1620 function get_post_type_labels( $post_type_object ) {
1621         $nohier_vs_hier_defaults = array(
1622                 'name' => array( _x('Posts', 'post type general name'), _x('Pages', 'post type general name') ),
1623                 'singular_name' => array( _x('Post', 'post type singular name'), _x('Page', 'post type singular name') ),
1624                 'add_new' => array( _x('Add New', 'post'), _x('Add New', 'page') ),
1625                 'add_new_item' => array( __('Add New Post'), __('Add New Page') ),
1626                 'edit_item' => array( __('Edit Post'), __('Edit Page') ),
1627                 'new_item' => array( __('New Post'), __('New Page') ),
1628                 'view_item' => array( __('View Post'), __('View Page') ),
1629                 'search_items' => array( __('Search Posts'), __('Search Pages') ),
1630                 'not_found' => array( __('No posts found.'), __('No pages found.') ),
1631                 'not_found_in_trash' => array( __('No posts found in Trash.'), __('No pages found in Trash.') ),
1632                 'parent_item_colon' => array( null, __('Parent Page:') ),
1633                 'all_items' => array( __( 'All Posts' ), __( 'All Pages' ) )
1634         );
1635         $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
1636
1637         $labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
1638
1639         $post_type = $post_type_object->name;
1640
1641         /**
1642          * Filter the labels of a specific post type.
1643          *
1644          * The dynamic portion of the hook name, `$post_type`, refers to
1645          * the post type slug.
1646          *
1647          * @since 3.5.0
1648          *
1649          * @see get_post_type_labels() for the full list of labels.
1650          *
1651          * @param array $labels Array of labels for the given post type.
1652          */
1653         return apply_filters( "post_type_labels_{$post_type}", $labels );
1654 }
1655
1656 /**
1657  * Build an object with custom-something object (post type, taxonomy) labels
1658  * out of a custom-something object
1659  *
1660  * @since 3.0.0
1661  * @access private
1662  *
1663  * @param object $object                  A custom-something object.
1664  * @param array  $nohier_vs_hier_defaults Hierarchical vs non-hierarchical default labels.
1665  */
1666 function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) {
1667         $object->labels = (array) $object->labels;
1668
1669         if ( isset( $object->label ) && empty( $object->labels['name'] ) )
1670                 $object->labels['name'] = $object->label;
1671
1672         if ( !isset( $object->labels['singular_name'] ) && isset( $object->labels['name'] ) )
1673                 $object->labels['singular_name'] = $object->labels['name'];
1674
1675         if ( ! isset( $object->labels['name_admin_bar'] ) )
1676                 $object->labels['name_admin_bar'] = isset( $object->labels['singular_name'] ) ? $object->labels['singular_name'] : $object->name;
1677
1678         if ( !isset( $object->labels['menu_name'] ) && isset( $object->labels['name'] ) )
1679                 $object->labels['menu_name'] = $object->labels['name'];
1680
1681         if ( !isset( $object->labels['all_items'] ) && isset( $object->labels['menu_name'] ) )
1682                 $object->labels['all_items'] = $object->labels['menu_name'];
1683
1684         foreach ( $nohier_vs_hier_defaults as $key => $value )
1685                         $defaults[$key] = $object->hierarchical ? $value[1] : $value[0];
1686
1687         $labels = array_merge( $defaults, $object->labels );
1688         return (object)$labels;
1689 }
1690
1691 /**
1692  * Add submenus for post types.
1693  *
1694  * @access private
1695  * @since 3.1.0
1696  */
1697 function _add_post_type_submenus() {
1698         foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) {
1699                 $ptype_obj = get_post_type_object( $ptype );
1700                 // Sub-menus only.
1701                 if ( ! $ptype_obj->show_in_menu || $ptype_obj->show_in_menu === true )
1702                         continue;
1703                 add_submenu_page( $ptype_obj->show_in_menu, $ptype_obj->labels->name, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype" );
1704         }
1705 }
1706 add_action( 'admin_menu', '_add_post_type_submenus' );
1707
1708 /**
1709  * Register support of certain features for a post type.
1710  *
1711  * All core features are directly associated with a functional area of the edit
1712  * screen, such as the editor or a meta box. Features include: 'title', 'editor',
1713  * 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes',
1714  * 'thumbnail', 'custom-fields', and 'post-formats'.
1715  *
1716  * Additionally, the 'revisions' feature dictates whether the post type will
1717  * store revisions, and the 'comments' feature dictates whether the comments
1718  * count will show on the edit screen.
1719  *
1720  * @since 3.0.0
1721  *
1722  * @param string       $post_type The post type for which to add the feature.
1723  * @param string|array $feature   The feature being added, accepts an array of
1724  *                                feature strings or a single string.
1725  */
1726 function add_post_type_support( $post_type, $feature ) {
1727         global $_wp_post_type_features;
1728
1729         $features = (array) $feature;
1730         foreach ($features as $feature) {
1731                 if ( func_num_args() == 2 )
1732                         $_wp_post_type_features[$post_type][$feature] = true;
1733                 else
1734                         $_wp_post_type_features[$post_type][$feature] = array_slice( func_get_args(), 2 );
1735         }
1736 }
1737
1738 /**
1739  * Remove support for a feature from a post type.
1740  *
1741  * @since 3.0.0
1742  *
1743  * @param string $post_type The post type for which to remove the feature.
1744  * @param string $feature   The feature being removed.
1745  */
1746 function remove_post_type_support( $post_type, $feature ) {
1747         global $_wp_post_type_features;
1748
1749         if ( isset( $_wp_post_type_features[$post_type][$feature] ) )
1750                 unset( $_wp_post_type_features[$post_type][$feature] );
1751 }
1752
1753 /**
1754  * Get all the post type features
1755  *
1756  * @since 3.4.0
1757  *
1758  * @param string $post_type The post type.
1759  * @return array Post type supports list.
1760  */
1761 function get_all_post_type_supports( $post_type ) {
1762         global $_wp_post_type_features;
1763
1764         if ( isset( $_wp_post_type_features[$post_type] ) )
1765                 return $_wp_post_type_features[$post_type];
1766
1767         return array();
1768 }
1769
1770 /**
1771  * Check a post type's support for a given feature.
1772  *
1773  * @since 3.0.0
1774  *
1775  * @param string $post_type The post type being checked.
1776  * @param string $feature the feature being checked.
1777  * @return bool Whether the post type supports the given feature.
1778  */
1779 function post_type_supports( $post_type, $feature ) {
1780         global $_wp_post_type_features;
1781
1782         return ( isset( $_wp_post_type_features[$post_type][$feature] ) );
1783 }
1784
1785 /**
1786  * Update the post type for the post ID.
1787  *
1788  * The page or post cache will be cleaned for the post ID.
1789  *
1790  * @since 2.5.0
1791  *
1792  * @global wpdb $wpdb WordPress database abstraction object.
1793  *
1794  * @param int    $post_id   Optional. Post ID to change post type. Default 0.
1795  * @param string $post_type Optional. Post type. Accepts 'post' or 'page' to
1796  *                          name a few. Default 'post'.
1797  * @return int Amount of rows changed. Should be 1 for success and 0 for failure.
1798  */
1799 function set_post_type( $post_id = 0, $post_type = 'post' ) {
1800         global $wpdb;
1801
1802         $post_type = sanitize_post_field('post_type', $post_type, $post_id, 'db');
1803         $return = $wpdb->update( $wpdb->posts, array('post_type' => $post_type), array('ID' => $post_id) );
1804
1805         clean_post_cache( $post_id );
1806
1807         return $return;
1808 }
1809
1810 /**
1811  * Retrieve list of latest posts or posts matching criteria.
1812  *
1813  * The defaults are as follows:
1814  *
1815  * @since 1.2.0
1816  *
1817  * @see WP_Query::parse_query()
1818  *
1819  * @param array $args {
1820  *     Optional. Arguments to retrieve posts. {@see WP_Query::parse_query()} for more
1821  *     available arguments.
1822  *
1823  *     @type int        $numberposts      Total number of posts to retrieve. Is an alias of $posts_per_page
1824  *                                        in WP_Query. Accepts 1+ and -1 for all. Default 5.
1825  *     @type int        $offset           The number of posts to offset before retrieval. Default 0.
1826  *     @type int|string $category         Category ID or comma-separated list of IDs (this or any children).
1827  *                                        Is an alias of $cat in WP_Query. Default 0.
1828  *     @type string     $orderby          Which field to order posts by. Accepts post fields. Default 'date'.
1829  *     @type array      $include          An array of post IDs to retrieve, sticky posts will be included.
1830  *                                        Is an alias of $post__in in WP_Query. Default empty array.
1831  *     @type array      $exclude          An array of post IDs not to retrieve. Default empty array.
1832  *     @type string     $meta_key         Custom field key. Default empty.
1833  *     @type mixed      $meta_value       Custom field value. Default empty string.
1834  *     @type string     $post_type        Post type. Default 'post'.
1835  *     @type bool       $suppress_filters Whether to suppress filters. Default true.
1836  * }
1837  * @return array List of posts.
1838  */
1839 function get_posts( $args = null ) {
1840         $defaults = array(
1841                 'numberposts' => 5, 'offset' => 0,
1842                 'category' => 0, 'orderby' => 'date',
1843                 'order' => 'DESC', 'include' => array(),
1844                 'exclude' => array(), 'meta_key' => '',
1845                 'meta_value' =>'', 'post_type' => 'post',
1846                 'suppress_filters' => true
1847         );
1848
1849         $r = wp_parse_args( $args, $defaults );
1850         if ( empty( $r['post_status'] ) )
1851                 $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish';
1852         if ( ! empty($r['numberposts']) && empty($r['posts_per_page']) )
1853                 $r['posts_per_page'] = $r['numberposts'];
1854         if ( ! empty($r['category']) )
1855                 $r['cat'] = $r['category'];
1856         if ( ! empty($r['include']) ) {
1857                 $incposts = wp_parse_id_list( $r['include'] );
1858                 $r['posts_per_page'] = count($incposts);  // only the number of posts included
1859                 $r['post__in'] = $incposts;
1860         } elseif ( ! empty($r['exclude']) )
1861                 $r['post__not_in'] = wp_parse_id_list( $r['exclude'] );
1862
1863         $r['ignore_sticky_posts'] = true;
1864         $r['no_found_rows'] = true;
1865
1866         $get_posts = new WP_Query;
1867         return $get_posts->query($r);
1868
1869 }
1870
1871 //
1872 // Post meta functions
1873 //
1874
1875 /**
1876  * Add meta data field to a post.
1877  *
1878  * Post meta data is called "Custom Fields" on the Administration Screen.
1879  *
1880  * @since 1.5.0
1881  *
1882  * @param int    $post_id    Post ID.
1883  * @param string $meta_key   Metadata name.
1884  * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
1885  * @param bool   $unique     Optional. Whether the same key should not be added.
1886  *                           Default false.
1887  * @return int|bool Meta ID on success, false on failure.
1888  */
1889 function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) {
1890         // Make sure meta is added to the post, not a revision.
1891         if ( $the_post = wp_is_post_revision($post_id) )
1892                 $post_id = $the_post;
1893
1894         return add_metadata('post', $post_id, $meta_key, $meta_value, $unique);
1895 }
1896
1897 /**
1898  * Remove metadata matching criteria from a post.
1899  *
1900  * You can match based on the key, or key and value. Removing based on key and
1901  * value, will keep from removing duplicate metadata with the same key. It also
1902  * allows removing all metadata matching key, if needed.
1903  *
1904  * @since 1.5.0
1905  *
1906  * @param int    $post_id    Post ID.
1907  * @param string $meta_key   Metadata name.
1908  * @param mixed  $meta_value Optional. Metadata value. Must be serializable if
1909  *                           non-scalar. Default empty.
1910  * @return bool True on success, false on failure.
1911  */
1912 function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) {
1913         // Make sure meta is added to the post, not a revision.
1914         if ( $the_post = wp_is_post_revision($post_id) )
1915                 $post_id = $the_post;
1916
1917         return delete_metadata('post', $post_id, $meta_key, $meta_value);
1918 }
1919
1920 /**
1921  * Retrieve post meta field for a post.
1922  *
1923  * @since 1.5.0
1924  *
1925  * @param int    $post_id Post ID.
1926  * @param string $key     Optional. The meta key to retrieve. By default, returns
1927  *                        data for all keys. Default empty.
1928  * @param bool   $single  Optional. Whether to return a single value. Default false.
1929  * @return mixed Will be an array if $single is false. Will be value of meta data
1930  *               field if $single is true.
1931  */
1932 function get_post_meta( $post_id, $key = '', $single = false ) {
1933         return get_metadata('post', $post_id, $key, $single);
1934 }
1935
1936 /**
1937  * Update post meta field based on post ID.
1938  *
1939  * Use the $prev_value parameter to differentiate between meta fields with the
1940  * same key and post ID.
1941  *
1942  * If the meta field for the post does not exist, it will be added.
1943  *
1944  * @since 1.5.0
1945  *
1946  * @param int    $post_id    Post ID.
1947  * @param string $meta_key   Metadata key.
1948  * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
1949  * @param mixed  $prev_value Optional. Previous value to check before removing.
1950  *                           Default empty.
1951  * @return int|bool Meta ID if the key didn't exist, true on successful update,
1952  *                  false on failure.
1953  */
1954 function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) {
1955         // Make sure meta is added to the post, not a revision.
1956         if ( $the_post = wp_is_post_revision($post_id) )
1957                 $post_id = $the_post;
1958
1959         return update_metadata('post', $post_id, $meta_key, $meta_value, $prev_value);
1960 }
1961
1962 /**
1963  * Delete everything from post meta matching meta key.
1964  *
1965  * @since 2.3.0
1966  *
1967  * @param string $post_meta_key Key to search for when deleting.
1968  * @return bool Whether the post meta key was deleted from the database.
1969  */
1970 function delete_post_meta_by_key( $post_meta_key ) {
1971         return delete_metadata( 'post', null, $post_meta_key, '', true );
1972 }
1973
1974 /**
1975  * Retrieve post meta fields, based on post ID.
1976  *
1977  * The post meta fields are retrieved from the cache where possible,
1978  * so the function is optimized to be called more than once.
1979  *
1980  * @since 1.2.0
1981  *
1982  * @param int $post_id Optional. Post ID. Default is ID of the global $post.
1983  * @return array Post meta for the given post.
1984  */
1985 function get_post_custom( $post_id = 0 ) {
1986         $post_id = absint( $post_id );
1987         if ( ! $post_id )
1988                 $post_id = get_the_ID();
1989
1990         return get_post_meta( $post_id );
1991 }
1992
1993 /**
1994  * Retrieve meta field names for a post.
1995  *
1996  * If there are no meta fields, then nothing (null) will be returned.
1997  *
1998  * @since 1.2.0
1999  *
2000  * @param int $post_id Optional. Post ID. Default is ID of the global $post.
2001  * @return array|null Either array of the keys, or null if keys could not be
2002  *                    retrieved.
2003  */
2004 function get_post_custom_keys( $post_id = 0 ) {
2005         $custom = get_post_custom( $post_id );
2006
2007         if ( !is_array($custom) )
2008                 return;
2009
2010         if ( $keys = array_keys($custom) )
2011                 return $keys;
2012 }
2013
2014 /**
2015  * Retrieve values for a custom post field.
2016  *
2017  * The parameters must not be considered optional. All of the post meta fields
2018  * will be retrieved and only the meta field key values returned.
2019  *
2020  * @since 1.2.0
2021  *
2022  * @param string $key     Optional. Meta field key. Default empty.
2023  * @param int    $post_id Optional. Post ID. Default is ID of the global $post.
2024  * @return array Meta field values.
2025  */
2026 function get_post_custom_values( $key = '', $post_id = 0 ) {
2027         if ( !$key )
2028                 return null;
2029
2030         $custom = get_post_custom($post_id);
2031
2032         return isset($custom[$key]) ? $custom[$key] : null;
2033 }
2034
2035 /**
2036  * Check if post is sticky.
2037  *
2038  * Sticky posts should remain at the top of The Loop. If the post ID is not
2039  * given, then The Loop ID for the current post will be used.
2040  *
2041  * @since 2.7.0
2042  *
2043  * @param int $post_id Optional. Post ID. Default is ID of the global $post.
2044  * @return bool Whether post is sticky.
2045  */
2046 function is_sticky( $post_id = 0 ) {
2047         $post_id = absint( $post_id );
2048
2049         if ( ! $post_id )
2050                 $post_id = get_the_ID();
2051
2052         $stickies = get_option( 'sticky_posts' );
2053
2054         if ( ! is_array( $stickies ) )
2055                 return false;
2056
2057         if ( in_array( $post_id, $stickies ) )
2058                 return true;
2059
2060         return false;
2061 }
2062
2063 /**
2064  * Sanitize every post field.
2065  *
2066  * If the context is 'raw', then the post object or array will get minimal
2067  * sanitization of the integer fields.
2068  *
2069  * @since 2.3.0
2070  *
2071  * @see sanitize_post_field()
2072  *
2073  * @param object|WP_Post|array $post    The Post Object or Array
2074  * @param string               $context Optional. How to sanitize post fields.
2075  *                                      Accepts 'raw', 'edit', 'db', or 'display'.
2076  *                                      Default 'display'.
2077  * @return object|WP_Post|array The now sanitized Post Object or Array (will be the
2078  *                              same type as $post).
2079  */
2080 function sanitize_post( $post, $context = 'display' ) {
2081         if ( is_object($post) ) {
2082                 // Check if post already filtered for this context.
2083                 if ( isset($post->filter) && $context == $post->filter )
2084                         return $post;
2085                 if ( !isset($post->ID) )
2086                         $post->ID = 0;
2087                 foreach ( array_keys(get_object_vars($post)) as $field )
2088                         $post->$field = sanitize_post_field($field, $post->$field, $post->ID, $context);
2089                 $post->filter = $context;
2090         } else {
2091                 // Check if post already filtered for this context.
2092                 if ( isset($post['filter']) && $context == $post['filter'] )
2093                         return $post;
2094                 if ( !isset($post['ID']) )
2095                         $post['ID'] = 0;
2096                 foreach ( array_keys($post) as $field )
2097                         $post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context);
2098                 $post['filter'] = $context;
2099         }
2100         return $post;
2101 }
2102
2103 /**
2104  * Sanitize post field based on context.
2105  *
2106  * Possible context values are:  'raw', 'edit', 'db', 'display', 'attribute' and
2107  * 'js'. The 'display' context is used by default. 'attribute' and 'js' contexts
2108  * are treated like 'display' when calling filters.
2109  *
2110  * @since 2.3.0
2111  *
2112  * @param string $field   The Post Object field name.
2113  * @param mixed  $value   The Post Object value.
2114  * @param int    $post_id Post ID.
2115  * @param string $context How to sanitize post fields. Looks for 'raw', 'edit',
2116  *                        'db', 'display', 'attribute' and 'js'.
2117  * @return mixed Sanitized value.
2118  */
2119 function sanitize_post_field($field, $value, $post_id, $context) {
2120         $int_fields = array('ID', 'post_parent', 'menu_order');
2121         if ( in_array($field, $int_fields) )
2122                 $value = (int) $value;
2123
2124         // Fields which contain arrays of integers.
2125         $array_int_fields = array( 'ancestors' );
2126         if ( in_array($field, $array_int_fields) ) {
2127                 $value = array_map( 'absint', $value);
2128                 return $value;
2129         }
2130
2131         if ( 'raw' == $context )
2132                 return $value;
2133
2134         $prefixed = false;
2135         if ( false !== strpos($field, 'post_') ) {
2136                 $prefixed = true;
2137                 $field_no_prefix = str_replace('post_', '', $field);
2138         }
2139
2140         if ( 'edit' == $context ) {
2141                 $format_to_edit = array('post_content', 'post_excerpt', 'post_title', 'post_password');
2142
2143                 if ( $prefixed ) {
2144
2145                         /**
2146                          * Filter the value of a specific post field to edit.
2147                          *
2148                          * The dynamic portion of the hook name, `$field`, refers to the post
2149                          * field name.
2150                          *
2151                          * @since 2.3.0
2152                          *
2153                          * @param mixed $value   Value of the post field.
2154                          * @param int   $post_id Post ID.
2155                          */
2156                         $value = apply_filters( "edit_{$field}", $value, $post_id );
2157
2158                         /**
2159                          * Filter the value of a specific post field to edit.
2160                          *
2161                          * The dynamic portion of the hook name, `$field_no_prefix`, refers to
2162                          * the post field name.
2163                          *
2164                          * @since 2.3.0
2165                          *
2166                          * @param mixed $value   Value of the post field.
2167                          * @param int   $post_id Post ID.
2168                          */
2169                         $value = apply_filters( "{$field_no_prefix}_edit_pre", $value, $post_id );
2170                 } else {
2171                         $value = apply_filters( "edit_post_{$field}", $value, $post_id );
2172                 }
2173
2174                 if ( in_array($field, $format_to_edit) ) {
2175                         if ( 'post_content' == $field )
2176                                 $value = format_to_edit($value, user_can_richedit());
2177                         else
2178                                 $value = format_to_edit($value);
2179                 } else {
2180                         $value = esc_attr($value);
2181                 }
2182         } else if ( 'db' == $context ) {
2183                 if ( $prefixed ) {
2184
2185                         /**
2186                          * Filter the value of a specific post field before saving.
2187                          *
2188                          * The dynamic portion of the hook name, `$field`, refers to the post
2189                          * field name.
2190                          *
2191                          * @since 2.3.0
2192                          *
2193                          * @param mixed $value Value of the post field.
2194                          */
2195                         $value = apply_filters( "pre_{$field}", $value );
2196
2197                         /**
2198                          * Filter the value of a specific field before saving.
2199                          *
2200                          * The dynamic portion of the hook name, `$field_no_prefix`, refers
2201                          * to the post field name.
2202                          *
2203                          * @since 2.3.0
2204                          *
2205                          * @param mixed $value Value of the post field.
2206                          */
2207                         $value = apply_filters( "{$field_no_prefix}_save_pre", $value );
2208                 } else {
2209                         $value = apply_filters( "pre_post_{$field}", $value );
2210
2211                         /**
2212                          * Filter the value of a specific post field before saving.
2213                          *
2214                          * The dynamic portion of the hook name, `$field`, refers to the post
2215                          * field name.
2216                          *
2217                          * @since 2.3.0
2218                          *
2219                          * @param mixed $value Value of the post field.
2220                          */
2221                         $value = apply_filters( "{$field}_pre", $value );
2222                 }
2223         } else {
2224
2225                 // Use display filters by default.
2226                 if ( $prefixed ) {
2227
2228                         /**
2229                          * Filter the value of a specific post field for display.
2230                          *
2231                          * The dynamic portion of the hook name, `$field`, refers to the post
2232                          * field name.
2233                          *
2234                          * @since 2.3.0
2235                          *
2236                          * @param mixed  $value   Value of the prefixed post field.
2237                          * @param int    $post_id Post ID.
2238                          * @param string $context Context for how to sanitize the field. Possible
2239                          *                        values include 'raw', 'edit', 'db', 'display',
2240                          *                        'attribute' and 'js'.
2241                          */
2242                         $value = apply_filters( $field, $value, $post_id, $context );
2243                 } else {
2244                         $value = apply_filters( "post_{$field}", $value, $post_id, $context );
2245                 }
2246         }
2247
2248         if ( 'attribute' == $context )
2249                 $value = esc_attr($value);
2250         else if ( 'js' == $context )
2251                 $value = esc_js($value);
2252
2253         return $value;
2254 }
2255
2256 /**
2257  * Make a post sticky.
2258  *
2259  * Sticky posts should be displayed at the top of the front page.
2260  *
2261  * @since 2.7.0
2262  *
2263  * @param int $post_id Post ID.
2264  */
2265 function stick_post( $post_id ) {
2266         $stickies = get_option('sticky_posts');
2267
2268         if ( !is_array($stickies) )
2269                 $stickies = array($post_id);
2270
2271         if ( ! in_array($post_id, $stickies) )
2272                 $stickies[] = $post_id;
2273
2274         update_option('sticky_posts', $stickies);
2275 }
2276
2277 /**
2278  * Un-stick a post.
2279  *
2280  * Sticky posts should be displayed at the top of the front page.
2281  *
2282  * @since 2.7.0
2283  *
2284  * @param int $post_id Post ID.
2285  */
2286 function unstick_post( $post_id ) {
2287         $stickies = get_option('sticky_posts');
2288
2289         if ( !is_array($stickies) )
2290                 return;
2291
2292         if ( ! in_array($post_id, $stickies) )
2293                 return;
2294
2295         $offset = array_search($post_id, $stickies);
2296         if ( false === $offset )
2297                 return;
2298
2299         array_splice($stickies, $offset, 1);
2300
2301         update_option('sticky_posts', $stickies);
2302 }
2303
2304 /**
2305  * Return the cache key for wp_count_posts() based on the passed arguments.
2306  *
2307  * @since 3.9.0
2308  *
2309  * @param string $type Optional. Post type to retrieve count Default 'post'.
2310  * @param string $perm Optional. 'readable' or empty. Default empty.
2311  * @return string The cache key.
2312  */
2313 function _count_posts_cache_key( $type = 'post', $perm = '' ) {
2314         $cache_key = 'posts-' . $type;
2315         if ( 'readable' == $perm && is_user_logged_in() ) {
2316                 $post_type_object = get_post_type_object( $type );
2317                 if ( $post_type_object && ! current_user_can( $post_type_object->cap->read_private_posts ) ) {
2318                         $cache_key .= '_' . $perm . '_' . get_current_user_id();
2319                 }
2320         }
2321         return $cache_key;
2322 }
2323
2324 /**
2325  * Count number of posts of a post type and if user has permissions to view.
2326  *
2327  * This function provides an efficient method of finding the amount of post's
2328  * type a blog has. Another method is to count the amount of items in
2329  * get_posts(), but that method has a lot of overhead with doing so. Therefore,
2330  * when developing for 2.5+, use this function instead.
2331  *
2332  * The $perm parameter checks for 'readable' value and if the user can read
2333  * private posts, it will display that for the user that is signed in.
2334  *
2335  * @since 2.5.0
2336  *
2337  * @param string $type Optional. Post type to retrieve count. Default 'post'.
2338  * @param string $perm Optional. 'readable' or empty. Default empty.
2339  * @return object Number of posts for each status.
2340  */
2341 function wp_count_posts( $type = 'post', $perm = '' ) {
2342         global $wpdb;
2343
2344         if ( ! post_type_exists( $type ) )
2345                 return new stdClass;
2346
2347         $cache_key = _count_posts_cache_key( $type, $perm );
2348
2349         $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
2350         if ( 'readable' == $perm && is_user_logged_in() ) {
2351                 $post_type_object = get_post_type_object($type);
2352                 if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) {
2353                         $query .= $wpdb->prepare( " AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))",
2354                                 get_current_user_id()
2355                         );
2356                 }
2357         }
2358         $query .= ' GROUP BY post_status';
2359
2360         $counts = wp_cache_get( $cache_key, 'counts' );
2361         if ( false === $counts ) {
2362                 $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
2363                 $counts = array_fill_keys( get_post_stati(), 0 );
2364
2365                 foreach ( $results as $row )
2366                         $counts[ $row['post_status'] ] = $row['num_posts'];
2367
2368                 $counts = (object) $counts;
2369                 wp_cache_set( $cache_key, $counts, 'counts' );
2370         }
2371
2372         /**
2373          * Modify returned post counts by status for the current post type.
2374          *
2375          * @since 3.7.0
2376          *
2377          * @param object $counts An object containing the current post_type's post
2378          *                       counts by status.
2379          * @param string $type   Post type.
2380          * @param string $perm   The permission to determine if the posts are 'readable'
2381          *                       by the current user.
2382          */
2383         return apply_filters( 'wp_count_posts', $counts, $type, $perm );
2384 }
2385
2386 /**
2387  * Count number of attachments for the mime type(s).
2388  *
2389  * If you set the optional mime_type parameter, then an array will still be
2390  * returned, but will only have the item you are looking for. It does not give
2391  * you the number of attachments that are children of a post. You can get that
2392  * by counting the number of children that post has.
2393  *
2394  * @since 2.5.0
2395  *
2396  * @param string|array $mime_type Optional. Array or comma-separated list of
2397  *                                MIME patterns. Default empty.
2398  * @return object An object containing the attachment counts by mime type.
2399  */
2400 function wp_count_attachments( $mime_type = '' ) {
2401         global $wpdb;
2402
2403         $and = wp_post_mime_type_where( $mime_type );
2404         $count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' $and GROUP BY post_mime_type", ARRAY_A );
2405
2406         $counts = array();
2407         foreach( (array) $count as $row ) {
2408                 $counts[ $row['post_mime_type'] ] = $row['num_posts'];
2409         }
2410         $counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and");
2411
2412         /**
2413          * Modify returned attachment counts by mime type.
2414          *
2415          * @since 3.7.0
2416          *
2417          * @param object $counts    An object containing the attachment counts by
2418          *                          mime type.
2419          * @param string $mime_type The mime type pattern used to filter the attachments
2420          *                          counted.
2421          */
2422         return apply_filters( 'wp_count_attachments', (object) $counts, $mime_type );
2423 }
2424
2425 /**
2426  * Get default post mime types.
2427  *
2428  * @since 2.9.0
2429  *
2430  * @return array List of post mime types.
2431  */
2432 function get_post_mime_types() {
2433         $post_mime_types = array(       //      array( adj, noun )
2434                 'image' => array(__('Images'), __('Manage Images'), _n_noop('Image <span class="count">(%s)</span>', 'Images <span class="count">(%s)</span>')),
2435                 'audio' => array(__('Audio'), __('Manage Audio'), _n_noop('Audio <span class="count">(%s)</span>', 'Audio <span class="count">(%s)</span>')),
2436                 'video' => array(__('Video'), __('Manage Video'), _n_noop('Video <span class="count">(%s)</span>', 'Video <span class="count">(%s)</span>')),
2437         );
2438
2439         /**
2440          * Filter the default list of post mime types.
2441          *
2442          * @since 2.5.0
2443          *
2444          * @param array $post_mime_types Default list of post mime types.
2445          */
2446         return apply_filters( 'post_mime_types', $post_mime_types );
2447 }
2448
2449 /**
2450  * Check a MIME-Type against a list.
2451  *
2452  * If the wildcard_mime_types parameter is a string, it must be comma separated
2453  * list. If the real_mime_types is a string, it is also comma separated to
2454  * create the list.
2455  *
2456  * @since 2.5.0
2457  *
2458  * @param string|array $wildcard_mime_types Mime types, e.g. audio/mpeg or image (same as image/*)
2459  *                                          or flash (same as *flash*).
2460  * @param string|array $real_mime_types     Real post mime type values.
2461  * @return array array(wildcard=>array(real types)).
2462  */
2463 function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) {
2464         $matches = array();
2465         if ( is_string( $wildcard_mime_types ) ) {
2466                 $wildcard_mime_types = array_map( 'trim', explode( ',', $wildcard_mime_types ) );
2467         }
2468         if ( is_string( $real_mime_types ) ) {
2469                 $real_mime_types = array_map( 'trim', explode( ',', $real_mime_types ) );
2470         }
2471
2472         $patternses = array();
2473         $wild = '[-._a-z0-9]*';
2474
2475         foreach ( (array) $wildcard_mime_types as $type ) {
2476                 $regex = str_replace( '__wildcard__', $wild, preg_quote( str_replace( '*', '__wildcard__', $type ) ) );
2477                 $patternses[1][$type] = "^$regex$";
2478                 if ( false === strpos($type, '/') ) {
2479                         $patternses[2][$type] = "^$regex/";
2480                         $patternses[3][$type] = $regex;
2481                 }
2482         }
2483         asort( $patternses );
2484
2485         foreach ( $patternses as $patterns ) {
2486                 foreach ( $patterns as $type => $pattern ) {
2487                         foreach ( (array) $real_mime_types as $real ) {
2488                                 if ( preg_match( "#$pattern#", $real ) && ( empty( $matches[$type] ) || false === array_search( $real, $matches[$type] ) ) ) {
2489                                         $matches[$type][] = $real;
2490                                 }
2491                         }
2492                 }
2493         }
2494         return $matches;
2495 }
2496
2497 /**
2498  * Convert MIME types into SQL.
2499  *
2500  * @since 2.5.0
2501  *
2502  * @param string|array $post_mime_types List of mime types or comma separated string
2503  *                                      of mime types.
2504  * @param string       $table_alias     Optional. Specify a table alias, if needed.
2505  *                                      Default empty.
2506  * @return string The SQL AND clause for mime searching.
2507  */
2508 function wp_post_mime_type_where( $post_mime_types, $table_alias = '' ) {
2509         $where = '';
2510         $wildcards = array('', '%', '%/%');
2511         if ( is_string($post_mime_types) )
2512                 $post_mime_types = array_map('trim', explode(',', $post_mime_types));
2513         foreach ( (array) $post_mime_types as $mime_type ) {
2514                 $mime_type = preg_replace('/\s/', '', $mime_type);
2515                 $slashpos = strpos($mime_type, '/');
2516                 if ( false !== $slashpos ) {
2517                         $mime_group = preg_replace('/[^-*.a-zA-Z0-9]/', '', substr($mime_type, 0, $slashpos));
2518                         $mime_subgroup = preg_replace('/[^-*.+a-zA-Z0-9]/', '', substr($mime_type, $slashpos + 1));
2519                         if ( empty($mime_subgroup) )
2520                                 $mime_subgroup = '*';
2521                         else
2522                                 $mime_subgroup = str_replace('/', '', $mime_subgroup);
2523                         $mime_pattern = "$mime_group/$mime_subgroup";
2524                 } else {
2525                         $mime_pattern = preg_replace('/[^-*.a-zA-Z0-9]/', '', $mime_type);
2526                         if ( false === strpos($mime_pattern, '*') )
2527                                 $mime_pattern .= '/*';
2528                 }
2529
2530                 $mime_pattern = preg_replace('/\*+/', '%', $mime_pattern);
2531
2532                 if ( in_array( $mime_type, $wildcards ) )
2533                         return '';
2534
2535                 if ( false !== strpos($mime_pattern, '%') )
2536                         $wheres[] = empty($table_alias) ? "post_mime_type LIKE '$mime_pattern'" : "$table_alias.post_mime_type LIKE '$mime_pattern'";
2537                 else
2538                         $wheres[] = empty($table_alias) ? "post_mime_type = '$mime_pattern'" : "$table_alias.post_mime_type = '$mime_pattern'";
2539         }
2540         if ( !empty($wheres) )
2541                 $where = ' AND (' . join(' OR ', $wheres) . ') ';
2542         return $where;
2543 }
2544
2545 /**
2546  * Trash or delete a post or page.
2547  *
2548  * When the post and page is permanently deleted, everything that is tied to
2549  * it is deleted also. This includes comments, post meta fields, and terms
2550  * associated with the post.
2551  *
2552  * The post or page is moved to trash instead of permanently deleted unless
2553  * trash is disabled, item is already in the trash, or $force_delete is true.
2554  *
2555  * @since 1.0.0
2556  *
2557  * @global wpdb $wpdb WordPress database abstraction object.
2558  * @see wp_delete_attachment()
2559  * @see wp_trash_post()
2560  *
2561  * @param int  $postid       Optional. Post ID. Default 0.
2562  * @param bool $force_delete Optional. Whether to bypass trash and force deletion.
2563  *                           Default false.
2564  * @return array|bool|WP_Post False on failure.
2565  */
2566 function wp_delete_post( $postid = 0, $force_delete = false ) {
2567         global $wpdb;
2568
2569         if ( !$post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $postid)) )
2570                 return $post;
2571
2572         if ( !$force_delete && ( $post->post_type == 'post' || $post->post_type == 'page') && get_post_status( $postid ) != 'trash' && EMPTY_TRASH_DAYS )
2573                         return wp_trash_post($postid);
2574
2575         if ( $post->post_type == 'attachment' )
2576                 return wp_delete_attachment( $postid, $force_delete );
2577
2578         /**
2579          * Fires before a post is deleted, at the start of wp_delete_post().
2580          *
2581          * @since 3.2.0
2582          *
2583          * @see wp_delete_post()
2584          *
2585          * @param int $postid Post ID.
2586          */
2587         do_action( 'before_delete_post', $postid );
2588
2589         delete_post_meta($postid,'_wp_trash_meta_status');
2590         delete_post_meta($postid,'_wp_trash_meta_time');
2591
2592         wp_delete_object_term_relationships($postid, get_object_taxonomies($post->post_type));
2593
2594         $parent_data = array( 'post_parent' => $post->post_parent );
2595         $parent_where = array( 'post_parent' => $postid );
2596
2597         if ( is_post_type_hierarchical( $post->post_type ) ) {
2598                 // Point children of this page to its parent, also clean the cache of affected children.
2599                 $children_query = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s", $postid, $post->post_type );
2600                 $children = $wpdb->get_results( $children_query );
2601
2602                 $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => $post->post_type ) );
2603         }
2604
2605         // Do raw query. wp_get_post_revisions() is filtered.
2606         $revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) );
2607         // Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up.
2608         foreach ( $revision_ids as $revision_id )
2609                 wp_delete_post_revision( $revision_id );
2610
2611         // Point all attachments to this post up one level.
2612         $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) );
2613
2614         $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $postid ));
2615         foreach ( $comment_ids as $comment_id )
2616                 wp_delete_comment( $comment_id, true );
2617
2618         $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $postid ));
2619         foreach ( $post_meta_ids as $mid )
2620                 delete_metadata_by_mid( 'post', $mid );
2621
2622         /**
2623          * Fires immediately before a post is deleted from the database.
2624          *
2625          * @since 1.2.0
2626          *
2627          * @param int $postid Post ID.
2628          */
2629         do_action( 'delete_post', $postid );
2630         $result = $wpdb->delete( $wpdb->posts, array( 'ID' => $postid ) );
2631         if ( ! $result ) {
2632                 return false;
2633         }
2634
2635         /**
2636          * Fires immediately after a post is deleted from the database.
2637          *
2638          * @since 2.2.0
2639          *
2640          * @param int $postid Post ID.
2641          */
2642         do_action( 'deleted_post', $postid );
2643
2644         clean_post_cache( $post );
2645
2646         if ( is_post_type_hierarchical( $post->post_type ) && $children ) {
2647                 foreach ( $children as $child )
2648                         clean_post_cache( $child );
2649         }
2650
2651         wp_clear_scheduled_hook('publish_future_post', array( $postid ) );
2652
2653         /**
2654          * Fires after a post is deleted, at the conclusion of wp_delete_post().
2655          *
2656          * @since 3.2.0
2657          *
2658          * @see wp_delete_post()
2659          *
2660          * @param int $postid Post ID.
2661          */
2662         do_action( 'after_delete_post', $postid );
2663
2664         return $post;
2665 }
2666
2667 /**
2668  * Reset the page_on_front, show_on_front, and page_for_post settings when
2669  * a linked page is deleted or trashed.
2670  *
2671  * Also ensures the post is no longer sticky.
2672  *
2673  * @since 3.7.0
2674  * @access private
2675  *
2676  * @param int $post_id Post ID.
2677  */
2678 function _reset_front_page_settings_for_post( $post_id ) {
2679         $post = get_post( $post_id );
2680         if ( 'page' == $post->post_type ) {
2681                 /*
2682                  * If the page is defined in option page_on_front or post_for_posts,
2683                  * adjust the corresponding options.
2684                  */
2685                 if ( get_option( 'page_on_front' ) == $post->ID ) {
2686                         update_option( 'show_on_front', 'posts' );
2687                         update_option( 'page_on_front', 0 );
2688                 }
2689                 if ( get_option( 'page_for_posts' ) == $post->ID ) {
2690                         delete_option( 'page_for_posts', 0 );
2691                 }
2692         }
2693         unstick_post( $post->ID );
2694 }
2695 add_action( 'before_delete_post', '_reset_front_page_settings_for_post' );
2696 add_action( 'wp_trash_post',      '_reset_front_page_settings_for_post' );
2697
2698 /**
2699  * Move a post or page to the Trash
2700  *
2701  * If trash is disabled, the post or page is permanently deleted.
2702  *
2703  * @since 2.9.0
2704  *
2705  * @see wp_delete_post()
2706  *
2707  * @param int $post_id Optional. Post ID. Default is ID of the global $post
2708  *                     if EMPTY_TRASH_DAYS equals true.
2709  * @return bool|array Post data array, otherwise false.
2710  */
2711 function wp_trash_post( $post_id = 0 ) {
2712         if ( !EMPTY_TRASH_DAYS )
2713                 return wp_delete_post($post_id, true);
2714
2715         if ( !$post = get_post($post_id, ARRAY_A) )
2716                 return $post;
2717
2718         if ( $post['post_status'] == 'trash' )
2719                 return false;
2720
2721         /**
2722          * Fires before a post is sent to the trash.
2723          *
2724          * @since 3.3.0
2725          *
2726          * @param int $post_id Post ID.
2727          */
2728         do_action( 'wp_trash_post', $post_id );
2729
2730         add_post_meta($post_id,'_wp_trash_meta_status', $post['post_status']);
2731         add_post_meta($post_id,'_wp_trash_meta_time', time());
2732
2733         $post['post_status'] = 'trash';
2734         wp_insert_post($post);
2735
2736         wp_trash_post_comments($post_id);
2737
2738         /**
2739          * Fires after a post is sent to the trash.
2740          *
2741          * @since 2.9.0
2742          *
2743          * @param int $post_id Post ID.
2744          */
2745         do_action( 'trashed_post', $post_id );
2746
2747         return $post;
2748 }
2749
2750 /**
2751  * Restore a post or page from the Trash.
2752  *
2753  * @since 2.9.0
2754  *
2755  * @param int $post_id Optional. Post ID. Default is ID of the global $post.
2756  * @return WP_Post|bool WP_Post object. False on failure.
2757  */
2758 function wp_untrash_post( $post_id = 0 ) {
2759         if ( !$post = get_post($post_id, ARRAY_A) )
2760                 return $post;
2761
2762         if ( $post['post_status'] != 'trash' )
2763                 return false;
2764
2765         /**
2766          * Fires before a post is restored from the trash.
2767          *
2768          * @since 2.9.0
2769          *
2770          * @param int $post_id Post ID.
2771          */
2772         do_action( 'untrash_post', $post_id );
2773
2774         $post_status = get_post_meta($post_id, '_wp_trash_meta_status', true);
2775
2776         $post['post_status'] = $post_status;
2777
2778         delete_post_meta($post_id, '_wp_trash_meta_status');
2779         delete_post_meta($post_id, '_wp_trash_meta_time');
2780
2781         wp_insert_post($post);
2782
2783         wp_untrash_post_comments($post_id);
2784
2785         /**
2786          * Fires after a post is restored from the trash.
2787          *
2788          * @since 2.9.0
2789          *
2790          * @param int $post_id Post ID.
2791          */
2792         do_action( 'untrashed_post', $post_id );
2793
2794         return $post;
2795 }
2796
2797 /**
2798  * Moves comments for a post to the trash.
2799  *
2800  * @since 2.9.0
2801  *
2802  * @global wpdb $wpdb WordPress database abstraction object.
2803  *
2804  * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post.
2805  * @return mixed False on failure.
2806  */
2807 function wp_trash_post_comments( $post = null ) {
2808         global $wpdb;
2809
2810         $post = get_post($post);
2811         if ( empty($post) )
2812                 return;
2813
2814         $post_id = $post->ID;
2815
2816         /**
2817          * Fires before comments are sent to the trash.
2818          *
2819          * @since 2.9.0
2820          *
2821          * @param int $post_id Post ID.
2822          */
2823         do_action( 'trash_post_comments', $post_id );
2824
2825         $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_ID, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id) );
2826         if ( empty($comments) )
2827                 return;
2828
2829         // Cache current status for each comment.
2830         $statuses = array();
2831         foreach ( $comments as $comment )
2832                 $statuses[$comment->comment_ID] = $comment->comment_approved;
2833         add_post_meta($post_id, '_wp_trash_meta_comments_status', $statuses);
2834
2835         // Set status for all comments to post-trashed.
2836         $result = $wpdb->update($wpdb->comments, array('comment_approved' => 'post-trashed'), array('comment_post_ID' => $post_id));
2837
2838         clean_comment_cache( array_keys($statuses) );
2839
2840         /**
2841          * Fires after comments are sent to the trash.
2842          *
2843          * @since 2.9.0
2844          *
2845          * @param int   $post_id  Post ID.
2846          * @param array $statuses Array of comment statuses.
2847          */
2848         do_action( 'trashed_post_comments', $post_id, $statuses );
2849
2850         return $result;
2851 }
2852
2853 /**
2854  * Restore comments for a post from the trash.
2855  *
2856  * @since 2.9.0
2857  *
2858  * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post.
2859  * @return null|bool Null on failure.
2860  */
2861 function wp_untrash_post_comments( $post = null ) {
2862         global $wpdb;
2863
2864         $post = get_post($post);
2865         if ( empty($post) )
2866                 return;
2867
2868         $post_id = $post->ID;
2869
2870         $statuses = get_post_meta($post_id, '_wp_trash_meta_comments_status', true);
2871
2872         if ( empty($statuses) )
2873                 return true;
2874
2875         /**
2876          * Fires before comments are restored for a post from the trash.
2877          *
2878          * @since 2.9.0
2879          *
2880          * @param int $post_id Post ID.
2881          */
2882         do_action( 'untrash_post_comments', $post_id );
2883
2884         // Restore each comment to its original status.
2885         $group_by_status = array();
2886         foreach ( $statuses as $comment_id => $comment_status )
2887                 $group_by_status[$comment_status][] = $comment_id;
2888
2889         foreach ( $group_by_status as $status => $comments ) {
2890                 // Sanity check. This shouldn't happen.
2891                 if ( 'post-trashed' == $status )
2892                         $status = '0';
2893                 $comments_in = implode( "', '", $comments );
2894                 $wpdb->query( "UPDATE $wpdb->comments SET comment_approved = '$status' WHERE comment_ID IN ('" . $comments_in . "')" );
2895         }
2896
2897         clean_comment_cache( array_keys($statuses) );
2898
2899         delete_post_meta($post_id, '_wp_trash_meta_comments_status');
2900
2901         /**
2902          * Fires after comments are restored for a post from the trash.
2903          *
2904          * @since 2.9.0
2905          *
2906          * @param int $post_id Post ID.
2907          */
2908         do_action( 'untrashed_post_comments', $post_id );
2909 }
2910
2911 /**
2912  * Retrieve the list of categories for a post.
2913  *
2914  * Compatibility layer for themes and plugins. Also an easy layer of abstraction
2915  * away from the complexity of the taxonomy layer.
2916  *
2917  * @since 2.1.0
2918  *
2919  * @see wp_get_object_terms()
2920  *
2921  * @param int   $post_id Optional. The Post ID. Does not default to the ID of the
2922  *                       global $post. Default 0.
2923  * @param array $args    Optional. Category arguments. Default empty.
2924  * @return array List of categories.
2925  */
2926 function wp_get_post_categories( $post_id = 0, $args = array() ) {
2927         $post_id = (int) $post_id;
2928
2929         $defaults = array('fields' => 'ids');
2930         $args = wp_parse_args( $args, $defaults );
2931
2932         $cats = wp_get_object_terms($post_id, 'category', $args);
2933         return $cats;
2934 }
2935
2936 /**
2937  * Retrieve the tags for a post.
2938  *
2939  * There is only one default for this function, called 'fields' and by default
2940  * is set to 'all'. There are other defaults that can be overridden in
2941  * {@link wp_get_object_terms()}.
2942  *
2943  * @since 2.3.0
2944  *
2945  * @param int   $post_id Optional. The Post ID. Does not default to the ID of the
2946  *                       global $post. Defualt 0.
2947  * @param array $args Optional. Overwrite the defaults
2948  * @return array List of post tags.
2949  */
2950 function wp_get_post_tags( $post_id = 0, $args = array() ) {
2951         return wp_get_post_terms( $post_id, 'post_tag', $args);
2952 }
2953
2954 /**
2955  * Retrieve the terms for a post.
2956  *
2957  * There is only one default for this function, called 'fields' and by default
2958  * is set to 'all'. There are other defaults that can be overridden in
2959  * {@link wp_get_object_terms()}.
2960  *
2961  * @since 2.8.0
2962  *
2963  * @param int    $post_id  Optional. The Post ID. Does not default to the ID of the
2964  *                         global $post. Default 0.
2965  * @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'.
2966  * @param array  $args     Optional. {@link wp_get_object_terms()} arguments. Default empty array.
2967  * @return array List of post tags.
2968  */
2969 function wp_get_post_terms( $post_id = 0, $taxonomy = 'post_tag', $args = array() ) {
2970         $post_id = (int) $post_id;
2971
2972         $defaults = array('fields' => 'all');
2973         $args = wp_parse_args( $args, $defaults );
2974
2975         $tags = wp_get_object_terms($post_id, $taxonomy, $args);
2976
2977         return $tags;
2978 }
2979
2980 /**
2981  * Retrieve a number of recent posts.
2982  *
2983  * @since 1.0.0
2984  *
2985  * @see get_posts()
2986  *
2987  * @param array  $args       Optional. Arguments to retrieve posts. Default empty array.
2988  * @param string $output     Optional. Type of output. Accepts ARRAY_A or ''. Default ARRAY_A.
2989  * @return array|bool Associative array if $output equals ARRAY_A, array or false if no results.
2990  */
2991 function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) {
2992
2993         if ( is_numeric( $args ) ) {
2994                 _deprecated_argument( __FUNCTION__, '3.1', __( 'Passing an integer number of posts is deprecated. Pass an array of arguments instead.' ) );
2995                 $args = array( 'numberposts' => absint( $args ) );
2996         }
2997
2998         // Set default arguments.
2999         $defaults = array(
3000                 'numberposts' => 10, 'offset' => 0,
3001                 'category' => 0, 'orderby' => 'post_date',
3002                 'order' => 'DESC', 'include' => '',
3003                 'exclude' => '', 'meta_key' => '',
3004                 'meta_value' =>'', 'post_type' => 'post', 'post_status' => 'draft, publish, future, pending, private',
3005                 'suppress_filters' => true
3006         );
3007
3008         $r = wp_parse_args( $args, $defaults );
3009
3010         $results = get_posts( $r );
3011
3012         // Backward compatibility. Prior to 3.1 expected posts to be returned in array.
3013         if ( ARRAY_A == $output ){
3014                 foreach( $results as $key => $result ) {
3015                         $results[$key] = get_object_vars( $result );
3016                 }
3017                 return $results ? $results : array();
3018         }
3019
3020         return $results ? $results : false;
3021
3022 }
3023
3024 /**
3025  * Insert or update a post.
3026  *
3027  * If the $postarr parameter has 'ID' set to a value, then post will be updated.
3028  *
3029  * You can set the post date manually, by setting the values for 'post_date'
3030  * and 'post_date_gmt' keys. You can close the comments or open the comments by
3031  * setting the value for 'comment_status' key.
3032  *
3033  * @since 1.0.0
3034  *
3035  * @see sanitize_post()
3036  * @global wpdb $wpdb WordPress database abstraction object.
3037  *
3038  * @param array $postarr {
3039  *     An array of elements that make up a post to update or insert.
3040  *
3041  *     @type int    $ID                    The post ID. If equal to something other than 0,
3042  *                                         the post with that ID will be updated. Default 0.
3043  *     @type string $post_status           The post status. Default 'draft'.
3044  *     @type string $post_type             The post type. Default 'post'.
3045  *     @type int    $post_author           The ID of the user who added the post. Default is
3046  *                                         the current user ID.
3047  *     @type bool   $ping_status           Whether the post can accept pings. Default is the
3048  *                                         value of 'default_ping_status' option.
3049  *     @type int    $post_parent           Set this for the post it belongs to, if any. Default 0.
3050  *     @type int    $menu_order            The order it is displayed. Default 0.
3051  *     @type string $to_ping               Space or carriage return-separated list of URLs to ping.
3052  *                                         Default empty string.
3053  *     @type string $pinged                Space or carriage return-separated list of URLs that have
3054  *                                         been pinged. Default empty string.
3055  *     @type string $post_password         The password to access the post. Default empty string.
3056  *     @type string $guid'                 Global Unique ID for referencing the post.
3057  *     @type string $post_content_filtered The filtered post content. Default empty string.
3058  *     @type string $post_excerpt          The post excerpt. Default empty string.
3059  * }
3060  * @param bool  $wp_error Optional. Whether to allow return of WP_Error on failure. Default false.
3061  * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure.
3062  */
3063 function wp_insert_post( $postarr, $wp_error = false ) {
3064         global $wpdb;
3065
3066         $user_id = get_current_user_id();
3067
3068         $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_id,
3069                 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
3070                 'menu_order' => 0, 'to_ping' =>  '', 'pinged' => '', 'post_password' => '',
3071                 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0,
3072                 'post_content' => '', 'post_title' => '', 'context' => '');
3073
3074         $postarr = wp_parse_args($postarr, $defaults);
3075
3076         unset( $postarr[ 'filter' ] );
3077
3078         $postarr = sanitize_post($postarr, 'db');
3079
3080         // Are we updating or creating?
3081         $post_ID = 0;
3082         $update = false;
3083         $guid = $postarr['guid'];
3084
3085         if ( ! empty( $postarr['ID'] ) ) {
3086                 $update = true;
3087
3088                 // Get the post ID and GUID.
3089                 $post_ID = $postarr['ID'];
3090                 $post_before = get_post( $post_ID );
3091                 if ( is_null( $post_before ) ) {
3092                         if ( $wp_error ) {
3093                                 return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );
3094                         }
3095                         return 0;
3096                 }
3097
3098                 $guid = get_post_field( 'guid', $post_ID );
3099                 $previous_status = get_post_field('post_status', $post_ID );
3100         } else {
3101                 $previous_status = 'new';
3102         }
3103
3104         $post_type = empty( $postarr['post_type'] ) ? 'post' : $postarr['post_type'];
3105
3106         $post_title = $postarr['post_title'];
3107         $post_content = $postarr['post_content'];
3108         $post_excerpt = $postarr['post_excerpt'];
3109         if ( isset( $postarr['post_name'] ) ) {
3110                 $post_name = $postarr['post_name'];
3111         }
3112
3113         $maybe_empty = 'attachment' !== $post_type
3114                 && ! $post_content && ! $post_title && ! $post_excerpt
3115                 && post_type_supports( $post_type, 'editor' )
3116                 && post_type_supports( $post_type, 'title' )
3117                 && post_type_supports( $post_type, 'excerpt' );
3118
3119         /**
3120          * Filter whether the post should be considered "empty".
3121          *
3122          * The post is considered "empty" if both:
3123          * 1. The post type supports the title, editor, and excerpt fields
3124          * 2. The title, editor, and excerpt fields are all empty
3125          *
3126          * Returning a truthy value to the filter will effectively short-circuit
3127          * the new post being inserted, returning 0. If $wp_error is true, a WP_Error
3128          * will be returned instead.
3129          *
3130          * @since 3.3.0
3131          *
3132          * @param bool  $maybe_empty Whether the post should be considered "empty".
3133          * @param array $postarr     Array of post data.
3134          */
3135         if ( apply_filters( 'wp_insert_post_empty_content', $maybe_empty, $postarr ) ) {
3136                 if ( $wp_error ) {
3137                         return new WP_Error( 'empty_content', __( 'Content, title, and excerpt are empty.' ) );
3138                 } else {
3139                         return 0;
3140                 }
3141         }
3142
3143         $post_status = empty( $postarr['post_status'] ) ? 'draft' : $postarr['post_status'];
3144         if ( 'attachment' === $post_type && ! in_array( $post_status, array( 'inherit', 'private', 'trash' ) ) ) {
3145                 $post_status = 'inherit';
3146         }
3147
3148         if ( ! empty( $postarr['post_category'] ) ) {
3149                 // Filter out empty terms.
3150                 $post_category = array_filter( $postarr['post_category'] );
3151         }
3152
3153         // Make sure we set a valid category.
3154         if ( empty( $post_category ) || 0 == count( $post_category ) || ! is_array( $post_category ) ) {
3155                 // 'post' requires at least one category.
3156                 if ( 'post' == $post_type && 'auto-draft' != $post_status ) {
3157                         $post_category = array( get_option('default_category') );
3158                 } else {
3159                         $post_category = array();
3160                 }
3161         }
3162
3163         // Don't allow contributors to set the post slug for pending review posts.
3164         if ( 'pending' == $post_status && !current_user_can( 'publish_posts' ) ) {
3165                 $post_name = '';
3166         }
3167
3168         /*
3169          * Create a valid post name. Drafts and pending posts are allowed to have
3170          * an empty post name.
3171          */
3172         if ( empty($post_name) ) {
3173                 if ( !in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {
3174                         $post_name = sanitize_title($post_title);
3175                 } else {
3176                         $post_name = '';
3177                 }
3178         } else {
3179                 // On updates, we need to check to see if it's using the old, fixed sanitization context.
3180                 $check_name = sanitize_title( $post_name, '', 'old-save' );
3181                 if ( $update && strtolower( urlencode( $post_name ) ) == $check_name && get_post_field( 'post_name', $post_ID ) == $check_name ) {
3182                         $post_name = $check_name;
3183                 } else { // new post, or slug has changed.
3184                         $post_name = sanitize_title($post_name);
3185                 }
3186         }
3187
3188         /*
3189          * If the post date is empty (due to having been new or a draft) and status
3190          * is not 'draft' or 'pending', set date to now.
3191          */
3192         if ( empty( $postarr['post_date'] ) || '0000-00-00 00:00:00' == $postarr['post_date'] ) {
3193                 $post_date = current_time( 'mysql' );
3194         } else {
3195                 $post_date = $postarr['post_date'];
3196         }
3197
3198         // Validate the date.
3199         $mm = substr( $post_date, 5, 2 );
3200         $jj = substr( $post_date, 8, 2 );
3201         $aa = substr( $post_date, 0, 4 );
3202         $valid_date = wp_checkdate( $mm, $jj, $aa, $post_date );
3203         if ( ! $valid_date ) {
3204                 if ( $wp_error ) {
3205                         return new WP_Error( 'invalid_date', __( 'Whoops, the provided date is invalid.' ) );
3206                 } else {
3207                         return 0;
3208                 }
3209         }
3210
3211         if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' == $postarr['post_date_gmt'] ) {
3212                 if ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {
3213                         $post_date_gmt = get_gmt_from_date( $post_date );
3214                 } else {
3215                         $post_date_gmt = '0000-00-00 00:00:00';
3216                 }
3217         } else {
3218                 $post_date_gmt = $postarr['post_date_gmt'];
3219         }
3220
3221         if ( $update || '0000-00-00 00:00:00' == $post_date ) {
3222                 $post_modified     = current_time( 'mysql' );
3223                 $post_modified_gmt = current_time( 'mysql', 1 );
3224         } else {
3225                 $post_modified     = $post_date;
3226                 $post_modified_gmt = $post_date_gmt;
3227         }
3228
3229         if ( 'attachment' !== $post_type ) {
3230                 if ( 'publish' == $post_status ) {
3231                         $now = gmdate('Y-m-d H:i:59');
3232                         if ( mysql2date('U', $post_date_gmt, false) > mysql2date('U', $now, false) ) {
3233                                 $post_status = 'future';
3234                         }
3235                 } elseif( 'future' == $post_status ) {
3236                         $now = gmdate('Y-m-d H:i:59');
3237                         if ( mysql2date('U', $post_date_gmt, false) <= mysql2date('U', $now, false) ) {
3238                                 $post_status = 'publish';
3239                         }
3240                 }
3241         }
3242
3243         if ( empty( $postarr['comment_status'] ) ) {
3244                 if ( $update ) {
3245                         $comment_status = 'closed';
3246                 } else {
3247                         $comment_status = get_option('default_comment_status');
3248                 }
3249         } else {
3250                 $comment_status = $postarr['comment_status'];
3251         }
3252
3253         // These variables are needed by compact() later.
3254         $post_content_filtered = $postarr['post_content_filtered'];
3255         $post_author = empty( $postarr['post_author'] ) ? $user_id : $postarr['post_author'];
3256         $ping_status = empty( $postarr['ping_status'] ) ? get_option( 'default_ping_status' ) : $postarr['ping_status'];
3257         $to_ping = isset( $postarr['to_ping'] ) ? sanitize_trackback_urls( $postarr['to_ping'] ) : '';
3258         $pinged = isset( $postarr['pinged'] ) ? $postarr['pinged'] : '';
3259         $import_id = isset( $postarr['import_id'] ) ? $postarr['import_id'] : 0;
3260
3261         /*
3262          * The 'wp_insert_post_parent' filter expects all variables to be present.
3263          * Previously, these variables would have already been extracted
3264          */
3265         if ( isset( $postarr['menu_order'] ) ) {
3266                 $menu_order = (int) $postarr['menu_order'];
3267         } else {
3268                 $menu_order = 0;
3269         }
3270
3271         $post_password = isset( $postarr['post_password'] ) ? $postarr['post_password'] : '';
3272         if ( 'private' == $post_status ) {
3273                 $post_password = '';
3274         }
3275
3276         if ( isset( $postarr['post_parent'] ) ) {
3277                 $post_parent = (int) $postarr['post_parent'];
3278         } else {
3279                 $post_parent = 0;
3280         }
3281
3282         /**
3283          * Filter the post parent -- used to check for and prevent hierarchy loops.
3284          *
3285          * @since 3.1.0
3286          *
3287          * @param int   $post_parent Post parent ID.
3288          * @param int   $post_ID     Post ID.
3289          * @param array $new_postarr Array of parsed post data.
3290          * @param array $postarr     Array of sanitized, but otherwise unmodified post data.
3291          */
3292         $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr );
3293
3294         $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );
3295
3296         // Don't unslash.
3297         $post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : '';
3298
3299         // Expected_slashed (everything!).
3300         $data = compact( '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' );
3301
3302         if ( 'attachment' === $post_type ) {
3303                 /**
3304                  * Filter attachment post data before it is updated in or added to the database.
3305                  *
3306                  * @since 3.9.0
3307                  *
3308                  * @param array $data    An array of sanitized attachment post data.
3309                  * @param array $postarr An array of unsanitized attachment post data.
3310                  */
3311                 $data = apply_filters( 'wp_insert_attachment_data', $data, $postarr );
3312         } else {
3313                 /**
3314                  * Filter slashed post data just before it is inserted into the database.
3315                  *
3316                  * @since 2.7.0
3317                  *
3318                  * @param array $data    An array of slashed post data.
3319                  * @param array $postarr An array of sanitized, but otherwise unmodified post data.
3320                  */
3321                 $data = apply_filters( 'wp_insert_post_data', $data, $postarr );
3322         }
3323         $data = wp_unslash( $data );
3324         $where = array( 'ID' => $post_ID );
3325
3326         if ( $update ) {
3327                 /**
3328                  * Fires immediately before an existing post is updated in the database.
3329                  *
3330                  * @since 2.5.0
3331                  *
3332                  * @param int   $post_ID Post ID.
3333                  * @param array $data    Array of unslashed post data.
3334                  */
3335                 do_action( 'pre_post_update', $post_ID, $data );
3336                 if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) {
3337                         if ( $wp_error ) {
3338                                 return new WP_Error('db_update_error', __('Could not update post in the database'), $wpdb->last_error);
3339                         } else {
3340                                 return 0;
3341                         }
3342                 }
3343         } else {
3344                 // If there is a suggested ID, use it if not already present.
3345                 if ( ! empty( $import_id ) ) {
3346                         $import_id = (int) $import_id;
3347                         if ( ! $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id) ) ) {
3348                                 $data['ID'] = $import_id;
3349                         }
3350                 }
3351                 if ( false === $wpdb->insert( $wpdb->posts, $data ) ) {
3352                         if ( $wp_error ) {
3353                                 return new WP_Error('db_insert_error', __('Could not insert post into the database'), $wpdb->last_error);
3354                         } else {
3355                                 return 0;
3356                         }
3357                 }
3358                 $post_ID = (int) $wpdb->insert_id;
3359
3360                 // Use the newly generated $post_ID.
3361                 $where = array( 'ID' => $post_ID );
3362         }
3363
3364         if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
3365                 $data['post_name'] = sanitize_title( $data['post_title'], $post_ID );
3366                 $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
3367         }
3368
3369         if ( is_object_in_taxonomy( $post_type, 'category' ) ) {
3370                 wp_set_post_categories( $post_ID, $post_category );
3371         }
3372
3373         if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $post_type, 'post_tag' ) ) {
3374                 wp_set_post_tags( $post_ID, $postarr['tags_input'] );
3375         }
3376
3377         // New-style support for all custom taxonomies.
3378         if ( ! empty( $postarr['tax_input'] ) ) {
3379                 foreach ( $postarr['tax_input'] as $taxonomy => $tags ) {
3380                         $taxonomy_obj = get_taxonomy($taxonomy);
3381                         // array = hierarchical, string = non-hierarchical.
3382                         if ( is_array( $tags ) ) {
3383                                 $tags = array_filter($tags);
3384                         }
3385                         if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) {
3386                                 wp_set_post_terms( $post_ID, $tags, $taxonomy );
3387                         }
3388                 }
3389         }
3390
3391         $current_guid = get_post_field( 'guid', $post_ID );
3392
3393         // Set GUID.
3394         if ( ! $update && '' == $current_guid ) {
3395                 $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where );
3396         }
3397
3398         if ( 'attachment' === $postarr['post_type'] ) {
3399                 if ( ! empty( $postarr['file'] ) ) {
3400                         update_attached_file( $post_ID, $postarr['file'] );
3401                 }
3402
3403                 if ( ! empty( $postarr['context'] ) ) {
3404                         add_post_meta( $post_ID, '_wp_attachment_context', $postarr['context'], true );
3405                 }
3406         }
3407
3408         clean_post_cache( $post_ID );
3409
3410         $post = get_post( $post_ID );
3411
3412         if ( ! empty( $postarr['page_template'] ) && 'page' == $data['post_type'] ) {
3413                 $post->page_template = $postarr['page_template'];
3414                 $page_templates = wp_get_theme()->get_page_templates( $post );
3415                 if ( 'default' != $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) {
3416                         if ( $wp_error ) {
3417                                 return new WP_Error('invalid_page_template', __('The page template is invalid.'));
3418                         } else {
3419                                 return 0;
3420                         }
3421                 }
3422                 update_post_meta( $post_ID, '_wp_page_template', $postarr['page_template'] );
3423         }
3424
3425         if ( 'attachment' !== $postarr['post_type'] ) {
3426                 wp_transition_post_status( $data['post_status'], $previous_status, $post );
3427         } else {
3428                 if ( $update ) {
3429                         /**
3430                          * Fires once an existing attachment has been updated.
3431                          *
3432                          * @since 2.0.0
3433                          *
3434                          * @param int $post_ID Attachment ID.
3435                          */
3436                         do_action( 'edit_attachment', $post_ID );
3437                 } else {
3438
3439                         /**
3440                          * Fires once an attachment has been added.
3441                          *
3442                          * @since 2.0.0
3443                          *
3444                          * @param int $post_ID Attachment ID.
3445                          */
3446                         do_action( 'add_attachment', $post_ID );
3447                 }
3448
3449                 return $post_ID;
3450         }
3451
3452         if ( $update ) {
3453                 /**
3454                  * Fires once an existing post has been updated.
3455                  *
3456                  * @since 1.2.0
3457                  *
3458                  * @param int     $post_ID Post ID.
3459                  * @param WP_Post $post    Post object.
3460                  */
3461                 do_action( 'edit_post', $post_ID, $post );
3462                 $post_after = get_post($post_ID);
3463
3464                 /**
3465                  * Fires once an existing post has been updated.
3466                  *
3467                  * @since 3.0.0
3468                  *
3469                  * @param int     $post_ID      Post ID.
3470                  * @param WP_Post $post_after   Post object following the update.
3471                  * @param WP_Post $post_before  Post object before the update.
3472                  */
3473                 do_action( 'post_updated', $post_ID, $post_after, $post_before);
3474         }
3475
3476         /**
3477          * Fires once a post has been saved.
3478          *
3479          * The dynamic portion of the hook name, `$post->post_type`, refers to
3480          * the post type slug.
3481          *
3482          * @since 3.7.0
3483          *
3484          * @param int     $post_ID Post ID.
3485          * @param WP_Post $post    Post object.
3486          * @param bool    $update  Whether this is an existing post being updated or not.
3487          */
3488         do_action( "save_post_{$post->post_type}", $post_ID, $post, $update );
3489
3490         /**
3491          * Fires once a post has been saved.
3492          *
3493          * @since 1.5.0
3494          *
3495          * @param int     $post_ID Post ID.
3496          * @param WP_Post $post    Post object.
3497          * @param bool    $update  Whether this is an existing post being updated or not.
3498          */
3499         do_action( 'save_post', $post_ID, $post, $update );
3500
3501         /**
3502          * Fires once a post has been saved.
3503          *
3504          * @since 2.0.0
3505          *
3506          * @param int     $post_ID Post ID.
3507          * @param WP_Post $post    Post object.
3508          * @param bool    $update  Whether this is an existing post being updated or not.
3509          */
3510         do_action( 'wp_insert_post', $post_ID, $post, $update );
3511
3512         return $post_ID;
3513 }
3514
3515 /**
3516  * Update a post with new post data.
3517  *
3518  * The date does not have to be set for drafts. You can set the date and it will
3519  * not be overridden.
3520  *
3521  * @since 1.0.0
3522  *
3523  * @param array|object $postarr  Optional. Post data. Arrays are expected to be escaped,
3524  *                               objects are not. Default array.
3525  * @param bool         $wp_error Optional. Allow return of WP_Error on failure. Default false.
3526  * @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success.
3527  */
3528 function wp_update_post( $postarr = array(), $wp_error = false ) {
3529         if ( is_object($postarr) ) {
3530                 // Non-escaped post was passed.
3531                 $postarr = get_object_vars($postarr);
3532                 $postarr = wp_slash($postarr);
3533         }
3534
3535         // First, get all of the original fields.
3536         $post = get_post($postarr['ID'], ARRAY_A);
3537
3538         if ( is_null( $post ) ) {
3539                 if ( $wp_error )
3540                         return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );
3541                 return 0;
3542         }
3543
3544         // Escape data pulled from DB.
3545         $post = wp_slash($post);
3546
3547         // Passed post category list overwrites existing category list if not empty.
3548         if ( isset($postarr['post_category']) && is_array($postarr['post_category'])
3549                          && 0 != count($postarr['post_category']) )
3550                 $post_cats = $postarr['post_category'];
3551         else
3552                 $post_cats = $post['post_category'];
3553
3554         // Drafts shouldn't be assigned a date unless explicitly done so by the user.
3555         if ( isset( $post['post_status'] ) && in_array($post['post_status'], array('draft', 'pending', 'auto-draft')) && empty($postarr['edit_date']) &&
3556                          ('0000-00-00 00:00:00' == $post['post_date_gmt']) )
3557                 $clear_date = true;
3558         else
3559                 $clear_date = false;
3560
3561         // Merge old and new fields with new fields overwriting old ones.
3562         $postarr = array_merge($post, $postarr);
3563         $postarr['post_category'] = $post_cats;
3564         if ( $clear_date ) {
3565                 $postarr['post_date'] = current_time('mysql');
3566                 $postarr['post_date_gmt'] = '';
3567         }
3568
3569         if ($postarr['post_type'] == 'attachment')
3570                 return wp_insert_attachment($postarr);
3571
3572         return wp_insert_post( $postarr, $wp_error );
3573 }
3574
3575 /**
3576  * Publish a post by transitioning the post status.
3577  *
3578  * @since 2.1.0
3579  *
3580  * @global wpdb $wpdb WordPress database abstraction object.
3581  *
3582  * @param int|WP_Post $post Post ID or post object.
3583  */
3584 function wp_publish_post( $post ) {
3585         global $wpdb;
3586
3587         if ( ! $post = get_post( $post ) )
3588                 return;
3589
3590         if ( 'publish' == $post->post_status )
3591                 return;
3592
3593         $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) );
3594
3595         clean_post_cache( $post->ID );
3596
3597         $old_status = $post->post_status;
3598         $post->post_status = 'publish';
3599         wp_transition_post_status( 'publish', $old_status, $post );
3600
3601         /** This action is documented in wp-includes/post.php */
3602         do_action( 'edit_post', $post->ID, $post );
3603
3604         /** This action is documented in wp-includes/post.php */
3605         do_action( "save_post_{$post->post_type}", $post->ID, $post, true );
3606
3607         /** This action is documented in wp-includes/post.php */
3608         do_action( 'save_post', $post->ID, $post, true );
3609
3610         /** This action is documented in wp-includes/post.php */
3611         do_action( 'wp_insert_post', $post->ID, $post, true );
3612 }
3613
3614 /**
3615  * Publish future post and make sure post ID has future post status.
3616  *
3617  * Invoked by cron 'publish_future_post' event. This safeguard prevents cron
3618  * from publishing drafts, etc.
3619  *
3620  * @since 2.5.0
3621  *
3622  * @param int|WP_Post $post_id Post ID or post object.
3623  * @return null Nothing is returned. Which can mean that no action is required
3624  *              or post was published.
3625  */
3626 function check_and_publish_future_post( $post_id ) {
3627
3628         $post = get_post($post_id);
3629
3630         if ( empty($post) )
3631                 return;
3632
3633         if ( 'future' != $post->post_status )
3634                 return;
3635
3636         $time = strtotime( $post->post_date_gmt . ' GMT' );
3637
3638         // Uh oh, someone jumped the gun!
3639         if ( $time > time() ) {
3640                 wp_clear_scheduled_hook( 'publish_future_post', array( $post_id ) ); // clear anything else in the system
3641                 wp_schedule_single_event( $time, 'publish_future_post', array( $post_id ) );
3642                 return;
3643         }
3644
3645         return wp_publish_post($post_id);
3646 }
3647
3648 /**
3649  * Computes a unique slug for the post, when given the desired slug and some post details.
3650  *
3651  * @since 2.8.0
3652  *
3653  * @global wpdb $wpdb WordPress database abstraction object.
3654  * @global WP_Rewrite $wp_rewrite
3655  *
3656  * @param string $slug        The desired slug (post_name).
3657  * @param int    $post_ID     Post ID.
3658  * @param string $post_status No uniqueness checks are made if the post is still draft or pending.
3659  * @param string $post_type   Post type.
3660  * @param int    $post_parent Post parent ID.
3661  * @return string Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix)
3662  */
3663 function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) {
3664         if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) || ( 'inherit' == $post_status && 'revision' == $post_type ) )
3665                 return $slug;
3666
3667         global $wpdb, $wp_rewrite;
3668
3669         $original_slug = $slug;
3670
3671         $feeds = $wp_rewrite->feeds;
3672         if ( ! is_array( $feeds ) )
3673                 $feeds = array();
3674
3675         if ( 'attachment' == $post_type ) {
3676                 // Attachment slugs must be unique across all types.
3677                 $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1";
3678                 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) );
3679
3680                 /**
3681                  * Filter whether the post slug would make a bad attachment slug.
3682                  *
3683                  * @since 3.1.0
3684                  *
3685                  * @param bool   $bad_slug Whether the slug would be bad as an attachment slug.
3686                  * @param string $slug     The post slug.
3687                  */
3688                 if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) {
3689                         $suffix = 2;
3690                         do {
3691                                 $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
3692                                 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID ) );
3693                                 $suffix++;
3694                         } while ( $post_name_check );
3695                         $slug = $alt_post_name;
3696                 }
3697         } elseif ( is_post_type_hierarchical( $post_type ) ) {
3698                 if ( 'nav_menu_item' == $post_type )
3699                         return $slug;
3700
3701                 /*
3702                  * Page slugs must be unique within their own trees. Pages are in a separate
3703                  * namespace than posts so page slugs are allowed to overlap post slugs.
3704                  */
3705                 $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1";
3706                 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) );
3707
3708                 /**
3709                  * Filter whether the post slug would make a bad hierarchical post slug.
3710                  *
3711                  * @since 3.1.0
3712                  *
3713                  * @param bool   $bad_slug    Whether the post slug would be bad in a hierarchical post context.
3714                  * @param string $slug        The post slug.
3715                  * @param string $post_type   Post type.
3716                  * @param int    $post_parent Post parent ID.
3717                  */
3718                 if ( $post_name_check || in_array( $slug, $feeds ) || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug )  || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) {
3719                         $suffix = 2;
3720                         do {
3721                                 $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
3722                                 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID, $post_parent ) );
3723                                 $suffix++;
3724                         } while ( $post_name_check );
3725                         $slug = $alt_post_name;
3726                 }
3727         } else {
3728                 // Post slugs must be unique across all posts.
3729                 $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
3730                 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) );
3731
3732                 /**
3733                  * Filter whether the post slug would be bad as a flat slug.
3734                  *
3735                  * @since 3.1.0
3736                  *
3737                  * @param bool   $bad_slug  Whether the post slug would be bad as a flat slug.
3738                  * @param string $slug      The post slug.
3739                  * @param string $post_type Post type.
3740                  */
3741                 if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) {
3742                         $suffix = 2;
3743                         do {
3744                                 $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
3745                                 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) );
3746                                 $suffix++;
3747                         } while ( $post_name_check );
3748                         $slug = $alt_post_name;
3749                 }
3750         }
3751
3752         /**
3753          * Filter the unique post slug.
3754          *
3755          * @since 3.3.0
3756          *
3757          * @param string $slug          The post slug.
3758          * @param int    $post_ID       Post ID.
3759          * @param string $post_status   The post status.
3760          * @param string $post_type     Post type.
3761          * @param int    $post_parent   Post parent ID
3762          * @param string $original_slug The original post slug.
3763          */
3764         return apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug );
3765 }
3766
3767 /**
3768  * Truncate a post slug.
3769  *
3770  * @since 3.6.0
3771  * @access private
3772  *
3773  * @see utf8_uri_encode()
3774  *
3775  * @param string $slug   The slug to truncate.
3776  * @param int    $length Optional. Max length of the slug. Default 200 (characters).
3777  * @return string The truncated slug.
3778  */
3779 function _truncate_post_slug( $slug, $length = 200 ) {
3780         if ( strlen( $slug ) > $length ) {
3781                 $decoded_slug = urldecode( $slug );
3782                 if ( $decoded_slug === $slug )
3783                         $slug = substr( $slug, 0, $length );
3784                 else
3785                         $slug = utf8_uri_encode( $decoded_slug, $length );
3786         }
3787
3788         return rtrim( $slug, '-' );
3789 }
3790
3791 /**
3792  * Add tags to a post.
3793  *
3794  * @see wp_set_post_tags()
3795  *
3796  * @since 2.3.0
3797  *
3798  * @param int    $post_id Optional. The Post ID. Does not default to the ID of the global $post.
3799  *                        Default 0.
3800  * @param string $tags    Optional. The tags to set for the post, separated by commas. Default empty.
3801  * @return bool|null Will return false if $post_id is not an integer or is 0. Will return null otherwise.
3802  */
3803 function wp_add_post_tags( $post_id = 0, $tags = '' ) {
3804         return wp_set_post_tags($post_id, $tags, true);
3805 }
3806
3807 /**
3808  * Set the tags for a post.
3809  *
3810  * @since 2.3.0
3811  *
3812  * @see wp_set_object_terms()
3813  *
3814  * @param int    $post_id Optional. The Post ID. Does not default to the ID of the global $post.
3815  * @param string $tags    Optional. The tags to set for the post, separated by commas.
3816  *                        Default empty.
3817  * @param bool   $append  Optional. If true, don't delete existing tags, just add on. If false,
3818  *                        replace the tags with the new tags. Default false.
3819  * @return mixed Array of affected term IDs. WP_Error or false on failure.
3820  */
3821 function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) {
3822         return wp_set_post_terms( $post_id, $tags, 'post_tag', $append);
3823 }
3824
3825 /**
3826  * Set the terms for a post.
3827  *
3828  * @since 2.8.0
3829  *
3830  * @see wp_set_object_terms()
3831  *
3832  * @param int    $post_id  Optional. The Post ID. Does not default to the ID of the global $post.
3833  * @param string $tags     Optional. The tags to set for the post, separated by commas. Default empty.
3834  * @param string $taxonomy Optional. Taxonomy name. Default 'post_tag'.
3835  * @param bool   $append   Optional. If true, don't delete existing tags, just add on. If false,
3836  *                         replace the tags with the new tags. Default false.
3837  * @return mixed Array of affected term IDs. WP_Error or false on failure.
3838  */
3839 function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false ) {
3840         $post_id = (int) $post_id;
3841
3842         if ( !$post_id )
3843                 return false;
3844
3845         if ( empty($tags) )
3846                 $tags = array();
3847
3848         if ( ! is_array( $tags ) ) {
3849                 $comma = _x( ',', 'tag delimiter' );
3850                 if ( ',' !== $comma )
3851                         $tags = str_replace( $comma, ',', $tags );
3852                 $tags = explode( ',', trim( $tags, " \n\t\r\0\x0B," ) );
3853         }
3854
3855         /*
3856          * Hierarchical taxonomies must always pass IDs rather than names so that
3857          * children with the same names but different parents aren't confused.
3858          */
3859         if ( is_taxonomy_hierarchical( $taxonomy ) ) {
3860                 $tags = array_unique( array_map( 'intval', $tags ) );
3861         }
3862
3863         return wp_set_object_terms( $post_id, $tags, $taxonomy, $append );
3864 }
3865
3866 /**
3867  * Set categories for a post.
3868  *
3869  * If the post categories parameter is not set, then the default category is
3870  * going used.
3871  *
3872  * @since 2.1.0
3873  *
3874  * @param int       $post_ID         Optional. The Post ID. Does not default to the ID
3875  *                                   of the global $post. Default 0.
3876  * @param array|int $post_categories Optional. List of categories or ID of category.
3877  *                                   Default empty array.
3878  * @param bool      $append         If true, don't delete existing categories, just add on.
3879  *                                  If false, replace the categories with the new categories.
3880  * @return bool|mixed
3881  */
3882 function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $append = false ) {
3883         $post_ID = (int) $post_ID;
3884         $post_type = get_post_type( $post_ID );
3885         $post_status = get_post_status( $post_ID );
3886         // If $post_categories isn't already an array, make it one:
3887         $post_categories = (array) $post_categories;
3888         if ( empty( $post_categories ) ) {
3889                 if ( 'post' == $post_type && 'auto-draft' != $post_status ) {
3890                         $post_categories = array( get_option('default_category') );
3891                         $append = false;
3892                 } else {
3893                         $post_categories = array();
3894                 }
3895         } else if ( 1 == count($post_categories) && '' == reset($post_categories) ) {
3896                 return true;
3897         }
3898
3899         return wp_set_post_terms( $post_ID, $post_categories, 'category', $append );
3900 }
3901
3902 /**
3903  * Transition the post status of a post.
3904  *
3905  * Calls hooks to transition post status.
3906  *
3907  * The first is 'transition_post_status' with new status, old status, and post data.
3908  *
3909  * The next action called is 'OLDSTATUS_to_NEWSTATUS' the 'NEWSTATUS' is the
3910  * $new_status parameter and the 'OLDSTATUS' is $old_status parameter; it has the
3911  * post data.
3912  *
3913  * The final action is named 'NEWSTATUS_POSTTYPE', 'NEWSTATUS' is from the $new_status
3914  * parameter and POSTTYPE is post_type post data.
3915  *
3916  * @since 2.3.0
3917  *
3918  * @param string  $new_status Transition to this post status.
3919  * @param string  $old_status Previous post status.
3920  * @param WP_Post $post Post data.
3921  */
3922 function wp_transition_post_status( $new_status, $old_status, $post ) {
3923         /**
3924          * Fires when a post is transitioned from one status to another.
3925          *
3926          * @since 2.3.0
3927          *
3928          * @param string  $new_status New post status.
3929          * @param string  $old_status Old post status.
3930          * @param WP_Post $post       Post object.
3931          */
3932         do_action( 'transition_post_status', $new_status, $old_status, $post );
3933
3934         /**
3935          * Fires when a post is transitioned from one status to another.
3936          *
3937          * The dynamic portions of the hook name, `$new_status` and `$old status`,
3938          * refer to the old and new post statuses, respectively.
3939          *
3940          * @since 2.3.0
3941          *
3942          * @param WP_Post $post Post object.
3943          */
3944         do_action( "{$old_status}_to_{$new_status}", $post );
3945
3946         /**
3947          * Fires when a post is transitioned from one status to another.
3948          *
3949          * The dynamic portions of the hook name, `$new_status` and `$post->post_type`,
3950          * refer to the new post status and post type, respectively.
3951          *
3952          * @since 2.3.0
3953          *
3954          * @param int     $post_id Post ID.
3955          * @param WP_Post $post    Post object.
3956          */
3957         do_action( "{$new_status}_{$post->post_type}", $post->ID, $post );
3958 }
3959
3960 //
3961 // Trackback and ping functions
3962 //
3963
3964 /**
3965  * Add a URL to those already pinged.
3966  *
3967  * @since 1.5.0
3968  *
3969  * @global wpdb $wpdb WordPress database abstraction object.
3970  *
3971  * @param int    $post_id Post ID.
3972  * @param string $uri     Ping URI.
3973  * @return int How many rows were updated.
3974  */
3975 function add_ping( $post_id, $uri ) {
3976         global $wpdb;
3977         $pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id ));
3978         $pung = trim($pung);
3979         $pung = preg_split('/\s/', $pung);
3980         $pung[] = $uri;
3981         $new = implode("\n", $pung);
3982
3983         /**
3984          * Filter the new ping URL to add for the given post.
3985          *
3986          * @since 2.0.0
3987          *
3988          * @param string $new New ping URL to add.
3989          */
3990         $new = apply_filters( 'add_ping', $new );
3991
3992         // expected_slashed ($new).
3993         $new = wp_unslash($new);
3994         return $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post_id ) );
3995 }
3996
3997 /**
3998  * Retrieve enclosures already enclosed for a post.
3999  *
4000  * @since 1.5.0
4001  *
4002  * @param int $post_id Post ID.
4003  * @return array List of enclosures.
4004  */
4005 function get_enclosed( $post_id ) {
4006         $custom_fields = get_post_custom( $post_id );
4007         $pung = array();
4008         if ( !is_array( $custom_fields ) )
4009                 return $pung;
4010
4011         foreach ( $custom_fields as $key => $val ) {
4012                 if ( 'enclosure' != $key || !is_array( $val ) )
4013                         continue;
4014                 foreach( $val as $enc ) {
4015                         $enclosure = explode( "\n", $enc );
4016                         $pung[] = trim( $enclosure[ 0 ] );
4017                 }
4018         }
4019
4020         /**
4021          * Filter the list of enclosures already enclosed for the given post.
4022          *
4023          * @since 2.0.0
4024          *
4025          * @param array $pung    Array of enclosures for the given post.
4026          * @param int   $post_id Post ID.
4027          */
4028         $pung = apply_filters( 'get_enclosed', $pung, $post_id );
4029         return $pung;
4030 }
4031
4032 /**
4033  * Retrieve URLs already pinged for a post.
4034  *
4035  * @since 1.5.0
4036  *
4037  * @global wpdb $wpdb WordPress database abstraction object.
4038  *
4039  * @param int $post_id Post ID.
4040  * @return array
4041  */
4042 function get_pung( $post_id ) {
4043         global $wpdb;
4044         $pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id ));
4045         $pung = trim($pung);
4046         $pung = preg_split('/\s/', $pung);
4047
4048         /**
4049          * Filter the list of already-pinged URLs for the given post.
4050          *
4051          * @since 2.0.0
4052          *
4053          * @param array $pung Array of URLs already pinged for the given post.
4054          */
4055         $pung = apply_filters( 'get_pung', $pung );
4056         return $pung;
4057 }
4058
4059 /**
4060  * Retrieve URLs that need to be pinged.
4061  *
4062  * @since 1.5.0
4063  *
4064  * @global wpdb $wpdb WordPress database abstraction object.
4065  *
4066  * @param int $post_id Post ID
4067  * @return array
4068  */
4069 function get_to_ping( $post_id ) {
4070         global $wpdb;
4071         $to_ping = $wpdb->get_var( $wpdb->prepare( "SELECT to_ping FROM $wpdb->posts WHERE ID = %d", $post_id ));
4072         $to_ping = sanitize_trackback_urls( $to_ping );
4073         $to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY);
4074
4075         /**
4076          * Filter the list of URLs yet to ping for the given post.
4077          *
4078          * @since 2.0.0
4079          *
4080          * @param array $to_ping List of URLs yet to ping.
4081          */
4082         $to_ping = apply_filters( 'get_to_ping', $to_ping );
4083         return $to_ping;
4084 }
4085
4086 /**
4087  * Do trackbacks for a list of URLs.
4088  *
4089  * @since 1.0.0
4090  *
4091  * @param string $tb_list Comma separated list of URLs.
4092  * @param int    $post_id Post ID.
4093  */
4094 function trackback_url_list( $tb_list, $post_id ) {
4095         if ( ! empty( $tb_list ) ) {
4096                 // Get post data.
4097                 $postdata = get_post( $post_id, ARRAY_A );
4098
4099                 // Form an excerpt.
4100                 $excerpt = strip_tags( $postdata['post_excerpt'] ? $postdata['post_excerpt'] : $postdata['post_content'] );
4101
4102                 if ( strlen( $excerpt ) > 255 ) {
4103                         $excerpt = substr( $excerpt, 0, 252 ) . '&hellip;';
4104                 }
4105
4106                 $trackback_urls = explode( ',', $tb_list );
4107                 foreach( (array) $trackback_urls as $tb_url ) {
4108                         $tb_url = trim( $tb_url );
4109                         trackback( $tb_url, wp_unslash( $postdata['post_title'] ), $excerpt, $post_id );
4110                 }
4111         }
4112 }
4113
4114 //
4115 // Page functions
4116 //
4117
4118 /**
4119  * Get a list of page IDs.
4120  *
4121  * @since 2.0.0
4122  *
4123  * @global wpdb $wpdb WordPress database abstraction object.
4124  *
4125  * @return array List of page IDs.
4126  */
4127 function get_all_page_ids() {
4128         global $wpdb;
4129
4130         $page_ids = wp_cache_get('all_page_ids', 'posts');
4131         if ( ! is_array( $page_ids ) ) {
4132                 $page_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'page'");
4133                 wp_cache_add('all_page_ids', $page_ids, 'posts');
4134         }
4135
4136         return $page_ids;
4137 }
4138
4139 /**
4140  * Retrieves page data given a page ID or page object.
4141  *
4142  * Use get_post() instead of get_page().
4143  *
4144  * @since 1.5.1
4145  * @deprecated 3.5.0 Use get_post()
4146  *
4147  * @param mixed  $page   Page object or page ID. Passed by reference.
4148  * @param string $output Optional. What to output. Accepts OBJECT, ARRAY_A, or ARRAY_N.
4149  *                       Default OBJECT.
4150  * @param string $filter Optional. How the return value should be filtered. Accepts 'raw',
4151  *                       'edit', 'db', 'display'. Default 'raw'.
4152  * @return WP_Post|null WP_Post on success or null on failure.
4153  */
4154 function get_page( $page, $output = OBJECT, $filter = 'raw') {
4155         return get_post( $page, $output, $filter );
4156 }
4157
4158 /**
4159  * Retrieves a page given its path.
4160  *
4161  * @since 2.1.0
4162  *
4163  * @global wpdb $wpdb WordPress database abstraction object.
4164  *
4165  * @param string       $page_path Page path.
4166  * @param string       $output    Optional. Output type. Accepts OBJECT, ARRAY_N, or ARRAY_A.
4167  *                                Default OBJECT.
4168  * @param string|array $post_type Optional. Post type or array of post types. Default 'page'.
4169  * @return WP_Post|null WP_Post on success or null on failure.
4170  */
4171 function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
4172         global $wpdb;
4173
4174         $page_path = rawurlencode(urldecode($page_path));
4175         $page_path = str_replace('%2F', '/', $page_path);
4176         $page_path = str_replace('%20', ' ', $page_path);
4177         $parts = explode( '/', trim( $page_path, '/' ) );
4178         $parts = esc_sql( $parts );
4179         $parts = array_map( 'sanitize_title_for_query', $parts );
4180
4181         $in_string = "'" . implode( "','", $parts ) . "'";
4182
4183         if ( is_array( $post_type ) ) {
4184                 $post_types = $post_type;
4185         } else {
4186                 $post_types = array( $post_type, 'attachment' );
4187         }
4188
4189         $post_types = esc_sql( $post_types );
4190         $post_type_in_string = "'" . implode( "','", $post_types ) . "'";
4191         $sql = "
4192                 SELECT ID, post_name, post_parent, post_type
4193                 FROM $wpdb->posts
4194                 WHERE post_name IN ($in_string)
4195                 AND post_type IN ($post_type_in_string)
4196         ";
4197
4198         $pages = $wpdb->get_results( $sql, OBJECT_K );
4199
4200         $revparts = array_reverse( $parts );
4201
4202         $foundid = 0;
4203         foreach ( (array) $pages as $page ) {
4204                 if ( $page->post_name == $revparts[0] ) {
4205                         $count = 0;
4206                         $p = $page;
4207                         while ( $p->post_parent != 0 && isset( $pages[ $p->post_parent ] ) ) {
4208                                 $count++;
4209                                 $parent = $pages[ $p->post_parent ];
4210                                 if ( ! isset( $revparts[ $count ] ) || $parent->post_name != $revparts[ $count ] )
4211                                         break;
4212                                 $p = $parent;
4213                         }
4214
4215                         if ( $p->post_parent == 0 && $count+1 == count( $revparts ) && $p->post_name == $revparts[ $count ] ) {
4216                                 $foundid = $page->ID;
4217                                 if ( $page->post_type == $post_type )
4218                                         break;
4219                         }
4220                 }
4221         }
4222
4223         if ( $foundid )
4224                 return get_post( $foundid, $output );
4225
4226         return null;
4227 }
4228
4229 /**
4230  * Retrieve a page given its title.
4231  *
4232  * @since 2.1.0
4233  *
4234  * @global wpdb $wpdb WordPress database abstraction object.
4235  *
4236  * @param string       $page_title Page title
4237  * @param string       $output     Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A.
4238  *                                 Default OBJECT.
4239  * @param string|array $post_type  Optional. Post type or array of post types. Default 'page'.
4240  * @return WP_Post|null WP_Post on success or null on failure
4241  */
4242 function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
4243         global $wpdb;
4244
4245         if ( is_array( $post_type ) ) {
4246                 $post_type = esc_sql( $post_type );
4247                 $post_type_in_string = "'" . implode( "','", $post_type ) . "'";
4248                 $sql = $wpdb->prepare( "
4249                         SELECT ID
4250                         FROM $wpdb->posts
4251                         WHERE post_title = %s
4252                         AND post_type IN ($post_type_in_string)
4253                 ", $page_title );
4254         } else {
4255                 $sql = $wpdb->prepare( "
4256                         SELECT ID
4257                         FROM $wpdb->posts
4258                         WHERE post_title = %s
4259                         AND post_type = %s
4260                 ", $page_title, $post_type );
4261         }
4262
4263         $page = $wpdb->get_var( $sql );
4264
4265         if ( $page )
4266                 return get_post( $page, $output );
4267
4268         return null;
4269 }
4270
4271 /**
4272  * Retrieve child pages from list of pages matching page ID.
4273  *
4274  * Matches against the pages parameter against the page ID. Also matches all
4275  * children for the same to retrieve all children of a page. Does not make any
4276  * SQL queries to get the children.
4277  *
4278  * @since 1.5.1
4279  *
4280  * @param int   $page_id    Page ID.
4281  * @param array $pages      List of pages' objects.
4282  * @return array List of page children.
4283  */
4284 function get_page_children( $page_id, $pages ) {
4285         $page_list = array();
4286         foreach ( (array) $pages as $page ) {
4287                 if ( $page->post_parent == $page_id ) {
4288                         $page_list[] = $page;
4289                         if ( $children = get_page_children( $page->ID, $pages ) ) {
4290                                 $page_list = array_merge( $page_list, $children );
4291                         }
4292                 }
4293         }
4294
4295         return $page_list;
4296 }
4297
4298 /**
4299  * Order the pages with children under parents in a flat list.
4300  *
4301  * It uses auxiliary structure to hold parent-children relationships and
4302  * runs in O(N) complexity
4303  *
4304  * @since 2.0.0
4305  *
4306  * @param array $pages   Posts array, passed by reference.
4307  * @param int   $page_id Optional. Parent page ID. Default 0.
4308  * @return array A list arranged by hierarchy. Children immediately follow their parents.
4309  */
4310 function get_page_hierarchy( &$pages, $page_id = 0 ) {
4311         if ( empty( $pages ) ) {
4312                 $result = array();
4313                 return $result;
4314         }
4315
4316         $children = array();
4317         foreach ( (array) $pages as $p ) {
4318                 $parent_id = intval( $p->post_parent );
4319                 $children[ $parent_id ][] = $p;
4320         }
4321
4322         $result = array();
4323         _page_traverse_name( $page_id, $children, $result );
4324
4325         return $result;
4326 }
4327
4328 /**
4329  * Traverse and return all the nested children post names of a root page.
4330  *
4331  * $children contains parent-children relations
4332  *
4333  * @since 2.9.0
4334  *
4335  * @see _page_traverse_name()
4336  *
4337  * @param int   $page_id   Page ID.
4338  * @param array &$children Parent-children relations, passed by reference.
4339  * @param array &$result   Result, passed by reference.
4340  */
4341 function _page_traverse_name( $page_id, &$children, &$result ){
4342         if ( isset( $children[ $page_id ] ) ){
4343                 foreach( (array)$children[ $page_id ] as $child ) {
4344                         $result[ $child->ID ] = $child->post_name;
4345                         _page_traverse_name( $child->ID, $children, $result );
4346                 }
4347         }
4348 }
4349
4350 /**
4351  * Build URI for a page.
4352  *
4353  * Sub pages will be in the "directory" under the parent page post name.
4354  *
4355  * @since 1.5.0
4356  *
4357  * @param WP_Post|object|int $page Page object or page ID.
4358  * @return string|false Page URI, false on error.
4359  */
4360 function get_page_uri( $page ) {
4361         $page = get_post( $page );
4362
4363         if ( ! $page )
4364                 return false;
4365
4366         $uri = $page->post_name;
4367
4368         foreach ( $page->ancestors as $parent ) {
4369                 $uri = get_post( $parent )->post_name . '/' . $uri;
4370         }
4371
4372         return $uri;
4373 }
4374
4375 /**
4376  * Retrieve a list of pages.
4377  *
4378  * @global wpdb $wpdb WordPress database abstraction object.
4379  *
4380  * @since 1.5.0
4381  *
4382  * @param array|string $args {
4383  *     Optional. Array or string of arguments to retrieve pages.
4384  *
4385  *     @type int          $child_of     Page ID to return child and grandchild pages of.
4386  *                                      Default 0, or no restriction.
4387  *     @type string       $sort_order   How to sort retrieved pages. Accepts 'ASC', 'DESC'. Default 'ASC'.
4388  *     @type string       $sort_column  What columns to sort pages by, comma-separated. Accepts 'post_author',
4389  *                                      'post_date', 'post_title', 'post_name', 'post_modified', 'menu_order',
4390  *                                      'post_modified_gmt', 'post_parent', 'ID', 'rand', 'comment_count'.
4391  *                                      'post_' can be omitted for any values that start with it.
4392  *                                      Default 'post_title'.
4393  *     @type bool         $hierarchical Whether to return pages hierarchically. Default true.
4394  *     @type array        $exclude      Array of page IDs to exclude. Default empty array.
4395  *     @type array        $include      Array of page IDs to include. Cannot be used with `$child_of`,
4396  *                                      `$parent`, `$exclude`, `$meta_key`, `$meta_value`, or `$hierarchical`.
4397  *                                      Default empty array.
4398  *     @type string       $meta_key     Only include pages with this meta key. Default empty.
4399  *     @type string       $meta_value   Only include pages with this meta value. Requires `$meta_key`.
4400  *                                      Default empty.
4401  *     @type string       $authors      A comma-separated list of author IDs. Default empty.
4402  *     @type int          $parent       Page ID to return direct children of. `$hierarchical` must be false.
4403  *                                      Default -1, or no restriction.
4404  *     @type string|array $exclude_tree Comma-separated string or array of page IDs to exclude.
4405  *                                      Default empty array.
4406  *     @type int          $number       The number of pages to return. Default 0, or all pages.
4407  *     @type int          $offset       The number of pages to skip before returning. Requires `$number`.
4408  *                                      Default 0.
4409  *     @type string       $post_type    The post type to query. Default 'page'.
4410  *     @type string       $post_status  A comma-separated list of post status types to include.
4411  *                                      Default 'publish'.
4412  * }
4413  * @return array List of pages matching defaults or `$args`.
4414  */
4415 function get_pages( $args = array() ) {
4416         global $wpdb;
4417
4418         $defaults = array(
4419                 'child_of' => 0, 'sort_order' => 'ASC',
4420                 'sort_column' => 'post_title', 'hierarchical' => 1,
4421                 'exclude' => array(), 'include' => array(),
4422                 'meta_key' => '', 'meta_value' => '',
4423                 'authors' => '', 'parent' => -1, 'exclude_tree' => array(),
4424                 'number' => '', 'offset' => 0,
4425                 'post_type' => 'page', 'post_status' => 'publish',
4426         );
4427
4428         $r = wp_parse_args( $args, $defaults );
4429
4430         $number = (int) $r['number'];
4431         $offset = (int) $r['offset'];
4432         $child_of = (int) $r['child_of'];
4433         $hierarchical = $r['hierarchical'];
4434         $exclude = $r['exclude'];
4435         $meta_key = $r['meta_key'];
4436         $meta_value = $r['meta_value'];
4437         $parent = $r['parent'];
4438         $post_status = $r['post_status'];
4439
4440         // Make sure the post type is hierarchical.
4441         $hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) );
4442         if ( ! in_array( $r['post_type'], $hierarchical_post_types ) ) {
4443                 return false;
4444         }
4445
4446         if ( $parent > 0 && ! $child_of ) {
4447                 $hierarchical = false;
4448         }
4449
4450         // Make sure we have a valid post status.
4451         if ( ! is_array( $post_status ) ) {
4452                 $post_status = explode( ',', $post_status );
4453         }
4454         if ( array_diff( $post_status, get_post_stati() ) ) {
4455                 return false;
4456         }
4457
4458         // $args can be whatever, only use the args defined in defaults to compute the key.
4459         $key = md5( serialize( wp_array_slice_assoc( $r, array_keys( $defaults ) ) ) );
4460         $last_changed = wp_cache_get( 'last_changed', 'posts' );
4461         if ( ! $last_changed ) {
4462                 $last_changed = microtime();
4463                 wp_cache_set( 'last_changed', $last_changed, 'posts' );
4464         }
4465
4466         $cache_key = "get_pages:$key:$last_changed";
4467         if ( $cache = wp_cache_get( $cache_key, 'posts' ) ) {
4468                 // Convert to WP_Post instances.
4469                 $pages = array_map( 'get_post', $cache );
4470                 /** This filter is documented in wp-includes/post.php */
4471                 $pages = apply_filters( 'get_pages', $pages, $r );
4472                 return $pages;
4473         }
4474
4475         $inclusions = '';
4476         if ( ! empty( $r['include'] ) ) {
4477                 $child_of = 0; //ignore child_of, parent, exclude, meta_key, and meta_value params if using include
4478                 $parent = -1;
4479                 $exclude = '';
4480                 $meta_key = '';
4481                 $meta_value = '';
4482                 $hierarchical = false;
4483                 $incpages = wp_parse_id_list( $r['include'] );
4484                 if ( ! empty( $incpages ) ) {
4485                         $inclusions = ' AND ID IN (' . implode( ',', $incpages ) .  ')';
4486                 }
4487         }
4488
4489         $exclusions = '';
4490         if ( ! empty( $exclude ) ) {
4491                 $expages = wp_parse_id_list( $exclude );
4492                 if ( ! empty( $expages ) ) {
4493                         $exclusions = ' AND ID NOT IN (' . implode( ',', $expages ) .  ')';
4494                 }
4495         }
4496
4497         $author_query = '';
4498         if ( ! empty( $r['authors'] ) ) {
4499                 $post_authors = preg_split( '/[\s,]+/', $r['authors'] );
4500
4501                 if ( ! empty( $post_authors ) ) {
4502                         foreach ( $post_authors as $post_author ) {
4503                                 //Do we have an author id or an author login?
4504                                 if ( 0 == intval($post_author) ) {
4505                                         $post_author = get_user_by('login', $post_author);
4506                                         if ( empty( $post_author ) ) {
4507                                                 continue;
4508                                         }
4509                                         if ( empty( $post_author->ID ) ) {
4510                                                 continue;
4511                                         }
4512                                         $post_author = $post_author->ID;
4513                                 }
4514
4515                                 if ( '' == $author_query ) {
4516                                         $author_query = $wpdb->prepare(' post_author = %d ', $post_author);
4517                                 } else {
4518                                         $author_query .= $wpdb->prepare(' OR post_author = %d ', $post_author);
4519                                 }
4520                         }
4521                         if ( '' != $author_query ) {
4522                                 $author_query = " AND ($author_query)";
4523                         }
4524                 }
4525         }
4526
4527         $join = '';
4528         $where = "$exclusions $inclusions ";
4529         if ( '' !== $meta_key || '' !== $meta_value ) {
4530                 $join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )";
4531
4532                 // meta_key and meta_value might be slashed
4533                 $meta_key = wp_unslash($meta_key);
4534                 $meta_value = wp_unslash($meta_value);
4535                 if ( '' !== $meta_key ) {
4536                         $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s", $meta_key);
4537                 }
4538                 if ( '' !== $meta_value ) {
4539                         $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_value = %s", $meta_value);
4540                 }
4541
4542         }
4543
4544         if ( is_array( $parent ) ) {
4545                 $post_parent__in = implode( ',', array_map( 'absint', (array) $parent ) );
4546                 if ( ! empty( $post_parent__in ) ) {
4547                         $where .= " AND post_parent IN ($post_parent__in)";
4548                 }
4549         } elseif ( $parent >= 0 ) {
4550                 $where .= $wpdb->prepare(' AND post_parent = %d ', $parent);
4551         }
4552
4553         if ( 1 == count( $post_status ) ) {
4554                 $where_post_type = $wpdb->prepare( "post_type = %s AND post_status = %s", $r['post_type'], array_shift( $post_status ) );
4555         } else {
4556                 $post_status = implode( "', '", $post_status );
4557                 $where_post_type = $wpdb->prepare( "post_type = %s AND post_status IN ('$post_status')", $r['post_type'] );
4558         }
4559
4560         $orderby_array = array();
4561         $allowed_keys = array( 'author', 'post_author', 'date', 'post_date', 'title', 'post_title', 'name', 'post_name', 'modified',
4562                 'post_modified', 'modified_gmt', 'post_modified_gmt', 'menu_order', 'parent', 'post_parent',
4563                 'ID', 'rand', 'comment_count' );
4564
4565         foreach ( explode( ',', $r['sort_column'] ) as $orderby ) {
4566                 $orderby = trim( $orderby );
4567                 if ( ! in_array( $orderby, $allowed_keys ) ) {
4568                         continue;
4569                 }
4570
4571                 switch ( $orderby ) {
4572                         case 'menu_order':
4573                                 break;
4574                         case 'ID':
4575                                 $orderby = "$wpdb->posts.ID";
4576                                 break;
4577                         case 'rand':
4578                                 $orderby = 'RAND()';
4579                                 break;
4580                         case 'comment_count':
4581                                 $orderby = "$wpdb->posts.comment_count";
4582                                 break;
4583                         default:
4584                                 if ( 0 === strpos( $orderby, 'post_' ) ) {
4585                                         $orderby = "$wpdb->posts." . $orderby;
4586                                 } else {
4587                                         $orderby = "$wpdb->posts.post_" . $orderby;
4588                                 }
4589                 }
4590
4591                 $orderby_array[] = $orderby;
4592
4593         }
4594         $sort_column = ! empty( $orderby_array ) ? implode( ',', $orderby_array ) : "$wpdb->posts.post_title";
4595
4596         $sort_order = strtoupper( $r['sort_order'] );
4597         if ( '' !== $sort_order && ! in_array( $sort_order, array( 'ASC', 'DESC' ) ) ) {
4598                 $sort_order = 'ASC';
4599         }
4600
4601         $query = "SELECT * FROM $wpdb->posts $join WHERE ($where_post_type) $where ";
4602         $query .= $author_query;
4603         $query .= " ORDER BY " . $sort_column . " " . $sort_order ;
4604
4605         if ( ! empty( $number ) ) {
4606                 $query .= ' LIMIT ' . $offset . ',' . $number;
4607         }
4608
4609         $pages = $wpdb->get_results($query);
4610
4611         if ( empty($pages) ) {
4612                 /** This filter is documented in wp-includes/post.php */
4613                 $pages = apply_filters( 'get_pages', array(), $r );
4614                 return $pages;
4615         }
4616
4617         // Sanitize before caching so it'll only get done once.
4618         $num_pages = count($pages);
4619         for ($i = 0; $i < $num_pages; $i++) {
4620                 $pages[$i] = sanitize_post($pages[$i], 'raw');
4621         }
4622
4623         // Update cache.
4624         update_post_cache( $pages );
4625
4626         if ( $child_of || $hierarchical ) {
4627                 $pages = get_page_children($child_of, $pages);
4628         }
4629
4630         if ( ! empty( $r['exclude_tree'] ) ) {
4631                 $exclude = wp_parse_id_list( $r['exclude_tree'] );
4632                 foreach( $exclude as $id ) {
4633                         $children = get_page_children( $id, $pages );
4634                         foreach ( $children as $child ) {
4635                                 $exclude[] = $child->ID;
4636                         }
4637                 }
4638
4639                 $num_pages = count( $pages );
4640                 for ( $i = 0; $i < $num_pages; $i++ ) {
4641                         if ( in_array( $pages[$i]->ID, $exclude ) ) {
4642                                 unset( $pages[$i] );
4643                         }
4644                 }
4645         }
4646
4647         $page_structure = array();
4648         foreach ( $pages as $page ) {
4649                 $page_structure[] = $page->ID;
4650         }
4651
4652         wp_cache_set( $cache_key, $page_structure, 'posts' );
4653
4654         // Convert to WP_Post instances
4655         $pages = array_map( 'get_post', $pages );
4656
4657         /**
4658          * Filter the retrieved list of pages.
4659          *
4660          * @since 2.1.0
4661          *
4662          * @param array $pages List of pages to retrieve.
4663          * @param array $r     Array of get_pages() arguments.
4664          */
4665         $pages = apply_filters( 'get_pages', $pages, $r );
4666
4667         return $pages;
4668 }
4669
4670 //
4671 // Attachment functions
4672 //
4673
4674 /**
4675  * Check if the attachment URI is local one and is really an attachment.
4676  *
4677  * @since 2.0.0
4678  *
4679  * @param string $url URL to check
4680  * @return bool True on success, false on failure.
4681  */
4682 function is_local_attachment($url) {
4683         if (strpos($url, home_url()) === false)
4684                 return false;
4685         if (strpos($url, home_url('/?attachment_id=')) !== false)
4686                 return true;
4687         if ( $id = url_to_postid($url) ) {
4688                 $post = get_post($id);
4689                 if ( 'attachment' == $post->post_type )
4690                         return true;
4691         }
4692         return false;
4693 }
4694
4695 /**
4696  * Insert an attachment.
4697  *
4698  * If you set the 'ID' in the $args parameter, it will mean that you are
4699  * updating and attempt to update the attachment. You can also set the
4700  * attachment name or title by setting the key 'post_name' or 'post_title'.
4701  *
4702  * You can set the dates for the attachment manually by setting the 'post_date'
4703  * and 'post_date_gmt' keys' values.
4704  *
4705  * By default, the comments will use the default settings for whether the
4706  * comments are allowed. You can close them manually or keep them open by
4707  * setting the value for the 'comment_status' key.
4708  *
4709  * @since 2.0.0
4710  *
4711  * @see wp_insert_post()
4712  *
4713  * @param string|array $args   Arguments for inserting an attachment.
4714  * @param string       $file   Optional. Filename.
4715  * @param int          $parent Optional. Parent post ID.
4716  * @return int Attachment ID.
4717  */
4718 function wp_insert_attachment( $args, $file = false, $parent = 0 ) {
4719         $defaults = array(
4720                 'file'        => $file,
4721                 'post_parent' => 0
4722         );
4723
4724         $data = wp_parse_args( $args, $defaults );
4725
4726         if ( ! empty( $parent ) ) {
4727                 $data['post_parent'] = $parent;
4728         }
4729
4730         $data['post_type'] = 'attachment';
4731
4732         return wp_insert_post( $data );
4733 }
4734
4735 /**
4736  * Trash or delete an attachment.
4737  *
4738  * When an attachment is permanently deleted, the file will also be removed.
4739  * Deletion removes all post meta fields, taxonomy, comments, etc. associated
4740  * with the attachment (except the main post).
4741  *
4742  * The attachment is moved to the trash instead of permanently deleted unless trash
4743  * for media is disabled, item is already in the trash, or $force_delete is true.
4744  *
4745  * @since 2.0.0
4746  *
4747  * @global wpdb $wpdb WordPress database abstraction object.
4748  *
4749  * @param int  $post_id      Attachment ID.
4750  * @param bool $force_delete Optional. Whether to bypass trash and force deletion.
4751  *                           Default false.
4752  * @return mixed False on failure. Post data on success.
4753  */
4754 function wp_delete_attachment( $post_id, $force_delete = false ) {
4755         global $wpdb;
4756
4757         if ( !$post = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id) ) )
4758                 return $post;
4759
4760         if ( 'attachment' != $post->post_type )
4761                 return false;
4762
4763         if ( !$force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' != $post->post_status )
4764                 return wp_trash_post( $post_id );
4765
4766         delete_post_meta($post_id, '_wp_trash_meta_status');
4767         delete_post_meta($post_id, '_wp_trash_meta_time');
4768
4769         $meta = wp_get_attachment_metadata( $post_id );
4770         $backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true );
4771         $file = get_attached_file( $post_id );
4772
4773         if ( is_multisite() )
4774                 delete_transient( 'dirsize_cache' );
4775
4776         /**
4777          * Fires before an attachment is deleted, at the start of wp_delete_attachment().
4778          *
4779          * @since 2.0.0
4780          *
4781          * @param int $post_id Attachment ID.
4782          */
4783         do_action( 'delete_attachment', $post_id );
4784
4785         wp_delete_object_term_relationships($post_id, array('category', 'post_tag'));
4786         wp_delete_object_term_relationships($post_id, get_object_taxonomies($post->post_type));
4787
4788         // Delete all for any posts.
4789         delete_metadata( 'post', null, '_thumbnail_id', $post_id, true );
4790
4791         $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ));
4792         foreach ( $comment_ids as $comment_id )
4793                 wp_delete_comment( $comment_id, true );
4794
4795         $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id ));
4796         foreach ( $post_meta_ids as $mid )
4797                 delete_metadata_by_mid( 'post', $mid );
4798
4799         /** This action is documented in wp-includes/post.php */
4800         do_action( 'delete_post', $post_id );
4801         $result = $wpdb->delete( $wpdb->posts, array( 'ID' => $post_id ) );
4802         if ( ! $result ) {
4803                 return false;
4804         }
4805         /** This action is documented in wp-includes/post.php */
4806         do_action( 'deleted_post', $post_id );
4807
4808         $uploadpath = wp_upload_dir();
4809
4810         if ( ! empty($meta['thumb']) ) {
4811                 // Don't delete the thumb if another attachment uses it.
4812                 if (! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $wpdb->esc_like( $meta['thumb'] ) . '%', $post_id)) ) {
4813                         $thumbfile = str_replace(basename($file), $meta['thumb'], $file);
4814                         /** This filter is documented in wp-admin/custom-header.php */
4815                         $thumbfile = apply_filters( 'wp_delete_file', $thumbfile );
4816                         @ unlink( path_join($uploadpath['basedir'], $thumbfile) );
4817                 }
4818         }
4819
4820         // Remove intermediate and backup images if there are any.
4821         if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
4822                 foreach ( $meta['sizes'] as $size => $sizeinfo ) {
4823                         $intermediate_file = str_replace( basename( $file ), $sizeinfo['file'], $file );
4824                         /** This filter is documented in wp-admin/custom-header.php */
4825                         $intermediate_file = apply_filters( 'wp_delete_file', $intermediate_file );
4826                         @ unlink( path_join( $uploadpath['basedir'], $intermediate_file ) );
4827                 }
4828         }
4829
4830         if ( is_array($backup_sizes) ) {
4831                 foreach ( $backup_sizes as $size ) {
4832                         $del_file = path_join( dirname($meta['file']), $size['file'] );
4833                         /** This filter is documented in wp-admin/custom-header.php */
4834                         $del_file = apply_filters( 'wp_delete_file', $del_file );
4835                         @ unlink( path_join($uploadpath['basedir'], $del_file) );
4836                 }
4837         }
4838
4839         /** This filter is documented in wp-admin/custom-header.php */
4840         $file = apply_filters( 'wp_delete_file', $file );
4841
4842         if ( ! empty($file) )
4843                 @ unlink($file);
4844
4845         clean_post_cache( $post );
4846
4847         return $post;
4848 }
4849
4850 /**
4851  * Retrieve attachment meta field for attachment ID.
4852  *
4853  * @since 2.1.0
4854  *
4855  * @param int  $post_id    Attachment ID. Default 0.
4856  * @param bool $unfiltered Optional. If true, filters are not run. Default false.
4857  * @return string|bool Attachment meta field. False on failure.
4858  */
4859 function wp_get_attachment_metadata( $post_id = 0, $unfiltered = false ) {
4860         $post_id = (int) $post_id;
4861         if ( !$post = get_post( $post_id ) )
4862                 return false;
4863
4864         $data = get_post_meta( $post->ID, '_wp_attachment_metadata', true );
4865
4866         if ( $unfiltered )
4867                 return $data;
4868
4869         /**
4870          * Filter the attachment meta data.
4871          *
4872          * @since 2.1.0
4873          *
4874          * @param array|bool $data    Array of meta data for the given attachment, or false
4875          *                            if the object does not exist.
4876          * @param int        $post_id Attachment ID.
4877          */
4878         return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID );
4879 }
4880
4881 /**
4882  * Update metadata for an attachment.
4883  *
4884  * @since 2.1.0
4885  *
4886  * @param int   $post_id Attachment ID.
4887  * @param array $data    Attachment data.
4888  * @return int|bool False if $post is invalid.
4889  */
4890 function wp_update_attachment_metadata( $post_id, $data ) {
4891         $post_id = (int) $post_id;
4892         if ( !$post = get_post( $post_id ) )
4893                 return false;
4894
4895         /**
4896          * Filter the updated attachment meta data.
4897          *
4898          * @since 2.1.0
4899          *
4900          * @param array $data    Array of updated attachment meta data.
4901          * @param int   $post_id Attachment ID.
4902          */
4903         if ( $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID ) )
4904                 return update_post_meta( $post->ID, '_wp_attachment_metadata', $data );
4905         else
4906                 return delete_post_meta( $post->ID, '_wp_attachment_metadata' );
4907 }
4908
4909 /**
4910  * Retrieve the URL for an attachment.
4911  *
4912  * @since 2.1.0
4913  *
4914  * @param int $post_id Optional. Attachment ID. Default 0.
4915  * @return string|bool Attachment URL, otherwise false.
4916  */
4917 function wp_get_attachment_url( $post_id = 0 ) {
4918         $post_id = (int) $post_id;
4919         if ( !$post = get_post( $post_id ) )
4920                 return false;
4921
4922         if ( 'attachment' != $post->post_type )
4923                 return false;
4924
4925         $url = '';
4926         // Get attached file.
4927         if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true) ) {
4928                 // Get upload directory.
4929                 if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) {
4930                         // Check that the upload base exists in the file location.
4931                         if ( 0 === strpos( $file, $uploads['basedir'] ) ) {
4932                                 // Replace file location with url location.
4933                                 $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
4934                         } elseif ( false !== strpos($file, 'wp-content/uploads') ) {
4935                                 $url = $uploads['baseurl'] . substr( $file, strpos($file, 'wp-content/uploads') + 18 );
4936                         } else {
4937                                 // It's a newly-uploaded file, therefore $file is relative to the basedir.
4938                                 $url = $uploads['baseurl'] . "/$file";
4939                         }
4940                 }
4941         }
4942
4943         /*
4944          * If any of the above options failed, Fallback on the GUID as used pre-2.7,
4945          * not recommended to rely upon this.
4946          */
4947         if ( empty($url) ) {
4948                 $url = get_the_guid( $post->ID );
4949         }
4950
4951         /**
4952          * Filter the attachment URL.
4953          *
4954          * @since 2.1.0
4955          *
4956          * @param string $url     URL for the given attachment.
4957          * @param int    $post_id Attachment ID.
4958          */
4959         $url = apply_filters( 'wp_get_attachment_url', $url, $post->ID );
4960
4961         if ( empty( $url ) )
4962                 return false;
4963
4964         return $url;
4965 }
4966
4967 /**
4968  * Retrieve thumbnail for an attachment.
4969  *
4970  * @since 2.1.0
4971  *
4972  * @param int $post_id Optional. Attachment ID. Default 0.
4973  * @return mixed False on failure. Thumbnail file path on success.
4974  */
4975 function wp_get_attachment_thumb_file( $post_id = 0 ) {
4976         $post_id = (int) $post_id;
4977         if ( !$post = get_post( $post_id ) )
4978                 return false;
4979         if ( !is_array( $imagedata = wp_get_attachment_metadata( $post->ID ) ) )
4980                 return false;
4981
4982         $file = get_attached_file( $post->ID );
4983
4984         if ( !empty($imagedata['thumb']) && ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file)) && file_exists($thumbfile) ) {
4985                 /**
4986                  * Filter the attachment thumbnail file path.
4987                  *
4988                  * @since 2.1.0
4989                  *
4990                  * @param string $thumbfile File path to the attachment thumbnail.
4991                  * @param int    $post_id   Attachment ID.
4992                  */
4993                 return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID );
4994         }
4995         return false;
4996 }
4997
4998 /**
4999  * Retrieve URL for an attachment thumbnail.
5000  *
5001  * @since 2.1.0
5002  *
5003  * @param int $post_id Optional. Attachment ID. Default 0.
5004  * @return string|bool False on failure. Thumbnail URL on success.
5005  */
5006 function wp_get_attachment_thumb_url( $post_id = 0 ) {
5007         $post_id = (int) $post_id;
5008         if ( !$post = get_post( $post_id ) )
5009                 return false;
5010         if ( !$url = wp_get_attachment_url( $post->ID ) )
5011                 return false;
5012
5013         $sized = image_downsize( $post_id, 'thumbnail' );
5014         if ( $sized )
5015                 return $sized[0];
5016
5017         if ( !$thumb = wp_get_attachment_thumb_file( $post->ID ) )
5018                 return false;
5019
5020         $url = str_replace(basename($url), basename($thumb), $url);
5021
5022         /**
5023          * Filter the attachment thumbnail URL.
5024          *
5025          * @since 2.1.0
5026          *
5027          * @param string $url     URL for the attachment thumbnail.
5028          * @param int    $post_id Attachment ID.
5029          */
5030         return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID );
5031 }
5032
5033 /**
5034  * Check if the attachment is an image.
5035  *
5036  * @since 2.1.0
5037  *
5038  * @param int $post_id Optional. Attachment ID. Default 0.
5039  * @return bool Whether the attachment is an image.
5040  */
5041 function wp_attachment_is_image( $post_id = 0 ) {
5042         $post_id = (int) $post_id;
5043         if ( !$post = get_post( $post_id ) )
5044                 return false;
5045
5046         if ( !$file = get_attached_file( $post->ID ) )
5047                 return false;
5048
5049         $ext = preg_match('/\.([^.]+)$/', $file, $matches) ? strtolower($matches[1]) : false;
5050
5051         $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' );
5052
5053         if ( 'image/' == substr($post->post_mime_type, 0, 6) || $ext && 'import' == $post->post_mime_type && in_array($ext, $image_exts) )
5054                 return true;
5055         return false;
5056 }
5057
5058 /**
5059  * Retrieve the icon for a MIME type.
5060  *
5061  * @since 2.1.0
5062  *
5063  * @param string|int $mime MIME type or attachment ID.
5064  * @return string|bool Icon, false otherwise.
5065  */
5066 function wp_mime_type_icon( $mime = 0 ) {
5067         if ( !is_numeric($mime) )
5068                 $icon = wp_cache_get("mime_type_icon_$mime");
5069
5070         $post_id = 0;
5071         if ( empty($icon) ) {
5072                 $post_mimes = array();
5073                 if ( is_numeric($mime) ) {
5074                         $mime = (int) $mime;
5075                         if ( $post = get_post( $mime ) ) {
5076                                 $post_id = (int) $post->ID;
5077                                 $ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $post->guid);
5078                                 if ( !empty($ext) ) {
5079                                         $post_mimes[] = $ext;
5080                                         if ( $ext_type = wp_ext2type( $ext ) )
5081                                                 $post_mimes[] = $ext_type;
5082                                 }
5083                                 $mime = $post->post_mime_type;
5084                         } else {
5085                                 $mime = 0;
5086                         }
5087                 } else {
5088                         $post_mimes[] = $mime;
5089                 }
5090
5091                 $icon_files = wp_cache_get('icon_files');
5092
5093                 if ( !is_array($icon_files) ) {
5094                         /**
5095                          * Filter the icon directory path.
5096                          *
5097                          * @since 2.0.0
5098                          *
5099                          * @param string $path Icon directory absolute path.
5100                          */
5101                         $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );
5102
5103                         /**
5104                          * Filter the icon directory URI.
5105                          *
5106                          * @since 2.0.0
5107                          *
5108                          * @param string $uri Icon directory URI.
5109                          */
5110                         $icon_dir_uri = apply_filters( 'icon_dir_uri', includes_url( 'images/media' ) );
5111
5112                         /**
5113                          * Filter the list of icon directory URIs.
5114                          *
5115                          * @since 2.5.0
5116                          *
5117                          * @param array $uris List of icon directory URIs.
5118                          */
5119                         $dirs = apply_filters( 'icon_dirs', array( $icon_dir => $icon_dir_uri ) );
5120                         $icon_files = array();
5121                         while ( $dirs ) {
5122                                 $keys = array_keys( $dirs );
5123                                 $dir = array_shift( $keys );
5124                                 $uri = array_shift($dirs);
5125                                 if ( $dh = opendir($dir) ) {
5126                                         while ( false !== $file = readdir($dh) ) {
5127                                                 $file = basename($file);
5128                                                 if ( substr($file, 0, 1) == '.' )
5129                                                         continue;
5130                                                 if ( !in_array(strtolower(substr($file, -4)), array('.png', '.gif', '.jpg') ) ) {
5131                                                         if ( is_dir("$dir/$file") )
5132                                                                 $dirs["$dir/$file"] = "$uri/$file";
5133                                                         continue;
5134                                                 }
5135                                                 $icon_files["$dir/$file"] = "$uri/$file";
5136                                         }
5137                                         closedir($dh);
5138                                 }
5139                         }
5140                         wp_cache_add( 'icon_files', $icon_files, 'default', 600 );
5141                 }
5142
5143                 // Icon basename - extension = MIME wildcard.
5144                 foreach ( $icon_files as $file => $uri )
5145                         $types[ preg_replace('/^([^.]*).*$/', '$1', basename($file)) ] =& $icon_files[$file];
5146
5147                 if ( ! empty($mime) ) {
5148                         $post_mimes[] = substr($mime, 0, strpos($mime, '/'));
5149                         $post_mimes[] = substr($mime, strpos($mime, '/') + 1);
5150                         $post_mimes[] = str_replace('/', '_', $mime);
5151                 }
5152
5153                 $matches = wp_match_mime_types(array_keys($types), $post_mimes);
5154                 $matches['default'] = array('default');
5155
5156                 foreach ( $matches as $match => $wilds ) {
5157                         if ( isset($types[$wilds[0]])) {
5158                                 $icon = $types[$wilds[0]];
5159                                 if ( !is_numeric($mime) )
5160                                         wp_cache_add("mime_type_icon_$mime", $icon);
5161                                 break;
5162                         }
5163                 }
5164         }
5165
5166         /**
5167          * Filter the mime type icon.
5168          *
5169          * @since 2.1.0
5170          *
5171          * @param string $icon    Path to the mime type icon.
5172          * @param string $mime    Mime type.
5173          * @param int    $post_id Attachment ID. Will equal 0 if the function passed
5174          *                        the mime type.
5175          */
5176         return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id );
5177 }
5178
5179 /**
5180  * Check for changed slugs for published post objects and save the old slug.
5181  *
5182  * The function is used when a post object of any type is updated,
5183  * by comparing the current and previous post objects.
5184  *
5185  * If the slug was changed and not already part of the old slugs then it will be
5186  * added to the post meta field ('_wp_old_slug') for storing old slugs for that
5187  * post.
5188  *
5189  * The most logically usage of this function is redirecting changed post objects, so
5190  * that those that linked to an changed post will be redirected to the new post.
5191  *
5192  * @since 2.1.0
5193  *
5194  * @param int     $post_id     Post ID.
5195  * @param WP_Post $post        The Post Object
5196  * @param WP_Post $post_before The Previous Post Object
5197  * @return int Same as $post_id
5198  */
5199 function wp_check_for_changed_slugs( $post_id, $post, $post_before ) {
5200         // Don't bother if it hasnt changed.
5201         if ( $post->post_name == $post_before->post_name )
5202                 return;
5203
5204         // We're only concerned with published, non-hierarchical objects.
5205         if ( $post->post_status != 'publish' || is_post_type_hierarchical( $post->post_type ) )
5206                 return;
5207
5208         $old_slugs = (array) get_post_meta($post_id, '_wp_old_slug');
5209
5210         // If we haven't added this old slug before, add it now.
5211         if ( !empty( $post_before->post_name ) && !in_array($post_before->post_name, $old_slugs) )
5212                 add_post_meta($post_id, '_wp_old_slug', $post_before->post_name);
5213
5214         // If the new slug was used previously, delete it from the list.
5215         if ( in_array($post->post_name, $old_slugs) )
5216                 delete_post_meta($post_id, '_wp_old_slug', $post->post_name);
5217 }
5218
5219 /**
5220  * Retrieve the private post SQL based on capability.
5221  *
5222  * This function provides a standardized way to appropriately select on the
5223  * post_status of a post type. The function will return a piece of SQL code
5224  * that can be added to a WHERE clause; this SQL is constructed to allow all
5225  * published posts, and all private posts to which the user has access.
5226  *
5227  * @since 2.2.0
5228  *
5229  * @param string $post_type Post type. Currently only supports 'post' or 'page'.
5230  * @return string SQL code that can be added to a where clause.
5231  */
5232 function get_private_posts_cap_sql( $post_type ) {
5233         return get_posts_by_author_sql( $post_type, false );
5234 }
5235
5236 /**
5237  * Retrieve the post SQL based on capability, author, and type.
5238  *
5239  * @since 3.0.0
5240  *
5241  * @see get_private_posts_cap_sql()
5242  *
5243  * @param string $post_type   Post type.
5244  * @param bool   $full        Optional. Returns a full WHERE statement instead of just
5245  *                            an 'andalso' term. Default true.
5246  * @param int    $post_author Optional. Query posts having a single author ID. Default null.
5247  * @param bool   $public_only Optional. Only return public posts. Skips cap checks for
5248  *                            $current_user.  Default false.
5249  * @return string SQL WHERE code that can be added to a query.
5250  */
5251 function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) {
5252         global $wpdb;
5253
5254         // Private posts.
5255         $post_type_obj = get_post_type_object( $post_type );
5256         if ( ! $post_type_obj )
5257                 return $full ? 'WHERE 1 = 0' : ' 1 = 0 ';
5258
5259         /**
5260          * Filter the capability to read private posts for a custom post type
5261          * when generating SQL for getting posts by author.
5262          *
5263          * @since 2.2.0
5264          * @deprecated 3.2.0 The hook transitioned from "somewhat useless" to "totally useless".
5265          *
5266          * @param string $cap Capability.
5267          */
5268         if ( ! $cap = apply_filters( 'pub_priv_sql_capability', '' ) ) {
5269                 $cap = $post_type_obj->cap->read_private_posts;
5270         }
5271
5272         if ( $full ) {
5273                 if ( null === $post_author ) {
5274                         $sql = $wpdb->prepare( 'WHERE post_type = %s AND ', $post_type );
5275                 } else {
5276                         $sql = $wpdb->prepare( 'WHERE post_author = %d AND post_type = %s AND ', $post_author, $post_type );
5277                 }
5278         } else {
5279                 $sql = '';
5280         }
5281
5282         $sql .= "(post_status = 'publish'";
5283
5284         // Only need to check the cap if $public_only is false.
5285         if ( false === $public_only ) {
5286                 if ( current_user_can( $cap ) ) {
5287                         // Does the user have the capability to view private posts? Guess so.
5288                         $sql .= " OR post_status = 'private'";
5289                 } elseif ( is_user_logged_in() ) {
5290                         // Users can view their own private posts.
5291                         $id = get_current_user_id();
5292                         if ( null === $post_author || ! $full ) {
5293                                 $sql .= " OR post_status = 'private' AND post_author = $id";
5294                         } elseif ( $id == (int) $post_author ) {
5295                                 $sql .= " OR post_status = 'private'";
5296                         } // else none
5297                 } // else none
5298         }
5299
5300         $sql .= ')';
5301
5302         return $sql;
5303 }
5304
5305 /**
5306  * Retrieve the date that the last post was published.
5307  *
5308  * The server timezone is the default and is the difference between GMT and
5309  * server time. The 'blog' value is the date when the last post was posted. The
5310  * 'gmt' is when the last post was posted in GMT formatted date.
5311  *
5312  * @since 0.71
5313  *
5314  * @param string $timezone The location to get the time. Accepts 'gmt', 'blog',
5315  *                         or 'server'. Default 'server'.
5316  * @return string The date of the last post.
5317  */
5318 function get_lastpostdate( $timezone = 'server' ) {
5319         /**
5320          * Filter the date the last post was published.
5321          *
5322          * @since 2.3.0
5323          *
5324          * @param string $date     Date the last post was published. Likely values are 'gmt',
5325          *                         'blog', or 'server'.
5326          * @param string $timezone Location to use for getting the post published date.
5327          */
5328         return apply_filters( 'get_lastpostdate', _get_last_post_time( $timezone, 'date' ), $timezone );
5329 }
5330
5331 /**
5332  * Get the timestamp of the last time any post was modified.
5333  *
5334  * The server timezone is the default and is the difference between GMT and
5335  * server time. The 'blog' value is just when the last post was modified. The
5336  * 'gmt' is when the last post was modified in GMT time.
5337  *
5338  * @since 1.2.0
5339  *
5340  * @param string $timezone Optional. The timezone for the timestamp. Uses the server's internal timezone.
5341  *                         Accepts 'server', 'blog', 'gmt'. or 'server'. 'server' uses the server's
5342  *                         internal timezone. 'blog' uses the `post_modified` field, which proxies
5343  *                         to the timezone set for the site. 'gmt' uses the `post_modified_gmt` field.
5344  *                         Default 'server'.
5345  * @return string The timestamp.
5346  */
5347 function get_lastpostmodified( $timezone = 'server' ) {
5348         $lastpostmodified = _get_last_post_time( $timezone, 'modified' );
5349
5350         $lastpostdate = get_lastpostdate($timezone);
5351         if ( $lastpostdate > $lastpostmodified )
5352                 $lastpostmodified = $lastpostdate;
5353
5354         /**
5355          * Filter the date the last post was modified.
5356          *
5357          * @since 2.3.0
5358          *
5359          * @param string $lastpostmodified Date the last post was modified.
5360          * @param string $timezone         Location to use for getting the post modified date.
5361          *                                 See {@see get_lastpostmodified()} for accepted `$timezone` values.
5362          */
5363         return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone );
5364 }
5365
5366 /**
5367  * Get the timestamp of the last time any post was modified or published.
5368  *
5369  * @since 3.1.0
5370  * @access private
5371  *
5372  * @param string $timezone The timezone for the timestamp. See {@see get_lastpostmodified()}
5373  *                         for information on accepted values.
5374  * @param string $field    Post field to check. Accepts 'date' or 'modified'.
5375  * @return string The timestamp.
5376  */
5377 function _get_last_post_time( $timezone, $field ) {
5378         global $wpdb;
5379
5380         if ( !in_array( $field, array( 'date', 'modified' ) ) )
5381                 return false;
5382
5383         $timezone = strtolower( $timezone );
5384
5385         $key = "lastpost{$field}:$timezone";
5386
5387         $date = wp_cache_get( $key, 'timeinfo' );
5388
5389         if ( !$date ) {
5390                 $add_seconds_server = date('Z');
5391
5392                 $post_types = get_post_types( array( 'public' => true ) );
5393                 array_walk( $post_types, array( &$wpdb, 'escape_by_ref' ) );
5394                 $post_types = "'" . implode( "', '", $post_types ) . "'";
5395
5396                 switch ( $timezone ) {
5397                         case 'gmt':
5398                                 $date = $wpdb->get_var("SELECT post_{$field}_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
5399                                 break;
5400                         case 'blog':
5401                                 $date = $wpdb->get_var("SELECT post_{$field} FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
5402                                 break;
5403                         case 'server':
5404                                 $date = $wpdb->get_var("SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
5405                                 break;
5406                 }
5407
5408                 if ( $date )
5409                         wp_cache_set( $key, $date, 'timeinfo' );
5410         }
5411
5412         return $date;
5413 }
5414
5415 /**
5416  * Updates posts in cache.
5417  *
5418  * @since 1.5.1
5419  *
5420  * @param array $posts Array of post objects, passed by reference.
5421  */
5422 function update_post_cache( &$posts ) {
5423         if ( ! $posts )
5424                 return;
5425
5426         foreach ( $posts as $post )
5427                 wp_cache_add( $post->ID, $post, 'posts' );
5428 }
5429
5430 /**
5431  * Will clean the post in the cache.
5432  *
5433  * Cleaning means delete from the cache of the post. Will call to clean the term
5434  * object cache associated with the post ID.
5435  *
5436  * This function not run if $_wp_suspend_cache_invalidation is not empty. See
5437  * wp_suspend_cache_invalidation().
5438  *
5439  * @since 2.0.0
5440  *
5441  * @global wpdb $wpdb WordPress database abstraction object.
5442  *
5443  * @param int|WP_Post $post Post ID or post object to remove from the cache.
5444  */
5445 function clean_post_cache( $post ) {
5446         global $_wp_suspend_cache_invalidation, $wpdb;
5447
5448         if ( ! empty( $_wp_suspend_cache_invalidation ) )
5449                 return;
5450
5451         $post = get_post( $post );
5452         if ( empty( $post ) )
5453                 return;
5454
5455         wp_cache_delete( $post->ID, 'posts' );
5456         wp_cache_delete( $post->ID, 'post_meta' );
5457
5458         clean_object_term_cache( $post->ID, $post->post_type );
5459
5460         wp_cache_delete( 'wp_get_archives', 'general' );
5461
5462         /**
5463          * Fires immediately after the given post's cache is cleaned.
5464          *
5465          * @since 2.5.0
5466          *
5467          * @param int     $post_id Post ID.
5468          * @param WP_Post $post    Post object.
5469          */
5470         do_action( 'clean_post_cache', $post->ID, $post );
5471
5472         if ( 'page' == $post->post_type ) {
5473                 wp_cache_delete( 'all_page_ids', 'posts' );
5474
5475                 /**
5476                  * Fires immediately after the given page's cache is cleaned.
5477                  *
5478                  * @since 2.5.0
5479                  *
5480                  * @param int $post_id Post ID.
5481                  */
5482                 do_action( 'clean_page_cache', $post->ID );
5483         }
5484
5485         wp_cache_set( 'last_changed', microtime(), 'posts' );
5486 }
5487
5488 /**
5489  * Call major cache updating functions for list of Post objects.
5490  *
5491  * @since 1.5.0
5492  *
5493  * @param array  $posts             Array of Post objects
5494  * @param string $post_type         Optional. Post type. Default 'post'.
5495  * @param bool   $update_term_cache Optional. Whether to update the term cache. Default true.
5496  * @param bool   $update_meta_cache Optional. Whether to update the meta cache. Default true.
5497  */
5498 function update_post_caches( &$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true ) {
5499         // No point in doing all this work if we didn't match any posts.
5500         if ( !$posts )
5501                 return;
5502
5503         update_post_cache($posts);
5504
5505         $post_ids = array();
5506         foreach ( $posts as $post )
5507                 $post_ids[] = $post->ID;
5508
5509         if ( ! $post_type )
5510                 $post_type = 'any';
5511
5512         if ( $update_term_cache ) {
5513                 if ( is_array($post_type) ) {
5514                         $ptypes = $post_type;
5515                 } elseif ( 'any' == $post_type ) {
5516                         // Just use the post_types in the supplied posts.
5517                         foreach ( $posts as $post )
5518                                 $ptypes[] = $post->post_type;
5519                         $ptypes = array_unique($ptypes);
5520                 } else {
5521                         $ptypes = array($post_type);
5522                 }
5523
5524                 if ( ! empty($ptypes) )
5525                         update_object_term_cache($post_ids, $ptypes);
5526         }
5527
5528         if ( $update_meta_cache )
5529                 update_postmeta_cache($post_ids);
5530 }
5531
5532 /**
5533  * Updates metadata cache for list of post IDs.
5534  *
5535  * Performs SQL query to retrieve the metadata for the post IDs and updates the
5536  * metadata cache for the posts. Therefore, the functions, which call this
5537  * function, do not need to perform SQL queries on their own.
5538  *
5539  * @since 2.1.0
5540  *
5541  * @param array $post_ids List of post IDs.
5542  * @return bool|array Returns false if there is nothing to update or an array
5543  *                    of metadata.
5544  */
5545 function update_postmeta_cache( $post_ids ) {
5546         return update_meta_cache('post', $post_ids);
5547 }
5548
5549 /**
5550  * Will clean the attachment in the cache.
5551  *
5552  * Cleaning means delete from the cache. Optionally will clean the term
5553  * object cache associated with the attachment ID.
5554  *
5555  * This function will not run if $_wp_suspend_cache_invalidation is not empty.
5556  *
5557  * @since 3.0.0
5558  *
5559  * @see wp_suspend_cache_invalidation()
5560  *
5561  * @param int  $id          The attachment ID in the cache to clean.
5562  * @param bool $clean_terms Optional. Whether to clean terms cache. Default false.
5563  */
5564 function clean_attachment_cache( $id, $clean_terms = false ) {
5565         global $_wp_suspend_cache_invalidation;
5566
5567         if ( !empty($_wp_suspend_cache_invalidation) )
5568                 return;
5569
5570         $id = (int) $id;
5571
5572         wp_cache_delete($id, 'posts');
5573         wp_cache_delete($id, 'post_meta');
5574
5575         if ( $clean_terms )
5576                 clean_object_term_cache($id, 'attachment');
5577
5578         /**
5579          * Fires after the given attachment's cache is cleaned.
5580          *
5581          * @since 3.0.0
5582          *
5583          * @param int $id Attachment ID.
5584          */
5585         do_action( 'clean_attachment_cache', $id );
5586 }
5587
5588 //
5589 // Hooks
5590 //
5591
5592 /**
5593  * Hook for managing future post transitions to published.
5594  *
5595  * @since 2.3.0
5596  * @access private
5597  *
5598  * @see wp_clear_scheduled_hook()
5599  * @global wpdb $wpdb WordPress database abstraction object.
5600  *
5601  * @param string  $new_status New post status.
5602  * @param string  $old_status Previous post status.
5603  * @param WP_Post $post       Post object.
5604  */
5605 function _transition_post_status( $new_status, $old_status, $post ) {
5606         global $wpdb;
5607
5608         if ( $old_status != 'publish' && $new_status == 'publish' ) {
5609                 // Reset GUID if transitioning to publish and it is empty.
5610                 if ( '' == get_the_guid($post->ID) )
5611                         $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post->ID ) ), array( 'ID' => $post->ID ) );
5612
5613                 /**
5614                  * Fires when a post's status is transitioned from private to published.
5615                  *
5616                  * @since 1.5.0
5617                  * @deprecated 2.3.0 Use 'private_to_publish' instead.
5618                  *
5619                  * @param int $post_id Post ID.
5620                  */
5621                 do_action('private_to_published', $post->ID);
5622         }
5623
5624         // If published posts changed clear the lastpostmodified cache.
5625         if ( 'publish' == $new_status || 'publish' == $old_status) {
5626                 foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) {
5627                         wp_cache_delete( "lastpostmodified:$timezone", 'timeinfo' );
5628                         wp_cache_delete( "lastpostdate:$timezone", 'timeinfo' );
5629                 }
5630         }
5631
5632         if ( $new_status !== $old_status ) {
5633                 wp_cache_delete( _count_posts_cache_key( $post->post_type ), 'counts' );
5634                 wp_cache_delete( _count_posts_cache_key( $post->post_type, 'readable' ), 'counts' );
5635         }
5636
5637         // Always clears the hook in case the post status bounced from future to draft.
5638         wp_clear_scheduled_hook('publish_future_post', array( $post->ID ) );
5639 }
5640
5641 /**
5642  * Hook used to schedule publication for a post marked for the future.
5643  *
5644  * The $post properties used and must exist are 'ID' and 'post_date_gmt'.
5645  *
5646  * @since 2.3.0
5647  * @access private
5648  *
5649  * @param int     $deprecated Not used. Can be set to null. Never implemented. Not marked
5650  *                            as deprecated with _deprecated_argument() as it conflicts with
5651  *                            wp_transition_post_status() and the default filter for
5652  *                            {@see _future_post_hook()}.
5653  * @param WP_Post $post       Post object.
5654  */
5655 function _future_post_hook( $deprecated, $post ) {
5656         wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) );
5657         wp_schedule_single_event( strtotime( get_gmt_from_date( $post->post_date ) . ' GMT') , 'publish_future_post', array( $post->ID ) );
5658 }
5659
5660 /**
5661  * Hook to schedule pings and enclosures when a post is published.
5662  *
5663  * Uses XMLRPC_REQUEST and WP_IMPORTING constants.
5664  *
5665  * @since 2.3.0
5666  * @access private
5667  *
5668  * @param int $post_id The ID in the database table of the post being published.
5669  */
5670 function _publish_post_hook( $post_id ) {
5671         if ( defined( 'XMLRPC_REQUEST' ) ) {
5672                 /**
5673                  * Fires when _publish_post_hook() is called during an XML-RPC request.
5674                  *
5675                  * @since 2.1.0
5676                  *
5677                  * @param int $post_id Post ID.
5678                  */
5679                 do_action( 'xmlrpc_publish_post', $post_id );
5680         }
5681
5682         if ( defined('WP_IMPORTING') )
5683                 return;
5684
5685         if ( get_option('default_pingback_flag') )
5686                 add_post_meta( $post_id, '_pingme', '1' );
5687         add_post_meta( $post_id, '_encloseme', '1' );
5688
5689         wp_schedule_single_event(time(), 'do_pings');
5690 }
5691
5692 /**
5693  * Return the post's parent's post_ID
5694  *
5695  * @since 3.1.0
5696  *
5697  * @param int $post_ID
5698  *
5699  * @return int|bool Post parent ID, otherwise false.
5700  */
5701 function wp_get_post_parent_id( $post_ID ) {
5702         $post = get_post( $post_ID );
5703         if ( !$post || is_wp_error( $post ) )
5704                 return false;
5705         return (int) $post->post_parent;
5706 }
5707
5708 /**
5709  * Check the given subset of the post hierarchy for hierarchy loops.
5710  *
5711  * Prevents loops from forming and breaks those that it finds. Attached
5712  * to the 'wp_insert_post_parent' filter.
5713  *
5714  * @since 3.1.0
5715  *
5716  * @see wp_find_hierarchy_loop()
5717  *
5718  * @param int $post_parent ID of the parent for the post we're checking.
5719  * @param int $post_ID     ID of the post we're checking.
5720  * @return int The new post_parent for the post, 0 otherwise.
5721  */
5722 function wp_check_post_hierarchy_for_loops( $post_parent, $post_ID ) {
5723         // Nothing fancy here - bail.
5724         if ( !$post_parent )
5725                 return 0;
5726
5727         // New post can't cause a loop.
5728         if ( empty( $post_ID ) )
5729                 return $post_parent;
5730
5731         // Can't be its own parent.
5732         if ( $post_parent == $post_ID )
5733                 return 0;
5734
5735         // Now look for larger loops.
5736         if ( !$loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_ID, $post_parent ) )
5737                 return $post_parent; // No loop
5738
5739         // Setting $post_parent to the given value causes a loop.
5740         if ( isset( $loop[$post_ID] ) )
5741                 return 0;
5742
5743         // There's a loop, but it doesn't contain $post_ID. Break the loop.
5744         foreach ( array_keys( $loop ) as $loop_member )
5745                 wp_update_post( array( 'ID' => $loop_member, 'post_parent' => 0 ) );
5746
5747         return $post_parent;
5748 }
5749
5750 /**
5751  * Set a post thumbnail.
5752  *
5753  * @since 3.1.0
5754  *
5755  * @param int|WP_Post $post         Post ID or post object where thumbnail should be attached.
5756  * @param int         $thumbnail_id Thumbnail to attach.
5757  * @return bool True on success, false on failure.
5758  */
5759 function set_post_thumbnail( $post, $thumbnail_id ) {
5760         $post = get_post( $post );
5761         $thumbnail_id = absint( $thumbnail_id );
5762         if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {
5763                 if ( wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) )
5764                         return update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );
5765                 else
5766                         return delete_post_meta( $post->ID, '_thumbnail_id' );
5767         }
5768         return false;
5769 }
5770
5771 /**
5772  * Remove a post thumbnail.
5773  *
5774  * @since 3.3.0
5775  *
5776  * @param int|WP_Post $post Post ID or post object where thumbnail should be removed from.
5777  * @return bool True on success, false on failure.
5778  */
5779 function delete_post_thumbnail( $post ) {
5780         $post = get_post( $post );
5781         if ( $post )
5782                 return delete_post_meta( $post->ID, '_thumbnail_id' );
5783         return false;
5784 }
5785
5786 /**
5787  * Delete auto-drafts for new posts that are > 7 days old.
5788  *
5789  * @since 3.4.0
5790  *
5791  * @global wpdb $wpdb WordPress database abstraction object.
5792  */
5793 function wp_delete_auto_drafts() {
5794         global $wpdb;
5795
5796         // Cleanup old auto-drafts more than 7 days old.
5797         $old_posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft' AND DATE_SUB( NOW(), INTERVAL 7 DAY ) > post_date" );
5798         foreach ( (array) $old_posts as $delete ) {
5799                 // Force delete.
5800                 wp_delete_post( $delete, true );
5801         }
5802 }
5803
5804 /**
5805  * Update the custom taxonomies' term counts when a post's status is changed.
5806  *
5807  * For example, default posts term counts (for custom taxonomies) don't include
5808  * private / draft posts.
5809  *
5810  * @since 3.3.0
5811  * @access private
5812  *
5813  * @param string  $new_status New post status.
5814  * @param string  $old_status Old post status.
5815  * @param WP_Post $post       Post object.
5816  */
5817 function _update_term_count_on_transition_post_status( $new_status, $old_status, $post ) {
5818         // Update counts for the post's terms.
5819         foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) {
5820                 $tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) );
5821                 wp_update_term_count( $tt_ids, $taxonomy );
5822         }
5823 }
5824
5825 /**
5826  * Adds any posts from the given ids to the cache that do not already exist in cache
5827  *
5828  * @since 3.4.0
5829  * @access private
5830  *
5831  * @see update_post_caches()
5832  *
5833  * @param array $ids               ID list
5834  * @param bool  $update_term_cache Optional. Whether to update the term cache. Default true.
5835  * @param bool  $update_meta_cache Optional. Whether to update the meta cache. Default true.
5836  */
5837 function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache = true ) {
5838         global $wpdb;
5839
5840         $non_cached_ids = _get_non_cached_ids( $ids, 'posts' );
5841         if ( !empty( $non_cached_ids ) ) {
5842                 $fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", join( ",", $non_cached_ids ) ) );
5843
5844                 update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
5845         }
5846 }