WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 2 years ago

#30429 reopened defect (bug)

wp.newPost gets non-GMT date calculation wrong

Reported by: smerriman Owned by: wonderboymusic
Milestone: Future Release Priority: normal
Severity: critical Version: 3.4
Component: XML-RPC Keywords: needs-unit-tests needs-patch
Focuses: Cc:

Description

In class-wp-xmlrpc-server.php, there is a bug with the calculation of dates if a post_date is set.

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' );
}

get_date_from_gmt assumes the parameter passed is a GMT string. iso8601_to_datetime( $dateCreated ), however, is the local time. It should have the GMT parameter as per the next line to ensure the two dates are always in sync; currently the first one is always incorrect.

Attachments (5)

30429.patch (570 bytes) - added by smerriman 4 years ago.
Renaming patch to ticket number
30429.test.patch (991 bytes) - added by justdaiv 3 years ago.
Unit test
30429.2.patch (570 bytes) - added by smerriman 3 years ago.
30429.3.patch (2.1 KB) - added by smerriman 3 years ago.
Sorry, attached the wrong file.
30429.diff (6.7 KB) - added by wonderboymusic 3 years ago.

Download all attachments as: .zip

Change History (22)

@smerriman
4 years ago

Renaming patch to ticket number

#1 @smerriman
4 years ago

  • Component changed from General to XML-RPC
  • Keywords has-patch added

#2 @johnbillion
3 years ago

  • Keywords needs-testing reporter-feedback needs-unit-tests added
  • Version changed from trunk to 3.4

Thanks for the report. Can you provide some example code or an example XML-RPC request which demonstrates the issue?

The patch will benefit from some unit tests too.

#3 @smerriman
3 years ago

  • Keywords reporter-feedback removed

Sure. As a specific example, set the Wordpress timezone to Pacific/Auckland (currently GMT+13), and run the following code from the root dir (altering domain/user/pass as appropriate):

<?php
include('./wp-load.php');

include_once( ABSPATH . WPINC . '/class-IXR.php' );
include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' );

$client = new WP_HTTP_IXR_CLIENT( 'http://www.domain.com/xmlrpc.php' );

$content = array(
	'post_type'=>'post',
	'post_title'=>'Test post',
	'post_status'=>'publish',
	'post_author'=>1,
	'post_date'=>'2014-12-01 12:00:00'
);

$client->query('wp.newPost','','username','password',$content,true);

The resulting dates in the database will be:

post_date_gmt: 2014-11-30 23:00:00 (correct) post_date: 2014-12-02 01:00:00 (13 hours later than it should be)

Not experienced enough with organising unit tests myself yet sorry!sorry!

#4 @smerriman
3 years ago

  • Severity changed from normal to critical

This bug causes wp.newpost to be completely unusable. Can it be looked at?

@justdaiv
3 years ago

Unit test

#5 @justdaiv
3 years ago

I believe the code patch provided only fixes the case where the local timezone is not in daylight savings time. The unit test I uploaded has the timezone set to America/New_York (which currently is in dst) and the test fails. If I change the unit test to have a timezone of America/Phoenix (where it is never dst), the test passes. Or am I missing something?

#6 @smerriman
3 years ago

It looks like this is due to: #25399

#7 @smerriman
3 years ago

Here's an updated patch which should fix the problem properly (and also updates a few other similar places in the same file). It converts the ISO date to the date variable directly; then uses get_gmt_from_date to calculate the GMT date, rather than the opposite way around. That avoids complications with timezones.

@smerriman
3 years ago

@smerriman
3 years ago

Sorry, attached the wrong file.

#8 @wonderboymusic
3 years ago

  • Keywords needs-testing removed
  • Milestone changed from Awaiting Review to 4.4

@wonderboymusic
3 years ago

#9 @wonderboymusic
3 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 34681:

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.

#10 @chriscct7
2 years ago

#18998 was marked as a duplicate.

This ticket was mentioned in Slack in #core by hinaloe. View the logs.


2 years ago

This ticket was mentioned in Slack in #core by redsweater. View the logs.


2 years ago

#13 @nacin
2 years ago

In 36163:

XML-RPC: Revert [34681] as it broke date handling.

props dossy, hnle, redsweater.
see #35053, #30429 (original ticket).

#14 @nacin
2 years ago

In 36164:

XML-RPC: Revert [34681] as it broke date handling.

Merges [36163] to the 4.4 branch.

props dossy, hnle, redsweater.
see #35053, #30429 (original ticket).

#15 @ocean90
2 years ago

  • Keywords needs-patch added; has-patch removed
  • Milestone changed from 4.4 to 4.5
  • Resolution fixed deleted
  • Status changed from closed to reopened

This ticket was mentioned in Slack in #core by chriscct7. View the logs.


2 years ago

#17 @chriscct7
2 years ago

  • Milestone changed from 4.5 to Future Release

Punting due to lack of activity per slack discussion 6 days ago.

Note: See TracTickets for help on using tickets.