| 1 | function wp_match_mime_types($wildcard_mime_types, $real_mime_types) { |
|---|
| 2 | $matches = array(); |
|---|
| 3 | if ( is_string($wildcard_mime_types) ) |
|---|
| 4 | $wildcard_mime_types = array_map('trim', explode(',', $wildcard_mime_types)); |
|---|
| 5 | if ( is_string($real_mime_types) ) |
|---|
| 6 | $real_mime_types = array_map('trim', explode(',', $real_mime_types)); |
|---|
| 7 | |
|---|
| 8 | $combined_types = array(); |
|---|
| 9 | $temp_types = array(); |
|---|
| 10 | foreach ( (array) $wildcard_mime_types as $k => $type ) { |
|---|
| 11 | if(false !== strpos($type, ',')) { |
|---|
| 12 | $subtypes = array_map('trim', explode(',', $type)); |
|---|
| 13 | foreach($subtypes as $subtype) { |
|---|
| 14 | $temp_types[] = $subtype; |
|---|
| 15 | $combined_types[$subtype] = $type; |
|---|
| 16 | } |
|---|
| 17 | } else { |
|---|
| 18 | $combined_types[$type] = $type; |
|---|
| 19 | } |
|---|
| 20 | } |
|---|
| 21 | $wildcard_mime_types = array_merge($wildcard_mime_types, $temp_types); |
|---|
| 22 | |
|---|
| 23 | $wild = '[-._a-z0-9]*'; |
|---|
| 24 | foreach ( (array) $wildcard_mime_types as $type ) { |
|---|
| 25 | $type = str_replace('*', $wild, $type); |
|---|
| 26 | $patternses[1][$type] = "^$type$"; |
|---|
| 27 | if ( false === strpos($type, '/') ) { |
|---|
| 28 | $patternses[2][$type] = "^$type/"; |
|---|
| 29 | $patternses[3][$type] = $type; |
|---|
| 30 | } |
|---|
| 31 | } |
|---|
| 32 | asort($patternses); |
|---|
| 33 | foreach ( $patternses as $patterns ) |
|---|
| 34 | foreach ( $patterns as $type => $pattern ) |
|---|
| 35 | foreach ( (array) $real_mime_types as $real ) |
|---|
| 36 | if ( preg_match("#$pattern#", $real) && ( empty($matches[$type]) || false === array_search($real, $matches[$type]) ) ) |
|---|
| 37 | $matches[$combined_types[$type]][] = $real; |
|---|
| 38 | return $matches; |
|---|
| 39 | } |
|---|