Make WordPress Core

Ticket #13382: 13382.3.diff

File 13382.3.diff, 10.3 KB (added by SergeyBiryukov, 9 years ago)
  • src/wp-admin/edit-form-advanced.php

     
    197197
    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 ) );
    203203                        break;
  • src/wp-admin/includes/post.php

     
    16651665
    16661666        // Store one autosave per author. If there is already an autosave, overwrite it.
    16671667        if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) {
    1668                 $new_autosave = _wp_post_revision_fields( $post_data, true );
     1668                $new_autosave = _wp_post_revision_data( $post_data, true );
    16691669                $new_autosave['ID'] = $old_autosave->ID;
    16701670                $new_autosave['post_author'] = $post_author;
    16711671
     
    16721672                // If the new autosave has the same content as the post, delete the autosave.
    16731673                $post = get_post( $post_id );
    16741674                $autosave_is_different = false;
    1675                 foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields() ) ) as $field ) {
     1675                foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) {
    16761676                        if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) {
    16771677                                $autosave_is_different = true;
    16781678                                break;
  • src/wp-admin/includes/revision.php

     
    5454
    5555        $return = array();
    5656
    57         foreach ( _wp_post_revision_fields() as $field => $name ) {
     57        foreach ( _wp_post_revision_fields( $post ) as $field => $name ) {
    5858                /**
    5959                 * Contextually filter a post revision field.
    6060                 *
  • src/wp-includes/revision.php

     
    99/**
    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  *
    1612 * @since 2.6.0
     13 * @since 4.4.0 A `WP_Post` object can now be passed to the `$post` parameter.
    1714 * @access private
    1815 *
    1916 * @staticvar array $fields
    2017 *
    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.
     18 * @param array|WP_Post $post       Optional. A post array or a WP_Post object being processed
     19 *                                  for insertion as a post revision. Default empty array.
     20 * @param bool          $deprecated Not used.
     21 * @return array Array of fields that can be versioned.
    2422 */
    25 function _wp_post_revision_fields( $post = null, $autosave = false ) {
     23function _wp_post_revision_fields( $post = array(), $deprecated = false ) {
    2624        static $fields = null;
    2725
     26        if ( ! is_array( $post ) ) {
     27                $post = get_post( $post, ARRAY_A );
     28        }
     29
    2830        if ( is_null( $fields ) ) {
    2931                // Allow these to be versioned
    3032                $fields = array(
     
    3234                        'post_content' => __( 'Content' ),
    3335                        'post_excerpt' => __( 'Excerpt' ),
    3436                );
     37        }
    3538
    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 );
     39        /**
     40         * Filter the list of fields saved in post revisions.
     41         *
     42         * Included by default: 'post_title', 'post_content' and 'post_excerpt'.
     43         *
     44         * Disallowed fields: 'ID', 'post_name', 'post_parent', 'post_date',
     45         * 'post_date_gmt', 'post_status', 'post_type', 'comment_count',
     46         * and 'post_author'.
     47         *
     48         * @since 2.6.0
     49         * @since 4.4.0 The `$post` parameter was added.
     50         *
     51         * @param array $fields List of fields to revision. Contains 'post_title',
     52         *                      'post_content', and 'post_excerpt' by default.
     53         * @param array $post   A post array being processed for insertion as a post revision.
     54         */
     55        $fields = apply_filters( '_wp_post_revision_fields', $fields, $post );
    5156
    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] );
     57        // WP uses these internally either in versioning or elsewhere - they cannot be versioned
     58        foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect ) {
     59                unset( $fields[ $protect ] );
    5560        }
    5661
    57         if ( !is_array($post) )
    58                 return $fields;
    5962
    60         $return = array();
    61         foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field )
    62                 $return[$field] = $post[$field];
     63        return $fields;
     64}
    6365
    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'] : '';
     66/**
     67 * Returns a post array ready to be inserted into the posts table as a post revision.
     68 *
     69 * @since 4.4.0
     70 * @access private
     71 *
     72 * @param array|WP_Post $post     Optional. A post array or a WP_Post object to be processed
     73 *                                for insertion as a post revision. Default empty array.
     74 * @param bool          $autosave Optional. Is the revision an autosave? Default false.
     75 * @return array Post array ready to be inserted as a post revision.
     76 */
     77function _wp_post_revision_data( $post = array(), $autosave = false ) {
     78        if ( ! is_array( $post ) ) {
     79                $post = get_post( $post, ARRAY_A );
     80        }
    7081
    71         return $return;
     82        $fields = _wp_post_revision_fields( $post );
     83
     84        $revision_data = array();
     85
     86        foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field ) {
     87                $revision_data[ $field ] = $post[ $field ];
     88        }
     89
     90        $revision_data['post_parent']   = $post['ID'];
     91        $revision_data['post_status']   = 'inherit';
     92        $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
     94        $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'] : '';
     96
     97        return $revision_data;
    7298}
    7399
    74100/**
     
    127153                if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', $check_for_changes = true, $last_revision, $post ) ) {
    128154                        $post_has_changed = false;
    129155
    130                         foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
     156                        foreach ( array_keys( _wp_post_revision_fields( $post ) ) as $field ) {
    131157                                if ( normalize_whitespace( $post->$field ) != normalize_whitespace( $last_revision->$field ) ) {
    132158                                        $post_has_changed = true;
    133159                                        break;
     
    267293        if ( isset($post['post_type']) && 'revision' == $post['post_type'] )
    268294                return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
    269295
    270         $post = _wp_post_revision_fields( $post, $autosave );
     296        $post = _wp_post_revision_data( $post, $autosave );
    271297        $post = wp_slash($post); //since data is from db
    272298
    273299        $revision_id = wp_insert_post( $post );
     
    333359                return $revision;
    334360
    335361        if ( !is_array( $fields ) )
    336                 $fields = array_keys( _wp_post_revision_fields() );
     362                $fields = array_keys( _wp_post_revision_fields( $revision ) );
    337363
    338364        $update = array();
    339365        foreach ( array_intersect( array_keys( $revision ), $fields ) as $field ) {
  • tests/phpunit/tests/post/revisions.php

     
    340340                $post = self::factory()->post->create_and_get( array( 'post_title' => 'some-post', 'post_type' => 'post', 'post_content' => 'some_content' ) );
    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
    346346                $revision_ids = array();
     
    368368                $post = self::factory()->post->create_and_get( array( 'post_title' => 'some-post', 'post_type' => 'post', 'post_content' => 'some_content' ) );
    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
    374374                $revision_ids = array();