Skip to content
34 changes: 23 additions & 11 deletions src/wp-includes/revision.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,11 @@ function _wp_post_revision_data( $post = array(), $autosave = false ) {
*
* @since 6.4.0
*
* @param int $post_id The post id that was inserted.
* @param WP_Post $post The post object that was inserted.
* @param int $post_id The post ID that was inserted or updated.
* @param WP_Post $post The post object that was inserted or updated.
* @param bool $update Whether this insert is updating an existing post.
*/
function wp_save_post_revision_on_insert( $post_id, $post, $update ) {
if ( ! $update ) {
return;
}

if ( ! has_action( 'post_updated', 'wp_save_post_revision' ) ) {
return;
}
Expand Down Expand Up @@ -216,7 +212,16 @@ function wp_save_post_revision( $post_id ) {
}
}

$return = _wp_put_post_revision( $post );
/*
* With no current user (eg. imports or cron), credit the first revision
* to the post author instead of user 0.
*/
$revision_author = null;
if ( ! $revisions && 0 === get_current_user_id() ) {
$revision_author = (int) $post->post_author;
}

$return = _wp_put_post_revision( $post, false, $revision_author );

/*
* If a limit for the number of revisions to keep has been set,
Expand Down Expand Up @@ -348,14 +353,16 @@ function wp_is_post_autosave( $post ) {
* Inserts post data into the posts table as a post revision.
*
* @since 2.6.0
* @since 7.3.0 The `$post_author` parameter was added.
* @access private
*
* @param int|WP_Post|array|null $post Post ID, post object OR post array.
* @param bool $autosave Optional. Whether the revision is an autosave or not.
* Default false.
* @param int|WP_Post|array|null $post Post ID, post object OR post array.
* @param bool $autosave Optional. Whether the revision is an autosave or not.
* Default false.
* @param int|null $post_author Optional. The revision author ID. Default null.
* @return int|WP_Error WP_Error or 0 if error, new revision ID if success.
*/
function _wp_put_post_revision( $post = null, $autosave = false ) {
function _wp_put_post_revision( $post = null, $autosave = false, $post_author = null ) {
if ( is_object( $post ) ) {
$post = get_object_vars( $post );
} elseif ( ! is_array( $post ) ) {
Expand All @@ -371,6 +378,11 @@ function _wp_put_post_revision( $post = null, $autosave = false ) {
}

$post = _wp_post_revision_data( $post, $autosave );

if ( null !== $post_author ) {
$post['post_author'] = absint( $post_author );
}

$post = wp_slash( $post ); // Since data is from DB.

$revision_id = wp_insert_post( $post, true );
Expand Down
12 changes: 2 additions & 10 deletions src/wp-includes/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -2149,16 +2149,8 @@ function wp_update_custom_css_post( $css, $args = array() ) {
} else {
$r = wp_insert_post( wp_slash( $post_data ), true );

if ( ! is_wp_error( $r ) ) {
if ( get_stylesheet() === $args['stylesheet'] ) {
set_theme_mod( 'custom_css_post_id', $r );
}

// Trigger creation of a revision. This should be removed once #30854 is resolved.
$revisions = wp_get_latest_revision_id_and_total_count( $r );
if ( ! is_wp_error( $revisions ) && 0 === $revisions['count'] ) {
wp_save_post_revision( $r );
}
if ( ! is_wp_error( $r ) && get_stylesheet() === $args['stylesheet'] ) {
set_theme_mod( 'custom_css_post_id', $r );
}
}

Expand Down
18 changes: 9 additions & 9 deletions tests/phpunit/tests/post/metaRevisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function test_revisions_stores_meta_values_with_slashes( $passed, $expect
// Set up a new post.
$post_id = self::factory()->post->create();

// And update to store an initial revision.
// And update to store another revision.
wp_update_post(
array(
'post_content' => 'some initial content',
Expand Down Expand Up @@ -120,17 +120,17 @@ public function test_revisions_stores_meta_values() {
$post_id = self::factory()->post->create();
$original_post_id = $post_id;

// And update to store an initial revision.
// And update to store another revision.
wp_update_post(
array(
'post_content' => 'some initial content',
'ID' => $post_id,
)
);

// One revision so far.
// Two revisions so far, including the initial revision.
$revisions = wp_get_post_revisions( $post_id );
$this->assertCount( 1, $revisions );
$this->assertCount( 2, $revisions );

/*
* First set up a meta value.
Expand All @@ -148,7 +148,7 @@ public function test_revisions_stores_meta_values() {
);

$revisions = wp_get_post_revisions( $post_id );
$this->assertCount( 2, $revisions );
$this->assertCount( 3, $revisions );

// Next, store some updated meta values for the same key.
update_post_meta( $post_id, 'meta_revision_test', 'update1' );
Expand All @@ -162,7 +162,7 @@ public function test_revisions_stores_meta_values() {
);

$revisions = wp_get_post_revisions( $post_id );
$this->assertCount( 3, $revisions );
$this->assertCount( 4, $revisions );

/*
* Now restore the original revision.
Expand All @@ -180,7 +180,7 @@ public function test_revisions_stores_meta_values() {

wp_update_post( array( 'ID' => $post_id ) );
$revisions = wp_get_post_revisions( $post_id );
$this->assertCount( 4, $revisions );
$this->assertCount( 5, $revisions );

/*
* Check the meta values to verify they are NOT revisioned - they are not revisioned by default.
Expand All @@ -207,7 +207,7 @@ public function test_revisions_stores_meta_values() {
);

$revisions = array_values( wp_get_post_revisions( $post_id ) );
$this->assertCount( 5, $revisions );
$this->assertCount( 6, $revisions );
$this->assertSame( 'update2', get_post_meta( $revisions[0]->ID, 'meta_revision_test', true ) );

// Store custom meta values, which should now be revisioned.
Expand All @@ -227,7 +227,7 @@ public function test_revisions_stores_meta_values() {

// This revision contains the existing post meta ('update3').
$revisions = wp_get_post_revisions( $post_id );
$this->assertCount( 6, $revisions );
$this->assertCount( 7, $revisions );

// Verify that previous post meta is set.
$this->assertSame( 'update3', get_post_meta( $post_id, 'meta_revision_test', true ) );
Expand Down
Loading
Loading