web/wp-includes/post.php
changeset 204 09a1c134465b
parent 194 32102edaa81b
--- a/web/wp-includes/post.php	Wed Dec 19 12:35:13 2012 -0800
+++ b/web/wp-includes/post.php	Wed Dec 19 17:46:52 2012 -0800
@@ -52,21 +52,27 @@
 
 	register_post_type( 'attachment', array(
 		'labels' => array(
-			'name' => __( 'Media' ),
-			'edit_item' => __( 'Edit Media' ),
+			'name' => _x('Media', 'post type general name'),
+			'name_admin_bar' => _x( 'Media', 'add new from admin bar' ),
+			'add_new' => _x( 'Add New', 'add new media' ),
+ 			'edit_item' => __( 'Edit Media' ),
+ 			'view_item' => __( 'View Attachment Page' ),
 		),
 		'public' => true,
-		'show_ui' => false,
+		'show_ui' => true,
 		'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
-		'_edit_link' => 'media.php?attachment_id=%d', /* internal use only. don't use this when registering your own post type. */
+		'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
 		'capability_type' => 'post',
+		'capabilities' => array(
+			'create_posts' => 'upload_files',
+		),
 		'map_meta_cap' => true,
 		'hierarchical' => false,
 		'rewrite' => false,
 		'query_var' => false,
 		'show_in_nav_menus' => false,
 		'delete_with_user' => true,
-		'supports' => array( 'comments', 'author' ),
+		'supports' => array( 'title', 'author', 'comments' ),
 	) );
 
 	register_post_type( 'revision', array(
@@ -204,9 +210,10 @@
 		return false;
 
 	$file = apply_filters( 'update_attached_file', $file, $attachment_id );
-	$file = _wp_relative_upload_path($file);
-
-	return update_post_meta( $attachment_id, '_wp_attached_file', $file );
+	if ( $file = _wp_relative_upload_path( $file ) )
+		return update_post_meta( $attachment_id, '_wp_attached_file', $file );
+	else
+		return delete_post_meta( $attachment_id, '_wp_attached_file' );
 }
 
 /**
@@ -223,11 +230,10 @@
 function _wp_relative_upload_path( $path ) {
 	$new_path = $path;
 
-	if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) {
-		if ( 0 === strpos($new_path, $uploads['basedir']) ) {
-				$new_path = str_replace($uploads['basedir'], '', $new_path);
-				$new_path = ltrim($new_path, '/');
-		}
+	$uploads = wp_upload_dir();
+	if ( 0 === strpos( $new_path, $uploads['basedir'] ) ) {
+			$new_path = str_replace( $uploads['basedir'], '', $new_path );
+			$new_path = ltrim( $new_path, '/' );
 	}
 
 	return apply_filters( '_wp_relative_upload_path', $new_path, $path );
@@ -372,56 +378,297 @@
  * @uses $wpdb
  * @link http://codex.wordpress.org/Function_Reference/get_post
  *
- * @param int|object $post Post ID or post object.
+ * @param int|object $post Post ID or post object. Optional, default is the current post from the loop.
  * @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
  * @param string $filter Optional, default is raw.
- * @return mixed Post data
+ * @return WP_Post|null WP_Post on success or null on failure
+ */
+function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) {
+	if ( empty( $post ) && isset( $GLOBALS['post'] ) )
+		$post = $GLOBALS['post'];
+
+	if ( is_a( $post, 'WP_Post' ) ) {
+		$_post = $post;
+	} elseif ( is_object( $post ) ) {
+		if ( empty( $post->filter ) ) {
+			$_post = sanitize_post( $post, 'raw' );
+			$_post = new WP_Post( $_post );
+		} elseif ( 'raw' == $post->filter ) {
+			$_post = new WP_Post( $post );
+		} else {
+			$_post = WP_Post::get_instance( $post->ID );
+		}
+	} else {
+		$_post = WP_Post::get_instance( $post );
+	}
+
+	if ( ! $_post )
+		return null;
+
+	$_post = $_post->filter( $filter );
+
+	if ( $output == ARRAY_A )
+		return $_post->to_array();
+	elseif ( $output == ARRAY_N )
+		return array_values( $_post->to_array() );
+
+	return $_post;
+}
+
+/**
+ * WordPress Post class.
+ *
+ * @since 3.5.0
+ *
  */
-function &get_post(&$post, $output = OBJECT, $filter = 'raw') {
-	global $wpdb;
-	$null = null;
-
-	if ( empty($post) ) {
-		if ( isset($GLOBALS['post']) )
-			$_post = & $GLOBALS['post'];
-		else
-			return $null;
-	} elseif ( is_object($post) && empty($post->filter) ) {
-		_get_post_ancestors($post);
-		$_post = sanitize_post($post, 'raw');
-		wp_cache_add($post->ID, $_post, 'posts');
-	} elseif ( is_object($post) && 'raw' == $post->filter ) {
-		$_post = $post;
-	} else {
-		if ( is_object($post) )
-			$post_id = $post->ID;
-		else
-			$post_id = $post;
+final class WP_Post {
+
+	/**
+	 *
+	 * @var int
+	 */
+	public $ID;
+
+	/**
+	 *
+	 * @var int
+	 */
+	public $post_author = 0;
+
+	/**
+	 *
+	 * @var string
+	 */
+	public $post_date = '0000-00-00 00:00:00';
+
+	/**
+	 *
+	 * @var string
+	 */
+	public $post_date_gmt = '0000-00-00 00:00:00';
+
+	/**
+	 *
+	 * @var string
+	 */
+	public $post_content = '';
+
+	/**
+	 *
+	 * @var string
+	 */
+	public $post_title = '';
+
+	/**
+	 *
+	 * @var string
+	 */
+	public $post_excerpt = '';
+
+	/**
+	 *
+	 * @var string
+	 */
+	public $post_status = 'publish';
+
+	/**
+	 *
+	 * @var string
+	 */
+	public $comment_status = 'open';
+
+	/**
+	 *
+	 * @var string
+	 */
+	public $ping_status = 'open';
+
+	/**
+	 *
+	 * @var string
+	 */
+	public $post_password = '';
+
+	/**
+	 *
+	 * @var string
+	 */
+	public $post_name = '';
+
+	/**
+	 *
+	 * @var string
+	 */
+	public $to_ping = '';
+
+	/**
+	 *
+	 * @var string
+	 */
+	public $pinged = '';
+
+	/**
+	 *
+	 * @var string
+	 */
+	public $post_modified = '0000-00-00 00:00:00';
+
+	/**
+	 *
+	 * @var string
+	 */
+	public $post_modified_gmt = '0000-00-00 00:00:00';
+
+	/**
+	 *
+	 * @var string
+	 */
+	public $post_content_filtered = '';
+
+	/**
+	 *
+	 * @var int
+	 */
+	public $post_parent = 0;
+
+	/**
+	 *
+	 * @var string
+	 */
+	public $guid = '';
+
+	/**
+	 *
+	 * @var int
+	 */
+	public $menu_order = 0;
+
+	/**
+	 *
+	 * @var string
+	 */
+	public $post_type = 'post';
+
+	/**
+	 *
+	 * @var string
+	 */
+	public $post_mime_type = '';
+
+	/**
+	 *
+	 * @var int
+	 */
+	public $comment_count = 0;
+
+	/**
+	 *
+	 * @var string
+	 */
+	public $filter;
+
+	public static function get_instance( $post_id ) {
+		global $wpdb;
 
 		$post_id = (int) $post_id;
-		if ( ! $_post = wp_cache_get($post_id, 'posts') ) {
-			$_post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id));
+		if ( ! $post_id )
+			return false;
+
+		$_post = wp_cache_get( $post_id, 'posts' );
+
+		if ( ! $_post ) {
+			$_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id ) );
+
 			if ( ! $_post )
-				return $null;
-			_get_post_ancestors($_post);
-			$_post = sanitize_post($_post, 'raw');
-			wp_cache_add($_post->ID, $_post, 'posts');
+				return false;
+
+			$_post = sanitize_post( $_post, 'raw' );
+			wp_cache_add( $_post->ID, $_post, 'posts' );
+		} elseif ( empty( $_post->filter ) ) {
+			$_post = sanitize_post( $_post, 'raw' );
 		}
+
+		return new WP_Post( $_post );
+	}
+
+	public function __construct( $post ) {
+		foreach ( get_object_vars( $post ) as $key => $value )
+			$this->$key = $value;
+	}
+
+	public function __isset( $key ) {
+		if ( 'ancestors' == $key )
+			return true;
+
+		if ( 'page_template' == $key )
+			return ( 'page' == $this->post_type );
+
+		if ( 'post_category' == $key )
+		   return true;
+
+		if ( 'tags_input' == $key )
+		   return true;
+
+		return metadata_exists( 'post', $this->ID, $key );
 	}
 
-	if ($filter != 'raw')
-		$_post = sanitize_post($_post, $filter);
-
-	if ( $output == OBJECT ) {
-		return $_post;
-	} elseif ( $output == ARRAY_A ) {
-		$__post = get_object_vars($_post);
-		return $__post;
-	} elseif ( $output == ARRAY_N ) {
-		$__post = array_values(get_object_vars($_post));
-		return $__post;
-	} else {
-		return $_post;
+	public function __get( $key ) {
+		if ( 'page_template' == $key && $this->__isset( $key ) ) {
+			return get_post_meta( $this->ID, '_wp_page_template', true );
+		}
+
+		if ( 'post_category' == $key ) {
+			if ( is_object_in_taxonomy( $this->post_type, 'category' ) )
+				$terms = get_the_terms( $this, 'category' );
+
+			if ( empty( $terms ) )
+				return array();
+
+			return wp_list_pluck( $terms, 'term_id' );
+		}
+
+		if ( 'tags_input' == $key ) {
+			if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) )
+				$terms = get_the_terms( $this, 'post_tag' );
+
+			if ( empty( $terms ) )
+				return array();
+
+			return wp_list_pluck( $terms, 'name' );
+		}
+
+		// Rest of the values need filtering
+
+		if ( 'ancestors' == $key )
+			$value = get_post_ancestors( $this );
+		else
+			$value = get_post_meta( $this->ID, $key, true );
+
+		if ( $this->filter )
+			$value = sanitize_post_field( $key, $value, $this->ID, $this->filter );
+
+		return $value;
+	}
+
+	public function filter( $filter ) {
+		if ( $this->filter == $filter )
+			return $this;
+
+		if ( $filter == 'raw' )
+			return self::get_instance( $this->ID );
+
+		return sanitize_post( $this, $filter );
+	}
+
+	public function to_array() {
+		$post = get_object_vars( $this );
+
+		foreach ( array( 'ancestors', 'page_template', 'post_category', 'tags_input' ) as $key ) {
+			if ( $this->__isset( $key ) )
+				$post[ $key ] = $this->__get( $key );
+		}
+
+		return $post;
 	}
 }
 
@@ -433,16 +680,28 @@
  * @param int|object $post Post ID or post object
  * @return array Ancestor IDs or empty array if none are found.
  */
-function get_post_ancestors($post) {
-	$post = get_post($post);
-
-	if ( ! isset( $post->ancestors ) )
-		_get_post_ancestors( $post );
-
-	if ( ! empty( $post->ancestors ) )
-		return $post->ancestors;
-
-	return array();
+function get_post_ancestors( $post ) {
+	if ( ! $post )
+		return false;
+
+	$post = get_post( $post );
+
+	if ( empty( $post->post_parent ) || $post->post_parent == $post->ID )
+		return array();
+
+	$ancestors = array();
+
+	$id = $ancestors[] = $post->post_parent;
+
+	while ( $ancestor = get_post( $id ) ) {
+		// Loop detection: If the ancestor has been seen before, break.
+		if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors ) )
+			break;
+
+		$id = $ancestors[] = $ancestor->post_parent;
+	}
+
+	return $ancestors;
 }
 
 /**
@@ -460,16 +719,12 @@
  * @param string $field Post field name
  * @param id $post Post ID
  * @param string $context Optional. How to filter the field. Default is display.
- * @return WP_Error|string Value in post field or WP_Error on failure
+ * @return bool|string False on failure or returns the value in post field
  */
 function get_post_field( $field, $post, $context = 'display' ) {
-	$post = (int) $post;
 	$post = get_post( $post );
 
-	if ( is_wp_error($post) )
-		return $post;
-
-	if ( !is_object($post) )
+	if ( !$post )
 		return '';
 
 	if ( !isset($post->$field) )
@@ -490,7 +745,7 @@
  * @return bool|string False on failure or returns the mime type
  */
 function get_post_mime_type($ID = '') {
-	$post = & get_post($ID);
+	$post = get_post($ID);
 
 	if ( is_object($post) )
 		return $post->post_mime_type;
@@ -671,7 +926,19 @@
 		$wp_post_statuses = array();
 
 	// Args prefixed with an underscore are reserved for internal use.
-	$defaults = array('label' => false, 'label_count' => false, 'exclude_from_search' => null, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'public' => null, 'internal' => null, 'protected' => null, 'private' => null, 'show_in_admin_all' => null, 'publicly_queryable' => null, 'show_in_admin_status_list' => null, 'show_in_admin_all_list' => null, 'single_view_cap' => null);
+	$defaults = array(
+		'label' => false,
+		'label_count' => false,
+		'exclude_from_search' => null,
+		'_builtin' => false,
+		'public' => null,
+		'internal' => null,
+		'protected' => null,
+		'private' => null,
+		'publicly_queryable' => null,
+		'show_in_admin_status_list' => null,
+		'show_in_admin_all_list' => null,
+	);
 	$args = wp_parse_args($args, $defaults);
 	$args = (object) $args;
 
@@ -703,10 +970,7 @@
 		$args->show_in_admin_all_list = !$args->internal;
 
 	if ( null === $args->show_in_admin_status_list )
-			$args->show_in_admin_status_list = !$args->internal;
-
-	if ( null === $args->single_view_cap )
-		$args->single_view_cap = $args->public ? '' : 'edit';
+		$args->show_in_admin_status_list = !$args->internal;
 
 	if ( false === $args->label )
 		$args->label = $post_status;
@@ -804,19 +1068,12 @@
  *
  * @uses $post The Loop current post global
  *
- * @param mixed $the_post Optional. Post object or post ID.
+ * @param mixed $post Optional. Post object or post ID.
  * @return bool|string post type or false on failure.
  */
-function get_post_type( $the_post = false ) {
-	global $post;
-
-	if ( false === $the_post )
-		$the_post = $post;
-	elseif ( is_numeric($the_post) )
-		$the_post = get_post($the_post);
-
-	if ( is_object($the_post) )
-		return $the_post->post_type;
+function get_post_type( $post = null ) {
+	if ( $post = get_post( $post ) )
+		return $post->post_type;
 
 	return false;
 }
@@ -1020,7 +1277,7 @@
 	if ( ! empty($args->supports) ) {
 		add_post_type_support($post_type, $args->supports);
 		unset($args->supports);
-	} else {
+	} elseif ( false !== $args->supports ) {
 		// Add default features
 		add_post_type_support($post_type, array('title', 'editor'));
 	}
@@ -1028,7 +1285,8 @@
 	if ( false !== $args->query_var && !empty($wp) ) {
 		if ( true === $args->query_var )
 			$args->query_var = $post_type;
-		$args->query_var = sanitize_title_with_dashes($args->query_var);
+		else
+			$args->query_var = sanitize_title_with_dashes($args->query_var);
 		$wp->add_query_var($args->query_var);
 	}
 
@@ -1182,6 +1440,10 @@
 
 	$capabilities = array_merge( $default_capabilities, $args->capabilities );
 
+	// Post creation capability simply maps to edit_posts by default:
+	if ( ! isset( $capabilities['create_posts'] ) )
+		$capabilities['create_posts'] = $capabilities['edit_posts'];
+
 	// Remember meta capabilities for future reference.
 	if ( $args->map_meta_cap )
 		_post_type_meta_capabilities( $capabilities );
@@ -1245,7 +1507,11 @@
 		'all_items' => array( __( 'All Posts' ), __( 'All Pages' ) )
 	);
 	$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
-	return _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
+
+	$labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
+
+	$post_type = $post_type_object->name;
+	return apply_filters( "post_type_labels_{$post_type}", $labels );
 }
 
 /**
@@ -1668,9 +1934,9 @@
  * @since 2.3.0
  * @uses sanitize_post_field() Used to sanitize the fields.
  *
- * @param object|array $post The Post Object or Array
+ * @param object|WP_Post|array $post The Post Object or Array
  * @param string $context Optional, default is 'display'. How to sanitize post fields.
- * @return object|array The now sanitized Post Object or Array (will be the same type as $post)
+ * @return object|WP_Post|array The now sanitized Post Object or Array (will be the same type as $post)
  */
 function sanitize_post($post, $context = 'display') {
 	if ( is_object($post) ) {
@@ -1917,6 +2183,23 @@
 }
 
 /**
+ * Get default post mime types
+ *
+ * @since 2.9.0
+ *
+ * @return array
+ */
+function get_post_mime_types() {
+	$post_mime_types = array(	//	array( adj, noun )
+		'image' => array(__('Images'), __('Manage Images'), _n_noop('Image <span class="count">(%s)</span>', 'Images <span class="count">(%s)</span>')),
+		'audio' => array(__('Audio'), __('Manage Audio'), _n_noop('Audio <span class="count">(%s)</span>', 'Audio <span class="count">(%s)</span>')),
+		'video' => array(__('Video'), __('Manage Video'), _n_noop('Video <span class="count">(%s)</span>', 'Video <span class="count">(%s)</span>')),
+	);
+
+	return apply_filters('post_mime_types', $post_mime_types);
+}
+
+/**
  * Check a MIME-Type against a list.
  *
  * If the wildcard_mime_types parameter is a string, it must be comma separated
@@ -2086,8 +2369,8 @@
 
 	clean_post_cache( $post );
 
-	if ( is_post_type_hierarchical( $post->post_type ) ) {
-		foreach ( (array) $children as $child )
+	if ( is_post_type_hierarchical( $post->post_type ) && $children ) {
+		foreach ( $children as $child )
 			clean_post_cache( $child );
 	}
 
@@ -2115,7 +2398,7 @@
 	if ( !EMPTY_TRASH_DAYS )
 		return wp_delete_post($post_id, true);
 
-	if ( !$post = wp_get_single_post($post_id, ARRAY_A) )
+	if ( !$post = get_post($post_id, ARRAY_A) )
 		return $post;
 
 	if ( $post['post_status'] == 'trash' )
@@ -2147,7 +2430,7 @@
  * @return mixed False on failure
  */
 function wp_untrash_post($post_id = 0) {
-	if ( !$post = wp_get_single_post($post_id, ARRAY_A) )
+	if ( !$post = get_post($post_id, ARRAY_A) )
 		return $post;
 
 	if ( $post['post_status'] != 'trash' )
@@ -2378,49 +2661,6 @@
 }
 
 /**
- * Retrieve a single post, based on post ID.
- *
- * Has categories in 'post_category' property or key. Has tags in 'tags_input'
- * property or key.
- *
- * @since 1.0.0
- *
- * @param int $postid Post ID.
- * @param string $mode How to return result, either OBJECT, ARRAY_N, or ARRAY_A.
- * @return object|array Post object or array holding post contents and information
- */
-function wp_get_single_post($postid = 0, $mode = OBJECT) {
-	$postid = (int) $postid;
-
-	$post = get_post($postid, $mode);
-
-	if (
-		( OBJECT == $mode && empty( $post->ID ) ) ||
-		( OBJECT != $mode && empty( $post['ID'] ) )
-	)
-		return ( OBJECT == $mode ? null : array() );
-
-	// Set categories and tags
-	if ( $mode == OBJECT ) {
-		$post->post_category = array();
-		if ( is_object_in_taxonomy($post->post_type, 'category') )
-			$post->post_category = wp_get_post_categories($postid);
-		$post->tags_input = array();
-		if ( is_object_in_taxonomy($post->post_type, 'post_tag') )
-			$post->tags_input = wp_get_post_tags($postid, array('fields' => 'names'));
-	} else {
-		$post['post_category'] = array();
-		if ( is_object_in_taxonomy($post['post_type'], 'category') )
-			$post['post_category'] = wp_get_post_categories($postid);
-		$post['tags_input'] = array();
-		if ( is_object_in_taxonomy($post['post_type'], 'post_tag') )
-			$post['tags_input'] = wp_get_post_tags($postid, array('fields' => 'names'));
-	}
-
-	return $post;
-}
-
-/**
  * Insert a post.
  *
  * If the $postarr parameter has 'ID' set to a value, then post will be updated.
@@ -2547,6 +2787,18 @@
 	if ( empty($post_date) || '0000-00-00 00:00:00' == $post_date )
 		$post_date = current_time('mysql');
 
+		// validate the date
+		$mm = substr( $post_date, 5, 2 );
+		$jj = substr( $post_date, 8, 2 );
+		$aa = substr( $post_date, 0, 4 );
+		$valid_date = wp_checkdate( $mm, $jj, $aa, $post_date );
+		if ( !$valid_date ) {
+			if ( $wp_error )
+				return new WP_Error( 'invalid_date', __( 'Whoops, the provided date is invalid.' ) );
+			else
+				return 0;
+		}
+
 	if ( empty($post_date_gmt) || '0000-00-00 00:00:00' == $post_date_gmt ) {
 		if ( !in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) )
 			$post_date_gmt = get_gmt_from_date($post_date);
@@ -2667,18 +2919,18 @@
 
 	$current_guid = get_post_field( 'guid', $post_ID );
 
-	clean_post_cache( $post_ID );
-
 	// Set GUID
 	if ( !$update && '' == $current_guid )
 		$wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where );
 
+	clean_post_cache( $post_ID );
+
 	$post = get_post($post_ID);
 
 	if ( !empty($page_template) && 'page' == $data['post_type'] ) {
 		$post->page_template = $page_template;
-		$page_templates = get_page_templates();
-		if ( 'default' != $page_template && !in_array($page_template, $page_templates) ) {
+		$page_templates = wp_get_theme()->get_page_templates();
+		if ( 'default' != $page_template && ! isset( $page_templates[ $page_template ] ) ) {
 			if ( $wp_error )
 				return new WP_Error('invalid_page_template', __('The page template is invalid.'));
 			else
@@ -2710,9 +2962,10 @@
  * @since 1.0.0
  *
  * @param array|object $postarr Post data. Arrays are expected to be escaped, objects are not.
- * @return int 0 on failure, Post ID on success.
+ * @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.
  */
-function wp_update_post($postarr = array()) {
+function wp_update_post( $postarr = array(), $wp_error = false ) {
 	if ( is_object($postarr) ) {
 		// non-escaped post was passed
 		$postarr = get_object_vars($postarr);
@@ -2720,7 +2973,7 @@
 	}
 
 	// First, get all of the original fields
-	$post = wp_get_single_post($postarr['ID'], ARRAY_A);
+	$post = get_post($postarr['ID'], ARRAY_A);
 
 	// Escape data pulled from DB.
 	$post = add_magic_quotes($post);
@@ -2750,39 +3003,25 @@
 	if ($postarr['post_type'] == 'attachment')
 		return wp_insert_attachment($postarr);
 
-	return wp_insert_post($postarr);
+	return wp_insert_post( $postarr, $wp_error );
 }
 
 /**
  * Publish a post by transitioning the post status.
  *
  * @since 2.1.0
- * @uses $wpdb
- * @uses do_action() Calls 'edit_post', 'save_post', and 'wp_insert_post' on post_id and post data.
- *
- * @param int $post_id Post ID.
- * @return null
+ * @uses wp_update_post()
+ *
+ * @param mixed $post Post ID or object.
  */
-function wp_publish_post($post_id) {
-	global $wpdb;
-
-	$post = get_post($post_id);
-
-	if ( empty($post) )
+function wp_publish_post( $post ) {
+	if ( ! $post = get_post( $post ) )
 		return;
-
 	if ( 'publish' == $post->post_status )
 		return;
 
-	$wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post_id ) );
-
-	$old_status = $post->post_status;
 	$post->post_status = 'publish';
-	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);
+	wp_update_post( $post );
 }
 
 /**
@@ -2837,6 +3076,8 @@
 
 	global $wpdb, $wp_rewrite;
 
+	$original_slug = $slug;
+
 	$feeds = $wp_rewrite->feeds;
 	if ( ! is_array( $feeds ) )
 		$feeds = array();
@@ -2857,6 +3098,8 @@
 			$slug = $alt_post_name;
 		}
 	} elseif ( in_array( $post_type, $hierarchical_post_types ) ) {
+		if ( 'nav_menu_item' == $post_type )
+			return $slug;
 		// Page slugs must be unique within their own trees. Pages are in a separate
 		// namespace than posts so page slugs are allowed to overlap post slugs.
 		$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( '" . implode( "', '", esc_sql( $hierarchical_post_types ) ) . "' ) AND ID != %d AND post_parent = %d LIMIT 1";
@@ -2887,7 +3130,7 @@
 		}
 	}
 
-	return apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent );
+	return apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug );
 }
 
 /**
@@ -2953,11 +3196,10 @@
 	// Hierarchical taxonomies must always pass IDs rather than names so that children with the same
 	// names but different parents aren't confused.
 	if ( is_taxonomy_hierarchical( $taxonomy ) ) {
-		$tags = array_map( 'intval', $tags );
-		$tags = array_unique( $tags );
+		$tags = array_unique( array_map( 'intval', $tags ) );
 	}
 
-	return wp_set_object_terms($post_id, $tags, $taxonomy, $append);
+	return wp_set_object_terms( $post_id, $tags, $taxonomy, $append );
 }
 
 /**
@@ -2986,12 +3228,7 @@
 		return true;
 	}
 
-	if ( !empty($post_categories) ) {
-		$post_categories = array_map('intval', $post_categories);
-		$post_categories = array_unique($post_categories);
-	}
-
-	return wp_set_object_terms($post_ID, $post_categories, 'category');
+	return wp_set_post_terms($post_ID, $post_categories, 'category');
 }
 
 /**
@@ -3127,7 +3364,7 @@
 function trackback_url_list($tb_list, $post_id) {
 	if ( ! empty( $tb_list ) ) {
 		// get post data
-		$postdata = wp_get_single_post($post_id, ARRAY_A);
+		$postdata = get_post($post_id, ARRAY_A);
 
 		// import postdata as variables
 		extract($postdata, EXTR_SKIP);
@@ -3174,16 +3411,18 @@
 /**
  * Retrieves page data given a page ID or page object.
  *
+ * Use get_post() instead of get_page().
+ *
  * @since 1.5.1
+ * @deprecated 3.5.0
  *
  * @param mixed $page Page object or page ID. Passed by reference.
  * @param string $output What to output. OBJECT, ARRAY_A, or ARRAY_N.
  * @param string $filter How the return value should be filtered.
- * @return mixed Page data.
+ * @return WP_Post|null WP_Post on success or null on failure
  */
-function &get_page(&$page, $output = OBJECT, $filter = 'raw') {
-	$p = get_post($page, $output, $filter);
-	return $p;
+function get_page( $page, $output = OBJECT, $filter = 'raw') {
+	return get_post( $page, $output, $filter );
 }
 
 /**
@@ -3195,7 +3434,7 @@
  * @param string $page_path Page path
  * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT.
  * @param string $post_type Optional. Post type. Default page.
- * @return mixed Null when complete.
+ * @return WP_Post|null WP_Post on success or null on failure
  */
 function get_page_by_path($page_path, $output = OBJECT, $post_type = 'page') {
 	global $wpdb;
@@ -3210,7 +3449,7 @@
 	$in_string = "'". implode( "','", $parts ) . "'";
 	$post_type_sql = $post_type;
 	$wpdb->escape_by_ref( $post_type_sql );
-	$pages = $wpdb->get_results( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name IN ($in_string) AND (post_type = '$post_type_sql' OR post_type = 'attachment')", OBJECT_K );
+	$pages = $wpdb->get_results( "SELECT ID, post_name, post_parent, post_type FROM $wpdb->posts WHERE post_name IN ($in_string) AND (post_type = '$post_type_sql' OR post_type = 'attachment')", OBJECT_K );
 
 	$revparts = array_reverse( $parts );
 
@@ -3229,13 +3468,14 @@
 
 			if ( $p->post_parent == 0 && $count+1 == count( $revparts ) && $p->post_name == $revparts[ $count ] ) {
 				$foundid = $page->ID;
-				break;
+				if ( $page->post_type == $post_type )
+					break;
 			}
 		}
 	}
 
 	if ( $foundid )
-		return get_page( $foundid, $output );
+		return get_post( $foundid, $output );
 
 	return null;
 }
@@ -3249,13 +3489,13 @@
  * @param string $page_title Page title
  * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT.
  * @param string $post_type Optional. Post type. Default page.
- * @return mixed
+ * @return WP_Post|null WP_Post on success or null on failure
  */
 function get_page_by_title($page_title, $output = OBJECT, $post_type = 'page' ) {
 	global $wpdb;
 	$page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", $page_title, $post_type ) );
 	if ( $page )
-		return get_page($page, $output);
+		return get_post( $page, $output );
 
 	return null;
 }
@@ -3273,7 +3513,7 @@
  * @param array $pages List of pages' objects.
  * @return array
  */
-function &get_page_children($page_id, $pages) {
+function get_page_children($page_id, $pages) {
 	$page_list = array();
 	foreach ( (array) $pages as $page ) {
 		if ( $page->post_parent == $page_id ) {
@@ -3297,7 +3537,7 @@
  * @param int $page_id Parent page ID.
  * @return array A list arranged by hierarchy. Children immediately follow their parents.
  */
-function &get_page_hierarchy( &$pages, $page_id = 0 ) {
+function get_page_hierarchy( &$pages, $page_id = 0 ) {
 	if ( empty( $pages ) ) {
 		$result = array();
 		return $result;
@@ -3342,16 +3582,11 @@
  */
 function get_page_uri($page) {
 	if ( ! is_object($page) )
-		$page = get_page($page);
+		$page = get_post( $page );
 	$uri = $page->post_name;
 
-	// A page cannot be it's own parent.
-	if ( $page->post_parent == $page->ID )
-		return $uri;
-
-	while ($page->post_parent != 0) {
-		$page = get_page($page->post_parent);
-		$uri = $page->post_name . "/" . $uri;
+	foreach ( $page->ancestors as $parent ) {
+		$uri = get_post( $parent )->post_name . "/" . $uri;
 	}
 
 	return $uri;
@@ -3370,9 +3605,11 @@
  * @param mixed $args Optional. Array or string of options that overrides defaults.
  * @return array List of pages matching defaults or $args
  */
-function &get_pages($args = '') {
+function get_pages($args = '') {
 	global $wpdb;
 
+	$pages = false;
+
 	$defaults = array(
 		'child_of' => 0, 'sort_order' => 'ASC',
 		'sort_column' => 'post_title', 'hierarchical' => 1,
@@ -3391,19 +3628,21 @@
 	// Make sure the post type is hierarchical
 	$hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) );
 	if ( !in_array( $post_type, $hierarchical_post_types ) )
-		return false;
+		return $pages;
 
 	// Make sure we have a valid post status
 	if ( !is_array( $post_status ) )
 		$post_status = explode( ',', $post_status );
 	if ( array_diff( $post_status, get_post_stati() ) )
-		return false;
+		return $pages;
 
 	$cache = array();
 	$key = md5( serialize( compact(array_keys($defaults)) ) );
 	if ( $cache = wp_cache_get( 'get_pages', 'posts' ) ) {
-		if ( is_array($cache) && isset( $cache[ $key ] ) ) {
-			$pages = apply_filters('get_pages', $cache[ $key ], $r );
+		if ( is_array($cache) && isset( $cache[ $key ] ) && is_array( $cache[ $key ] ) ) {
+			// Convert to WP_Post instances
+			$pages = array_map( 'get_post', $cache[ $key ] );
+			$pages = apply_filters( 'get_pages', $pages, $r );
 			return $pages;
 		}
 	}
@@ -3559,7 +3798,7 @@
 	update_post_cache( $pages );
 
 	if ( $child_of || $hierarchical )
-		$pages = & get_page_children($child_of, $pages);
+		$pages = get_page_children($child_of, $pages);
 
 	if ( !empty($exclude_tree) ) {
 		$exclude = (int) $exclude_tree;
@@ -3578,6 +3817,9 @@
 	$cache[ $key ] = $pages;
 	wp_cache_set( 'get_pages', $cache, 'posts' );
 
+	// Convert to WP_Post instances
+	$pages = array_map( 'get_post', $pages );
+
 	$pages = apply_filters('get_pages', $pages, $r);
 
 	return $pages;
@@ -3601,7 +3843,7 @@
 	if (strpos($url, home_url('/?attachment_id=')) !== false)
 		return true;
 	if ( $id = url_to_postid($url) ) {
-		$post = & get_post($id);
+		$post = get_post($id);
 		if ( 'attachment' == $post->post_type )
 			return true;
 	}
@@ -3676,13 +3918,12 @@
 	if ( ! in_array( $post_status, array( 'inherit', 'private' ) ) )
 		$post_status = 'inherit';
 
+	if ( !empty($post_category) )
+		$post_category = array_filter($post_category); // Filter out empty terms
+
 	// Make sure we set a valid category.
-	if ( !isset($post_category) || 0 == count($post_category) || !is_array($post_category) ) {
-		// 'post' requires at least one category.
-		if ( 'post' == $post_type )
-			$post_category = array( get_option('default_category') );
-		else
-			$post_category = array();
+	if ( empty($post_category) || 0 == count($post_category) || !is_array($post_category) ) {
+		$post_category = array();
 	}
 
 	// Are we updating or creating?
@@ -3767,7 +4008,22 @@
 		$wpdb->update( $wpdb->posts, compact("post_name"), array( 'ID' => $post_ID ) );
 	}
 
-	wp_set_post_categories($post_ID, $post_category);
+	if ( is_object_in_taxonomy($post_type, 'category') )
+		wp_set_post_categories( $post_ID, $post_category );
+
+	if ( isset( $tags_input ) && is_object_in_taxonomy($post_type, 'post_tag') )
+		wp_set_post_tags( $post_ID, $tags_input );
+
+	// support for all custom taxonomies
+	if ( !empty($tax_input) ) {
+		foreach ( $tax_input as $taxonomy => $tags ) {
+			$taxonomy_obj = get_taxonomy($taxonomy);
+			if ( is_array($tags) ) // array = hierarchical, string = non-hierarchical.
+				$tags = array_filter($tags);
+			if ( current_user_can($taxonomy_obj->cap->assign_terms) )
+				wp_set_post_terms( $post_ID, $tags, $taxonomy );
+		}
+	}
 
 	if ( $file )
 		update_attached_file( $post_ID, $file );
@@ -3897,7 +4153,7 @@
  */
 function wp_get_attachment_metadata( $post_id = 0, $unfiltered = false ) {
 	$post_id = (int) $post_id;
-	if ( !$post =& get_post( $post_id ) )
+	if ( !$post = get_post( $post_id ) )
 		return false;
 
 	$data = get_post_meta( $post->ID, '_wp_attachment_metadata', true );
@@ -3919,12 +4175,13 @@
  */
 function wp_update_attachment_metadata( $post_id, $data ) {
 	$post_id = (int) $post_id;
-	if ( !$post =& get_post( $post_id ) )
+	if ( !$post = get_post( $post_id ) )
 		return false;
 
-	$data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID );
-
-	return update_post_meta( $post->ID, '_wp_attachment_metadata', $data);
+	if ( $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID ) )
+		return update_post_meta( $post->ID, '_wp_attachment_metadata', $data );
+	else
+		return delete_post_meta( $post->ID, '_wp_attachment_metadata' );
 }
 
 /**
@@ -3937,7 +4194,7 @@
  */
 function wp_get_attachment_url( $post_id = 0 ) {
 	$post_id = (int) $post_id;
-	if ( !$post =& get_post( $post_id ) )
+	if ( !$post = get_post( $post_id ) )
 		return false;
 
 	if ( 'attachment' != $post->post_type )
@@ -3976,7 +4233,7 @@
  */
 function wp_get_attachment_thumb_file( $post_id = 0 ) {
 	$post_id = (int) $post_id;
-	if ( !$post =& get_post( $post_id ) )
+	if ( !$post = get_post( $post_id ) )
 		return false;
 	if ( !is_array( $imagedata = wp_get_attachment_metadata( $post->ID ) ) )
 		return false;
@@ -3998,7 +4255,7 @@
  */
 function wp_get_attachment_thumb_url( $post_id = 0 ) {
 	$post_id = (int) $post_id;
-	if ( !$post =& get_post( $post_id ) )
+	if ( !$post = get_post( $post_id ) )
 		return false;
 	if ( !$url = wp_get_attachment_url( $post->ID ) )
 		return false;
@@ -4025,7 +4282,7 @@
  */
 function wp_attachment_is_image( $post_id = 0 ) {
 	$post_id = (int) $post_id;
-	if ( !$post =& get_post( $post_id ) )
+	if ( !$post = get_post( $post_id ) )
 		return false;
 
 	if ( !$file = get_attached_file( $post->ID ) )
@@ -4033,7 +4290,7 @@
 
 	$ext = preg_match('/\.([^.]+)$/', $file, $matches) ? strtolower($matches[1]) : false;
 
-	$image_exts = array('jpg', 'jpeg', 'gif', 'png');
+	$image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' );
 
 	if ( 'image/' == substr($post->post_mime_type, 0, 6) || $ext && 'import' == $post->post_mime_type && in_array($ext, $image_exts) )
 		return true;
@@ -4051,12 +4308,13 @@
 function wp_mime_type_icon( $mime = 0 ) {
 	if ( !is_numeric($mime) )
 		$icon = wp_cache_get("mime_type_icon_$mime");
+
+	$post_id = 0;
 	if ( empty($icon) ) {
-		$post_id = 0;
 		$post_mimes = array();
 		if ( is_numeric($mime) ) {
 			$mime = (int) $mime;
-			if ( $post =& get_post( $mime ) ) {
+			if ( $post = get_post( $mime ) ) {
 				$post_id = (int) $post->ID;
 				$ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $post->guid);
 				if ( !empty($ext) ) {
@@ -4098,7 +4356,7 @@
 					closedir($dh);
 				}
 			}
-			wp_cache_set('icon_files', $icon_files, 600);
+			wp_cache_add( 'icon_files', $icon_files, 'default', 600 );
 		}
 
 		// Icon basename - extension = MIME wildcard
@@ -4118,7 +4376,7 @@
 			if ( isset($types[$wilds[0]])) {
 				$icon = $types[$wilds[0]];
 				if ( !is_numeric($mime) )
-					wp_cache_set("mime_type_icon_$mime", $icon);
+					wp_cache_add("mime_type_icon_$mime", $icon);
 				break;
 			}
 		}
@@ -4195,9 +4453,10 @@
  * @param string $post_type Post type.
  * @param bool $full Optional. Returns a full WHERE statement instead of just an 'andalso' term.
  * @param int $post_author Optional. Query posts having a single author ID.
+ * @param bool $public_only Optional. Only return public posts. Skips cap checks for $current_user.  Default is false.
  * @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 ) {
+function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) {
 	global $user_ID, $wpdb;
 
 	// Private posts
@@ -4221,18 +4480,21 @@
 
 	$sql .= "(post_status = 'publish'";
 
-	if ( current_user_can( $cap ) ) {
-		// Does the user have the capability to view private posts? Guess so.
-		$sql .= " OR post_status = 'private'";
-	} elseif ( is_user_logged_in() ) {
-		// Users can view their own private posts.
-		$id = (int) $user_ID;
-		if ( null === $post_author || ! $full ) {
-			$sql .= " OR post_status = 'private' AND post_author = $id";
-		} elseif ( $id == (int) $post_author ) {
+	// Only need to check the cap if $public_only is false
+	if ( false === $public_only ) {
+		if ( current_user_can( $cap ) ) {
+			// Does the user have the capability to view private posts? Guess so.
 			$sql .= " OR post_status = 'private'";
+		} elseif ( is_user_logged_in() ) {
+			// Users can view their own private posts.
+			$id = (int) $user_ID;
+			if ( null === $post_author || ! $full ) {
+				$sql .= " OR post_status = 'private' AND post_author = $id";
+			} elseif ( $id == (int) $post_author ) {
+				$sql .= " OR post_status = 'private'";
+			} // else none
 		} // else none
-	} // else none
+	}
 
 	$sql .= ')';
 
@@ -4351,8 +4613,6 @@
  * Cleaning means delete from the cache of the post. Will call to clean the term
  * object cache associated with the post ID.
  *
- * clean_post_cache() will call itself recursively for each child post.
- *
  * This function not run if $_wp_suspend_cache_invalidation is not empty. See
  * wp_suspend_cache_invalidation().
  *
@@ -4383,23 +4643,13 @@
 
 	do_action( 'clean_post_cache', $post->ID, $post );
 
+	if ( is_post_type_hierarchical( $post->post_type ) )
+		wp_cache_delete( 'get_pages', 'posts' );
+
 	if ( 'page' == $post->post_type ) {
 		wp_cache_delete( 'all_page_ids', 'posts' );
-		wp_cache_delete( 'get_pages', 'posts' );
 		do_action( 'clean_page_cache', $post->ID );
 	}
-
-	if ( $children = $wpdb->get_results( $wpdb->prepare("SELECT ID, post_type FROM $wpdb->posts WHERE post_parent = %d", $post->ID) ) ) {
-		foreach ( $children as $child ) {
-			// Loop detection
-			if ( $child->ID == $post->ID )
-				continue;
-			clean_post_cache( $child );
-		}
-	}
-
-	if ( is_multisite() )
-		wp_cache_delete( $wpdb->blogid . '-' . $post->ID, 'global-posts' );
 }
 
 /**
@@ -4430,8 +4680,8 @@
 	foreach ( $posts as $post )
 		$post_ids[] = $post->ID;
 
-	if ( empty($post_type) )
-		$post_type = 'post';
+	if ( ! $post_type )
+		$post_type = 'any';
 
 	if ( $update_term_cache ) {
 		if ( is_array($post_type) ) {
@@ -4571,9 +4821,8 @@
  * @since 2.3.0
  * @access private
  * @uses $wpdb
- * @uses XMLRPC_REQUEST and APP_REQUEST constants.
+ * @uses XMLRPC_REQUEST constant.
  * @uses do_action() Calls 'xmlprc_publish_post' on post ID if XMLRPC_REQUEST is defined.
- * @uses do_action() Calls 'app_publish_post' on post ID if APP_REQUEST is defined.
  *
  * @param int $post_id The ID in the database table of the post being published
  */
@@ -4582,8 +4831,6 @@
 
 	if ( defined('XMLRPC_REQUEST') )
 		do_action('xmlrpc_publish_post', $post_id);
-	if ( defined('APP_REQUEST') )
-		do_action('app_publish_post', $post_id);
 
 	if ( defined('WP_IMPORTING') )
 		return;
@@ -4596,58 +4843,6 @@
 }
 
 /**
- * Hook used to prevent page/post cache from staying dirty when a post is saved.
- *
- * @since 2.3.0
- * @access private
- *
- * @param int $post_id The ID in the database table for the $post
- * @param object $post Object type containing the post information
- */
-function _save_post_hook( $post_id, $post ) {
-	clean_post_cache( $post );
-}
-
-/**
- * Retrieve post ancestors and append to post ancestors property.
- *
- * Will only retrieve ancestors once, if property is already set, then nothing
- * will be done. If there is not a parent post, or post ID and post parent ID
- * are the same then nothing will be done.
- *
- * The parameter is passed by reference, so nothing needs to be returned. The
- * property will be updated and can be referenced after the function is
- * complete. The post parent will be an ancestor and the parent of the post
- * parent will be an ancestor. There will only be two ancestors at the most.
- *
- * @since 2.5.0
- * @access private
- * @uses $wpdb
- *
- * @param object $_post Post data.
- * @return null When nothing needs to be done.
- */
-function _get_post_ancestors(&$_post) {
-	global $wpdb;
-
-	if ( isset($_post->ancestors) )
-		return;
-
-	$_post->ancestors = array();
-
-	if ( empty($_post->post_parent) || $_post->ID == $_post->post_parent )
-		return;
-
-	$id = $_post->ancestors[] = (int) $_post->post_parent;
-	while ( $ancestor = $wpdb->get_var( $wpdb->prepare("SELECT `post_parent` FROM $wpdb->posts WHERE ID = %d LIMIT 1", $id) ) ) {
-		// Loop detection: If the ancestor has been seen before, break.
-		if ( ( $ancestor == $_post->ID ) || in_array($ancestor,  $_post->ancestors) )
-			break;
-		$id = $_post->ancestors[] = (int) $ancestor;
-	}
-}
-
-/**
  * Determines which fields of posts are to be saved in revisions.
  *
  * Does two things. If passed a post *array*, it will return a post array ready
@@ -4726,6 +4921,9 @@
 	if ( !$post = get_post( $post_id, ARRAY_A ) )
 		return;
 
+	if ( 'auto-draft' == $post['post_status'] )
+		return;
+
 	if ( !post_type_supports($post['post_type'], 'revisions') )
 		return;
 
@@ -4890,7 +5088,7 @@
  * @param string $filter Optional sanitation filter. @see sanitize_post()
  * @return mixed Null if error or post object if success
  */
-function &wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') {
+function wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') {
 	$null = null;
 	if ( !$revision = get_post( $post, OBJECT, $filter ) )
 		return $revision;
@@ -5166,10 +5364,10 @@
 	$post = get_post( $post );
 	$thumbnail_id = absint( $thumbnail_id );
 	if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {
-		$thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' );
-		if ( ! empty( $thumbnail_html ) ) {
+		if ( $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) )
 			return update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );
-		}
+		else
+			return delete_post_meta( $post->ID, '_thumbnail_id' );
 	}
 	return false;
 }