WordPress.org

Make WordPress Core

Changeset 36659


Ignore:
Timestamp:
02/24/2016 12:43:31 AM (5 years ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/edit-form-advanced.php

    r35883 r36659  
    198198// Detect if there exists an autosave newer than the post and if that autosave is different than the post
    199199if ( $autosave && mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) {
    200     foreach ( _wp_post_revision_fields() as $autosave_field => $_autosave_field ) {
     200    foreach ( _wp_post_revision_fields( $post ) as $autosave_field => $_autosave_field ) {
    201201        if ( normalize_whitespace( $autosave->$autosave_field ) != normalize_whitespace( $post->$autosave_field ) ) {
    202202            $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 ) );
  • trunk/src/wp-admin/includes/post.php

    r36584 r36659  
    16631663    // Store one autosave per author. If there is already an autosave, overwrite it.
    16641664    if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) {
    1665         $new_autosave = _wp_post_revision_fields( $post_data, true );
     1665        $new_autosave = _wp_post_revision_data( $post_data, true );
    16661666        $new_autosave['ID'] = $old_autosave->ID;
    16671667        $new_autosave['post_author'] = $post_author;
     
    16701670        $post = get_post( $post_id );
    16711671        $autosave_is_different = false;
    1672         foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields() ) ) as $field ) {
     1672        foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) {
    16731673            if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) {
    16741674                $autosave_is_different = true;
  • trunk/src/wp-admin/includes/revision.php

    r35387 r36659  
    5656    $return = array();
    5757
    58     foreach ( _wp_post_revision_fields() as $field => $name ) {
     58    foreach ( _wp_post_revision_fields( $post ) as $field => $name ) {
    5959        /**
    6060         * Contextually filter a post revision field.
  • trunk/src/wp-includes/revision.php

    r35981 r36659  
    1010 * Determines which fields of posts are to be saved in revisions.
    1111 *
    12  * Does two things. If passed a post *array*, it will return a post array ready
    13  * to be inserted into the posts table as a post revision. Otherwise, returns
    14  * an array whose keys are the post fields to be saved for post revisions.
    15  *
    16  * @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.
     14 * @since 4.5.0 The optional `$autosave` parameter was deprecated and renamed to `$deprecated`.
    1715 * @access private
    1816 *
    1917 * @staticvar array $fields
    2018 *
    21  * @param array|null $post     Optional. A post array to be processed for insertion as a post revision. Default null.
    22  * @param bool       $autosave Optional. Is the revision an autosave? Default false.
    23  * @return array Post array ready to be inserted as a post revision or array of fields that can be versioned.
    24  */
    25 function _wp_post_revision_fields( $post = null, $autosave = false ) {
     19 * @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.
     21 * @param bool          $deprecated Not used.
     22 * @return array Array of fields that can be versioned.
     23 */
     24function _wp_post_revision_fields( $post = array(), $deprecated = false ) {
    2625    static $fields = null;
     26
     27    if ( ! is_array( $post ) ) {
     28        $post = get_post( $post, ARRAY_A );
     29    }
    2730
    2831    if ( is_null( $fields ) ) {
     
    3336            'post_excerpt' => __( 'Excerpt' ),
    3437        );
    35 
    36         /**
    37          * Filter the list of fields saved in post revisions.
    38          *
    39          * Included by default: 'post_title', 'post_content' and 'post_excerpt'.
    40          *
    41          * Disallowed fields: 'ID', 'post_name', 'post_parent', 'post_date',
    42          * 'post_date_gmt', 'post_status', 'post_type', 'comment_count',
    43          * and 'post_author'.
    44          *
    45          * @since 2.6.0
    46          *
    47          * @param array $fields List of fields to revision. Contains 'post_title',
    48          *                      'post_content', and 'post_excerpt' by default.
    49          */
    50         $fields = apply_filters( '_wp_post_revision_fields', $fields );
    51 
    52         // WP uses these internally either in versioning or elsewhere - they cannot be versioned
    53         foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect )
    54             unset( $fields[$protect] );
    55     }
    56 
    57     if ( !is_array($post) )
    58         return $fields;
    59 
    60     $return = array();
    61     foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field )
    62         $return[$field] = $post[$field];
    63 
    64     $return['post_parent']   = $post['ID'];
    65     $return['post_status']   = 'inherit';
    66     $return['post_type']     = 'revision';
    67     $return['post_name']     = $autosave ? "$post[ID]-autosave-v1" : "$post[ID]-revision-v1"; // "1" is the revisioning system version
    68     $return['post_date']     = isset($post['post_modified']) ? $post['post_modified'] : '';
    69     $return['post_date_gmt'] = isset($post['post_modified_gmt']) ? $post['post_modified_gmt'] : '';
    70 
    71     return $return;
     38    }
     39
     40    /**
     41     * Filter the list of fields saved in post revisions.
     42     *
     43     * Included by default: 'post_title', 'post_content' and 'post_excerpt'.
     44     *
     45     * Disallowed fields: 'ID', 'post_name', 'post_parent', 'post_date',
     46     * 'post_date_gmt', 'post_status', 'post_type', 'comment_count',
     47     * and 'post_author'.
     48     *
     49     * @since 2.6.0
     50     * @since 4.5.0 The `$post` parameter was added.
     51     *
     52     * @param array $fields List of fields to revision. Contains 'post_title',
     53     *                      'post_content', and 'post_excerpt' by default.
     54     * @param array $post   A post array being processed for insertion as a post revision.
     55     */
     56    $fields = apply_filters( '_wp_post_revision_fields', $fields, $post );
     57
     58    // 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 ) {
     60        unset( $fields[ $protect ] );
     61    }
     62
     63
     64    return $fields;
     65}
     66
     67/**
     68 * Returns a post array ready to be inserted into the posts table as a post revision.
     69 *
     70 * @since 4.5.0
     71 * @access private
     72 *
     73 * @param array|WP_Post $post     Optional. A post array or a WP_Post object to be processed
     74 *                                for insertion as a post revision. Default empty array.
     75 * @param bool          $autosave Optional. Is the revision an autosave? Default false.
     76 * @return array Post array ready to be inserted as a post revision.
     77 */
     78function _wp_post_revision_data( $post = array(), $autosave = false ) {
     79    if ( ! is_array( $post ) ) {
     80        $post = get_post( $post, ARRAY_A );
     81    }
     82
     83    $fields = _wp_post_revision_fields( $post );
     84
     85    $revision_data = array();
     86
     87    foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field ) {
     88        $revision_data[ $field ] = $post[ $field ];
     89    }
     90
     91    $revision_data['post_parent']   = $post['ID'];
     92    $revision_data['post_status']   = 'inherit';
     93    $revision_data['post_type']     = 'revision';
     94    $revision_data['post_name']     = $autosave ? "$post[ID]-autosave-v1" : "$post[ID]-revision-v1"; // "1" is the revisioning system version
     95    $revision_data['post_date']     = isset( $post['post_modified'] ) ? $post['post_modified'] : '';
     96    $revision_data['post_date_gmt'] = isset( $post['post_modified_gmt'] ) ? $post['post_modified_gmt'] : '';
     97
     98    return $revision_data;
    7299}
    73100
     
    128155            $post_has_changed = false;
    129156
    130             foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
     157            foreach ( array_keys( _wp_post_revision_fields( $post ) ) as $field ) {
    131158                if ( normalize_whitespace( $post->$field ) != normalize_whitespace( $last_revision->$field ) ) {
    132159                    $post_has_changed = true;
     
    268295        return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
    269296
    270     $post = _wp_post_revision_fields( $post, $autosave );
     297    $post = _wp_post_revision_data( $post, $autosave );
    271298    $post = wp_slash($post); //since data is from db
    272299
     
    334361
    335362    if ( !is_array( $fields ) )
    336         $fields = array_keys( _wp_post_revision_fields() );
     363        $fields = array_keys( _wp_post_revision_fields( $revision ) );
    337364
    338365    $update = array();
  • trunk/tests/phpunit/tests/post/revisions.php

    r35242 r36659  
    341341
    342342        $post = (array) $post;
    343         $post_revision_fields = _wp_post_revision_fields( $post );
     343        $post_revision_fields = _wp_post_revision_data( $post );
    344344        $post_revision_fields = wp_slash( $post_revision_fields );
    345345
     
    369369
    370370        $post = (array) $post;
    371         $post_revision_fields = _wp_post_revision_fields( $post );
     371        $post_revision_fields = _wp_post_revision_data( $post );
    372372        $post_revision_fields = wp_slash( $post_revision_fields );
    373373
Note: See TracChangeset for help on using the changeset viewer.