Make WordPress Core

Changeset 53233


Ignore:
Timestamp:
04/20/2022 12:46:01 PM (23 months ago)
Author:
audrasjb
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp.php

    r52815 r53233  
    414414        $status        = null;
    415415        $exit_required = false;
     416        $date_format   = 'D, d M Y H:i:s';
    416417
    417418        if ( is_user_logged_in() ) {
     
    421422            $expires = 10 * MINUTE_IN_SECONDS;
    422423
    423             $headers['Expires']       = gmdate( 'D, d M Y H:i:s', time() + $expires );
     424            $headers['Expires']       = gmdate( $date_format, time() + $expires );
    424425            $headers['Cache-Control'] = sprintf(
    425426                'max-age=%d, must-revalidate',
     
    460461                )
    461462            ) {
    462                 $wp_last_modified = mysql2date( 'D, d M Y H:i:s', get_lastcommentmodified( 'GMT' ), false );
     463                $wp_last_modified_post    = mysql2date( $date_format, get_lastpostmodified( 'GMT' ), false );
     464                $wp_last_modified_comment = mysql2date( $date_format, get_lastcommentmodified( 'GMT' ), false );
     465                if ( strtotime( $wp_last_modified_post ) > strtotime( $wp_last_modified_comment ) ) {
     466                    $wp_last_modified = $wp_last_modified_post;
     467                } else {
     468                    $wp_last_modified = $wp_last_modified_comment;
     469                }
    463470            } else {
    464                 $wp_last_modified = mysql2date( 'D, d M Y H:i:s', get_lastpostmodified( 'GMT' ), false );
     471                $wp_last_modified = mysql2date( $date_format, get_lastpostmodified( 'GMT' ), false );
    465472            }
    466473
    467474            if ( ! $wp_last_modified ) {
    468                 $wp_last_modified = gmdate( 'D, d M Y H:i:s' );
     475                $wp_last_modified = gmdate( $date_format );
    469476            }
    470477
  • trunk/tests/phpunit/tests/feed/rss2.php

    r52010 r53233  
    495495
    496496    }
     497
     498    /**
     499     * Test that the Last-Modified is a post's date when a more recent comment exists,
     500     * but the "withcomments=1" query var is not passed.
     501     *
     502     * @ticket 47968
     503     *
     504     * @covers ::send_headers
     505     */
     506    public function test_feed_last_modified_should_be_a_post_date_when_withcomments_is_not_passed() {
     507        $last_week = gmdate( 'Y-m-d H:i:s', strtotime( '-1 week' ) );
     508        $yesterday = gmdate( 'Y-m-d H:i:s', strtotime( '-1 day' ) );
     509
     510        // Create a post dated last week.
     511        $post_id = $this->factory()->post->create( array( 'post_date' => $last_week ) );
     512
     513        // Create a comment dated yesterday.
     514        $this->factory()->comment->create(
     515            array(
     516                'comment_post_ID' => $post_id,
     517                'comment_date'    => $yesterday,
     518            )
     519        );
     520
     521        // The Last-Modified header should have the post's date when "withcomments" is not passed.
     522        add_filter(
     523            'wp_headers',
     524            function( $headers ) use ( $last_week ) {
     525                $this->assertSame(
     526                    strtotime( $headers['Last-Modified'] ),
     527                    strtotime( $last_week ),
     528                    'Last-Modified was not the date of the post'
     529                );
     530                return $headers;
     531            }
     532        );
     533
     534        $this->go_to( '/?feed=rss2' );
     535    }
     536
     537    /**
     538     * Test that the Last-Modified is a comment's date when a more recent comment exists,
     539     * and the "withcomments=1" query var is passed.
     540     *
     541     * @ticket 47968
     542     *
     543     * @covers ::send_headers
     544     */
     545    public function test_feed_last_modified_should_be_the_date_of_a_comment_that_is_the_latest_update_when_withcomments_is_passed() {
     546        $last_week = gmdate( 'Y-m-d H:i:s', strtotime( '-1 week' ) );
     547        $yesterday = gmdate( 'Y-m-d H:i:s', strtotime( '-1 day' ) );
     548
     549        // Create a post dated last week.
     550        $post_id = $this->factory()->post->create( array( 'post_date' => $last_week ) );
     551
     552        // Create a comment dated yesterday.
     553        $this->factory()->comment->create(
     554            array(
     555                'comment_post_ID' => $post_id,
     556                'comment_date'    => $yesterday,
     557            )
     558        );
     559
     560        // The Last-Modified header should have the comment's date when "withcomments=1" is passed.
     561        add_filter(
     562            'wp_headers',
     563            function( $headers ) use ( $yesterday ) {
     564                $this->assertSame(
     565                    strtotime( $headers['Last-Modified'] ),
     566                    strtotime( $yesterday ),
     567                    'Last-Modified was not the date of the comment'
     568                );
     569                return $headers;
     570            }
     571        );
     572
     573        $this->go_to( '/?feed=rss2&withcomments=1' );
     574    }
     575
     576    /**
     577     * Test that the Last-Modified is the latest post's date when an earlier post and comment exist,
     578     * and the "withcomments=1" query var is passed.
     579     *
     580     * @ticket 47968
     581     *
     582     * @covers ::send_headers
     583     */
     584    public function test_feed_last_modified_should_be_the_date_of_a_post_that_is_the_latest_update_when_withcomments_is_passed() {
     585        $last_week = gmdate( 'Y-m-d H:i:s', strtotime( '-1 week' ) );
     586        $yesterday = gmdate( 'Y-m-d H:i:s', strtotime( '-1 day' ) );
     587        $today     = gmdate( 'Y-m-d H:i:s' );
     588
     589        // Create a post dated last week.
     590        $post_id = $this->factory()->post->create( array( 'post_date' => $last_week ) );
     591
     592        // Create a comment dated yesterday.
     593        $this->factory()->comment->create(
     594            array(
     595                'comment_post_ID' => $post_id,
     596                'comment_date'    => $yesterday,
     597            )
     598        );
     599
     600        // Create a post dated today.
     601        $this->factory()->post->create( array( 'post_date' => $today ) );
     602
     603        // The Last-Modified header should have the date from today's post when it is the latest update.
     604        add_filter(
     605            'wp_headers',
     606            function( $headers ) use ( $today ) {
     607                $this->assertSame(
     608                    strtotime( $headers['Last-Modified'] ),
     609                    strtotime( $today ),
     610                    'Last-Modified was not the date of the most recent most'
     611                );
     612                return $headers;
     613            }
     614        );
     615
     616        $this->go_to( '/?feed=rss2&withcomments=1' );
     617    }
    497618}
Note: See TracChangeset for help on using the changeset viewer.