WordPress.org

Make WordPress Core

Ticket #28636: wp-date.patch

File wp-date.patch, 8.1 KB (added by Rarst, 15 months ago)
  • src/wp-includes/default-filters.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    209209add_filter( 'widget_text_content', 'shortcode_unautop' );
    210210add_filter( 'widget_text_content', 'do_shortcode', 11 ); // Runs after wpautop(); note that $post global will be null when shortcodes run.
    211211
    212 add_filter( 'date_i18n', 'wp_maybe_decline_date' );
    213 
    214212// RSS filters
    215213add_filter( 'the_title_rss', 'strip_tags' );
    216214add_filter( 'the_title_rss', 'ent2ncr', 8 );
  • src/wp-includes/functions.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    149149 *
    150150 * @global WP_Locale $wp_locale WordPress date and time locale object.
    151151 *
    152  * @param string   $dateformatstring      Format to display the date.
     152 * @param string   $format                Format to display the date.
    153153 * @param int|bool $timestamp_with_offset Optional. A sum of Unix timestamp and timezone offset in seconds.
    154154 *                                        Default false.
    155155 * @param bool     $gmt                   Optional. Whether to use GMT timezone. Only applies if timestamp is
     
    157157 *
    158158 * @return string The date, translated if locale specifies it.
    159159 */
    160 function date_i18n( $dateformatstring, $timestamp_with_offset = false, $gmt = false ) {
    161         global $wp_locale;
    162 
    163         $i = $timestamp_with_offset;
     160function date_i18n( $format, $timestamp_with_offset = false, $gmt = false ) {
    164161
    165         if ( ! is_numeric( $i ) ) {
    166                 $i = current_time( 'timestamp', $gmt );
    167         }
     162        // If timestamp is omitted it should be current time (summed with offset, unless `$gmt` is true).
     163        $timestamp = $timestamp_with_offset ?: current_time( 'timestamp', $gmt );
    168164
    169165        /*
    170          * Store original value for language with untypical grammars.
    171          * See https://core.trac.wordpress.org/ticket/9396
     166         * This is a legacy implementation quirk that the returned timestamp is also with offset.
     167         * Ideally this function should never be used to produce a timestamp.
    172168         */
    173         $req_format = $dateformatstring;
    174         $new_format = '';
    175 
    176         // We need to unpack shorthand `r` format because it has parts that might be localized.
    177         $dateformatstring = preg_replace( '/(?<!\\\\)r/', DATE_RFC2822, $dateformatstring );
    178 
    179         /*
    180          * Timestamp with offset is typically produced by a UTC `strtotime()` call on an input without timezone.
    181          * This is the best attempt to reverse that operation into a local time to use.
    182          */
    183         $local_time = gmdate( 'Y-m-d H:i:s', $i );
    184         $gmt_mode   = $gmt && ( false === $timestamp_with_offset );
    185         $timezone   = $gmt_mode ? new DateTimeZone( 'UTC' ) : wp_timezone();
    186         $datetime   = date_create( $local_time, $timezone );
     169        if ( 'U' === $format ) {
     170                $date = $timestamp;
     171        } elseif ( $gmt && ( false === $timestamp_with_offset ) ) { // Current time in UTC.
     172                $date = wp_date( $format, null, new DateTimeZone( 'UTC' ) );
     173        } elseif ( false === $timestamp_with_offset ) { // Current time in site's time zone.
     174                $date = wp_date( $format );
     175        } else {
     176                /*
     177                 * Timestamp with offset is typically produced by a UTC `strtotime()` call on an input without timezone.
     178                 * This is the best attempt to reverse that operation into a local time to use.
     179                 */
     180                $local_time = gmdate( 'Y-m-d H:i:s', $timestamp );
     181                $timezone   = wp_timezone();
     182                $datetime   = date_create( $local_time, $timezone );
     183                $date       = wp_date( $format, $datetime->getTimestamp(), $timezone );
     184        }
     185
     186        /**
     187         * Filters the date formatted based on the locale.
     188         *
     189         * @param string $date                  Formatted date string.
     190         * @param string $format                Format to display the date.
     191         * @param int    $timestamp             A sum of Unix timestamp and timezone offset in seconds.
     192         *                                      Might be without offset if input omitted timestamp but requested GMT.
     193         * @param bool   $gmt                   Whether to use GMT timezone. Only applies if timestamp was
     194         *                                      not provided. Default false.
     195         *
     196         * @since 2.8.0
     197         *
     198         */
     199        $date = apply_filters( 'date_i18n', $date, $format, $timestamp, $gmt );
     200
     201        return $date;
     202}
    187203
    188         /*
    189          * This is a legacy implementation quirk that the returned timestamp is also with offset.
    190          * Ideally this function should never be used to produce a timestamp.
    191          */
    192         $timestamp_mode = ( 'U' === $dateformatstring );
     204/**
     205 * Retrieves the date, in localized format.
     206 *
     207 * This is a newer function, intended to replace `date_i18n()` without legacy quirks in it.
     208 *
     209 * Note, that unlike `date_18n()` this function accepts a true Unix timestamp, not summed with time zone offset.
     210 *
     211 * @param string       $format    PHP date format.
     212 * @param int          $timestamp Optional. Unix timestamp. Defaults to current time.
     213 * @param DateTimeZone $timezone  Optional. Timezone to output result in. Defaults to timezone from WordPress settings.
     214 *
     215 * @return string The date, translated if locale specifies it.
     216 */
     217function wp_date( $format, $timestamp = null, $timezone = null ) {
     218
     219        global $wp_locale;
    193220
    194         if ( $timestamp_mode ) {
    195                 $new_format = $i;
     221        if ( null === $timestamp ) {
     222                $timestamp = time();
    196223        }
    197224
    198         if ( ! $timestamp_mode && ! empty( $wp_locale->month ) && ! empty( $wp_locale->weekday ) ) {
    199                 $month   = $wp_locale->get_month( $datetime->format( 'm' ) );
    200                 $weekday = $wp_locale->get_weekday( $datetime->format( 'w' ) );
     225        if ( null === $timezone ) {
     226                $timezone = wp_timezone();
     227        }
     228
     229        $datetime = date_create( '@' . $timestamp );
     230        $datetime->setTimezone( $timezone );
     231
     232        if ( empty( $wp_locale->month ) || empty( $wp_locale->weekday ) ) {
     233                $date = $datetime->format( $format );
     234        } else {
     235                // We need to unpack shorthand `r` format because it has parts that might be localized.
     236                $format = preg_replace( '/(?<!\\\\)r/', DATE_RFC2822, $format );
     237
     238                $new_format    = '';
     239                $format_length = strlen( $format );
     240                $month         = $wp_locale->get_month( $datetime->format( 'm' ) );
     241                $weekday       = $wp_locale->get_weekday( $datetime->format( 'w' ) );
    201242
    202                 $format_length = strlen( $dateformatstring );
    203 
    204243                for ( $i = 0; $i < $format_length; $i ++ ) {
    205                         switch ( $dateformatstring[ $i ] ) {
     244                        switch ( $format[ $i ] ) {
    206245                                case 'D':
    207246                                        $new_format .= backslashit( $wp_locale->get_weekday_abbrev( $weekday ) );
    208247                                        break;
     
    222261                                        $new_format .= backslashit( $wp_locale->get_meridiem( $datetime->format( 'A' ) ) );
    223262                                        break;
    224263                                case '\\':
    225                                         $new_format .= $dateformatstring[ $i ];
     264                                        $new_format .= $format[ $i ];
    226265
    227266                                        // If character follows a slash, we add it without translating.
    228267                                        if ( $i < $format_length ) {
    229                                                 $new_format .= $dateformatstring[ ++$i ];
     268                                                $new_format .= $format[ ++ $i ];
    230269                                        }
    231270                                        break;
    232271                                default:
    233                                         $new_format .= $dateformatstring[ $i ];
     272                                        $new_format .= $format[ $i ];
    234273                                        break;
    235274                        }
    236275                }
    237         }
    238276
    239         $j = $datetime->format( $new_format );
     277                $date = $datetime->format( $new_format );
     278                $date = wp_maybe_decline_date( $date );
     279        }
    240280
    241281        /**
    242282         * Filters the date formatted based on the locale.
    243283         *
    244          * @since 2.8.0
     284         * @param string       $date      Formatted date string.
     285         * @param string       $format    Format to display the date.
     286         * @param int          $timestamp Unix timestamp.
     287         * @param DateTimeZone $timezone  Time zone.
    245288         *
    246          * @param string $j          Formatted date string.
    247          * @param string $req_format Format to display the date.
    248          * @param int    $i          A sum of Unix timestamp and timezone offset in seconds.
    249          * @param bool   $gmt        Whether to use GMT timezone. Only applies if timestamp was
    250          *                           not provided. Default false.
    251289         */
    252         $j = apply_filters( 'date_i18n', $j, $req_format, $i, $gmt );
     290        $date = apply_filters( 'wp_date', $date, $format, $timestamp, $timezone );
    253291
    254         return $j;
     292        return $date;
    255293}
    256294
    257295/**