Permalink
Browse files

Updating plugin to use version 0.7 of the script.

git-svn-id: http://svn.locallylost.com/plugins/get-the-image/trunk@862 dba0f204-706d-4bc1-bc29-8b92e0485636
  • Loading branch information...
1 parent bd4cc24 commit 9933b0bd9c24f0b79f68c0d6f39d52986583d82e greenshady committed Sep 19, 2011
Showing with 143 additions and 68 deletions.
  1. +134 −65 get-the-image.php
  2. +9 −3 readme.txt
View
@@ -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.2
+ * Version: 0.7
* Author: Justin Tadlock
* Author URI: http://justintadlock.com
*
@@ -23,21 +23,21 @@
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @package GetTheImage
- * @version 0.6.2
+ * @version 0.7.0
* @author Justin Tadlock <justin@justintadlock.com>
- * @copyright Copyright (c) 2008 - 2010, Justin Tadlock
+ * @copyright Copyright (c) 2008 - 2011, 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
*/
-/* Adds theme support for post images. */
+/* Adds theme support for WordPress 'featured 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' );
+add_action( 'save_post', 'get_the_image_delete_cache_by_post' );
+add_action( 'deleted_post_meta', 'get_the_image_delete_cache_by_meta', 10, 2 );
+add_action( 'updated_post_meta', 'get_the_image_delete_cache_by_meta', 10, 2 );
+add_action( 'added_post_meta', 'get_the_image_delete_cache_by_meta', 10, 2 );
/**
* The main image function for displaying an image. It supports several arguments that allow developers to
@@ -48,7 +48,7 @@
* 'image_scan', 'callback', and 'default_image'.
*
* @since 0.1.0
- * @global $post The current post's DB object.
+ * @global $post The current post's database object.
* @param array $args Arguments for how to load and display the image.
* @return string|array The HTML for the image. | Image attributes in an array.
*/
@@ -113,47 +113,45 @@ function get_the_image( $args = array() ) {
/* If a custom field key (array) is defined, check for images by custom field. */
if ( !empty( $meta_key ) )
- $image = image_by_custom_field( $args );
+ $image = get_the_image_by_meta_key( $args );
/* If no image found and $the_post_thumbnail is set to true, check for a post image (WP feature). */
if ( empty( $image ) && !empty( $the_post_thumbnail ) )
- $image = image_by_the_post_thumbnail( $args );
+ $image = get_the_image_by_post_thumbnail( $args );
/* If no image found and $attachment is set to true, check for an image by attachment. */
if ( empty( $image ) && !empty( $attachment ) )
- $image = image_by_attachment( $args );
+ $image = get_the_image_by_attachment( $args );
/* If no image found and $image_scan is set to true, scan the post for images. */
if ( empty( $image ) && !empty( $image_scan ) )
- $image = image_by_scan( $args );
+ $image = get_the_image_by_scan( $args );
- /* If no image found and a callback function was given. */
+ /* If no image found and a callback function was given. Callback function must pass back array of <img> attributes. */
if ( empty( $image ) && !is_null( $callback ) && function_exists( $callback ) )
$image = call_user_func( $callback, $args );
/* If no image found and a $default_image is set, get the default image. */
if ( empty( $image ) && !empty( $default_image ) )
- $image = image_by_default( $args );
+ $image = get_the_image_by_default( $args );
/* 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 $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 );
+ get_the_image_meta_key_save( $args, $image['src'] );
- /* If an image is returned, run it through the display function. */
- if ( !empty( $image ) )
- $image = display_the_image( $args, $image );
- }
+ /* Format the image HTML. */
+ $image = get_the_image_format( $args, $image );
- $image_cache[$key] = $image;
- wp_cache_set( $post_id, $image_cache, 'get_the_image' );
+ /* Set the image cache for the specific post. */
+ $image_cache[$key] = $image;
+ wp_cache_set( $post_id, $image_cache, 'get_the_image' );
+ }
}
+
+ /* If an image was already cached for the post and arguments, use it. */
else {
$image = $image_cache[$key];
}
@@ -163,17 +161,25 @@ function get_the_image( $args = array() ) {
/* If $format is set to 'array', return an array of image attributes. */
if ( 'array' == $format ) {
+
+ /* Set up a default empty array. */
+ $out = array();
+
+ /* Get the image attributes. */
$atts = wp_kses_hair( $image, array( 'http' ) );
+ /* Loop through the image attributes and add them in key/value pairs for the return array. */
foreach ( $atts as $att )
$out[$att['name']] = $att['value'];
$out['url'] = $out['src']; // @deprecated 0.5 Use 'src' instead of 'url'.
+
+ /* Return the array of attributes. */
return $out;
}
/* Or, if $echo is set to false, return the formatted image. */
- elseif ( empty( $echo ) ) {
+ elseif ( false === $echo ) {
return $image;
}
@@ -187,11 +193,11 @@ function get_the_image( $args = array() ) {
* Calls images by custom field key. Script loops through multiple custom field keys. If that particular key
* is found, $image is set and the loop breaks. If an image is found, it is returned.
*
- * @since 0.3.0
- * @param array $args
- * @return array|bool
+ * @since 0.7.0
+ * @param array $args Arguments for how to load and display the image.
+ * @return array|bool Array of image attributes. | False if no image is found.
*/
-function image_by_custom_field( $args = array() ) {
+function get_the_image_by_meta_key( $args = array() ) {
/* If $meta_key is not an array. */
if ( !is_array( $args['meta_key'] ) ) {
@@ -217,7 +223,7 @@ function image_by_custom_field( $args = array() ) {
/* If a custom key value has been given for one of the keys, return the image URL. */
if ( !empty( $image ) )
- return array( 'url' => $image );
+ return array( 'src' => $image );
return false;
}
@@ -227,11 +233,11 @@ function image_by_custom_field( $args = array() ) {
* If an image is found, return it and the $post_thumbnail_id. The WordPress function's other filters are
* later added in the display_the_image() function.
*
- * @since 0.4.0
- * @param array $args
- * @return array|bool
+ * @since 0.7.0
+ * @param array $args Arguments for how to load and display the image.
+ * @return array|bool Array of image attributes. | False if no image is found.
*/
-function image_by_the_post_thumbnail( $args = array() ) {
+function get_the_image_by_post_thumbnail( $args = array() ) {
/* Check for a post image ID (set by WP as a custom field). */
$post_thumbnail_id = get_post_thumbnail_id( $args['post_id'] );
@@ -250,18 +256,18 @@ function image_by_the_post_thumbnail( $args = array() ) {
$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, 'alt' => $alt );
+ return array( 'src' => $image[0], 'post_thumbnail_id' => $post_thumbnail_id, 'alt' => $alt );
}
/**
* Check for attachment images. Uses get_children() to check if the post has images attached. If image
* attachments are found, loop through each. The loop only breaks once $order_of_image is reached.
*
- * @since 0.3.0
- * @param array $args
- * @return array|bool
+ * @since 0.7.0
+ * @param array $args Arguments for how to load and display the image.
+ * @return array|bool Array of image attributes. | False if no image is found.
*/
-function image_by_attachment( $args = array() ) {
+function get_the_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' ) );
@@ -291,26 +297,25 @@ function image_by_attachment( $args = array() ) {
}
/* Return the image URL. */
- return array( 'url' => $image[0], 'alt' => $alt );
+ return array( 'src' => $image[0], 'alt' => $alt );
@danielbachhuber

danielbachhuber Mar 15, 2013

@justintadlock this seems like a pretty big change without any backwards compat. Can you explain your thinking?

@danielbachhuber

danielbachhuber Mar 15, 2013

I've hotfixed for now in 4f7f8dc (sorry for the big changeset, our FS bot decided to change the permissions on the file too)

@justintadlock

justintadlock Mar 15, 2013

Owner

I don't really concern myself with back-compatibility when it comes to functions that are marked as being for internal use by the plugin. The get_the_image() function handles the compatibility for the url argument.

}
/**
* Scans the post for images within the content. Not called by default with get_the_image(). Shouldn't use
* if using large images within posts, better to use the other options.
*
- * @since 0.3.0
- * @global $post The current post's DB object.
- * @param array $args
- * @return array|bool
+ * @since 0.7.0
+ * @param array $args Arguments for how to load and display the image.
+ * @return array|bool Array of image attributes. | False if no image is found.
*/
-function image_by_scan( $args = array() ) {
+function get_the_image_by_scan( $args = array() ) {
/* Search the post's content for the <img /> tag and get its URL. */
preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i', get_post_field( 'post_content', $args['post_id'] ), $matches );
/* If there is a match for the image, return its URL. */
- if ( isset( $matches ) && $matches[1][0] )
- return array( 'url' => $matches[1][0] );
+ if ( isset( $matches ) && !empty( $matches[1][0] ) )
+ return array( 'src' => $matches[1][0] );
return false;
}
@@ -319,24 +324,24 @@ function image_by_scan( $args = array() ) {
* Used for setting a default image. The function simply returns the image URL it was given in an array.
* Not used with get_the_image() by default.
*
- * @since 0.3.0
- * @param array $args
- * @return array
+ * @since 0.7.0
+ * @param array $args Arguments for how to load and display the image.
+ * @return array|bool Array of image attributes. | False if no image is found.
*/
-function image_by_default( $args = array() ) {
- return array( 'url' => $args['default_image'] );
+function get_the_image_by_default( $args = array() ) {
+ return array( 'src' => $args['default_image'] );
}
/**
* Formats an image with appropriate alt text and class. Adds a link to the post if argument is set. Should
* only be called if there is an image to display, but will handle it if not.
*
- * @since 0.1.0
- * @param array $args
- * @param array $image Array of image info ($image, $classes, $alt, $caption).
+ * @since 0.7.0
+ * @param array $args Arguments for how to load and display the image.
+ * @param array $image Array of image attributes ($image, $classes, $alt, $caption).
* @return string $image Formatted image (w/link to post if the option is set).
*/
-function display_the_image( $args = array(), $image = false ) {
+function get_the_image_format( $args = array(), $image = false ) {
/* If there is no image URL, return false. */
if ( empty( $image['src'] ) )
@@ -393,8 +398,8 @@ function display_the_image( $args = array(), $image = false ) {
* of expensive scans of the content when using the image scan feature.
*
* @since 0.6.0
- * @param array $args Parameters for what image to get.
- * @param array $image Array of image info ($image, $classes, $alt, $caption).
+ * @param array $args Arguments for how to load and display the image.
+ * @param array $image Array of image attributes ($image, $classes, $alt, $caption).
*/
function get_the_image_meta_key_save( $args = array(), $image = array() ) {
@@ -415,22 +420,86 @@ function get_the_image_meta_key_save( $args = array(), $image = array() ) {
}
/**
- * Deletes the image cache for users that are using a persistent-caching plugin.
+ * Deletes the image cache for the specific post when the 'save_post' hook is fired.
*
- * @since 0.5.0
+ * @since 0.7.0
*/
-function get_the_image_delete_cache() {
- wp_cache_delete( 'get_the_image' );
+function get_the_image_delete_cache_by_post( $post_id ) {
+ wp_cache_delete( $post_id, 'get_the_image' );
}
/**
- * Get the image with a link to the post. Use get_the_image() instead.
+ * Deletes the image cache for a specific post when the 'added_post_meta', 'deleted_post_meta',
+ * or 'updated_post_meta' hooks are called.
*
+ * @since 0.7.0
+ */
+function get_the_image_delete_cache_by_meta( $meta_id, $post_id ) {
+ wp_cache_delete( $post_id, 'get_the_image' );
+}
+
+/**
* @since 0.1.0
* @deprecated 0.3.0
*/
function get_the_image_link( $deprecated = '', $deprecated_2 = '', $deprecated_3 = '' ) {
get_the_image();
}
+/**
+ * @since 0.3.0
+ * @deprecated 0.7.0
+ */
+function image_by_custom_field( $args = array() ) {
+ return get_the_image_by_meta_key( $args );
+}
+
+/**
+ * @since 0.4.0
+ * @deprecated 0.7.0
+ */
+function image_by_the_post_thumbnail( $args = array() ) {
+ return get_the_image_by_post_thumbnail( $args );
+}
+
+/**
+ * @since 0.3.0
+ * @deprecated 0.7.0
+ */
+function image_by_attachment( $args = array() ) {
+ return get_the_image_by_attachment( $args );
+}
+
+/**
+ * @since 0.3.0
+ * @deprecated 0.7.0
+ */
+function image_by_scan( $args = array() ) {
+ return get_the_image_by_scan( $args );
+}
+
+/**
+ * @since 0.3.0
+ * @deprecated 0.7.0
+ */
+function image_by_default( $args = array() ) {
+ return get_the_image_by_default( $args );
+}
+
+/**
+ * @since 0.1.0
+ * @deprecated 0.7.0
+ */
+function display_the_image( $args = array(), $image = false ) {
+ return get_the_image_format( $args, $image );
+}
+
+/**
+ * @since 0.5.0
+ * @deprecated 0.7.0 Replaced by cache delete functions specifically for the post ID.
+ */
+function get_the_image_delete_cache() {
+ return;
+}
+
?>
View
@@ -2,9 +2,9 @@
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.1.1
-Stable tag: 0.6.2
+Requires at least: 3.2
+Tested up to: 3.2.1
+Stable tag: 0.7
An easy-to-use image script for adding things such as thumbnails and feature images.
@@ -56,6 +56,12 @@ You can view this plugin in action on my <a href="http://justintadlock.com" titl
== Changelog ==
+**Version 0.7**
+
+* Deprecated and replaced functions that lacked the `get_the_image_` prefix.
+* New cache delete functions that delete when a post or post meta is updated.
+* Fixed notice when `image_scan` was used.
+
**Version 0.6.2**
* Updated the cache to save by post ID instead of a single object.

0 comments on commit 9933b0b

Please sign in to comment.