Make WordPress Core

Ticket #47968: 47968.1.patch

File 47968.1.patch, 4.2 KB (added by mauteri, 4 years ago)

Updated patch with code adjustment and unit test.

  • src/wp-includes/class-wp.php

     
    401401                $headers       = array();
    402402                $status        = null;
    403403                $exit_required = false;
     404                $date_format   = 'D, d M Y H:i:s';
    404405
    405406                if ( is_user_logged_in() ) {
    406407                        $headers = array_merge( $headers, wp_get_nocache_headers() );
     
    408409                        // Unmoderated comments are only visible for 10 minutes via the moderation hash.
    409410                        $expires = 10 * MINUTE_IN_SECONDS;
    410411
    411                         $headers['Expires']       = gmdate( 'D, d M Y H:i:s', time() + $expires );
     412                        $headers['Expires']       = gmdate( $date_format, time() + $expires );
    412413                        $headers['Cache-Control'] = sprintf(
    413414                                'max-age=%d, must-revalidate',
    414415                                $expires
     
    447448                                        )
    448449                                )
    449450                        ) {
    450                                 $wp_last_modified = mysql2date( 'D, d M Y H:i:s', get_lastcommentmodified( 'GMT' ), false );
     451                                $wp_last_modified_post    = mysql2date( $date_format, get_lastpostmodified( 'GMT' ), false );
     452                                $wp_last_modified_comment = mysql2date( $date_format, get_lastcommentmodified( 'GMT' ), false );
     453
     454                                if ( strtotime( $wp_last_modified_post ) > strtotime( $wp_last_modified_comment ) ) {
     455                                        $wp_last_modified = $wp_last_modified_post;
     456                                } else {
     457                                        $wp_last_modified = $wp_last_modified_comment;
     458                                }
    451459                        } else {
    452                                 $wp_last_modified = mysql2date( 'D, d M Y H:i:s', get_lastpostmodified( 'GMT' ), false );
     460                                $wp_last_modified = mysql2date( $date_format, get_lastpostmodified( 'GMT' ), false );
    453461                        }
    454462
    455463                        if ( ! $wp_last_modified ) {
    456                                 $wp_last_modified = gmdate( 'D, d M Y H:i:s' );
     464                                $wp_last_modified = gmdate( $date_format );
    457465                        }
    458466
    459467                        $wp_last_modified .= ' GMT';
  • tests/phpunit/tests/feed/rss2.php

     
    494494                );
    495495
    496496        }
     497
     498        /**
     499         * Test that Last-Modified in headers is properly updated from posts and comments where appropriate.
     500         *
     501         * @ticket 47968
     502         */
     503        public function test_feed_last_modified_header() {
     504                $last_week = gmdate( 'Y-m-d H:i:s', strtotime( '-1 week' ) );
     505                $yesterday = gmdate( 'Y-m-d H:i:s', strtotime( '-1 day' ) );
     506                $today     = gmdate( 'Y-m-d H:i:s' );
     507
     508                $post_id = $this->factory()->post->create(
     509                        array(
     510                                'post_date' => $last_week,
     511                        )
     512                );
     513
     514                $filter_post_modified_last_week = function( $headers ) use ( $last_week ) {
     515                        $this->assertSame( strtotime( $headers['Last-Modified'] ), strtotime( $last_week ) );
     516
     517                        return $headers;
     518                };
     519
     520                add_filter( 'wp_headers', $filter_post_modified_last_week );
     521
     522                // Last modified by a post last week.
     523                $this->go_to( '/?feed=rss2' );
     524
     525                $this->factory()->comment->create(
     526                        array(
     527                                'comment_post_ID' => $post_id,
     528                                'comment_date'    => $yesterday,
     529                        )
     530                );
     531
     532                // Last modified by a comment yesterday, however, we are calling a feed without comments, so Last-Modified is still from last week's post.
     533                $this->go_to( '/?feed=rss2' );
     534
     535                remove_filter( 'wp_headers', $filter_post_modified_last_week );
     536
     537                $filter_comment_modified_yesterday = function( $headers ) use ( $yesterday ) {
     538                        $this->assertSame( strtotime( $headers['Last-Modified'] ), strtotime( $yesterday ) );
     539
     540                        return $headers;
     541                };
     542
     543                add_filter( 'wp_headers', $filter_comment_modified_yesterday );
     544
     545                // Last modified by a comment yesterday, we are calling a feed with comments, so Last-Modified is from yesterday's comment.
     546                $this->go_to( '/?feed=rss2&withcomments=1' );
     547
     548                remove_filter( 'wp_headers', $filter_comment_modified_yesterday );
     549
     550                $this->factory()->post->create(
     551                        array(
     552                                'post_date' => $today,
     553                        )
     554                );
     555
     556                $filter_post_modified_today = function( $headers ) use ( $today ) {
     557                        $this->assertSame( strtotime( $headers['Last-Modified'] ), strtotime( $today ) );
     558
     559                        return $headers;
     560                };
     561
     562                add_filter( 'wp_headers', $filter_post_modified_today );
     563
     564                // Last modified by a post today, so that should be the Last-Modified datetime.
     565                $this->go_to( '/?feed=rss2&withcomments=1' );
     566
     567                remove_filter( 'wp_headers', $filter_post_modified_today );
     568        }
    497569}