Skip to content

Commit 98b44fa

Browse files
committed
Preserve post_date when transitioning to future status
1 parent 9d9d73c commit 98b44fa

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/wp-includes/post.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5129,8 +5129,11 @@ function wp_update_post( $postarr = array(), $wp_error = false, $fire_after_hook
51295129
}
51305130

51315131
// Drafts shouldn't be assigned a date unless explicitly done so by the user.
5132+
// Additionally, if the post_status is being updated to 'publish', 'future', or 'private',
5133+
// do not clear the date, as the provided date should take precedence.
51325134
if ( isset( $post['post_status'] )
51335135
&& in_array( $post['post_status'], array( 'draft', 'pending', 'auto-draft' ), true )
5136+
&& ( ! isset( $postarr['post_status'] ) || ! in_array( $postarr['post_status'], array( 'publish', 'future', 'private' ), true ) )
51345137
&& empty( $postarr['edit_date'] ) && ( '0000-00-00 00:00:00' === $post['post_date_gmt'] )
51355138
) {
51365139
$clear_date = true;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/**
4+
* Test wp_update_post() date handling when changing post status
5+
*
6+
* @group post
7+
* @covers ::wp_update_post
8+
*/
9+
class Tests_Post_wpUpdatePostDateStatus extends WP_UnitTestCase {
10+
11+
/**
12+
* Data provider for test_update_post_preserves_date
13+
*
14+
* @return array[] Test parameters
15+
*/
16+
public function data_post_status_transitions() {
17+
return array(
18+
'pending to future' => array(
19+
'initial_status' => 'pending',
20+
),
21+
'draft to future' => array(
22+
'initial_status' => 'draft',
23+
),
24+
);
25+
}
26+
27+
/**
28+
* Test that wp_update_post() preserves post_date when changing to future status
29+
*
30+
* @ticket 62468
31+
*
32+
* @dataProvider data_post_status_transitions
33+
* @covers ::wp_update_post
34+
*
35+
* @param string $initial_status Initial post status
36+
*/
37+
public function test_update_post_preserves_date( $initial_status ) {
38+
$post_data = array(
39+
'post_title' => 'Test Post',
40+
'post_content' => 'Test content',
41+
'post_status' => $initial_status,
42+
'post_author' => self::factory()->user->create( array( 'role' => 'editor' ) ),
43+
);
44+
45+
$post_id = wp_insert_post( $post_data );
46+
$this->assertIsInt( $post_id );
47+
48+
// Set future date (1 day from now)
49+
$future_date = gmdate( 'Y-m-d H:i:s', strtotime( '+1 day' ) );
50+
$future_date_gmt = get_gmt_from_date( $future_date );
51+
52+
$update_data = array(
53+
'ID' => $post_id,
54+
'post_status' => 'future',
55+
'post_date' => $future_date,
56+
'post_date_gmt' => $future_date_gmt,
57+
);
58+
59+
$update_result = wp_update_post( $update_data );
60+
$this->assertIsInt( $update_result );
61+
62+
$updated_post = get_post( $post_id );
63+
64+
$this->assertEquals( 'future', $updated_post->post_status );
65+
$this->assertEquals( $future_date, $updated_post->post_date );
66+
$this->assertEquals( $future_date_gmt, $updated_post->post_date_gmt );
67+
}
68+
}

0 commit comments

Comments
 (0)