Posts: Create a new function for resolving the post date.

`wp_insert_post()` has a few checks using `post_date` and `post_date_gmt`, to determine the correct post date. This functionality is now extracted out into a new `wp_resolve_post_date()` function, allowing the checks to be reused elsewhere.

Props jmdodd.
Fixes #52187.



git-svn-id: https://develop.svn.wordpress.org/trunk@50012 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast
2021-01-25 01:06:25 +00:00
parent 04237613be
commit e2bb95aa50
2 changed files with 285 additions and 18 deletions
+242
View File
@@ -1395,4 +1395,246 @@ class Tests_Post extends WP_UnitTestCase {
$tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
$this->assertSameSets( array( $tag_2['term_id'], $tag_3['term_id'] ), $tags );
}
/**
* @ticket 52187
*/
public function test_insert_empty_post_date() {
$post_date_gmt = '2020-12-29 10:11:45';
$invalid_date = '2020-12-41 14:15:27';
// Empty post_date_gmt with floating status
$post_id = self::factory()->post->create(
array(
'post_status' => 'draft',
)
);
$post = get_post( $post_id );
$this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' );
$this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
$post_id = self::factory()->post->create(
array(
'post_date_gmt' => '0000-00-00 00:00:00',
'post_status' => 'draft',
)
);
$post = get_post( $post_id );
$this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' );
$this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
// Empty post_date_gmt without floating status
$post_id = self::factory()->post->create(
array(
'post_status' => 'publish',
)
);
$post = get_post( $post_id );
$this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' );
$this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( get_gmt_from_date( $post->post_date ) ), 2, 'The dates should be equal' );
$post_id = self::factory()->post->create(
array(
'post_date_gmt' => '0000-00-00 00:00:00',
'post_status' => 'publish',
)
);
$post = get_post( $post_id );
$this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' );
$this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( get_gmt_from_date( $post->post_date ) ), 2, 'The dates should be equal' );
// Valid post_date_gmt
$post_id = self::factory()->post->create(
array(
'post_date_gmt' => $post_date_gmt,
)
);
$post = get_post( $post_id );
$this->assertEquals( get_date_from_gmt( $post_date_gmt ), $post->post_date );
$this->assertEquals( $post_date_gmt, $post->post_date_gmt );
// Invalid post_date_gmt
$post_id = self::factory()->post->create(
array(
'post_date_gmt' => $invalid_date,
)
);
$post = get_post( $post_id );
$this->assertEquals( '1970-01-01 00:00:00', $post->post_date );
$this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
}
/**
* @ticket 52187
*/
public function test_insert_valid_post_date() {
$post_date = '2020-12-28 11:26:35';
$post_date_gmt = '2020-12-29 10:11:45';
$invalid_date = '2020-12-41 14:15:27';
// Empty post_date_gmt with floating status
$post_id = self::factory()->post->create(
array(
'post_date' => $post_date,
'post_status' => 'draft',
)
);
$post = get_post( $post_id );
$this->assertEquals( $post_date, $post->post_date );
$this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
$post_id = self::factory()->post->create(
array(
'post_date' => $post_date,
'post_date_gmt' => '0000-00-00 00:00:00',
'post_status' => 'draft',
)
);
$post = get_post( $post_id );
$this->assertEquals( $post_date, $post->post_date );
$this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
// Empty post_date_gmt without floating status
$post_id = self::factory()->post->create(
array(
'post_date' => $post_date,
'post_status' => 'publish',
)
);
$post = get_post( $post_id );
$this->assertEquals( $post_date, $post->post_date );
$this->assertEquals( get_gmt_from_date( $post_date ), $post->post_date_gmt );
$post_id = self::factory()->post->create(
array(
'post_date' => $post_date,
'post_date_gmt' => '0000-00-00 00:00:00',
'post_status' => 'publish',
)
);
$post = get_post( $post_id );
$this->assertEquals( $post_date, $post->post_date );
$this->assertEquals( get_gmt_from_date( $post_date ), $post->post_date_gmt );
// Valid post_date_gmt
$post_id = self::factory()->post->create(
array(
'post_date' => $post_date,
'post_date_gmt' => $post_date_gmt,
)
);
$post = get_post( $post_id );
$this->assertEquals( $post_date, $post->post_date );
$this->assertEquals( $post_date_gmt, $post->post_date_gmt );
// Invalid post_date_gmt
$post_id = self::factory()->post->create(
array(
'post_date' => $post_date,
'post_date_gmt' => $invalid_date,
)
);
$post = get_post( $post_id );
$this->assertEquals( $post_date, $post->post_date );
$this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
}
/**
* @ticket 52187
*/
public function test_insert_invalid_post_date() {
$post_date = '2020-12-28 11:26:35';
$post_date_gmt = '2020-12-29 10:11:45';
$invalid_date = '2020-12-41 14:15:27';
// Empty post_date_gmt with floating status
$post_id = self::factory()->post->create(
array(
'post_date' => $invalid_date,
'post_status' => 'draft',
)
);
$this->assertEquals( 0, $post_id );
$post_id = self::factory()->post->create(
array(
'post_date' => $invalid_date,
'post_date_gmt' => '0000-00-00 00:00:00',
'post_status' => 'draft',
)
);
$this->assertEquals( 0, $post_id );
// Empty post_date_gmt without floating status
$post_id = self::factory()->post->create(
array(
'post_date' => $invalid_date,
'post_status' => 'publish',
)
);
$this->assertEquals( 0, $post_id );
$post_id = self::factory()->post->create(
array(
'post_date' => $invalid_date,
'post_date_gmt' => '0000-00-00 00:00:00',
'post_status' => 'publish',
)
);
$this->assertEquals( 0, $post_id );
// Valid post_date_gmt
$post_id = self::factory()->post->create(
array(
'post_date' => $invalid_date,
'post_date_gmt' => $post_date_gmt,
)
);
$this->assertEquals( 0, $post_id );
// Invalid post_date_gmt
$post_id = self::factory()->post->create(
array(
'post_date' => $invalid_date,
'post_date_gmt' => $invalid_date,
)
);
$this->assertEquals( 0, $post_id );
}
/**
* @ticket 52187
*/
function test_wp_resolve_post_date() {
$post_date = '2020-12-28 11:26:35';
$post_date_gmt = '2020-12-29 10:11:45';
$invalid_date = '2020-12-41 14:15:27';
$resolved_post_date = wp_resolve_post_date();
$this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $resolved_post_date ), 2, 'The dates should be equal' );
$resolved_post_date = wp_resolve_post_date( '', $post_date_gmt );
$this->assertEquals( get_date_from_gmt( $post_date_gmt ), $resolved_post_date );
$resolved_post_date = wp_resolve_post_date( '', $invalid_date );
$this->assertEquals( '1970-01-01 00:00:00', $resolved_post_date );
$resolved_post_date = wp_resolve_post_date( $post_date );
$this->assertEquals( $post_date, $resolved_post_date );
$resolved_post_date = wp_resolve_post_date( $post_date, $post_date_gmt );
$this->assertEquals( $post_date, $resolved_post_date );
$resolved_post_date = wp_resolve_post_date( $post_date, $invalid_date );
$this->assertEquals( $post_date, $resolved_post_date );
$resolved_post_date = wp_resolve_post_date( $invalid_date );
$this->assertFalse( $resolved_post_date );
$resolved_post_date = wp_resolve_post_date( $invalid_date, $post_date_gmt );
$this->assertFalse( $resolved_post_date );
$resolved_post_date = wp_resolve_post_date( $invalid_date, $invalid_date );
$this->assertFalse( $resolved_post_date );
}
}