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.
Built from https://develop.svn.wordpress.org/branches/5.3@46977


git-svn-id: http://core.svn.wordpress.org/branches/5.3@46777 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2019-12-17 20:54:03 +00:00
parent 6d7dca07b0
commit cd10cf0c79
3 changed files with 43 additions and 38 deletions

View File

@ -649,55 +649,59 @@ function self_link() {
}
/**
* Get the timestamp of the most recently modified post from WP_Query.
* Get the UTC time of the most recently modified post from WP_Query.
*
* If viewing a comment feed, the timestamp of the most recently modified
* If viewing a comment feed, the time of the most recently modified
* comment will be returned.
*
* @global WP_Query $wp_query WordPress Query object.
*
* @since 5.2.0
*
* @param string $format Format of the timestamp to return, passed to mysql2date.
*
* @return string The timestamp.
* @param string $format Date format string to return the time in.
* @return string|false The time in requested format, or false on failure.
*/
function get_feed_build_date( $format ) {
global $wp_query;
if ( empty( $wp_query ) || ! $wp_query->have_posts() ) {
// Fallback to last time any post was modified or published.
return get_lastpostmodified( 'GMT' );
$datetime = false;
$max_modified_time = false;
$utc = new DateTimeZone( 'UTC' );
if ( ! empty( $wp_query ) && $wp_query->have_posts() ) {
// Extract the post modified times from the posts.
$modified_times = wp_list_pluck( $wp_query->posts, 'post_modified_gmt' );
// If this is a comment feed, check those objects too.
if ( $wp_query->is_comment_feed() && $wp_query->comment_count ) {
// Extract the comment modified times from the comments.
$comment_times = wp_list_pluck( $wp_query->comments, 'comment_date_gmt' );
// Add the comment times to the post times for comparison.
$modified_times = array_merge( $modified_times, $comment_times );
}
// Determine the maximum modified time.
$datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', max( $modified_times ), $utc );
}
// Extract the post modified times from the posts.
$modified_times = wp_list_pluck( $wp_query->posts, 'post_modified_gmt' );
// If this is a comment feed, check those objects too.
if ( $wp_query->is_comment_feed() && $wp_query->comment_count ) {
// Extract the comment modified times from the comments.
$comment_times = wp_list_pluck( $wp_query->comments, 'comment_date_gmt' );
// Add the comment times to the post times for comparison.
$modified_times = array_merge( $modified_times, $comment_times );
if ( false === $datetime ) {
// Fall back to last time any post was modified or published.
$datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', get_lastpostmodified( 'GMT' ), $utc );
}
// Determine the maximum modified time.
$datetime = date_create_immutable_from_format(
'Y-m-d H:i:s',
max( $modified_times ),
new DateTimeZone( 'UTC' )
);
$max_modified_time = $datetime->format( $format );
if ( false !== $datetime ) {
$max_modified_time = $datetime->format( $format );
}
/**
* Filters the date the last post or comment in the query was modified.
*
* @since 5.2.0
*
* @param string $max_modified_time Date the last post or comment was modified in the query.
* @param string $format The date format requested in get_feed_build_date.
* @param string|false $max_modified_time Date the last post or comment was modified in the query, in UTC.
* False on failure.
* @param string $format The date format requested in get_feed_build_date().
*/
return apply_filters( 'get_feed_build_date', $max_modified_time, $format );
}

View File

@ -6386,7 +6386,7 @@ function get_posts_by_author_sql( $post_type, $full = true, $post_author = null,
* 'gmt' uses the `post_date_gmt` field.
* Default 'server'.
* @param string $post_type Optional. The post type to check. Default 'any'.
* @return string The date of the last post.
* @return string The date of the last post, or false on failure.
*/
function get_lastpostdate( $timezone = 'server', $post_type = 'any' ) {
/**
@ -6394,9 +6394,9 @@ function get_lastpostdate( $timezone = 'server', $post_type = 'any' ) {
*
* @since 2.3.0
*
* @param string $date Date the last post was published.
* @param string $timezone Location to use for getting the post published date.
* See get_lastpostdate() for accepted `$timezone` values.
* @param string|false $date Date the last post was published. False on failure.
* @param string $timezone Location to use for getting the post published date.
* See get_lastpostdate() for accepted `$timezone` values.
*/
return apply_filters( 'get_lastpostdate', _get_last_post_time( $timezone, 'date', $post_type ), $timezone );
}
@ -6415,7 +6415,7 @@ function get_lastpostdate( $timezone = 'server', $post_type = 'any' ) {
* for information on accepted values.
* Default 'server'.
* @param string $post_type Optional. The post type to check. Default 'any'.
* @return string The timestamp in 'Y-m-d H:i:s' format.
* @return string The timestamp in 'Y-m-d H:i:s' format, or false on failure.
*/
function get_lastpostmodified( $timezone = 'server', $post_type = 'any' ) {
/**
@ -6446,9 +6446,10 @@ function get_lastpostmodified( $timezone = 'server', $post_type = 'any' ) {
*
* @since 2.3.0
*
* @param string $lastpostmodified The most recent time that a post was modified, in 'Y-m-d H:i:s' format.
* @param string $timezone Location to use for getting the post modified date.
* See get_lastpostdate() for accepted `$timezone` values.
* @param string|false $lastpostmodified The most recent time that a post was modified, in 'Y-m-d H:i:s' format.
* False on failure.
* @param string $timezone Location to use for getting the post modified date.
* See get_lastpostdate() for accepted `$timezone` values.
*/
return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone );
}
@ -6466,7 +6467,7 @@ function get_lastpostmodified( $timezone = 'server', $post_type = 'any' ) {
* for information on accepted values.
* @param string $field Post field to check. Accepts 'date' or 'modified'.
* @param string $post_type Optional. The post type to check. Default 'any'.
* @return string|false The timestamp in 'Y-m-d H:i:s' format, or false on error.
* @return string|false The timestamp in 'Y-m-d H:i:s' format, or false on failure.
*/
function _get_last_post_time( $timezone, $field, $post_type = 'any' ) {
global $wpdb;

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.3.2-alpha-46975';
$wp_version = '5.3.2-alpha-46977';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.