Make WordPress Core

Ticket #52187: 52187.2.diff

File 52187.2.diff, 5.2 KB (added by jmdodd, 4 years ago)

Updated patch with requested changes and unit tests.

  • src/wp-includes/post.php

     
    36853685                'guid'                  => '',
    36863686                'import_id'             => 0,
    36873687                'context'               => '',
     3688                'post_date'             => '',
     3689                'post_date_gmt'         => '',
    36883690        );
    36893691
    36903692        $postarr = wp_parse_args( $postarr, $defaults );
     
    38183820        }
    38193821
    38203822        /*
    3821          * If the post date is empty (due to having been new or a draft) and status
    3822          * is not 'draft' or 'pending', set date to now.
     3823         * Resolve the post date from any provided post date or post date GMT strings;
     3824         * if none are provided, the date will be set to now.
    38233825         */
    3824         if ( empty( $postarr['post_date'] ) || '0000-00-00 00:00:00' === $postarr['post_date'] ) {
    3825                 if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' === $postarr['post_date_gmt'] ) {
    3826                         $post_date = current_time( 'mysql' );
    3827                 } else {
    3828                         $post_date = get_date_from_gmt( $postarr['post_date_gmt'] );
    3829                 }
    3830         } else {
    3831                 $post_date = $postarr['post_date'];
    3832         }
    3833 
    3834         // Validate the date.
    3835         $mm         = substr( $post_date, 5, 2 );
    3836         $jj         = substr( $post_date, 8, 2 );
    3837         $aa         = substr( $post_date, 0, 4 );
    3838         $valid_date = wp_checkdate( $mm, $jj, $aa, $post_date );
    3839         if ( ! $valid_date ) {
     3826        $post_date = wp_resolve_post_date( $postarr['post_date'], $postarr['post_date_gmt'] );
     3827        if ( ! $post_date ) {
    38403828                if ( $wp_error ) {
    38413829                        return new WP_Error( 'invalid_date', __( 'Invalid date.' ) );
    38423830                } else {
     
    45224510}
    45234511
    45244512/**
     4513 * Uses wp_checkdate to return a valid Gregorian-calendar value for post_date.
     4514 * If post_date is not provided, this first checks post_date_gmt if provided,
     4515 * then falls back to use the current time.
     4516 *
     4517 * For back-compat purposes in wp_insert_post, an empty post_date and an invalid
     4518 * post_date_gmt will continue to return '1970-01-01 00:00:00' rather than false.
     4519 *
     4520 * @param string $post_date     The date in mysql format.
     4521 * @param string $post_date_gmt The GMT date in mysql format.
     4522 * @return string|false A valid Gregorian-calendar date string, or false on failure.
     4523 */
     4524function wp_resolve_post_date( $post_date = '', $post_date_gmt = '' ) {
     4525        // If the date is empty, set the date to now.
     4526        if ( empty( $post_date ) || '0000-00-00 00:00:00' === $post_date ) {
     4527                if ( empty( $post_date_gmt ) || '0000-00-00 00:00:00' === $post_date_gmt ) {
     4528                        $post_date = current_time( 'mysql' );
     4529                } else {
     4530                        $post_date = get_date_from_gmt( $post_date_gmt );
     4531                }
     4532        }
     4533
     4534        // Validate the date.
     4535        $mm         = substr( $post_date, 5, 2 );
     4536        $jj         = substr( $post_date, 8, 2 );
     4537        $aa         = substr( $post_date, 0, 4 );
     4538        $valid_date = wp_checkdate( $mm, $jj, $aa, $post_date );
     4539        if ( ! $valid_date ) {
     4540                return false;
     4541        }
     4542        return $post_date;
     4543}
     4544
     4545/**
    45254546 * Computes a unique slug for the post, when given the desired slug and some post details.
    45264547 *
    45274548 * @since 2.8.0
  • tests/phpunit/tests/post.php

     
    13951395                $tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
    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_and_post_date_gmt() {
     1403                $post_id = self::factory()->post->create();
     1404                $post = get_post( $post_id );
     1405                $this->assertEqualsWithDelta( strtotime( date( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' );
     1406        }
     1407
     1408        /**
     1409         * @ticket 52187
     1410         */
     1411        public function test_insert_empty_post_date_and_valid_post_date_gmt() {
     1412                $post_date_gmt = '2020-12-29 10:15:27';
     1413                $post_id = self::factory()->post->create(
     1414                        array(
     1415                                'post_date_gmt' => $post_date_gmt,
     1416                        )
     1417                );
     1418                $post = get_post( $post_id );
     1419                $this->assertEquals( get_date_from_gmt( $post_date_gmt ), $post->post_date );
     1420        }
     1421
     1422        /**
     1423         * @ticket 52187
     1424         */
     1425        public function test_insert_empty_post_date_and_invalid_post_date_gmt() {
     1426                $post_id = self::factory()->post->create(
     1427                        array(
     1428                                'post_date' => '',
     1429                                'post_date_gmt' => '2020-12-41 10:10:10',
     1430                        )
     1431                );
     1432                $post = get_post( $post_id );
     1433                $this->assertEquals( '1970-01-01 00:00:00', $post->post_date );
     1434        }
     1435
     1436        /**
     1437         * @ticket 52187
     1438         */
     1439        public function test_wp_resolve_post_date() {
     1440                // Both parameters empty, should return "now" date, hence the delta
     1441                $this->assertEqualsWithDelta( strtotime( date( 'Y-m-d H:i:s' ) ), strtotime( wp_resolve_post_date() ), 2, 'The dates should be equal' );
     1442
     1443                $post_date = '2020-12-29 10:15:27';
     1444                $invalid_post_date = '2020-12-41 10:15:27';
     1445
     1446                $resolved_post_date = wp_resolve_post_date( $post_date );
     1447                $this->assertEquals( $post_date, $resolved_post_date );
     1448
     1449                $resolved_post_date = wp_resolve_post_date( $invalid_post_date );
     1450                $this->assertEquals( false, $resolved_post_date );
     1451
     1452                $resolved_post_date = wp_resolve_post_date( '', $post_date );
     1453                $this->assertEquals( get_date_from_gmt( $post_date ), $resolved_post_date );
     1454
     1455                $resolved_post_date = wp_resolve_post_date( '', $invalid_post_date );
     1456                $this->assertEquals( '1970-01-01 00:00:00', $resolved_post_date );
     1457        }
    13981458}