Skip to content

Commit

Permalink
Posts: Create a new function for resolving the post date.
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
pento committed Jan 25, 2021
1 parent 0423761 commit e2bb95a
Show file tree
Hide file tree
Showing 2 changed files with 285 additions and 18 deletions.
61 changes: 43 additions & 18 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -3702,6 +3702,8 @@ function wp_insert_post( $postarr, $wp_error = false, $fire_after_hooks = true )
'guid' => '',
'import_id' => 0,
'context' => '',
'post_date' => '',
'post_date_gmt' => '',
);

$postarr = wp_parse_args( $postarr, $defaults );
Expand Down Expand Up @@ -3835,25 +3837,11 @@ function wp_insert_post( $postarr, $wp_error = false, $fire_after_hooks = true )
}

/*
* If the post date is empty (due to having been new or a draft) and status
* is not 'draft' or 'pending', set date to now.
* Resolve the post date from any provided post date or post date GMT strings;
* if none are provided, the date will be set to now.
*/
if ( empty( $postarr['post_date'] ) || '0000-00-00 00:00:00' === $postarr['post_date'] ) {
if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' === $postarr['post_date_gmt'] ) {
$post_date = current_time( 'mysql' );
} else {
$post_date = get_date_from_gmt( $postarr['post_date_gmt'] );
}
} else {
$post_date = $postarr['post_date'];
}

// Validate the date.
$mm = substr( $post_date, 5, 2 );
$jj = substr( $post_date, 8, 2 );
$aa = substr( $post_date, 0, 4 );
$valid_date = wp_checkdate( $mm, $jj, $aa, $post_date );
if ( ! $valid_date ) {
$post_date = wp_resolve_post_date( $postarr['post_date'], $postarr['post_date_gmt'] );
if ( ! $post_date ) {
if ( $wp_error ) {
return new WP_Error( 'invalid_date', __( 'Invalid date.' ) );
} else {
Expand Down Expand Up @@ -4538,6 +4526,43 @@ function check_and_publish_future_post( $post_id ) {
wp_publish_post( $post_id );
}

/**
* Uses wp_checkdate to return a valid Gregorian-calendar value for post_date.
* If post_date is not provided, this first checks post_date_gmt if provided,
* then falls back to use the current time.
*
* For back-compat purposes in wp_insert_post, an empty post_date and an invalid
* post_date_gmt will continue to return '1970-01-01 00:00:00' rather than false.
*
* @since 5.7.0
*
* @param string $post_date The date in mysql format.
* @param string $post_date_gmt The GMT date in mysql format.
* @return string|false A valid Gregorian-calendar date string, or false on failure.
*/
function wp_resolve_post_date( $post_date = '', $post_date_gmt = '' ) {
// If the date is empty, set the date to now.
if ( empty( $post_date ) || '0000-00-00 00:00:00' === $post_date ) {
if ( empty( $post_date_gmt ) || '0000-00-00 00:00:00' === $post_date_gmt ) {
$post_date = current_time( 'mysql' );
} else {
$post_date = get_date_from_gmt( $post_date_gmt );
}
}

// Validate the date.
$month = substr( $post_date, 5, 2 );
$day = substr( $post_date, 8, 2 );
$year = substr( $post_date, 0, 4 );

$valid_date = wp_checkdate( $month, $day, $year, $post_date );

if ( ! $valid_date ) {
return false;
}
return $post_date;
}

/**
* Computes a unique slug for the post, when given the desired slug and some post details.
*
Expand Down
242 changes: 242 additions & 0 deletions tests/phpunit/tests/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -1395,4 +1395,246 @@ public function test_update_post_should_only_modify_post_tags_if_different_tags_
$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 );
}
}

0 comments on commit e2bb95a

Please sign in to comment.