From 948ab018d2041f19dc4a9ed7d877d617a54484a7 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Tue, 29 Sep 2015 04:04:16 +0000 Subject: [PATCH] XML-RPC: calculate the proper offset for GMT in `wp.newPost`, `mw.newPost`, and `mw.editPost` when `post_date` is set, `wp.editComment` when `comment_date` is set. `post|comment_date` is assumed to be GMT. This is only true if the timezone string for the site matches GMT. Adds unit tests for each. Props smerriman, justdaiv, wonderboymusic. Fixes #30429. git-svn-id: https://develop.svn.wordpress.org/trunk@34681 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-xmlrpc-server.php | 16 +++++----- tests/phpunit/tests/xmlrpc/mw/editPost.php | 26 +++++++++++++++ tests/phpunit/tests/xmlrpc/mw/newPost.php | 23 +++++++++++++ tests/phpunit/tests/xmlrpc/wp/editComment.php | 32 +++++++++++++++++++ tests/phpunit/tests/xmlrpc/wp/newPost.php | 18 +++++++++++ 5 files changed, 107 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/class-wp-xmlrpc-server.php b/src/wp-includes/class-wp-xmlrpc-server.php index 8661a44b4c..6eadaf8bbb 100644 --- a/src/wp-includes/class-wp-xmlrpc-server.php +++ b/src/wp-includes/class-wp-xmlrpc-server.php @@ -1324,8 +1324,8 @@ class wp_xmlrpc_server extends IXR_Server { } 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'] = get_gmt_from_date( $post_data['post_date'] ); } if ( ! isset( $post_data['ID'] ) ) @@ -3395,8 +3395,8 @@ class wp_xmlrpc_server extends IXR_Server { 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_date = get_date_from_gmt(iso8601_to_datetime($dateCreated)); - $comment_date_gmt = iso8601_to_datetime($dateCreated, 'GMT'); + $comment_date = iso8601_to_datetime( $dateCreated ); + $comment_date_gmt = get_gmt_from_date( $comment_date ); } if ( isset($content_struct['content']) ) @@ -4960,8 +4960,8 @@ class wp_xmlrpc_server extends IXR_Server { $dateCreated = $content_struct['dateCreated']->getIso(); 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 = get_gmt_from_date( $post_date ); } else { $post_date = ''; $post_date_gmt = ''; @@ -5314,8 +5314,8 @@ class wp_xmlrpc_server extends IXR_Server { $dateCreated = $content_struct['dateCreated']->getIso(); 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 = get_gmt_from_date( $post_date, 'GMT' ); } else { $post_date = $postdata['post_date']; $post_date_gmt = $postdata['post_date_gmt']; diff --git a/tests/phpunit/tests/xmlrpc/mw/editPost.php b/tests/phpunit/tests/xmlrpc/mw/editPost.php index 50f97929e3..a147990d58 100644 --- a/tests/phpunit/tests/xmlrpc/mw/editPost.php +++ b/tests/phpunit/tests/xmlrpc/mw/editPost.php @@ -229,4 +229,30 @@ class Tests_XMLRPC_mw_editPost extends WP_XMLRPC_UnitTestCase { $this->assertInstanceOf( 'IXR_Error', $result ); $this->assertEquals( $result->code, 401 ); } + + /** + * @ticket 30429 + */ + function test_post_date_timezone_conversion() { + $tz = get_option( 'timezone_string' ); + update_option( 'timezone_string', 'America/New_York' ); + + $editor_id = $this->make_user_by_role( 'editor' ); + + $post_id = $this->factory->post->create( array( + 'post_author' => $editor_id + ) ); + + $date_string = '1984-01-11 05:00:00'; + $result = $this->myxmlrpcserver->mw_editPost( array( $post_id, 'editor', 'editor', array( + 'dateCreated' => new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date_string, false ) ), + ) ) ); + + $fetched_post = get_post( $post_id ); + + update_option( 'timezone_string', $tz ); + + $this->assertTrue( $result ); + $this->assertEquals( $date_string, $fetched_post->post_date ); + } } diff --git a/tests/phpunit/tests/xmlrpc/mw/newPost.php b/tests/phpunit/tests/xmlrpc/mw/newPost.php index 34b34d64e8..41b591bc9b 100644 --- a/tests/phpunit/tests/xmlrpc/mw/newPost.php +++ b/tests/phpunit/tests/xmlrpc/mw/newPost.php @@ -180,4 +180,27 @@ class Tests_XMLRPC_mw_newPost extends WP_XMLRPC_UnitTestCase { $this->assertEquals( 'draft', $out->post_status ); $this->assertEquals( '0000-00-00 00:00:00', $out->post_date_gmt ); } + + /** + * @ticket 30429 + */ + function test_post_date_timezone_conversion() { + $tz = get_option( 'timezone_string' ); + update_option( 'timezone_string', 'America/New_York' ); + + $this->make_user_by_role( 'editor' ); + $date_string = '1984-01-11 05:00:00'; + $post = array( + 'title' => 'test', + 'post_content' => 'test', + 'dateCreated' => new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date_string, false ) ) + ); + $result = $this->myxmlrpcserver->mw_newPost( array( 1, 'editor', 'editor', $post ) ); + $fetched_post = get_post( $result ); + + update_option( 'timezone_string', $tz ); + + $this->assertStringMatchesFormat( '%d', $result ); + $this->assertEquals( $date_string , $fetched_post->post_date ); + } } diff --git a/tests/phpunit/tests/xmlrpc/wp/editComment.php b/tests/phpunit/tests/xmlrpc/wp/editComment.php index 868e369124..aeb03e1263 100644 --- a/tests/phpunit/tests/xmlrpc/wp/editComment.php +++ b/tests/phpunit/tests/xmlrpc/wp/editComment.php @@ -69,4 +69,36 @@ class Tests_XMLRPC_wp_editComment extends WP_XMLRPC_UnitTestCase { $this->assertEquals( 'trash', get_comment( $comment_id )->comment_approved ); } + + /** + * @ticket 30429 + */ + function test_post_date_timezone_conversion() { + $tz = get_option( 'timezone_string' ); + update_option( 'timezone_string', 'America/New_York' ); + + $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 ); + + $date_string = '1984-01-11 05:00:00'; + $result = $this->myxmlrpcserver->wp_editComment( array( 1, 'administrator', 'administrator', $comment_id, array( + 'date_created_gmt' => new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date_string, false ) ) + ) ) ); + $fetched_comment = get_comment( $comment_id ); + + update_option( 'timezone_string', $tz ); + + $this->assertTrue( $result ); + $this->assertEquals( $date_string, $fetched_comment->comment_date ); + } } \ No newline at end of file diff --git a/tests/phpunit/tests/xmlrpc/wp/newPost.php b/tests/phpunit/tests/xmlrpc/wp/newPost.php index bcab61fa9c..9cc2d2877a 100644 --- a/tests/phpunit/tests/xmlrpc/wp/newPost.php +++ b/tests/phpunit/tests/xmlrpc/wp/newPost.php @@ -383,4 +383,22 @@ class Tests_XMLRPC_wp_newPost extends WP_XMLRPC_UnitTestCase { $this->assertEquals( $date_string , $fetched_post->post_date_gmt ); } + /** + * @ticket 30429 + */ + function test_post_date_timezone_conversion() { + $tz = get_option( 'timezone_string' ); + update_option( 'timezone_string', 'America/New_York' ); + + $this->make_user_by_role( 'author' ); + $date_string = '1984-01-11 05:00:00'; + $post = array( 'post_title' => 'test', 'post_content' => 'test', 'post_date' => $date_string ); + $result = $this->myxmlrpcserver->wp_newPost( array( 1, 'author', 'author', $post ) ); + $fetched_post = get_post( $result ); + + update_option( 'timezone_string', $tz ); + + $this->assertStringMatchesFormat( '%d', $result ); + $this->assertEquals( $date_string , $fetched_post->post_date ); + } }