diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 019086c3b8..f56318392f 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -3576,10 +3576,18 @@ function wp_enqueue_media( $args = array() ) { $month_year->text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month_year->month ), $month_year->year ); } + // Filter to show only available mime types. + $avail_post_mime_types = get_available_post_mime_types( 'attachment' ); + $mimeTypes = wp_list_pluck( get_post_mime_types(), 0 ); + foreach ( $mimeTypes as $mime_type => $label ) { + if ( ! wp_match_mime_types( $mime_type, $avail_post_mime_types ) ) { + unset( $mimeTypes[ $mime_type ] ); + } + } $settings = array( 'tabs' => $tabs, 'tabUrl' => add_query_arg( array( 'chromeless' => true ), admin_url( 'media-upload.php' ) ), - 'mimeTypes' => wp_list_pluck( get_post_mime_types(), 0 ), + 'mimeTypes' => $mimeTypes, /** This filter is documented in wp-admin/includes/media.php */ 'captions' => ! apply_filters( 'disable_captions', '' ), 'nonce' => array( diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php index 88b9a46475..7c1c97ce8a 100644 --- a/tests/phpunit/tests/functions.php +++ b/tests/phpunit/tests/functions.php @@ -465,6 +465,53 @@ class Tests_Functions extends WP_UnitTestCase { $this->assertNotEmpty( $mimes ); } + /** + * Test that the media grid uses the correct available single media type. + * @ticket 43658 + */ + function test_wp_enqueue_media_single_mime_type() { + $filename = DIR_TESTDATA . '/images/test-image.jpg'; + $contents = file_get_contents( $filename ); + $upload = wp_upload_bits( basename( $filename ), null, $contents ); + $attachment_id = $this->_make_attachment( $upload ); + + add_filter( + 'media_view_settings', + function( $settings ) { + $this->assertEquals( array( 'image' ), array_keys( $settings['mimeTypes'] ) ); + return $settings; + } + ); + wp_enqueue_media(); + remove_all_filters( 'media_view_settings' ); + } + + /** + * Test that the media grid uses the correct available multiple media types. + * @ticket 43658 + */ + function test_wp_enqueue_media_multiple_mime_types() { + $filename = DIR_TESTDATA . '/images/test-image.jpg'; + $contents = file_get_contents( $filename ); + $upload = wp_upload_bits( basename( $filename ), null, $contents ); + $attachment_id = $this->_make_attachment( $upload ); + + $filename = DIR_TESTDATA . '/uploads/small-audio.mp3'; + $contents = file_get_contents( $filename ); + $upload = wp_upload_bits( basename( $filename ), null, $contents ); + $attachment_id = $this->_make_attachment( $upload ); + + add_filter( + 'media_view_settings', + function( $settings ) { + $this->assertEquals( array( 'image', 'audio' ), array_keys( $settings['mimeTypes'] ) ); + return $settings; + } + ); + wp_enqueue_media(); + remove_all_filters( 'media_view_settings' ); + } + /** * @ticket 21594 */