Pass the `$post` parameter to the `_wp_post_revision_fields` filter. This provides more context to the filter, which allows for different fields to be displayed on the revisions screen depending on the post.

The `_wp_post_revision_fields()` function now also accepts a `WP_Post` object (in addition to an array of post fields) to facilitate this change.

Fixes #13382
Props adamsilverstein


git-svn-id: https://develop.svn.wordpress.org/trunk@34917 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn 2015-10-07 21:52:18 +00:00
parent e40575aa99
commit ee251924b3
4 changed files with 15 additions and 8 deletions

View File

@ -197,7 +197,7 @@ $form_extra .= "<input type='hidden' id='post_ID' name='post_ID' value='" . esc_
// Detect if there exists an autosave newer than the post and if that autosave is different than the post
if ( $autosave && mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) {
foreach ( _wp_post_revision_fields() as $autosave_field => $_autosave_field ) {
foreach ( _wp_post_revision_fields( $post ) as $autosave_field => $_autosave_field ) {
if ( normalize_whitespace( $autosave->$autosave_field ) != normalize_whitespace( $post->$autosave_field ) ) {
$notice = sprintf( __( 'There is an autosave of this post that is more recent than the version below. <a href="%s">View the autosave</a>' ), get_edit_post_link( $autosave->ID ) );
break;

View File

@ -1656,7 +1656,7 @@ function wp_create_post_autosave( $post_data ) {
// If the new autosave has the same content as the post, delete the autosave.
$post = get_post( $post_id );
$autosave_is_different = false;
foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields() ) ) as $field ) {
foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) {
if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) {
$autosave_is_different = true;
break;

View File

@ -54,7 +54,7 @@ function wp_get_revision_ui_diff( $post, $compare_from, $compare_to ) {
$return = array();
foreach ( _wp_post_revision_fields() as $field => $name ) {
foreach ( _wp_post_revision_fields( $post ) as $field => $name ) {
/**
* Contextually filter a post revision field.
*

View File

@ -14,17 +14,22 @@
* an array whose keys are the post fields to be saved for post revisions.
*
* @since 2.6.0
* @since 4.4.0 A `WP_Post` object can now be passed to the `$post` parameter.
* @access private
*
* @staticvar array $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?
* @param array|WP_Post $post Optional. A post array, or a WP_Post object to be processed for insertion as a post revision.
* @param bool $autosave Optional. Is the revision an autosave? Default false.
* @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 = null;
if ( is_object( $post ) ) {
$post = get_post( $post, ARRAY_A );
}
if ( is_null( $fields ) ) {
// Allow these to be versioned
$fields = array(
@ -43,11 +48,13 @@ function _wp_post_revision_fields( $post = null, $autosave = false ) {
* and 'post_author'.
*
* @since 2.6.0
* @since 4.4.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 );
$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 )
@ -127,7 +134,7 @@ function wp_save_post_revision( $post_id ) {
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;
@ -333,7 +340,7 @@ function wp_restore_post_revision( $revision_id, $fields = null ) {
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 ) {