Make WordPress Core

Changeset 50012


Ignore:
Timestamp:
01/25/2021 01:06:25 AM (4 years ago)
Author:
pento
Message:

Posts: Create a new function for resolving the post date.

wp_insert_post() has a few checks using post_date and post_date_gmt, to determine the correct post date. This functionality is now extracted out into a new wp_resolve_post_date() function, allowing the checks to be reused elsewhere.

Props jmdodd.
Fixes #52187.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r49986 r50012  
    37033703        'import_id'             => 0,
    37043704        'context'               => '',
     3705        'post_date'             => '',
     3706        'post_date_gmt'         => '',
    37053707    );
    37063708
     
    38363838
    38373839    /*
    3838      * If the post date is empty (due to having been new or a draft) and status
    3839      * is not 'draft' or 'pending', set date to now.
     3840     * Resolve the post date from any provided post date or post date GMT strings;
     3841     * if none are provided, the date will be set to now.
    38403842     */
    3841     if ( empty( $postarr['post_date'] ) || '0000-00-00 00:00:00' === $postarr['post_date'] ) {
    3842         if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' === $postarr['post_date_gmt'] ) {
    3843             $post_date = current_time( 'mysql' );
    3844         } else {
    3845             $post_date = get_date_from_gmt( $postarr['post_date_gmt'] );
    3846         }
    3847     } else {
    3848         $post_date = $postarr['post_date'];
    3849     }
    3850 
    3851     // Validate the date.
    3852     $mm         = substr( $post_date, 5, 2 );
    3853     $jj         = substr( $post_date, 8, 2 );
    3854     $aa         = substr( $post_date, 0, 4 );
    3855     $valid_date = wp_checkdate( $mm, $jj, $aa, $post_date );
    3856     if ( ! $valid_date ) {
     3843    $post_date = wp_resolve_post_date( $postarr['post_date'], $postarr['post_date_gmt'] );
     3844    if ( ! $post_date ) {
    38573845        if ( $wp_error ) {
    38583846            return new WP_Error( 'invalid_date', __( 'Invalid date.' ) );
     
    45374525    // wp_publish_post() returns no meaningful value.
    45384526    wp_publish_post( $post_id );
     4527}
     4528
     4529/**
     4530 * Uses wp_checkdate to return a valid Gregorian-calendar value for post_date.
     4531 * If post_date is not provided, this first checks post_date_gmt if provided,
     4532 * then falls back to use the current time.
     4533 *
     4534 * For back-compat purposes in wp_insert_post, an empty post_date and an invalid
     4535 * post_date_gmt will continue to return '1970-01-01 00:00:00' rather than false.
     4536 *
     4537 * @since 5.7.0
     4538 *
     4539 * @param string $post_date     The date in mysql format.
     4540 * @param string $post_date_gmt The GMT date in mysql format.
     4541 * @return string|false A valid Gregorian-calendar date string, or false on failure.
     4542 */
     4543function wp_resolve_post_date( $post_date = '', $post_date_gmt = '' ) {
     4544    // If the date is empty, set the date to now.
     4545    if ( empty( $post_date ) || '0000-00-00 00:00:00' === $post_date ) {
     4546        if ( empty( $post_date_gmt ) || '0000-00-00 00:00:00' === $post_date_gmt ) {
     4547            $post_date = current_time( 'mysql' );
     4548        } else {
     4549            $post_date = get_date_from_gmt( $post_date_gmt );
     4550        }
     4551    }
     4552
     4553    // Validate the date.
     4554    $month = substr( $post_date, 5, 2 );
     4555    $day   = substr( $post_date, 8, 2 );
     4556    $year  = substr( $post_date, 0, 4 );
     4557
     4558    $valid_date = wp_checkdate( $month, $day, $year, $post_date );
     4559
     4560    if ( ! $valid_date ) {
     4561        return false;
     4562    }
     4563    return $post_date;
    45394564}
    45404565
  • trunk/tests/phpunit/tests/post.php

    r49974 r50012  
    13961396        $this->assertSameSets( array( $tag_2['term_id'], $tag_3['term_id'] ), $tags );
    13971397    }
     1398
     1399    /**
     1400     * @ticket 52187
     1401     */
     1402    public function test_insert_empty_post_date() {
     1403        $post_date_gmt = '2020-12-29 10:11:45';
     1404        $invalid_date  = '2020-12-41 14:15:27';
     1405
     1406        // Empty post_date_gmt with floating status
     1407        $post_id = self::factory()->post->create(
     1408            array(
     1409                'post_status' => 'draft',
     1410            )
     1411        );
     1412        $post    = get_post( $post_id );
     1413        $this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' );
     1414        $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
     1415
     1416        $post_id = self::factory()->post->create(
     1417            array(
     1418                'post_date_gmt' => '0000-00-00 00:00:00',
     1419                'post_status'   => 'draft',
     1420            )
     1421        );
     1422        $post    = get_post( $post_id );
     1423        $this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' );
     1424        $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
     1425
     1426        // Empty post_date_gmt without floating status
     1427        $post_id = self::factory()->post->create(
     1428            array(
     1429                'post_status' => 'publish',
     1430            )
     1431        );
     1432        $post    = get_post( $post_id );
     1433        $this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' );
     1434        $this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( get_gmt_from_date( $post->post_date ) ), 2, 'The dates should be equal' );
     1435
     1436        $post_id = self::factory()->post->create(
     1437            array(
     1438                'post_date_gmt' => '0000-00-00 00:00:00',
     1439                'post_status'   => 'publish',
     1440            )
     1441        );
     1442        $post    = get_post( $post_id );
     1443        $this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' );
     1444        $this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( get_gmt_from_date( $post->post_date ) ), 2, 'The dates should be equal' );
     1445
     1446        // Valid post_date_gmt
     1447        $post_id = self::factory()->post->create(
     1448            array(
     1449                'post_date_gmt' => $post_date_gmt,
     1450            )
     1451        );
     1452        $post    = get_post( $post_id );
     1453        $this->assertEquals( get_date_from_gmt( $post_date_gmt ), $post->post_date );
     1454        $this->assertEquals( $post_date_gmt, $post->post_date_gmt );
     1455
     1456        // Invalid post_date_gmt
     1457        $post_id = self::factory()->post->create(
     1458            array(
     1459                'post_date_gmt' => $invalid_date,
     1460            )
     1461        );
     1462        $post    = get_post( $post_id );
     1463        $this->assertEquals( '1970-01-01 00:00:00', $post->post_date );
     1464        $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
     1465    }
     1466
     1467    /**
     1468     * @ticket 52187
     1469     */
     1470    public function test_insert_valid_post_date() {
     1471        $post_date     = '2020-12-28 11:26:35';
     1472        $post_date_gmt = '2020-12-29 10:11:45';
     1473        $invalid_date  = '2020-12-41 14:15:27';
     1474
     1475        // Empty post_date_gmt with floating status
     1476        $post_id = self::factory()->post->create(
     1477            array(
     1478                'post_date'   => $post_date,
     1479                'post_status' => 'draft',
     1480            )
     1481        );
     1482        $post    = get_post( $post_id );
     1483        $this->assertEquals( $post_date, $post->post_date );
     1484        $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
     1485
     1486        $post_id = self::factory()->post->create(
     1487            array(
     1488                'post_date'     => $post_date,
     1489                'post_date_gmt' => '0000-00-00 00:00:00',
     1490                'post_status'   => 'draft',
     1491            )
     1492        );
     1493        $post    = get_post( $post_id );
     1494        $this->assertEquals( $post_date, $post->post_date );
     1495        $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
     1496
     1497        // Empty post_date_gmt without floating status
     1498        $post_id = self::factory()->post->create(
     1499            array(
     1500                'post_date'   => $post_date,
     1501                'post_status' => 'publish',
     1502            )
     1503        );
     1504        $post    = get_post( $post_id );
     1505        $this->assertEquals( $post_date, $post->post_date );
     1506        $this->assertEquals( get_gmt_from_date( $post_date ), $post->post_date_gmt );
     1507
     1508        $post_id = self::factory()->post->create(
     1509            array(
     1510                'post_date'     => $post_date,
     1511                'post_date_gmt' => '0000-00-00 00:00:00',
     1512                'post_status'   => 'publish',
     1513            )
     1514        );
     1515        $post    = get_post( $post_id );
     1516        $this->assertEquals( $post_date, $post->post_date );
     1517        $this->assertEquals( get_gmt_from_date( $post_date ), $post->post_date_gmt );
     1518
     1519        // Valid post_date_gmt
     1520        $post_id = self::factory()->post->create(
     1521            array(
     1522                'post_date'     => $post_date,
     1523                'post_date_gmt' => $post_date_gmt,
     1524            )
     1525        );
     1526        $post    = get_post( $post_id );
     1527        $this->assertEquals( $post_date, $post->post_date );
     1528        $this->assertEquals( $post_date_gmt, $post->post_date_gmt );
     1529
     1530        // Invalid post_date_gmt
     1531        $post_id = self::factory()->post->create(
     1532            array(
     1533                'post_date'     => $post_date,
     1534                'post_date_gmt' => $invalid_date,
     1535            )
     1536        );
     1537        $post    = get_post( $post_id );
     1538        $this->assertEquals( $post_date, $post->post_date );
     1539        $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
     1540    }
     1541
     1542    /**
     1543     * @ticket 52187
     1544     */
     1545    public function test_insert_invalid_post_date() {
     1546        $post_date     = '2020-12-28 11:26:35';
     1547        $post_date_gmt = '2020-12-29 10:11:45';
     1548        $invalid_date  = '2020-12-41 14:15:27';
     1549
     1550        // Empty post_date_gmt with floating status
     1551        $post_id = self::factory()->post->create(
     1552            array(
     1553                'post_date'   => $invalid_date,
     1554                'post_status' => 'draft',
     1555            )
     1556        );
     1557        $this->assertEquals( 0, $post_id );
     1558
     1559        $post_id = self::factory()->post->create(
     1560            array(
     1561                'post_date'     => $invalid_date,
     1562                'post_date_gmt' => '0000-00-00 00:00:00',
     1563                'post_status'   => 'draft',
     1564            )
     1565        );
     1566        $this->assertEquals( 0, $post_id );
     1567
     1568        // Empty post_date_gmt without floating status
     1569        $post_id = self::factory()->post->create(
     1570            array(
     1571                'post_date'   => $invalid_date,
     1572                'post_status' => 'publish',
     1573            )
     1574        );
     1575        $this->assertEquals( 0, $post_id );
     1576
     1577        $post_id = self::factory()->post->create(
     1578            array(
     1579                'post_date'     => $invalid_date,
     1580                'post_date_gmt' => '0000-00-00 00:00:00',
     1581                'post_status'   => 'publish',
     1582            )
     1583        );
     1584        $this->assertEquals( 0, $post_id );
     1585
     1586        // Valid post_date_gmt
     1587        $post_id = self::factory()->post->create(
     1588            array(
     1589                'post_date'     => $invalid_date,
     1590                'post_date_gmt' => $post_date_gmt,
     1591            )
     1592        );
     1593        $this->assertEquals( 0, $post_id );
     1594
     1595        // Invalid post_date_gmt
     1596        $post_id = self::factory()->post->create(
     1597            array(
     1598                'post_date'     => $invalid_date,
     1599                'post_date_gmt' => $invalid_date,
     1600            )
     1601        );
     1602        $this->assertEquals( 0, $post_id );
     1603    }
     1604
     1605    /**
     1606     * @ticket 52187
     1607     */
     1608    function test_wp_resolve_post_date() {
     1609        $post_date     = '2020-12-28 11:26:35';
     1610        $post_date_gmt = '2020-12-29 10:11:45';
     1611        $invalid_date  = '2020-12-41 14:15:27';
     1612
     1613        $resolved_post_date = wp_resolve_post_date();
     1614        $this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $resolved_post_date ), 2, 'The dates should be equal' );
     1615
     1616        $resolved_post_date = wp_resolve_post_date( '', $post_date_gmt );
     1617        $this->assertEquals( get_date_from_gmt( $post_date_gmt ), $resolved_post_date );
     1618
     1619        $resolved_post_date = wp_resolve_post_date( '', $invalid_date );
     1620        $this->assertEquals( '1970-01-01 00:00:00', $resolved_post_date );
     1621
     1622        $resolved_post_date = wp_resolve_post_date( $post_date );
     1623        $this->assertEquals( $post_date, $resolved_post_date );
     1624
     1625        $resolved_post_date = wp_resolve_post_date( $post_date, $post_date_gmt );
     1626        $this->assertEquals( $post_date, $resolved_post_date );
     1627
     1628        $resolved_post_date = wp_resolve_post_date( $post_date, $invalid_date );
     1629        $this->assertEquals( $post_date, $resolved_post_date );
     1630
     1631        $resolved_post_date = wp_resolve_post_date( $invalid_date );
     1632        $this->assertFalse( $resolved_post_date );
     1633
     1634        $resolved_post_date = wp_resolve_post_date( $invalid_date, $post_date_gmt );
     1635        $this->assertFalse( $resolved_post_date );
     1636
     1637        $resolved_post_date = wp_resolve_post_date( $invalid_date, $invalid_date );
     1638        $this->assertFalse( $resolved_post_date );
     1639    }
    13981640}
Note: See TracChangeset for help on using the changeset viewer.