wp/wp-includes/revision.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    11  *
    11  *
    12  * @since 2.6.0
    12  * @since 2.6.0
    13  * @since 4.5.0 A `WP_Post` object can now be passed to the `$post` parameter.
    13  * @since 4.5.0 A `WP_Post` object can now be passed to the `$post` parameter.
    14  * @since 4.5.0 The optional `$autosave` parameter was deprecated and renamed to `$deprecated`.
    14  * @since 4.5.0 The optional `$autosave` parameter was deprecated and renamed to `$deprecated`.
    15  * @access private
    15  * @access private
    16  *
       
    17  * @staticvar array $fields
       
    18  *
    16  *
    19  * @param array|WP_Post $post       Optional. A post array or a WP_Post object being processed
    17  * @param array|WP_Post $post       Optional. A post array or a WP_Post object being processed
    20  *                                  for insertion as a post revision. Default empty array.
    18  *                                  for insertion as a post revision. Default empty array.
    21  * @param bool          $deprecated Not used.
    19  * @param bool          $deprecated Not used.
    22  * @return array Array of fields that can be versioned.
    20  * @return array Array of fields that can be versioned.
    27 	if ( ! is_array( $post ) ) {
    25 	if ( ! is_array( $post ) ) {
    28 		$post = get_post( $post, ARRAY_A );
    26 		$post = get_post( $post, ARRAY_A );
    29 	}
    27 	}
    30 
    28 
    31 	if ( is_null( $fields ) ) {
    29 	if ( is_null( $fields ) ) {
    32 		// Allow these to be versioned
    30 		// Allow these to be versioned.
    33 		$fields = array(
    31 		$fields = array(
    34 			'post_title'   => __( 'Title' ),
    32 			'post_title'   => __( 'Title' ),
    35 			'post_content' => __( 'Content' ),
    33 			'post_content' => __( 'Content' ),
    36 			'post_excerpt' => __( 'Excerpt' ),
    34 			'post_excerpt' => __( 'Excerpt' ),
    37 		);
    35 		);
    53 	 *                      'post_content', and 'post_excerpt' by default.
    51 	 *                      'post_content', and 'post_excerpt' by default.
    54 	 * @param array $post   A post array being processed for insertion as a post revision.
    52 	 * @param array $post   A post array being processed for insertion as a post revision.
    55 	 */
    53 	 */
    56 	$fields = apply_filters( '_wp_post_revision_fields', $fields, $post );
    54 	$fields = apply_filters( '_wp_post_revision_fields', $fields, $post );
    57 
    55 
    58 	// WP uses these internally either in versioning or elsewhere - they cannot be versioned
    56 	// WP uses these internally either in versioning or elsewhere - they cannot be versioned.
    59 	foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect ) {
    57 	foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect ) {
    60 		unset( $fields[ $protect ] );
    58 		unset( $fields[ $protect ] );
    61 	}
    59 	}
    62 
    60 
    63 	return $fields;
    61 	return $fields;
    88 	}
    86 	}
    89 
    87 
    90 	$revision_data['post_parent']   = $post['ID'];
    88 	$revision_data['post_parent']   = $post['ID'];
    91 	$revision_data['post_status']   = 'inherit';
    89 	$revision_data['post_status']   = 'inherit';
    92 	$revision_data['post_type']     = 'revision';
    90 	$revision_data['post_type']     = 'revision';
    93 	$revision_data['post_name']     = $autosave ? "$post[ID]-autosave-v1" : "$post[ID]-revision-v1"; // "1" is the revisioning system version
    91 	$revision_data['post_name']     = $autosave ? "$post[ID]-autosave-v1" : "$post[ID]-revision-v1"; // "1" is the revisioning system version.
    94 	$revision_data['post_date']     = isset( $post['post_modified'] ) ? $post['post_modified'] : '';
    92 	$revision_data['post_date']     = isset( $post['post_modified'] ) ? $post['post_modified'] : '';
    95 	$revision_data['post_date_gmt'] = isset( $post['post_modified_gmt'] ) ? $post['post_modified_gmt'] : '';
    93 	$revision_data['post_date_gmt'] = isset( $post['post_modified_gmt'] ) ? $post['post_modified_gmt'] : '';
    96 
    94 
    97 	return $revision_data;
    95 	return $revision_data;
    98 }
    96 }
   111 function wp_save_post_revision( $post_id ) {
   109 function wp_save_post_revision( $post_id ) {
   112 	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
   110 	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
   113 		return;
   111 		return;
   114 	}
   112 	}
   115 
   113 
   116 	if ( ! $post = get_post( $post_id ) ) {
   114 	$post = get_post( $post_id );
       
   115 	if ( ! $post ) {
   117 		return;
   116 		return;
   118 	}
   117 	}
   119 
   118 
   120 	if ( ! post_type_supports( $post->post_type, 'revisions' ) ) {
   119 	if ( ! post_type_supports( $post->post_type, 'revisions' ) ) {
   121 		return;
   120 		return;
   122 	}
   121 	}
   123 
   122 
   124 	if ( 'auto-draft' == $post->post_status ) {
   123 	if ( 'auto-draft' === $post->post_status ) {
   125 		return;
   124 		return;
   126 	}
   125 	}
   127 
   126 
   128 	if ( ! wp_revisions_enabled( $post ) ) {
   127 	if ( ! wp_revisions_enabled( $post ) ) {
   129 		return;
   128 		return;
   130 	}
   129 	}
   131 
   130 
   132 	// Compare the proposed update with the last stored revision verifying that
   131 	/*
   133 	// they are different, unless a plugin tells us to always save regardless.
   132 	 * Compare the proposed update with the last stored revision verifying that
   134 	// If no previous revisions, save one
   133 	 * they are different, unless a plugin tells us to always save regardless.
   135 	if ( $revisions = wp_get_post_revisions( $post_id ) ) {
   134 	 * If no previous revisions, save one.
   136 		// grab the last revision, but not an autosave
   135 	 */
       
   136 	$revisions = wp_get_post_revisions( $post_id );
       
   137 	if ( $revisions ) {
       
   138 		// Grab the last revision, but not an autosave.
   137 		foreach ( $revisions as $revision ) {
   139 		foreach ( $revisions as $revision ) {
   138 			if ( false !== strpos( $revision->post_name, "{$revision->post_parent}-revision" ) ) {
   140 			if ( false !== strpos( $revision->post_name, "{$revision->post_parent}-revision" ) ) {
   139 				$last_revision = $revision;
   141 				$last_revision = $revision;
   140 				break;
   142 				break;
   141 			}
   143 			}
   152 		 * @param bool    $check_for_changes Whether to check for changes before saving a new revision.
   154 		 * @param bool    $check_for_changes Whether to check for changes before saving a new revision.
   153 		 *                                   Default true.
   155 		 *                                   Default true.
   154 		 * @param WP_Post $last_revision     The last revision post object.
   156 		 * @param WP_Post $last_revision     The last revision post object.
   155 		 * @param WP_Post $post              The post object.
   157 		 * @param WP_Post $post              The post object.
   156 		 */
   158 		 */
   157 		if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', $check_for_changes = true, $last_revision, $post ) ) {
   159 		if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', true, $last_revision, $post ) ) {
   158 			$post_has_changed = false;
   160 			$post_has_changed = false;
   159 
   161 
   160 			foreach ( array_keys( _wp_post_revision_fields( $post ) ) as $field ) {
   162 			foreach ( array_keys( _wp_post_revision_fields( $post ) ) as $field ) {
   161 				if ( normalize_whitespace( $post->$field ) != normalize_whitespace( $last_revision->$field ) ) {
   163 				if ( normalize_whitespace( $post->$field ) !== normalize_whitespace( $last_revision->$field ) ) {
   162 					$post_has_changed = true;
   164 					$post_has_changed = true;
   163 					break;
   165 					break;
   164 				}
   166 				}
   165 			}
   167 			}
   166 
   168 
   176 			 * @param WP_Post $last_revision    The last revision post object.
   178 			 * @param WP_Post $last_revision    The last revision post object.
   177 			 * @param WP_Post $post             The post object.
   179 			 * @param WP_Post $post             The post object.
   178 			 */
   180 			 */
   179 			$post_has_changed = (bool) apply_filters( 'wp_save_post_revision_post_has_changed', $post_has_changed, $last_revision, $post );
   181 			$post_has_changed = (bool) apply_filters( 'wp_save_post_revision_post_has_changed', $post_has_changed, $last_revision, $post );
   180 
   182 
   181 			//don't save revision if post unchanged
   183 			// Don't save revision if post unchanged.
   182 			if ( ! $post_has_changed ) {
   184 			if ( ! $post_has_changed ) {
   183 				return;
   185 				return;
   184 			}
   186 			}
   185 		}
   187 		}
   186 	}
   188 	}
   217 }
   219 }
   218 
   220 
   219 /**
   221 /**
   220  * Retrieve the autosaved data of the specified post.
   222  * Retrieve the autosaved data of the specified post.
   221  *
   223  *
   222  * Returns a post object containing the information that was autosaved for the
   224  * Returns a post object with the information that was autosaved for the specified post.
   223  * specified post. If the optional $user_id is passed, returns the autosave for that user
   225  * If the optional $user_id is passed, returns the autosave for that user, otherwise
   224  * otherwise returns the latest autosave.
   226  * returns the latest autosave.
   225  *
   227  *
   226  * @since 2.6.0
   228  * @since 2.6.0
   227  *
   229  *
   228  * @param int $post_id The post ID.
   230  * @param int $post_id The post ID.
   229  * @param int $user_id Optional The post author ID.
   231  * @param int $user_id Optional The post author ID.
   230  * @return WP_Post|false The autosaved data or false on failure or when no autosave exists.
   232  * @return WP_Post|false The autosaved data or false on failure or when no autosave exists.
   231  */
   233  */
   232 function wp_get_post_autosave( $post_id, $user_id = 0 ) {
   234 function wp_get_post_autosave( $post_id, $user_id = 0 ) {
   233 	$revisions = wp_get_post_revisions( $post_id, array( 'check_enabled' => false ) );
   235 	global $wpdb;
   234 
   236 
   235 	foreach ( $revisions as $revision ) {
   237 	$autosave_name = $post_id . '-autosave-v1';
   236 		if ( false !== strpos( $revision->post_name, "{$post_id}-autosave" ) ) {
   238 	$user_id_query = ( 0 !== $user_id ) ? "AND post_author = $user_id" : null;
   237 			if ( $user_id && $user_id != $revision->post_author ) {
   239 
   238 				continue;
   240 	// Construct the autosave query.
   239 			}
   241 	$autosave_query = "
   240 
   242 		SELECT *
   241 			return $revision;
   243 		FROM $wpdb->posts
   242 		}
   244 		WHERE post_parent = %d
   243 	}
   245 		AND post_type = 'revision'
   244 
   246 		AND post_status = 'inherit'
   245 	return false;
   247 		AND post_name   = %s " . $user_id_query . '
       
   248 		ORDER BY post_date DESC
       
   249 		LIMIT 1';
       
   250 
       
   251 	$autosave = $wpdb->get_results(
       
   252 		$wpdb->prepare(
       
   253 			$autosave_query,
       
   254 			$post_id,
       
   255 			$autosave_name
       
   256 		)
       
   257 	);
       
   258 
       
   259 	if ( ! $autosave ) {
       
   260 		return false;
       
   261 	}
       
   262 
       
   263 	return get_post( $autosave[0] );
   246 }
   264 }
   247 
   265 
   248 /**
   266 /**
   249  * Determines if the specified post is a revision.
   267  * Determines if the specified post is a revision.
   250  *
   268  *
   251  * @since 2.6.0
   269  * @since 2.6.0
   252  *
   270  *
   253  * @param int|WP_Post $post Post ID or post object.
   271  * @param int|WP_Post $post Post ID or post object.
   254  * @return false|int False if not a revision, ID of revision's parent otherwise.
   272  * @return int|false ID of revision's parent on success, false if not a revision.
   255  */
   273  */
   256 function wp_is_post_revision( $post ) {
   274 function wp_is_post_revision( $post ) {
   257 	if ( ! $post = wp_get_post_revision( $post ) ) {
   275 	$post = wp_get_post_revision( $post );
       
   276 	if ( ! $post ) {
   258 		return false;
   277 		return false;
   259 	}
   278 	}
   260 
   279 
   261 	return (int) $post->post_parent;
   280 	return (int) $post->post_parent;
   262 }
   281 }
   265  * Determines if the specified post is an autosave.
   284  * Determines if the specified post is an autosave.
   266  *
   285  *
   267  * @since 2.6.0
   286  * @since 2.6.0
   268  *
   287  *
   269  * @param int|WP_Post $post Post ID or post object.
   288  * @param int|WP_Post $post Post ID or post object.
   270  * @return false|int False if not a revision, ID of autosave's parent otherwise
   289  * @return int|false ID of autosave's parent on success, false if not a revision.
   271  */
   290  */
   272 function wp_is_post_autosave( $post ) {
   291 function wp_is_post_autosave( $post ) {
   273 	if ( ! $post = wp_get_post_revision( $post ) ) {
   292 	$post = wp_get_post_revision( $post );
       
   293 	if ( ! $post ) {
   274 		return false;
   294 		return false;
   275 	}
   295 	}
   276 
   296 
   277 	if ( false !== strpos( $post->post_name, "{$post->post_parent}-autosave" ) ) {
   297 	if ( false !== strpos( $post->post_name, "{$post->post_parent}-autosave" ) ) {
   278 		return (int) $post->post_parent;
   298 		return (int) $post->post_parent;
   300 
   320 
   301 	if ( ! $post || empty( $post['ID'] ) ) {
   321 	if ( ! $post || empty( $post['ID'] ) ) {
   302 		return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );
   322 		return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );
   303 	}
   323 	}
   304 
   324 
   305 	if ( isset( $post['post_type'] ) && 'revision' == $post['post_type'] ) {
   325 	if ( isset( $post['post_type'] ) && 'revision' === $post['post_type'] ) {
   306 		return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
   326 		return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
   307 	}
   327 	}
   308 
   328 
   309 	$post = _wp_post_revision_data( $post, $autosave );
   329 	$post = _wp_post_revision_data( $post, $autosave );
   310 	$post = wp_slash( $post ); //since data is from db
   330 	$post = wp_slash( $post ); // Since data is from DB.
   311 
   331 
   312 	$revision_id = wp_insert_post( $post );
   332 	$revision_id = wp_insert_post( $post );
   313 	if ( is_wp_error( $revision_id ) ) {
   333 	if ( is_wp_error( $revision_id ) ) {
   314 		return $revision_id;
   334 		return $revision_id;
   315 	}
   335 	}
   332  * Gets a post revision.
   352  * Gets a post revision.
   333  *
   353  *
   334  * @since 2.6.0
   354  * @since 2.6.0
   335  *
   355  *
   336  * @param int|WP_Post $post   The post ID or object.
   356  * @param int|WP_Post $post   The post ID or object.
   337  * @param string      $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
   357  * @param string      $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
   338  *                            a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT.
   358  *                            correspond to a WP_Post object, an associative array, or a numeric array,
       
   359  *                            respectively. Default OBJECT.
   339  * @param string      $filter Optional sanitation filter. See sanitize_post().
   360  * @param string      $filter Optional sanitation filter. See sanitize_post().
   340  * @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
   361  * @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
   341  */
   362  */
   342 function wp_get_post_revision( &$post, $output = OBJECT, $filter = 'raw' ) {
   363 function wp_get_post_revision( &$post, $output = OBJECT, $filter = 'raw' ) {
   343 	if ( ! $revision = get_post( $post, OBJECT, $filter ) ) {
   364 	$revision = get_post( $post, OBJECT, $filter );
       
   365 	if ( ! $revision ) {
   344 		return $revision;
   366 		return $revision;
   345 	}
   367 	}
   346 	if ( 'revision' !== $revision->post_type ) {
   368 	if ( 'revision' !== $revision->post_type ) {
   347 		return null;
   369 		return null;
   348 	}
   370 	}
   349 
   371 
   350 	if ( $output == OBJECT ) {
   372 	if ( OBJECT == $output ) {
   351 		return $revision;
   373 		return $revision;
   352 	} elseif ( $output == ARRAY_A ) {
   374 	} elseif ( ARRAY_A == $output ) {
   353 		$_revision = get_object_vars( $revision );
   375 		$_revision = get_object_vars( $revision );
   354 		return $_revision;
   376 		return $_revision;
   355 	} elseif ( $output == ARRAY_N ) {
   377 	} elseif ( ARRAY_N == $output ) {
   356 		$_revision = array_values( get_object_vars( $revision ) );
   378 		$_revision = array_values( get_object_vars( $revision ) );
   357 		return $_revision;
   379 		return $_revision;
   358 	}
   380 	}
   359 
   381 
   360 	return $revision;
   382 	return $revision;
   370  * @param int|WP_Post $revision_id Revision ID or revision object.
   392  * @param int|WP_Post $revision_id Revision ID or revision object.
   371  * @param array       $fields      Optional. What fields to restore from. Defaults to all.
   393  * @param array       $fields      Optional. What fields to restore from. Defaults to all.
   372  * @return int|false|null Null if error, false if no fields to restore, (int) post ID if success.
   394  * @return int|false|null Null if error, false if no fields to restore, (int) post ID if success.
   373  */
   395  */
   374 function wp_restore_post_revision( $revision_id, $fields = null ) {
   396 function wp_restore_post_revision( $revision_id, $fields = null ) {
   375 	if ( ! $revision = wp_get_post_revision( $revision_id, ARRAY_A ) ) {
   397 	$revision = wp_get_post_revision( $revision_id, ARRAY_A );
       
   398 	if ( ! $revision ) {
   376 		return $revision;
   399 		return $revision;
   377 	}
   400 	}
   378 
   401 
   379 	if ( ! is_array( $fields ) ) {
   402 	if ( ! is_array( $fields ) ) {
   380 		$fields = array_keys( _wp_post_revision_fields( $revision ) );
   403 		$fields = array_keys( _wp_post_revision_fields( $revision ) );
   389 		return false;
   412 		return false;
   390 	}
   413 	}
   391 
   414 
   392 	$update['ID'] = $revision['post_parent'];
   415 	$update['ID'] = $revision['post_parent'];
   393 
   416 
   394 	$update = wp_slash( $update ); //since data is from db
   417 	$update = wp_slash( $update ); // Since data is from DB.
   395 
   418 
   396 	$post_id = wp_update_post( $update );
   419 	$post_id = wp_update_post( $update );
   397 	if ( ! $post_id || is_wp_error( $post_id ) ) {
   420 	if ( ! $post_id || is_wp_error( $post_id ) ) {
   398 		return $post_id;
   421 		return $post_id;
   399 	}
   422 	}
   400 
   423 
   401 	// Update last edit user
   424 	// Update last edit user.
   402 	update_post_meta( $post_id, '_edit_last', get_current_user_id() );
   425 	update_post_meta( $post_id, '_edit_last', get_current_user_id() );
   403 
   426 
   404 	/**
   427 	/**
   405 	 * Fires after a post revision has been restored.
   428 	 * Fires after a post revision has been restored.
   406 	 *
   429 	 *
   423  *
   446  *
   424  * @param int|WP_Post $revision_id Revision ID or revision object.
   447  * @param int|WP_Post $revision_id Revision ID or revision object.
   425  * @return array|false|WP_Post|WP_Error|null Null or WP_Error if error, deleted post if success.
   448  * @return array|false|WP_Post|WP_Error|null Null or WP_Error if error, deleted post if success.
   426  */
   449  */
   427 function wp_delete_post_revision( $revision_id ) {
   450 function wp_delete_post_revision( $revision_id ) {
   428 	if ( ! $revision = wp_get_post_revision( $revision_id ) ) {
   451 	$revision = wp_get_post_revision( $revision_id );
       
   452 	if ( ! $revision ) {
   429 		return $revision;
   453 		return $revision;
   430 	}
   454 	}
   431 
   455 
   432 	$delete = wp_delete_post( $revision->ID );
   456 	$delete = wp_delete_post( $revision->ID );
   433 	if ( $delete ) {
   457 	if ( $delete ) {
   434 		/**
   458 		/**
   435 		 * Fires once a post revision has been deleted.
   459 		 * Fires once a post revision has been deleted.
   436 		 *
   460 		 *
   437 		 * @since 2.6.0
   461 		 * @since 2.6.0
   438 		 *
   462 		 *
   439 		 * @param int          $revision_id Post revision ID.
   463 		 * @param int     $revision_id Post revision ID.
   440 		 * @param object|array $revision    Post revision object or array.
   464 		 * @param WP_Post $revision    Post revision object.
   441 		 */
   465 		 */
   442 		do_action( 'wp_delete_post_revision', $revision->ID, $revision );
   466 		do_action( 'wp_delete_post_revision', $revision->ID, $revision );
   443 	}
   467 	}
   444 
   468 
   445 	return $delete;
   469 	return $delete;
   480 			'post_type'   => 'revision',
   504 			'post_type'   => 'revision',
   481 			'post_status' => 'inherit',
   505 			'post_status' => 'inherit',
   482 		)
   506 		)
   483 	);
   507 	);
   484 
   508 
   485 	if ( ! $revisions = get_children( $args ) ) {
   509 	$revisions = get_children( $args );
       
   510 	if ( ! $revisions ) {
   486 		return array();
   511 		return array();
   487 	}
   512 	}
   488 
   513 
   489 	return $revisions;
   514 	return $revisions;
   490 }
   515 }
   599  * @param int    $post_id
   624  * @param int    $post_id
   600  * @param string $taxonomy
   625  * @param string $taxonomy
   601  * @return array
   626  * @return array
   602  */
   627  */
   603 function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) {
   628 function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) {
   604 	if ( ! $post = get_post() ) {
   629 	$post = get_post();
       
   630 	if ( ! $post ) {
   605 		return $terms;
   631 		return $terms;
   606 	}
   632 	}
   607 
   633 
   608 	if ( empty( $_REQUEST['post_format'] ) || $post->ID != $post_id || 'post_format' != $taxonomy || 'revision' == $post->post_type ) {
   634 	if ( empty( $_REQUEST['post_format'] ) || $post->ID != $post_id
       
   635 		|| 'post_format' !== $taxonomy || 'revision' === $post->post_type
       
   636 	) {
   609 		return $terms;
   637 		return $terms;
   610 	}
   638 	}
   611 
   639 
   612 	if ( 'standard' == $_REQUEST['post_format'] ) {
   640 	if ( 'standard' === $_REQUEST['post_format'] ) {
   613 		$terms = array();
   641 		$terms = array();
   614 	} elseif ( $term = get_term_by( 'slug', 'post-format-' . sanitize_key( $_REQUEST['post_format'] ), 'post_format' ) ) {
   642 	} else {
   615 		$terms = array( $term ); // Can only have one post format
   643 		$term = get_term_by( 'slug', 'post-format-' . sanitize_key( $_REQUEST['post_format'] ), 'post_format' );
       
   644 		if ( $term ) {
       
   645 			$terms = array( $term ); // Can only have one post format.
       
   646 		}
   616 	}
   647 	}
   617 
   648 
   618 	return $terms;
   649 	return $terms;
   619 }
   650 }
   620 
   651 
   628  * @param int               $post_id  Post ID.
   659  * @param int               $post_id  Post ID.
   629  * @param string            $meta_key Meta key.
   660  * @param string            $meta_key Meta key.
   630  * @return null|array The default return value or the post thumbnail meta array.
   661  * @return null|array The default return value or the post thumbnail meta array.
   631  */
   662  */
   632 function _wp_preview_post_thumbnail_filter( $value, $post_id, $meta_key ) {
   663 function _wp_preview_post_thumbnail_filter( $value, $post_id, $meta_key ) {
   633 	if ( ! $post = get_post() ) {
   664 	$post = get_post();
       
   665 	if ( ! $post ) {
   634 		return $value;
   666 		return $value;
   635 	}
   667 	}
   636 
   668 
   637 	if ( empty( $_REQUEST['_thumbnail_id'] ) ||
   669 	if ( empty( $_REQUEST['_thumbnail_id'] ) ||
   638 		empty( $_REQUEST['preview_id'] ) ||
   670 		empty( $_REQUEST['preview_id'] ) ||
   639 		$post->ID != $post_id ||
   671 		$post->ID != $post_id ||
   640 		'_thumbnail_id' != $meta_key ||
   672 		'_thumbnail_id' !== $meta_key ||
   641 		'revision' == $post->post_type ||
   673 		'revision' === $post->post_type ||
   642 		$post_id != $_REQUEST['preview_id'] ) {
   674 		$post_id != $_REQUEST['preview_id'] ) {
   643 
   675 
   644 		return $value;
   676 		return $value;
   645 	}
   677 	}
   646 
   678 
   688  * @return bool true if the revisions were upgraded, false if problems
   720  * @return bool true if the revisions were upgraded, false if problems
   689  */
   721  */
   690 function _wp_upgrade_revisions_of_post( $post, $revisions ) {
   722 function _wp_upgrade_revisions_of_post( $post, $revisions ) {
   691 	global $wpdb;
   723 	global $wpdb;
   692 
   724 
   693 	// Add post option exclusively
   725 	// Add post option exclusively.
   694 	$lock   = "revision-upgrade-{$post->ID}";
   726 	$lock   = "revision-upgrade-{$post->ID}";
   695 	$now    = time();
   727 	$now    = time();
   696 	$result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $now ) );
   728 	$result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $now ) );
   697 	if ( ! $result ) {
   729 	if ( ! $result ) {
   698 		// If we couldn't get a lock, see how old the previous lock is
   730 		// If we couldn't get a lock, see how old the previous lock is.
   699 		$locked = get_option( $lock );
   731 		$locked = get_option( $lock );
   700 		if ( ! $locked ) {
   732 		if ( ! $locked ) {
   701 			// Can't write to the lock, and can't read the lock.
   733 			// Can't write to the lock, and can't read the lock.
   702 			// Something broken has happened
   734 			// Something broken has happened.
   703 			return false;
   735 			return false;
   704 		}
   736 		}
   705 
   737 
   706 		if ( $locked > $now - 3600 ) {
   738 		if ( $locked > $now - 3600 ) {
   707 			// Lock is not too old: some other process may be upgrading this post.  Bail.
   739 			// Lock is not too old: some other process may be upgrading this post. Bail.
   708 			return false;
   740 			return false;
   709 		}
   741 		}
   710 
   742 
   711 		// Lock is too old - update it (below) and continue
   743 		// Lock is too old - update it (below) and continue.
   712 	}
   744 	}
   713 
   745 
   714 	// If we could get a lock, re-"add" the option to fire all the correct filters.
   746 	// If we could get a lock, re-"add" the option to fire all the correct filters.
   715 	update_option( $lock, $now );
   747 	update_option( $lock, $now );
   716 
   748 
   721 		$this_revision = current( $revisions );
   753 		$this_revision = current( $revisions );
   722 		$prev_revision = next( $revisions );
   754 		$prev_revision = next( $revisions );
   723 
   755 
   724 		$this_revision_version = _wp_get_post_revision_version( $this_revision );
   756 		$this_revision_version = _wp_get_post_revision_version( $this_revision );
   725 
   757 
   726 		// Something terrible happened
   758 		// Something terrible happened.
   727 		if ( false === $this_revision_version ) {
   759 		if ( false === $this_revision_version ) {
   728 			continue;
   760 			continue;
   729 		}
   761 		}
   730 
   762 
   731 		// 1 is the latest revision version, so we're already up to date.
   763 		// 1 is the latest revision version, so we're already up to date.
   733 		if ( 0 < $this_revision_version ) {
   765 		if ( 0 < $this_revision_version ) {
   734 			$add_last = false;
   766 			$add_last = false;
   735 			continue;
   767 			continue;
   736 		}
   768 		}
   737 
   769 
   738 		// Always update the revision version
   770 		// Always update the revision version.
   739 		$update = array(
   771 		$update = array(
   740 			'post_name' => preg_replace( '/^(\d+-(?:autosave|revision))[\d-]*$/', '$1-v1', $this_revision->post_name ),
   772 			'post_name' => preg_replace( '/^(\d+-(?:autosave|revision))[\d-]*$/', '$1-v1', $this_revision->post_name ),
   741 		);
   773 		);
   742 
   774 
   743 		// If this revision is the oldest revision of the post, i.e. no $prev_revision,
   775 		/*
   744 		// the correct post_author is probably $post->post_author, but that's only a good guess.
   776 		 * If this revision is the oldest revision of the post, i.e. no $prev_revision,
   745 		// Update the revision version only and Leave the author as-is.
   777 		 * the correct post_author is probably $post->post_author, but that's only a good guess.
       
   778 		 * Update the revision version only and Leave the author as-is.
       
   779 		 */
   746 		if ( $prev_revision ) {
   780 		if ( $prev_revision ) {
   747 			$prev_revision_version = _wp_get_post_revision_version( $prev_revision );
   781 			$prev_revision_version = _wp_get_post_revision_version( $prev_revision );
   748 
   782 
   749 			// If the previous revision is already up to date, it no longer has the information we need :(
   783 			// If the previous revision is already up to date, it no longer has the information we need :(
   750 			if ( $prev_revision_version < 1 ) {
   784 			if ( $prev_revision_version < 1 ) {
   751 				$update['post_author'] = $prev_revision->post_author;
   785 				$update['post_author'] = $prev_revision->post_author;
   752 			}
   786 			}
   753 		}
   787 		}
   754 
   788 
   755 		// Upgrade this revision
   789 		// Upgrade this revision.
   756 		$result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) );
   790 		$result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) );
   757 
   791 
   758 		if ( $result ) {
   792 		if ( $result ) {
   759 			wp_cache_delete( $this_revision->ID, 'posts' );
   793 			wp_cache_delete( $this_revision->ID, 'posts' );
   760 		}
   794 		}