Make WordPress Core

Ticket #27985: 27985.diff

File 27985.diff, 2.3 KB (added by pento, 10 years ago)
  • src/wp-includes/post.php

     
    22712271function wp_count_attachments( $mime_type = '' ) {
    22722272        global $wpdb;
    22732273
    2274         $and = wp_post_mime_type_where( $mime_type );
    2275         $count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' $and GROUP BY post_mime_type", ARRAY_A );
     2274        if ( empty( $mime_type ) ) {
     2275                $key = 'default';
     2276        } else {
     2277                if ( is_array( $mime_type ) ) {
     2278                        $key = implode( ',', $mime_type );
     2279                } else {
     2280                        $key = $mime_type;
     2281                }
     2282        }
    22762283
    2277         $counts = array();
    2278         foreach( (array) $count as $row ) {
    2279                 $counts[ $row['post_mime_type'] ] = $row['num_posts'];
     2284        $counts_cache = wp_cache_get( 'wp_count_attachments' );
     2285
     2286        if ( false !== $counts_cache && ! empty( $counts_cache[ $key ] ) ) {
     2287                $counts = $counts_cache[ $key ];
     2288        } else {
     2289                $and = wp_post_mime_type_where( $mime_type );
     2290                $count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' $and GROUP BY post_mime_type", ARRAY_A );
     2291
     2292                $counts = array();
     2293                foreach( (array) $count as $row ) {
     2294                        $counts[ $row['post_mime_type'] ] = $row['num_posts'];
     2295                }
     2296                $counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and");
     2297
     2298                if ( ! is_array( $counts_cache ) ) {
     2299                        $counts_cache = array();
     2300                }
     2301                $counts_cache[ $key ] = $counts;
     2302
     2303                wp_cache_set( 'wp_count_attachments', $counts_cache );
    22802304        }
    2281         $counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and");
    22822305
    22832306        /**
    22842307         * Modify returned attachment counts by mime type.
     
    22922315}
    22932316
    22942317/**
     2318 * Helper function to clear the cache for number of attachments.
     2319 *
     2320 * @private
     2321 */
     2322function __clear_count_attachments_cache( $new_status, $old_status, $post ) {
     2323        if ( 'attachment' === $post->post_type ) {
     2324                wp_cache_delete( 'wp_count_attachments' );
     2325        }
     2326}
     2327add_action( 'transition_post_status', '__clear_count_attachments_cache', 1, 3 );
     2328
     2329/**
    22952330 * Get default post mime types
    22962331 *
    22972332 * @since 2.9.0