Skip to content

Commit

Permalink
Date/Time: XML-RPC: Сalculate the proper offset for GMT in `wp.newPos…
Browse files Browse the repository at this point in the history
…t`, `wp.editComment`, `mw.newPost`, `mw.editPost` when `post_date` or `comment_date` is set.

Previously, `post_date` or `comment_date` was assumed to be GMT, which is only true if the timezone string for the site matches GMT.

Add unit tests.

Props Rarst, smerriman, justdaiv, wonderboymusic, noyle.
Merges [46864] to the 5.3 branch.
Fixes #30429.

git-svn-id: https://develop.svn.wordpress.org/branches/5.3@46865 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Dec 9, 2019
1 parent a626f2e commit 398e10d
Show file tree
Hide file tree
Showing 2 changed files with 248 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/wp-includes/class-wp-xmlrpc-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -1469,8 +1469,8 @@ protected function _insert_post( $user, $content_struct ) {
$post_data['edit_date'] = false;

if ( ! empty( $dateCreated ) ) {
$post_data['post_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
$post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' );
$post_data['post_date'] = iso8601_to_datetime( $dateCreated );
$post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'gmt' );

// Flag the post date to be edited.
$post_data['edit_date'] = true;
Expand Down Expand Up @@ -3762,8 +3762,8 @@ public function wp_editComment( $args ) {
if ( ! empty( $content_struct['date_created_gmt'] ) ) {
// We know this is supposed to be GMT, so we're going to slap that Z on there by force
$dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
$comment['comment_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
$comment['comment_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' );
$comment['comment_date'] = get_date_from_gmt( $dateCreated );
$comment['comment_date_gmt'] = iso8601_to_datetime( $dateCreated, 'gmt' );
}

if ( isset( $content_struct['content'] ) ) {
Expand Down Expand Up @@ -5481,8 +5481,8 @@ public function mw_newPost( $args ) {
}

if ( ! empty( $dateCreated ) ) {
$post_date = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
$post_date_gmt = iso8601_to_datetime( $dateCreated, 'GMT' );
$post_date = iso8601_to_datetime( $dateCreated );
$post_date_gmt = iso8601_to_datetime( $dateCreated, 'gmt' );
} else {
$post_date = '';
$post_date_gmt = '';
Expand Down Expand Up @@ -5870,8 +5870,8 @@ public function mw_editPost( $args ) {
$edit_date = false;

if ( ! empty( $dateCreated ) ) {
$post_date = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
$post_date_gmt = iso8601_to_datetime( $dateCreated, 'GMT' );
$post_date = iso8601_to_datetime( $dateCreated );
$post_date_gmt = iso8601_to_datetime( $dateCreated, 'gmt' );

// Flag the post date to be edited.
$edit_date = true;
Expand Down
240 changes: 240 additions & 0 deletions tests/phpunit/tests/date/xmlrpc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
<?php

/**
* @group date
* @group datetime
* @group xmlrpc
*/
class Tests_Date_XMLRPC extends WP_XMLRPC_UnitTestCase {

/**
* @ticket 30429
*/
public function test_date_new_post() {
$timezone = 'Europe/Kiev';
update_option( 'timezone_string', $timezone );

$datetime = new DateTimeImmutable( 'now', new DateTimeZone( $timezone ) );
$datetimeutc = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );

$this->make_user_by_role( 'editor' );

$post = get_post(
$this->myxmlrpcserver->mw_newPost(
array(
1,
'editor',
'editor',
array(
'title' => 'test',
'post_content' => 'test',
'dateCreated' => new IXR_Date( $datetimeutc->format( 'Ymd\TH:i:s\Z' ) ),
),
)
)
);

$this->assertEquals(
$datetime->format( 'Y-m-d H:i:s' ),
$post->post_date,
'UTC time with explicit time zone into mw_newPost'
);

$post = get_post(
$this->myxmlrpcserver->mw_newPost(
array(
1,
'editor',
'editor',
array(
'title' => 'test',
'post_content' => 'test',
'dateCreated' => new IXR_Date( $datetime->format( 'Ymd\TH:i:s' ) ),
),
)
)
);

$this->assertEquals(
$datetime->format( 'Y-m-d H:i:s' ),
$post->post_date,
'Local time w/o time zone into mw_newPost'
);

$post = get_post(
$this->myxmlrpcserver->mw_newPost(
array(
1,
'editor',
'editor',
array(
'title' => 'test',
'post_content' => 'test',
'date_created_gmt' => new IXR_Date( $datetimeutc->format( 'Ymd\TH:i:s' ) ),
),
)
)
);

$this->assertEquals(
$datetime->format( 'Y-m-d H:i:s' ),
$post->post_date,
'UTC time into mw_newPost'
);

$post = get_post(
$this->myxmlrpcserver->wp_newPost(
array(
1,
'editor',
'editor',
array(
'title' => 'test',
'post_content' => 'test',
'post_date' => $datetime->format( 'Ymd\TH:i:s' ),
),
)
)
);

$this->assertEquals(
$datetime->format( 'Y-m-d H:i:s' ),
$post->post_date,
'Local time into wp_newPost'
);

$post = get_post(
$this->myxmlrpcserver->wp_newPost(
array(
1,
'editor',
'editor',
array(
'title' => 'test',
'post_content' => 'test',
'post_date_gmt' => $datetimeutc->format( 'Ymd\TH:i:s' ),
),
)
)
);

$this->assertEquals(
$datetime->format( 'Y-m-d H:i:s' ),
$post->post_date,
'UTC time into wp_newPost'
);
}

/**
* @ticket 30429
*/
public function test_date_edit_post() {
$timezone = 'Europe/Kiev';
update_option( 'timezone_string', $timezone );

$datetime = new DateTimeImmutable( 'now', new DateTimeZone( $timezone ) );
$datetimeutc = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );

$editor_id = $this->make_user_by_role( 'editor' );

$post_id = self::factory()->post->create(
array(
'post_author' => $editor_id,
'post_date' => $datetime->modify( '-1 hour' )->format( 'Y-m-d H:i:s' ),
)
);

$result = $this->myxmlrpcserver->mw_editPost(
array(
$post_id,
'editor',
'editor',
array(
'dateCreated' => new IXR_Date( $datetime->format( 'Ymd\TH:i:s' ) ),
),
)
);

$fetched_post = get_post( $post_id );

$this->assertTrue( $result );
$this->assertEquals(
$datetime->format( 'Y-m-d H:i:s' ),
$fetched_post->post_date,
'Local time into mw_editPost'
);

$post_id = self::factory()->post->create(
array(
'post_author' => $editor_id,
'post_date' => $datetime->modify( '-1 hour' )->format( 'Y-m-d H:i:s' ),
)
);

$result = $this->myxmlrpcserver->mw_editPost(
array(
$post_id,
'editor',
'editor',
array(
'date_created_gmt' => new IXR_Date( $datetimeutc->format( 'Ymd\TH:i:s' ) ),
),
)
);

$fetched_post = get_post( $post_id );

$this->assertTrue( $result );
$this->assertEquals(
$datetime->format( 'Y-m-d H:i:s' ),
$fetched_post->post_date,
'UTC time into mw_editPost'
);
}

/**
* @ticket 30429
*/
function test_date_edit_comment() {
$timezone = 'Europe/Kiev';
update_option( 'timezone_string', $timezone );

$datetime = new DateTimeImmutable( 'now', new DateTimeZone( $timezone ) );
$datetime = $datetime->modify( '-1 hour' );
$datetimeutc = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );

$this->make_user_by_role( 'administrator' );
$post_id = $this->factory->post->create();

$comment_data = array(
'comment_post_ID' => $post_id,
'comment_author' => 'Test commenter',
'comment_author_url' => 'http://example.com/',
'comment_author_email' => 'example@example.com',
'comment_content' => rand_str( 100 ),
'comment_approved' => '1',
);
$comment_id = wp_insert_comment( $comment_data );

$result = $this->myxmlrpcserver->wp_editComment(
array(
1,
'administrator',
'administrator',
$comment_id,
array(
'date_created_gmt' => new IXR_Date( $datetimeutc->format( 'Ymd\TH:i:s' ) ),
),
)
);

$fetched_comment = get_comment( $comment_id );

$this->assertTrue( $result );
$this->assertEquals(
$datetime->format( 'Y-m-d H:i:s' ),
$fetched_comment->comment_date,
'UTC time into wp_editComment'
);
}
}

0 comments on commit 398e10d

Please sign in to comment.