Make WordPress Core


Ignore:
Timestamp:
02/24/2017 09:58:07 PM (8 years ago)
Author:
SergeyBiryukov
Message:

REST API: Fix multiple issues with setting dates of posts and comments.

This commit modifies the rest_get_date_with_gmt function to correctly parse local and UTC timestamps with or without timezone information.

It also ensures that the REST API can edit the dates of draft posts by setting the edit_date flag to wp_update_post.

Overall this commit ensures that post and comment dates can be set and updated as expected.

Props jnylen0.
Merges [40101] to the 4.7 branch.
Fixes #39256.

Location:
branches/4.7
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.7

  • branches/4.7/src/wp-includes/rest-api.php

    r40079 r40114  
    781781
    782782/**
    783  * Retrieves a local date with its GMT equivalent, in MySQL datetime format.
     783 * Parses a date into both its local and UTC equivalent, in MySQL datetime format.
    784784 *
    785785 * @since 4.4.0
     
    787787 * @see rest_parse_date()
    788788 *
    789  * @param string $date      RFC3339 timestamp.
    790  * @param bool   $force_utc Whether a UTC timestamp should be forced. Default false.
     789 * @param string $date   RFC3339 timestamp.
     790 * @param bool   $is_utc Whether the provided date should be interpreted as UTC. Default false.
    791791 * @return array|null Local and UTC datetime strings, in MySQL datetime format (Y-m-d H:i:s),
    792792 *                    null on failure.
    793793 */
    794 function rest_get_date_with_gmt( $date, $force_utc = false ) {
    795     $date = rest_parse_date( $date, $force_utc );
     794function rest_get_date_with_gmt( $date, $is_utc = false ) {
     795    // Whether or not the original date actually has a timezone string
     796    // changes the way we need to do timezone conversion.  Store this info
     797    // before parsing the date, and use it later.
     798    $has_timezone = preg_match( '#(Z|[+-]\d{2}(:\d{2})?)$#', $date );
     799
     800    $date = rest_parse_date( $date );
    796801
    797802    if ( empty( $date ) ) {
     
    799804    }
    800805
    801     $utc = date( 'Y-m-d H:i:s', $date );
    802     $local = get_date_from_gmt( $utc );
     806    // At this point $date could either be a local date (if we were passed a
     807    // *local* date without a timezone offset) or a UTC date (otherwise).
     808    // Timezone conversion needs to be handled differently between these two
     809    // cases.
     810    if ( ! $is_utc && ! $has_timezone ) {
     811        $local = date( 'Y-m-d H:i:s', $date );
     812        $utc = get_gmt_from_date( $local );
     813    } else {
     814        $utc = date( 'Y-m-d H:i:s', $date );
     815        $local = get_date_from_gmt( $utc );
     816    }
    803817
    804818    return array( $local, $utc );
Note: See TracChangeset for help on using the changeset viewer.