diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 3c1234f9cfa8e..560f1e007e9c7 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -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 ); diff --git a/src/wp-includes/theme.php b/src/wp-includes/theme.php index e27f3e15003d5..0487336ea59af 100644 --- a/src/wp-includes/theme.php +++ b/src/wp-includes/theme.php @@ -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 ); } } diff --git a/tests/phpunit/tests/post/revisions.php b/tests/phpunit/tests/post/revisions.php index c208ccfd37c05..174558fbce573 100644 --- a/tests/phpunit/tests/post/revisions.php +++ b/tests/phpunit/tests/post/revisions.php @@ -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 ); @@ -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 ) ); } @@ -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 ) ); } @@ -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 ) { @@ -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 ) ); } @@ -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 ) ); } @@ -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' ) ) ); } @@ -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' ) ) ); } @@ -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. * @@ -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 ) { @@ -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', ) ); @@ -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 ) { @@ -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', ) ); @@ -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 ) ); } diff --git a/tests/phpunit/tests/xmlrpc/wp/getRevisions.php b/tests/phpunit/tests/xmlrpc/wp/getRevisions.php index 01d8614460d8d..1272f60564027 100644 --- a/tests/phpunit/tests/xmlrpc/wp/getRevisions.php +++ b/tests/phpunit/tests/xmlrpc/wp/getRevisions.php @@ -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( @@ -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 ); } /**