Get the Image - Version 0.7

Version Description

Download this release

Release Info

Developer greenshady
Plugin Icon 128x128 Get the Image
Version 0.7
Comparing to
See all releases

Code changes from version 0.6.2 to 0.7

Files changed (2) hide show
  1. get-the-image.php +134 -65
  2. readme.txt +9 -3
get-the-image.php CHANGED
@@ -3,7 +3,7 @@
3
  * Plugin Name: Get The Image
4
  * Plugin URI: http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin
5
  * 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.6.2
7
  * Author: Justin Tadlock
8
  * Author URI: http://justintadlock.com
9
  *
@@ -23,21 +23,21 @@
23
  * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24
  *
25
  * @package GetTheImage
26
- * @version 0.6.2
27
  * @author Justin Tadlock <justin@justintadlock.com>
28
- * @copyright Copyright (c) 2008 - 2010, Justin Tadlock
29
  * @link http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin
30
  * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
31
  */
32
 
33
- /* Adds theme support for post images. */
34
  add_theme_support( 'post-thumbnails' );
35
 
36
  /* Delete the cache when a post or post metadata is updated. */
37
- add_action( 'save_post', 'get_the_image_delete_cache' );
38
- add_action( 'deleted_post_meta', 'get_the_image_delete_cache' );
39
- add_action( 'updated_post_meta', 'get_the_image_delete_cache' );
40
- add_action( 'added_post_meta', 'get_the_image_delete_cache' );
41
 
42
  /**
43
  * The main image function for displaying an image. It supports several arguments that allow developers to
@@ -48,7 +48,7 @@ add_action( 'added_post_meta', 'get_the_image_delete_cache' );
48
  * 'image_scan', 'callback', and 'default_image'.
49
  *
50
  * @since 0.1.0
51
- * @global $post The current post's DB object.
52
  * @param array $args Arguments for how to load and display the image.
53
  * @return string|array The HTML for the image. | Image attributes in an array.
54
  */
@@ -113,47 +113,45 @@ function get_the_image( $args = array() ) {
113
 
114
  /* If a custom field key (array) is defined, check for images by custom field. */
115
  if ( !empty( $meta_key ) )
116
- $image = image_by_custom_field( $args );
117
 
118
  /* If no image found and $the_post_thumbnail is set to true, check for a post image (WP feature). */
119
  if ( empty( $image ) && !empty( $the_post_thumbnail ) )
120
- $image = image_by_the_post_thumbnail( $args );
121
 
122
  /* If no image found and $attachment is set to true, check for an image by attachment. */
123
  if ( empty( $image ) && !empty( $attachment ) )
124
- $image = image_by_attachment( $args );
125
 
126
  /* If no image found and $image_scan is set to true, scan the post for images. */
127
  if ( empty( $image ) && !empty( $image_scan ) )
128
- $image = image_by_scan( $args );
129
 
130
- /* If no image found and a callback function was given. */
131
  if ( empty( $image ) && !is_null( $callback ) && function_exists( $callback ) )
132
  $image = call_user_func( $callback, $args );
133
 
134
  /* If no image found and a $default_image is set, get the default image. */
135
  if ( empty( $image ) && !empty( $default_image ) )
136
- $image = image_by_default( $args );
137
 
138
  /* If an image was found. */
139
  if ( !empty( $image ) ) {
140
 
141
- /* Change $image['url'] to $image['src']. */
142
- if ( isset( $image['url'] ) ) // 'url' key deprecated 0.6.2
143
- $image['src'] = $image['url'];
144
-
145
  /* If $meta_key_save was set, save the image to a custom field. */
146
  if ( !empty( $meta_key_save ) )
147
- get_the_image_meta_key_save( $args, $image );
148
 
149
- /* If an image is returned, run it through the display function. */
150
- if ( !empty( $image ) )
151
- $image = display_the_image( $args, $image );
152
- }
153
 
154
- $image_cache[$key] = $image;
155
- wp_cache_set( $post_id, $image_cache, 'get_the_image' );
 
 
156
  }
 
 
157
  else {
158
  $image = $image_cache[$key];
159
  }
@@ -163,17 +161,25 @@ function get_the_image( $args = array() ) {
163
 
164
  /* If $format is set to 'array', return an array of image attributes. */
165
  if ( 'array' == $format ) {
 
 
 
 
 
166
  $atts = wp_kses_hair( $image, array( 'http' ) );
167
 
 
168
  foreach ( $atts as $att )
169
  $out[$att['name']] = $att['value'];
170
 
171
  $out['url'] = $out['src']; // @deprecated 0.5 Use 'src' instead of 'url'.
 
 
172
  return $out;
173
  }
174
 
175
  /* Or, if $echo is set to false, return the formatted image. */
176
- elseif ( empty( $echo ) ) {
177
  return $image;
178
  }
179
 
@@ -187,11 +193,11 @@ function get_the_image( $args = array() ) {
187
  * Calls images by custom field key. Script loops through multiple custom field keys. If that particular key
188
  * is found, $image is set and the loop breaks. If an image is found, it is returned.
189
  *
190
- * @since 0.3.0
191
- * @param array $args
192
- * @return array|bool
193
  */
194
- function image_by_custom_field( $args = array() ) {
195
 
196
  /* If $meta_key is not an array. */
197
  if ( !is_array( $args['meta_key'] ) ) {
@@ -217,7 +223,7 @@ function image_by_custom_field( $args = array() ) {
217
 
218
  /* If a custom key value has been given for one of the keys, return the image URL. */
219
  if ( !empty( $image ) )
220
- return array( 'url' => $image );
221
 
222
  return false;
223
  }
@@ -227,11 +233,11 @@ function image_by_custom_field( $args = array() ) {
227
  * If an image is found, return it and the $post_thumbnail_id. The WordPress function's other filters are
228
  * later added in the display_the_image() function.
229
  *
230
- * @since 0.4.0
231
- * @param array $args
232
- * @return array|bool
233
  */
234
- function image_by_the_post_thumbnail( $args = array() ) {
235
 
236
  /* Check for a post image ID (set by WP as a custom field). */
237
  $post_thumbnail_id = get_post_thumbnail_id( $args['post_id'] );
@@ -250,18 +256,18 @@ function image_by_the_post_thumbnail( $args = array() ) {
250
  $alt = trim( strip_tags( get_post_field( 'post_excerpt', $post_thumbnail_id ) ) );
251
 
252
  /* Return both the image URL and the post thumbnail ID. */
253
- return array( 'url' => $image[0], 'post_thumbnail_id' => $post_thumbnail_id, 'alt' => $alt );
254
  }
255
 
256
  /**
257
  * Check for attachment images. Uses get_children() to check if the post has images attached. If image
258
  * attachments are found, loop through each. The loop only breaks once $order_of_image is reached.
259
  *
260
- * @since 0.3.0
261
- * @param array $args
262
- * @return array|bool
263
  */
264
- function image_by_attachment( $args = array() ) {
265
 
266
  /* Get attachments for the inputted $post_id. */
267
  $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() ) {
291
  }
292
 
293
  /* Return the image URL. */
294
- return array( 'url' => $image[0], 'alt' => $alt );
295
  }
296
 
297
  /**
298
  * Scans the post for images within the content. Not called by default with get_the_image(). Shouldn't use
299
  * if using large images within posts, better to use the other options.
300
  *
301
- * @since 0.3.0
302
- * @global $post The current post's DB object.
303
- * @param array $args
304
- * @return array|bool
305
  */
306
- function image_by_scan( $args = array() ) {
307
 
308
  /* Search the post's content for the <img /> tag and get its URL. */
309
  preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i', get_post_field( 'post_content', $args['post_id'] ), $matches );
310
 
311
  /* If there is a match for the image, return its URL. */
312
- if ( isset( $matches ) && $matches[1][0] )
313
- return array( 'url' => $matches[1][0] );
314
 
315
  return false;
316
  }
@@ -319,24 +324,24 @@ function image_by_scan( $args = array() ) {
319
  * Used for setting a default image. The function simply returns the image URL it was given in an array.
320
  * Not used with get_the_image() by default.
321
  *
322
- * @since 0.3.0
323
- * @param array $args
324
- * @return array
325
  */
326
- function image_by_default( $args = array() ) {
327
- return array( 'url' => $args['default_image'] );
328
  }
329
 
330
  /**
331
  * Formats an image with appropriate alt text and class. Adds a link to the post if argument is set. Should
332
  * only be called if there is an image to display, but will handle it if not.
333
  *
334
- * @since 0.1.0
335
- * @param array $args
336
- * @param array $image Array of image info ($image, $classes, $alt, $caption).
337
  * @return string $image Formatted image (w/link to post if the option is set).
338
  */
339
- function display_the_image( $args = array(), $image = false ) {
340
 
341
  /* If there is no image URL, return false. */
342
  if ( empty( $image['src'] ) )
@@ -393,8 +398,8 @@ function display_the_image( $args = array(), $image = false ) {
393
  * of expensive scans of the content when using the image scan feature.
394
  *
395
  * @since 0.6.0
396
- * @param array $args Parameters for what image to get.
397
- * @param array $image Array of image info ($image, $classes, $alt, $caption).
398
  */
399
  function get_the_image_meta_key_save( $args = array(), $image = array() ) {
400
 
@@ -415,17 +420,25 @@ function get_the_image_meta_key_save( $args = array(), $image = array() ) {
415
  }
416
 
417
  /**
418
- * Deletes the image cache for users that are using a persistent-caching plugin.
419
  *
420
- * @since 0.5.0
421
  */
422
- function get_the_image_delete_cache() {
423
- wp_cache_delete( 'get_the_image' );
424
  }
425
 
426
  /**
427
- * Get the image with a link to the post. Use get_the_image() instead.
 
428
  *
 
 
 
 
 
 
 
429
  * @since 0.1.0
430
  * @deprecated 0.3.0
431
  */
@@ -433,4 +446,60 @@ function get_the_image_link( $deprecated = '', $deprecated_2 = '', $deprecated_3
433
  get_the_image();
434
  }
435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436
  ?>
3
  * Plugin Name: Get The Image
4
  * Plugin URI: http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin
5
  * 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
7
  * Author: Justin Tadlock
8
  * Author URI: http://justintadlock.com
9
  *
23
  * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24
  *
25
  * @package GetTheImage
26
+ * @version 0.7.0
27
  * @author Justin Tadlock <justin@justintadlock.com>
28
+ * @copyright Copyright (c) 2008 - 2011, Justin Tadlock
29
  * @link http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin
30
  * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
31
  */
32
 
33
+ /* Adds theme support for WordPress 'featured images'. */
34
  add_theme_support( 'post-thumbnails' );
35
 
36
  /* Delete the cache when a post or post metadata is updated. */
37
+ add_action( 'save_post', 'get_the_image_delete_cache_by_post' );
38
+ add_action( 'deleted_post_meta', 'get_the_image_delete_cache_by_meta', 10, 2 );
39
+ add_action( 'updated_post_meta', 'get_the_image_delete_cache_by_meta', 10, 2 );
40
+ add_action( 'added_post_meta', 'get_the_image_delete_cache_by_meta', 10, 2 );
41
 
42
  /**
43
  * The main image function for displaying an image. It supports several arguments that allow developers to
48
  * 'image_scan', 'callback', and 'default_image'.
49
  *
50
  * @since 0.1.0
51
+ * @global $post The current post's database object.
52
  * @param array $args Arguments for how to load and display the image.
53
  * @return string|array The HTML for the image. | Image attributes in an array.
54
  */
113
 
114
  /* If a custom field key (array) is defined, check for images by custom field. */
115
  if ( !empty( $meta_key ) )
116
+ $image = get_the_image_by_meta_key( $args );
117
 
118
  /* If no image found and $the_post_thumbnail is set to true, check for a post image (WP feature). */
119
  if ( empty( $image ) && !empty( $the_post_thumbnail ) )
120
+ $image = get_the_image_by_post_thumbnail( $args );
121
 
122
  /* If no image found and $attachment is set to true, check for an image by attachment. */
123
  if ( empty( $image ) && !empty( $attachment ) )
124
+ $image = get_the_image_by_attachment( $args );
125
 
126
  /* If no image found and $image_scan is set to true, scan the post for images. */
127
  if ( empty( $image ) && !empty( $image_scan ) )
128
+ $image = get_the_image_by_scan( $args );
129
 
130
+ /* If no image found and a callback function was given. Callback function must pass back array of <img> attributes. */
131
  if ( empty( $image ) && !is_null( $callback ) && function_exists( $callback ) )
132
  $image = call_user_func( $callback, $args );
133
 
134
  /* If no image found and a $default_image is set, get the default image. */
135
  if ( empty( $image ) && !empty( $default_image ) )
136
+ $image = get_the_image_by_default( $args );
137
 
138
  /* If an image was found. */
139
  if ( !empty( $image ) ) {
140
 
 
 
 
 
141
  /* If $meta_key_save was set, save the image to a custom field. */
142
  if ( !empty( $meta_key_save ) )
143
+ get_the_image_meta_key_save( $args, $image['src'] );
144
 
145
+ /* Format the image HTML. */
146
+ $image = get_the_image_format( $args, $image );
 
 
147
 
148
+ /* Set the image cache for the specific post. */
149
+ $image_cache[$key] = $image;
150
+ wp_cache_set( $post_id, $image_cache, 'get_the_image' );
151
+ }
152
  }
153
+
154
+ /* If an image was already cached for the post and arguments, use it. */
155
  else {
156
  $image = $image_cache[$key];
157
  }
161
 
162
  /* If $format is set to 'array', return an array of image attributes. */
163
  if ( 'array' == $format ) {
164
+
165
+ /* Set up a default empty array. */
166
+ $out = array();
167
+
168
+ /* Get the image attributes. */
169
  $atts = wp_kses_hair( $image, array( 'http' ) );
170
 
171
+ /* Loop through the image attributes and add them in key/value pairs for the return array. */
172
  foreach ( $atts as $att )
173
  $out[$att['name']] = $att['value'];
174
 
175
  $out['url'] = $out['src']; // @deprecated 0.5 Use 'src' instead of 'url'.
176
+
177
+ /* Return the array of attributes. */
178
  return $out;
179
  }
180
 
181
  /* Or, if $echo is set to false, return the formatted image. */
182
+ elseif ( false === $echo ) {
183
  return $image;
184
  }
185
 
193
  * Calls images by custom field key. Script loops through multiple custom field keys. If that particular key
194
  * is found, $image is set and the loop breaks. If an image is found, it is returned.
195
  *
196
+ * @since 0.7.0
197
+ * @param array $args Arguments for how to load and display the image.
198
+ * @return array|bool Array of image attributes. | False if no image is found.
199
  */
200
+ function get_the_image_by_meta_key( $args = array() ) {
201
 
202
  /* If $meta_key is not an array. */
203
  if ( !is_array( $args['meta_key'] ) ) {
223
 
224
  /* If a custom key value has been given for one of the keys, return the image URL. */
225
  if ( !empty( $image ) )
226
+ return array( 'src' => $image );
227
 
228
  return false;
229
  }
233
  * If an image is found, return it and the $post_thumbnail_id. The WordPress function's other filters are
234
  * later added in the display_the_image() function.
235
  *
236
+ * @since 0.7.0
237
+ * @param array $args Arguments for how to load and display the image.
238
+ * @return array|bool Array of image attributes. | False if no image is found.
239
  */
240
+ function get_the_image_by_post_thumbnail( $args = array() ) {
241
 
242
  /* Check for a post image ID (set by WP as a custom field). */
243
  $post_thumbnail_id = get_post_thumbnail_id( $args['post_id'] );
256
  $alt = trim( strip_tags( get_post_field( 'post_excerpt', $post_thumbnail_id ) ) );
257
 
258
  /* Return both the image URL and the post thumbnail ID. */
259
+ return array( 'src' => $image[0], 'post_thumbnail_id' => $post_thumbnail_id, 'alt' => $alt );
260
  }
261
 
262
  /**
263
  * Check for attachment images. Uses get_children() to check if the post has images attached. If image
264
  * attachments are found, loop through each. The loop only breaks once $order_of_image is reached.
265
  *
266
+ * @since 0.7.0
267
+ * @param array $args Arguments for how to load and display the image.
268
+ * @return array|bool Array of image attributes. | False if no image is found.
269
  */
270
+ function get_the_image_by_attachment( $args = array() ) {
271
 
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' ) );
297
  }
298
 
299
  /* Return the image URL. */
300
+ return array( 'src' => $image[0], 'alt' => $alt );
301
  }
302
 
303
  /**
304
  * Scans the post for images within the content. Not called by default with get_the_image(). Shouldn't use
305
  * if using large images within posts, better to use the other options.
306
  *
307
+ * @since 0.7.0
308
+ * @param array $args Arguments for how to load and display the image.
309
+ * @return array|bool Array of image attributes. | False if no image is found.
 
310
  */
311
+ function get_the_image_by_scan( $args = array() ) {
312
 
313
  /* Search the post's content for the <img /> tag and get its URL. */
314
  preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i', get_post_field( 'post_content', $args['post_id'] ), $matches );
315
 
316
  /* If there is a match for the image, return its URL. */
317
+ if ( isset( $matches ) && !empty( $matches[1][0] ) )
318
+ return array( 'src' => $matches[1][0] );
319
 
320
  return false;
321
  }
324
  * Used for setting a default image. The function simply returns the image URL it was given in an array.
325
  * Not used with get_the_image() by default.
326
  *
327
+ * @since 0.7.0
328
+ * @param array $args Arguments for how to load and display the image.
329
+ * @return array|bool Array of image attributes. | False if no image is found.
330
  */
331
+ function get_the_image_by_default( $args = array() ) {
332
+ return array( 'src' => $args['default_image'] );
333
  }
334
 
335
  /**
336
  * Formats an image with appropriate alt text and class. Adds a link to the post if argument is set. Should
337
  * only be called if there is an image to display, but will handle it if not.
338
  *
339
+ * @since 0.7.0
340
+ * @param array $args Arguments for how to load and display the image.
341
+ * @param array $image Array of image attributes ($image, $classes, $alt, $caption).
342
  * @return string $image Formatted image (w/link to post if the option is set).
343
  */
344
+ function get_the_image_format( $args = array(), $image = false ) {
345
 
346
  /* If there is no image URL, return false. */
347
  if ( empty( $image['src'] ) )
398
  * of expensive scans of the content when using the image scan feature.
399
  *
400
  * @since 0.6.0
401
+ * @param array $args Arguments for how to load and display the image.
402
+ * @param array $image Array of image attributes ($image, $classes, $alt, $caption).
403
  */
404
  function get_the_image_meta_key_save( $args = array(), $image = array() ) {
405
 
420
  }
421
 
422
  /**
423
+ * Deletes the image cache for the specific post when the 'save_post' hook is fired.
424
  *
425
+ * @since 0.7.0
426
  */
427
+ function get_the_image_delete_cache_by_post( $post_id ) {
428
+ wp_cache_delete( $post_id, 'get_the_image' );
429
  }
430
 
431
  /**
432
+ * Deletes the image cache for a specific post when the 'added_post_meta', 'deleted_post_meta',
433
+ * or 'updated_post_meta' hooks are called.
434
  *
435
+ * @since 0.7.0
436
+ */
437
+ function get_the_image_delete_cache_by_meta( $meta_id, $post_id ) {
438
+ wp_cache_delete( $post_id, 'get_the_image' );
439
+ }
440
+
441
+ /**
442
  * @since 0.1.0
443
  * @deprecated 0.3.0
444
  */
446
  get_the_image();
447
  }
448
 
449
+ /**
450
+ * @since 0.3.0
451
+ * @deprecated 0.7.0
452
+ */
453
+ function image_by_custom_field( $args = array() ) {
454
+ return get_the_image_by_meta_key( $args );
455
+ }
456
+
457
+ /**
458
+ * @since 0.4.0
459
+ * @deprecated 0.7.0
460
+ */
461
+ function image_by_the_post_thumbnail( $args = array() ) {
462
+ return get_the_image_by_post_thumbnail( $args );
463
+ }
464
+
465
+ /**
466
+ * @since 0.3.0
467
+ * @deprecated 0.7.0
468
+ */
469
+ function image_by_attachment( $args = array() ) {
470
+ return get_the_image_by_attachment( $args );
471
+ }
472
+
473
+ /**
474
+ * @since 0.3.0
475
+ * @deprecated 0.7.0
476
+ */
477
+ function image_by_scan( $args = array() ) {
478
+ return get_the_image_by_scan( $args );
479
+ }
480
+
481
+ /**
482
+ * @since 0.3.0
483
+ * @deprecated 0.7.0
484
+ */
485
+ function image_by_default( $args = array() ) {
486
+ return get_the_image_by_default( $args );
487
+ }
488
+
489
+ /**
490
+ * @since 0.1.0
491
+ * @deprecated 0.7.0
492
+ */
493
+ function display_the_image( $args = array(), $image = false ) {
494
+ return get_the_image_format( $args, $image );
495
+ }
496
+
497
+ /**
498
+ * @since 0.5.0
499
+ * @deprecated 0.7.0 Replaced by cache delete functions specifically for the post ID.
500
+ */
501
+ function get_the_image_delete_cache() {
502
+ return;
503
+ }
504
+
505
  ?>
readme.txt CHANGED
@@ -2,9 +2,9 @@
2
  Contributors: greenshady
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=3687060
4
  Tags: image, images, thumbnail
5
- Requires at least: 2.9
6
- Tested up to: 3.1.1
7
- Stable tag: 0.6.2
8
 
9
  An easy-to-use image script for adding things such as thumbnails and feature images.
10
 
@@ -56,6 +56,12 @@ You can view this plugin in action on my <a href="http://justintadlock.com" titl
56
 
57
  == Changelog ==
58
 
 
 
 
 
 
 
59
  **Version 0.6.2**
60
 
61
  * Updated the cache to save by post ID instead of a single object.
2
  Contributors: greenshady
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=3687060
4
  Tags: image, images, thumbnail
5
+ Requires at least: 3.2
6
+ Tested up to: 3.2.1
7
+ Stable tag: 0.7
8
 
9
  An easy-to-use image script for adding things such as thumbnails and feature images.
10
 
56
 
57
  == Changelog ==
58
 
59
+ **Version 0.7**
60
+
61
+ * Deprecated and replaced functions that lacked the `get_the_image_` prefix.
62
+ * New cache delete functions that delete when a post or post meta is updated.
63
+ * Fixed notice when `image_scan` was used.
64
+
65
  **Version 0.6.2**
66
 
67
  * Updated the cache to save by post ID instead of a single object.