Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Posts: Preserve post_date when transitioning to future status in wp_update_post() #8118

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -5129,8 +5129,11 @@ function wp_update_post( $postarr = array(), $wp_error = false, $fire_after_hook
}

// Drafts shouldn't be assigned a date unless explicitly done so by the user.
// Additionally, if the post_status is being updated to 'publish', 'future', or 'private',
// do not clear the date, as the provided date should take precedence.
if ( isset( $post['post_status'] )
&& in_array( $post['post_status'], array( 'draft', 'pending', 'auto-draft' ), true )
&& ( ! isset( $postarr['post_status'] ) || ! in_array( $postarr['post_status'], array( 'publish', 'future', 'private' ), true ) )
&& empty( $postarr['edit_date'] ) && ( '0000-00-00 00:00:00' === $post['post_date_gmt'] )
) {
$clear_date = true;
Expand Down
68 changes: 68 additions & 0 deletions tests/phpunit/tests/post/wpUpdatePostDateStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/**
* Test wp_update_post() date handling when changing post status
*
* @group post
* @covers ::wp_update_post
*/
class Tests_Post_wpUpdatePostDateStatus extends WP_UnitTestCase {

/**
* Data provider for test_update_post_preserves_date
*
* @return array[] Test parameters
*/
public function data_post_status_transitions() {
return array(
'pending to future' => array(
'initial_status' => 'pending',
),
'draft to future' => array(
'initial_status' => 'draft',
),
);
}

/**
* Test that wp_update_post() preserves post_date when changing to future status
*
* @ticket 62468
*
* @dataProvider data_post_status_transitions
* @covers ::wp_update_post
*
* @param string $initial_status Initial post status
*/
public function test_update_post_preserves_date( $initial_status ) {
$post_data = array(
'post_title' => 'Test Post',
'post_content' => 'Test content',
'post_status' => $initial_status,
'post_author' => self::factory()->user->create( array( 'role' => 'editor' ) ),
);

$post_id = wp_insert_post( $post_data );
$this->assertIsInt( $post_id );

// Set future date (1 day from now)
$future_date = gmdate( 'Y-m-d H:i:s', strtotime( '+1 day' ) );
$future_date_gmt = get_gmt_from_date( $future_date );

$update_data = array(
'ID' => $post_id,
'post_status' => 'future',
'post_date' => $future_date,
'post_date_gmt' => $future_date_gmt,
);

$update_result = wp_update_post( $update_data );
$this->assertIsInt( $update_result );

$updated_post = get_post( $post_id );

$this->assertEquals( 'future', $updated_post->post_status );
$this->assertEquals( $future_date, $updated_post->post_date );
$this->assertEquals( $future_date_gmt, $updated_post->post_date_gmt );
}
}
Loading