WordPress.org

Plugin Directory

Changeset 556924


Ignore:
Timestamp:
06/12/12 20:41:57 (23 months ago)
Author:
greenshady
Message:
 
Location:
get-the-image/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • get-the-image/trunk/get-the-image.php

    r440355 r556924  
    22/** 
    33 * Plugin Name: Get The Image 
    4  * Plugin URI: http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin 
     4 * Plugin URI: http://themehybrid.com/plugins/get-the-image 
    55 * Description: This is a highly intuitive script that can grab an image by custom field input, post attachment, or extracting it from the post's content. 
    6  * Version: 0.7 
     6 * Version: 0.8 
    77 * Author: Justin Tadlock 
    88 * Author URI: http://justintadlock.com 
     
    2424 * 
    2525 * @package GetTheImage 
    26  * @version 0.7.0 
     26 * @version 0.8.0 
    2727 * @author Justin Tadlock <justin@justintadlock.com> 
    28  * @copyright Copyright (c) 2008 - 2011, Justin Tadlock 
     28 * @copyright Copyright (c) 2008 - 2012, Justin Tadlock 
    2929 * @link http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin 
    3030 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 
     
    4949 * 
    5050 * @since 0.1.0 
     51 * @access public 
    5152 * @global $post The current post's database object. 
    5253 * @param array $args Arguments for how to load and display the image. 
     
    5455 */ 
    5556function get_the_image( $args = array() ) { 
    56     global $post; 
    5757 
    5858    /* Set the default arguments. */ 
    5959    $defaults = array( 
    60         'meta_key' => array( 'Thumbnail', 'thumbnail' ), 
    61         'post_id' => $post->ID, 
    62         'attachment' => true, 
    63         'the_post_thumbnail' => true, // WP 2.9+ image function 
    64         'size' => 'thumbnail', 
    65         'default_image' => false, 
    66         'order_of_image' => 1, 
    67         'link_to_post' => true, 
    68         'image_class' => false, 
    69         'image_scan' => false, 
    70         'width' => false, 
    71         'height' => false, 
    72         'format' => 'img', 
    73         'meta_key_save' => false, 
    74         'callback' => null, 
    75         'cache' => true, 
    76         'echo' => true, 
    77         'custom_key' => null, // @deprecated 0.6. Use 'meta_key'. 
    78         'default_size' => null, // @deprecated 0.5.  Use 'size'. 
     60        'meta_key' =>       array( 'Thumbnail', 'thumbnail' ), // array|string 
     61        'post_id' =>        get_the_ID(), 
     62        'attachment' =>         true, 
     63        'the_post_thumbnail' =>     true, // WP 2.9+ image function 
     64        'size' =>           'thumbnail', 
     65        'default_image' =>  false, 
     66        'order_of_image' =>     1, 
     67        'link_to_post' =>       true, 
     68        'image_class' =>        false, 
     69        'image_scan' =>         false, 
     70        'width' =>      false, 
     71        'height' =>         false, 
     72        'format' =>         'img', 
     73        'meta_key_save' =>  false, 
     74        'thumbnail_id_save' =>  false, // Set 'featured image'. 
     75        'callback' =>       null, 
     76        'cache' =>      true, 
     77        'before' =>         '', 
     78        'after' =>      '', 
     79        'echo' =>       true, 
     80        'custom_key' =>         null, // @deprecated 0.6. Use 'meta_key'. 
     81        'default_size' =>       null, // @deprecated 0.5.  Use 'size'. 
    7982    ); 
    8083 
     
    109112        $image_cache = array(); 
    110113 
     114    /* Set up a default, empty $image_html variable. */ 
     115    $image_html = ''; 
     116 
    111117    /* If there is no cached image, let's see if one exists. */ 
    112118    if ( !isset( $image_cache[$key] ) || empty( $cache ) ) { 
     
    144150 
    145151            /* Format the image HTML. */ 
    146             $image = get_the_image_format( $args, $image ); 
     152            $image_html = get_the_image_format( $args, $image ); 
    147153 
    148154            /* Set the image cache for the specific post. */ 
    149             $image_cache[$key] = $image; 
     155            $image_cache[$key] = $image_html; 
    150156            wp_cache_set( $post_id, $image_cache, 'get_the_image' ); 
    151157        } 
     
    154160    /* If an image was already cached for the post and arguments, use it. */ 
    155161    else { 
    156         $image = $image_cache[$key]; 
     162        $image_html = $image_cache[$key]; 
    157163    } 
    158164 
    159165    /* Allow plugins/theme to override the final output. */ 
    160     $image = apply_filters( 'get_the_image', $image ); 
     166    $image_html = apply_filters( 'get_the_image', $image_html ); 
    161167 
    162168    /* If $format is set to 'array', return an array of image attributes. */ 
     
    167173 
    168174        /* Get the image attributes. */ 
    169         $atts = wp_kses_hair( $image, array( 'http' ) ); 
     175        $atts = wp_kses_hair( $image_html, array( 'http' ) ); 
    170176 
    171177        /* Loop through the image attributes and add them in key/value pairs for the return array. */ 
     
    181187    /* Or, if $echo is set to false, return the formatted image. */ 
    182188    elseif ( false === $echo ) { 
    183         return $image; 
    184     } 
     189        return $args['before'] . $image_html . $args['after']; 
     190    } 
     191 
     192    /* If there is a $post_thumbnail_id, do the actions associated with get_the_post_thumbnail(). */ 
     193    if ( isset( $image['post_thumbnail_id'] ) ) 
     194        do_action( 'begin_fetch_post_thumbnail_html', $post_id, $image['post_thumbnail_id'], $size ); 
    185195 
    186196    /* Display the image if we get to this point. */ 
    187     echo $image; 
     197    echo $args['before'] . $image_html . $args['after']; 
     198 
     199    /* If there is a $post_thumbnail_id, do the actions associated with get_the_post_thumbnail(). */ 
     200    if ( isset( $image['post_thumbnail_id'] ) ) 
     201        do_action( 'end_fetch_post_thumbnail_html', $post_id, $image['post_thumbnail_id'], $size ); 
    188202} 
    189203 
     
    195209 * 
    196210 * @since 0.7.0 
     211 * @access private 
    197212 * @param array $args Arguments for how to load and display the image. 
    198213 * @return array|bool Array of image attributes. | False if no image is found. 
     
    201216 
    202217    /* If $meta_key is not an array. */ 
    203     if ( !is_array( $args['meta_key'] ) ) { 
    204  
    205         /* Get the image URL by the single meta key. */ 
    206         $image = get_post_meta( $args['post_id'], $args['meta_key'], true ); 
    207     } 
    208  
    209     /* If $meta_key is an array. */ 
    210     elseif ( is_array( $args['meta_key'] ) ) { 
    211  
    212         /* Loop through each of the given meta keys. */ 
    213         foreach ( $args['meta_key'] as $meta_key ) { 
    214  
    215             /* Get the image URL by the current meta key in the loop. */ 
    216             $image = get_post_meta( $args['post_id'], $meta_key, true ); 
    217  
    218             /* If an image was found, break out of the loop. */ 
    219             if ( !empty( $image ) ) 
    220                 break; 
    221         } 
     218    if ( !is_array( $args['meta_key'] ) ) 
     219        $args['meta_key'] = array( $args['meta_key'] ); 
     220 
     221    /* Loop through each of the given meta keys. */ 
     222    foreach ( $args['meta_key'] as $meta_key ) { 
     223 
     224        /* Get the image URL by the current meta key in the loop. */ 
     225        $image = get_post_meta( $args['post_id'], $meta_key, true ); 
     226 
     227        /* If an image was found, break out of the loop. */ 
     228        if ( !empty( $image ) ) 
     229            break; 
    222230    } 
    223231 
     
    235243 * 
    236244 * @since 0.7.0 
     245 * @access private 
    237246 * @param array $args Arguments for how to load and display the image. 
    238247 * @return array|bool Array of image attributes. | False if no image is found. 
     
    265274 * 
    266275 * @since 0.7.0 
     276 * @access private 
    267277 * @param array $args Arguments for how to load and display the image. 
    268278 * @return array|bool Array of image attributes. | False if no image is found. 
     
    270280function get_the_image_by_attachment( $args = array() ) { 
    271281 
    272     /* Get attachments for the inputted $post_id. */ 
    273     $attachments = get_children( array( 'post_parent' => $args['post_id'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) ); 
    274  
    275     /* If no attachments are found, check if the post itself is an attachment and grab its image. */ 
    276     if ( empty( $attachments ) && $args['size'] ) { 
    277         if ( 'attachment' == get_post_type( $args['post_id'] ) ) { 
    278             $image = wp_get_attachment_image_src( $args['post_id'], $args['size'] ); 
    279             $alt = trim( strip_tags( get_post_field( 'post_excerpt', $args['post_id'] ) ) ); 
     282    /* Get the post type of the current post. */ 
     283    $post_type = get_post_type( $args['post_id'] ); 
     284 
     285    /* Check if the post itself is an image attachment. */ 
     286    if ( 'attachment' == $post_type && wp_attachment_is_image( $args['post_id'] ) ) { 
     287        $attachment_id = $args['post_id']; 
     288    } 
     289 
     290    /* If the post is not an attachment, check if it has any image attachments. */ 
     291    elseif ( 'attachment' !== $post_type ) { 
     292 
     293        /* Get attachments for the inputted $post_id. */ 
     294        $attachments = get_children( 
     295            array( 
     296                'post_parent' => $args['post_id'], 
     297                'post_status' => 'inherit', 
     298                'post_type' => 'attachment', 
     299                'post_mime_type' => 'image', 
     300                'order' => 'ASC', 
     301                'orderby' => 'menu_order ID', 
     302                'suppress_filters' => true 
     303            ) 
     304        ); 
     305 
     306        /* Check if any attachments were found. */ 
     307        if ( !empty( $attachments ) ) { 
     308 
     309            /* Set the default iterator to 0. */ 
     310            $i = 0; 
     311 
     312            /* Loop through each attachment. */ 
     313            foreach ( $attachments as $id => $attachment ) { 
     314 
     315                /* Set the attachment ID as the current ID in the loop. */ 
     316                $attachment_id = $id; 
     317 
     318                /* Break if/when we hit 'order_of_image'. */ 
     319                if ( ++$i == $args['order_of_image'] ) 
     320                    break; 
     321            } 
    280322        } 
    281323    } 
    282324 
    283     /* If no attachments or image is found, return false. */ 
    284     if ( empty( $attachments ) && empty( $image ) ) 
    285         return false; 
    286  
    287     /* Set the default iterator to 0. */ 
    288     $i = 0; 
    289  
    290     /* Loop through each attachment. Once the $order_of_image (default is '1') is reached, break the loop. */ 
    291     foreach ( $attachments as $id => $attachment ) { 
    292         if ( ++$i == $args['order_of_image'] ) { 
    293             $image = wp_get_attachment_image_src( $id, $args['size'] ); 
    294             $alt = trim( strip_tags( get_post_field( 'post_excerpt', $id ) ) ); 
    295             break; 
    296         } 
    297     } 
    298  
    299     /* Return the image URL. */ 
    300     return array( 'src' => $image[0], 'alt' => $alt ); 
     325    /* Check if we have an attachment ID before proceeding. */ 
     326    if ( !empty( $attachment_id ) ) { 
     327 
     328        /* Get the attachment image. */ 
     329        $image = wp_get_attachment_image_src( $id, $args['size'] ); 
     330 
     331        /* Get the attachment excerpt. */ 
     332        $alt = trim( strip_tags( get_post_field( 'post_excerpt', $id ) ) ); 
     333 
     334        /* Save the attachment as the 'featured image'. */ 
     335        if ( true === $args['thumbnail_id_save'] ) 
     336            set_post_thumbnail( $args['post_id'], $id ); 
     337 
     338        /* Return the image URL. */ 
     339        return array( 'src' => $image[0], 'alt' => $alt ); 
     340    } 
     341 
     342    /* Return false for anything else. */ 
     343    return false; 
    301344} 
    302345 
     
    306349 * 
    307350 * @since 0.7.0 
     351 * @access private 
    308352 * @param array $args Arguments for how to load and display the image. 
    309353 * @return array|bool Array of image attributes. | False if no image is found. 
     
    326370 * 
    327371 * @since 0.7.0 
     372 * @access private 
    328373 * @param array $args Arguments for how to load and display the image. 
    329374 * @return array|bool Array of image attributes. | False if no image is found. 
     
    338383 * 
    339384 * @since 0.7.0 
     385 * @access private 
    340386 * @param array $args Arguments for how to load and display the image. 
    341387 * @param array $image Array of image attributes ($image, $classes, $alt, $caption). 
     
    361407    if ( is_array( $meta_key ) ) { 
    362408        foreach ( $meta_key as $key ) 
    363             $classes[] = str_replace( ' ', '-', strtolower( $key ) ); 
     409            $classes[] = sanitize_html_class( $key ); 
    364410    } 
    365411 
    366412    /* Add the $size and any user-added $image_class to the class. */ 
    367     $classes[] = $size; 
    368     $classes[] = $image_class; 
     413    $classes[] = sanitize_html_class( $size ); 
     414    $classes[] = sanitize_html_class( $image_class ); 
    369415 
    370416    /* Join all the classes into a single string and make sure there are no duplicates. */ 
    371417    $class = join( ' ', array_unique( $classes ) ); 
    372  
    373     /* If there is a $post_thumbnail_id, apply the WP filters normally associated with get_the_post_thumbnail(). */ 
    374     if ( !empty( $image['post_thumbnail_id'] ) ) 
    375         do_action( 'begin_fetch_post_thumbnail_html', $post_id, $image['post_thumbnail_id'], $size ); 
    376418 
    377419    /* Add the image attributes to the <img /> element. */ 
     
    384426    /* If there is a $post_thumbnail_id, apply the WP filters normally associated with get_the_post_thumbnail(). */ 
    385427    if ( !empty( $image['post_thumbnail_id'] ) ) 
    386         do_action( 'end_fetch_post_thumbnail_html', $post_id, $image['post_thumbnail_id'], $size ); 
    387  
    388     /* If there is a $post_thumbnail_id, apply the WP filters normally associated with get_the_post_thumbnail(). */ 
    389     if ( !empty( $image['post_thumbnail_id'] ) ) 
    390428        $html = apply_filters( 'post_thumbnail_html', $html, $post_id, $image['post_thumbnail_id'], $size, '' ); 
    391429 
     
    399437 * 
    400438 * @since 0.6.0 
     439 * @access private 
    401440 * @param array $args Arguments for how to load and display the image. 
    402441 * @param array $image Array of image attributes ($image, $classes, $alt, $caption). 
     
    424463 * 
    425464 * @since 0.7.0 
     465 * @access private 
     466 * @param int $post_id The ID of the post to delete the cache for. 
     467 * @return void 
    426468 */ 
    427469function get_the_image_delete_cache_by_post( $post_id ) { 
     
    434476 * 
    435477 * @since 0.7.0 
     478 * @access private 
     479 * @param int $meta_id The ID of the metadata being updated. 
     480 * @param int $post_id The ID of the post to delete the cache for. 
     481 * @return void 
    436482 */ 
    437483function get_the_image_delete_cache_by_meta( $meta_id, $post_id ) { 
  • get-the-image/trunk/readme.html

    r373128 r556924  
    5151 
    5252<pre><code>$defaults = array( 
    53     'meta_key' => array( 'Thumbnail', 'thumbnail' ), 
    54     'post_id' => $post->ID, 
     53    'meta_key' => array( 'Thumbnail', 'thumbnail' ), // array|string 
     54    'post_id' => get_the_ID(), 
    5555    'attachment' => true, 
    56     'the_post_thumbnail' => true, 
     56    'the_post_thumbnail' => true, // WP 2.9+ image function 
    5757    'size' => 'thumbnail', 
    5858    'default_image' => false, 
     
    6565    'format' => 'img', 
    6666    'meta_key_save' => false, 
     67    'thumbnail_id_save' => false, // Set 'featured image'. 
    6768    'callback' => null, 
    6869    'cache' => true, 
    69     'echo' => true 
     70    'before' => '', 
     71    'after' => '', 
     72    'echo' => true, 
     73    'custom_key' => null, // @deprecated 0.6. Use 'meta_key'. 
     74    'default_size' => null, // @deprecated 0.5.  Use 'size'. 
    7075);</code></pre> 
    7176 
     
    99104    <dt>meta_key_save</dt> 
    100105    <dd>A meta key to save the image <acronym title="Uniform Resource Locator">URL</acronym> as.  This is useful if you're not using custom fields but want to cut back on database queries by having the script automatically set the custom field for you.  By default, this is set to <code>false</code>.</dd> 
     106    <dt>thumbnail_id_save</dt> 
     107    <dd>Whether to save the attachment ID as the post thumbnail (featured image) ID if no featured image is set for the post.  By default, this is set to <code>false</code></dd> 
    101108    <dt>callback</dt> 
    102109    <dd>A custom callback function that will be called if set.  It's only called if no images are found by any other options of the plugin.  However, it will be run before the <code>default_image</code> is set.  The <code>$args</code> array is passed to the callback function as the only parameter.</dd> 
    103110    <dt>cache</dt> 
    104111    <dd>Whether to use the WordPress Cache <acronym title="Application Programming Interface">API</acronym> (integrates with caching plugins) to serve the post images.  By default, this is set to <code>true</code>.</dd> 
     112    <dt>before</dt> 
     113    <dd><abbr title="Hypertext Markup Language">HTML</abbr> to place before the output of the image.</dd> 
     114    <dt>after</dt> 
     115    <dd><abbr title="Hypertext Markup Language">HTML</abbr> to place after the output of the image.</dd> 
    105116    <dt>echo</dt> 
    106117    <dd>If set to <code>true</code>, the image is shown on the page.  If set to <code>false</code>, the image will be returned to use in your own function. (Set to <code>true</code> by default.)</dd> 
     
    175186<p>Let's suppose you've used this code:</p> 
    176187 
    177 <pre><code>&lt;?php get_the_image( array( 'custom_key' => array( 'Donkey Kong', 'mario' ), 'size' => 'full' ) ); ?></code></pre> 
     188<pre><code>&lt;?php get_the_image( array( 'meta_key' => array( 'Donkey Kong', 'mario' ), 'size' => 'full' ) ); ?></code></pre> 
    178189 
    179190<p>This will give you these CSS classes to work with:</p> 
     
    194205<h2>Plugin support</h2> 
    195206 
    196 <p>I run a WordPress community called <a href="http://themehybrid.com" title="Theme Hybrid">Theme Hybrid</a>, which is where I fully support all of my WordPress projects, including plugins.  You can sign up for an account to get plugin support for a small yearly fee ($25 <acronym title="United States Dollars">USD</acronym> at the time of writing).</p> 
     207<p>I run a WordPress community called <a href="http://themehybrid.com" title="Theme Hybrid">Theme Hybrid</a>, which is where I fully support all of my WordPress projects, including plugins.  You can sign up for an account to get plugin support for a small yearly fee ($29 <acronym title="United States Dollars">USD</acronym> at the time of writing).</p> 
    197208 
    198209<p>I know.  I know.  You might not want to pay for support, but just consider it a donation to the project.  To continue making cool, GPL-licensed plugins and having the time to support them, I must pay the bills.</p> 
     
    204215<p>This plugin is copyrighted to <a href="http://justintadlock.com" title="Justin Tadlock">Justin Tadlock</a>.</p> 
    205216 
    206 <p>2008&thinsp;&ndash;&thinsp;2011 &copy; Justin Tadlock.  All rights reserved.</p> 
     217<p>2008&thinsp;&ndash;&thinsp;2012 &copy; Justin Tadlock.  All rights reserved.</p> 
    207218 
    208219</body> 
  • get-the-image/trunk/readme.txt

    r440355 r556924  
    44Tags: image, images, thumbnail 
    55Requires at least: 3.2 
    6 Tested up to: 3.2.1 
    7 Stable tag: 0.7 
     6Tested up to: 3.4 
     7Stable tag: 0.8 
    88 
    99An easy-to-use image script for adding things such as thumbnails and feature images. 
     
    5656 
    5757== Changelog == 
     58 
     59**Version 0.8** 
     60 
     61* Inline docs updates. 
     62* Added the `before` argument to output HTML before the image. 
     63* Added the `after` argument to output HTML after the image. 
     64* Added the `thumbnail_id_save` argument to allow the attached image to be saved as the thumbnail/featured image. 
     65* Get the post ID via `get_the_ID()` rather than the global `$post` object. 
     66* Fixed debug notice with `$image_html`. 
     67* Moved the `*_fetch_post_thumbnail_html` hooks into the main function and only fire them if displaying to the screen. 
     68* Simplified the `meta_key` logic. 
     69* Completely rewrote the `attachment` logic. 
     70* Sanitize classes with `sanitize_html_class()`. 
    5871 
    5972**Version 0.7** 
Note: See TracChangeset for help on using the changeset viewer.