WordPress.org

Make WordPress Core

Changeset 46975


Ignore:
Timestamp:
12/17/2019 08:45:18 PM (8 months ago)
Author:
SergeyBiryukov
Message:

Date/Time: In wp_insert_post(), when checking the post date to set future or publish status, use a proper delta comparison.

[3525] allowed a difference up to 59 seconds between the post date/time and the current time to consider the post published instead of scheduled, but that didn't take start of a new minute into account.

Rapidly creating post fixtures in unit tests could encounter a one-second discrepancy between current_time( 'mysql' ) and gmdate( 'Y-m-d H:i:s' ), returning values like 2019-12-16 23:43:00 vs. 2019-12-16 23:42:59, respectively, and setting the post to a future status instead of publish.

[45851], while working as intended, made the issue somewhat more likely to occur.

This caused all sorts of occasional random failures in various tests on Travis, mostly on PHP 7.1.

Merges [46968] and [46969] to the 5.3 branch.
Fixes #48145.

Location:
branches/5.3
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/5.3

  • branches/5.3/src/wp-includes/post.php

    r46553 r46975  
    37483748
    37493749    if ( 'attachment' !== $post_type ) {
     3750        $now = gmdate( 'Y-m-d H:i:s' );
     3751
    37503752        if ( 'publish' === $post_status ) {
    3751             // String comparison to work around far future dates (year 2038+) on 32-bit systems.
    3752             if ( $post_date_gmt > gmdate( 'Y-m-d H:i:59' ) ) {
     3753            if ( strtotime( $post_date_gmt ) - strtotime( $now ) >= MINUTE_IN_SECONDS ) {
    37533754                $post_status = 'future';
    37543755            }
    37553756        } elseif ( 'future' === $post_status ) {
    3756             if ( $post_date_gmt <= gmdate( 'Y-m-d H:i:59' ) ) {
     3757            if ( strtotime( $post_date_gmt ) - strtotime( $now ) < MINUTE_IN_SECONDS ) {
    37573758                $post_status = 'publish';
    37583759            }
  • branches/5.3/tests/phpunit/tests/post.php

    r46428 r46975  
    636636
    637637    /**
     638     * @ticket 48145
     639     */
     640    function test_wp_insert_post_should_default_to_publish_if_post_date_is_within_59_seconds_from_current_time() {
     641        $future_date = gmdate( 'Y-m-d H:i:s', time() + 59 );
     642        $post_id     = self::factory()->post->create(
     643            array(
     644                'post_date' => $future_date,
     645            )
     646        );
     647
     648        $post = get_post( $post_id );
     649        $this->assertEquals( 'publish', $post->post_status );
     650        $this->assertEquals( $future_date, $post->post_date );
     651    }
     652
     653    /**
    638654     * @ticket 22944
    639655     */
Note: See TracChangeset for help on using the changeset viewer.