Revisions: Clean up `_wp_post_revision_fields()`:

* Move the array processing to a new function, `_wp_post_revision_data()`.
* Make both functions accept a post array or a `WP_Post` object. 
* Always apply the `_wp_post_revision_fields` filter and pass the post data to it.

Fixes #13382.
Built from https://develop.svn.wordpress.org/trunk@36659


git-svn-id: http://core.svn.wordpress.org/trunk@36626 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2016-02-24 00:44:59 +00:00
parent f9a4120299
commit ab0eb71d21
5 changed files with 75 additions and 48 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

@ -1662,14 +1662,14 @@ function wp_create_post_autosave( $post_data ) {
// Store one autosave per author. If there is already an autosave, overwrite it.
if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) {
$new_autosave = _wp_post_revision_fields( $post_data, true );
$new_autosave = _wp_post_revision_data( $post_data, true );
$new_autosave['ID'] = $old_autosave->ID;
$new_autosave['post_author'] = $post_author;
// 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

@ -55,7 +55,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

@ -9,22 +9,25 @@
/**
* 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
*
* @staticvar array $fields
*
* @param array|null $post Optional. A post array to be processed for insertion as a post revision. Default null.
* @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.
* @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 ) {
function _wp_post_revision_fields( $post = array(), $deprecated = false ) {
static $fields = null;
if ( ! is_array( $post ) ) {
$post = get_post( $post, ARRAY_A );
}
if ( is_null( $fields ) ) {
// Allow these to be versioned
$fields = array(
@ -32,43 +35,67 @@ function _wp_post_revision_fields( $post = null, $autosave = false ) {
'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 );
// 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;
/**
* 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
* @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 );
$return = array();
foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field )
$return[$field] = $post[$field];
// 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 ] );
}
$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'] : '';
return $return;
return $fields;
}
/**
* 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 );
}
$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;
}
/**
@ -127,7 +154,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;
@ -267,7 +294,7 @@ function _wp_put_post_revision( $post = null, $autosave = false ) {
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 );
@ -333,7 +360,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 ) {

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.5-alpha-36658';
$wp_version = '4.5-alpha-36659';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.