Feeds: ensure build/update date matches current query.

Displaying the correct build date in feeds is as important today as it was twelve years ago when this ticket was opened.

Fix an issue where all feeds in WordPress showed the same date for their last build date (the datapoint is `lastBuildDate`, `updated` or `dc:date` depending on the feed type). 

Introduce a new `get_last_build_date` filter to adjust the date used for `lastBuildDate`. Developers who previously filtered `get_lastcommentmodified` to alter feed dates should use this filter instead.

* `get_last_build_date` extracts the latest post (or comment) in the current WP_Query object.
* In all feed templates, use `get_last_build_date` vs `get_lastpostmodified( 'GMT' );`.

Props stevenkword, spacedmonkey, ryanshoover, mauteri, nacin, jorbin, MikeNGarrett, Denis-de-Bernardy, peaceablewhale.
Fixes #4575.


Built from https://develop.svn.wordpress.org/trunk@44948


git-svn-id: http://core.svn.wordpress.org/trunk@44779 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Adam Silverstein 2019-03-20 20:39:00 +00:00
parent b07bafba7e
commit 4268033aaf
8 changed files with 52 additions and 7 deletions

View File

@ -45,7 +45,7 @@ do_action( 'rss_tag_pre', 'atom-comments' );
<updated> <updated>
<?php <?php
$date = get_lastcommentmodified( 'GMT' ); $date = get_last_build_date();
echo $date ? mysql2date( 'Y-m-d\TH:i:s\Z', $date, false ) : date( 'Y-m-d\TH:i:s\Z' ); echo $date ? mysql2date( 'Y-m-d\TH:i:s\Z', $date, false ) : date( 'Y-m-d\TH:i:s\Z' );
?> ?>
</updated> </updated>

View File

@ -32,7 +32,7 @@ do_action( 'rss_tag_pre', 'atom' );
<updated> <updated>
<?php <?php
$date = get_lastpostmodified( 'GMT' ); $date = get_last_build_date();
echo $date ? mysql2date( 'Y-m-d\TH:i:s\Z', $date, false ) : date( 'Y-m-d\TH:i:s\Z' ); echo $date ? mysql2date( 'Y-m-d\TH:i:s\Z', $date, false ) : date( 'Y-m-d\TH:i:s\Z' );
?> ?>
</updated> </updated>

View File

@ -35,7 +35,7 @@ do_action( 'rss_tag_pre', 'rdf' );
<description><?php bloginfo_rss( 'description' ); ?></description> <description><?php bloginfo_rss( 'description' ); ?></description>
<dc:date> <dc:date>
<?php <?php
$date = get_lastpostmodified( 'GMT' ); $date = get_last_build_date();
echo $date ? mysql2date( 'Y-m-d\TH:i:s\Z', $date ) : date( 'Y-m-d\TH:i:s\Z' ); echo $date ? mysql2date( 'Y-m-d\TH:i:s\Z', $date ) : date( 'Y-m-d\TH:i:s\Z' );
?> ?>
</dc:date> </dc:date>

View File

@ -16,7 +16,7 @@ echo '<?xml version="1.0" encoding="' . get_option( 'blog_charset' ) . '"?' . '>
<description><?php bloginfo_rss( 'description' ); ?></description> <description><?php bloginfo_rss( 'description' ); ?></description>
<lastBuildDate> <lastBuildDate>
<?php <?php
$date = get_lastpostmodified( 'GMT' ); $date = get_last_build_date();
echo $date ? mysql2date( 'D, d M Y H:i:s +0000', $date ) : date( 'D, d M Y H:i:s +0000' ); echo $date ? mysql2date( 'D, d M Y H:i:s +0000', $date ) : date( 'D, d M Y H:i:s +0000' );
?> ?>
</lastBuildDate> </lastBuildDate>

View File

@ -51,7 +51,7 @@ do_action( 'rss_tag_pre', 'rss2-comments' );
<description><?php bloginfo_rss( 'description' ); ?></description> <description><?php bloginfo_rss( 'description' ); ?></description>
<lastBuildDate> <lastBuildDate>
<?php <?php
$date = get_lastcommentmodified( 'GMT' ); $date = get_last_build_date();
echo $date ? mysql2date( 'r', $date, false ) : date( 'r' ); echo $date ? mysql2date( 'r', $date, false ) : date( 'r' );
?> ?>
</lastBuildDate> </lastBuildDate>

View File

@ -44,7 +44,7 @@ do_action( 'rss_tag_pre', 'rss2' );
<description><?php bloginfo_rss( 'description' ); ?></description> <description><?php bloginfo_rss( 'description' ); ?></description>
<lastBuildDate> <lastBuildDate>
<?php <?php
$date = get_lastpostmodified( 'GMT' ); $date = get_last_build_date();
echo $date ? mysql2date( 'r', $date, false ) : date( 'r' ); echo $date ? mysql2date( 'r', $date, false ) : date( 'r' );
?> ?>
</lastBuildDate> </lastBuildDate>

View File

@ -637,6 +637,51 @@ function self_link() {
echo esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) ); echo esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) );
} }
/*
* Get the timestamp of the most recently modified post from WP_Query.
*
* If viewing a comment feed, the date of the most recently modified
* comment will be returned.
*
* @global WP_Query $wp_query The global WP_Query object.
*
* @since 5.2.0
*
* @return string The timestamp.
*/
function get_last_build_date() {
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' );
}
// 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.
$max_modified_time = max( $modified_times );
/**
* Filters the date the last post or comment in the query was modified.
*
* @since 5.2.0
*
* @param string $max_modified_times Date the last post or comment was modified in the query.
*/
return apply_filters( 'get_last_build_date', $max_modified_time );
}
/** /**
* Return the content type for specified feed type. * Return the content type for specified feed type.
* *

View File

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