Feeds: Use latest comment date for the Last-Modified header of comments feed.

Previously, the `Last-Modified` header of comments feed was using the date/time of the last comment. This behavior was breaking caching and causing feed readers to believe there is no new content even when there might be new posts.

This commit changes this behavior to use the newest date from both `get_lastcommentmodified()` and `get_lastpostmodified()` functions instead of only using the result from `get_lastcommentmodified()`.

Props xiven, mauteri, costdev, audrasjb.
Fixes #47968.


git-svn-id: https://develop.svn.wordpress.org/trunk@53233 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jb Audras 2022-04-20 12:46:01 +00:00
parent 8ad5aec839
commit 8cb291cbd0
2 changed files with 132 additions and 4 deletions

View File

@ -413,6 +413,7 @@ class WP {
$headers = array();
$status = null;
$exit_required = false;
$date_format = 'D, d M Y H:i:s';
if ( is_user_logged_in() ) {
$headers = array_merge( $headers, wp_get_nocache_headers() );
@ -420,7 +421,7 @@ class WP {
// Unmoderated comments are only visible for 10 minutes via the moderation hash.
$expires = 10 * MINUTE_IN_SECONDS;
$headers['Expires'] = gmdate( 'D, d M Y H:i:s', time() + $expires );
$headers['Expires'] = gmdate( $date_format, time() + $expires );
$headers['Cache-Control'] = sprintf(
'max-age=%d, must-revalidate',
$expires
@ -459,13 +460,19 @@ class WP {
)
)
) {
$wp_last_modified = mysql2date( 'D, d M Y H:i:s', get_lastcommentmodified( 'GMT' ), false );
$wp_last_modified_post = mysql2date( $date_format, get_lastpostmodified( 'GMT' ), false );
$wp_last_modified_comment = mysql2date( $date_format, get_lastcommentmodified( 'GMT' ), false );
if ( strtotime( $wp_last_modified_post ) > strtotime( $wp_last_modified_comment ) ) {
$wp_last_modified = $wp_last_modified_post;
} else {
$wp_last_modified = $wp_last_modified_comment;
}
} else {
$wp_last_modified = mysql2date( 'D, d M Y H:i:s', get_lastpostmodified( 'GMT' ), false );
$wp_last_modified = mysql2date( $date_format, get_lastpostmodified( 'GMT' ), false );
}
if ( ! $wp_last_modified ) {
$wp_last_modified = gmdate( 'D, d M Y H:i:s' );
$wp_last_modified = gmdate( $date_format );
}
$wp_last_modified .= ' GMT';

View File

@ -494,4 +494,125 @@ class Tests_Feed_RSS2 extends WP_UnitTestCase {
);
}
/**
* Test that the Last-Modified is a post's date when a more recent comment exists,
* but the "withcomments=1" query var is not passed.
*
* @ticket 47968
*
* @covers ::send_headers
*/
public function test_feed_last_modified_should_be_a_post_date_when_withcomments_is_not_passed() {
$last_week = gmdate( 'Y-m-d H:i:s', strtotime( '-1 week' ) );
$yesterday = gmdate( 'Y-m-d H:i:s', strtotime( '-1 day' ) );
// Create a post dated last week.
$post_id = $this->factory()->post->create( array( 'post_date' => $last_week ) );
// Create a comment dated yesterday.
$this->factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_date' => $yesterday,
)
);
// The Last-Modified header should have the post's date when "withcomments" is not passed.
add_filter(
'wp_headers',
function( $headers ) use ( $last_week ) {
$this->assertSame(
strtotime( $headers['Last-Modified'] ),
strtotime( $last_week ),
'Last-Modified was not the date of the post'
);
return $headers;
}
);
$this->go_to( '/?feed=rss2' );
}
/**
* Test that the Last-Modified is a comment's date when a more recent comment exists,
* and the "withcomments=1" query var is passed.
*
* @ticket 47968
*
* @covers ::send_headers
*/
public function test_feed_last_modified_should_be_the_date_of_a_comment_that_is_the_latest_update_when_withcomments_is_passed() {
$last_week = gmdate( 'Y-m-d H:i:s', strtotime( '-1 week' ) );
$yesterday = gmdate( 'Y-m-d H:i:s', strtotime( '-1 day' ) );
// Create a post dated last week.
$post_id = $this->factory()->post->create( array( 'post_date' => $last_week ) );
// Create a comment dated yesterday.
$this->factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_date' => $yesterday,
)
);
// The Last-Modified header should have the comment's date when "withcomments=1" is passed.
add_filter(
'wp_headers',
function( $headers ) use ( $yesterday ) {
$this->assertSame(
strtotime( $headers['Last-Modified'] ),
strtotime( $yesterday ),
'Last-Modified was not the date of the comment'
);
return $headers;
}
);
$this->go_to( '/?feed=rss2&withcomments=1' );
}
/**
* Test that the Last-Modified is the latest post's date when an earlier post and comment exist,
* and the "withcomments=1" query var is passed.
*
* @ticket 47968
*
* @covers ::send_headers
*/
public function test_feed_last_modified_should_be_the_date_of_a_post_that_is_the_latest_update_when_withcomments_is_passed() {
$last_week = gmdate( 'Y-m-d H:i:s', strtotime( '-1 week' ) );
$yesterday = gmdate( 'Y-m-d H:i:s', strtotime( '-1 day' ) );
$today = gmdate( 'Y-m-d H:i:s' );
// Create a post dated last week.
$post_id = $this->factory()->post->create( array( 'post_date' => $last_week ) );
// Create a comment dated yesterday.
$this->factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_date' => $yesterday,
)
);
// Create a post dated today.
$this->factory()->post->create( array( 'post_date' => $today ) );
// The Last-Modified header should have the date from today's post when it is the latest update.
add_filter(
'wp_headers',
function( $headers ) use ( $today ) {
$this->assertSame(
strtotime( $headers['Last-Modified'] ),
strtotime( $today ),
'Last-Modified was not the date of the most recent most'
);
return $headers;
}
);
$this->go_to( '/?feed=rss2&withcomments=1' );
}
}