Make WordPress Core


Ignore:
File:
1 edited

Legend:

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

    r16668 r17429  
    1414/**
    1515 * Creates the initial post types when 'init' action is fired.
     16 *
     17 * @since 2.9.0
    1618 */
    1719function create_initial_post_types() {
     
    2123        '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
    2224        'capability_type' => 'post',
     25        'map_meta_cap' => true,
    2326        'hierarchical' => false,
    2427        'rewrite' => false,
    2528        'query_var' => false,
    26         'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions' ),
     29        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
    2730    ) );
    2831
     
    3235        '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
    3336        'capability_type' => 'page',
     37        'map_meta_cap' => true,
    3438        'hierarchical' => true,
    3539        'rewrite' => false,
     
    4751        '_edit_link' => 'media.php?attachment_id=%d', /* internal use only. don't use this when registering your own post type. */
    4852        'capability_type' => 'post',
     53        'map_meta_cap' => true,
    4954        'hierarchical' => false,
    5055        'rewrite' => false,
    5156        'query_var' => false,
    52         'can_export' => false,
    5357        'show_in_nav_menus' => false,
    5458    ) );
     
    6367        '_edit_link' => 'revision.php?revision=%d', /* internal use only. don't use this when registering your own post type. */
    6468        'capability_type' => 'post',
     69        'map_meta_cap' => true,
    6570        'hierarchical' => false,
    6671        'rewrite' => false,
    6772        'query_var' => false,
     73        'can_export' => false,
    6874    ) );
    6975
     
    7581        'public' => false,
    7682        '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
    77         'capability_type' => 'post',
    7883        'hierarchical' => false,
    7984        'rewrite' => false,
     
    262267 * @return array|bool False on failure and the type will be determined by $output parameter.
    263268 */
    264 function &get_children($args = '', $output = OBJECT) {
     269function get_children($args = '', $output = OBJECT) {
    265270    $kids = array();
    266271    if ( empty( $args ) ) {
     
    291296
    292297    foreach ( $children as $key => $child )
    293         $kids[$child->ID] =& $children[$key];
     298        $kids[$child->ID] = $children[$key];
    294299
    295300    if ( $output == OBJECT ) {
     
    472477
    473478/**
     479 * Retrieve the format slug for a post
     480 *
     481 * @since 3.1.0
     482 *
     483 * @param int|object $post A post
     484 *
     485 * @return mixed The format if successful. False if no format is set.  WP_Error if errors.
     486 */
     487function get_post_format( $post = null ) {
     488    $post = get_post($post);
     489
     490    if ( ! post_type_supports( $post->post_type, 'post-formats' ) )
     491        return false;
     492
     493    $_format = get_the_terms( $post->ID, 'post_format' );
     494
     495    if ( empty( $_format ) )
     496        return false;
     497
     498    $format = array_shift( $_format );
     499
     500    return ( str_replace('post-format-', '', $format->slug ) );
     501}
     502
     503/**
     504 * Check if a post has a particular format
     505 *
     506 * @since 3.1.0
     507 * @uses has_term()
     508 *
     509 * @param string $format The format to check for
     510 * @param object|id $post The post to check. If not supplied, defaults to the current post if used in the loop.
     511 * @return bool True if the post has the format, false otherwise.
     512 */
     513function has_post_format( $format, $post = null ) {
     514    return has_term('post-format-' . sanitize_key($format), 'post_format', $post);
     515}
     516
     517/**
     518 * Assign a format to a post
     519 *
     520 * @since 3.1.0
     521 *
     522 * @param int|object $post The post for which to assign a format
     523 * @param string $format  A format to assign.  Use an empty string or array to remove all formats from the post.
     524 * @return mixed WP_Error on error. Array of affected term IDs on success.
     525 */
     526function set_post_format( $post, $format ) {
     527    $post = get_post($post);
     528
     529    if ( empty($post) )
     530        return new WP_Error('invalid_post', __('Invalid post'));
     531
     532    if ( !empty($format) ) {
     533        $format = sanitize_key($format);
     534        if ( 'standard' == $format || !in_array( $format, array_keys( get_post_format_slugs() ) ) )
     535            $format = '';
     536        else
     537            $format = 'post-format-' . $format;
     538    }
     539
     540    return wp_set_post_terms($post->ID, $format, 'post_format');
     541}
     542
     543/**
    474544 * Retrieve the post status based on the Post ID.
    475545 *
     
    550620 *
    551621 * label - A descriptive name for the post status marked for translation. Defaults to $post_status.
    552  * public - Whether posts of this status should be shown in the admin UI. Defaults to true.
     622 * public - Whether posts of this status should be shown in the front end of the site. Defaults to true.
    553623 * exclude_from_search - Whether to exclude posts with this post status from search results. Defaults to true.
     624 * show_in_admin_all_list - Whether to include posts in the edit listing for their post type
     625 * show_in_admin_status_list - Show in the list of statuses with post counts at the top of the edit
     626 *                             listings, e.g. All (12) | Published (9) | My Custom Status (2) ...
     627 *
     628 * Arguments prefixed with an _underscore shouldn't be used by plugins and themes.
    554629 *
    555630 * @package WordPress
     
    572647    $args = (object) $args;
    573648
    574     $post_status = sanitize_user($post_status, true);
     649    $post_status = sanitize_key($post_status);
    575650    $args->name = $post_status;
    576651
     
    626701 * @see get_post_statuses
    627702 *
    628  * @param string $post_type The name of a registered post status
     703 * @param string $post_status The name of a registered post status
    629704 * @return object A post status object
    630705 */
     
    670745 * @see get_post_type_object
    671746 *
    672  * @param string $post Post type name
     747 * @param string $post_type Post type name
    673748 * @return bool Whether post type is hierarchical.
    674749 */
     
    687762 * @uses get_post_type_object()
    688763 *
    689  * @param string Post type name
     764 * @param string $post_type Post type name
    690765 * @return bool Whether post type is registered.
    691766 */
     
    766841 * Register a post type. Do not use before init.
    767842 *
    768  * A simple function for creating or modifying a post type based on the
     843 * A function for creating or modifying a post type based on the
    769844 * parameters given. The function will accept an array (second optional
    770845 * parameter), along with a string for the post type name.
    771  *
    772846 *
    773847 * Optional $args contents:
     
    776850 * - description - A short descriptive summary of what the post type is. Defaults to blank.
    777851 * - public - Whether posts of this type should be shown in the admin UI. Defaults to false.
    778  * - exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true if the type is not public, false if the type is public.
    779  * - publicly_queryable - Whether post_type queries can be performed from the front page.  Defaults to whatever public is set as.
    780  * - show_ui - Whether to generate a default UI for managing this post type. Defaults to true if the type is public, false if the type is not public.
     852 * - exclude_from_search - Whether to exclude posts with this post type from search results.
     853 *     Defaults to true if the type is not public, false if the type is public.
     854 * - publicly_queryable - Whether post_type queries can be performed from the front page.
     855 *     Defaults to whatever public is set as.
     856 * - show_ui - Whether to generate a default UI for managing this post type. Defaults to true
     857 *     if the type is public, false if the type is not public.
     858 * - show_in_menu - Where to show the post type in the admin menu. True for a top level menu,
     859 *     false for no menu, or can be a top level page like 'tools.php' or 'edit.php?post_type=page'.
     860 *     show_ui must be true.
    781861 * - menu_position - The position in the menu order the post type should appear. Defaults to the bottom.
    782862 * - menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon.
    783  * - capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post".
    784  * - capabilities - Array of capabilities for this post type. You can see accepted values in {@link get_post_type_capabilities()}. By default the capability_type is used to construct capabilities.
     863 * - capability_type - The string to use to build the read, edit, and delete capabilities. Defaults to 'post'.
     864 *   May be passed as an array to allow for alternative plurals when using this argument as a base to construct the
     865 *   capabilities, e.g. array('story', 'stories').
     866 * - capabilities - Array of capabilities for this post type. By default the capability_type is used
     867 *      as a base to construct capabilities. You can see accepted values in {@link get_post_type_capabilities()}.
     868 * - map_meta_cap - Whether to use the internal default meta capability handling. Defaults to false.
    785869 * - hierarchical - Whether the post type is hierarchical. Defaults to false.
    786  * - supports - An alias for calling add_post_type_support() directly. See add_post_type_support() for Documentation. Defaults to none.
    787  * - register_meta_box_cb - Provide a callback function that will be called when setting up the meta boxes for the edit form.  Do remove_meta_box() and add_meta_box() calls in the callback.
    788  * - taxonomies - An array of taxonomy identifiers that will be registered for the post type.  Default is no taxonomies. Taxonomies can be registered later with register_taxonomy() or register_taxonomy_for_object_type().
    789  * - labels - An array of labels for this post type. You can see accepted values in {@link get_post_type_labels()}. By default post labels are used for non-hierarchical types and page labels for hierarchical ones.
     870 * - supports - An alias for calling add_post_type_support() directly. See {@link add_post_type_support()}
     871 *     for documentation. Defaults to none.
     872 * - register_meta_box_cb - Provide a callback function that will be called when setting up the
     873 *     meta boxes for the edit form.  Do remove_meta_box() and add_meta_box() calls in the callback.
     874 * - taxonomies - An array of taxonomy identifiers that will be registered for the post type.
     875 *     Default is no taxonomies. Taxonomies can be registered later with register_taxonomy() or
     876 *     register_taxonomy_for_object_type().
     877 * - labels - An array of labels for this post type. By default post labels are used for non-hierarchical
     878 *     types and page labels for hierarchical ones. You can see accepted values in {@link get_post_type_labels()}.
    790879 * - permalink_epmask - The default rewrite endpoint bitmasks.
    791  * - rewrite - false to prevent rewrite, or array('slug'=>$slug) to customize permastruct; default will use $post_type as slug.
     880 * - has_archive - True to enable post type archives. Will generate the proper rewrite rules if rewrite is enabled.
     881 * - rewrite - false to prevent rewrite. Defaults to true. Use array('slug'=>$slug) to customize permastruct;
     882 *     default will use $post_type as slug. Other options include 'with_front', 'feeds', and 'pages'.
    792883 * - query_var - false to prevent queries, or string to value of the query var to use for this post type
    793884 * - can_export - true allows this post type to be exported.
    794885 * - show_in_nav_menus - true makes this post type available for selection in navigation menus.
    795  * - _builtin - true if this post type is a native or "built-in" post_type.  THIS IS FOR INTERNAL USE ONLY!
    796  * - _edit_link - URL segement to use for edit link of this post type.  Set to 'post.php?post=%d'.  THIS IS FOR INTERNAL USE ONLY!
     886 * - _builtin - true if this post type is a native or "built-in" post_type. THIS IS FOR INTERNAL USE ONLY!
     887 * - _edit_link - URL segement to use for edit link of this post type. THIS IS FOR INTERNAL USE ONLY!
    797888 *
    798889 * @since 2.9.0
     
    801892 * @param string $post_type Name of the post type.
    802893 * @param array|string $args See above description.
    803  * @return object the registered post type object
     894 * @return object|WP_Error the registered post type object, or an error object
    804895 */
    805896function register_post_type($post_type, $args = array()) {
     
    812903    $defaults = array(
    813904        'labels' => array(), 'description' => '', 'publicly_queryable' => null, 'exclude_from_search' => null,
    814         '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'capabilities' => array(), 'hierarchical' => false,
    815         'public' => false, 'rewrite' => true, 'query_var' => true, 'supports' => array(), 'register_meta_box_cb' => null,
     905        'capability_type' => 'post', 'capabilities' => array(), 'map_meta_cap' => null,
     906        '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'hierarchical' => false,
     907        'public' => false, 'rewrite' => true, 'has_archive' => false, 'query_var' => true,
     908        'supports' => array(), 'register_meta_box_cb' => null,
    816909        'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null,
    817         'permalink_epmask' => EP_PERMALINK, 'can_export' => true, 'show_in_nav_menus' => null
     910        'permalink_epmask' => EP_PERMALINK, 'can_export' => true, 'show_in_nav_menus' => null, 'show_in_menu' => null,
    818911    );
    819912    $args = wp_parse_args($args, $defaults);
    820913    $args = (object) $args;
    821914
    822     $post_type = sanitize_user($post_type, true);
     915    $post_type = sanitize_key($post_type);
    823916    $args->name = $post_type;
     917
     918    if ( strlen( $post_type ) > 20 )
     919            return new WP_Error( 'post_type_too_long', __( 'Post types cannot exceed 20 characters in length' ) );
    824920
    825921    // If not set, default to the setting for public.
     
    831927        $args->show_ui = $args->public;
    832928
     929    // If not set, default to the setting for show_ui.
     930    if ( null === $args->show_in_menu || ! $args->show_ui )
     931        $args->show_in_menu = $args->show_ui;
     932
    833933    // Whether to show this type in nav-menus.php.  Defaults to the setting for public.
    834934    if ( null === $args->show_in_nav_menus )
     
    839939        $args->exclude_from_search = !$args->public;
    840940
    841     if ( empty($args->capability_type) )
    842         $args->capability_type = 'post';
     941    // Back compat with quirky handling in version 3.0. #14122
     942    if ( empty( $args->capabilities ) && null === $args->map_meta_cap && in_array( $args->capability_type, array( 'post', 'page' ) ) )
     943        $args->map_meta_cap = true;
     944
     945    if ( null === $args->map_meta_cap )
     946        $args->map_meta_cap = false;
    843947
    844948    $args->cap = get_post_type_capabilities( $args );
    845949    unset($args->capabilities);
     950
     951    if ( is_array( $args->capability_type ) )
     952        $args->capability_type = $args->capability_type[0];
    846953
    847954    if ( ! empty($args->supports) ) {
     
    861968
    862969    if ( false !== $args->rewrite && '' != get_option('permalink_structure') ) {
    863         if ( !is_array($args->rewrite) )
     970        if ( ! is_array( $args->rewrite ) )
    864971            $args->rewrite = array();
    865         if ( !isset($args->rewrite['slug']) )
     972        if ( empty( $args->rewrite['slug'] ) )
    866973            $args->rewrite['slug'] = $post_type;
    867         if ( !isset($args->rewrite['with_front']) )
     974        if ( ! isset( $args->rewrite['with_front'] ) )
    868975            $args->rewrite['with_front'] = true;
     976        if ( ! isset( $args->rewrite['pages'] ) )
     977            $args->rewrite['pages'] = true;
     978        if ( ! isset( $args->rewrite['feeds'] ) || ! $args->has_archive )
     979            $args->rewrite['feeds'] = (bool) $args->has_archive;
     980
    869981        if ( $args->hierarchical )
    870982            $wp_rewrite->add_rewrite_tag("%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
    871983        else
    872984            $wp_rewrite->add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
     985
     986        if ( $args->has_archive ) {
     987            $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive;
     988            $wp_rewrite->add_rule( "{$archive_slug}/?$", "index.php?post_type=$post_type", 'top' );
     989            if ( $args->rewrite['feeds'] && $wp_rewrite->feeds ) {
     990                $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
     991                $wp_rewrite->add_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
     992                $wp_rewrite->add_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
     993            }
     994            if ( $args->rewrite['pages'] )
     995                $wp_rewrite->add_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&paged=$matches[1]', 'top' );
     996        }
     997
    873998        $wp_rewrite->add_permastruct($post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite['with_front'], $args->permalink_epmask);
    874999    }
     
    8941019 * Builds an object with all post type capabilities out of a post type object
    8951020 *
    896  * Accepted keys of the capabilities array in the post type object:
    897  * - edit_post - The meta capability that controls editing a particular object of this post type. Defaults to "edit_ . $capability_type" (edit_post).
    898  * - edit_posts - The capability that controls editing objects of this post type as a class. Defaults to "edit_ . $capability_type . s" (edit_posts).
    899  * - edit_others_posts - The capability that controls editing objects of this post type that are owned by other users. Defaults to "edit_others_ . $capability_type . s" (edit_others_posts).
    900  * - publish_posts - The capability that controls publishing objects of this post type. Defaults to "publish_ . $capability_type . s" (publish_posts).
    901  * - read_post - The meta capability that controls reading a particular object of this post type. Defaults to "read_ . $capability_type" (read_post).
    902  * - read_private_posts - The capability that controls reading private posts. Defaults to "read_private . $capability_type . s" (read_private_posts).
    903  * - delete_post - The meta capability that controls deleting a particular object of this post type. Defaults to "delete_ . $capability_type" (delete_post).
    904  *
     1021 * Post type capabilities use the 'capability_type' argument as a base, if the
     1022 * capability is not set in the 'capabilities' argument array or if the
     1023 * 'capabilities' argument is not supplied.
     1024 *
     1025 * The capability_type argument can optionally be registered as an array, with
     1026 * the first value being singular and the second plural, e.g. array('story, 'stories')
     1027 * Otherwise, an 's' will be added to the value for the plural form. After
     1028 * registration, capability_type will always be a string of the singular value.
     1029 *
     1030 * By default, seven keys are accepted as part of the capabilities array:
     1031 *
     1032 * - edit_post, read_post, and delete_post are meta capabilities, which are then
     1033 *   generally mapped to corresponding primitive capabilities depending on the
     1034 *   context, which would be the post being edited/read/deleted and the user or
     1035 *   role being checked. Thus these capabilities would generally not be granted
     1036 *   directly to users or roles.
     1037 *
     1038 * - edit_posts - Controls whether objects of this post type can be edited.
     1039 * - edit_others_posts - Controls whether objects of this type owned by other users
     1040 *   can be edited. If the post type does not support an author, then this will
     1041 *   behave like edit_posts.
     1042 * - publish_posts - Controls publishing objects of this post type.
     1043 * - read_private_posts - Controls whether private objects can be read.
     1044
     1045 * These four primitive capabilities are checked in core in various locations.
     1046 * There are also seven other primitive capabilities which are not referenced
     1047 * directly in core, except in map_meta_cap(), which takes the three aforementioned
     1048 * meta capabilities and translates them into one or more primitive capabilities
     1049 * that must then be checked against the user or role, depending on the context.
     1050 *
     1051 * - read - Controls whether objects of this post type can be read.
     1052 * - delete_posts - Controls whether objects of this post type can be deleted.
     1053 * - delete_private_posts - Controls whether private objects can be deleted.
     1054 * - delete_published_posts - Controls whether published objects can be deleted.
     1055 * - delete_others_posts - Controls whether objects owned by other users can be
     1056 *   can be deleted. If the post type does not support an author, then this will
     1057 *   behave like delete_posts.
     1058 * - edit_private_posts - Controls whether private objects can be edited.
     1059 * - edit_published_posts - Controls whether published objects can be deleted.
     1060 *
     1061 * These additional capabilities are only used in map_meta_cap(). Thus, they are
     1062 * only assigned by default if the post type is registered with the 'map_meta_cap'
     1063 * argument set to true (default is false).
     1064 *
     1065 * @see map_meta_cap()
    9051066 * @since 3.0.0
    906  * @param object $args
     1067 *
     1068 * @param object $args Post type registration arguments
    9071069 * @return object object with all the capabilities as member variables
    9081070 */
    9091071function get_post_type_capabilities( $args ) {
    910     $defaults = array(
    911         'edit_post'          => 'edit_'         . $args->capability_type,
    912         'edit_posts'         => 'edit_'         . $args->capability_type . 's',
    913         'edit_others_posts'  => 'edit_others_'  . $args->capability_type . 's',
    914         'publish_posts'      => 'publish_'      . $args->capability_type . 's',
    915         'read_post'          => 'read_'         . $args->capability_type,
    916         'read_private_posts' => 'read_private_' . $args->capability_type . 's',
    917         'delete_post'        => 'delete_'       . $args->capability_type,
     1072    if ( ! is_array( $args->capability_type ) )
     1073        $args->capability_type = array( $args->capability_type, $args->capability_type . 's' );
     1074
     1075    // Singular base for meta capabilities, plural base for primitive capabilities.
     1076    list( $singular_base, $plural_base ) = $args->capability_type;
     1077
     1078    $default_capabilities = array(
     1079        // Meta capabilities
     1080        'edit_post'          => 'edit_'         . $singular_base,
     1081        'read_post'          => 'read_'         . $singular_base,
     1082        'delete_post'        => 'delete_'       . $singular_base,
     1083        // Primitive capabilities used outside of map_meta_cap():
     1084        'edit_posts'         => 'edit_'         . $plural_base,
     1085        'edit_others_posts'  => 'edit_others_'  . $plural_base,
     1086        'publish_posts'      => 'publish_'      . $plural_base,
     1087        'read_private_posts' => 'read_private_' . $plural_base,
    9181088    );
    919     $labels = array_merge( $defaults, $args->capabilities );
    920     return (object) $labels;
     1089
     1090    // Primitive capabilities used within map_meta_cap():
     1091    if ( $args->map_meta_cap ) {
     1092        $default_capabilities_for_mapping = array(
     1093            'read'                   => 'read',
     1094            'delete_posts'           => 'delete_'           . $plural_base,
     1095            'delete_private_posts'   => 'delete_private_'   . $plural_base,
     1096            'delete_published_posts' => 'delete_published_' . $plural_base,
     1097            'delete_others_posts'    => 'delete_others_'    . $plural_base,
     1098            'edit_private_posts'     => 'edit_private_'     . $plural_base,
     1099            'edit_published_posts'   => 'edit_published_'   . $plural_base,
     1100        );
     1101        $default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping );
     1102    }
     1103
     1104    $capabilities = array_merge( $default_capabilities, $args->capabilities );
     1105
     1106    // Remember meta capabilities for future reference.
     1107    if ( $args->map_meta_cap )
     1108        _post_type_meta_capabilities( $capabilities );
     1109
     1110    return (object) $capabilities;
     1111}
     1112
     1113/**
     1114 * Stores or returns a list of post type meta caps for map_meta_cap().
     1115 *
     1116 * @since 3.1.0
     1117 * @access private
     1118 */
     1119function _post_type_meta_capabilities( $capabilities = null ) {
     1120    static $meta_caps = array();
     1121    if ( null === $capabilities )
     1122        return $meta_caps;
     1123    foreach ( $capabilities as $core => $custom ) {
     1124        if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) )
     1125            $meta_caps[ $custom ] = $core;
     1126    }
    9211127}
    9221128
     
    9371143 * - parent_item_colon - This string isn't used on non-hierarchical types. In hierarchical ones the default is Parent Page:
    9381144 *
    939  * Above, the first default value is for non-hierarchical post types (like posts) and the second one is for hierarchical post types (like pages.)
     1145 * Above, the first default value is for non-hierarchical post types (like posts) and the second one is for hierarchical post types (like pages).
    9401146 *
    9411147 * @since 3.0.0
     
    9531159        'view_item' => array( __('View Post'), __('View Page') ),
    9541160        'search_items' => array( __('Search Posts'), __('Search Pages') ),
    955         'not_found' => array( __('No posts found'), __('No pages found') ),
    956         'not_found_in_trash' => array( __('No posts found in Trash'), __('No pages found in Trash') ),
    957         'parent_item_colon' => array( null, __('Parent Page:') )
     1161        'not_found' => array( __('No posts found.'), __('No pages found.') ),
     1162        'not_found_in_trash' => array( __('No posts found in Trash.'), __('No pages found in Trash.') ),
     1163        'parent_item_colon' => array( null, __('Parent Page:') ),
    9581164    );
     1165    $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
    9591166    return _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
    9601167}
     
    9641171 *
    9651172 * @access private
     1173 * @since 3.0.0
    9661174 */
    9671175function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) {
     
    9731181        $object->labels['singular_name'] = $object->labels['name'];
    9741182
    975     $defaults = array_map( create_function( '$x', $object->hierarchical? 'return $x[1];' : 'return $x[0];' ), $nohier_vs_hier_defaults );
     1183    if ( !isset( $object->labels['menu_name'] ) && isset( $object->labels['name'] ) )
     1184        $object->labels['menu_name'] = $object->labels['name'];
     1185
     1186    foreach ( $nohier_vs_hier_defaults as $key => $value )
     1187            $defaults[$key] = $object->hierarchical ? $value[1] : $value[0];
     1188
    9761189    $labels = array_merge( $defaults, $object->labels );
    9771190    return (object)$labels;
    9781191}
     1192
     1193/**
     1194 * Adds submenus for post types.
     1195 *
     1196 * @access private
     1197 * @since 3.1.0
     1198 */
     1199function _add_post_type_submenus() {
     1200    foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) {
     1201        $ptype_obj = get_post_type_object( $ptype );
     1202        // Submenus only.
     1203        if ( ! $ptype_obj->show_in_menu || $ptype_obj->show_in_menu === true )
     1204            continue;
     1205        add_submenu_page( $ptype_obj->show_in_menu, $ptype_obj->labels->name, $ptype_obj->labels->menu_name, $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype" );
     1206    }
     1207}
     1208add_action( 'admin_menu', '_add_post_type_submenus' );
    9791209
    9801210/**
     
    11121342    if ( empty( $r['post_status'] ) )
    11131343        $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish';
    1114     if ( ! empty($r['numberposts']) )
     1344    if ( ! empty($r['numberposts']) && empty($r['posts_per_page']) )
    11151345        $r['posts_per_page'] = $r['numberposts'];
    11161346    if ( ! empty($r['category']) )
     
    11231353        $r['post__not_in'] = wp_parse_id_list( $r['exclude'] );
    11241354
    1125     $r['caller_get_posts'] = true;
     1355    $r['ignore_sticky_posts'] = true;
     1356    $r['no_found_rows'] = true;
    11261357
    11271358    $get_posts = new WP_Query;
     
    11441375 *
    11451376 * @param int $post_id Post ID.
    1146  * @param string $key Metadata name.
    1147  * @param mixed $value Metadata value.
     1377 * @param string $meta_key Metadata name.
     1378 * @param mixed $meta_value Metadata value.
    11481379 * @param bool $unique Optional, default is false. Whether the same key should not be added.
    11491380 * @return bool False for failure. True for success.
     
    12061437 * If the meta field for the post does not exist, it will be added.
    12071438 *
    1208  * @since 1.5
     1439 * @since 1.5.0
    12091440 * @uses $wpdb
    12101441 * @link http://codex.wordpress.org/Function_Reference/update_post_meta
    12111442 *
    12121443 * @param int $post_id Post ID.
    1213  * @param string $key Metadata key.
    1214  * @param mixed $value Metadata value.
     1444 * @param string $meta_key Metadata key.
     1445 * @param mixed $meta_value Metadata value.
    12151446 * @param mixed $prev_value Optional. Previous value to check before removing.
    12161447 * @return bool False on failure, true if success.
     
    12671498 * @return array
    12681499 */
    1269 function get_post_custom($post_id = 0) {
    1270     global $id;
    1271 
    1272     if ( !$post_id )
    1273         $post_id = (int) $id;
    1274 
    1275     $post_id = (int) $post_id;
    1276 
    1277     if ( ! wp_cache_get($post_id, 'post_meta') )
    1278         update_postmeta_cache($post_id);
    1279 
    1280     return wp_cache_get($post_id, 'post_meta');
     1500function get_post_custom( $post_id = 0 ) {
     1501    $post_id = absint( $post_id );
     1502
     1503    if ( ! $post_id )
     1504        $post_id = get_the_ID();
     1505
     1506    if ( ! wp_cache_get( $post_id, 'post_meta' ) )
     1507        update_postmeta_cache( $post_id );
     1508
     1509    return wp_cache_get( $post_id, 'post_meta' );
    12811510}
    12821511
     
    13351564 * @return bool Whether post is sticky.
    13361565 */
    1337 function is_sticky($post_id = null) {
    1338     global $id;
    1339 
    1340     $post_id = absint($post_id);
    1341 
    1342     if ( !$post_id )
    1343         $post_id = absint($id);
    1344 
    1345     $stickies = get_option('sticky_posts');
    1346 
    1347     if ( !is_array($stickies) )
     1566function is_sticky( $post_id = 0 ) {
     1567    $post_id = absint( $post_id );
     1568
     1569    if ( ! $post_id )
     1570        $post_id = get_the_ID();
     1571
     1572    $stickies = get_option( 'sticky_posts' );
     1573
     1574    if ( ! is_array( $stickies ) )
    13481575        return false;
    13491576
    1350     if ( in_array($post_id, $stickies) )
     1577    if ( in_array( $post_id, $stickies ) )
    13511578        return true;
    13521579
     
    13971624 *
    13981625 * @since 2.3.0
    1399  * @uses apply_filters() Calls 'edit_$field' and '${field_no_prefix}_edit_pre' passing $value and
     1626 * @uses apply_filters() Calls 'edit_$field' and '{$field_no_prefix}_edit_pre' passing $value and
    14001627 *  $post_id if $context == 'edit' and field name prefix == 'post_'.
    14011628 *
    14021629 * @uses apply_filters() Calls 'edit_post_$field' passing $value and $post_id if $context == 'db'.
    14031630 * @uses apply_filters() Calls 'pre_$field' passing $value if $context == 'db' and field name prefix == 'post_'.
    1404  * @uses apply_filters() Calls '${field}_pre' passing $value if $context == 'db' and field name prefix != 'post_'.
     1631 * @uses apply_filters() Calls '{$field}_pre' passing $value if $context == 'db' and field name prefix != 'post_'.
    14051632 *
    14061633 * @uses apply_filters() Calls '$field' passing $value, $post_id and $context if $context == anything
     
    14411668
    14421669        if ( $prefixed ) {
    1443             $value = apply_filters("edit_$field", $value, $post_id);
     1670            $value = apply_filters("edit_{$field}", $value, $post_id);
    14441671            // Old school
    1445             $value = apply_filters("${field_no_prefix}_edit_pre", $value, $post_id);
     1672            $value = apply_filters("{$field_no_prefix}_edit_pre", $value, $post_id);
    14461673        } else {
    1447             $value = apply_filters("edit_post_$field", $value, $post_id);
     1674            $value = apply_filters("edit_post_{$field}", $value, $post_id);
    14481675        }
    14491676
     
    14581685    } else if ( 'db' == $context ) {
    14591686        if ( $prefixed ) {
    1460             $value = apply_filters("pre_$field", $value);
    1461             $value = apply_filters("${field_no_prefix}_save_pre", $value);
     1687            $value = apply_filters("pre_{$field}", $value);
     1688            $value = apply_filters("{$field_no_prefix}_save_pre", $value);
    14621689        } else {
    1463             $value = apply_filters("pre_post_$field", $value);
    1464             $value = apply_filters("${field}_pre", $value);
     1690            $value = apply_filters("pre_post_{$field}", $value);
     1691            $value = apply_filters("{$field}_pre", $value);
    14651692        }
    14661693    } else {
     
    14691696            $value = apply_filters($field, $value, $post_id, $context);
    14701697        else
    1471             $value = apply_filters("post_$field", $value, $post_id, $context);
     1698            $value = apply_filters("post_{$field}", $value, $post_id, $context);
    14721699    }
    14731700
     
    16541881 * @since 2.5.0
    16551882 *
    1656  * @param string|array $mime_types List of mime types or comma separated string of mime types.
     1883 * @param string|array $post_mime_types List of mime types or comma separated string of mime types.
    16571884 * @param string $table_alias Optional. Specify a table alias, if needed.
    16581885 * @return string The SQL AND clause for mime searching.
     
    18132040 * @uses wp_delete_post() if trash is disabled
    18142041 *
    1815  * @param int $postid Post ID.
     2042 * @param int $post_id Post ID.
    18162043 * @return mixed False on failure
    18172044 */
     
    18482075 * @uses do_action() on 'untrashed_post' after undeletion
    18492076 *
    1850  * @param int $postid Post ID.
     2077 * @param int $post_id Post ID.
    18512078 * @return mixed False on failure
    18522079 */
     
    20412268 *
    20422269 * @since 1.0.0
    2043  * @uses $wpdb
    2044  *
    2045  * @param int $num Optional, default is 10. Number of posts to get.
    2046  * @return array List of posts.
    2047  */
    2048 function wp_get_recent_posts($num = 10) {
    2049     global $wpdb;
    2050 
    2051     // Set the limit clause, if we got a limit
    2052     $num = (int) $num;
    2053     if ( $num ) {
    2054         $limit = "LIMIT $num";
    2055     }
    2056 
    2057     $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND post_status IN ( 'draft', 'publish', 'future', 'pending', 'private' ) ORDER BY post_date DESC $limit";
    2058     $result = $wpdb->get_results($sql, ARRAY_A);
    2059 
    2060     return $result ? $result : array();
     2270 * @uses wp_parse_args()
     2271 * @uses get_posts()
     2272 *
     2273 * @param string $deprecated Deprecated.
     2274 * @param array $args Optional. Overrides defaults.
     2275 * @param string $output Optional.
     2276 * @return unknown.
     2277 */
     2278function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) {
     2279
     2280    if ( is_numeric( $args ) ) {
     2281        _deprecated_argument( __FUNCTION__, '3.1', __( 'Passing an integer number of posts is deprecated. Pass an array of arguments instead.' ) );
     2282        $args = array( 'numberposts' => absint( $args ) );
     2283    }
     2284
     2285    // Set default arguments
     2286    $defaults = array(
     2287        'numberposts' => 10, 'offset' => 0,
     2288        'category' => 0, 'orderby' => 'post_date',
     2289        'order' => 'DESC', 'include' => '',
     2290        'exclude' => '', 'meta_key' => '',
     2291        'meta_value' =>'', 'post_type' => 'post', 'post_status' => 'draft, publish, future, pending, private',
     2292        'suppress_filters' => true
     2293    );
     2294
     2295    $r = wp_parse_args( $args, $defaults );
     2296
     2297    $results = get_posts( $r );
     2298
     2299    // Backward compatibility. Prior to 3.1 expected posts to be returned in array
     2300    if ( ARRAY_A == $output ){
     2301        foreach( $results as $key => $result ) {
     2302            $results[$key] = get_object_vars( $result );
     2303        }
     2304        return $results ? $results : array();
     2305    }
     2306
     2307    return $results ? $results : false;
     2308
    20612309}
    20622310
     
    20782326    $post = get_post($postid, $mode);
    20792327
    2080     if ( 
     2328    if (
    20812329        ( OBJECT == $mode && empty( $post->ID ) ) ||
    20822330        ( OBJECT != $mode && empty( $post['ID'] ) )
     
    21292377 *
    21302378 * @since 1.0.0
    2131  * @link http://core.trac.wordpress.org/ticket/9084 Bug report on 'wp_insert_post_data' filter.
    21322379 * @uses $wpdb
    21332380 * @uses $wp_rewrite
    21342381 * @uses $user_ID
    2135  *
    21362382 * @uses do_action() Calls 'pre_post_update' on post ID if this is an update.
    21372383 * @uses do_action() Calls 'edit_post' action on post ID and post data if this is an update.
    2138  * @uses do_action() Calls 'save_post' and 'wp_insert_post' on post id and post data just before
    2139  *                   returning.
    2140  *
    2141  * @uses apply_filters() Calls 'wp_insert_post_data' passing $data, $postarr prior to database
    2142  *                       update or insert.
     2384 * @uses do_action() Calls 'save_post' and 'wp_insert_post' on post id and post data just before returning.
     2385 * @uses apply_filters() Calls 'wp_insert_post_data' passing $data, $postarr prior to database update or insert.
    21432386 * @uses wp_transition_post_status()
    21442387 *
    2145  * @param array $postarr Optional. Overrides defaults.
     2388 * @param array $postarr Elements that make up post to insert.
    21462389 * @param bool $wp_error Optional. Allow return of WP_Error on failure.
    21472390 * @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success.
    21482391 */
    2149 function wp_insert_post($postarr = array(), $wp_error = false) {
     2392function wp_insert_post($postarr, $wp_error = false) {
    21502393    global $wpdb, $wp_rewrite, $user_ID;
    21512394
     
    22742517        $post_parent = 0;
    22752518
    2276     if ( !empty($post_ID) ) {
    2277         if ( $post_parent == $post_ID ) {
    2278             // Post can't be its own parent
    2279             $post_parent = 0;
    2280         } elseif ( !empty($post_parent) ) {
    2281             $parent_post = get_post($post_parent);
    2282             // Check for circular dependency
    2283             if ( isset( $parent_post->post_parent ) && $parent_post->post_parent == $post_ID )
    2284                 $post_parent = 0;
    2285         }
    2286     }
     2519    // Check the post_parent to see if it will cause a hierarchy loop
     2520    $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr );
    22872521
    22882522    if ( isset($menu_order) )
     
    25192753 * Computes a unique slug for the post, when given the desired slug and some post details.
    25202754 *
     2755 * @since 2.8.0
     2756 *
    25212757 * @global wpdb $wpdb
    25222758 * @global WP_Rewrite $wp_rewrite
     
    25442780        $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) );
    25452781
    2546         if ( $post_name_check || in_array( $slug, $feeds ) ) {
     2782        if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) {
    25472783            $suffix = 2;
    25482784            do {
     
    25592795        $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID, $post_parent ) );
    25602796
    2561         if ( $post_name_check || in_array( $slug, $feeds ) || preg_match( '@^(page)?\d+$@', $slug ) ) {
     2797        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 ) ) {
    25622798            $suffix = 2;
    25632799            do {
     
    25732809        $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) );
    25742810
    2575         if ( $post_name_check || in_array( $slug, $feeds ) ) {
     2811        if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) {
    25762812            $suffix = 2;
    25772813            do {
     
    26142850 * @param string $tags The tags to set for the post, separated by commas.
    26152851 * @param bool $append If true, don't delete existing tags, just add on. If false, replace the tags with the new tags.
    2616  * @return bool|null Will return false if $post_id is not an integer or is 0. Will return null otherwise
     2852 * @return mixed Array of affected term IDs. WP_Error or false on failure.
    26172853 */
    26182854function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) {
     
    26292865 * @param string $tags The tags to set for the post, separated by commas.
    26302866 * @param bool $append If true, don't delete existing tags, just add on. If false, replace the tags with the new tags.
    2631  * @return bool|null Will return false if $post_id is not an integer or is 0. Will return null otherwise
     2867 * @return mixed Array of affected term IDs. WP_Error or false on failure.
    26322868 */
    26332869function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false ) {
     
    26492885    }
    26502886
    2651     wp_set_object_terms($post_id, $tags, $taxonomy, $append);
     2887    return wp_set_object_terms($post_id, $tags, $taxonomy, $append);
    26522888}
    26532889
     
    27052941 * @uses do_action() Calls 'transition_post_status' on $new_status, $old_status and
    27062942 *  $post if there is a status change.
    2707  * @uses do_action() Calls '${old_status}_to_$new_status' on $post if there is a status change.
    2708  * @uses do_action() Calls '${new_status}_$post->post_type' on post ID and $post.
     2943 * @uses do_action() Calls '{$old_status}_to_{$new_status}' on $post if there is a status change.
     2944 * @uses do_action() Calls '{$new_status}_{$post->post_type}' on post ID and $post.
    27092945 *
    27102946 * @param string $new_status Transition to this post status.
     
    27142950function wp_transition_post_status($new_status, $old_status, $post) {
    27152951    do_action('transition_post_status', $new_status, $old_status, $post);
    2716     do_action("${old_status}_to_$new_status", $post);
    2717     do_action("${new_status}_$post->post_type", $post->ID, $post);
     2952    do_action("{$old_status}_to_{$new_status}", $post);
     2953    do_action("{$new_status}_{$post->post_type}", $post->ID, $post);
    27182954}
    27192955
     
    27683004        }
    27693005    }
    2770     $pung = apply_filters('get_enclosed', $pung);
     3006    $pung = apply_filters('get_enclosed', $pung, $post_id);
    27713007    return $pung;
    27723008}
     
    28903126function get_page_by_path($page_path, $output = OBJECT, $post_type = 'page') {
    28913127    global $wpdb;
     3128    $null = null;
    28923129    $page_path = rawurlencode(urldecode($page_path));
    28933130    $page_path = str_replace('%2F', '/', $page_path);
     
    29033140
    29043141    if ( empty($pages) )
    2905         return null;
     3142        return $null;
    29063143
    29073144    foreach ( $pages as $page ) {
     
    29093146        $curpage = $page;
    29103147        while ( $curpage->post_parent != 0 ) {
    2911             $curpage = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = %d and post_type = %s", $curpage->post_parent, $post_type ));
     3148            $post_parent = $curpage->post_parent;
     3149            $curpage = wp_cache_get( $post_parent, 'posts' );
     3150            if ( false === $curpage )
     3151                $curpage = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = %d and post_type = %s", $post_parent, $post_type ) );
    29123152            $path = '/' . $curpage->post_name . $path;
    29133153        }
     
    29173157    }
    29183158
    2919     return null;
     3159    return $null;
    29203160}
    29213161
     
    29733213 * @since 2.0.0
    29743214 *
    2975  * @param array $posts Posts array.
    2976  * @param int $parent Parent page ID.
     3215 * @param array $pages Posts array.
     3216 * @param int $page_id Parent page ID.
    29773217 * @return array A list arranged by hierarchy. Children immediately follow their parents.
    29783218 */
    29793219function &get_page_hierarchy( &$pages, $page_id = 0 ) {
    2980 
    29813220    if ( empty( $pages ) ) {
    29823221        $result = array();
     
    29863225    $children = array();
    29873226    foreach ( (array) $pages as $p ) {
    2988 
    29893227        $parent_id = intval( $p->post_parent );
    29903228        $children[ $parent_id ][] = $p;
    2991      }
    2992 
    2993      $result = array();
    2994      _page_traverse_name( $page_id, $children, $result );
     3229    }
     3230
     3231    $result = array();
     3232    _page_traverse_name( $page_id, $children, $result );
    29953233
    29963234    return $result;
     
    30013239 * $children contains parent-chilren relations
    30023240 *
     3241 * @since 2.9.0
    30033242 */
    30043243function _page_traverse_name( $page_id, &$children, &$result ){
    3005 
    30063244    if ( isset( $children[ $page_id ] ) ){
    3007 
    30083245        foreach( (array)$children[ $page_id ] as $child ) {
    3009 
    30103246            $result[ $child->ID ] = $child->post_name;
    30113247            _page_traverse_name( $child->ID, $children, $result );
     
    32863522 * @param string|array $object Arguments to override defaults.
    32873523 * @param string $file Optional filename.
    3288  * @param int $post_parent Parent post ID.
     3524 * @param int $parent Parent post ID.
    32893525 * @return int Attachment ID.
    32903526 */
     
    34363672 * @uses do_action() Calls 'delete_attachment' hook on Attachment ID.
    34373673 *
    3438  * @param int $postid Attachment ID.
     3674 * @param int $post_id Attachment ID.
    34393675 * @param bool $force_delete Whether to bypass trash and force deletion. Defaults to false.
    34403676 * @return mixed False on failure. Post data on success.
     
    35123748            $del_file = path_join( dirname($meta['file']), $size['file'] );
    35133749            $del_file = apply_filters('wp_delete_file', $del_file);
    3514             @ unlink( path_join($uploadpath['basedir'], $del_file) );
     3750            @ unlink( path_join($uploadpath['basedir'], $del_file) );
    35153751        }
    35163752    }
     
    35853821            if ( 0 === strpos($file, $uploads['basedir']) ) //Check that the upload base exists in the file location
    35863822                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); //replace file location with url location
    3587             elseif ( false !== strpos($file, 'wp-content/uploads') )
    3588                 $url = $uploads['baseurl'] . substr( $file, strpos($file, 'wp-content/uploads') + 18 );
    3589             else
    3590                 $url = $uploads['baseurl'] . "/$file"; //Its a newly uploaded file, therefor $file is relative to the basedir.
     3823            elseif ( false !== strpos($file, 'wp-content/uploads') )
     3824                $url = $uploads['baseurl'] . substr( $file, strpos($file, 'wp-content/uploads') + 18 );
     3825            else
     3826                $url = $uploads['baseurl'] . "/$file"; //Its a newly uploaded file, therefor $file is relative to the basedir.
    35913827        }
    35923828    }
     
    35953831        $url = get_the_guid( $post->ID );
    35963832
    3597     if ( 'attachment' != $post->post_type || empty($url) )
     3833    $url = apply_filters( 'wp_get_attachment_url', $url, $post->ID );
     3834
     3835    if ( 'attachment' != $post->post_type || empty( $url ) )
    35983836        return false;
    35993837
    3600     return apply_filters( 'wp_get_attachment_url', $url, $post->ID );
     3838    return $url;
    36013839}
    36023840
     
    37624000
    37634001/**
    3764  * Checked for changed slugs for published posts and save old slug.
    3765  *
    3766  * The function is used along with form POST data. It checks for the wp-old-slug
    3767  * POST field. Will only be concerned with published posts and the slug actually
    3768  * changing.
     4002 * Checked for changed slugs for published post objects and save the old slug.
     4003 *
     4004 * The function is used when a post object of any type is updated,
     4005 * by comparing the current and previous post objects.
    37694006 *
    37704007 * If the slug was changed and not already part of the old slugs then it will be
     
    37724009 * post.
    37734010 *
    3774  * The most logically usage of this function is redirecting changed posts, so
     4011 * The most logically usage of this function is redirecting changed post objects, so
    37754012 * that those that linked to an changed post will be redirected to the new post.
    37764013 *
     
    37784015 *
    37794016 * @param int $post_id Post ID.
     4017 * @param object $post The Post Object
     4018 * @param object $post_before The Previous Post Object
    37804019 * @return int Same as $post_id
    37814020 */
     
    37854024        return;
    37864025
    3787     // we're only concerned with published posts
    3788     if ( $post->post_status != 'publish' || $post->post_type != 'post' )
     4026    // we're only concerned with published, non-hierarchical objects
     4027    if ( $post->post_status != 'publish' || is_post_type_hierarchical( $post->post_type ) )
    37894028        return;
    37904029
     
    37924031
    37934032    // if we haven't added this old slug before, add it now
    3794     if ( !in_array($post_before->post_name, $old_slugs) )
     4033    if ( !empty( $post_before->post_name ) && !in_array($post_before->post_name, $old_slugs) )
    37954034        add_post_meta($post_id, '_wp_old_slug', $post_before->post_name);
    37964035
     
    38954134 * @since 0.71
    38964135 *
    3897  * @uses $wpdb
    3898  * @uses $blog_id
    38994136 * @uses apply_filters() Calls 'get_lastpostdate' filter
    3900  *
    3901  * @global mixed $cache_lastpostdate Stores the last post date
    3902  * @global mixed $pagenow The current page being viewed
    39034137 *
    39044138 * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'.
     
    39064140 */
    39074141function get_lastpostdate($timezone = 'server') {
    3908     global $cache_lastpostdate, $wpdb, $blog_id;
    3909     $add_seconds_server = date('Z');
    3910     if ( !isset($cache_lastpostdate[$blog_id][$timezone]) ) {
    3911         switch(strtolower($timezone)) {
    3912             case 'gmt':
    3913                 $lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date_gmt DESC LIMIT 1");
    3914                 break;
    3915             case 'blog':
    3916                 $lastpostdate = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date_gmt DESC LIMIT 1");
    3917                 break;
    3918             case 'server':
    3919                 $lastpostdate = $wpdb->get_var("SELECT DATE_ADD(post_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date_gmt DESC LIMIT 1");
    3920                 break;
    3921         }
    3922         $cache_lastpostdate[$blog_id][$timezone] = $lastpostdate;
    3923     } else {
    3924         $lastpostdate = $cache_lastpostdate[$blog_id][$timezone];
    3925     }
    3926     return apply_filters( 'get_lastpostdate', $lastpostdate, $timezone );
     4142    return apply_filters( 'get_lastpostdate', _get_last_post_time( $timezone, 'date' ), $timezone );
    39274143}
    39284144
     
    39354151 *
    39364152 * @since 1.2.0
    3937  * @uses $wpdb
    3938  * @uses $blog_id
    39394153 * @uses apply_filters() Calls 'get_lastpostmodified' filter
    39404154 *
     
    39434157 */
    39444158function get_lastpostmodified($timezone = 'server') {
    3945     global $wpdb;
    3946 
    3947     $add_seconds_server = date('Z');
    3948     $timezone = strtolower( $timezone );
    3949 
    3950     $lastpostmodified = wp_cache_get( "lastpostmodified:$timezone", 'timeinfo' );
    3951     if ( $lastpostmodified )
    3952         return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone );
    3953 
    3954     switch ( strtolower($timezone) ) {
    3955         case 'gmt':
    3956             $lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_modified_gmt DESC LIMIT 1");
    3957             break;
    3958         case 'blog':
    3959             $lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_modified_gmt DESC LIMIT 1");
    3960             break;
    3961         case 'server':
    3962             $lastpostmodified = $wpdb->get_var("SELECT DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_modified_gmt DESC LIMIT 1");
    3963             break;
    3964     }
     4159    $lastpostmodified = _get_last_post_time( $timezone, 'modified' );
    39654160
    39664161    $lastpostdate = get_lastpostdate($timezone);
     
    39684163        $lastpostmodified = $lastpostdate;
    39694164
    3970     if ( $lastpostmodified )
    3971         wp_cache_set( "lastpostmodified:$timezone", $lastpostmodified, 'timeinfo' );
    3972 
    39734165    return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone );
     4166}
     4167
     4168/**
     4169 * Retrieve latest post date data based on timezone.
     4170 *
     4171 * @access private
     4172 * @since 3.1.0
     4173 *
     4174 * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'.
     4175 * @param string $field Field to check. Can be 'date' or 'modified'.
     4176 * @return string The date.
     4177 */
     4178function _get_last_post_time( $timezone, $field ) {
     4179    global $wpdb;
     4180
     4181    if ( !in_array( $field, array( 'date', 'modified' ) ) )
     4182        return false;
     4183
     4184    $timezone = strtolower( $timezone );
     4185
     4186    $key = "lastpost{$field}:$timezone";
     4187
     4188    $date = wp_cache_get( $key, 'timeinfo' );
     4189
     4190    if ( !$date ) {
     4191        $add_seconds_server = date('Z');
     4192
     4193        $post_types = get_post_types( array( 'publicly_queryable' => true ) );
     4194        array_walk( $post_types, array( &$wpdb, 'escape_by_ref' ) );
     4195        $post_types = "'" . implode( "', '", $post_types ) . "'";
     4196
     4197        switch ( $timezone ) {
     4198            case 'gmt':
     4199                $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");
     4200                break;
     4201            case 'blog':
     4202                $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");
     4203                break;
     4204            case 'server':
     4205                $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");
     4206                break;
     4207        }
     4208
     4209        if ( $date )
     4210            wp_cache_set( $key, $date, 'timeinfo' );
     4211    }
     4212
     4213    return $date;
    39744214}
    39754215
     
    40204260    $id = (int) $id;
    40214261
     4262    if ( 0 === $id )
     4263        return;
     4264
    40224265    wp_cache_delete($id, 'posts');
    40234266    wp_cache_delete($id, 'post_meta');
     
    40304273
    40314274    if ( $children = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_parent = %d", $id) ) ) {
    4032         foreach( $children as $cid )
     4275        foreach ( $children as $cid ) {
     4276            // Loop detection
     4277            if ( $cid == $id )
     4278                continue;
    40334279            clean_post_cache( $cid );
     4280        }
    40344281    }
    40354282
     
    41074354        $post_type = 'post';
    41084355
    4109     if ( !is_array($post_type) && 'any' != $post_type && $update_term_cache )
    4110         update_object_term_cache($post_ids, $post_type);
     4356    if ( $update_term_cache ) {
     4357        if ( is_array($post_type) ) {
     4358            $ptypes = $post_type;
     4359        } elseif ( 'any' == $post_type ) {
     4360            // Just use the post_types in the supplied posts.
     4361            foreach ( $posts as $post )
     4362                $ptypes[] = $post->post_type;
     4363            $ptypes = array_unique($ptypes);
     4364        } else {
     4365            $ptypes = array($post_type);
     4366        }
     4367
     4368        if ( ! empty($ptypes) )
     4369            update_object_term_cache($post_ids, $ptypes);
     4370    }
    41114371
    41124372    if ( $update_meta_cache )
     
    41984458    // If published posts changed clear the lastpostmodified cache
    41994459    if ( 'publish' == $new_status || 'publish' == $old_status) {
    4200         wp_cache_delete( 'lastpostmodified:server', 'timeinfo' );
    4201         wp_cache_delete( 'lastpostmodified:gmt',    'timeinfo' );
    4202         wp_cache_delete( 'lastpostmodified:blog',   'timeinfo' );
     4460        foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) {
     4461            wp_cache_delete( "lastpostmodified:$timezone", 'timeinfo' );
     4462            wp_cache_delete( "lastpostdate:$timezone", 'timeinfo' );
     4463        }
    42034464    }
    42044465
     
    43034564 * parent will be an ancestor. There will only be two ancestors at the most.
    43044565 *
    4305  * @since unknown
     4566 * @since 2.5.0
    43064567 * @access private
    43074568 * @uses $wpdb
     
    43234584    $id = $_post->ancestors[] = $_post->post_parent;
    43244585    while ( $ancestor = $wpdb->get_var( $wpdb->prepare("SELECT `post_parent` FROM $wpdb->posts WHERE ID = %d LIMIT 1", $id) ) ) {
    4325         if ( $id == $ancestor )
     4586        // Loop detection: If the ancestor has been seen before, break.
     4587        if ( ( $ancestor == $_post->ID ) || in_array($ancestor,  $_post->ancestors) )
    43264588            break;
    43274589        $id = $_post->ancestors[] = $ancestor;
     
    46514913 *
    46524914 * @param int|object $revision_id Revision ID or revision object.
    4653  * @param array $fields Optional. What fields to restore from.  Defaults to all.
    4654  * @return mixed Null if error, false if no fields to restore, (int) post ID if success.
     4915 * @return mixed Null or WP_Error if error, deleted post if success.
    46554916 */
    46564917function wp_delete_post_revision( $revision_id ) {
     
    47254986    }
    47264987}
     4988
     4989/**
     4990 * Returns the post's parent's post_ID
     4991 *
     4992 * @since 3.1.0
     4993 *
     4994 * @param int $post_id
     4995 *
     4996 * @return int|bool false on error
     4997 */
     4998function wp_get_post_parent_id( $post_ID ) {
     4999    $post = get_post( $post_ID );
     5000    if ( !$post || is_wp_error( $post ) )
     5001        return false;
     5002    return (int) $post->post_parent;
     5003}
     5004
     5005/**
     5006 * Checks the given subset of the post hierarchy for hierarchy loops.
     5007 * Prevents loops from forming and breaks those that it finds.
     5008 *
     5009 * Attached to the wp_insert_post_parent filter.
     5010 *
     5011 * @since 3.1.0
     5012 * @uses wp_find_hierarchy_loop()
     5013 *
     5014 * @param int $post_parent ID of the parent for the post we're checking.
     5015 * @parem int $post_ID ID of the post we're checking.
     5016 *
     5017 * @return int The new post_parent for the post.
     5018 */
     5019function wp_check_post_hierarchy_for_loops( $post_parent, $post_ID ) {
     5020    // Nothing fancy here - bail
     5021    if ( !$post_parent )
     5022        return 0;
     5023
     5024    // New post can't cause a loop
     5025    if ( empty( $post_ID ) )
     5026        return $post_parent;
     5027
     5028    // Can't be its own parent
     5029    if ( $post_parent == $post_ID )
     5030        return 0;
     5031
     5032    // Now look for larger loops
     5033
     5034    if ( !$loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_ID, $post_parent ) )
     5035        return $post_parent; // No loop
     5036
     5037    // Setting $post_parent to the given value causes a loop
     5038    if ( isset( $loop[$post_ID] ) )
     5039        return 0;
     5040
     5041    // There's a loop, but it doesn't contain $post_ID.  Break the loop.
     5042    foreach ( array_keys( $loop ) as $loop_member )
     5043        wp_update_post( array( 'ID' => $loop_member, 'post_parent' => 0 ) );
     5044
     5045    return $post_parent;
     5046}
     5047
     5048/**
     5049 * Returns an array of post format slugs to their translated and pretty display versions
     5050 *
     5051 * @since 3.1.0
     5052 *
     5053 * @return array The array of translations
     5054 */
     5055function get_post_format_strings() {
     5056    $strings = array(
     5057        'standard' => _x( 'Standard', 'Post format' ), // Special case. any value that evals to false will be considered standard
     5058        'aside'    => _x( 'Aside',    'Post format' ),
     5059        'chat'     => _x( 'Chat',     'Post format' ),
     5060        'gallery'  => _x( 'Gallery',  'Post format' ),
     5061        'link'     => _x( 'Link',     'Post format' ),
     5062        'image'    => _x( 'Image',    'Post format' ),
     5063        'quote'    => _x( 'Quote',    'Post format' ),
     5064        'status'   => _x( 'Status',   'Post format' ),
     5065        'video'    => _x( 'Video',    'Post format' ),
     5066        'audio'    => _x( 'Audio',    'Post format' ),
     5067    );
     5068    return $strings;
     5069}
     5070
     5071/**
     5072 * Retrieves an array of post format slugs.
     5073 *
     5074 * @since 3.1.0
     5075 *
     5076 * @return array The array of post format slugs.
     5077 */
     5078function get_post_format_slugs() {
     5079    // 3.2-early: use array_combine() and array_keys( get_post_format_strings() )
     5080    $slugs = array(
     5081        'standard' => 'standard', // Special case. any value that evals to false will be considered standard
     5082        'aside'    => 'aside',
     5083        'chat'     => 'chat',
     5084        'gallery'  => 'gallery',
     5085        'link'     => 'link',
     5086        'image'    => 'image',
     5087        'quote'    => 'quote',
     5088        'status'   => 'status',
     5089        'video'    => 'video',
     5090        'audio'    => 'audio',
     5091    );
     5092    return $slugs;
     5093}
     5094
     5095/**
     5096 * Returns a pretty, translated version of a post format slug
     5097 *
     5098 * @since 3.1.0
     5099 *
     5100 * @param string $slug A post format slug
     5101 * @return string The translated post format name
     5102 */
     5103function get_post_format_string( $slug ) {
     5104    $strings = get_post_format_strings();
     5105    if ( !$slug )
     5106        return $strings['standard'];
     5107    else
     5108        return ( isset( $strings[$slug] ) ) ? $strings[$slug] : '';
     5109}
     5110
     5111/**
     5112 * Sets a post thumbnail.
     5113 *
     5114 * @since 3.1.0
     5115 *
     5116 * @param int|object $post Post ID or object where thumbnail should be attached.
     5117 * @param int $thumbnail_id Thumbnail to attach.
     5118 * @return bool True on success, false on failure.
     5119 */
     5120function set_post_thumbnail( $post, $thumbnail_id ) {
     5121    $post = get_post( $post );
     5122    $thumbnail_id = absint( $thumbnail_id );
     5123    if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {
     5124        $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' );
     5125        if ( ! empty( $thumbnail_html ) ) {
     5126            update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );
     5127            return true;
     5128        }
     5129    }
     5130    return false;
     5131}
     5132
     5133/**
     5134 * Returns a link to a post format index.
     5135 *
     5136 * @since 3.1.0
     5137 *
     5138 * @param $format string Post format
     5139 * @return string Link
     5140 */
     5141function get_post_format_link( $format ) {
     5142    $term = get_term_by('slug', 'post-format-' . $format, 'post_format' );
     5143    if ( ! $term || is_wp_error( $term ) )
     5144        return false;
     5145    return get_term_link( $term );
     5146}
     5147
     5148/**
     5149 * Filters the request to allow for the format prefix.
     5150 *
     5151 * @access private
     5152 * @since 3.1.0
     5153 */
     5154function _post_format_request( $qvs ) {
     5155    if ( ! isset( $qvs['post_format'] ) )
     5156        return $qvs;
     5157    $slugs = get_post_format_slugs();
     5158    if ( isset( $slugs[ $qvs['post_format'] ] ) )
     5159        $qvs['post_format'] = 'post-format-' . $slugs[ $qvs['post_format'] ];
     5160    $tax = get_taxonomy( 'post_format' );
     5161    $qvs['post_type'] = $tax->object_type;
     5162    return $qvs;
     5163}
     5164add_filter( 'request', '_post_format_request' );
     5165
     5166/**
     5167 * Filters the post format term link to remove the format prefix.
     5168 *
     5169 * @access private
     5170 * @since 3.1.0
     5171 */
     5172function _post_format_link( $link, $term, $taxonomy ) {
     5173    global $wp_rewrite;
     5174    if ( 'post_format' != $taxonomy )
     5175        return $link;
     5176    if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) {
     5177        return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link );
     5178    } else {
     5179        $link = remove_query_arg( 'post_format', $link );
     5180        return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link );
     5181    }
     5182}
     5183add_filter( 'term_link', '_post_format_link', 10, 3 );
     5184
     5185/**
     5186 * Remove the post format prefix from the name property of the term object created by get_term().
     5187 *
     5188 * @access private
     5189 * @since 3.1.0
     5190 */
     5191function _post_format_get_term( $term ) {
     5192    if ( isset( $term->slug ) ) {
     5193        $term->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
     5194    }
     5195    return $term;
     5196}
     5197add_filter( 'get_post_format', '_post_format_get_term' );
     5198
     5199/**
     5200 * Remove the post format prefix from the name property of the term objects created by get_terms().
     5201 *
     5202 * @access private
     5203 * @since 3.1.0
     5204 */
     5205function _post_format_get_terms( $terms, $taxonomies, $args ) {
     5206    if ( in_array( 'post_format', (array) $taxonomies ) ) {
     5207        if ( isset( $args['fields'] ) && 'names' == $args['fields'] ) {
     5208            foreach( $terms as $order => $name ) {
     5209                $terms[$order] = get_post_format_string( str_replace( 'post-format-', '', $name ) );
     5210            }
     5211        } else {
     5212            foreach ( (array) $terms as $order => $term ) {
     5213                if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
     5214                    $terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
     5215                }
     5216            }
     5217        }
     5218    }
     5219    return $terms;
     5220}
     5221add_filter( 'get_terms', '_post_format_get_terms', 10, 3 );
     5222
     5223/**
     5224 * Remove the post format prefix from the name property of the term objects created by wp_get_object_terms().
     5225 *
     5226 * @access private
     5227 * @since 3.1.0
     5228 */
     5229function _post_format_wp_get_object_terms( $terms ) {
     5230    foreach ( (array) $terms as $order => $term ) {
     5231        if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
     5232            $terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
     5233        }
     5234    }
     5235    return $terms;
     5236}
     5237add_filter( 'wp_get_object_terms', '_post_format_wp_get_object_terms' );
     5238
     5239?>
Note: See TracChangeset for help on using the changeset viewer.