From cccb2939db966657484d350f5db6a4057d9ff4dd Mon Sep 17 00:00:00 2001 From: Mark Jaquith Date: Wed, 27 Mar 2013 20:21:38 +0000 Subject: [PATCH] Fix a longstanding "off by one" revision authorship bug. * Fixes old revision data on the fly when you open a post for editing. * Uses post_name of revisions to store a post version number (-v1), so we know what has been fixed. * Latest version should also have a revision stored, whereas before it did not. props adamsilverstein, mdawaffe. fixes #16215. git-svn-id: http://core.svn.wordpress.org/trunk@23823 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/post.php | 4 + wp-includes/default-filters.php | 2 +- wp-includes/post.php | 2 +- wp-includes/revision.php | 197 +++++++++++++++++++++++++++++--- 4 files changed, 187 insertions(+), 18 deletions(-) diff --git a/wp-admin/post.php b/wp-admin/post.php index 78cddf525b..5e6356ad60 100644 --- a/wp-admin/post.php +++ b/wp-admin/post.php @@ -135,6 +135,7 @@ case 'edit': $p = $post_id; + if ( empty($post->ID) ) wp_die( __('You attempted to edit an item that doesn’t exist. Perhaps it was deleted?') ); @@ -153,6 +154,9 @@ case 'edit': exit(); } + //upgrade any old bad revision data (#16215) + _wp_upgrade_revisions_of_post( $p ); + $post_type = $post->post_type; if ( 'post' == $post_type ) { $parent_file = "edit.php"; diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php index 1827dc94bd..d8a4ff3bf7 100644 --- a/wp-includes/default-filters.php +++ b/wp-includes/default-filters.php @@ -250,7 +250,7 @@ add_action( 'init', 'smilies_init', add_action( 'plugins_loaded', 'wp_maybe_load_widgets', 0 ); add_action( 'plugins_loaded', 'wp_maybe_load_embeds', 0 ); add_action( 'shutdown', 'wp_ob_end_flush_all', 1 ); -add_action( 'pre_post_update', 'wp_save_post_revision', 10, 2 ); +add_action( 'post_updated', 'wp_save_post_revision', 10, 1 ); add_action( 'publish_post', '_publish_post_hook', 5, 1 ); add_action( 'transition_post_status', '_transition_post_status', 5, 3 ); add_action( 'transition_post_status', '_update_term_count_on_transition_post_status', 10, 3 ); diff --git a/wp-includes/post.php b/wp-includes/post.php index cc80dcee4f..4e7676bdc5 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -3012,7 +3012,7 @@ function check_and_publish_future_post($post_id) { * @return string unique slug for the post, based on $post_name (with a -1, -2, etc. suffix) */ function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) { - if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) + if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) || ( 'inherit' == $post_status && 'revision' == $post_type ) ) return $slug; global $wpdb, $wp_rewrite; diff --git a/wp-includes/revision.php b/wp-includes/revision.php index 7970335415..5451d86088 100644 --- a/wp-includes/revision.php +++ b/wp-includes/revision.php @@ -52,9 +52,10 @@ function _wp_post_revision_fields( $post = null, $autosave = false ) { $return['post_parent'] = $post['ID']; $return['post_status'] = 'inherit'; $return['post_type'] = 'revision'; - $return['post_name'] = $autosave ? "$post[ID]-autosave" : "$post[ID]-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['post_author'] = get_post_meta( $post['ID'], '_edit_last', true ); return $return; } @@ -62,19 +63,27 @@ function _wp_post_revision_fields( $post = null, $autosave = false ) { /** * Saves an already existing post as a post revision. * - * Typically used immediately prior to post updates. + * Typically used immediately prior and after post updates. + * Prior to update checks for old revision data (latest revision != current post before update) and adds a copy of the current post as a revision if missing + * After update adds a copy of the current post as a revision, so latest revision always matches current post * * @package WordPress * @subpackage Post_Revisions * @since 2.6.0 * * @uses _wp_put_post_revision() + * @uses wp_first_revision_matches_current_version() * * @param int $post_id The ID of the post to save as a revision. * @return mixed Null or 0 if error, new revision ID, if success. */ -function wp_save_post_revision( $post_id, $new_data = null ) { - // We do autosaves manually with wp_create_post_autosave() +function wp_save_post_revision( $post_id ) { + //check to see if the post's first revision already matches the post data + //should be true before post update, _except_ for old data which + //doesn't include a copy of the current post data in revisions + if ( wp_first_revision_matches_current_version( $post_id ) ) + return; + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; @@ -90,18 +99,32 @@ function wp_save_post_revision( $post_id, $new_data = null ) { if ( ! post_type_supports( $post['post_type'], 'revisions' ) ) return; - // if new data is supplied, check that it is different from last saved revision, unless a plugin tells us to always save regardless - if ( apply_filters( 'wp_save_post_revision_check_for_changes', true, $post, $new_data ) && is_array( $new_data ) ) { - $post_has_changed = false; - foreach ( array_keys( _wp_post_revision_fields() ) as $field ) { - if ( normalize_whitespace( $new_data[ $field ] ) != normalize_whitespace( $post[ $field ] ) ) { - $post_has_changed = true; - break; + // compare the proposed update with the last stored revision, verify + // different, unless a plugin tells us to always save regardless + if ( $revisions = wp_get_post_revisions( $post_id ) ) { + // grab the last revision + $last_revision = array_shift( $revisions ); + + //if no previous revisions, save one for sure + if ( $last_revision_array = get_post( $last_revision->ID, ARRAY_A ) ) { + + if ( apply_filters( 'wp_save_post_revision_check_for_changes', true, $last_revision_array, $post ) && is_array( $post ) ) { + $post_has_changed = false; + + foreach ( array_keys( _wp_post_revision_fields() ) as $field ) { + + if ( normalize_whitespace( $post[ $field ] ) != normalize_whitespace( $last_revision_array[ $field ] ) ) { + $post_has_changed = true; + break; + + } + } + + //don't save revision if post unchanged + if( ! $post_has_changed ) + return; } } - //don't save revision if post unchanged - if( ! $post_has_changed ) - return; } $return = _wp_put_post_revision( $post ); @@ -122,9 +145,9 @@ function wp_save_post_revision( $post_id, $new_data = null ) { $revisions = array_slice( $revisions, 0, $delete ); for ( $i = 0; isset($revisions[$i]); $i++ ) { - if ( false !== strpos( $revisions[$i]->post_name, 'autosave' ) ) + if ( false !== strpos( $revisions[ $i ]->post_name, 'autosave' ) ) continue; - wp_delete_post_revision( $revisions[$i]->ID ); + wp_delete_post_revision( $revisions[ $i ]->ID ); } return $return; @@ -441,6 +464,148 @@ function _set_preview($post) { return $post; } +function _wp_get_post_revision_version( $post ) { + if ( is_array( $post ) ) { + if ( ! isset( $post['post_name'] ) ) { + return false; + } + + $name = $post['post_name']; + } elseif ( is_object( $post ) ) { + if ( ! isset( $post->post_name ) ) { + return false; + } + + $name = $post->post_name; + } else { + return false; + } + + if ( ! preg_match( '/^(\d+-)(?:autosave|revision)(?:-v)(\d+)$/', $name, $matches ) ) { + return 0; + } + + if ( '1' === $matches[2] ) { + return 1; + } + + return 0; +} + +/** + * Upgrade the data + * + * @package WordPress + * @subpackage Post_Revisions + * @since 3.6.0 + * + * @uses get_post() + * @uses post_type_supports() + * @uses wp_get_post_revisions() + * @uses wp_save_post_revision() + * + * @param int|object $post_id Post ID or post object + * @return true if success, false if problems + */ +function _wp_upgrade_revisions_of_post( $post ) { + global $wpdb; + + $post = get_post( $post ); + if ( ! $post ) + return false; + + //make sure we have a current revision, only adds one if missing + wp_save_post_revision( $post->ID ); + + if ( ! post_type_supports( $post->post_type, 'revisions' ) ) + return false; + + $revisions = wp_get_post_revisions( $post->ID ); // array( 'order' => 'DESC', 'orderby' => 'date' ); // Always work from most recent to oldest + + + if ( ! $revisions ) + return true; + + // Add post option exclusively + $lock = "revision-upgrade-{$post->ID}"; + $locked_at = number_format( microtime( true ), 10, '.', '' ); + $result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $locked_at ) ); + if ( ! $result ) { + // If we couldn't get a lock, see how old the previous lock is + $locked_at = get_option( $lock ); + if ( !$locked_at ) { + // Can't write to the lock, and can't read the lock. + // Something broken has happened + return false; + } + + if ( $lock_at < number_format( microtime( true ), 10, '.', '' ) - 3600 ) { + // Lock is too old - try again + delete_option( $lock ); + return wp_upgrade_revisions_of_post( $post ); + } + + // Lock is not too old: some other process may be upgrading this post. Bail. + return; + } else { + // If we could get a lock, re-"add" the option to fire all the correct filters. + add_option( $lock, $locked_at ); + } + + $success = true; + + reset( $revisions ); + do { + $this_revision = current( $revisions ); + $prev_revision = next( $revisions ); + + $this_revision_version = _wp_get_post_revision_version( $this_revision ); + + error_log($this_revision_version); + + // Something terrible happened + if ( false === $this_revision_version ) + continue; + + // 1 is the latest revision version, so we're already up to date + if ( 0 < $this_revision_version ) + continue; + + // This revision is the oldest revision of the post. + // The correct post_author is probably $post->post_author, but that's only a good guess. + // Leave un-upgraded. + if ( ! $prev_revision ) { + continue; + } + + $prev_revision_version = _wp_get_post_revision_version( $prev_revision ); + + // If the previous revision is already up to date, it no longer has the information we need :( + if ( 0 < $prev_revision_version ) { + continue; + } + + // Upgrade this revision + // Cast as object so that wp_update_post() handles slashing for us + $update = (object) array( + 'ID' => $this_revision->ID, + 'post_name' => preg_replace( '/^(\d+-)(autosave|revision)-(\d+)$/', '$1$2-v1', $this_revision->post_name ), + 'post_author' => $prev_revision->post_author, + ); + //error_log(json_encode($update)); + $result = wp_update_post( $update ); + if ( ! $result || is_wp_error( $result ) ) { + // Wilhelm! + $success = false; + break; + } + } while ( $prev_revision ); + + delete_option( $lock ); + return true; +} + + function _show_post_preview() { if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) {