From f2512c50eb3802c86691835c087dddb9976987a9 Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Tue, 6 May 2014 03:22:17 +0000 Subject: [PATCH] Avoid an expensive attachment counting query on the post editing screen. Merges [28191], [28194] to the 3.9 branch. props johnbillion. fixes #27985. Built from https://develop.svn.wordpress.org/branches/3.9@28261 git-svn-id: http://core.svn.wordpress.org/branches/3.9@28089 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/media.php | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/wp-includes/media.php b/wp-includes/media.php index e9b4a1da83..84ed4d587f 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -2654,7 +2654,7 @@ function wp_enqueue_media( $args = array() ) { if ( did_action( 'wp_enqueue_media' ) ) return; - global $content_width; + global $content_width, $wpdb; $defaults = array( 'post' => null, @@ -2693,15 +2693,20 @@ function wp_enqueue_media( $args = array() ) { } } - $audio = $video = 0; - $counts = (array) wp_count_attachments(); - foreach ( $counts as $mime => $total ) { - if ( 0 === strpos( $mime, 'audio/' ) ) { - $audio += (int) $total; - } elseif ( 0 === strpos( $mime, 'video/' ) ) { - $video += (int) $total; - } - } + $has_audio = $wpdb->get_var( " + SELECT ID + FROM $wpdb->posts + WHERE post_type = 'attachment' + AND post_mime_type LIKE 'audio%' + LIMIT 1 + " ); + $has_video = $wpdb->get_var( " + SELECT ID + FROM $wpdb->posts + WHERE post_type = 'attachment' + AND post_mime_type LIKE 'video%' + LIMIT 1 + " ); $settings = array( 'tabs' => $tabs, @@ -2717,8 +2722,8 @@ function wp_enqueue_media( $args = array() ) { ), 'defaultProps' => $props, 'attachmentCounts' => array( - 'audio' => $audio, - 'video' => $video + 'audio' => (int) $has_audio, + 'video' => (int) $has_video, ), 'embedExts' => $exts, 'embedMimes' => $ext_mimes,