From 56d4e7fb86f3ecdda93e9a4f79f84f1655c85174 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 29 Aug 2019 23:17:30 +0000 Subject: [PATCH] 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 --- src/wp-includes/functions.php | 12 +++++++++--- tests/phpunit/tests/date/currentTime.php | 2 +- tests/phpunit/tests/date/dateI18n.php | 15 +++++++++++++++ tests/phpunit/tests/date/theDate.php | 2 +- tests/phpunit/tests/date/wpDate.php | 15 +++++++++++++++ tests/phpunit/tests/date/wpTimezone.php | 2 +- 6 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 tests/phpunit/tests/date/wpDate.php diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 8a7897a909..3acfe7318e 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -163,8 +163,12 @@ function wp_timezone() { * @return string The date, translated if locale specifies it. */ 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). - $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. @@ -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 DateTimeZone $timezone Optional. Timezone to output result in. Defaults to timezone * 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 ) { global $wp_locale; - if ( ! $timestamp ) { + if ( null === $timestamp ) { $timestamp = time(); + } elseif ( ! is_numeric( $timestamp ) ) { + return false; } if ( ! $timezone ) { diff --git a/tests/phpunit/tests/date/currentTime.php b/tests/phpunit/tests/date/currentTime.php index 8a38574d98..836f2737ee 100644 --- a/tests/phpunit/tests/date/currentTime.php +++ b/tests/phpunit/tests/date/currentTime.php @@ -4,7 +4,7 @@ * @group date * @group datetime */ -class Tests_Date_CurrentTime extends WP_UnitTestCase { +class Tests_Date_Current_Time extends WP_UnitTestCase { /** * @ticket 37440 diff --git a/tests/phpunit/tests/date/dateI18n.php b/tests/phpunit/tests/date/dateI18n.php index 8f98c93ee6..5a13e3c437 100644 --- a/tests/phpunit/tests/date/dateI18n.php +++ b/tests/phpunit/tests/date/dateI18n.php @@ -5,6 +5,20 @@ * @group datetime */ 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() { $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', '' ); $offset = $datetimezone->getOffset( new DateTime() ) / 3600; update_option( 'gmt_offset', $offset ); + $datetime = new DateTime( 'now', $datetimezone ); $datetime = new DateTime( $datetime->format( 'P' ) ); diff --git a/tests/phpunit/tests/date/theDate.php b/tests/phpunit/tests/date/theDate.php index 1f6c1d1a42..2542240d6b 100644 --- a/tests/phpunit/tests/date/theDate.php +++ b/tests/phpunit/tests/date/theDate.php @@ -4,7 +4,7 @@ * @group date * @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. */ protected $hooks_called = array( diff --git a/tests/phpunit/tests/date/wpDate.php b/tests/phpunit/tests/date/wpDate.php new file mode 100644 index 0000000000..d3fb58aba2 --- /dev/null +++ b/tests/phpunit/tests/date/wpDate.php @@ -0,0 +1,15 @@ +assertFalse( wp_date( DATE_RFC3339, 'invalid' ) ); + } +} diff --git a/tests/phpunit/tests/date/wpTimezone.php b/tests/phpunit/tests/date/wpTimezone.php index b4ab35ea9b..359ab1244f 100644 --- a/tests/phpunit/tests/date/wpTimezone.php +++ b/tests/phpunit/tests/date/wpTimezone.php @@ -4,7 +4,7 @@ * @group date * @group datetime */ -class Tests_WP_Timezone extends WP_UnitTestCase { +class Tests_Date_WP_Timezone extends WP_UnitTestCase { /** * @ticket 24730