From 34b0686be1c0d9082696681f3bdf44f802ed139a Mon Sep 17 00:00:00 2001 From: johnbillion Date: Wed, 23 Apr 2014 22:04:03 +0000 Subject: [PATCH] Avoid an expensive attachment counting query on the post editing screen. See #27985, for trunk. git-svn-id: https://develop.svn.wordpress.org/trunk@28191 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/media.php | 14 ++------------ src/wp-includes/post.php | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index fe5b63b8ff..b65529e2e3 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -2695,16 +2695,6 @@ 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; - } - } - $settings = array( 'tabs' => $tabs, 'tabUrl' => add_query_arg( array( 'chromeless' => true ), admin_url('media-upload.php') ), @@ -2719,8 +2709,8 @@ function wp_enqueue_media( $args = array() ) { ), 'defaultProps' => $props, 'attachmentCounts' => array( - 'audio' => $audio, - 'video' => $video + 'audio' => wp_has_mime_type_attachments( 'audio' ) ? 1 : 0, + 'video' => wp_has_mime_type_attachments( 'video' ) ? 1 : 0 ), 'embedExts' => $exts, 'embedMimes' => $ext_mimes, diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index d537148d29..8da9437251 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -2291,6 +2291,23 @@ function wp_count_attachments( $mime_type = '' ) { return apply_filters( 'wp_count_attachments', (object) $counts, $mime_type ); } +/** + * Determine if at least one attachment of a particular mime-type has been uploaded + * + * @global wpdb $wpdb + * + * @since 3.9.1 + * + * @param string $mime_type The mime-type string to check. + * + * @return int|null If exist, the post ID of the first found attachment. + */ +function wp_has_mime_type_attachments( $mime_type ) { + global $wpdb; + $sql = sprintf( "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_mime_type LIKE '%s%%' LIMIT 1", like_escape( $mime_type ) ); + return $wpdb->get_var( $sql ); +} + /** * Get default post mime types *