Make WordPress Core

Ticket #20564: 20564.4.diff

File 20564.4.diff, 4.2 KB (added by kovshenin, 12 years ago)
  • wp-includes/post.php

     
    16671667 * @return bool False for failure. True for success.
    16681668 */
    16691669function add_post_meta($post_id, $meta_key, $meta_value, $unique = false) {
    1670         // make sure meta is added to the post, not a revision
    1671         if ( $the_post = wp_is_post_revision($post_id) )
     1670        // make sure meta is added to the post, not a revision, unless the key is revisioned
     1671        if ( $the_post = wp_is_post_revision( $post_id ) && ! in_array( $meta_key, _wp_post_revision_meta_keys() ) )
    16721672                $post_id = $the_post;
    16731673
    16741674        return add_metadata('post', $post_id, $meta_key, $meta_value, $unique);
     
    16911691 * @return bool False for failure. True for success.
    16921692 */
    16931693function delete_post_meta($post_id, $meta_key, $meta_value = '') {
    1694         // make sure meta is added to the post, not a revision
    1695         if ( $the_post = wp_is_post_revision($post_id) )
     1694        // make sure meta is deleted from the post, not the revision, unless the key is revisioned
     1695        if ( $the_post = wp_is_post_revision( $post_id ) && ! in_array( $meta_key, _wp_post_revision_meta_keys() ) )
    16961696                $post_id = $the_post;
    16971697
    16981698        return delete_metadata('post', $post_id, $meta_key, $meta_value);
     
    17341734 * @return bool False on failure, true if success.
    17351735 */
    17361736function update_post_meta($post_id, $meta_key, $meta_value, $prev_value = '') {
    1737         // make sure meta is added to the post, not a revision
    1738         if ( $the_post = wp_is_post_revision($post_id) )
     1737        // make sure meta is updated on the post, not a revision, unless the key is revisioned
     1738        if ( $the_post = wp_is_post_revision( $post_id ) && ! in_array( $meta_key, _wp_post_revision_meta_keys() ) )
    17391739                $post_id = $the_post;
    17401740
    17411741        return update_metadata('post', $post_id, $meta_key, $meta_value, $prev_value);
  • wp-includes/revision.php

     
    6161}
    6262
    6363/**
     64 * Determines which post meta fields are revisioned.
     65 *
     66 * @since 3.6
     67 * @access private
     68 * @return array An array of meta keys that should be revisioned.
     69 */
     70function _wp_post_revision_meta_keys() {
     71        $keys = array(
     72                '_wp_format_url',
     73                '_wp_format_quote',
     74                '_wp_format_quote_source',
     75                '_wp_format_image',
     76                '_wp_format_gallery',
     77                '_wp_format_audio',
     78                '_wp_format_video',
     79        );
     80        return $keys;
     81}
     82
     83/**
    6484 * Saves an already existing post as a post revision.
    6585 *
    6686 * Typically used immediately after post updates.
     
    113133                                }
    114134                        }
    115135
     136                        // Check whether revisioned meta fields have changed.
     137                        foreach ( _wp_post_revision_meta_keys() as $meta_key ) {
     138                                if ( get_post_meta( $post->ID, $meta_key ) != get_post_meta( $last_revision->ID, $meta_key ) ) {
     139                                        $post_has_changed = true;
     140                                        break;
     141                                }
     142                        }
     143
    116144                        //don't save revision if post unchanged
    117145                        if( ! $post_has_changed )
    118146                                return;
     
    240268        if ( isset($post['post_type']) && 'revision' == $post['post_type'] )
    241269                return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
    242270
     271        $post_id = $post['ID'];
    243272        $post = _wp_post_revision_fields( $post, $autosave );
    244273        $post = wp_slash($post); //since data is from db
    245274
     
    250279        if ( $revision_id )
    251280                do_action( '_wp_put_post_revision', $revision_id );
    252281
     282        // Save revisioned meta fields.
     283        foreach ( _wp_post_revision_meta_keys() as $meta_key ) {
     284                $meta_values = get_post_meta( $post_id, $meta_key );
     285                if ( false === $meta_values )
     286                        continue;
     287
     288                foreach ( $meta_values as $meta_value )
     289                        add_post_meta( $revision_id, $meta_key, $meta_value );
     290        }
     291
    253292        return $revision_id;
    254293}
    255294
     
    324363
    325364        $update = wp_slash( $update ); //since data is from db
    326365
     366        // Restore revisioned meta fields.
     367        foreach ( _wp_post_revision_meta_keys() as $meta_key ) {
     368                delete_post_meta( $update['ID'], $meta_key );
     369                $meta_values = get_post_meta( $revision['ID'], $meta_key );
     370                if ( false === $meta_values )
     371                        continue;
     372
     373                foreach ( $meta_values as $meta_value )
     374                        add_post_meta( $update['ID'], $meta_key, $meta_value );
     375        }
     376
    327377        $post_id = wp_update_post( $update );
    328378        if ( is_wp_error( $post_id ) )
    329379                return $post_id;