Make WordPress Core


Ignore:
Timestamp:
10/25/2016 08:47:06 PM (8 years ago)
Author:
rachelbaker
Message:

Feeds: Always return a valid timestamp for the Last-Modified header of comment or post feeds.

Fixes bug where an invalid Last-Modified value would be returned in feed requests for sites that had 0 items to return. Comment or post feeds will now return the current timestamp as the Last-Modified header value. Example: a request for the comments feed for a site without any comments.

Replaced use of the local static variable $cache_lastcommentmodified to store the modified date in get_lastcommentmodified() with the Object Cache API. The get_lastcommentmodified() function returns early if there is a cached value and returns false if there where no comments found. Introduced _clear_modified_cache_on_transition_comment_status() to flush the lastcommentmodified cache key when a comment enters or leaves approval status. In get_lastpostmodified() return early if there is a cached value and return false if there are no posts found.

Props swissspidy, rachelbaker, dllh, leobaiano.
Fixes #38027.

File:
1 edited

Legend:

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

    r38671 r38925  
    423423            $headers['Content-Type'] = feed_content_type( $type ) . '; charset=' . get_option( 'blog_charset' );
    424424
    425             // We're showing a feed, so WP is indeed the only thing that last changed
    426             if ( !empty($this->query_vars['withcomments'])
    427                 || false !== strpos( $this->query_vars['feed'], 'comments-' )
    428                 || ( empty($this->query_vars['withoutcomments'])
    429                     && ( !empty($this->query_vars['p'])
    430                         || !empty($this->query_vars['name'])
    431                         || !empty($this->query_vars['page_id'])
    432                         || !empty($this->query_vars['pagename'])
    433                         || !empty($this->query_vars['attachment'])
    434                         || !empty($this->query_vars['attachment_id'])
    435                     )
    436                 )
    437             )
    438                 $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastcommentmodified('GMT'), 0).' GMT';
    439             else
    440                 $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0).' GMT';
     425            // We're showing a feed, so WP is indeed the only thing that last changed.
     426            if ( ! empty( $this->query_vars['withcomments'] )
     427                 || false !== strpos( $this->query_vars['feed'], 'comments-' )
     428                 || ( empty( $this->query_vars['withoutcomments'] )
     429                      && ( ! empty( $this->query_vars['p'] )
     430                           || ! empty( $this->query_vars['name'] )
     431                           || ! empty( $this->query_vars['page_id'] )
     432                           || ! empty( $this->query_vars['pagename'] )
     433                           || ! empty( $this->query_vars['attachment'] )
     434                           || ! empty( $this->query_vars['attachment_id'] )
     435                      )
     436                 )
     437            ) {
     438                $wp_last_modified = mysql2date( 'D, d M Y H:i:s', get_lastcommentmodified( 'GMT' ), false );
     439            } else {
     440                $wp_last_modified = mysql2date( 'D, d M Y H:i:s', get_lastpostmodified( 'GMT' ), false );
     441            }
     442
     443            if ( ! $wp_last_modified ) {
     444                $wp_last_modified = date( 'D, d M Y H:i:s' );
     445            }
     446
     447            $wp_last_modified .= ' GMT';
     448
    441449            $wp_etag = '"' . md5($wp_last_modified) . '"';
    442450            $headers['Last-Modified'] = $wp_last_modified;
Note: See TracChangeset for help on using the changeset viewer.