wp/wp-includes/revision.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
--- a/wp/wp-includes/revision.php	Tue Jun 09 11:14:17 2015 +0000
+++ b/wp/wp-includes/revision.php	Mon Oct 14 17:39:30 2019 +0200
@@ -9,64 +9,93 @@
 /**
  * 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 inserted into the posts table as a post revision. Otherwise, returns
- * an array whose keys are the post fields to be saved for post revisions.
- *
  * @since 2.6.0
+ * @since 4.5.0 A `WP_Post` object can now be passed to the `$post` parameter.
+ * @since 4.5.0 The optional `$autosave` parameter was deprecated and renamed to `$deprecated`.
  * @access private
  *
- * @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.
+ * @staticvar array $fields
+ *
+ * @param array|WP_Post $post       Optional. A post array or a WP_Post object being processed
+ *                                  for insertion as a post revision. Default empty array.
+ * @param bool          $deprecated Not used.
+ * @return array Array of fields that can be versioned.
  */
-function _wp_post_revision_fields( $post = null, $autosave = false ) {
-	static $fields = false;
+function _wp_post_revision_fields( $post = array(), $deprecated = false ) {
+	static $fields = null;
 
-	if ( !$fields ) {
+	if ( ! is_array( $post ) ) {
+		$post = get_post( $post, ARRAY_A );
+	}
+
+	if ( is_null( $fields ) ) {
 		// Allow these to be versioned
 		$fields = array(
 			'post_title' => __( 'Title' ),
 			'post_content' => __( 'Content' ),
 			'post_excerpt' => __( 'Excerpt' ),
 		);
+	}
 
-		/**
-		 * Filter the list of fields saved in post revisions.
-		 *
-		 * Included by default: 'post_title', 'post_content' and 'post_excerpt'.
-		 *
-		 * Disallowed fields: 'ID', 'post_name', 'post_parent', 'post_date',
-		 * 'post_date_gmt', 'post_status', 'post_type', 'comment_count',
-		 * and 'post_author'.
-		 *
-		 * @since 2.6.0
-		 *
-		 * @param array $fields List of fields to revision. Contains 'post_title',
-		 *                      'post_content', and 'post_excerpt' by default.
-		 */
-		$fields = apply_filters( '_wp_post_revision_fields', $fields );
+	/**
+	 * Filters the list of fields saved in post revisions.
+	 *
+	 * Included by default: 'post_title', 'post_content' and 'post_excerpt'.
+	 *
+	 * Disallowed fields: 'ID', 'post_name', 'post_parent', 'post_date',
+	 * 'post_date_gmt', 'post_status', 'post_type', 'comment_count',
+	 * and 'post_author'.
+	 *
+	 * @since 2.6.0
+	 * @since 4.5.0 The `$post` parameter was added.
+	 *
+	 * @param array $fields List of fields to revision. Contains 'post_title',
+	 *                      'post_content', and 'post_excerpt' by default.
+	 * @param array $post   A post array being processed for insertion as a post revision.
+	 */
+	$fields = apply_filters( '_wp_post_revision_fields', $fields, $post );
 
-		// 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] );
+	// 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 $fields;
+}
 
-	$return['post_parent']   = $post['ID'];
-	$return['post_status']   = 'inherit';
-	$return['post_type']     = 'revision';
-	$return['post_name']     = $autosave ? "$post[ID]-autosave-v1" : "$post[ID]-revision-v1"; // "1" is the revisioning system version
-	$return['post_date']     = isset($post['post_modified']) ? $post['post_modified'] : '';
-	$return['post_date_gmt'] = isset($post['post_modified_gmt']) ? $post['post_modified_gmt'] : '';
+/**
+ * Returns a post array ready to be inserted into the posts table as a post revision.
+ *
+ * @since 4.5.0
+ * @access private
+ *
+ * @param array|WP_Post $post     Optional. A post array or a WP_Post object to be processed
+ *                                for insertion as a post revision. Default empty array.
+ * @param bool          $autosave Optional. Is the revision an autosave? Default false.
+ * @return array Post array ready to be inserted as a post revision.
+ */
+function _wp_post_revision_data( $post = array(), $autosave = false ) {
+	if ( ! is_array( $post ) ) {
+		$post = get_post( $post, ARRAY_A );
+	}
 
-	return $return;
+	$fields = _wp_post_revision_fields( $post );
+
+	$revision_data = array();
+
+	foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field ) {
+		$revision_data[ $field ] = $post[ $field ];
+	}
+
+	$revision_data['post_parent']   = $post['ID'];
+	$revision_data['post_status']   = 'inherit';
+	$revision_data['post_type']     = 'revision';
+	$revision_data['post_name']     = $autosave ? "$post[ID]-autosave-v1" : "$post[ID]-revision-v1"; // "1" is the revisioning system version
+	$revision_data['post_date']     = isset( $post['post_modified'] ) ? $post['post_modified'] : '';
+	$revision_data['post_date_gmt'] = isset( $post['post_modified_gmt'] ) ? $post['post_modified_gmt'] : '';
+
+	return $revision_data;
 }
 
 /**
@@ -77,8 +106,8 @@
  *
  * @since 2.6.0
  *
- * @param  int $post_id The ID of the post to save as a revision.
- * @return null|int Null or 0 if error, new revision ID, if success.
+ * @param int $post_id The ID of the post to save as a revision.
+ * @return int|WP_Error|void Void or 0 if error, new revision ID, if success.
  */
 function wp_save_post_revision( $post_id ) {
 	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
@@ -109,7 +138,7 @@
 		}
 
 		/**
-		 * Filter whether the post has changed since the last revision.
+		 * Filters whether the post has changed since the last revision.
 		 *
 		 * By default a revision is saved only if one of the revisioned fields has changed.
 		 * This filter can override that so a revision is saved even if nothing has changed.
@@ -118,14 +147,14 @@
 		 *
 		 * @param bool    $check_for_changes Whether to check for changes before saving a new revision.
 		 *                                   Default true.
-		 * @param WP_Post $last_revision     The the last revision post object.
+		 * @param WP_Post $last_revision     The last revision post object.
 		 * @param WP_Post $post              The post object.
 		 *
 		 */
 		if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', $check_for_changes = true, $last_revision, $post ) ) {
 			$post_has_changed = false;
 
-			foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
+			foreach ( array_keys( _wp_post_revision_fields( $post ) ) as $field ) {
 				if ( normalize_whitespace( $post->$field ) != normalize_whitespace( $last_revision->$field ) ) {
 					$post_has_changed = true;
 					break;
@@ -133,7 +162,7 @@
 			}
 
 			/**
-			 * Filter whether a post has changed.
+			 * Filters whether a post has changed.
 			 *
 			 * By default a revision is saved only if one of the revisioned fields has changed.
 			 * This filter allows for additional checks to determine if there were changes.
@@ -148,7 +177,7 @@
 			$post_has_changed = (bool) apply_filters( 'wp_save_post_revision_post_has_changed', $post_has_changed, $last_revision, $post );
 
 			//don't save revision if post unchanged
-			if( ! $post_has_changed ) {
+			if ( ! $post_has_changed ) {
 				return;
 			}
 		}
@@ -192,8 +221,8 @@
  * @since 2.6.0
  *
  * @param int $post_id The post ID.
- * @param int $user_id optional The post author ID.
- * @return object|bool The autosaved data or false on failure or when no autosave exists.
+ * @param int $user_id Optional The post author ID.
+ * @return WP_Post|false The autosaved data or false on failure or when no autosave exists.
  */
 function wp_get_post_autosave( $post_id, $user_id = 0 ) {
 	$revisions = wp_get_post_revisions( $post_id, array( 'check_enabled' => false ) );
@@ -215,8 +244,8 @@
  *
  * @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.
+ * @param int|WP_Post $post Post ID or post object.
+ * @return false|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 ) )
@@ -230,8 +259,8 @@
  *
  * @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
+ * @param int|WP_Post $post Post ID or post object.
+ * @return false|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 ) )
@@ -249,9 +278,9 @@
  * @since 2.6.0
  * @access private
  *
- * @param int|object|array $post Post ID, post object OR post array.
- * @param bool $autosave Optional. Is the revision an autosave?
- * @return mixed WP_Error or 0 if error, new revision ID if success.
+ * @param int|WP_Post|array|null $post     Post ID, post object OR post array.
+ * @param bool                   $autosave Optional. Is the revision an autosave?
+ * @return int|WP_Error WP_Error or 0 if error, new revision ID if success.
  */
 function _wp_put_post_revision( $post = null, $autosave = false ) {
 	if ( is_object($post) )
@@ -260,12 +289,12 @@
 		$post = get_post($post, ARRAY_A);
 
 	if ( ! $post || empty($post['ID']) )
-		return new WP_Error( 'invalid_post', __( 'Invalid post ID' ) );
+		return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );
 
 	if ( isset($post['post_type']) && 'revision' == $post['post_type'] )
 		return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
 
-	$post = _wp_post_revision_fields( $post, $autosave );
+	$post = _wp_post_revision_data( $post, $autosave );
 	$post = wp_slash($post); //since data is from db
 
 	$revision_id = wp_insert_post( $post );
@@ -291,10 +320,11 @@
  *
  * @since 2.6.0
  *
- * @param int|object $post The post ID or object.
- * @param string $output Optional. OBJECT, ARRAY_A, or ARRAY_N.
- * @param string $filter Optional sanitation filter. @see sanitize_post().
- * @return mixed Null if error or post object if success.
+ * @param int|WP_Post $post   The post ID or object.
+ * @param string      $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
+ *                            a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT.
+ * @param string      $filter Optional sanitation filter. See sanitize_post().
+ * @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
  */
 function wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') {
 	if ( !$revision = get_post( $post, OBJECT, $filter ) )
@@ -322,19 +352,19 @@
  *
  * @since 2.6.0
  *
- * @param int|object $revision_id Revision ID or revision object.
- * @param array $fields Optional. What fields to restore from. Defaults to all.
- * @return mixed Null if error, false if no fields to restore, (int) post ID if success.
+ * @param int|WP_Post $revision_id Revision ID or revision object.
+ * @param array       $fields      Optional. What fields to restore from. Defaults to all.
+ * @return int|false|null Null if error, false if no fields to restore, (int) post ID if success.
  */
 function wp_restore_post_revision( $revision_id, $fields = null ) {
 	if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) )
 		return $revision;
 
 	if ( !is_array( $fields ) )
-		$fields = array_keys( _wp_post_revision_fields() );
+		$fields = array_keys( _wp_post_revision_fields( $revision ) );
 
 	$update = array();
-	foreach( array_intersect( array_keys( $revision ), $fields ) as $field ) {
+	foreach ( array_intersect( array_keys( $revision ), $fields ) as $field ) {
 		$update[$field] = $revision[$field];
 	}
 
@@ -349,14 +379,6 @@
 	if ( ! $post_id || is_wp_error( $post_id ) )
 		return $post_id;
 
-	// Add restore from details
-	$restore_details = array(
-		'restored_revision_id' => $revision_id,
-		'restored_by_user'     => get_current_user_id(),
-		'restored_time'        => time()
-	);
-	update_post_meta( $post_id, '_post_restored_from', $restore_details );
-
 	// Update last edit user
 	update_post_meta( $post_id, '_edit_last', get_current_user_id() );
 
@@ -380,17 +402,15 @@
  *
  * @since 2.6.0
  *
- * @param int|object $revision_id Revision ID or revision object.
- * @return mixed Null or WP_Error if error, deleted post if success.
+ * @param int|WP_Post $revision_id Revision ID or revision object.
+ * @return array|false|WP_Post|WP_Error|null Null or WP_Error if error, deleted post if success.
  */
 function wp_delete_post_revision( $revision_id ) {
-	if ( !$revision = wp_get_post_revision( $revision_id ) )
+	if ( ! $revision = wp_get_post_revision( $revision_id ) ) {
 		return $revision;
+	}
 
 	$delete = wp_delete_post( $revision->ID );
-	if ( is_wp_error( $delete ) )
-		return $delete;
-
 	if ( $delete ) {
 		/**
 		 * Fires once a post revision has been deleted.
@@ -411,7 +431,10 @@
  *
  * @since 2.6.0
  *
- * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
+ * @see get_children()
+ *
+ * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global `$post`.
+ * @param array|null  $args    Optional. Arguments for retrieving post revisions. Default null.
  * @return array An array of revisions, or an empty array if none.
  */
 function wp_get_post_revisions( $post_id = 0, $args = null ) {
@@ -442,7 +465,7 @@
  * @return bool True if number of revisions to keep isn't zero, false otherwise.
  */
 function wp_revisions_enabled( $post ) {
-	return wp_revisions_to_keep( $post ) != 0;
+	return wp_revisions_to_keep( $post ) !== 0;
 }
 
 /**
@@ -470,7 +493,7 @@
 		$num = 0;
 
 	/**
-	 * Filter the number of revisions to save for the given post.
+	 * Filters the number of revisions to save for the given post.
 	 *
 	 * Overrides the value of WP_POST_REVISIONS.
 	 *
@@ -487,24 +510,28 @@
  *
  * @since 2.7.0
  * @access private
+ *
+ * @param WP_Post $post
+ * @return WP_Post|false
  */
-function _set_preview($post) {
-
-	if ( ! is_object($post) )
+function _set_preview( $post ) {
+	if ( ! is_object( $post ) ) {
 		return $post;
+	}
 
-	$preview = wp_get_post_autosave($post->ID);
+	$preview = wp_get_post_autosave( $post->ID );
+	if ( ! is_object( $preview ) ) {
+		return $post;
+	}
 
-	if ( ! is_object($preview) )
-		return $post;
-
-	$preview = sanitize_post($preview);
+	$preview = sanitize_post( $preview );
 
 	$post->post_content = $preview->post_content;
 	$post->post_title = $preview->post_title;
 	$post->post_excerpt = $preview->post_excerpt;
 
 	add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 );
+	add_filter( 'get_post_metadata', '_wp_preview_post_thumbnail_filter', 10, 3 );
 
 	return $post;
 }
@@ -516,12 +543,11 @@
  * @access private
  */
 function _show_post_preview() {
-
 	if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) {
 		$id = (int) $_GET['preview_id'];
 
-		if ( false == wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) )
-			wp_die( __('You do not have permission to preview drafts.') );
+		if ( false === wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) )
+			wp_die( __('Sorry, you are not allowed to preview drafts.') );
 
 		add_filter('the_preview', '_set_preview');
 	}
@@ -532,6 +558,11 @@
  *
  * @since 3.6.0
  * @access private
+ *
+ * @param array  $terms
+ * @param int    $post_id
+ * @param string $taxonomy
+ * @return array
  */
 function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) {
 	if ( ! $post = get_post() )
@@ -549,11 +580,48 @@
 }
 
 /**
+ * Filters post thumbnail lookup to set the post thumbnail.
+ *
+ * @since 4.6.0
+ * @access private
+ *
+ * @param null|array|string $value    The value to return - a single metadata value, or an array of values.
+ * @param int               $post_id  Post ID.
+ * @param string            $meta_key Meta key.
+ * @return null|array The default return value or the post thumbnail meta array.
+ */
+function _wp_preview_post_thumbnail_filter( $value, $post_id, $meta_key ) {
+	if ( ! $post = get_post() ) {
+		return $value;
+	}
+
+	if ( empty( $_REQUEST['_thumbnail_id'] ) ||
+	     empty( $_REQUEST['preview_id'] ) ||
+	     $post->ID != $post_id ||
+	     '_thumbnail_id' != $meta_key ||
+	     'revision' == $post->post_type ||
+	     $post_id != $_REQUEST['preview_id']
+	) {
+		return $value;
+	}
+
+	$thumbnail_id = intval( $_REQUEST['_thumbnail_id'] );
+	if ( $thumbnail_id <= 0 ) {
+		return '';
+	}
+
+	return strval( $thumbnail_id );
+}
+
+/**
  * Gets the post revision version.
  *
  * @since 3.6.0
  * @access private
-*/
+ *
+ * @param WP_Post $revision
+ * @return int|false
+ */
 function _wp_get_post_revision_version( $revision ) {
 	if ( is_object( $revision ) )
 		$revision = get_object_vars( $revision );
@@ -572,8 +640,10 @@
  * @since 3.6.0
  * @access private
  *
- * @param WP_Post $post Post object
- * @param array $revisions Current revisions of the post
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param WP_Post $post      Post object
+ * @param array   $revisions Current revisions of the post
  * @return bool true if the revisions were upgraded, false if problems
  */
 function _wp_upgrade_revisions_of_post( $post, $revisions ) {