Skip to content

Commit

Permalink
REST API: Allow posts to be published with a publication date of midn…
Browse files Browse the repository at this point in the history
…ight 1970-01-01.

Explicitly checks date parsing return values for `false`, so that `0` (the value returned for the UNIX epoch of `1970-01-01 00:00:00`) is correctly treated as a valid timestamp.

It should be valid to create a post dated to any point in history.

Props emmanuel78, sabernhardt, siliconforks, drjosh07, antpb, TimothyBlynJacobs.
Fixes #60184.




git-svn-id: https://develop.svn.wordpress.org/trunk@59040 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
kadamwhite committed Sep 17, 2024
1 parent c1520f3 commit 71c69da
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/wp-includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,9 @@ function rest_get_avatar_sizes() {
/**
* Parses an RFC3339 time into a Unix timestamp.
*
* Explicitly check for `false` to detect failure, as zero is a valid return
* value on success.
*
* @since 4.4.0
*
* @param string $date RFC3339 timestamp.
Expand Down Expand Up @@ -1369,7 +1372,7 @@ function rest_get_date_with_gmt( $date, $is_utc = false ) {

$date = rest_parse_date( $date );

if ( empty( $date ) ) {
if ( false === $date ) {
return null;
}

Expand Down Expand Up @@ -2259,7 +2262,7 @@ function rest_validate_value_from_schema( $value, $args, $param = '' ) {
break;

case 'date-time':
if ( ! rest_parse_date( $value ) ) {
if ( false === rest_parse_date( $value ) ) {
return new WP_Error( 'rest_invalid_date', __( 'Invalid date.' ) );
}
break;
Expand Down
11 changes: 11 additions & 0 deletions tests/phpunit/tests/rest-api/rest-schema-validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,17 @@ public function test_nullable_date() {
$this->assertSame( 'Invalid date.', $error->get_error_message() );
}

/**
* @ticket 60184
*/
public function test_epoch() {
$schema = array(
'type' => 'string',
'format' => 'date-time',
);
$this->assertTrue( rest_validate_value_from_schema( '1970-01-01T00:00:00Z', $schema ) );
}

public function test_object_or_string() {
$schema = array(
'type' => array( 'object', 'string' ),
Expand Down

0 comments on commit 71c69da

Please sign in to comment.