diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 2f7cba613f0ed..993056ff2c032 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -8094,10 +8094,11 @@ function get_available_post_mime_types( $type = 'attachment' ) { $mime_types = apply_filters( 'pre_get_available_post_mime_types', null, $type ); if ( ! is_array( $mime_types ) ) { - $mime_types = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type ) ); + $mime_types = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s AND post_mime_type != ''", $type ) ); } - return $mime_types; + // Remove nulls from returned $mime_types. + return array_values( array_filter( $mime_types ) ); } /** diff --git a/tests/phpunit/tests/post/getAvailablePostMimeTypes.php b/tests/phpunit/tests/post/getAvailablePostMimeTypes.php new file mode 100644 index 0000000000000..cf6f46b920324 --- /dev/null +++ b/tests/phpunit/tests/post/getAvailablePostMimeTypes.php @@ -0,0 +1,58 @@ +remove_added_uploads(); + remove_filter( 'pre_get_available_post_mime_types', array( $this, 'filter_add_null_to_post_mime_types' ) ); + parent::tear_down(); + } + + public function test_should_return_expected_post_mime_types() { + // Upload a JPEG image. + $filename = DIR_TESTDATA . '/images/test-image.jpg'; + $contents = file_get_contents( $filename ); + $upload = wp_upload_bits( wp_basename( $filename ), null, $contents ); + $this->assertEmpty( $upload['error'], 'Uploading a JPEG file should not result in an error.' ); + $this->_make_attachment( $upload ); + + // Upload a PDF file. + $filename = DIR_TESTDATA . '/images/test-alpha.pdf'; + $contents = file_get_contents( $filename ); + $upload = wp_upload_bits( wp_basename( $filename ), null, $contents ); + $this->assertEmpty( $upload['error'], 'Uploading a PDF file should not result in an error.' ); + $this->_make_attachment( $upload ); + + $mime_types = get_available_post_mime_types(); + + $this->assertSame( array( 'image/jpeg', 'application/pdf' ), $mime_types, 'The MIME types returned should match the uploaded file MIME types.' ); + } + + public function test_should_remove_null() { + // Add filter to inject null into the mime types array. + add_filter( 'pre_get_available_post_mime_types', array( $this, 'filter_add_null_to_post_mime_types' ) ); + + $mime_types = get_available_post_mime_types(); + $this->assertEqualsCanonicalizing( array( 'image/jpeg', 'image/png' ), $mime_types ); + } + + /** + * Filter to inject null into the mime types array. + * + * @param string $type Post type. + * @return array + */ + public function filter_add_null_to_post_mime_types( $type ) { + return array( 'image/jpeg', null, 'image/png' ); + } +}