Make WordPress Core


Ignore:
Timestamp:
12/17/2019 08:52:44 PM (6 years ago)
Author:
SergeyBiryukov
Message:

Date/Time: Ensure that get_feed_build_date() correctly handles a modified post object with invalid date.

  • Clarify in the documentation that the function returns false on failure.
  • Consistently pass the return value through the get_feed_build_date filter.

Props Rarst, dd32, azaozz, tellyworth.
Merges [46974] and [46973] to the 5.3 branch.
Fixes #48957.

Location:
branches/5.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.3

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

    r46774 r46977  
    650650
    651651/**
    652  * Get the timestamp of the most recently modified post from WP_Query.
    653  *
    654  * If viewing a comment feed, the timestamp of the most recently modified
     652 * Get the UTC time of the most recently modified post from WP_Query.
     653 *
     654 * If viewing a comment feed, the time of the most recently modified
    655655 * comment will be returned.
    656656 *
     
    659659 * @since 5.2.0
    660660 *
    661  * @param string $format Format of the timestamp to return, passed to mysql2date.
    662  *
    663  * @return string The timestamp.
     661 * @param string $format Date format string to return the time in.
     662 * @return string|false The time in requested format, or false on failure.
    664663 */
    665664function get_feed_build_date( $format ) {
    666665    global $wp_query;
    667666
    668     if ( empty( $wp_query ) || ! $wp_query->have_posts() ) {
    669         // Fallback to last time any post was modified or published.
    670         return get_lastpostmodified( 'GMT' );
    671     }
    672 
    673     // Extract the post modified times from the posts.
    674     $modified_times = wp_list_pluck( $wp_query->posts, 'post_modified_gmt' );
    675 
    676     // If this is a comment feed, check those objects too.
    677     if ( $wp_query->is_comment_feed() && $wp_query->comment_count ) {
    678         // Extract the comment modified times from the comments.
    679         $comment_times = wp_list_pluck( $wp_query->comments, 'comment_date_gmt' );
    680 
    681         // Add the comment times to the post times for comparison.
    682         $modified_times = array_merge( $modified_times, $comment_times );
    683     }
    684 
    685     // Determine the maximum modified time.
    686     $datetime = date_create_immutable_from_format(
    687         'Y-m-d H:i:s',
    688         max( $modified_times ),
    689         new DateTimeZone( 'UTC' )
    690     );
    691 
    692     $max_modified_time = $datetime->format( $format );
     667    $datetime          = false;
     668    $max_modified_time = false;
     669    $utc               = new DateTimeZone( 'UTC' );
     670
     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' );
     674
     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' );
     679
     680            // Add the comment times to the post times for comparison.
     681            $modified_times = array_merge( $modified_times, $comment_times );
     682        }
     683
     684        // Determine the maximum modified time.
     685        $datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', max( $modified_times ), $utc );
     686    }
     687
     688    if ( false === $datetime ) {
     689        // Fall back 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    }
    693696
    694697    /**
     
    697700     * @since 5.2.0
    698701     *
    699      * @param string $max_modified_time Date the last post or comment was modified in the query.
    700      * @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     *                                        False on failure.
     704     * @param string       $format            The date format requested in get_feed_build_date().
    701705     */
    702706    return apply_filters( 'get_feed_build_date', $max_modified_time, $format );
Note: See TracChangeset for help on using the changeset viewer.