Skip to content
Closed
2 changes: 2 additions & 0 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@
// Create a revision whenever a post is updated.
add_action( 'wp_after_insert_post', 'wp_save_post_revision_on_insert', 9, 3 );
add_action( 'post_updated', 'wp_save_post_revision', 10, 1 );
// Fired in `wp_transition_post_status` for newly created posts with status 'publish'.
add_action( 'new_to_publish', '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 );
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
79 changes: 66 additions & 13 deletions tests/phpunit/tests/post/revisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public function test_revision_view_caps_post() {
);

$revisions = wp_get_post_revisions( $post_id );
$this->assertCount( 1, $revisions );
$this->assertCount( 2, $revisions );
$this->assertTrue( user_can( self::$editor_user_id, 'read_post', $post_id ) );

$this->assertNotEmpty( $revisions );
Expand Down Expand Up @@ -262,7 +262,7 @@ public function test_revision_restore_caps_post() {
);

$revisions = wp_get_post_revisions( $post_id );
$this->assertCount( 1, $revisions );
$this->assertCount( 2, $revisions );
foreach ( $revisions as $revision ) {
$this->assertTrue( user_can( self::$editor_user_id, 'edit_post', $revision->post_parent ) );
}
Expand Down Expand Up @@ -300,7 +300,7 @@ public function test_revision_diff_caps_post() {

// Diff checks if you can read both left and right revisions.
$revisions = wp_get_post_revisions( $post_id );
$this->assertCount( 2, $revisions );
$this->assertCount( 3, $revisions );
foreach ( $revisions as $revision ) {
$this->assertTrue( user_can( self::$editor_user_id, 'read_post', $revision->ID ) );
}
Expand Down Expand Up @@ -340,7 +340,7 @@ public function test_revision_view_caps_cpt() {
);

$revisions = wp_get_post_revisions( $post_id );
$this->assertCount( 1, $revisions );
$this->assertCount( 2, $revisions );
$this->assertTrue( user_can( self::$editor_user_id, 'read_post', $post_id ) );

foreach ( $revisions as $revision ) {
Expand Down Expand Up @@ -387,7 +387,7 @@ public function test_revision_restore_caps_cpt() {
);

$revisions = wp_get_post_revisions( $post_id );
$this->assertCount( 1, $revisions );
$this->assertCount( 2, $revisions );
foreach ( $revisions as $revision ) {
$this->assertTrue( user_can( self::$editor_user_id, 'edit_post', $revision->post_parent ) );
}
Expand Down Expand Up @@ -495,7 +495,7 @@ public function test_revision_diff_caps_cpt() {

// Diff checks if you can read both left and right revisions.
$revisions = wp_get_post_revisions( $post_id );
$this->assertCount( 2, $revisions );
$this->assertCount( 3, $revisions );
foreach ( $revisions as $revision ) {
$this->assertTrue( user_can( self::$editor_user_id, 'read_post', $revision->ID ) );
}
Expand Down Expand Up @@ -539,6 +539,8 @@ public function test_wp_get_post_revisions_should_order_by_post_date() {

$revisions = wp_get_post_revisions( $post['ID'] );

array_unshift( $revision_ids, reset( $revisions )->ID );

$this->assertSame( $revision_ids, array_values( wp_list_pluck( $revisions, 'ID' ) ) );
}

Expand Down Expand Up @@ -574,6 +576,8 @@ public function test_wp_get_post_revisions_should_order_by_ID_when_post_date_mat

$revisions = wp_get_post_revisions( $post['ID'] );

array_unshift( $revision_ids, reset( $revisions )->ID );

$this->assertSame( $revision_ids, array_values( wp_list_pluck( $revisions, 'ID' ) ) );
}

Expand Down Expand Up @@ -705,6 +709,38 @@ public function test_wp_get_latest_revision_id_and_total_count_no_revisions() {
$this->assertSame( $revision->get_error_code(), 'revisions_not_enabled' );
}

/**
* @ticket 30854
*/
public function test_wp_first_revision_is_not_lost() {
$post = self::factory()->post->create_and_get(
array(
'post_title' => 'some-post',
'post_type' => 'post',
'post_content' => 'Initial Content',
)
);

wp_update_post(
array(
'ID' => $post->ID,
'post_content' => 'Update #1',
)
);

wp_update_post(
array(
'ID' => $post->ID,
'post_content' => 'Update #2',
)
);

$revisions = wp_get_post_revisions( $post->ID );
$earliest_revision = end( $revisions );

$this->assertSame( 'Initial Content', $earliest_revision->post_content );
}

/**
* Tests that wp_get_post_revisions_url() returns the revisions URL.
*
Expand All @@ -719,7 +755,12 @@ public function test_wp_get_latest_revision_id_and_total_count_no_revisions() {
public function test_wp_get_post_revisions_url( $revisions ) {
wp_set_current_user( self::$admin_user_id );

$post_id = self::factory()->post->create( array( 'post_title' => 'Some Post' ) );
$post_id = self::factory()->post->create(
array(
'post_title' => 'Some Post',
'post_status' => 'draft',
)
);
$latest_revision_id = null;

if ( 0 !== $revisions ) {
Expand All @@ -728,8 +769,9 @@ public function test_wp_get_post_revisions_url( $revisions ) {
for ( $i = 0; $i < $revisions; ++$i ) {
wp_update_post(
array(
'ID' => $post_id,
'post_title' => 'Some Post ' . $i,
'ID' => $post_id,
'post_title' => 'Some Post ' . $i,
'post_status' => 'publish',
)
);

Expand Down Expand Up @@ -767,7 +809,12 @@ public function test_wp_get_post_revisions_url( $revisions ) {
public function test_wp_get_post_revisions_url_with_post_object( $revisions ) {
wp_set_current_user( self::$admin_user_id );

$post = self::factory()->post->create_and_get( array( 'post_title' => 'Some Post' ) );
$post = self::factory()->post->create_and_get(
array(
'post_title' => 'Some Post',
'post_status' => 'draft',
)
);
$latest_revision_id = null;

if ( 0 !== $revisions ) {
Expand All @@ -776,8 +823,9 @@ public function test_wp_get_post_revisions_url_with_post_object( $revisions ) {
for ( $i = 0; $i < $revisions; ++$i ) {
wp_update_post(
array(
'ID' => $post->ID,
'post_title' => 'Some Post ' . $i,
'ID' => $post->ID,
'post_title' => 'Some Post ' . $i,
'post_status' => 'publish',
)
);

Expand Down Expand Up @@ -834,7 +882,12 @@ public function test_wp_get_post_revisions_url_returns_null_when_post_does_not_e
*/
public function test_wp_get_post_revisions_url_returns_null_with_no_revisions() {
wp_set_current_user( self::$admin_user_id );
$post_id = self::factory()->post->create( array( 'post_title' => 'Some Post' ) );
$post_id = self::factory()->post->create(
array(
'post_title' => 'Some Post',
'post_status' => 'draft',
)
);
$this->assertNull( wp_get_post_revisions_url( $post_id ) );
}

Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/xmlrpc/wp/getRevisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function test_revision_count() {

$result = $this->myxmlrpcserver->wp_getRevisions( array( 1, 'editor', 'editor', $post_id ) );
$this->assertIsArray( $result );
$this->assertCount( 1, $result );
$this->assertCount( 2, $result );

wp_insert_post(
array(
Expand All @@ -53,7 +53,7 @@ public function test_revision_count() {

$result = $this->myxmlrpcserver->wp_getRevisions( array( 1, 'editor', 'editor', $post_id ) );
$this->assertIsArray( $result );
$this->assertCount( 2, $result );
$this->assertCount( 3, $result );
}

/**
Expand Down
Loading