Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Bringing code up to version 0.5.

git-svn-id: http://svn.locallylost.com/plugins/get-the-image/trunk@209 dba0f204-706d-4bc1-bc29-8b92e0485636
  • Loading branch information...
commit ca5ff9c5effc54c7d0ebb51d191a6013dd80e40b 1 parent 4c2ae75
greenshady authored
Showing with 124 additions and 58 deletions.
  1. +104 −46 get-the-image.php
  2. +10 −10 readme.html
  3. +10 −2 readme.txt
View
150 get-the-image.php
@@ -3,7 +3,7 @@
* Plugin Name: Get The Image
* Plugin URI: http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin
* 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.
- * Version: 0.4
+ * Version: 0.5
* Author: Justin Tadlock
* Author URI: http://justintadlock.com
*
@@ -12,8 +12,8 @@
* related to specific posts in some way. It creates an image-based representation of a WordPress
* post (or any post type).
*
- * @copyright 2008 - 2009
- * @version 0.4
+ * @copyright 2008 - 2010
+ * @version 0.5
* @author Justin Tadlock
* @link http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
@@ -28,6 +28,12 @@
/* Adds theme support for post images. */
add_theme_support( 'post-thumbnails' );
+/* Delete the cache when a post or post metadata is updated. */
+add_action( 'save_post', 'get_the_image_delete_cache' );
+add_action( 'deleted_post_meta', 'get_the_image_delete_cache' );
+add_action( 'updated_post_meta', 'get_the_image_delete_cache' );
+add_action( 'added_post_meta', 'get_the_image_delete_cache' );
+
/**
* This is a highly intuitive function that gets images. It first calls for custom field keys. If no
* custom field key is set, check for the_post_thumbnail(). If no post image, check for images
@@ -49,7 +55,8 @@ function get_the_image( $args = array() ) {
'post_id' => $post->ID,
'attachment' => true,
'the_post_thumbnail' => true, // WP 2.9+ image function
- 'default_size' => 'thumbnail',
+ 'default_size' => false, // Deprecated 0.5 in favor of $size
+ 'size' => 'thumbnail',
'default_image' => false,
'order_of_image' => 1,
'link_to_post' => true,
@@ -67,38 +74,71 @@ function get_the_image( $args = array() ) {
/* Merge the input arguments and the defaults. */
$args = wp_parse_args( $args, $defaults );
+ /* If $default_size is given, overwrite $size. */
+ if ( !empty( $args['default_size'] ) )
+ $args['size'] = $args['default_size'];
+
+ /* If $format is set to 'array', don't link to the post. */
+ if ( 'array' == $args['format'] )
+ $args['link_to_post'] = false;
+
/* Extract the array to allow easy use of variables. */
extract( $args );
- /* If a custom field key (array) is defined, check for images by custom field. */
- if ( $custom_key )
- $image = image_by_custom_field( $args );
+ /* Check for a cached image. */
+ $cache = wp_cache_get( 'get_the_image' );
- /* If no image found and $the_post_thumbnail is set to true, check for a post image (WP feature). */
- if ( !$image && $the_post_thumbnail )
- $image = image_by_the_post_thumbnail( $args );
+ if ( !is_array( $cache ) )
+ $cache = array();
- /* If no image found and $attachment is set to true, check for an image by attachment. */
- if ( !$image && $attachment )
- $image = image_by_attachment( $args );
+ /* If there is no cached image, let's see if one exists. */
+ if ( !isset( $cache[$post_id][$size] ) ) {
- /* If no image found and $image_scan is set to true, scan the post for images. */
- if ( !$image && $image_scan )
- $image = image_by_scan( $args );
+ /* If a custom field key (array) is defined, check for images by custom field. */
+ if ( $custom_key )
+ $image = image_by_custom_field( $args );
- /* If no image found and a $default_image is set, get the default image. */
- if ( !$image && $default_image )
- $image = image_by_default( $args );
+ /* If no image found and $the_post_thumbnail is set to true, check for a post image (WP feature). */
+ if ( !$image && $the_post_thumbnail )
+ $image = image_by_the_post_thumbnail( $args );
- /* If an image is returned, run it through the display function. */
- if ( $image )
- $image = display_the_image( $args, $image );
+ /* If no image found and $attachment is set to true, check for an image by attachment. */
+ if ( !$image && $attachment )
+ $image = image_by_attachment( $args );
+
+ /* If no image found and $image_scan is set to true, scan the post for images. */
+ if ( !$image && $image_scan )
+ $image = image_by_scan( $args );
+
+ /* If no image found and a $default_image is set, get the default image. */
+ if ( !$image && $default_image )
+ $image = image_by_default( $args );
+
+ /* If an image is returned, run it through the display function. */
+ if ( $image )
+ $image = display_the_image( $args, $image );
+
+ $cache[$post_id][$size] = $image;
+ wp_cache_set( 'get_the_image', $cache );
+ }
+ else {
+ $image = $cache[$post_id][$size];
+ }
/* Allow plugins/theme to override the final output. */
$image = apply_filters( 'get_the_image', $image );
/* Display the image if $echo is set to true and the $format isn't an array. Else, return the image. */
- if ( $echo && 'array' !== $format )
+ if ( 'array' == $format ) {
+ $atts = wp_kses_hair( $image, array( 'http' ) );
+
+ foreach ( $atts as $att )
+ $out[$att['name']] = $att['value'];
+
+ $out['url'] = $out['src']; // @deprecated 0.5 Use 'src' instead of 'url'.
+ return $out;
+ }
+ elseif ( $echo )
echo $image;
else
return $image;
@@ -156,13 +196,16 @@ function image_by_the_post_thumbnail( $args = array() ) {
return false;
/* Apply filters on post_thumbnail_size because this is a default WP filter used with its image feature. */
- $size = apply_filters( 'post_thumbnail_size', $args['default_size'] );
+ $size = apply_filters( 'post_thumbnail_size', $args['size'] );
/* Get the attachment image source. This should return an array. */
$image = wp_get_attachment_image_src( $post_thumbnail_id, $size );
+ /* Get the attachment excerpt to use as alt text. */
+ $alt = trim( strip_tags( get_post_field( 'post_excerpt', $post_thumbnail_id ) ) );
+
/* Return both the image URL and the post thumbnail ID. */
- return array( 'url' => $image[0], 'post_thumbnail_id' => $post_thumbnail_id );
+ return array( 'url' => $image[0], 'post_thumbnail_id' => $post_thumbnail_id, 'alt' => $alt );
}
/**
@@ -179,20 +222,29 @@ function image_by_attachment( $args = array() ) {
/* Get attachments for the inputted $post_id. */
$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' ) );
- /* If no attachments are found, return false. */
- if ( empty( $attachments ) )
+ /* If no attachments are found, check if the post itself is an attachment and grab its image. */
+ if ( empty( $attachments ) && $args['size'] ) {
+ if ( 'attachment' == get_post_type( $args['post_id'] ) ) {
+ $image = wp_get_attachment_image_src( $args['post_id'], $args['size'] );
+ $alt = trim( strip_tags( get_post_field( 'post_excerpt', $args['post_id'] ) ) );
+ }
+ }
+
+ /* If no attachments or image is found, return false. */
+ if ( empty( $attachments ) && empty( $image ) )
return false;
/* Loop through each attachment. Once the $order_of_image (default is '1') is reached, break the loop. */
foreach ( $attachments as $id => $attachment ) {
if ( ++$i == $args['order_of_image'] ) {
- $image = wp_get_attachment_image_src( $id, $args['default_size'] );
+ $image = wp_get_attachment_image_src( $id, $args['size'] );
+ $alt = trim( strip_tags( get_post_field( 'post_excerpt', $id ) ) );
break;
}
}
/* Return the image URL. */
- return array( 'url' => $image[0] );
+ return array( 'url' => $image[0], 'alt' => $alt );
}
/**
@@ -246,11 +298,12 @@ function display_the_image( $args = array(), $image = false ) {
/* Extract the arguments for easy-to-use variables. */
extract( $args );
+ /* If there is alt text, set it. Otherwise, default to the post title. */
+ $image_alt = ( ( $image['alt'] ) ? $image['alt'] : apply_filters( 'the_title', get_post_field( 'post_title', $post_id ) ) );
+
/* If there is a width or height, set them as HMTL-ready attributes. */
- if ( $width )
- $width = ' width="' . $width . '"';
- if ( $height )
- $height = ' height="' . $height . '"';
+ $width = ( ( $width ) ? ' width="' . esc_attr( $width ) . '"' : '' );
+ $height = ( ( $height ) ? ' height="' . esc_attr( $height ) . '"' : '' );
/* Loop through the custom field keys and add them as classes. */
if ( is_array( $custom_key ) ) {
@@ -258,40 +311,45 @@ function display_the_image( $args = array(), $image = false ) {
$classes[] = str_replace( ' ', '-', strtolower( $key ) );
}
- /* Add the $default_size and any user-added $image_class to the class. */
- $classes[] = $default_size;
+ /* Add the $size and any user-added $image_class to the class. */
+ $classes[] = $size;
$classes[] = $image_class;
- /* Join all the classes into a single string. */
- $class = join( ' ', $classes );
-
- /* If $format should be an array, return the attributes in array format. */
- if ( 'array' == $format )
- return array( 'url' => $image['url'], 'alt' => esc_attr( strip_tags( get_post_field( 'post_title', $post_id ) ) ), 'class' => $class, 'link' => get_permalink( $post_id ) );
+ /* Join all the classes into a single string and make sure there are no duplicates. */
+ $class = join( ' ', array_unique( $classes ) );
/* If there is a $post_thumbnail_id, apply the WP filters normally associated with get_the_post_thumbnail(). */
if ( $image['post_thumbnail_id'] )
- do_action( 'begin_fetch_post_thumbnail_html', $post_id, $image['post_thumbnail_id'], $default_size );
+ do_action( 'begin_fetch_post_thumbnail_html', $post_id, $image['post_thumbnail_id'], $size );
/* Add the image attributes to the <img /> element. */
- $html = '<img src="' . $image['url'] . '" alt="' . esc_attr( strip_tags( get_post_field( 'post_title', $post_id ) ) ) . '" class="' . $class . '"' . $width . $height . ' />';
+ $html = '<img src="' . $image['url'] . '" alt="' . esc_attr( strip_tags( $image_alt ) ) . '" class="' . esc_attr( $class ) . '"' . $width . $height . ' />';
/* If $link_to_post is set to true, link the image to its post. */
if ( $link_to_post )
- $html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '">' . $html . '</a>';
+ $html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( apply_filters( 'the_title', get_post_field( 'post_title', $post_id ) ) ) . '">' . $html . '</a>';
/* If there is a $post_thumbnail_id, apply the WP filters normally associated with get_the_post_thumbnail(). */
if ( $image['post_thumbnail_id'] )
- do_action( 'end_fetch_post_thumbnail_html', $post_id, $image['post_thumbnail_id'], $default_size );
+ do_action( 'end_fetch_post_thumbnail_html', $post_id, $image['post_thumbnail_id'], $size );
/* If there is a $post_thumbnail_id, apply the WP filters normally associated with get_the_post_thumbnail(). */
if ( $image['post_thumbnail_id'] )
- $html = apply_filters( 'post_thumbnail_html', $html, $post_id, $image['post_thumbnail_id'], $default_size, $attr );
+ $html = apply_filters( 'post_thumbnail_html', $html, $post_id, $image['post_thumbnail_id'], $size, '' );
return $html;
}
/**
+ * Deletes the image cache for users that are using a persistent-caching plugin.
+ *
+ * @since 0.5
+ */
+function get_the_image_delete_cache() {
+ wp_cache_delete( 'get_the_image' );
+}
+
+/**
* Get the image with a link to the post. Use get_the_image() instead.
*
* @since 0.1
View
20 readme.html
@@ -44,7 +44,7 @@
<p><strong>Example with function-style parameters</strong></p>
-<pre><code>&lt;?php get_the_image( array( 'custom_key' => array( 'Thumbnail', 'thumbnail' ), 'default_size' => 'thumbnail' ) ); ?></code></pre>
+<pre><code>&lt;?php get_the_image( array( 'custom_key' => array( 'Thumbnail', 'thumbnail' ), 'size' => 'thumbnail' ) ); ?></code></pre>
<h2>The image script parameters</h2>
@@ -53,7 +53,7 @@
<pre><code>$defaults = array(
'custom_key' => array( 'Thumbnail', 'thumbnail' ),
'attachment' => true,
- 'default_size' => 'thumbnail',
+ 'size' => 'thumbnail',
'the_post_thumbnail' => true,
'default_image' => false,
'order_of_image' => 1,
@@ -70,10 +70,10 @@
<dd>This parameter refers to a custom field key (or keys) that you use. Remember, custom field keys are case-sensitive (defaults are <code>Thumbnail</code> and <code>thumbnail</code>).</dd>
<dt>attachment</dt>
<dd>The script will look for images attached to the post (set to <code>true</code> by default).</dd>
- <dt>default_size</dt>
- <dd>This refers to the default size of an attached image. You can choose between <code>thumbnail</code>, <code>medium</code>, <code>large</code> (WP 2.7+), or <code>full</code> (the default is <code>thumbnail</code>).</dd>
+ <dt>size</dt>
+ <dd>This refers to the size of an attached image. You can choose between <code>thumbnail</code>, <code>medium</code>, <code>large</code>, <code>full</code>, or any custom image size you have available (the default is <code>thumbnail</code>).</dd>
<dt>the_post_thumbnail</dt>
- <dd>This refers to the WordPress 2.9's new <code>the_post_thumbnail()</code> feature. By having this set to <code>true</code>, you may select an image from the post image meta box while in the post editor.
+ <dd>This refers to the WordPress 2.9's new <code>the_post_thumbnail()</code> feature. By having this set to <code>true</code>, you may select an image from the post image meta box while in the post editor.</dd>
<dt>default_image</dt>
<dd>Will take the input of an image URL and use it if no other images are found (no default set).</dd>
<dt>order_of_image</dt>
@@ -102,7 +102,7 @@
<p><strong>Example 2:</strong> Let's suppose you want a full-sized image and maybe you want to grab it by a custom field key of <code>Feature</code>. Depending on your theme, this will need to go within the Loop in whatever file is calling the featured article.</p>
-<pre><code>&lt;?php get_the_image( array( 'custom_key' => array( 'Feature' ), 'default_size' => 'full' ) ); ?></code></pre>
+<pre><code>&lt;?php get_the_image( array( 'custom_key' => array( 'Feature' ), 'size' => 'full' ) ); ?></code></pre>
<p>If no feature image exists by custom field, it will look for images attached to your post.</p>
@@ -126,7 +126,7 @@
&lt;div id="post-&lt;?php the_ID(); ?>" &lt;?php post_class(); ?>>
- &lt;?php get_the_image( array( 'custom_key' => array( 'feature_img' ), 'default_size' => 'medium', 'width' => '200', 'height' => '200', 'image_class' => 'feature' ) ); ?>
+ &lt;?php get_the_image( array( 'custom_key' => array( 'feature_img' ), 'size' => 'medium', 'width' => '200', 'height' => '200', 'image_class' => 'feature' ) ); ?>
&lt;h2>&lt;a href="&lt;?php the_permalink(); ?>" title="&lt;?php the_title_attribute(); ?>" rel="bookmark">&lt;?php the_title(); ?>&lt;/a>&lt;/h2>
@@ -156,7 +156,7 @@
<p>Let's suppose you've used this code:</p>
-<pre><code>&lt;?php get_the_image( array( 'custom_key' => array( 'Donkey Kong', 'mario' ), 'default_size' => 'full' ) ); ?></code></pre>
+<pre><code>&lt;?php get_the_image( array( 'custom_key' => array( 'Donkey Kong', 'mario' ), 'size' => 'full' ) ); ?></code></pre>
<p>This will give you these CSS classes to work with:</p>
@@ -168,7 +168,7 @@
<pre><code>&lt;?php get_the_image( array( 'image_class' => 'custom-image' ) ); ?></code></pre>
-<p>You will still have the <code>default_size</code> and <code>custom_key</code> classes plus your additional class:</p>
+<p>You will still have the <code>size</code> and <code>custom_key</code> classes plus your additional class:</p>
<pre><code>img.custom-image { }
img.thumbnail { }</code></pre>
@@ -185,7 +185,7 @@
<p>This plugin is copyrighted to <a href="http://justintadlock.com" title="Justin Tadlock">Justin Tadlock</a>.</p>
-<p>2008&thinsp;&ndash;&thinsp;2009 &copy; Justin Tadlock. All rights reserved.</p>
+<p>2008&thinsp;&ndash;&thinsp;2010 &copy; Justin Tadlock. All rights reserved.</p>
</body>
</html>
View
12 readme.txt
@@ -3,8 +3,8 @@ Contributors: greenshady
Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=3687060
Tags: image, images, thumbnail
Requires at least: 2.9
-Tested up to: 2.9
-Stable tag: 0.4
+Tested up to: 3.0
+Stable tag: 0.5
An easy-to-use image script for adding things such as thumbnails and feature images.
@@ -58,6 +58,14 @@ You can view this plugin in action on my <a href="http://justintadlock.com" titl
Earlier versions were not documented well.
+**Version 0.5**
+
+* Added support for persistent-caching plugins.
+* Switched the `default_size` argument to `size` to be more in line with the WordPress post thumbnail arguments, but `default_size` will still work.
+* Now using `wp_kses_hair()` to extract image attributes when using the `array` value for `format`.
+* Image `alt` text will now use the attachment description if one has been given rather than the post title.
+* Updated the `readme.html` instructions for using the plugin.
+
**Version 0.4**
* Dropped support for older versions of WordPress. Now only compatible with 2.9+.
Please sign in to comment.
Something went wrong with that request. Please try again.