-/**
- * 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[] = $_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[] = $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
- * to be insterted into the posts table as a post revision. Otherwise, returns
- * an array whose keys are the post fields to be saved for post revisions.
- *
- * @package WordPress
- * @subpackage Post_Revisions
- * @since 2.6.0
- * @access private
- * @uses apply_filters() Calls '_wp_post_revision_fields' on 'title', 'content' and 'excerpt' fields.
- *
- * @param array $post Optional a post array to be processed for insertion as a post revision.
- * @param bool $autosave optional Is the revision an autosave?
- * @return array Post array ready to be inserted as a post revision or array of fields that can be versioned.
- */
-function _wp_post_revision_fields( $post = null, $autosave = false ) {
- static $fields = false;
-
- if ( !$fields ) {
- // Allow these to be versioned
- $fields = array(
- 'post_title' => __( 'Title' ),
- 'post_content' => __( 'Content' ),
- 'post_excerpt' => __( 'Excerpt' ),
- );
-
- // Runs only once
- $fields = apply_filters( '_wp_post_revision_fields', $fields );
-
- // WP uses these internally either in versioning or elsewhere - they cannot be versioned
- foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect )
- unset( $fields[$protect] );
- }
-
- if ( !is_array($post) )
- return $fields;
-
- $return = array();
- foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field )
- $return[$field] = $post[$field];
-
- $return['post_parent'] = $post['ID'];
- $return['post_status'] = 'inherit';
- $return['post_type'] = 'revision';
- $return['post_name'] = $autosave ? "$post[ID]-autosave" : "$post[ID]-revision";
- $return['post_date'] = isset($post['post_modified']) ? $post['post_modified'] : '';
- $return['post_date_gmt'] = isset($post['post_modified_gmt']) ? $post['post_modified_gmt'] : '';
-
- return $return;
-}
-
-/**
- * Saves an already existing post as a post revision.
- *
- * Typically used immediately prior to post updates.
- *
- * @package WordPress
- * @subpackage Post_Revisions
- * @since 2.6.0
- *
- * @uses _wp_put_post_revision()
- *
- * @param int $post_id The ID of the post to save as a revision.
- * @return mixed Null or 0 if error, new revision ID, if success.
- */
-function wp_save_post_revision( $post_id ) {
- // We do autosaves manually with wp_create_post_autosave()
- if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
- return;
-
- // WP_POST_REVISIONS = 0, false
- if ( ! WP_POST_REVISIONS )
- return;
-
- if ( !$post = get_post( $post_id, ARRAY_A ) )
- return;
-
- if ( !post_type_supports($post['post_type'], 'revisions') )
- return;
-
- $return = _wp_put_post_revision( $post );
-
- // WP_POST_REVISIONS = true (default), -1
- if ( !is_numeric( WP_POST_REVISIONS ) || WP_POST_REVISIONS < 0 )
- return $return;
-
- // all revisions and (possibly) one autosave
- $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) );
-
- // WP_POST_REVISIONS = (int) (# of autosaves to save)
- $delete = count($revisions) - WP_POST_REVISIONS;
-
- if ( $delete < 1 )
- return $return;
-
- $revisions = array_slice( $revisions, 0, $delete );
-
- for ( $i = 0; isset($revisions[$i]); $i++ ) {
- if ( false !== strpos( $revisions[$i]->post_name, 'autosave' ) )
- continue;
- wp_delete_post_revision( $revisions[$i]->ID );
- }
-
- return $return;
-}
-
-/**
- * Retrieve the autosaved data of the specified post.
- *
- * Returns a post object containing the information that was autosaved for the
- * specified post.
- *
- * @package WordPress
- * @subpackage Post_Revisions
- * @since 2.6.0
- *
- * @param int $post_id The post ID.
- * @return object|bool The autosaved data or false on failure or when no autosave exists.
- */
-function wp_get_post_autosave( $post_id ) {
-
- if ( !$post = get_post( $post_id ) )
- return false;
-
- $q = array(
- 'name' => "{$post->ID}-autosave",
- 'post_parent' => $post->ID,
- 'post_type' => 'revision',
- 'post_status' => 'inherit'
- );
-
- // Use WP_Query so that the result gets cached
- $autosave_query = new WP_Query;
-
- add_action( 'parse_query', '_wp_get_post_autosave_hack' );
- $autosave = $autosave_query->query( $q );
- remove_action( 'parse_query', '_wp_get_post_autosave_hack' );
-
- if ( $autosave && is_array($autosave) && is_object($autosave[0]) )
- return $autosave[0];
-
- return false;
-}
-
-/**
- * Internally used to hack WP_Query into submission.
- *
- * @package WordPress
- * @subpackage Post_Revisions
- * @since 2.6.0
- *
- * @param object $query WP_Query object
- */
-function _wp_get_post_autosave_hack( $query ) {
- $query->is_single = false;
-}
-
-/**
- * Determines if the specified post is a revision.
- *
- * @package WordPress
- * @subpackage Post_Revisions
- * @since 2.6.0
- *
- * @param int|object $post Post ID or post object.
- * @return bool|int False if not a revision, ID of revision's parent otherwise.
- */
-function wp_is_post_revision( $post ) {
- if ( !$post = wp_get_post_revision( $post ) )
- return false;
- return (int) $post->post_parent;
-}
-
-/**
- * Determines if the specified post is an autosave.
- *
- * @package WordPress
- * @subpackage Post_Revisions
- * @since 2.6.0
- *
- * @param int|object $post Post ID or post object.
- * @return bool|int False if not a revision, ID of autosave's parent otherwise
- */
-function wp_is_post_autosave( $post ) {
- if ( !$post = wp_get_post_revision( $post ) )
- return false;
- if ( "{$post->post_parent}-autosave" !== $post->post_name )
- return false;
- return (int) $post->post_parent;
-}
-
-/**
- * Inserts post data into the posts table as a post revision.
- *
- * @package WordPress
- * @subpackage Post_Revisions
- * @since 2.6.0
- *
- * @uses wp_insert_post()
- *
- * @param int|object|array $post Post ID, post object OR post array.
- * @param bool $autosave Optional. Is the revision an autosave?
- * @return mixed Null or 0 if error, new revision ID if success.
- */
-function _wp_put_post_revision( $post = null, $autosave = false ) {
- if ( is_object($post) )
- $post = get_object_vars( $post );
- elseif ( !is_array($post) )
- $post = get_post($post, ARRAY_A);
- if ( !$post || empty($post['ID']) )