Make WordPress Core

Ticket #25002: get-post-datetime.patch

File get-post-datetime.patch, 6.4 KB (added by Rarst, 6 years ago)
  • tests/phpunit/tests/date/postTime.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_Post_Time extends WP_UnitTestCase {
     8
     9        public function test_should_return_wp_timestamp() {
     10
     11                $timezone = 'Europe/Kiev';
     12                update_option( 'timezone_string', $timezone );
     13                $datetime     = new DateTimeImmutable( 'now', new DateTimeZone( $timezone ) );
     14                $mysql        = $datetime->format( 'Y-m-d H:i:s' );
     15                $timestamp    = $datetime->getTimestamp();
     16                $wp_timestamp = $datetime->getTimestamp() + $datetime->getOffset();
     17
     18                $post_id = self::factory()->post->create( array(
     19                        'post_date'     => $mysql,
     20                        'post_modified' => $mysql,
     21                ) );
     22
     23                $this->assertEquals( $wp_timestamp, get_post_time( 'U', false, $post_id ) );
     24                $this->assertEquals( $wp_timestamp, get_post_time( 'G', false, $post_id ) );
     25                $this->assertEquals( $timestamp, get_post_time( 'U', true, $post_id ) );
     26                $this->assertEquals( $timestamp, get_post_time( 'G', true, $post_id ) );
     27                $this->assertEquals( $wp_timestamp, get_post_modified_time( 'U', false, $post_id ) );
     28                $this->assertEquals( $wp_timestamp, get_post_modified_time( 'G', false, $post_id ) );
     29                $this->assertEquals( $timestamp, get_post_modified_time( 'U', true, $post_id ) );
     30                $this->assertEquals( $timestamp, get_post_modified_time( 'G', true, $post_id ) );
     31        }
     32
     33        public function test_should_return_time() {
     34
     35                $timezone = 'Europe/Kiev';
     36                update_option( 'timezone_string', $timezone );
     37                $datetime    = new DateTimeImmutable( 'now', new DateTimeZone( $timezone ) );
     38                $mysql       = $datetime->format( 'Y-m-d H:i:s' );
     39                $rfc3339     = $datetime->format( DATE_RFC3339 );
     40                $rfc3339_utc = $datetime->setTimezone( new DateTimeZone( 'UTC' ) )->format( DATE_RFC3339 );
     41                $post_id     = self::factory()->post->create( array(
     42                        'post_date'     => $mysql,
     43                        'post_modified' => $mysql,
     44                ) );
     45
     46                $this->assertEquals( $rfc3339, get_post_time( DATE_RFC3339, false, $post_id ) );
     47                $this->assertEquals( $rfc3339_utc, get_post_time( DATE_RFC3339, true, $post_id ) );
     48                $this->assertEquals( $rfc3339, get_post_time( DATE_RFC3339, false, $post_id, true ) );
     49                $this->assertEquals( $rfc3339_utc, get_post_time( DATE_RFC3339, true, $post_id, true ) );
     50                $this->assertEquals( $rfc3339, get_post_modified_time( DATE_RFC3339, false, $post_id ) );
     51                $this->assertEquals( $rfc3339_utc, get_post_modified_time( DATE_RFC3339, true, $post_id ) );
     52                $this->assertEquals( $rfc3339, get_post_modified_time( DATE_RFC3339, false, $post_id, true ) );
     53                $this->assertEquals( $rfc3339_utc, get_post_modified_time( DATE_RFC3339, true, $post_id, true ) );
     54        }
     55}
  • src/wp-includes/general-template.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    25622562                return false;
    25632563        }
    25642564
    2565         if ( $gmt ) {
    2566                 $time = $post->post_date_gmt;
    2567         } else {
    2568                 $time = $post->post_date;
     2565        $datetime = get_post_datetime( $post );
     2566
     2567        if ( false === $datetime ) {
     2568                return false;
     2569        }
     2570
     2571        if ( 'U' === $d || 'G' === $d ) {
     2572
     2573                $time = $datetime->getTimestamp();
     2574
     2575                // Returns a sum of timestamp with timezone offset. Ideally should never be used.
     2576                if ( ! $gmt ) {
     2577                        $time += $datetime->getOffset();
     2578                }
     2579        } elseif ( $translate ) {
     2580                $time = wp_date( $d, $datetime->getTimestamp(), $gmt ? new DateTimeZone( 'UTC' ) : null );
     2581        } else {
     2582                if ( $gmt ) {
     2583                        $datetime = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
     2584                }
     2585                $time = $datetime->format( $d );
    25692586        }
    2570 
    2571         $time = mysql2date( $d, $time, $translate );
    25722587
    25732588        /**
    25742589         * Filters the localized time a post was written.
     
    25832598        return apply_filters( 'get_post_time', $time, $d, $gmt );
    25842599}
    25852600
     2601/**
     2602 * Retrieve post published or modified time as `DateTimeImmutable` object instance.
     2603 *
     2604 * Object will be set to timezone from WordPress settings.
     2605 *
     2606 * @param WP_Post|array|int $post  Optional. WordPress post instance or ID. Defaults to the global `$post`.
     2607 * @param string            $field Optional. Post field to use. Can be `date` or `modified`.
     2608 *
     2609 * @return DateTimeImmutable|false Time object. False on failure.
     2610 */
     2611function get_post_datetime( $post = null, $field = 'date' ) {
     2612
     2613        $post = get_post( $post );
     2614
     2615        if ( ! $post ) {
     2616                return false;
     2617        }
     2618
     2619        $time = ( 'modified' === $field ) ? $post->post_modified : $post->post_date;
     2620
     2621        if ( empty( $time ) || '0000-00-00 00:00:00' === $time ) {
     2622                return false;
     2623        }
     2624
     2625        return date_create_immutable_from_format( 'Y-m-d H:i:s', $time, wp_timezone() );
     2626}
     2627
     2628/**
     2629 * Retrieve post published or modified time as Unix timestamp.
     2630 *
     2631 * Note that this function returns true Unix timestamp, not summed with time zone offset like older WP functions.
     2632 *
     2633 * @param WP_Post|array|int $post  Optional. WordPress post instance or ID. Defaults to the global `$post`.
     2634 * @param string            $field Optional. Post field to use. Can be `date` or `modified`.
     2635 *
     2636 * @return int|false Unix timestamp. False on failure.
     2637 */
     2638function get_post_timestamp( $post = null, $field = 'date' ) {
     2639
     2640        $datetime = get_post_datetime( $post, $field );
     2641
     2642        if ( false === $datetime ) {
     2643                return false;
     2644        }
     2645
     2646        return $datetime->getTimestamp();
     2647}
     2648
    25862649/**
    25872650 * Display the time at which the post was last modified.
    25882651 *
     
    26622725                return false;
    26632726        }
    26642727
    2665         if ( $gmt ) {
    2666                 $time = $post->post_modified_gmt;
     2728        $datetime = get_post_datetime( $post, 'modified' );
     2729
     2730        if ( false === $datetime ) {
     2731                return false;
     2732        }
     2733
     2734        if ( 'U' === $d || 'G' === $d ) {
     2735
     2736                $time = $datetime->getTimestamp();
     2737
     2738                // Returns a sum of timestamp with timezone offset. Ideally should never be used.
     2739                if ( ! $gmt ) {
     2740                        $time += $datetime->getOffset();
     2741                }
     2742        } elseif ( $translate ) {
     2743                $time = wp_date( $d, $datetime->getTimestamp(), $gmt ? new DateTimeZone( 'UTC' ) : null );
    26672744        } else {
    2668                 $time = $post->post_modified;
     2745                if ( $gmt ) {
     2746                        $datetime = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
     2747                }
     2748                $time = $datetime->format( $d );
    26692749        }
    2670 
    2671         $time = mysql2date( $d, $time, $translate );
    26722750
    26732751        /**
    26742752         * Filters the localized time a post was last modified.