Make WordPress Core


Ignore:
Timestamp:
10/25/2016 08:47:06 PM (10 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/post.php

    r38852 r38925  
    56135613
    56145614        $date = wp_cache_get( $key, 'timeinfo' );
    5615 
    5616         if ( ! $date ) {
    5617                 if ( 'any' === $post_type ) {
    5618                         $post_types = get_post_types( array( 'public' => true ) );
    5619                         array_walk( $post_types, array( $wpdb, 'escape_by_ref' ) );
    5620                         $post_types = "'" . implode( "', '", $post_types ) . "'";
    5621                 } else {
    5622                         $post_types = "'" . sanitize_key( $post_type ) . "'";
    5623                 }
    5624 
    5625                 switch ( $timezone ) {
    5626                         case 'gmt':
    5627                                 $date = $wpdb->get_var("SELECT post_{$field}_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
    5628                                 break;
    5629                         case 'blog':
    5630                                 $date = $wpdb->get_var("SELECT post_{$field} FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
    5631                                 break;
    5632                         case 'server':
    5633                                 $add_seconds_server = date( 'Z' );
    5634                                 $date = $wpdb->get_var("SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
    5635                                 break;
    5636                 }
    5637 
    5638                 if ( $date ) {
    5639                         wp_cache_set( $key, $date, 'timeinfo' );
    5640                 }
    5641         }
    5642 
    5643         return $date;
     5615        if ( false !== $date ) {
     5616                return $date;
     5617        }
     5618
     5619        if ( 'any' === $post_type ) {
     5620                $post_types = get_post_types( array( 'public' => true ) );
     5621                array_walk( $post_types, array( $wpdb, 'escape_by_ref' ) );
     5622                $post_types = "'" . implode( "', '", $post_types ) . "'";
     5623        } else {
     5624                $post_types = "'" . sanitize_key( $post_type ) . "'";
     5625        }
     5626
     5627        switch ( $timezone ) {
     5628                case 'gmt':
     5629                        $date = $wpdb->get_var("SELECT post_{$field}_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
     5630                        break;
     5631                case 'blog':
     5632                        $date = $wpdb->get_var("SELECT post_{$field} FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
     5633                        break;
     5634                case 'server':
     5635                        $add_seconds_server = date( 'Z' );
     5636                        $date = $wpdb->get_var("SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
     5637                        break;
     5638        }
     5639
     5640        if ( $date ) {
     5641                wp_cache_set( $key, $date, 'timeinfo' );
     5642
     5643                return $date;
     5644        }
     5645
     5646        return false;
    56445647}
    56455648
Note: See TracChangeset for help on using the changeset viewer.