Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Bringing plugin up to version 0.6.2.

git-svn-id: http://svn.locallylost.com/plugins/get-the-image/trunk@654 dba0f204-706d-4bc1-bc29-8b92e0485636
  • Loading branch information...
commit bd4cc2468f38e5d5276b6df4e4b531eee5e4357c 1 parent 7d49016
greenshady authored
Showing with 46 additions and 26 deletions.
  1. +38 −23 get-the-image.php
  2. +1 −1  readme.html
  3. +7 −2 readme.txt
View
61 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.6.1
+ * Version: 0.6.2
* Author: Justin Tadlock
* Author URI: http://justintadlock.com
*
@@ -23,7 +23,7 @@
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @package GetTheImage
- * @version 0.6.1
+ * @version 0.6.2
* @author Justin Tadlock <justin@justintadlock.com>
* @copyright Copyright (c) 2008 - 2010, Justin Tadlock
* @link http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin
@@ -99,14 +99,17 @@ function get_the_image( $args = array() ) {
/* Extract the array to allow easy use of variables. */
extract( $args );
+ /* Get cache key based on $args. */
+ $key = md5( serialize( compact( array_keys( $args ) ) ) );
+
/* Check for a cached image. */
- $image_cache = wp_cache_get( 'get_the_image' );
+ $image_cache = wp_cache_get( $post_id, 'get_the_image' );
if ( !is_array( $image_cache ) )
$image_cache = array();
/* If there is no cached image, let's see if one exists. */
- if ( !isset( $image_cache[$post_id][$size] ) || empty( $cache ) ) {
+ if ( !isset( $image_cache[$key] ) || empty( $cache ) ) {
/* If a custom field key (array) is defined, check for images by custom field. */
if ( !empty( $meta_key ) )
@@ -132,25 +135,33 @@ function get_the_image( $args = array() ) {
if ( empty( $image ) && !empty( $default_image ) )
$image = image_by_default( $args );
- /* If $meta_key_save was set, save the image to a custom field. */
- if ( !empty( $image ) && !empty( $meta_key_save ) )
- get_the_image_meta_key_save( $args, $image );
+ /* If an image was found. */
+ if ( !empty( $image ) ) {
+
+ /* Change $image['url'] to $image['src']. */
+ if ( isset( $image['url'] ) ) // 'url' key deprecated 0.6.2
+ $image['src'] = $image['url'];
- /* If an image is returned, run it through the display function. */
- if ( !empty( $image ) )
- $image = display_the_image( $args, $image );
+ /* If $meta_key_save was set, save the image to a custom field. */
+ if ( !empty( $meta_key_save ) )
+ get_the_image_meta_key_save( $args, $image );
+
+ /* If an image is returned, run it through the display function. */
+ if ( !empty( $image ) )
+ $image = display_the_image( $args, $image );
+ }
- $image_cache[$post_id][$size] = $image;
- wp_cache_set( 'get_the_image', $image_cache );
+ $image_cache[$key] = $image;
+ wp_cache_set( $post_id, $image_cache, 'get_the_image' );
}
else {
- $image = $image_cache[$post_id][$size];
+ $image = $image_cache[$key];
}
/* 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 $format is set to 'array', return an array of image attributes. */
if ( 'array' == $format ) {
$atts = wp_kses_hair( $image, array( 'http' ) );
@@ -160,10 +171,14 @@ function get_the_image( $args = array() ) {
$out['url'] = $out['src']; // @deprecated 0.5 Use 'src' instead of 'url'.
return $out;
}
- elseif ( !empty( $echo ) )
- echo $image;
- else
+
+ /* Or, if $echo is set to false, return the formatted image. */
+ elseif ( empty( $echo ) ) {
return $image;
+ }
+
+ /* Display the image if we get to this point. */
+ echo $image;
}
/* Internal Functions */
@@ -324,7 +339,7 @@ function image_by_default( $args = array() ) {
function display_the_image( $args = array(), $image = false ) {
/* If there is no image URL, return false. */
- if ( empty( $image['url'] ) )
+ if ( empty( $image['src'] ) )
return false;
/* Extract the arguments for easy-to-use variables. */
@@ -355,7 +370,7 @@ function display_the_image( $args = array(), $image = false ) {
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( $image_alt ) ) . '" class="' . esc_attr( $class ) . '"' . $width . $height . ' />';
+ $html = '<img src="' . $image['src'] . '" 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 )
@@ -384,7 +399,7 @@ function display_the_image( $args = array(), $image = false ) {
function get_the_image_meta_key_save( $args = array(), $image = array() ) {
/* If the $meta_key_save argument is empty or there is no image $url given, return. */
- if ( empty( $args['meta_key_save'] ) || empty( $image['url'] ) )
+ if ( empty( $args['meta_key_save'] ) || empty( $image['src'] ) )
return;
/* Get the current value of the meta key. */
@@ -392,11 +407,11 @@ function get_the_image_meta_key_save( $args = array(), $image = array() ) {
/* If there is no value for the meta key, set a new value with the image $url. */
if ( empty( $meta ) )
- add_post_meta( $args['post_id'], $args['meta_key_save'], $image['url'] );
+ add_post_meta( $args['post_id'], $args['meta_key_save'], $image['src'] );
/* If the current value doesn't match the image $url, update it. */
- elseif ( $meta !== $image['url'] )
- update_post_meta( $args['post_id'], $args['meta_key_save'], $image['url'], $meta );
+ elseif ( $meta !== $image['src'] )
+ update_post_meta( $args['post_id'], $args['meta_key_save'], $image['src'], $meta );
}
/**
View
2  readme.html
@@ -203,7 +203,7 @@
<p>This plugin is copyrighted to <a href="http://justintadlock.com" title="Justin Tadlock">Justin Tadlock</a>.</p>
-<p>2008&thinsp;&ndash;&thinsp;2010 &copy; Justin Tadlock. All rights reserved.</p>
+<p>2008&thinsp;&ndash;&thinsp;2011 &copy; Justin Tadlock. All rights reserved.</p>
</body>
</html>
View
9 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: 3.0.1
-Stable tag: 0.6.1
+Tested up to: 3.1.1
+Stable tag: 0.6.2
An easy-to-use image script for adding things such as thumbnails and feature images.
@@ -56,6 +56,11 @@ You can view this plugin in action on my <a href="http://justintadlock.com" titl
== Changelog ==
+**Version 0.6.2**
+
+* Updated the cache to save by post ID instead of a single object.
+* Minor code adjustments.
+
**Version 0.6.1**
* Updated inline documentation of the code.
Please sign in to comment.
Something went wrong with that request. Please try again.