Make WordPress Core

Ticket #28636: localized-time-invalid-input.patch

File localized-time-invalid-input.patch, 3.1 KB (added by Rarst, 4 years ago)
  • tests/phpunit/tests/date/wpDate.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
     1<?php
     2
     3/**
     4 * @group date
     5 * @group datetime
     6 */
     7class Tests_Date_Wp_Date extends WP_UnitTestCase {
     8
     9        public function test_should_return_false_on_invalid_timestamp() {
     10
     11                $this->assertFalse( wp_date( DATE_RFC3339, 'invalid' ) );
     12        }
     13}
  • src/wp-includes/functions.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    163163 * @return string The date, translated if locale specifies it.
    164164 */
    165165function date_i18n( $format, $timestamp_with_offset = false, $gmt = false ) {
     166
     167        $timestamp = $timestamp_with_offset;
     168
    166169        // If timestamp is omitted it should be current time (summed with offset, unless `$gmt` is true).
    167         $timestamp = $timestamp_with_offset ? $timestamp_with_offset : current_time( 'timestamp', $gmt );
     170        if ( ! is_numeric( $timestamp ) ) {
     171                $timestamp = current_time( 'timestamp', $gmt );
     172        }
    168173
    169174        /*
    170175         * This is a legacy implementation quirk that the returned timestamp is also with offset.
     
    218223 * @param int          $timestamp Optional. Unix timestamp. Defaults to current time.
    219224 * @param DateTimeZone $timezone  Optional. Timezone to output result in. Defaults to timezone
    220225 *                                from site settings.
    221  * @return string The date, translated if locale specifies it.
     226 *
     227 * @return string|false The date, translated if locale specifies it. False on invalid timestamp input.
    222228 */
    223229function wp_date( $format, $timestamp = null, $timezone = null ) {
    224230        global $wp_locale;
    225231
    226         if ( ! $timestamp ) {
     232        if ( null === $timestamp ) {
    227233                $timestamp = time();
     234        } elseif ( ! is_numeric( $timestamp ) ) {
     235                return false;
    228236        }
    229237
    230238        if ( ! $timezone ) {
  • tests/phpunit/tests/date/dateI18n.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    55 * @group datetime
    66 */
    77class Tests_Date_I18n extends WP_UnitTestCase {
     8
     9        public function test_should_return_current_time_on_invalid_timestamp() {
     10                $timezone = 'Europe/Kiev';
     11                update_option( 'timezone_string', $timezone );
     12                $datetime     = new DateTime( 'now', new DateTimeZone( $timezone ) );
     13                $wp_timestamp = $datetime->getTimestamp() + $datetime->getOffset();
     14
     15                $this->assertEquals( $wp_timestamp, date_i18n( 'U', 'invalid' ), '', 5 );
     16        }
     17
    818        public function test_should_format_date() {
    919                $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 );
    1020        }