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.

Fixes #39256.


git-svn-id: https://develop.svn.wordpress.org/trunk@40101 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
James Nylen
2017-02-21 18:17:32 +00:00
parent c31acbfe7e
commit 83d27ba447
5 changed files with 335 additions and 7 deletions

View File

@@ -780,26 +780,40 @@ function rest_parse_date( $date, $force_utc = false ) {
}
/**
* Retrieves a local date with its GMT equivalent, in MySQL datetime format.
* Parses a date into both its local and UTC equivalent, in MySQL datetime format.
*
* @since 4.4.0
*
* @see rest_parse_date()
*
* @param string $date RFC3339 timestamp.
* @param bool $force_utc Whether a UTC timestamp should be forced. Default false.
* @param string $date RFC3339 timestamp.
* @param bool $is_utc Whether the provided date should be interpreted as UTC. Default false.
* @return array|null Local and UTC datetime strings, in MySQL datetime format (Y-m-d H:i:s),
* null on failure.
*/
function rest_get_date_with_gmt( $date, $force_utc = false ) {
$date = rest_parse_date( $date, $force_utc );
function rest_get_date_with_gmt( $date, $is_utc = false ) {
// Whether or not the original date actually has a timezone string
// changes the way we need to do timezone conversion. Store this info
// before parsing the date, and use it later.
$has_timezone = preg_match( '#(Z|[+-]\d{2}(:\d{2})?)$#', $date );
$date = rest_parse_date( $date );
if ( empty( $date ) ) {
return null;
}
$utc = date( 'Y-m-d H:i:s', $date );
$local = get_date_from_gmt( $utc );
// At this point $date could either be a local date (if we were passed a
// *local* date without a timezone offset) or a UTC date (otherwise).
// Timezone conversion needs to be handled differently between these two
// cases.
if ( ! $is_utc && ! $has_timezone ) {
$local = date( 'Y-m-d H:i:s', $date );
$utc = get_gmt_from_date( $local );
} else {
$utc = date( 'Y-m-d H:i:s', $date );
$local = get_date_from_gmt( $utc );
}
return array( $local, $utc );
}