Make WordPress Core

Changeset 7036


Ignore:
Timestamp:
02/26/2008 08:54:11 AM (17 years ago)
Author:
ryan
Message:

update mime-to-icon matching and get filename from function instead of guid. Props andy. see #5911

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/edit-attachment-rows.php

    r6995 r7036  
    4848        ?>
    4949        <td><strong><a href="# TODO: upload.php?action=edit&amp;post=<?php the_ID(); ?>"><?php the_title(); ?></a></strong><br />
    50         <?php echo strtoupper(preg_replace('/^.*?\.(\w+)$/', '$1', $post->guid)); ?>
     50        <?php echo strtoupper(preg_replace('/^.*?\.(\w+)$/', '$1', get_attached_file($post->ID))); ?>
    5151        <?php do_action('manage_media_media_column', $post->ID); ?>
    5252        </td>
  • trunk/wp-includes/post.php

    r6985 r7036  
    23352335 */
    23362336function wp_mime_type_icon( $mime = 0 ) {
    2337     $post_id = 0;
    2338 
    2339     $icon = wp_cache_get('mime_type_icon');
    2340    
     2337    if ( !is_numeric($mime) )
     2338        $icon = wp_cache_get("mime_type_icon_$mime");
     2339
    23412340    if ( empty($icon) ) {
     2341        $post_id = 0;
     2342        $post_mimes = array();
    23422343        if ( is_numeric($mime) ) {
    23432344            $mime = (int) $mime;
    2344             if ( !$post =& get_post( $mime ) )
    2345                 return false;
    2346             $post_id = (int) $post->ID;
    2347             $mime = $post->post_mime_type;
    2348             $ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $post->guid);
    2349         }
    2350    
    2351         $types = array();
    2352    
    2353         if ( !empty($ext) )
    2354             $types[] = $ext;
    2355    
    2356         $icon_dir = apply_filters( 'icon_dir', get_template_directory() . '/images' );
    2357         $icon_dir_uri = apply_filters( 'icon_dir_uri', get_template_directory_uri() . '/images' );
    2358         $image_dir = apply_filters( 'image_dir', ABSPATH . WPINC . '/images' );
    2359         $image_dir_uri = apply_filters( 'image_dir', get_option('siteurl') . '/' . WPINC . '/images' );
    2360         $dirs = array($icon_dir => $icon_dir_uri, $image_dir => $image_dir_uri);
    2361    
    2362    
     2345            if ( $post =& get_post( $mime ) ) {
     2346                $post_id = (int) $post->ID;
     2347                $ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $post->guid);
     2348                if ( !empty($ext) )
     2349                    $post_mimes[] = $ext;
     2350                $mime = $post->post_mime_type;
     2351            } else {
     2352                $mime = 0;
     2353            }
     2354        } else {
     2355            $post_mimes[] = $mime;
     2356        }
     2357
     2358        $icon_files = wp_cache_get('icon_files');
     2359
     2360        if ( !is_array($icon_files) ) {
     2361            $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images' );
     2362            $icon_dir_uri = apply_filters( 'icon_dir_uri', trailingslashit(get_option('siteurl')) . WPINC . '/images' );
     2363            $dirs = apply_filters( 'icon_dirs', array($icon_dir => $icon_dir_uri) );
     2364            $icon_files = array();
     2365            foreach ( $dirs as $dir => $uri) {
     2366                if ( $dh = opendir($dir) ) {
     2367                    while ( false !== $file = readdir($dh) ) {
     2368                        $file = basename($file);
     2369                        if ( !in_array(strtolower(substr($file, -4)), array('.png', '.gif', '.jpg') ) ) {
     2370                            if ( is_dir($file) )
     2371                                $dirs["$dir/$file"] = "$uri/$file";
     2372                            continue;
     2373                        }
     2374                        $icon_files["$dir/$file"] = "$uri/$file";
     2375                    }
     2376                    closedir($dh);
     2377                }
     2378            }
     2379            wp_cache_set('icon_files', $icon_files, 600);
     2380        }
     2381
     2382        // Icon basename - extension = MIME wildcard
     2383        foreach ( $icon_files as $file => $uri )
     2384            $types[ preg_replace('/^([^.]*).*$/', '$1', basename($file)) ] =& $icon_files[$file];
     2385
    23632386        if ( ! empty($mime) ) {
    2364             $types[] = substr($mime, 0, strpos($mime, '/'));
    2365             $types[] = substr($mime, strpos($mime, '/') + 1);
    2366             $types[] = str_replace('/', '_', $mime);
    2367         }
    2368    
    2369         $types[] = 'default';
    2370    
    2371         $exts = array('png', 'gif', 'jpg');
    2372 
    2373         foreach ( $types as $type ) {
    2374             foreach ( $exts as $ext ) {
    2375                 foreach ( $dirs as $dir => $uri ) {
    2376                     $src_file = "$dir/$type.$ext";
    2377                     if ( file_exists($src_file) ) {
    2378                         $icon = "$uri/$type.$ext";
    2379                         break 3;
    2380                     }
    2381                 }
     2387            $post_mimes[] = substr($mime, 0, strpos($mime, '/'));
     2388            $post_mimes[] = substr($mime, strpos($mime, '/') + 1);
     2389            $post_mimes[] = str_replace('/', '_', $mime);
     2390        }
     2391
     2392        $post_mimes[] = 'default';
     2393
     2394        $matches = wp_match_mime_types(array_keys($types), $post_mimes);
     2395
     2396        foreach ( $matches as $match => $wilds ) {
     2397            if ( isset($types[$wilds[0]])) {
     2398                $icon = $types[$wilds[0]];
     2399                if ( !is_numeric($mime) )
     2400                    wp_cache_set("mime_type_icon_$mime", $icon);
     2401                break;
    23822402            }
    23832403        }
Note: See TracChangeset for help on using the changeset viewer.