From ee251924b3c2fcfb60581d5c94e952b363b27d5c Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Wed, 7 Oct 2015 21:52:18 +0000 Subject: [PATCH] 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 --- src/wp-admin/edit-form-advanced.php | 2 +- src/wp-admin/includes/post.php | 2 +- src/wp-admin/includes/revision.php | 2 +- src/wp-includes/revision.php | 17 ++++++++++++----- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/wp-admin/edit-form-advanced.php b/src/wp-admin/edit-form-advanced.php index aff0413182..385582f66e 100644 --- a/src/wp-admin/edit-form-advanced.php +++ b/src/wp-admin/edit-form-advanced.php @@ -197,7 +197,7 @@ $form_extra .= "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. View the autosave' ), get_edit_post_link( $autosave->ID ) ); break; diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index 83756e695a..4999966268 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -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; diff --git a/src/wp-admin/includes/revision.php b/src/wp-admin/includes/revision.php index 3595226111..5451491726 100644 --- a/src/wp-admin/includes/revision.php +++ b/src/wp-admin/includes/revision.php @@ -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. * diff --git a/src/wp-includes/revision.php b/src/wp-includes/revision.php index ff0b6b7d2a..8ad6fcc578 100644 --- a/src/wp-includes/revision.php +++ b/src/wp-includes/revision.php @@ -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 ) {