Make WordPress Core

Ticket #27985: 27985.2.diff

File 27985.2.diff, 2.9 KB (added by wonderboymusic, 10 years ago)
  • src/wp-includes/media.php

     
    26952695                }
    26962696        }
    26972697
    2698         $audio = $video = 0;
    2699         $counts = (array) wp_count_attachments();
    2700         foreach ( $counts as $mime => $total ) {
    2701                 if ( 0 === strpos( $mime, 'audio/' ) ) {
    2702                         $audio += (int) $total;
    2703                 } elseif ( 0 === strpos( $mime, 'video/' ) ) {
    2704                         $video += (int) $total;
    2705                 }
    2706         }
    2707 
    27082698        $settings = array(
    27092699                'tabs'      => $tabs,
    27102700                'tabUrl'    => add_query_arg( array( 'chromeless' => true ), admin_url('media-upload.php') ),
     
    27192709                ),
    27202710                'defaultProps' => $props,
    27212711                'attachmentCounts' => array(
    2722                         'audio' => $audio,
    2723                         'video' => $video
     2712                        'audio' => wp_has_mime_type_attachments( 'audio' ) ? 1 : 0,
     2713                        'video' => wp_has_mime_type_attachments( 'video' ) ? 1 : 0
    27242714                ),
    27252715                'embedExts'    => $exts,
    27262716                'embedMimes'   => $ext_mimes,
  • src/wp-includes/post.php

     
    22922292}
    22932293
    22942294/**
     2295 * Determine if at least one attachment of a particular mime-type has been uploaded
     2296 *
     2297 * @global wpdb $wpdb
     2298 *
     2299 * @since 3.9.1
     2300 *
     2301 * @param string $mime_type The mime-type string to check.
     2302 *
     2303 * @return int|null If exist, the post ID of the first found attachment.
     2304 */
     2305function wp_has_mime_type_attachments( $mime_type ) {
     2306        global $wpdb;
     2307        $sql = sprintf( "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_mime_type LIKE '%s%%' LIMIT 1", like_escape( $mime_type ) );
     2308        return $wpdb->get_var( $sql );
     2309}
     2310
     2311/**
    22952312 * Get default post mime types
    22962313 *
    22972314 * @since 2.9.0
  • tests/phpunit/tests/attachment/count.php

     
     1<?php
     2/**
     3 * @group attachment
     4 */
     5class Tests_Attachment_Counts extends WP_UnitTestCase {
     6
     7        function test_wp_has_mime_type_attachments() {
     8                $has = wp_has_mime_type_attachments( 'image' );
     9                $this->assertEmpty( $has );
     10
     11                $this->factory->attachment->create_object( 'whatever/image.jpg', 0, array( 'post_mime_type' => 'image/jpeg' ) );
     12                $has = wp_has_mime_type_attachments( 'image' );
     13                $this->assertNotEmpty( $has );
     14                $has = wp_has_mime_type_attachments( 'image/jpeg' );
     15                $this->assertNotEmpty( $has );
     16                $has = wp_has_mime_type_attachments( 'audio' );
     17                $this->assertEmpty( $has );
     18
     19                $this->factory->attachment->create_object( 'whatever/audio.mp3', 0, array( 'post_mime_type' => 'audio/mpeg' ) );
     20                $has = wp_has_mime_type_attachments( 'audio' );
     21                $this->assertNotEmpty( $has );
     22                $has = wp_has_mime_type_attachments( 'image' );
     23                $this->assertNotEmpty( $has );
     24        }
     25}
     26 No newline at end of file