Ticket #27985: 27985.diff
File 27985.diff, 2.3 KB (added by , 9 years ago) |
---|
-
src/wp-includes/post.php
2271 2271 function wp_count_attachments( $mime_type = '' ) { 2272 2272 global $wpdb; 2273 2273 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 } 2276 2283 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 ); 2280 2304 } 2281 $counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and");2282 2305 2283 2306 /** 2284 2307 * Modify returned attachment counts by mime type. … … 2292 2315 } 2293 2316 2294 2317 /** 2318 * Helper function to clear the cache for number of attachments. 2319 * 2320 * @private 2321 */ 2322 function __clear_count_attachments_cache( $new_status, $old_status, $post ) { 2323 if ( 'attachment' === $post->post_type ) { 2324 wp_cache_delete( 'wp_count_attachments' ); 2325 } 2326 } 2327 add_action( 'transition_post_status', '__clear_count_attachments_cache', 1, 3 ); 2328 2329 /** 2295 2330 * Get default post mime types 2296 2331 * 2297 2332 * @since 2.9.0