Make WordPress Core

Ticket #17455: 17455.3.diff

File 17455.3.diff, 5.8 KB (added by SergeyBiryukov, 10 years ago)
  • src/wp-includes/post-functions.php

     
    51855185 * 'gmt' is when the last post was posted in GMT formatted date.
    51865186 *
    51875187 * @since 0.71
     5188 * @since 4.3.0 The `$post_type` argument was added.
    51885189 *
    5189  * @param string $timezone The location to get the time. Accepts 'gmt', 'blog',
    5190  *                         or 'server'. Default 'server'.
     5190 * @param string $timezone  Optional. The location to get the time. Accepts 'gmt', 'blog',
     5191 *                          or 'server'. Default 'server'.
     5192 * @param string $post_type Optional. The post type to check. Default 'any'.
    51915193 * @return string The date of the last post.
    51925194 */
    5193 function get_lastpostdate( $timezone = 'server' ) {
     5195function get_lastpostdate( $timezone = 'server', $post_type = 'any' ) {
    51945196        /**
    51955197         * Filter the date the last post was published.
    51965198         *
     
    52005202         *                         'blog', or 'server'.
    52015203         * @param string $timezone Location to use for getting the post published date.
    52025204         */
    5203         return apply_filters( 'get_lastpostdate', _get_last_post_time( $timezone, 'date' ), $timezone );
     5205        return apply_filters( 'get_lastpostdate', _get_last_post_time( $timezone, 'date', $post_type ), $timezone );
    52045206}
    52055207
    52065208/**
     
    52115213 * 'gmt' is when the last post was modified in GMT time.
    52125214 *
    52135215 * @since 1.2.0
     5216 * @since 4.3.0 The `$post_type` argument was added.
    52145217 *
    5215  * @param string $timezone Optional. The timezone for the timestamp. Uses the server's internal timezone.
    5216  *                         Accepts 'server', 'blog', 'gmt'. or 'server'. 'server' uses the server's
    5217  *                         internal timezone. 'blog' uses the `post_modified` field, which proxies
    5218  *                         to the timezone set for the site. 'gmt' uses the `post_modified_gmt` field.
    5219  *                         Default 'server'.
     5218 * @param string $timezone  Optional. The timezone for the timestamp. Uses the server's internal timezone.
     5219 *                          Accepts 'server', 'blog', 'gmt'. or 'server'. 'server' uses the server's
     5220 *                          internal timezone. 'blog' uses the `post_modified` field, which proxies
     5221 *                          to the timezone set for the site. 'gmt' uses the `post_modified_gmt` field.
     5222 *                          Default 'server'.
     5223 * @param string $post_type Optional. The post type to check. Default 'any'.
    52205224 * @return string The timestamp.
    52215225 */
    5222 function get_lastpostmodified( $timezone = 'server' ) {
    5223         $lastpostmodified = _get_last_post_time( $timezone, 'modified' );
     5226function get_lastpostmodified( $timezone = 'server', $post_type = 'any' ) {
     5227        $lastpostmodified = _get_last_post_time( $timezone, 'modified', $post_type );
    52245228
    52255229        $lastpostdate = get_lastpostdate($timezone);
    52265230        if ( $lastpostdate > $lastpostmodified )
     
    52425246 * Get the timestamp of the last time any post was modified or published.
    52435247 *
    52445248 * @since 3.1.0
     5249 * @since 4.3.0 The `$post_type` argument was added.
    52455250 * @access private
    52465251 *
    52475252 * @global wpdb $wpdb
    52485253 *
    5249  * @param string $timezone The timezone for the timestamp. See {@see get_lastpostmodified()}
    5250  *                         for information on accepted values.
    5251  * @param string $field    Post field to check. Accepts 'date' or 'modified'.
     5254 * @param string $timezone  The timezone for the timestamp. See {@see get_lastpostmodified()}
     5255 *                          for information on accepted values.
     5256 * @param string $field     Post field to check. Accepts 'date' or 'modified'.
     5257 * @param string $post_type Optional. The post type to check. Default 'any'.
    52525258 * @return string|false The timestamp.
    52535259 */
    5254 function _get_last_post_time( $timezone, $field ) {
     5260function _get_last_post_time( $timezone, $field, $post_type = 'any' ) {
    52555261        global $wpdb;
    52565262
    52575263        if ( !in_array( $field, array( 'date', 'modified' ) ) )
     
    52605266        $timezone = strtolower( $timezone );
    52615267
    52625268        $key = "lastpost{$field}:$timezone";
     5269        if ( 'any' != $post_type ) {
     5270                $key .= ':' . sanitize_key( $post_type );
     5271        }
    52635272
    52645273        $date = wp_cache_get( $key, 'timeinfo' );
    52655274
    52665275        if ( !$date ) {
    5267                 $add_seconds_server = date('Z');
     5276                if ( 'any' == $post_type ) {
     5277                        $post_types = get_post_types( array( 'public' => true ) );
     5278                        array_walk( $post_types, array( &$wpdb, 'escape_by_ref' ) );
     5279                        $post_types = "'" . implode( "', '", $post_types ) . "'";
     5280                } else {
     5281                        $post_types = "'" . sanitize_key( $post_type ) . "'";
     5282                }
    52685283
    5269                 $post_types = get_post_types( array( 'public' => true ) );
    5270                 array_walk( $post_types, array( &$wpdb, 'escape_by_ref' ) );
    5271                 $post_types = "'" . implode( "', '", $post_types ) . "'";
    5272 
    52735284                switch ( $timezone ) {
    52745285                        case 'gmt':
    52755286                                $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");
     
    52785289                                $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");
    52795290                                break;
    52805291                        case 'server':
     5292                                $add_seconds_server = date( 'Z' );
    52815293                                $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");
    52825294                                break;
    52835295                }
    52845296
    5285                 if ( $date )
     5297                if ( $date ) {
    52865298                        wp_cache_set( $key, $date, 'timeinfo' );
     5299                }
    52875300        }
    52885301
    52895302        return $date;
     
    55065519                foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) {
    55075520                        wp_cache_delete( "lastpostmodified:$timezone", 'timeinfo' );
    55085521                        wp_cache_delete( "lastpostdate:$timezone", 'timeinfo' );
     5522                        wp_cache_delete( "lastpostdate:$timezone:{$post->post_type}", 'timeinfo' );
    55095523                }
    55105524        }
    55115525