Improve testability and coverage of wp_ext2type().

* Following pattern of `wp_get_mime_types()`, introduce `wp_get_ext_types()` function. New function returns a filtered list of file types with their extensions.
* Use this function in new tests for `wp_ext2type()`.

Props borgesbruno.
Fixes #35987.
Built from https://develop.svn.wordpress.org/trunk@37189


git-svn-id: http://core.svn.wordpress.org/trunk@37155 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Boone Gorges 2016-04-13 03:12:27 +00:00
parent 7e40b1d282
commit 0b772e928c
2 changed files with 35 additions and 23 deletions

View File

@ -2185,28 +2185,7 @@ function wp_upload_bits( $name, $deprecated, $bits, $time = null ) {
function wp_ext2type( $ext ) {
$ext = strtolower( $ext );
/**
* Filter file type based on the extension name.
*
* @since 2.5.0
*
* @see wp_ext2type()
*
* @param array $ext2type Multi-dimensional array with extensions for a default set
* of file types.
*/
$ext2type = apply_filters( 'ext2type', array(
'image' => array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico' ),
'audio' => array( 'aac', 'ac3', 'aif', 'aiff', 'm3a', 'm4a', 'm4b', 'mka', 'mp1', 'mp2', 'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ),
'video' => array( '3g2', '3gp', '3gpp', 'asf', 'avi', 'divx', 'dv', 'flv', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt', 'rm', 'vob', 'wmv' ),
'document' => array( 'doc', 'docx', 'docm', 'dotm', 'odt', 'pages', 'pdf', 'xps', 'oxps', 'rtf', 'wp', 'wpd', 'psd', 'xcf' ),
'spreadsheet' => array( 'numbers', 'ods', 'xls', 'xlsx', 'xlsm', 'xlsb' ),
'interactive' => array( 'swf', 'key', 'ppt', 'pptx', 'pptm', 'pps', 'ppsx', 'ppsm', 'sldx', 'sldm', 'odp' ),
'text' => array( 'asc', 'csv', 'tsv', 'txt' ),
'archive' => array( 'bz2', 'cab', 'dmg', 'gz', 'rar', 'sea', 'sit', 'sqx', 'tar', 'tgz', 'zip', '7z' ),
'code' => array( 'css', 'htm', 'html', 'php', 'js' ),
) );
$ext2type = wp_get_ext_types();
foreach ( $ext2type as $type => $exts )
if ( in_array( $ext, $exts ) )
return $type;
@ -2451,6 +2430,39 @@ function wp_get_mime_types() {
'pages' => 'application/vnd.apple.pages',
) );
}
/**
* Retrieve list of common file extensions and their types.
*
* @since 4.6.0
*
* @return array Array of file extensions types keyed by the type of file.
*/
function wp_get_ext_types() {
/**
* Filter file type based on the extension name.
*
* @since 2.5.0
*
* @see wp_ext2type()
*
* @param array $ext2type Multi-dimensional array with extensions for a default set
* of file types.
*/
return apply_filters( 'ext2type', array(
'image' => array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico' ),
'audio' => array( 'aac', 'ac3', 'aif', 'aiff', 'm3a', 'm4a', 'm4b', 'mka', 'mp1', 'mp2', 'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ),
'video' => array( '3g2', '3gp', '3gpp', 'asf', 'avi', 'divx', 'dv', 'flv', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt', 'rm', 'vob', 'wmv' ),
'document' => array( 'doc', 'docx', 'docm', 'dotm', 'odt', 'pages', 'pdf', 'xps', 'oxps', 'rtf', 'wp', 'wpd', 'psd', 'xcf' ),
'spreadsheet' => array( 'numbers', 'ods', 'xls', 'xlsx', 'xlsm', 'xlsb' ),
'interactive' => array( 'swf', 'key', 'ppt', 'pptx', 'pptm', 'pps', 'ppsx', 'ppsm', 'sldx', 'sldm', 'odp' ),
'text' => array( 'asc', 'csv', 'tsv', 'txt' ),
'archive' => array( 'bz2', 'cab', 'dmg', 'gz', 'rar', 'sea', 'sit', 'sqx', 'tar', 'tgz', 'zip', '7z' ),
'code' => array( 'css', 'htm', 'html', 'php', 'js' ),
) );
}
/**
* Retrieve list of allowed mime types and file extensions.
*

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.6-alpha-37188';
$wp_version = '4.6-alpha-37189';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.