X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/a6f44f0edcda2471c5a33e4156c1c9488c7f3210..refs/tags/wordpress-3.7:/wp-includes/post.php diff --git a/wp-includes/post.php b/wp-includes/post.php index c8888c6e..39d71a37 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -167,8 +167,9 @@ add_action( 'init', 'create_initial_post_types', 0 ); // highest priority /** * Retrieve attached file path based on attachment ID. * - * You can optionally send it through the 'get_attached_file' filter, but by - * default it will just return the file path unfiltered. + * By default the path will go through the 'get_attached_file' filter, but + * passing a true to the $unfiltered argument of get_attached_file() will + * return the file path unfiltered. * * The function works by getting the single post meta name, named * '_wp_attached_file' and returning it. This is a convenience function to @@ -176,11 +177,10 @@ add_action( 'init', 'create_initial_post_types', 0 ); // highest priority * attached filename through a filter. * * @since 2.0.0 - * @uses apply_filters() Calls 'get_attached_file' on file path and attachment ID. * * @param int $attachment_id Attachment ID. * @param bool $unfiltered Whether to apply filters. - * @return string|bool The file path to the attached file, or false if the attachment does not exist. + * @return string|bool The file path to where the attached file should be, false otherwise. */ function get_attached_file( $attachment_id, $unfiltered = false ) { $file = get_post_meta( $attachment_id, '_wp_attached_file', true ); @@ -310,9 +310,12 @@ function get_children($args = '', $output = OBJECT) { $children = get_posts( $r ); - if ( !$children ) + if ( ! $children ) return $kids; + if ( ! empty( $r['fields'] ) ) + return $children; + update_post_cache($children); foreach ( $children as $key => $child ) @@ -375,7 +378,6 @@ function get_extended($post) { * $post, must be given as a variable, since it is passed by reference. * * @since 1.5.1 - * @uses $wpdb * @link http://codex.wordpress.org/Function_Reference/get_post * * @param int|object $post Post ID or post object. Optional, default is the current post from the loop. @@ -424,159 +426,179 @@ function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) { final class WP_Post { /** + * Post ID. * * @var int */ public $ID; /** + * ID of post author. * - * @var int + * A numeric string, for compatibility reasons. + * + * @var string */ public $post_author = 0; /** + * The post's local publication time. * * @var string */ public $post_date = '0000-00-00 00:00:00'; /** + * The post's GMT publication time. * * @var string */ public $post_date_gmt = '0000-00-00 00:00:00'; /** + * The post's content. * * @var string */ public $post_content = ''; /** + * The post's title. * * @var string */ public $post_title = ''; /** + * The post's excerpt. * * @var string */ public $post_excerpt = ''; /** + * The post's status. * * @var string */ public $post_status = 'publish'; /** + * Whether comments are allowed. * * @var string */ public $comment_status = 'open'; /** + * Whether pings are allowed. * * @var string */ public $ping_status = 'open'; /** + * The post's password in plain text. * * @var string */ public $post_password = ''; /** + * The post's slug. * * @var string */ public $post_name = ''; /** + * URLs queued to be pinged. * * @var string */ public $to_ping = ''; /** + * URLs that have been pinged. * * @var string */ public $pinged = ''; /** + * The post's local modified time. * * @var string */ public $post_modified = '0000-00-00 00:00:00'; /** + * The post's GMT modified time. * * @var string */ public $post_modified_gmt = '0000-00-00 00:00:00'; /** + * A utility DB field for post content. + * * * @var string */ public $post_content_filtered = ''; /** + * ID of a post's parent post. * * @var int */ public $post_parent = 0; /** + * The unique identifier for a post, not necessarily a URL, used as the feed GUID. * * @var string */ public $guid = ''; /** + * A field used for ordering posts. * * @var int */ public $menu_order = 0; /** + * The post's type, like post or page. * * @var string */ public $post_type = 'post'; /** + * An attachment's mime type. * * @var string */ public $post_mime_type = ''; /** + * Cached comment count. * - * @var int - */ - public $comment_count = 0; - - /** + * A numeric string, for compatibility reasons. * * @var string */ - public $filter; + public $comment_count = 0; /** - * Private variable used by post formats to cache parsed content. + * Stores the post object's sanitization level. * - * @since 3.6.0 + * Does not correspond to a DB field. * - * @var array - * @access private + * @var string */ - public $format_content; - + public $filter; public static function get_instance( $post_id ) { global $wpdb; @@ -1082,6 +1104,7 @@ function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) * * Defaults to false. * * While the default settings of exclude_from_search, publicly_queryable, show_ui, and show_in_nav_menus are * inherited from public, each does not rely on this relationship and controls a very specific intention. + * - hierarchical - Whether the post type is hierarchical (e.g. page). Defaults to false. * - exclude_from_search - Whether to exclude posts with this post type from front end search results. * * If not set, the opposite of public's current value is used. * - publicly_queryable - Whether queries can be performed on the front end for the post type as part of parse_request(). @@ -1091,8 +1114,6 @@ function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) * * If not set, the default is inherited from public. * - show_ui - Whether to generate a default UI for managing this post type in the admin. * * If not set, the default is inherited from public. - * - show_in_nav_menus - Makes this post type available for selection in navigation menus. - * * If not set, the default is inherited from public. * - show_in_menu - Where to show the post type in the admin menu. * * If true, the post type is shown in its own top level menu. * * If false, no menu is shown @@ -1100,6 +1121,8 @@ function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) * be placed as a sub menu of that. * * show_ui must be true. * * If not set, the default is inherited from show_ui + * - show_in_nav_menus - Makes this post type available for selection in navigation menus. + * * If not set, the default is inherited from public. * - show_in_admin_bar - Makes this post type available via the admin bar. * * If not set, the default is inherited from show_in_menu * - menu_position - The position in the menu order the post type should appear. @@ -1113,11 +1136,10 @@ function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) * * By default the capability_type is used as a base to construct capabilities. * * You can see accepted values in {@link get_post_type_capabilities()}. * - map_meta_cap - Whether to use the internal default meta capability handling. Defaults to false. - * - hierarchical - Whether the post type is hierarchical (e.g. page). Defaults to false. * - supports - An alias for calling add_post_type_support() directly. Defaults to title and editor. * * See {@link add_post_type_support()} for documentation. - * - 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. + * - register_meta_box_cb - Provide a callback function that sets up the meta boxes + * for the edit form. Do remove_meta_box() and add_meta_box() calls in the callback. * - 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(). @@ -1146,37 +1168,55 @@ function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) * * @since 2.9.0 * @uses $wp_post_types Inserts new post type object into the list + * @uses $wp_rewrite Gets default feeds + * @uses $wp Adds query vars * - * @param string $post_type Post type key, must not exceed 20 characters + * @param string $post_type Post type key, must not exceed 20 characters. * @param array|string $args See optional args description above. - * @return object|WP_Error the registered post type object, or an error object + * @return object|WP_Error the registered post type object, or an error object. */ function register_post_type( $post_type, $args = array() ) { global $wp_post_types, $wp_rewrite, $wp; - if ( !is_array($wp_post_types) ) + if ( ! is_array( $wp_post_types ) ) $wp_post_types = array(); // Args prefixed with an underscore are reserved for internal use. $defaults = array( - 'labels' => array(), 'description' => '', 'publicly_queryable' => null, 'exclude_from_search' => null, - 'capability_type' => 'post', 'capabilities' => array(), 'map_meta_cap' => null, - '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'hierarchical' => false, - 'public' => false, 'rewrite' => true, 'has_archive' => false, 'query_var' => true, - 'supports' => array(), 'register_meta_box_cb' => null, - 'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null, - 'can_export' => true, - 'show_in_nav_menus' => null, 'show_in_menu' => null, 'show_in_admin_bar' => null, - 'delete_with_user' => null, + 'labels' => array(), + 'description' => '', + 'public' => false, + 'hierarchical' => false, + 'exclude_from_search' => null, + 'publicly_queryable' => null, + 'show_ui' => null, + 'show_in_menu' => null, + 'show_in_nav_menus' => null, + 'show_in_admin_bar' => null, + 'menu_position' => null, + 'menu_icon' => null, + 'capability_type' => 'post', + 'capabilities' => array(), + 'map_meta_cap' => null, + 'supports' => array(), + 'register_meta_box_cb' => null, + 'taxonomies' => array(), + 'has_archive' => false, + 'rewrite' => true, + 'query_var' => true, + 'can_export' => true, + 'delete_with_user' => null, + '_builtin' => false, + '_edit_link' => 'post.php?post=%d', ); - $args = wp_parse_args($args, $defaults); + $args = wp_parse_args( $args, $defaults ); $args = (object) $args; - $post_type = sanitize_key($post_type); + $post_type = sanitize_key( $post_type ); $args->name = $post_type; if ( strlen( $post_type ) > 20 ) - return new WP_Error( 'post_type_too_long', __( 'Post types cannot exceed 20 characters in length' ) ); + return new WP_Error( 'post_type_too_long', __( 'Post types cannot exceed 20 characters in length' ) ); // If not set, default to the setting for public. if ( null === $args->publicly_queryable ) @@ -1194,7 +1234,7 @@ function register_post_type( $post_type, $args = array() ) { if ( null === $args->show_in_admin_bar ) $args->show_in_admin_bar = true === $args->show_in_menu; - // Whether to show this type in nav-menus.php. Defaults to the setting for public. + // If not set, default to the setting for public. if ( null === $args->show_in_nav_menus ) $args->show_in_nav_menus = $args->public; @@ -1206,32 +1246,33 @@ function register_post_type( $post_type, $args = array() ) { if ( empty( $args->capabilities ) && null === $args->map_meta_cap && in_array( $args->capability_type, array( 'post', 'page' ) ) ) $args->map_meta_cap = true; + // If not set, default to false. if ( null === $args->map_meta_cap ) $args->map_meta_cap = false; $args->cap = get_post_type_capabilities( $args ); - unset($args->capabilities); + unset( $args->capabilities ); if ( is_array( $args->capability_type ) ) $args->capability_type = $args->capability_type[0]; - if ( ! empty($args->supports) ) { - add_post_type_support($post_type, $args->supports); - unset($args->supports); + if ( ! empty( $args->supports ) ) { + add_post_type_support( $post_type, $args->supports ); + unset( $args->supports ); } elseif ( false !== $args->supports ) { // Add default features - add_post_type_support($post_type, array('title', 'editor')); + add_post_type_support( $post_type, array( 'title', 'editor' ) ); } - if ( false !== $args->query_var && !empty($wp) ) { + if ( false !== $args->query_var && ! empty( $wp ) ) { if ( true === $args->query_var ) $args->query_var = $post_type; else - $args->query_var = sanitize_title_with_dashes($args->query_var); - $wp->add_query_var($args->query_var); + $args->query_var = sanitize_title_with_dashes( $args->query_var ); + $wp->add_query_var( $args->query_var ); } - if ( false !== $args->rewrite && ( is_admin() || '' != get_option('permalink_structure') ) ) { + if ( false !== $args->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) { if ( ! is_array( $args->rewrite ) ) $args->rewrite = array(); if ( empty( $args->rewrite['slug'] ) ) @@ -1250,9 +1291,9 @@ function register_post_type( $post_type, $args = array() ) { } if ( $args->hierarchical ) - add_rewrite_tag("%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name="); + add_rewrite_tag( "%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&pagename=" ); else - add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name="); + add_rewrite_tag( "%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=" ); if ( $args->has_archive ) { $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive; @@ -1271,16 +1312,18 @@ function register_post_type( $post_type, $args = array() ) { add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&paged=$matches[1]', 'top' ); } - add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite ); + $permastruct_args = $args->rewrite; + $permastruct_args['feed'] = $permastruct_args['feeds']; + add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $permastruct_args ); } if ( $args->register_meta_box_cb ) - add_action('add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1); + add_action( 'add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1 ); $args->labels = get_post_type_labels( $args ); $args->label = $args->labels->name; - $wp_post_types[$post_type] = $args; + $wp_post_types[ $post_type ] = $args; add_action( 'future_' . $post_type, '_future_post_hook', 5, 2 ); @@ -1429,6 +1472,8 @@ function _post_type_meta_capabilities( $capabilities = null ) { * Above, the first default value is for non-hierarchical post types (like posts) and the second one is for hierarchical post types (like pages). * * @since 3.0.0 + * @access private + * * @param object $post_type_object * @return object object with all the labels as member variables */ @@ -1462,6 +1507,7 @@ function get_post_type_labels( $post_type_object ) { * @since 3.0.0 */ function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) { + $object->labels = (array) $object->labels; if ( isset( $object->label ) && empty( $object->labels['name'] ) ) $object->labels['name'] = $object->label; @@ -1617,7 +1663,6 @@ function set_post_type( $post_id = 0, $post_type = 'post' ) { * 'post_status' - Default is 'publish'. Post status to retrieve. * * @since 1.2.0 - * @uses $wpdb * @uses WP_Query::query() See for more default arguments and information. * @link http://codex.wordpress.org/Template_Tags/get_posts * @@ -1666,12 +1711,11 @@ function get_posts($args = null) { * Post meta data is called "Custom Fields" on the Administration Screen. * * @since 1.5.0 - * @uses $wpdb * @link http://codex.wordpress.org/Function_Reference/add_post_meta * * @param int $post_id Post ID. * @param string $meta_key Metadata name. - * @param mixed $meta_value Metadata value. + * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. * @param bool $unique Optional, default is false. Whether the same key should not be added. * @return int|bool Meta ID on success, false on failure. */ @@ -1691,12 +1735,11 @@ function add_post_meta($post_id, $meta_key, $meta_value, $unique = false) { * allows removing all metadata matching key, if needed. * * @since 1.5.0 - * @uses $wpdb * @link http://codex.wordpress.org/Function_Reference/delete_post_meta * * @param int $post_id post ID * @param string $meta_key Metadata name. - * @param mixed $meta_value Optional. Metadata value. + * @param mixed $meta_value Optional. Metadata value. Must be serializable if non-scalar. * @return bool True on success, false on failure. */ function delete_post_meta($post_id, $meta_key, $meta_value = '') { @@ -1711,7 +1754,6 @@ function delete_post_meta($post_id, $meta_key, $meta_value = '') { * Retrieve post meta field for a post. * * @since 1.5.0 - * @uses $wpdb * @link http://codex.wordpress.org/Function_Reference/get_post_meta * * @param int $post_id Post ID. @@ -1733,12 +1775,11 @@ function get_post_meta($post_id, $key = '', $single = false) { * If the meta field for the post does not exist, it will be added. * * @since 1.5.0 - * @uses $wpdb * @link http://codex.wordpress.org/Function_Reference/update_post_meta * * @param int $post_id Post ID. * @param string $meta_key Metadata key. - * @param mixed $meta_value Metadata value. + * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. * @param mixed $prev_value Optional. Previous value to check before removing. * @return bool True on success, false on failure. */ @@ -1754,7 +1795,6 @@ function update_post_meta($post_id, $meta_key, $meta_value, $prev_value = '') { * Delete everything from post meta matching meta key. * * @since 2.3.0 - * @uses $wpdb * * @param string $post_meta_key Key to search for when deleting. * @return bool Whether the post meta key was deleted from the database @@ -2029,7 +2069,7 @@ function unstick_post($post_id) { } /** - * Count number of posts of a post type and is user has permissions to view. + * Count number of posts of a post type and if user has permissions to view. * * This function provides an efficient method of finding the amount of post's * type a blog has. Another method is to count the amount of items in @@ -2039,9 +2079,10 @@ function unstick_post($post_id) { * The $perm parameter checks for 'readable' value and if the user can read * private posts, it will display that for the user that is signed in. * - * @since 2.5.0 * @link http://codex.wordpress.org/Template_Tags/wp_count_posts * + * @since 2.5.0 + * * @param string $type Optional. Post type to retrieve count * @param string $perm Optional. 'readable' or empty. * @return object Number of posts for each status @@ -2049,9 +2090,12 @@ function unstick_post($post_id) { function wp_count_posts( $type = 'post', $perm = '' ) { global $wpdb; + if ( ! post_type_exists( $type ) ) + return new stdClass; + $user = wp_get_current_user(); - $cache_key = $type; + $cache_key = 'posts-' . $type; $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s"; if ( 'readable' == $perm && is_user_logged_in() ) { @@ -2063,23 +2107,28 @@ function wp_count_posts( $type = 'post', $perm = '' ) { } $query .= ' GROUP BY post_status'; - $count = wp_cache_get($cache_key, 'counts'); - if ( false !== $count ) - return $count; - - $count = $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A ); - - $stats = array(); - foreach ( get_post_stati() as $state ) - $stats[$state] = 0; + $counts = wp_cache_get( $cache_key, 'counts' ); + if ( false === $counts ) { + $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A ); + $counts = array_fill_keys( get_post_stati(), 0 ); - foreach ( (array) $count as $row ) - $stats[$row['post_status']] = $row['num_posts']; + foreach ( $results as $row ) + $counts[ $row['post_status'] ] = $row['num_posts']; - $stats = (object) $stats; - wp_cache_set($cache_key, $stats, 'counts'); + $counts = (object) $counts; + wp_cache_set( $cache_key, $counts, 'counts' ); + } - return $stats; + /** + * Modify returned post counts by status for the current post type. + * + * @since 3.7.0 + * + * @param object $counts An object containing the current post_type's post counts by status. + * @param string $type The post type. + * @param string $perm The permission to determine if the posts are 'readable' by the current user. + */ + return apply_filters( 'wp_count_posts', $counts, $type, $perm ); } /** @@ -2101,13 +2150,21 @@ function wp_count_attachments( $mime_type = '' ) { $and = wp_post_mime_type_where( $mime_type ); $count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' $and GROUP BY post_mime_type", ARRAY_A ); - $stats = array(); + $counts = array(); foreach( (array) $count as $row ) { - $stats[$row['post_mime_type']] = $row['num_posts']; + $counts[ $row['post_mime_type'] ] = $row['num_posts']; } - $stats['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and"); + $counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and"); - return (object) $stats; + /** + * Modify returned attachment counts by mime type. + * + * @since 3.7.0 + * + * @param object $counts An object containing the attachment counts by mime type. + * @param string $mime_type The mime type pattern used to filter the attachments counted. + */ + return apply_filters( 'wp_count_attachments', (object) $counts, $mime_type ); } /** @@ -2260,20 +2317,6 @@ function wp_delete_post( $postid = 0, $force_delete = false ) { $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => $post->post_type ) ); } - if ( 'page' == $post->post_type) { - // if the page is defined in option page_on_front or post_for_posts, - // adjust the corresponding options - if ( get_option('page_on_front') == $postid ) { - update_option('show_on_front', 'posts'); - delete_option('page_on_front'); - } - if ( get_option('page_for_posts') == $postid ) { - delete_option('page_for_posts'); - } - } else { - unstick_post($postid); - } - // Do raw query. wp_get_post_revisions() is filtered $revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) ); // Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up. @@ -2309,6 +2352,34 @@ function wp_delete_post( $postid = 0, $force_delete = false ) { return $post; } +/** + * Resets the page_on_front, show_on_front, and page_for_post settings when a + * linked page is deleted or trashed. + * + * Also ensures the post is no longer sticky. + * + * @access private + * @since 3.7.0 + * @param $post_id + */ +function _reset_front_page_settings_for_post( $post_id ) { + $post = get_post( $post_id ); + if ( 'page' == $post->post_type ) { + // If the page is defined in option page_on_front or post_for_posts, + // adjust the corresponding options + if ( get_option( 'page_on_front' ) == $post->ID ) { + update_option( 'show_on_front', 'posts' ); + update_option( 'page_on_front', 0 ); + } + if ( get_option( 'page_for_posts' ) == $post->ID ) { + delete_option( 'page_for_posts', 0 ); + } + } + unstick_post( $post->ID ); +} +add_action( 'before_delete_post', '_reset_front_page_settings_for_post' ); +add_action( 'wp_trash_post', '_reset_front_page_settings_for_post' ); + /** * Moves a post or page to the Trash * @@ -2589,7 +2660,7 @@ function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) { } /** - * Insert a post. + * Insert or update a post. * * If the $postarr parameter has 'ID' set to a value, then post will be updated. * @@ -2597,38 +2668,38 @@ function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) { * and 'post_date_gmt' keys. You can close the comments or open the comments by * setting the value for 'comment_status' key. * - * The defaults for the parameter $postarr are: - * 'post_status' - Default is 'draft'. - * 'post_type' - Default is 'post'. - * 'post_author' - Default is current user ID ($user_ID). The ID of the user who added the post. - * 'ping_status' - Default is the value in 'default_ping_status' option. - * Whether the attachment can accept pings. - * 'post_parent' - Default is 0. Set this for the post it belongs to, if any. - * 'menu_order' - Default is 0. The order it is displayed. - * 'to_ping' - Whether to ping. - * 'pinged' - Default is empty string. - * 'post_password' - Default is empty string. The password to access the attachment. - * 'guid' - Global Unique ID for referencing the attachment. - * 'post_content_filtered' - Post content filtered. - * 'post_excerpt' - Post excerpt. + * @global wpdb $wpdb WordPress database abstraction object. * * @since 1.0.0 - * @uses $wpdb - * @uses $user_ID - * @uses do_action() Calls 'pre_post_update' on post ID if this is an update. - * @uses do_action() Calls 'edit_post' action on post ID and post data if this is an update. - * @uses do_action() Calls 'save_post' and 'wp_insert_post' on post id and post data just before returning. - * @uses apply_filters() Calls 'wp_insert_post_data' passing $data, $postarr prior to database update or insert. - * @uses wp_transition_post_status() - * - * @param array $postarr Elements that make up post to insert. - * @param bool $wp_error Optional. Allow return of WP_Error on failure. - * @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success. + * + * @param array $postarr { + * An array of elements that make up a post to update or insert. + * + * @type int 'ID' The post ID. If equal to something other than 0, the post with that ID will + * be updated. Default 0. + * @type string 'post_status' The post status. Default 'draft'. + * @type string 'post_type' The post type. Default 'post'. + * @type int 'post_author' The ID of the user who added the post. Default the current user ID. + * @type bool 'ping_status' Whether the post can accept pings. Default value of 'default_ping_status' option. + * @type int 'post_parent' Set this for the post it belongs to, if any. Default 0. + * @type int 'menu_order' The order it is displayed. Default 0. + * @type string 'to_ping' Space or carriage return-separated list of URLs to ping. Default empty string. + * @type string 'pinged' Space or carriage return-separated list of URLs that have been pinged. + * Default empty string. + * @type string 'post_password The password to access the post. Default empty string. + * @type string 'guid' Global Unique ID for referencing the post. + * @type string 'post_content_filtered' The filtered post content. Default empty string. + * @type string 'post_excerpt' The post excerpt. Default empty string. + * } + * @param bool $wp_error Optional. Allow return of WP_Error on failure. + * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure. */ -function wp_insert_post($postarr, $wp_error = false) { - global $wpdb, $user_ID; +function wp_insert_post( $postarr, $wp_error = false ) { + global $wpdb; + + $user_id = get_current_user_id(); - $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID, + $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_id, 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0, 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '', 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0, @@ -2693,7 +2764,7 @@ function wp_insert_post($postarr, $wp_error = false) { } if ( empty($post_author) ) - $post_author = $user_ID; + $post_author = $user_id; // Don't allow contributors to set the post slug for pending review posts if ( 'pending' == $post_status && !current_user_can( 'publish_posts' ) ) @@ -2879,8 +2950,9 @@ function wp_insert_post($postarr, $wp_error = false) { do_action( 'post_updated', $post_ID, $post_after, $post_before); } - do_action('save_post', $post_ID, $post); - do_action('wp_insert_post', $post_ID, $post); + do_action( "save_post_{$post->post_type}", $post_ID, $post, $update ); + do_action( 'save_post', $post_ID, $post, $update ); + do_action( 'wp_insert_post', $post_ID, $post, $update ); return $post_ID; } @@ -2949,7 +3021,7 @@ function wp_update_post( $postarr = array(), $wp_error = false ) { * * @since 2.1.0 * @uses $wpdb - * @uses do_action() Calls 'edit_post', 'save_post', and 'wp_insert_post' on post_id and post data. + * @uses do_action() Calls 'edit_post', 'save_post_{$post_type}', 'save_post' and 'wp_insert_post' on post_id and post data. * * @param int|object $post Post ID or object. */ @@ -2971,8 +3043,9 @@ function wp_publish_post( $post ) { wp_transition_post_status( 'publish', $old_status, $post ); do_action( 'edit_post', $post->ID, $post ); - do_action( 'save_post', $post->ID, $post ); - do_action( 'wp_insert_post', $post->ID, $post ); + do_action( "save_post_{$post->post_type}", $post->ID, $post, true ); + do_action( 'save_post', $post->ID, $post, true ); + do_action( 'wp_insert_post', $post->ID, $post, true ); } /** @@ -3185,24 +3258,28 @@ function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $a * @since 2.1.0 * * @param int $post_ID Post ID. - * @param array $post_categories Optional. List of categories. + * @param array|int $post_categories Optional. List of categories or ID of category. + * @param bool $append If true, don't delete existing categories, just add on. If false, replace the categories with the new categories. * @return bool|mixed */ -function wp_set_post_categories($post_ID = 0, $post_categories = array()) { +function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $append = false ) { $post_ID = (int) $post_ID; $post_type = get_post_type( $post_ID ); $post_status = get_post_status( $post_ID ); // If $post_categories isn't already an array, make it one: - if ( !is_array($post_categories) || empty($post_categories) ) { - if ( 'post' == $post_type && 'auto-draft' != $post_status ) + $post_categories = (array) $post_categories; + if ( empty( $post_categories ) ) { + if ( 'post' == $post_type && 'auto-draft' != $post_status ) { $post_categories = array( get_option('default_category') ); - else + $append = false; + } else { $post_categories = array(); + } } else if ( 1 == count($post_categories) && '' == reset($post_categories) ) { return true; } - return wp_set_post_terms($post_ID, $post_categories, 'category'); + return wp_set_post_terms( $post_ID, $post_categories, 'category', $append ); } /** @@ -3268,7 +3345,6 @@ function add_ping($post_id, $uri) { * Retrieve enclosures already enclosed for a post. * * @since 1.5.0 - * @uses $wpdb * * @param int $post_id Post ID. * @return array List of enclosures @@ -3551,15 +3627,18 @@ function _page_traverse_name( $page_id, &$children, &$result ){ * @since 1.5.0 * * @param mixed $page Page object or page ID. - * @return string Page URI. + * @return string|false Page URI, false on error. */ -function get_page_uri($page) { +function get_page_uri( $page ) { $page = get_post( $page ); + if ( ! $page ) + return false; + $uri = $page->post_name; foreach ( $page->ancestors as $parent ) { - $uri = get_post( $parent )->post_name . "/" . $uri; + $uri = get_post( $parent )->post_name . '/' . $uri; } return $uri; @@ -3568,17 +3647,41 @@ function get_page_uri($page) { /** * Retrieve a list of pages. * - * The defaults that can be overridden are the following: 'child_of', - * 'sort_order', 'sort_column', 'post_title', 'hierarchical', 'exclude', - * 'include', 'meta_key', 'meta_value','authors', 'number', and 'offset'. + * @global wpdb $wpdb WordPress database abstraction object * * @since 1.5.0 - * @uses $wpdb * - * @param mixed $args Optional. Array or string of options that overrides defaults. - * @return array List of pages matching defaults or $args + * @param mixed $args { + * Array or string of arguments. Optional. + * + * @type int 'child_of' Page ID to return child and grandchild pages of. Default 0, or no restriction. + * @type string 'sort_order' How to sort retrieved pages. + * Default 'ASC'. Accepts 'ASC', 'DESC'. + * @type string 'sort_column' What columns to sort pages by, comma-separated. + * Default 'post_title'. Accepts 'post_author', 'post_date', 'post_title', 'post_name', + * 'post_modified', 'post_modified_gmt', 'menu_order', 'post_parent', 'ID', 'rand', + * 'comment_count'. 'post_' can be omitted for any values that start with it. + * @type bool 'hierarchical' Whether to return pages hierarchically. Default true. + * @type array 'exclude' Array of page IDs to exclude. + * @type array 'include' Array of page IDs to include. Cannot be used with 'child_of', 'parent', 'exclude', + * 'meta_key', 'meta_value', or 'hierarchical'. + * @type string 'meta_key' Only include pages with this meta key. + * @type string 'meta_value' Only include pages with this meta value. + * @type string 'authors' A comma-separated list of author IDs. + * @type int 'parent' Page ID to return direct children of. 'hierarchical' must be false. + * Default -1, or no restriction. + * @type int 'exclude_tree' Remove all children of the given ID from returned pages. + * @type int 'number' The number of pages to return. Default 0, or all pages. + * @type int 'offset' The number of pages to skip before returning. Requires 'number'. + * Default 0. + * @type string 'post_type' The post type to query. + * Default 'page'. + * @type string 'post_status' A comma-separated list of post status types to include. + * Default 'publish'. + * } + * @return array List of pages matching defaults or $args. */ -function get_pages($args = '') { +function get_pages( $args = array() ) { global $wpdb; $pages = false; @@ -3603,6 +3706,9 @@ function get_pages($args = '') { if ( !in_array( $post_type, $hierarchical_post_types ) ) return $pages; + if ( $parent && ! $child_of ) + $hierarchical = false; + // Make sure we have a valid post status if ( !is_array( $post_status ) ) $post_status = explode( ',', $post_status ); @@ -3629,7 +3735,7 @@ function get_pages($args = '') { $cache = array(); $inclusions = ''; - if ( !empty($include) ) { + if ( ! empty( $include ) ) { $child_of = 0; //ignore child_of, parent, exclude, meta_key, and meta_value params if using include $parent = -1; $exclude = ''; @@ -3637,32 +3743,16 @@ function get_pages($args = '') { $meta_value = ''; $hierarchical = false; $incpages = wp_parse_id_list( $include ); - if ( ! empty( $incpages ) ) { - foreach ( $incpages as $incpage ) { - if (empty($inclusions)) - $inclusions = $wpdb->prepare(' AND ( ID = %d ', $incpage); - else - $inclusions .= $wpdb->prepare(' OR ID = %d ', $incpage); - } - } + if ( ! empty( $incpages ) ) + $inclusions = ' AND ID IN (' . implode( ',', $incpages ) . ')'; } - if (!empty($inclusions)) - $inclusions .= ')'; $exclusions = ''; - if ( !empty($exclude) ) { + if ( ! empty( $exclude ) ) { $expages = wp_parse_id_list( $exclude ); - if ( ! empty( $expages ) ) { - foreach ( $expages as $expage ) { - if (empty($exclusions)) - $exclusions = $wpdb->prepare(' AND ( ID <> %d ', $expage); - else - $exclusions .= $wpdb->prepare(' AND ID <> %d ', $expage); - } - } + if ( ! empty( $expages ) ) + $exclusions = ' AND ID NOT IN (' . implode( ',', $expages ) . ')'; } - if (!empty($exclusions)) - $exclusions .= ')'; $author_query = ''; if (!empty($authors)) { @@ -3705,8 +3795,13 @@ function get_pages($args = '') { } - if ( $parent >= 0 ) + if ( is_array( $parent ) ) { + $post_parent__in = implode( ',', array_map( 'absint', (array) $parent ) ); + if ( ! empty( $post_parent__in ) ) + $where .= " AND post_parent IN ($post_parent__in)"; + } elseif ( $parent >= 0 ) { $where .= $wpdb->prepare(' AND post_parent = %d ', $parent); + } if ( 1 == count( $post_status ) ) { $where_post_type = $wpdb->prepare( "post_type = %s AND post_status = %s", $post_type, array_shift( $post_status ) ); @@ -3863,7 +3958,6 @@ function is_local_attachment($url) { * * @since 2.0.0 * @uses $wpdb - * @uses $user_ID * @uses do_action() Calls 'edit_attachment' on $post_ID if this is an update. * @uses do_action() Calls 'add_attachment' on $post_ID if this is not an update. * @@ -3873,11 +3967,13 @@ function is_local_attachment($url) { * @return int Attachment ID. */ function wp_insert_attachment($object, $file = false, $parent = 0) { - global $wpdb, $user_ID; + global $wpdb; + + $user_id = get_current_user_id(); - $defaults = array('post_status' => 'inherit', 'post_type' => 'post', 'post_author' => $user_ID, + $defaults = array('post_status' => 'inherit', 'post_type' => 'post', 'post_author' => $user_id, 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0, 'post_title' => '', - 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '', + 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '', 'post_content' => '', 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0, 'context' => ''); $object = wp_parse_args($object, $defaults); @@ -3892,7 +3988,7 @@ function wp_insert_attachment($object, $file = false, $parent = 0) { extract($object, EXTR_SKIP); if ( empty($post_author) ) - $post_author = $user_ID; + $post_author = $user_id; $post_type = 'attachment'; @@ -4094,6 +4190,7 @@ function wp_delete_attachment( $post_id, $force_delete = false ) { // Don't delete the thumb if another attachment uses it if (! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $meta['thumb'] . '%', $post_id)) ) { $thumbfile = str_replace(basename($file), $meta['thumb'], $file); + /** This filter is documented in wp-admin/custom-header.php */ $thumbfile = apply_filters('wp_delete_file', $thumbfile); @ unlink( path_join($uploadpath['basedir'], $thumbfile) ); } @@ -4101,6 +4198,7 @@ function wp_delete_attachment( $post_id, $force_delete = false ) { // remove intermediate and backup images if there are any foreach ( $intermediate_sizes as $intermediate ) { + /** This filter is documented in wp-admin/custom-header.php */ $intermediate_file = apply_filters( 'wp_delete_file', $intermediate['path'] ); @ unlink( path_join($uploadpath['basedir'], $intermediate_file) ); } @@ -4108,11 +4206,13 @@ function wp_delete_attachment( $post_id, $force_delete = false ) { if ( is_array($backup_sizes) ) { foreach ( $backup_sizes as $size ) { $del_file = path_join( dirname($meta['file']), $size['file'] ); + /** This filter is documented in wp-admin/custom-header.php */ $del_file = apply_filters('wp_delete_file', $del_file); @ unlink( path_join($uploadpath['basedir'], $del_file) ); } } + /** This filter is documented in wp-admin/custom-header.php */ $file = apply_filters('wp_delete_file', $file); if ( ! empty($file) ) @@ -4416,8 +4516,6 @@ function wp_check_for_changed_slugs($post_id, $post, $post_before) { * * @since 2.2.0 * - * @uses $user_ID - * * @param string $post_type currently only supports 'post' or 'page'. * @return string SQL code that can be added to a where clause. */ @@ -4438,7 +4536,7 @@ function get_private_posts_cap_sql( $post_type ) { * @return string SQL WHERE code that can be added to a query. */ function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) { - global $user_ID, $wpdb; + global $wpdb; // Private posts $post_type_obj = get_post_type_object( $post_type ); @@ -4468,7 +4566,7 @@ function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $sql .= " OR post_status = 'private'"; } elseif ( is_user_logged_in() ) { // Users can view their own private posts. - $id = (int) $user_ID; + $id = get_current_user_id(); if ( null === $post_author || ! $full ) { $sql .= " OR post_status = 'private' AND post_author = $id"; } elseif ( $id == (int) $post_author ) { @@ -4642,7 +4740,6 @@ function clean_post_cache( $post ) { * @subpackage Cache * @since 1.5.0 * - * @uses $wpdb * @uses update_post_cache() * @uses update_object_term_cache() * @uses update_postmeta_cache() @@ -4697,8 +4794,6 @@ function update_post_caches(&$posts, $post_type = 'post', $update_term_cache = t * @subpackage Cache * @since 2.1.0 * - * @uses $wpdb - * * @param array $post_ids List of post IDs. * @return bool|array Returns false if there is nothing to update or an array of metadata. */