WordPress.org

Make WordPress Core

Ticket #48957: 48957-feed-build-date-fallback.patch

File 48957-feed-build-date-fallback.patch, 5.3 KB (added by Rarst, 6 months ago)
  • tests/phpunit/tests/date/getFeedBuildDate.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    3737
    3838                $this->assertEquals( '2018-07-23T03:13:23+00:00', get_feed_build_date( DATE_RFC3339 ) );
    3939        }
     40
     41        /**
     42         * Test get_feed_build_date() works with invalid post dates.
     43         *
     44         * @ticket 48957
     45         */
     46        public function test_should_fall_back_to_last_post_modified() {
     47
     48                global $wp_query;
     49
     50                update_option( 'timezone_string', 'Europe/Kiev' );
     51                $datetime     = new DateTimeImmutable( 'now', wp_timezone() );
     52                $datetime_utc = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
     53
     54                $wp_query->posts = array();
     55
     56                $this->assertFalse( get_feed_build_date( DATE_RFC3339 ), 'False when unable to determine valid time' );
     57
     58                $this->factory->post->create( array(
     59                        'post_date' => $datetime->format( 'Y-m-d H:i:s' )
     60                ) );
     61
     62                $this->assertEquals(
     63                        $datetime_utc->format( DATE_RFC3339 ),
     64                        get_feed_build_date( DATE_RFC3339 ),
     65                        'Fall back to time of last post modified with no posts'
     66                );
     67
     68                $post_id_broken = $this->factory->post->create();
     69                $post_broken    = get_post( $post_id_broken );
     70
     71                $post_broken->post_modified_gmt = 0;
     72
     73                $wp_query->posts = array( $post_broken );
     74
     75                $this->assertEquals(
     76                        $datetime_utc->format( DATE_RFC3339 ),
     77                        get_feed_build_date( DATE_RFC3339 ),
     78                        'Fall back to time of last post modified with broken post object'
     79                );
     80        }
    4081}
  • src/wp-includes/feed.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    648648}
    649649
    650650/**
    651  * Get the timestamp of the most recently modified post from WP_Query.
     651 * Get the UTC time of the most recently modified post from WP_Query.
    652652 *
    653  * If viewing a comment feed, the timestamp of the most recently modified
     653 * If viewing a comment feed, the time of the most recently modified
    654654 * comment will be returned.
    655655 *
    656656 * @global WP_Query $wp_query WordPress Query object.
    657657 *
    658658 * @since 5.2.0
    659659 *
    660  * @param string $format Format of the timestamp to return, passed to mysql2date.
     660 * @param string    $format   Date format string to return the time in.
    661661 *
    662  * @return string The timestamp.
     662 * @return string|false The time in requested format or `false` on failure.
    663663 */
    664664function get_feed_build_date( $format ) {
    665665        global $wp_query;
    666666
    667         if ( empty( $wp_query ) || ! $wp_query->have_posts() ) {
    668                 // Fallback to last time any post was modified or published.
    669                 return get_lastpostmodified( 'GMT' );
    670         }
     667        $datetime          = false;
     668        $max_modified_time = false;
     669        $utc               = new DateTimeZone( 'UTC' );
    671670
    672         // Extract the post modified times from the posts.
    673         $modified_times = wp_list_pluck( $wp_query->posts, 'post_modified_gmt' );
     671        if ( ! empty( $wp_query ) && $wp_query->have_posts() ) {
     672                // Extract the post modified times from the posts.
     673                $modified_times = wp_list_pluck( $wp_query->posts, 'post_modified_gmt' );
    674674
    675         // If this is a comment feed, check those objects too.
    676         if ( $wp_query->is_comment_feed() && $wp_query->comment_count ) {
    677                 // Extract the comment modified times from the comments.
    678                 $comment_times = wp_list_pluck( $wp_query->comments, 'comment_date_gmt' );
     675                // If this is a comment feed, check those objects too.
     676                if ( $wp_query->is_comment_feed() && $wp_query->comment_count ) {
     677                        // Extract the comment modified times from the comments.
     678                        $comment_times = wp_list_pluck( $wp_query->comments, 'comment_date_gmt' );
    679679
    680                 // Add the comment times to the post times for comparison.
    681                 $modified_times = array_merge( $modified_times, $comment_times );
    682         }
     680                        // Add the comment times to the post times for comparison.
     681                        $modified_times = array_merge( $modified_times, $comment_times );
     682                }
    683683
    684         // Determine the maximum modified time.
    685         $datetime = date_create_immutable_from_format(
    686                 'Y-m-d H:i:s',
    687                 max( $modified_times ),
    688                 new DateTimeZone( 'UTC' )
    689         );
     684                // Determine the maximum modified time.
     685                $datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', max( $modified_times ), $utc );
     686        }
    690687
    691         $max_modified_time = $datetime->format( $format );
     688        if ( false === $datetime ) {
     689                // Fallback to last time any post was modified or published.
     690                $datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', get_lastpostmodified( 'GMT' ), $utc );
     691        }
     692
     693        if ( false !== $datetime ) {
     694                $max_modified_time = $datetime->format( $format );
     695        }
    692696
    693697        /**
    694698         * Filters the date the last post or comment in the query was modified.
    695699         *
    696700         * @since 5.2.0
    697701         *
    698          * @param string $max_modified_time Date the last post or comment was modified in the query.
    699          * @param string $format            The date format requested in get_feed_build_date.
     702         * @param string|false $max_modified_time Date the last post or comment was modified in the query, in UTC.
     703         * @param string       $format            The date format requested in get_feed_build_date.
    700704         */
    701705        return apply_filters( 'get_feed_build_date', $max_modified_time, $format );
    702706}