Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions src/wp-includes/revision.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,19 @@ 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;
}

wp_save_post_revision( $post_id );
wp_save_post_revision(
$post_id,
! $update && 0 === get_current_user_id() ? $post->post_author : null
);
}

/**
Expand All @@ -123,11 +122,13 @@ function wp_save_post_revision_on_insert( $post_id, $post, $update ) {
* and the most recent revision always matches the current post.
*
* @since 2.6.0
* @since 7.1.0 The `$revision_author` parameter was added.
*
* @param int $post_id The ID of the post to save as a revision.
* @param int $post_id The ID of the post to save as a revision.
* @param int|null $revision_author Optional. The revision author ID. Default null.
* @return int|WP_Error|null Null or 0 if error, new revision ID, if success.
*/
function wp_save_post_revision( $post_id ) {
function wp_save_post_revision( $post_id, $revision_author = null ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return null;
}
Expand Down Expand Up @@ -214,7 +215,7 @@ function wp_save_post_revision( $post_id ) {
}
}

$return = _wp_put_post_revision( $post );
$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 @@ -344,14 +345,16 @@ function wp_is_post_autosave( $post ) {
* Inserts post data into the posts table as a post revision.
*
* @since 2.6.0
* @since 7.1.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 @@ -367,6 +370,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 @@ -2141,16 +2141,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 @@ -39,7 +39,7 @@ public function test_revisions_stores_meta_values_with_slashes( $passed, $expect
// Set up a new post.
$post_id = $this->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 @@ -121,17 +121,17 @@ public function test_revisions_stores_meta_values() {
$post_id = $this->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 @@ -149,7 +149,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 @@ -163,7 +163,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 @@ -181,7 +181,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 @@ -208,7 +208,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 @@ -228,7 +228,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