Make WordPress Core

Changeset 15838


Ignore:
Timestamp:
10/18/2010 09:06:49 PM (16 years ago)
Author:
scribu
Message:

Fix custom post type feed when no regular posts present. Props aaroncampbell for initial patch. Fixes #14922

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions.php

    r15834 r15838  
    278278                return true;
    279279        return false;
     280}
     281
     282/**
     283 * Generates a unique key from an argument
     284 *
     285 * @since 3.1
     286 *
     287 * @param mixed $arg
     288 * @return string
     289 */
     290function wp_cache_key( $arg ) {
     291        if ( is_scalar( $arg ) )
     292                return md5( $arg );
     293
     294        $arg = (array) $arg;
     295        sort( $arg );
     296
     297        return md5( serialize( $arg ) );
    280298}
    281299
  • trunk/wp-includes/post.php

    r15831 r15838  
    39463946 * @since 0.71
    39473947 *
    3948  * @uses $wpdb
    3949  * @uses $blog_id
    39503948 * @uses apply_filters() Calls 'get_lastpostdate' filter
    3951  *
    3952  * @global mixed $cache_lastpostdate Stores the last post date
    3953  * @global mixed $pagenow The current page being viewed
    39543949 *
    39553950 * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'.
    … …  
    39573952 */
    39583953function get_lastpostdate($timezone = 'server') {
    3959         global $cache_lastpostdate, $wpdb, $blog_id;
    3960         $add_seconds_server = date('Z');
    3961         if ( !isset($cache_lastpostdate[$blog_id][$timezone]) ) {
    3962                 switch(strtolower($timezone)) {
    3963                         case 'gmt':
    3964                                 $lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date_gmt DESC LIMIT 1");
    3965                                 break;
    3966                         case 'blog':
    3967                                 $lastpostdate = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date_gmt DESC LIMIT 1");
    3968                                 break;
    3969                         case 'server':
    3970                                 $lastpostdate = $wpdb->get_var("SELECT DATE_ADD(post_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date_gmt DESC LIMIT 1");
    3971                                 break;
    3972                 }
    3973                 $cache_lastpostdate[$blog_id][$timezone] = $lastpostdate;
    3974         } else {
    3975                 $lastpostdate = $cache_lastpostdate[$blog_id][$timezone];
    3976         }
    3977         return apply_filters( 'get_lastpostdate', $lastpostdate, $timezone );
     3954        return apply_filters( 'get_lastpostdate', _get_last_post_time( $timezone, 'date' ), $timezone );
    39783955}
    39793956
    … …  
    39863963 *
    39873964 * @since 1.2.0
    3988  * @uses $wpdb
    3989  * @uses $blog_id
    39903965 * @uses apply_filters() Calls 'get_lastpostmodified' filter
    39913966 *
    … …  
    39943969 */
    39953970function get_lastpostmodified($timezone = 'server') {
    3996         global $wpdb;
    3997 
    3998         $add_seconds_server = date('Z');
    3999         $timezone = strtolower( $timezone );
    4000 
    4001         $lastpostmodified = wp_cache_get( "lastpostmodified:$timezone", 'timeinfo' );
    4002         if ( $lastpostmodified )
    4003                 return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone );
    4004 
    4005         switch ( strtolower($timezone) ) {
    4006                 case 'gmt':
    4007                         $lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_modified_gmt DESC LIMIT 1");
    4008                         break;
    4009                 case 'blog':
    4010                         $lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_modified_gmt DESC LIMIT 1");
    4011                         break;
    4012                 case 'server':
    4013                         $lastpostmodified = $wpdb->get_var("SELECT DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_modified_gmt DESC LIMIT 1");
    4014                         break;
    4015         }
     3971        $lastpostmodified = _get_last_post_time( $timezone, 'modified' );
    40163972
    40173973        $lastpostdate = get_lastpostdate($timezone);
    … …  
    40193975                $lastpostmodified = $lastpostdate;
    40203976
    4021         if ( $lastpostmodified )
    4022                 wp_cache_set( "lastpostmodified:$timezone", $lastpostmodified, 'timeinfo' );
    4023 
    40243977        return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone );
     3978}
     3979
     3980function _get_last_post_time( $timezone, $field ) {
     3981        global $wpdb, $blog_id;
     3982
     3983        if ( !in_array( $field, array( 'date', 'modified' ) ) )
     3984                return false;
     3985
     3986        $post_types = get_query_var('post_type');
     3987        if ( empty($post_types) )
     3988                $post_types = 'post';
     3989
     3990        $post_types = apply_filters( "get_lastpost{$field}_post_types", (array) $post_types );
     3991
     3992        $key = "lastpost{$field}:$blog_id:$timezone:" . wp_cache_key( $post_types );
     3993
     3994        $date = wp_cache_get( $key, 'timeinfo' );
     3995
     3996        if ( !$date ) {
     3997                $add_seconds_server = date('Z');
     3998
     3999                array_walk( $post_types, array( &$wpdb, 'escape_by_ref' ) );
     4000                $post_types = "'" . implode( "', '", $post_types ) . "'";
     4001
     4002                switch ( strtolower( $timezone ) ) {
     4003                        case 'gmt':
     4004                                $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");
     4005                                break;
     4006                        case 'blog':
     4007                                $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");
     4008                                break;
     4009                        case 'server':
     4010                                $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");
     4011                                break;
     4012                }
     4013
     4014                if ( $date )
     4015                        wp_cache_set( $key, $date, 'timeinfo' );
     4016        }
     4017
     4018        return $date;
    40254019}
    40264020
Note: See TracChangeset for help on using the changeset viewer.