Make WordPress Core


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.0/wp-includes/post.php

    r17429 r16668  
    1414/**
    1515 * Creates the initial post types when 'init' action is fired.
    16  *
    17  * @since 2.9.0
    1816 */
    1917function create_initial_post_types() {
     
    2321        '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
    2422        'capability_type' => 'post',
    25         'map_meta_cap' => true,
    2623        'hierarchical' => false,
    2724        'rewrite' => false,
    2825        'query_var' => false,
    29         'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
     26        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions' ),
    3027    ) );
    3128
     
    3532        '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
    3633        'capability_type' => 'page',
    37         'map_meta_cap' => true,
    3834        'hierarchical' => true,
    3935        'rewrite' => false,
     
    5147        '_edit_link' => 'media.php?attachment_id=%d', /* internal use only. don't use this when registering your own post type. */
    5248        'capability_type' => 'post',
    53         'map_meta_cap' => true,
    5449        'hierarchical' => false,
    5550        'rewrite' => false,
    5651        'query_var' => false,
     52        'can_export' => false,
    5753        'show_in_nav_menus' => false,
    5854    ) );
     
    6763        '_edit_link' => 'revision.php?revision=%d', /* internal use only. don't use this when registering your own post type. */
    6864        'capability_type' => 'post',
    69         'map_meta_cap' => true,
    7065        'hierarchical' => false,
    7166        'rewrite' => false,
    7267        'query_var' => false,
    73         'can_export' => false,
    7468    ) );
    7569
     
    8175        'public' => false,
    8276        '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
     77        'capability_type' => 'post',
    8378        'hierarchical' => false,
    8479        'rewrite' => false,
     
    267262 * @return array|bool False on failure and the type will be determined by $output parameter.
    268263 */
    269 function get_children($args = '', $output = OBJECT) {
     264function &get_children($args = '', $output = OBJECT) {
    270265    $kids = array();
    271266    if ( empty( $args ) ) {
     
    296291
    297292    foreach ( $children as $key => $child )
    298         $kids[$child->ID] = $children[$key];
     293        $kids[$child->ID] =& $children[$key];
    299294
    300295    if ( $output == OBJECT ) {
     
    477472
    478473/**
    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  */
    487 function 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  */
    513 function 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  */
    526 function 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 /**
    544474 * Retrieve the post status based on the Post ID.
    545475 *
     
    620550 *
    621551 * label - A descriptive name for the post status marked for translation. Defaults to $post_status.
    622  * public - Whether posts of this status should be shown in the front end of the site. Defaults to true.
     552 * public - Whether posts of this status should be shown in the admin UI. Defaults to true.
    623553 * 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.
    629554 *
    630555 * @package WordPress
     
    647572    $args = (object) $args;
    648573
    649     $post_status = sanitize_key($post_status);
     574    $post_status = sanitize_user($post_status, true);
    650575    $args->name = $post_status;
    651576
     
    701626 * @see get_post_statuses
    702627 *
    703  * @param string $post_status The name of a registered post status
     628 * @param string $post_type The name of a registered post status
    704629 * @return object A post status object
    705630 */
     
    745670 * @see get_post_type_object
    746671 *
    747  * @param string $post_type Post type name
     672 * @param string $post Post type name
    748673 * @return bool Whether post type is hierarchical.
    749674 */
     
    762687 * @uses get_post_type_object()
    763688 *
    764  * @param string $post_type Post type name
     689 * @param string Post type name
    765690 * @return bool Whether post type is registered.
    766691 */
     
    841766 * Register a post type. Do not use before init.
    842767 *
    843  * A function for creating or modifying a post type based on the
     768 * A simple function for creating or modifying a post type based on the
    844769 * parameters given. The function will accept an array (second optional
    845770 * parameter), along with a string for the post type name.
     771 *
    846772 *
    847773 * Optional $args contents:
     
    850776 * - description - A short descriptive summary of what the post type is. Defaults to blank.
    851777 * - public - Whether posts of this type should be shown in the admin UI. Defaults to false.
    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.
     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.
    861781 * - menu_position - The position in the menu order the post type should appear. Defaults to the bottom.
    862782 * - menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon.
    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.
     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.
    869785 * - hierarchical - Whether the post type is hierarchical. Defaults to false.
    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()}.
     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.
    879790 * - permalink_epmask - The default rewrite endpoint bitmasks.
    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'.
     791 * - rewrite - false to prevent rewrite, or array('slug'=>$slug) to customize permastruct; default will use $post_type as slug.
    883792 * - query_var - false to prevent queries, or string to value of the query var to use for this post type
    884793 * - can_export - true allows this post type to be exported.
    885794 * - show_in_nav_menus - true makes this post type available for selection in navigation menus.
    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!
     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!
    888797 *
    889798 * @since 2.9.0
     
    892801 * @param string $post_type Name of the post type.
    893802 * @param array|string $args See above description.
    894  * @return object|WP_Error the registered post type object, or an error object
     803 * @return object the registered post type object
    895804 */
    896805function register_post_type($post_type, $args = array()) {
     
    903812    $defaults = array(
    904813        'labels' => array(), 'description' => '', 'publicly_queryable' => null, 'exclude_from_search' => 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,
     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,
    909816        'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null,
    910         'permalink_epmask' => EP_PERMALINK, 'can_export' => true, 'show_in_nav_menus' => null, 'show_in_menu' => null,
     817        'permalink_epmask' => EP_PERMALINK, 'can_export' => true, 'show_in_nav_menus' => null
    911818    );
    912819    $args = wp_parse_args($args, $defaults);
    913820    $args = (object) $args;
    914821
    915     $post_type = sanitize_key($post_type);
     822    $post_type = sanitize_user($post_type, true);
    916823    $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' ) );
    920824
    921825    // If not set, default to the setting for public.
     
    927831        $args->show_ui = $args->public;
    928832
    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 
    933833    // Whether to show this type in nav-menus.php.  Defaults to the setting for public.
    934834    if ( null === $args->show_in_nav_menus )
     
    939839        $args->exclude_from_search = !$args->public;
    940840
    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;
     841    if ( empty($args->capability_type) )
     842        $args->capability_type = 'post';
    947843
    948844    $args->cap = get_post_type_capabilities( $args );
    949845    unset($args->capabilities);
    950 
    951     if ( is_array( $args->capability_type ) )
    952         $args->capability_type = $args->capability_type[0];
    953846
    954847    if ( ! empty($args->supports) ) {
     
    968861
    969862    if ( false !== $args->rewrite && '' != get_option('permalink_structure') ) {
    970         if ( ! is_array( $args->rewrite ) )
     863        if ( !is_array($args->rewrite) )
    971864            $args->rewrite = array();
    972         if ( empty( $args->rewrite['slug'] ) )
     865        if ( !isset($args->rewrite['slug']) )
    973866            $args->rewrite['slug'] = $post_type;
    974         if ( ! isset( $args->rewrite['with_front'] ) )
     867        if ( !isset($args->rewrite['with_front']) )
    975868            $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 
    981869        if ( $args->hierarchical )
    982870            $wp_rewrite->add_rewrite_tag("%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
    983871        else
    984872            $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 
    998873        $wp_rewrite->add_permastruct($post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite['with_front'], $args->permalink_epmask);
    999874    }
     
    1019894 * Builds an object with all post type capabilities out of a post type object
    1020895 *
    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()
     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 *
    1066905 * @since 3.0.0
    1067  *
    1068  * @param object $args Post type registration arguments
     906 * @param object $args
    1069907 * @return object object with all the capabilities as member variables
    1070908 */
    1071909function get_post_type_capabilities( $args ) {
    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,
     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,
    1088918    );
    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  */
    1119 function _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     }
     919    $labels = array_merge( $defaults, $args->capabilities );
     920    return (object) $labels;
    1127921}
    1128922
     
    1143937 * - parent_item_colon - This string isn't used on non-hierarchical types. In hierarchical ones the default is Parent Page:
    1144938 *
    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).
     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.)
    1146940 *
    1147941 * @since 3.0.0
     
    1159953        'view_item' => array( __('View Post'), __('View Page') ),
    1160954        'search_items' => array( __('Search Posts'), __('Search Pages') ),
    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:') ),
     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:') )
    1164958    );
    1165     $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
    1166959    return _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
    1167960}
     
    1171964 *
    1172965 * @access private
    1173  * @since 3.0.0
    1174966 */
    1175967function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) {
     
    1181973        $object->labels['singular_name'] = $object->labels['name'];
    1182974
    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 
     975    $defaults = array_map( create_function( '$x', $object->hierarchical? 'return $x[1];' : 'return $x[0];' ), $nohier_vs_hier_defaults );
    1189976    $labels = array_merge( $defaults, $object->labels );
    1190977    return (object)$labels;
    1191978}
    1192 
    1193 /**
    1194  * Adds submenus for post types.
    1195  *
    1196  * @access private
    1197  * @since 3.1.0
    1198  */
    1199 function _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 }
    1208 add_action( 'admin_menu', '_add_post_type_submenus' );
    1209979
    1210980/**
     
    13421112    if ( empty( $r['post_status'] ) )
    13431113        $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish';
    1344     if ( ! empty($r['numberposts']) && empty($r['posts_per_page']) )
     1114    if ( ! empty($r['numberposts']) )
    13451115        $r['posts_per_page'] = $r['numberposts'];
    13461116    if ( ! empty($r['category']) )
     
    13531123        $r['post__not_in'] = wp_parse_id_list( $r['exclude'] );
    13541124
    1355     $r['ignore_sticky_posts'] = true;
    1356     $r['no_found_rows'] = true;
     1125    $r['caller_get_posts'] = true;
    13571126
    13581127    $get_posts = new WP_Query;
     
    13751144 *
    13761145 * @param int $post_id Post ID.
    1377  * @param string $meta_key Metadata name.
    1378  * @param mixed $meta_value Metadata value.
     1146 * @param string $key Metadata name.
     1147 * @param mixed $value Metadata value.
    13791148 * @param bool $unique Optional, default is false. Whether the same key should not be added.
    13801149 * @return bool False for failure. True for success.
     
    14371206 * If the meta field for the post does not exist, it will be added.
    14381207 *
    1439  * @since 1.5.0
     1208 * @since 1.5
    14401209 * @uses $wpdb
    14411210 * @link http://codex.wordpress.org/Function_Reference/update_post_meta
    14421211 *
    14431212 * @param int $post_id Post ID.
    1444  * @param string $meta_key Metadata key.
    1445  * @param mixed $meta_value Metadata value.
     1213 * @param string $key Metadata key.
     1214 * @param mixed $value Metadata value.
    14461215 * @param mixed $prev_value Optional. Previous value to check before removing.
    14471216 * @return bool False on failure, true if success.
     
    14981267 * @return array
    14991268 */
    1500 function 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' );
     1269function 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');
    15101281}
    15111282
     
    15641335 * @return bool Whether post is sticky.
    15651336 */
    1566 function 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 ) )
     1337function 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) )
    15751348        return false;
    15761349
    1577     if ( in_array( $post_id, $stickies ) )
     1350    if ( in_array($post_id, $stickies) )
    15781351        return true;
    15791352
     
    16241397 *
    16251398 * @since 2.3.0
    1626  * @uses apply_filters() Calls 'edit_$field' and '{$field_no_prefix}_edit_pre' passing $value and
     1399 * @uses apply_filters() Calls 'edit_$field' and '${field_no_prefix}_edit_pre' passing $value and
    16271400 *  $post_id if $context == 'edit' and field name prefix == 'post_'.
    16281401 *
    16291402 * @uses apply_filters() Calls 'edit_post_$field' passing $value and $post_id if $context == 'db'.
    16301403 * @uses apply_filters() Calls 'pre_$field' 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_'.
     1404 * @uses apply_filters() Calls '${field}_pre' passing $value if $context == 'db' and field name prefix != 'post_'.
    16321405 *
    16331406 * @uses apply_filters() Calls '$field' passing $value, $post_id and $context if $context == anything
     
    16681441
    16691442        if ( $prefixed ) {
    1670             $value = apply_filters("edit_{$field}", $value, $post_id);
     1443            $value = apply_filters("edit_$field", $value, $post_id);
    16711444            // Old school
    1672             $value = apply_filters("{$field_no_prefix}_edit_pre", $value, $post_id);
     1445            $value = apply_filters("${field_no_prefix}_edit_pre", $value, $post_id);
    16731446        } else {
    1674             $value = apply_filters("edit_post_{$field}", $value, $post_id);
     1447            $value = apply_filters("edit_post_$field", $value, $post_id);
    16751448        }
    16761449
     
    16851458    } else if ( 'db' == $context ) {
    16861459        if ( $prefixed ) {
    1687             $value = apply_filters("pre_{$field}", $value);
    1688             $value = apply_filters("{$field_no_prefix}_save_pre", $value);
     1460            $value = apply_filters("pre_$field", $value);
     1461            $value = apply_filters("${field_no_prefix}_save_pre", $value);
    16891462        } else {
    1690             $value = apply_filters("pre_post_{$field}", $value);
    1691             $value = apply_filters("{$field}_pre", $value);
     1463            $value = apply_filters("pre_post_$field", $value);
     1464            $value = apply_filters("${field}_pre", $value);
    16921465        }
    16931466    } else {
     
    16961469            $value = apply_filters($field, $value, $post_id, $context);
    16971470        else
    1698             $value = apply_filters("post_{$field}", $value, $post_id, $context);
     1471            $value = apply_filters("post_$field", $value, $post_id, $context);
    16991472    }
    17001473
     
    18811654 * @since 2.5.0
    18821655 *
    1883  * @param string|array $post_mime_types List of mime types or comma separated string of mime types.
     1656 * @param string|array $mime_types List of mime types or comma separated string of mime types.
    18841657 * @param string $table_alias Optional. Specify a table alias, if needed.
    18851658 * @return string The SQL AND clause for mime searching.
     
    20401813 * @uses wp_delete_post() if trash is disabled
    20411814 *
    2042  * @param int $post_id Post ID.
     1815 * @param int $postid Post ID.
    20431816 * @return mixed False on failure
    20441817 */
     
    20751848 * @uses do_action() on 'untrashed_post' after undeletion
    20761849 *
    2077  * @param int $post_id Post ID.
     1850 * @param int $postid Post ID.
    20781851 * @return mixed False on failure
    20791852 */
     
    22682041 *
    22692042 * @since 1.0.0
    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  */
    2278 function 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 
     2043 * @uses $wpdb
     2044 *
     2045 * @param int $num Optional, default is 10. Number of posts to get.
     2046 * @return array List of posts.
     2047 */
     2048function 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();
    23092061}
    23102062
     
    23262078    $post = get_post($postid, $mode);
    23272079
    2328     if (
     2080    if ( 
    23292081        ( OBJECT == $mode && empty( $post->ID ) ) ||
    23302082        ( OBJECT != $mode && empty( $post['ID'] ) )
     
    23772129 *
    23782130 * @since 1.0.0
     2131 * @link http://core.trac.wordpress.org/ticket/9084 Bug report on 'wp_insert_post_data' filter.
    23792132 * @uses $wpdb
    23802133 * @uses $wp_rewrite
    23812134 * @uses $user_ID
     2135 *
    23822136 * @uses do_action() Calls 'pre_post_update' on post ID if this is an update.
    23832137 * @uses do_action() Calls 'edit_post' action on post ID and post data if this is an update.
    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.
     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.
    23862143 * @uses wp_transition_post_status()
    23872144 *
    2388  * @param array $postarr Elements that make up post to insert.
     2145 * @param array $postarr Optional. Overrides defaults.
    23892146 * @param bool $wp_error Optional. Allow return of WP_Error on failure.
    23902147 * @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success.
    23912148 */
    2392 function wp_insert_post($postarr, $wp_error = false) {
     2149function wp_insert_post($postarr = array(), $wp_error = false) {
    23932150    global $wpdb, $wp_rewrite, $user_ID;
    23942151
     
    25172274        $post_parent = 0;
    25182275
    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 );
     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    }
    25212287
    25222288    if ( isset($menu_order) )
     
    27532519 * Computes a unique slug for the post, when given the desired slug and some post details.
    27542520 *
    2755  * @since 2.8.0
    2756  *
    27572521 * @global wpdb $wpdb
    27582522 * @global WP_Rewrite $wp_rewrite
     
    27802544        $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) );
    27812545
    2782         if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) {
     2546        if ( $post_name_check || in_array( $slug, $feeds ) ) {
    27832547            $suffix = 2;
    27842548            do {
     
    27952559        $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID, $post_parent ) );
    27962560
    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 ) ) {
     2561        if ( $post_name_check || in_array( $slug, $feeds ) || preg_match( '@^(page)?\d+$@', $slug ) ) {
    27982562            $suffix = 2;
    27992563            do {
     
    28092573        $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) );
    28102574
    2811         if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) {
     2575        if ( $post_name_check || in_array( $slug, $feeds ) ) {
    28122576            $suffix = 2;
    28132577            do {
     
    28502614 * @param string $tags The tags to set for the post, separated by commas.
    28512615 * @param bool $append If true, don't delete existing tags, just add on. If false, replace the tags with the new tags.
    2852  * @return mixed Array of affected term IDs. WP_Error or false on failure.
     2616 * @return bool|null Will return false if $post_id is not an integer or is 0. Will return null otherwise
    28532617 */
    28542618function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) {
     
    28652629 * @param string $tags The tags to set for the post, separated by commas.
    28662630 * @param bool $append If true, don't delete existing tags, just add on. If false, replace the tags with the new tags.
    2867  * @return mixed Array of affected term IDs. WP_Error or false on failure.
     2631 * @return bool|null Will return false if $post_id is not an integer or is 0. Will return null otherwise
    28682632 */
    28692633function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false ) {
     
    28852649    }
    28862650
    2887     return wp_set_object_terms($post_id, $tags, $taxonomy, $append);
     2651    wp_set_object_terms($post_id, $tags, $taxonomy, $append);
    28882652}
    28892653
     
    29412705 * @uses do_action() Calls 'transition_post_status' on $new_status, $old_status and
    29422706 *  $post if there is a status change.
    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.
     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.
    29452709 *
    29462710 * @param string $new_status Transition to this post status.
     
    29502714function wp_transition_post_status($new_status, $old_status, $post) {
    29512715    do_action('transition_post_status', $new_status, $old_status, $post);
    2952     do_action("{$old_status}_to_{$new_status}", $post);
    2953     do_action("{$new_status}_{$post->post_type}", $post->ID, $post);
     2716    do_action("${old_status}_to_$new_status", $post);
     2717    do_action("${new_status}_$post->post_type", $post->ID, $post);
    29542718}
    29552719
     
    30042768        }
    30052769    }
    3006     $pung = apply_filters('get_enclosed', $pung, $post_id);
     2770    $pung = apply_filters('get_enclosed', $pung);
    30072771    return $pung;
    30082772}
     
    31262890function get_page_by_path($page_path, $output = OBJECT, $post_type = 'page') {
    31272891    global $wpdb;
    3128     $null = null;
    31292892    $page_path = rawurlencode(urldecode($page_path));
    31302893    $page_path = str_replace('%2F', '/', $page_path);
     
    31402903
    31412904    if ( empty($pages) )
    3142         return $null;
     2905        return null;
    31432906
    31442907    foreach ( $pages as $page ) {
     
    31462909        $curpage = $page;
    31472910        while ( $curpage->post_parent != 0 ) {
    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 ) );
     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 ));
    31522912            $path = '/' . $curpage->post_name . $path;
    31532913        }
     
    31572917    }
    31582918
    3159     return $null;
     2919    return null;
    31602920}
    31612921
     
    32132973 * @since 2.0.0
    32142974 *
    3215  * @param array $pages Posts array.
    3216  * @param int $page_id Parent page ID.
     2975 * @param array $posts Posts array.
     2976 * @param int $parent Parent page ID.
    32172977 * @return array A list arranged by hierarchy. Children immediately follow their parents.
    32182978 */
    32192979function &get_page_hierarchy( &$pages, $page_id = 0 ) {
     2980
    32202981    if ( empty( $pages ) ) {
    32212982        $result = array();
     
    32252986    $children = array();
    32262987    foreach ( (array) $pages as $p ) {
     2988
    32272989        $parent_id = intval( $p->post_parent );
    32282990        $children[ $parent_id ][] = $p;
    3229     }
    3230 
    3231     $result = array();
    3232     _page_traverse_name( $page_id, $children, $result );
     2991     }
     2992
     2993     $result = array();
     2994     _page_traverse_name( $page_id, $children, $result );
    32332995
    32342996    return $result;
     
    32393001 * $children contains parent-chilren relations
    32403002 *
    3241  * @since 2.9.0
    32423003 */
    32433004function _page_traverse_name( $page_id, &$children, &$result ){
     3005
    32443006    if ( isset( $children[ $page_id ] ) ){
     3007
    32453008        foreach( (array)$children[ $page_id ] as $child ) {
     3009
    32463010            $result[ $child->ID ] = $child->post_name;
    32473011            _page_traverse_name( $child->ID, $children, $result );
     
    35223286 * @param string|array $object Arguments to override defaults.
    35233287 * @param string $file Optional filename.
    3524  * @param int $parent Parent post ID.
     3288 * @param int $post_parent Parent post ID.
    35253289 * @return int Attachment ID.
    35263290 */
     
    36723436 * @uses do_action() Calls 'delete_attachment' hook on Attachment ID.
    36733437 *
    3674  * @param int $post_id Attachment ID.
     3438 * @param int $postid Attachment ID.
    36753439 * @param bool $force_delete Whether to bypass trash and force deletion. Defaults to false.
    36763440 * @return mixed False on failure. Post data on success.
     
    37483512            $del_file = path_join( dirname($meta['file']), $size['file'] );
    37493513            $del_file = apply_filters('wp_delete_file', $del_file);
    3750             @ unlink( path_join($uploadpath['basedir'], $del_file) );
     3514            @ unlink( path_join($uploadpath['basedir'], $del_file) );
    37513515        }
    37523516    }
     
    38213585            if ( 0 === strpos($file, $uploads['basedir']) ) //Check that the upload base exists in the file location
    38223586                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); //replace file location with url location
    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.
     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.
    38273591        }
    38283592    }
     
    38313595        $url = get_the_guid( $post->ID );
    38323596
    3833     $url = apply_filters( 'wp_get_attachment_url', $url, $post->ID );
    3834 
    3835     if ( 'attachment' != $post->post_type || empty( $url ) )
     3597    if ( 'attachment' != $post->post_type || empty($url) )
    38363598        return false;
    38373599
    3838     return $url;
     3600    return apply_filters( 'wp_get_attachment_url', $url, $post->ID );
    38393601}
    38403602
     
    40003762
    40013763/**
    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.
     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.
    40063769 *
    40073770 * If the slug was changed and not already part of the old slugs then it will be
     
    40093772 * post.
    40103773 *
    4011  * The most logically usage of this function is redirecting changed post objects, so
     3774 * The most logically usage of this function is redirecting changed posts, so
    40123775 * that those that linked to an changed post will be redirected to the new post.
    40133776 *
     
    40153778 *
    40163779 * @param int $post_id Post ID.
    4017  * @param object $post The Post Object
    4018  * @param object $post_before The Previous Post Object
    40193780 * @return int Same as $post_id
    40203781 */
     
    40243785        return;
    40253786
    4026     // we're only concerned with published, non-hierarchical objects
    4027     if ( $post->post_status != 'publish' || is_post_type_hierarchical( $post->post_type ) )
     3787    // we're only concerned with published posts
     3788    if ( $post->post_status != 'publish' || $post->post_type != 'post' )
    40283789        return;
    40293790
     
    40313792
    40323793    // if we haven't added this old slug before, add it now
    4033     if ( !empty( $post_before->post_name ) && !in_array($post_before->post_name, $old_slugs) )
     3794    if ( !in_array($post_before->post_name, $old_slugs) )
    40343795        add_post_meta($post_id, '_wp_old_slug', $post_before->post_name);
    40353796
     
    41343895 * @since 0.71
    41353896 *
     3897 * @uses $wpdb
     3898 * @uses $blog_id
    41363899 * @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
    41373903 *
    41383904 * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'.
     
    41403906 */
    41413907function get_lastpostdate($timezone = 'server') {
    4142     return apply_filters( 'get_lastpostdate', _get_last_post_time( $timezone, 'date' ), $timezone );
     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 );
    41433927}
    41443928
     
    41513935 *
    41523936 * @since 1.2.0
     3937 * @uses $wpdb
     3938 * @uses $blog_id
    41533939 * @uses apply_filters() Calls 'get_lastpostmodified' filter
    41543940 *
     
    41573943 */
    41583944function get_lastpostmodified($timezone = 'server') {
    4159     $lastpostmodified = _get_last_post_time( $timezone, 'modified' );
     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    }
    41603965
    41613966    $lastpostdate = get_lastpostdate($timezone);
     
    41633968        $lastpostmodified = $lastpostdate;
    41643969
     3970    if ( $lastpostmodified )
     3971        wp_cache_set( "lastpostmodified:$timezone", $lastpostmodified, 'timeinfo' );
     3972
    41653973    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  */
    4178 function _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;
    42143974}
    42153975
     
    42604020    $id = (int) $id;
    42614021
    4262     if ( 0 === $id )
    4263         return;
    4264 
    42654022    wp_cache_delete($id, 'posts');
    42664023    wp_cache_delete($id, 'post_meta');
     
    42734030
    42744031    if ( $children = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_parent = %d", $id) ) ) {
    4275         foreach ( $children as $cid ) {
    4276             // Loop detection
    4277             if ( $cid == $id )
    4278                 continue;
     4032        foreach( $children as $cid )
    42794033            clean_post_cache( $cid );
    4280         }
    42814034    }
    42824035
     
    43544107        $post_type = 'post';
    43554108
    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     }
     4109    if ( !is_array($post_type) && 'any' != $post_type && $update_term_cache )
     4110        update_object_term_cache($post_ids, $post_type);
    43714111
    43724112    if ( $update_meta_cache )
     
    44584198    // If published posts changed clear the lastpostmodified cache
    44594199    if ( 'publish' == $new_status || 'publish' == $old_status) {
    4460         foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) {
    4461             wp_cache_delete( "lastpostmodified:$timezone", 'timeinfo' );
    4462             wp_cache_delete( "lastpostdate:$timezone", 'timeinfo' );
    4463         }
     4200        wp_cache_delete( 'lastpostmodified:server', 'timeinfo' );
     4201        wp_cache_delete( 'lastpostmodified:gmt',    'timeinfo' );
     4202        wp_cache_delete( 'lastpostmodified:blog',   'timeinfo' );
    44644203    }
    44654204
     
    45644303 * parent will be an ancestor. There will only be two ancestors at the most.
    45654304 *
    4566  * @since 2.5.0
     4305 * @since unknown
    45674306 * @access private
    45684307 * @uses $wpdb
     
    45844323    $id = $_post->ancestors[] = $_post->post_parent;
    45854324    while ( $ancestor = $wpdb->get_var( $wpdb->prepare("SELECT `post_parent` FROM $wpdb->posts WHERE ID = %d LIMIT 1", $id) ) ) {
    4586         // Loop detection: If the ancestor has been seen before, break.
    4587         if ( ( $ancestor == $_post->ID ) || in_array($ancestor,  $_post->ancestors) )
     4325        if ( $id == $ancestor )
    45884326            break;
    45894327        $id = $_post->ancestors[] = $ancestor;
     
    49134651 *
    49144652 * @param int|object $revision_id Revision ID or revision object.
    4915  * @return mixed Null or WP_Error if error, deleted post if success.
     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.
    49164655 */
    49174656function wp_delete_post_revision( $revision_id ) {
     
    49864725    }
    49874726}
    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  */
    4998 function 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  */
    5019 function 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  */
    5055 function 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  */
    5078 function 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  */
    5103 function 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  */
    5120 function 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  */
    5141 function 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  */
    5154 function _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 }
    5164 add_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  */
    5172 function _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 }
    5183 add_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  */
    5191 function _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 }
    5197 add_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  */
    5205 function _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 }
    5221 add_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  */
    5229 function _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 }
    5237 add_filter( 'wp_get_object_terms', '_post_format_wp_get_object_terms' );
    5238 
    5239 ?>
Note: See TracChangeset for help on using the changeset viewer.