diff --git a/src/wp-includes/class-wp-xmlrpc-server.php b/src/wp-includes/class-wp-xmlrpc-server.php index 260c6f86dc..6dca9e1132 100644 --- a/src/wp-includes/class-wp-xmlrpc-server.php +++ b/src/wp-includes/class-wp-xmlrpc-server.php @@ -1708,6 +1708,16 @@ class wp_xmlrpc_server extends IXR_Server { $post['post_date_gmt'] = $this->_convert_date( $post['post_date_gmt'] ); } + /* + * If the API client did not provide post_date, then we must not perpetuate the value that was + * stored in the database, or it will appear to be an intentional edit. Conveying it here as if + * it was coming from the API client will cause an otherwise zeroed out post_date_gmt to get set + * with the value that was originally stored in the database when the draft was created. + */ + if ( ! isset( $content_struct['post_date'] ) ) { + unset( $post['post_date'] ); + } + $this->escape( $post ); $merged_content_struct = array_merge( $post, $content_struct ); diff --git a/tests/phpunit/tests/xmlrpc/wp/editPost.php b/tests/phpunit/tests/xmlrpc/wp/editPost.php index 920dd4c51b..7d38f532cd 100644 --- a/tests/phpunit/tests/xmlrpc/wp/editPost.php +++ b/tests/phpunit/tests/xmlrpc/wp/editPost.php @@ -496,4 +496,33 @@ class Tests_XMLRPC_wp_editPost extends WP_XMLRPC_UnitTestCase { $future_date_string = strftime( '%Y-%m-%d %H:%M:%S', $future_time ); $this->assertEquals( $future_date_string, $after->post_date ); } + + /** + * @ticket 45322 + */ + function test_draft_not_assigned_published_date() { + $editor_id = $this->make_user_by_role( 'editor' ); + + // Start with a draft post, confirming its post_date_gmt is "zero". + $post = array( + 'post_title' => 'Test', + 'post_status' => 'draft', + ); + $post_id = $this->myxmlrpcserver->wp_newPost( array( 1, 'editor', 'editor', $post ) ); + + $before = get_post( $post_id ); + $this->assertEquals( '0000-00-00 00:00:00', $before->post_date_gmt ); + + // Edit the post without specifying any dates. + $new_post_content = array( + 'ID' => $post_id, + 'post_title' => 'Updated', + ); + + $this->myxmlrpcserver->wp_editPost( array( 1, 'editor', 'editor', $post_id, $new_post_content ) ); + + // The published date should still be zero. + $after = get_post( $post_id ); + $this->assertEquals( '0000-00-00 00:00:00', $after->post_date_gmt ); + } }