Make WordPress Core

Changeset 23466


Ignore:
Timestamp:
02/21/2013 09:24:34 PM (10 years ago)
Author:
nacin
Message:

Move revisions/autosave and post format functions from wp-includes/post.php into revision.php and post-formats.php.

Location:
trunk
Files:
3 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/formatting.php

    r23450 r23466  
    17961796        $output = $text;
    17971797    }
    1798     return $output;
    1799 }
    1800 
    1801 /**
    1802  * Return the class for a post format content wrapper
    1803  *
    1804  * @since 3.6.0
    1805  *
    1806  * @param string $format
    1807  */
    1808 function get_post_format_content_class( $format ) {
    1809     return apply_filters( 'post_format_content_class', 'post-format-content', $format );
    1810 }
    1811 
    1812 /**
    1813  * Ouput the class for a post format content wrapper
    1814  *
    1815  * @since 3.6.0
    1816  *
    1817  * @param string $format
    1818  */
    1819 function post_format_content_class( $format ) {
    1820     echo get_post_format_content_class( $format );
    1821 }
    1822 
    1823 /**
    1824  * Provide fallback behavior for Posts that have associated post format
    1825  *
    1826  * @since 3.6.0
    1827  *
    1828  * @param string $content
    1829  */
    1830 function post_formats_compat( $content, $id = 0 ) {
    1831     $post = empty( $id ) ? get_post() : get_post( $id );
    1832     if ( empty( $post ) )
    1833         return $content;
    1834 
    1835     $format = get_post_format( $post );
    1836     if ( empty( $format ) || in_array( $format, array( 'status', 'aside', 'chat' ) ) )
    1837         return $content;
    1838 
    1839     if ( current_theme_supports( 'post-formats', $format ) )
    1840         return $content;
    1841 
    1842     $defaults = array(
    1843         'position' => 'after',
    1844         'tag' => 'div',
    1845         'class' => get_post_format_content_class( $format ),
    1846         'link_class' => '',
    1847         'image_class' => '',
    1848         'gallery' => '[gallery]',
    1849         'audio' => '',
    1850         'video' => ''
    1851     );
    1852 
    1853     $args = apply_filters( 'post_format_compat', array() );
    1854     $compat = wp_parse_args( $args, $defaults );
    1855 
    1856     $show_content = true;
    1857     $format_output = '';
    1858     $meta = get_post_format_meta( $post->ID );
    1859 
    1860     switch ( $format ) {
    1861         case 'link':
    1862             $compat['tag'] = '';
    1863 
    1864             if ( ! empty( $meta['url'] ) ) {
    1865                 $esc_url = preg_quote( $meta['url'], '#' );
    1866                 // Make sure the same URL isn't in the post (modified/extended versions allowed)
    1867                 if ( ! preg_match( '#' . $esc_url . '[^/&\?]#', $content ) ) {
    1868                     $format_output .= sprintf(
    1869                         '<a %shref="%s">%s</a>',
    1870                         empty( $compat['link_class'] ) ? '' : sprintf( 'class="%s" ', esc_attr( $compat['link_class'] ) ),
    1871                         esc_url( $meta['url'] ),
    1872                         empty( $post->post_title ) ? esc_url( $meta['url'] ) : apply_filters( 'the_title', $post->post_title )
    1873                     );
    1874                 }
    1875             }
    1876             break;
    1877 
    1878         case 'quote':
    1879             if ( ! empty( $meta['quote'] ) && ! stristr( $content, $meta['quote'] ) ) {
    1880                 $format_output .= sprintf( '<blockquote>%s</blockquote>', $meta['quote'] );
    1881                 if ( ! empty( $meta['quote_source'] ) ) {
    1882                     $format_output .= sprintf(
    1883                         '<cite>%s</cite>',
    1884                         ! empty( $meta['url'] ) ?
    1885                             sprintf( '<a href="%s">%s</a>', esc_url( $meta['url'] ), $meta['quote_source'] ) :
    1886                             $meta['quote_source']
    1887                     );
    1888                 }
    1889             }
    1890             break;
    1891 
    1892         case 'image':
    1893             if ( ! empty( $meta['image'] ) ) {
    1894                 $image = is_numeric( $meta['image'] ) ? wp_get_attachment_url( $meta['image'] ) : $meta['image'];
    1895 
    1896                 if ( ! empty( $image ) && ! stristr( $content, $image ) ) {
    1897                     $image_html = sprintf(
    1898                         '<img %ssrc="%s" alt="" />',
    1899                         empty( $compat['image_class'] ) ? '' : sprintf( 'class="%s" ', esc_attr( $compat['image_class'] ) ),
    1900                         $image
    1901                     );
    1902                     if ( empty( $meta['url'] ) ) {
    1903                         $format_output .= $image_html;
    1904                     } else {
    1905                         $format_output .= sprintf(
    1906                             '<a href="%s">%s</a>',
    1907                             esc_url( $meta['url'] ),
    1908                             $image_html
    1909                         );
    1910                     }
    1911                 }
    1912             }
    1913             break;
    1914 
    1915         case 'gallery':
    1916             preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches );
    1917             if ( ! empty( $matches ) && isset( $matches[2] ) ) {
    1918                 foreach ( (array) $matches[2] as $match ) {
    1919                     if ( 'gallery' === $match )
    1920                         break 2; // foreach + case
    1921                 }
    1922             }
    1923 
    1924             if ( empty( $meta['gallery'] ) && ! empty( $compat['gallery'] ) ) {
    1925                 $format_output .= $compat['gallery'];
    1926             } elseif ( ! empty( $meta['gallery'] ) ) {
    1927                 $format_output .= $meta['gallery'];
    1928             }
    1929             break;
    1930 
    1931         case 'video':
    1932         case 'audio':
    1933             $shortcode_regex = '/' . get_shortcode_regex() . '/s';
    1934             $matches = preg_match( $shortcode_regex, $content );
    1935             if ( ! $matches || $format !== $matches[2] ) {
    1936                 if ( empty( $meta['media'] ) && ! empty( $compat[$format] ) ) {
    1937                     $format_output .= $compat[$format];
    1938                 } elseif ( ! empty( $meta['media'] ) ) {
    1939                     // the metadata is a shortcode or an embed code
    1940                     if ( preg_match( $shortcode_regex, $meta['media'] ) || preg_match( '#<[^>]+>#', $meta['media'] ) ) {
    1941                         $format_output .= $meta['media'];
    1942                     } elseif ( ! stristr( $content, $meta['media'] ) ) {
    1943                         // attempt to embed the URL
    1944                         $format_output .= sprintf( '[embed]%s[/embed]', $meta['media'] );
    1945                     }
    1946                 }
    1947             }
    1948             break;
    1949         default:
    1950             return $content;
    1951             break;
    1952     }
    1953 
    1954     if ( empty( $format_output ) )
    1955         return $content;
    1956 
    1957     $output = '';
    1958 
    1959     if ( ! empty( $content ) && $show_content && 'before' !== $compat['position'] )
    1960         $output .= $content . PHP_EOL . PHP_EOL;
    1961 
    1962     if ( ! empty( $compat['tag'] ) )
    1963         $output .= sprintf( '<%s class="%s">', tag_escape( $compat['tag'] ), esc_attr( $compat['class'] ) );
    1964 
    1965     $output .= $format_output;
    1966 
    1967     if ( ! empty( $compat['tag'] ) )
    1968         $output .= sprintf( '</%s>', tag_escape( $compat['tag'] ) );
    1969 
    1970     if ( ! empty( $content ) && $show_content && 'before' === $compat['position'] )
    1971         $output .= PHP_EOL . PHP_EOL . $content;
    1972 
    19731798    return $output;
    19741799}
     
    35323357 */
    35333358function wp_slash( $value ) {
    3534     if ( is_array( $value ) ) { 
     3359    if ( is_array( $value ) ) {
    35353360        foreach ( $value as $k => $v ) {
    35363361            if ( is_array( $v ) ) {
     
    35413366        }
    35423367    } else {
    3543         $value = addslashes( $value ); 
    3544     } 
    3545 
    3546     return $value; 
     3368        $value = addslashes( $value );
     3369    }
     3370
     3371    return $value;
    35473372}
    35483373
     
    35633388 */
    35643389function wp_unslash( $value ) {
    3565     return stripslashes_deep( $value ); 
    3566 }
     3390    return stripslashes_deep( $value );
     3391}
  • trunk/wp-includes/post-formats.php

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

    r23449 r23466  
    752752
    753753    return false;
    754 }
    755 
    756 /**
    757  * Retrieve the format slug for a post
    758  *
    759  * @since 3.1.0
    760  *
    761  * @param int|object $post A post
    762  *
    763  * @return mixed The format if successful. False if no format is set. WP_Error if errors.
    764  */
    765 function get_post_format( $post = null ) {
    766     $post = get_post($post);
    767 
    768     if ( ! post_type_supports( $post->post_type, 'post-formats' ) )
    769         return false;
    770 
    771     $_format = get_the_terms( $post->ID, 'post_format' );
    772 
    773     if ( empty( $_format ) )
    774         return false;
    775 
    776     $format = array_shift( $_format );
    777 
    778     return ( str_replace('post-format-', '', $format->slug ) );
    779 }
    780 
    781 /**
    782  * Check if a post has a particular format
    783  *
    784  * @since 3.1.0
    785  * @uses has_term()
    786  *
    787  * @param string $format The format to check for
    788  * @param object|id $post The post to check. If not supplied, defaults to the current post if used in the loop.
    789  * @return bool True if the post has the format, false otherwise.
    790  */
    791 function has_post_format( $format, $post = null ) {
    792     return has_term('post-format-' . sanitize_key($format), 'post_format', $post);
    793 }
    794 
    795 /**
    796  * Assign a format to a post
    797  *
    798  * @since 3.1.0
    799  *
    800  * @param int|object $post The post for which to assign a format
    801  * @param string $format  A format to assign. Use an empty string or array to remove all formats from the post.
    802  * @return mixed WP_Error on error. Array of affected term IDs on success.
    803  */
    804 function set_post_format( $post, $format ) {
    805     $post = get_post($post);
    806 
    807     if ( empty($post) )
    808         return new WP_Error('invalid_post', __('Invalid post'));
    809 
    810     if ( !empty($format) ) {
    811         $format = sanitize_key($format);
    812         if ( 'standard' == $format || !in_array( $format, array_keys( get_post_format_slugs() ) ) )
    813             $format = '';
    814         else
    815             $format = 'post-format-' . $format;
    816     }
    817 
    818     return wp_set_post_terms($post->ID, $format, 'post_format');
    819754}
    820755
     
    19521887
    19531888/**
    1954  * Retrieve post format metadata for a post
    1955  *
    1956  * @since 3.6.0
    1957  *
    1958  * @param int $post_id
    1959  * @return null
    1960  */
    1961 function get_post_format_meta( $post_id = 0 ) {
    1962     $values = array(
    1963         'quote'        => '',
    1964         'quote_source' => '',
    1965         'image'        => '',
    1966         'url'          => '',
    1967         'gallery'      => '',
    1968         'media'        => '',
    1969     );
    1970 
    1971     foreach ( $values as $key => $value )
    1972         $values[$key] = get_post_meta( $post_id, '_wp_format_' . $key, true );
    1973 
    1974     return $values;
    1975 }
    1976 
    1977 /**
    19781889 * Check if post is sticky.
    19791890 *
     
    49514862
    49524863/**
    4953  * Determines which fields of posts are to be saved in revisions.
    4954  *
    4955  * Does two things. If passed a post *array*, it will return a post array ready
    4956  * to be inserted into the posts table as a post revision. Otherwise, returns
    4957  * an array whose keys are the post fields to be saved for post revisions.
    4958  *
    4959  * @package WordPress
    4960  * @subpackage Post_Revisions
    4961  * @since 2.6.0
    4962  * @access private
    4963  * @uses apply_filters() Calls '_wp_post_revision_fields' on 'title', 'content' and 'excerpt' fields.
    4964  *
    4965  * @param array $post Optional a post array to be processed for insertion as a post revision.
    4966  * @param bool $autosave optional Is the revision an autosave?
    4967  * @return array Post array ready to be inserted as a post revision or array of fields that can be versioned.
    4968  */
    4969 function _wp_post_revision_fields( $post = null, $autosave = false ) {
    4970     static $fields = false;
    4971 
    4972     if ( !$fields ) {
    4973         // Allow these to be versioned
    4974         $fields = array(
    4975             'post_title' => __( 'Title' ),
    4976             'post_content' => __( 'Content' ),
    4977             'post_excerpt' => __( 'Excerpt' ),
    4978         );
    4979 
    4980         // Runs only once
    4981         $fields = apply_filters( '_wp_post_revision_fields', $fields );
    4982 
    4983         // WP uses these internally either in versioning or elsewhere - they cannot be versioned
    4984         foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect )
    4985             unset( $fields[$protect] );
    4986     }
    4987 
    4988     if ( !is_array($post) )
    4989         return $fields;
    4990 
    4991     $return = array();
    4992     foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field )
    4993         $return[$field] = $post[$field];
    4994 
    4995     $return['post_parent']   = $post['ID'];
    4996     $return['post_status']   = 'inherit';
    4997     $return['post_type']     = 'revision';
    4998     $return['post_name']     = $autosave ? "$post[ID]-autosave" : "$post[ID]-revision";
    4999     $return['post_date']     = isset($post['post_modified']) ? $post['post_modified'] : '';
    5000     $return['post_date_gmt'] = isset($post['post_modified_gmt']) ? $post['post_modified_gmt'] : '';
    5001 
    5002     return $return;
    5003 }
    5004 
    5005 /**
    5006  * Saves an already existing post as a post revision.
    5007  *
    5008  * Typically used immediately prior to post updates.
    5009  *
    5010  * @package WordPress
    5011  * @subpackage Post_Revisions
    5012  * @since 2.6.0
    5013  *
    5014  * @uses _wp_put_post_revision()
    5015  *
    5016  * @param int $post_id The ID of the post to save as a revision.
    5017  * @return mixed Null or 0 if error, new revision ID, if success.
    5018  */
    5019 function wp_save_post_revision( $post_id, $new_data = null ) {
    5020     // We do autosaves manually with wp_create_post_autosave()
    5021     if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    5022         return;
    5023 
    5024     // WP_POST_REVISIONS = 0, false
    5025     if ( ! WP_POST_REVISIONS )
    5026         return;
    5027 
    5028     if ( !$post = get_post( $post_id, ARRAY_A ) )
    5029         return;
    5030 
    5031     if ( 'auto-draft' == $post['post_status'] )
    5032         return;
    5033 
    5034     if ( !post_type_supports($post['post_type'], 'revisions') )
    5035         return;
    5036 
    5037     // if new data is supplied, check that it is different from last saved revision, unless a plugin tells us to always save regardless
    5038     if ( apply_filters( 'wp_save_post_revision_check_for_changes', true, $post, $new_data ) && is_array( $new_data ) ) {
    5039         $post_has_changed = false;
    5040         foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
    5041             if ( normalize_whitespace( $new_data[ $field ] ) != normalize_whitespace( $post[ $field ] ) ) {
    5042                 $post_has_changed = true;
    5043                 break;
    5044             }
    5045         }
    5046         //don't save revision if post unchanged
    5047         if( ! $post_has_changed )
    5048             return;
    5049     }
    5050 
    5051     $return = _wp_put_post_revision( $post );
    5052 
    5053     // WP_POST_REVISIONS = true (default), -1
    5054     if ( !is_numeric( WP_POST_REVISIONS ) || WP_POST_REVISIONS < 0 )
    5055         return $return;
    5056 
    5057     // all revisions and (possibly) one autosave
    5058     $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) );
    5059 
    5060     // WP_POST_REVISIONS = (int) (# of autosaves to save)
    5061     $delete = count($revisions) - WP_POST_REVISIONS;
    5062 
    5063     if ( $delete < 1 )
    5064         return $return;
    5065 
    5066     $revisions = array_slice( $revisions, 0, $delete );
    5067 
    5068     for ( $i = 0; isset($revisions[$i]); $i++ ) {
    5069         if ( false !== strpos( $revisions[$i]->post_name, 'autosave' ) )
    5070             continue;
    5071         wp_delete_post_revision( $revisions[$i]->ID );
    5072     }
    5073 
    5074     return $return;
    5075 }
    5076 
    5077 /**
    5078  * Retrieve the autosaved data of the specified post.
    5079  *
    5080  * Returns a post object containing the information that was autosaved for the
    5081  * specified post.
    5082  *
    5083  * @package WordPress
    5084  * @subpackage Post_Revisions
    5085  * @since 2.6.0
    5086  *
    5087  * @param int $post_id The post ID.
    5088  * @return object|bool The autosaved data or false on failure or when no autosave exists.
    5089  */
    5090 function wp_get_post_autosave( $post_id ) {
    5091 
    5092     if ( !$post = get_post( $post_id ) )
    5093         return false;
    5094 
    5095     $q = array(
    5096         'name' => "{$post->ID}-autosave",
    5097         'post_parent' => $post->ID,
    5098         'post_type' => 'revision',
    5099         'post_status' => 'inherit'
    5100     );
    5101 
    5102     // Use WP_Query so that the result gets cached
    5103     $autosave_query = new WP_Query;
    5104 
    5105     add_action( 'parse_query', '_wp_get_post_autosave_hack' );
    5106     $autosave = $autosave_query->query( $q );
    5107     remove_action( 'parse_query', '_wp_get_post_autosave_hack' );
    5108 
    5109     if ( $autosave && is_array($autosave) && is_object($autosave[0]) )
    5110         return $autosave[0];
    5111 
    5112     return false;
    5113 }
    5114 
    5115 /**
    5116  * Internally used to hack WP_Query into submission.
    5117  *
    5118  * @package WordPress
    5119  * @subpackage Post_Revisions
    5120  * @since 2.6.0
    5121  *
    5122  * @param object $query WP_Query object
    5123  */
    5124 function _wp_get_post_autosave_hack( $query ) {
    5125     $query->is_single = false;
    5126 }
    5127 
    5128 /**
    5129  * Determines if the specified post is a revision.
    5130  *
    5131  * @package WordPress
    5132  * @subpackage Post_Revisions
    5133  * @since 2.6.0
    5134  *
    5135  * @param int|object $post Post ID or post object.
    5136  * @return bool|int False if not a revision, ID of revision's parent otherwise.
    5137  */
    5138 function wp_is_post_revision( $post ) {
    5139     if ( !$post = wp_get_post_revision( $post ) )
    5140         return false;
    5141     return (int) $post->post_parent;
    5142 }
    5143 
    5144 /**
    5145  * Determines if the specified post is an autosave.
    5146  *
    5147  * @package WordPress
    5148  * @subpackage Post_Revisions
    5149  * @since 2.6.0
    5150  *
    5151  * @param int|object $post Post ID or post object.
    5152  * @return bool|int False if not a revision, ID of autosave's parent otherwise
    5153  */
    5154 function wp_is_post_autosave( $post ) {
    5155     if ( !$post = wp_get_post_revision( $post ) )
    5156         return false;
    5157     if ( "{$post->post_parent}-autosave" !== $post->post_name )
    5158         return false;
    5159     return (int) $post->post_parent;
    5160 }
    5161 
    5162 /**
    5163  * Inserts post data into the posts table as a post revision.
    5164  *
    5165  * @package WordPress
    5166  * @subpackage Post_Revisions
    5167  * @since 2.6.0
    5168  *
    5169  * @uses wp_insert_post()
    5170  *
    5171  * @param int|object|array $post Post ID, post object OR post array.
    5172  * @param bool $autosave Optional. Is the revision an autosave?
    5173  * @return mixed Null or 0 if error, new revision ID if success.
    5174  */
    5175 function _wp_put_post_revision( $post = null, $autosave = false ) {
    5176     if ( is_object($post) )
    5177         $post = get_object_vars( $post );
    5178     elseif ( !is_array($post) )
    5179         $post = get_post($post, ARRAY_A);
    5180     if ( !$post || empty($post['ID']) )
    5181         return;
    5182 
    5183     if ( isset($post['post_type']) && 'revision' == $post['post_type'] )
    5184         return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
    5185 
    5186     $post = _wp_post_revision_fields( $post, $autosave );
    5187 
    5188     $revision_id = wp_insert_post( $post );
    5189     if ( is_wp_error($revision_id) )
    5190         return $revision_id;
    5191 
    5192     if ( $revision_id )
    5193         do_action( '_wp_put_post_revision', $revision_id );
    5194     return $revision_id;
    5195 }
    5196 
    5197 /**
    5198  * Gets a post revision.
    5199  *
    5200  * @package WordPress
    5201  * @subpackage Post_Revisions
    5202  * @since 2.6.0
    5203  *
    5204  * @uses get_post()
    5205  *
    5206  * @param int|object $post Post ID or post object
    5207  * @param string $output Optional. OBJECT, ARRAY_A, or ARRAY_N.
    5208  * @param string $filter Optional sanitation filter. @see sanitize_post()
    5209  * @return mixed Null if error or post object if success
    5210  */
    5211 function wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') {
    5212     $null = null;
    5213     if ( !$revision = get_post( $post, OBJECT, $filter ) )
    5214         return $revision;
    5215     if ( 'revision' !== $revision->post_type )
    5216         return $null;
    5217 
    5218     if ( $output == OBJECT ) {
    5219         return $revision;
    5220     } elseif ( $output == ARRAY_A ) {
    5221         $_revision = get_object_vars($revision);
    5222         return $_revision;
    5223     } elseif ( $output == ARRAY_N ) {
    5224         $_revision = array_values(get_object_vars($revision));
    5225         return $_revision;
    5226     }
    5227 
    5228     return $revision;
    5229 }
    5230 
    5231 /**
    5232  * Restores a post to the specified revision.
    5233  *
    5234  * Can restore a past revision using all fields of the post revision, or only selected fields.
    5235  *
    5236  * @package WordPress
    5237  * @subpackage Post_Revisions
    5238  * @since 2.6.0
    5239  *
    5240  * @uses wp_get_post_revision()
    5241  * @uses wp_update_post()
    5242  * @uses do_action() Calls 'wp_restore_post_revision' on post ID and revision ID if wp_update_post()
    5243  *  is successful.
    5244  *
    5245  * @param int|object $revision_id Revision ID or revision object.
    5246  * @param array $fields Optional. What fields to restore from. Defaults to all.
    5247  * @return mixed Null if error, false if no fields to restore, (int) post ID if success.
    5248  */
    5249 function wp_restore_post_revision( $revision_id, $fields = null ) {
    5250     if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) )
    5251         return $revision;
    5252 
    5253     if ( !is_array( $fields ) )
    5254         $fields = array_keys( _wp_post_revision_fields() );
    5255 
    5256     $update = array();
    5257     foreach( array_intersect( array_keys( $revision ), $fields ) as $field )
    5258         $update[$field] = $revision[$field];
    5259 
    5260     if ( !$update )
    5261         return false;
    5262 
    5263     $update['ID'] = $revision['post_parent'];
    5264 
    5265     $post_id = wp_update_post( $update );
    5266     if ( is_wp_error( $post_id ) )
    5267         return $post_id;
    5268 
    5269     if ( $post_id )
    5270         do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] );
    5271 
    5272     return $post_id;
    5273 }
    5274 
    5275 /**
    5276  * Deletes a revision.
    5277  *
    5278  * Deletes the row from the posts table corresponding to the specified revision.
    5279  *
    5280  * @package WordPress
    5281  * @subpackage Post_Revisions
    5282  * @since 2.6.0
    5283  *
    5284  * @uses wp_get_post_revision()
    5285  * @uses wp_delete_post()
    5286  *
    5287  * @param int|object $revision_id Revision ID or revision object.
    5288  * @return mixed Null or WP_Error if error, deleted post if success.
    5289  */
    5290 function wp_delete_post_revision( $revision_id ) {
    5291     if ( !$revision = wp_get_post_revision( $revision_id ) )
    5292         return $revision;
    5293 
    5294     $delete = wp_delete_post( $revision->ID );
    5295     if ( is_wp_error( $delete ) )
    5296         return $delete;
    5297 
    5298     if ( $delete )
    5299         do_action( 'wp_delete_post_revision', $revision->ID, $revision );
    5300 
    5301     return $delete;
    5302 }
    5303 
    5304 /**
    5305  * Returns all revisions of specified post.
    5306  *
    5307  * @package WordPress
    5308  * @subpackage Post_Revisions
    5309  * @since 2.6.0
    5310  *
    5311  * @uses get_children()
    5312  *
    5313  * @param int|object $post_id Post ID or post object
    5314  * @return array empty if no revisions
    5315  */
    5316 function wp_get_post_revisions( $post_id = 0, $args = null ) {
    5317     if ( ! WP_POST_REVISIONS )
    5318         return array();
    5319     if ( ( !$post = get_post( $post_id ) ) || empty( $post->ID ) )
    5320         return array();
    5321 
    5322     $defaults = array( 'order' => 'DESC', 'orderby' => 'date' );
    5323     $args = wp_parse_args( $args, $defaults );
    5324     $args = array_merge( $args, array( 'post_parent' => $post->ID, 'post_type' => 'revision', 'post_status' => 'inherit' ) );
    5325 
    5326     if ( !$revisions = get_children( $args ) )
    5327         return array();
    5328     return $revisions;
    5329 }
    5330 
    5331 function _set_preview($post) {
    5332 
    5333     if ( ! is_object($post) )
    5334         return $post;
    5335 
    5336     $preview = wp_get_post_autosave($post->ID);
    5337 
    5338     if ( ! is_object($preview) )
    5339         return $post;
    5340 
    5341     $preview = sanitize_post($preview);
    5342 
    5343     $post->post_content = $preview->post_content;
    5344     $post->post_title = $preview->post_title;
    5345     $post->post_excerpt = $preview->post_excerpt;
    5346 
    5347     return $post;
    5348 }
    5349 
    5350 function _show_post_preview() {
    5351 
    5352     if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) {
    5353         $id = (int) $_GET['preview_id'];
    5354 
    5355         if ( false == wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) )
    5356             wp_die( __('You do not have permission to preview drafts.') );
    5357 
    5358         add_filter('the_preview', '_set_preview');
    5359     }
    5360 }
    5361 
    5362 /**
    53634864 * Returns the post's parent's post_ID
    53644865 *
     
    54174918
    54184919    return $post_parent;
    5419 }