Make WordPress Core


Ignore:
Timestamp:
12/13/2005 07:19:56 PM (21 years ago)
Author:
ryan
Message:

Attachment enhancements from skeltoac. fixes #2074

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/template-functions-post.php

    r3229 r3303  
    440440}
    441441
    442 function prepend_attachment($content) {
    443         global $post;
    444 
    445         $p = '<p class="attachment">';
    446 
    447         if ( '' != $post->guid ) {
    448                 if ( substr($post->post_mime_type, 0, 6) == 'image/' )
    449                         $p .= '<a href="' . $post->guid . '" title="Click for full-size image" ><img class="attachmentimage" src="' . $post->guid . '" alt="' . $post->post_title . '" /></a>';
    450                 else
    451                         $p .= __('Attachment') . ' (' . $post->post_mime_type . ')';
     442function the_attachment_link($id = 0, $fullsize = false, $max_dims = false) {
     443        echo get_the_attachment_link($id, $fullsize, $max_dims);
     444}
     445
     446function get_the_attachment_link($id = 0, $fullsize = false, $max_dims = false) {
     447        $id = (int) $id;
     448        $_post = & get_post($id);
     449
     450        if ( ('attachment' != $_post->post_status) || ('' == $_post->guid) )
     451                return __('Missing Attachment');
     452
     453        if (! empty($_post->guid) ) {
     454                $innerHTML = get_attachment_innerHTML($_post->ID, $fullsize, $max_dims);
     455
     456                return "<a href=\"{$_post->guid}\" title=\"{$_post->post_title}\" >{$innerHTML}</a>";
     457
    452458        } else {
    453459                $p .= __('Missing attachment');
    454460        }
    455 
     461        return $p;
     462}
     463
     464function get_attachment_icon($id = 0, $fullsize = false, $max_dims = false) {
     465        $id = (int) $id;
     466        $post = & get_post($id);
     467
     468        $mime = $post->post_mime_type;
     469
     470        $imagedata = get_post_meta($post->ID, '_wp_attachment_metadata', true);
     471
     472        $file = get_post_meta($post->ID, '_wp_attached_file', true);
     473
     474        if ( !$fullsize && !empty($imagedata['thumb'])
     475                        && ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file))
     476                        && file_exists($thumbfile) ) {
     477
     478                // We have a thumbnail desired, specified and existing
     479
     480                $src = str_replace(basename($post->guid), $imagedata['thumb'], $post->guid);
     481                $src_file = $thumbfile;
     482                $class = 'attachmentthumb';
     483
     484        } elseif ( substr($mime, 0, 6) == 'image/'
     485                        && file_exists($file) ) {
     486
     487                // We have an image without a thumbnail
     488
     489                $src = $post->guid;
     490                $src_file = & $file;
     491                $class = 'attachmentimage';
     492        } elseif (! empty($mime) ) {
     493
     494                // No thumb, no image. We'll look for a mime-related icon instead.
     495                $icon_dir = apply_filters('icon_dir', get_template_directory().'/images');
     496                $icon_dir_uri = apply_filters('icon_dir_uri', get_template_directory_uri().'/images');
     497
     498                $types = array(substr($mime, 0, strpos($mime, '/')), substr($mime, strpos($mime, '/') + 1), str_replace('/', '_', $mime));
     499                $exts = array('jpg', 'gif', 'png');
     500                foreach ($types as $type) {
     501                        foreach ($exts as $ext) {
     502                                $src_file = "$icon_dir/$type.$ext";
     503                                if ( file_exists($src_file) ) {
     504                                        $src = "$icon_dir_uri/$type.$ext";
     505                                        break 2;
     506                                }
     507                        }
     508                }
     509        }
     510
     511        if (! isset($src) )
     512                return false;
     513
     514        // Do we need to constrain the image?
     515        if ( ($max_dims = apply_filters('attachment_max_dims', $max_dims)) && file_exists($src_file) ) {
     516
     517                $imagesize = getimagesize($src_file);
     518
     519                if (($imagesize[0] > $max_dims[0]) || $imagesize[1] > $max_dims[1] ) {
     520                        $actual_aspect = $imagesize[0] / $imagesize[1];
     521                        $desired_aspect = $max_dims[0] / $max_dims[1];
     522
     523                        if ( $actual_aspect >= $desired_aspect ) {
     524                                $height = $actual_aspect * $max_dims[0];
     525                                $constraint = "width=\"{$max_dims[0]}\" ";
     526                                $post->iconsize = array($max_dims[0], $height);
     527                        } else {
     528                                $width = $max_dims[1] / $actual_aspect;
     529                                $constraint = "height=\"{$max_dims[1]}\" ";
     530                                $post->iconsize = array($width, $max_dims[1]);
     531                        }
     532                } else {
     533                        $post->iconsize = array($imagesize[0], $imagesize[1]);
     534                }
     535        }
     536
     537        $icon = "<img src=\"{$src}\" title=\"{$post->post_title}\" {$constraint}/>";
     538
     539        return apply_filters('attachment_icon', $icon, $post->ID);
     540}
     541
     542function get_attachment_innerHTML($id = 0, $fullsize = false, $max_dims = false) {
     543        $id = (int) $id;
     544
     545        if ( $innerHTML = get_attachment_icon($id, $fullsize, $max_dims))
     546                return $innerHTML;
     547
     548        $post = & get_post($id);
     549
     550        $innerHTML = $post->post_title;
     551
     552        return apply_filters('attachment_innerHTML', $innerHTML, $post->ID);
     553}
     554
     555function prepend_attachment($content) {
     556        $p = '<p class="attachment">';
     557        $p .= get_the_attachment_link(false, true, array(400, 300));
    456558        $p .= '</p>';
     559        $p = apply_filters('prepend_attachment', $p);
    457560
    458561        return "$p\n$content";
    459562}
     563
    460564?>
Note: See TracChangeset for help on using the changeset viewer.