Make WordPress Core

Changeset 34201


Ignore:
Timestamp:
09/15/2015 03:24:04 PM (10 years ago)
Author:
wonderboymusic
Message:

Allow get_lastpostdate(), get_lastpostmodified(), and _get_last_post_time() to accept $post_type and cache separately.

Props _duck, SergeyBiryukov.
See #17455.

File:
1 edited

Legend:

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

    r34163 r34201  
    52285228 *
    52295229 * @since 0.71
    5230  *
    5231  * @param string $timezone The location to get the time. Accepts 'gmt', 'blog',
    5232  *                         or 'server'. Default 'server'.
     5230 * @since 4.4.0 The `$post_type` argument was added.
     5231 *
     5232 * @param string $timezone  Optional. The location to get the time. Accepts 'gmt', 'blog',
     5233 *                          or 'server'. Default 'server'.
     5234 * @param string $post_type Optional. The post type to check. Default 'any'.
    52335235 * @return string The date of the last post.
    52345236 */
    5235 function get_lastpostdate( $timezone = 'server' ) {
     5237function get_lastpostdate( $timezone = 'server', $post_type = 'any' ) {
    52365238    /**
    52375239     * Filter the date the last post was published.
     
    52435245     * @param string $timezone Location to use for getting the post published date.
    52445246     */
    5245     return apply_filters( 'get_lastpostdate', _get_last_post_time( $timezone, 'date' ), $timezone );
     5247    return apply_filters( 'get_lastpostdate', _get_last_post_time( $timezone, 'date', $post_type ), $timezone );
    52465248}
    52475249
     
    52545256 *
    52555257 * @since 1.2.0
    5256  *
    5257  * @param string $timezone Optional. The timezone for the timestamp. Uses the server's internal timezone.
    5258  *                         Accepts 'server', 'blog', 'gmt'. or 'server'. 'server' uses the server's
    5259  *                         internal timezone. 'blog' uses the `post_modified` field, which proxies
    5260  *                         to the timezone set for the site. 'gmt' uses the `post_modified_gmt` field.
    5261  *                         Default 'server'.
     5258 * @since 4.4.0 The `$post_type` argument was added.
     5259 *
     5260 * @param string $timezone  Optional. The timezone for the timestamp. Uses the server's internal timezone.
     5261 *                          Accepts 'server', 'blog', 'gmt'. or 'server'. 'server' uses the server's
     5262 *                          internal timezone. 'blog' uses the `post_modified` field, which proxies
     5263 *                          to the timezone set for the site. 'gmt' uses the `post_modified_gmt` field.
     5264 *                          Default 'server'.
     5265 * @param string $post_type Optional. The post type to check. Default 'any'.
    52625266 * @return string The timestamp.
    52635267 */
    5264 function get_lastpostmodified( $timezone = 'server' ) {
    5265     $lastpostmodified = _get_last_post_time( $timezone, 'modified' );
     5268function get_lastpostmodified( $timezone = 'server', $post_type = 'any' ) {
     5269    $lastpostmodified = _get_last_post_time( $timezone, 'modified', $post_type );
    52665270
    52675271    $lastpostdate = get_lastpostdate($timezone);
    5268     if ( $lastpostdate > $lastpostmodified )
     5272    if ( $lastpostdate > $lastpostmodified ) {
    52695273        $lastpostmodified = $lastpostdate;
     5274    }
    52705275
    52715276    /**
     
    52855290 *
    52865291 * @since 3.1.0
     5292 * @since 4.4.0 The `$post_type` argument was added.
    52875293 * @access private
    52885294 *
    52895295 * @global wpdb $wpdb
    52905296 *
    5291  * @param string $timezone The timezone for the timestamp. See {@see get_lastpostmodified()}
    5292  *                         for information on accepted values.
    5293  * @param string $field    Post field to check. Accepts 'date' or 'modified'.
     5297 * @param string $timezone  The timezone for the timestamp. See {@see get_lastpostmodified()}
     5298 *                          for information on accepted values.
     5299 * @param string $field     Post field to check. Accepts 'date' or 'modified'.
     5300 * @param string $post_type Optional. The post type to check. Default 'any'.
    52945301 * @return string|false The timestamp.
    52955302 */
    5296 function _get_last_post_time( $timezone, $field ) {
     5303function _get_last_post_time( $timezone, $field, $post_type = 'any' ) {
    52975304    global $wpdb;
    52985305
    5299     if ( !in_array( $field, array( 'date', 'modified' ) ) )
     5306    if ( ! in_array( $field, array( 'date', 'modified' ) ) ) {
    53005307        return false;
     5308    }
    53015309
    53025310    $timezone = strtolower( $timezone );
    53035311
    53045312    $key = "lastpost{$field}:$timezone";
     5313    if ( 'any' !== $post_type ) {
     5314        $key .= ':' . sanitize_key( $post_type );
     5315    }
    53055316
    53065317    $date = wp_cache_get( $key, 'timeinfo' );
    53075318
    5308     if ( !$date ) {
    5309         $add_seconds_server = date('Z');
    5310 
    5311         $post_types = get_post_types( array( 'public' => true ) );
    5312         array_walk( $post_types, array( &$wpdb, 'escape_by_ref' ) );
    5313         $post_types = "'" . implode( "', '", $post_types ) . "'";
     5319    if ( ! $date ) {
     5320        if ( 'any' === $post_type ) {
     5321            $post_types = get_post_types( array( 'public' => true ) );
     5322            array_walk( $post_types, array( $wpdb, 'escape_by_ref' ) );
     5323            $post_types = "'" . implode( "', '", $post_types ) . "'";
     5324        } else {
     5325            $post_types = "'" . sanitize_key( $post_type ) . "'";
     5326        }
    53145327
    53155328        switch ( $timezone ) {
     
    53215334                break;
    53225335            case 'server':
     5336                $add_seconds_server = date( 'Z' );
    53235337                $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");
    53245338                break;
    53255339        }
    53265340
    5327         if ( $date )
     5341        if ( $date ) {
    53285342            wp_cache_set( $key, $date, 'timeinfo' );
     5343        }
    53295344    }
    53305345
     
    55495564            wp_cache_delete( "lastpostmodified:$timezone", 'timeinfo' );
    55505565            wp_cache_delete( "lastpostdate:$timezone", 'timeinfo' );
     5566            wp_cache_delete( "lastpostdate:$timezone:{$post->post_type}", 'timeinfo' );
    55515567        }
    55525568    }
Note: See TracChangeset for help on using the changeset viewer.