'image', 'video/' => 'video', 'audio/' => 'audio', 'text/csv' => 'sheet', 'text/html' => 'code', 'text/' => 'text', ); foreach ( $prefixes as $prefix => $type ) { if ( strpos( $mime_type, $prefix ) === 0 ) { return $type; } } $needles = array( 'pdf' => 'pdf', 'wordprocessing' => 'doc', 'msword' => 'doc', 'opendocument.text' => 'doc', 'rtf' => 'doc', 'spreadsheet' => 'sheet', 'ms-excel' => 'sheet', 'presentation' => 'slides', 'ms-powerpoint' => 'slides', 'zip' => 'archive', 'compressed' => 'archive', 'tar' => 'archive', 'gzip' => 'archive', 'json' => 'code', 'xml' => 'code', 'javascript' => 'code', ); foreach ( $needles as $needle => $type ) { if ( strpos( $mime_type, $needle ) !== false ) { return $type; } } return 'default'; } /** * Inline SVG icon for a file family. * * Inline rather than a font, sprite or CSS mask: crisp at any pixel density, * coloured by the surrounding text through currentColor, no extra request, * and nothing a theme has to support. * * Every family has its own shape, so the icons still tell the types apart * when they are shown in a single colour. * * @param string $type File family, from wpatt_get_file_type(). * @param string $class CSS class for the . * @return string */ function wpatt_get_file_icon_svg( $type, $class = 'wpa-file-icon' ) { // Sheet of paper with a folded corner, shared by the document families. $page = '' . '' . ''; // Rounded frame shared by the media families. $frame = '' . ''; $glyphs = array( // Solid label band: tells a PDF from a text document without colour. 'pdf' => $page . '', 'doc' => $page . '', 'text' => $page . '', 'sheet' => $page . '' . '', 'slides' => $page . '' . '', 'code' => $page . '', 'image' => $frame . '' . '', 'video' => $frame . '', 'audio' => '' . '' . '', 'archive' => '' . '' . '' . '', 'default' => $page, ); $glyph = isset( $glyphs[ $type ] ) ? $glyphs[ $type ] : $glyphs['default']; // width/height attributes keep the icon small even where a theme's CSS // does not reach it (or resets svg sizing). return ''; } /** * File extension of an attachment, upper case, from its URL. * * @param int $attachment_id Attachment ID. * @return string */ function wpatt_get_file_extension( $attachment_id ) { $url = wp_get_attachment_url( $attachment_id ); // parse_url() first: a query string would otherwise end up in the extension. $path = $url ? wp_parse_url( $url, PHP_URL_PATH ) : ''; return $path ? strtoupper( pathinfo( $path, PATHINFO_EXTENSION ) ) : ''; }