Date/Time: Restore the previous behavior of date_i18n() where invalid input would result in current time.

Make `wp_date()` return `false` on invalid timestamp input, for consistency with upstream PHP `date()` function.

Props Rarst.
Fixes #28636.

git-svn-id: https://develop.svn.wordpress.org/trunk@45914 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2019-08-29 23:17:30 +00:00
parent 07ec966f17
commit 56d4e7fb86
6 changed files with 42 additions and 6 deletions

View File

@ -163,8 +163,12 @@ function wp_timezone() {
* @return string The date, translated if locale specifies it. * @return string The date, translated if locale specifies it.
*/ */
function date_i18n( $format, $timestamp_with_offset = false, $gmt = false ) { function date_i18n( $format, $timestamp_with_offset = false, $gmt = false ) {
$timestamp = $timestamp_with_offset;
// If timestamp is omitted it should be current time (summed with offset, unless `$gmt` is true). // If timestamp is omitted it should be current time (summed with offset, unless `$gmt` is true).
$timestamp = $timestamp_with_offset ? $timestamp_with_offset : current_time( 'timestamp', $gmt ); if ( ! is_numeric( $timestamp ) ) {
$timestamp = current_time( 'timestamp', $gmt );
}
/* /*
* This is a legacy implementation quirk that the returned timestamp is also with offset. * This is a legacy implementation quirk that the returned timestamp is also with offset.
@ -218,13 +222,15 @@ function date_i18n( $format, $timestamp_with_offset = false, $gmt = false ) {
* @param int $timestamp Optional. Unix timestamp. Defaults to current time. * @param int $timestamp Optional. Unix timestamp. Defaults to current time.
* @param DateTimeZone $timezone Optional. Timezone to output result in. Defaults to timezone * @param DateTimeZone $timezone Optional. Timezone to output result in. Defaults to timezone
* from site settings. * from site settings.
* @return string The date, translated if locale specifies it. * @return string|false The date, translated if locale specifies it. False on invalid timestamp input.
*/ */
function wp_date( $format, $timestamp = null, $timezone = null ) { function wp_date( $format, $timestamp = null, $timezone = null ) {
global $wp_locale; global $wp_locale;
if ( ! $timestamp ) { if ( null === $timestamp ) {
$timestamp = time(); $timestamp = time();
} elseif ( ! is_numeric( $timestamp ) ) {
return false;
} }
if ( ! $timezone ) { if ( ! $timezone ) {

View File

@ -4,7 +4,7 @@
* @group date * @group date
* @group datetime * @group datetime
*/ */
class Tests_Date_CurrentTime extends WP_UnitTestCase { class Tests_Date_Current_Time extends WP_UnitTestCase {
/** /**
* @ticket 37440 * @ticket 37440

View File

@ -5,6 +5,20 @@
* @group datetime * @group datetime
*/ */
class Tests_Date_I18n extends WP_UnitTestCase { class Tests_Date_I18n extends WP_UnitTestCase {
/**
* @ticket 28636
*/
public function test_should_return_current_time_on_invalid_timestamp() {
$timezone = 'Europe/Kiev';
update_option( 'timezone_string', $timezone );
$datetime = new DateTime( 'now', new DateTimeZone( $timezone ) );
$wp_timestamp = $datetime->getTimestamp() + $datetime->getOffset();
$this->assertEquals( $wp_timestamp, date_i18n( 'U', 'invalid' ), '', 5 );
}
public function test_should_format_date() { public function test_should_format_date() {
$this->assertEquals( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( date_i18n( 'Y-m-d H:i:s' ) ), 'The dates should be equal', 2 ); $this->assertEquals( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( date_i18n( 'Y-m-d H:i:s' ) ), 'The dates should be equal', 2 );
} }
@ -74,6 +88,7 @@ class Tests_Date_I18n extends WP_UnitTestCase {
update_option( 'timezone_string', '' ); update_option( 'timezone_string', '' );
$offset = $datetimezone->getOffset( new DateTime() ) / 3600; $offset = $datetimezone->getOffset( new DateTime() ) / 3600;
update_option( 'gmt_offset', $offset ); update_option( 'gmt_offset', $offset );
$datetime = new DateTime( 'now', $datetimezone ); $datetime = new DateTime( 'now', $datetimezone );
$datetime = new DateTime( $datetime->format( 'P' ) ); $datetime = new DateTime( $datetime->format( 'P' ) );

View File

@ -4,7 +4,7 @@
* @group date * @group date
* @group datetime * @group datetime
*/ */
class Tests_Date_TheDate extends WP_UnitTestCase { class Tests_Date_The_Date extends WP_UnitTestCase {
/** @var array $hooks_called Count of hooks called. */ /** @var array $hooks_called Count of hooks called. */
protected $hooks_called = array( protected $hooks_called = array(

View File

@ -0,0 +1,15 @@
<?php
/**
* @group date
* @group datetime
*/
class Tests_Date_WP_Date extends WP_UnitTestCase {
/**
* @ticket 28636
*/
public function test_should_return_false_on_invalid_timestamp() {
$this->assertFalse( wp_date( DATE_RFC3339, 'invalid' ) );
}
}

View File

@ -4,7 +4,7 @@
* @group date * @group date
* @group datetime * @group datetime
*/ */
class Tests_WP_Timezone extends WP_UnitTestCase { class Tests_Date_WP_Timezone extends WP_UnitTestCase {
/** /**
* @ticket 24730 * @ticket 24730