Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 940 lines (758 sloc) 31.114 kB
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
1 <?php
2 /**
3 * Plugin Name: Get The Image
8db0e0f @justintadlock Plugin header formatting.
authored
4 * Plugin URI: http://themehybrid.com/plugins/get-the-image
9e4b3a1 @justintadlock Plugin description updates.
authored
5 * Description: This is a highly intuitive script that can grab an image by custom field, featured image, post attachment, or extracting it from the post's content.
426b5f9 @justintadlock Version bump to 1.0.1.
authored
6 * Version: 1.0.1
8db0e0f @justintadlock Plugin header formatting.
authored
7 * Author: Justin Tadlock
8 * Author URI: http://justintadlock.com
74ab31b @justintadlock Porting changes from version 0.9.0 of the Get the Image script into t…
authored
9 */
10
11 /**
7d49016 Bringing this up to 0.6.1 standards.
greenshady authored
12 * Get the Image - An advanced post image script for WordPress.
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
13 *
7d49016 Bringing this up to 0.6.1 standards.
greenshady authored
14 * Get the Image was created to be a highly-intuitive image script that displays post-specific images (an
15 * image-based representation of a post). The script handles old-style post images via custom fields for
16 * backwards compatibility. It also supports WordPress' built-in featured image functionality. On top of
17 * those things, it can automatically set attachment images as the post image or scan the post content for
18 * the first image element used. It can also fall back to a given default image.
19 *
20 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
be3f6a3 @justintadlock Bring in the changes from the 0.8.1 version of the Get the Image exte…
authored
21 * General Public License as published by the Free Software Foundation; either version 2 of the License,
22 * or (at your option) any later version.
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
23 *
7d49016 Bringing this up to 0.6.1 standards.
greenshady authored
24 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
25 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
26 *
be3f6a3 @justintadlock Bring in the changes from the 0.8.1 version of the Get the Image exte…
authored
27 * @package GetTheImage
426b5f9 @justintadlock Version bump to 1.0.1.
authored
28 * @version 1.0.1
be3f6a3 @justintadlock Bring in the changes from the 0.8.1 version of the Get the Image exte…
authored
29 * @author Justin Tadlock <justin@justintadlock.com>
8d49584 @justintadlock Version bump to 1.0.0
authored
30 * @copyright Copyright (c) 2008 - 2014, Justin Tadlock
be3f6a3 @justintadlock Bring in the changes from the 0.8.1 version of the Get the Image exte…
authored
31 * @link http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin
32 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
33 */
34
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
35 /* Adds theme support for WordPress 'featured images'. */
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
36 add_theme_support( 'post-thumbnails' );
37
ca5ff9c Bringing code up to version 0.5.
greenshady authored
38 /* Delete the cache when a post or post metadata is updated. */
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
39 add_action( 'save_post', 'get_the_image_delete_cache_by_post' );
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
40 add_action( 'deleted_post_meta', 'get_the_image_delete_cache_by_meta', 10, 2 );
41 add_action( 'updated_post_meta', 'get_the_image_delete_cache_by_meta', 10, 2 );
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
42 add_action( 'added_post_meta', 'get_the_image_delete_cache_by_meta', 10, 2 );
ca5ff9c Bringing code up to version 0.5.
greenshady authored
43
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
44 /**
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
45 * The main image function for displaying an image. This is a wrapper for the Get_The_Image class. Use this
46 * function in themes rather than the class.
7d49016 Bringing this up to 0.6.1 standards.
greenshady authored
47 *
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
48 * @since 0.1.0
8d6c873 @justintadlock Bringing plugin up to date with version 0.8 of the Get the Image script.
authored
49 * @access public
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
50 * @param array $args Arguments for how to load and display the image.
51 * @return string|array The HTML for the image. | Image attributes in an array.
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
52 */
53 function get_the_image( $args = array() ) {
54
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
55 $image = new Get_The_Image( $args );
56
0a7b68f @justintadlock Make sure get_the_image() returns the value.
authored
57 return $image->get_image();
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
58 }
59
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
60
61 /* === Internal Plugin Code: Don't use the below unless you know what you're doing. Expect breakage. === */
62
63
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
64 /**
65 * Class for getting images related to a post. Only use this class in your projects if you absolutely know
66 * what you're doing and expect your code to break in future versions. Use the the `get_the_image()`
67 * wrapper function instead. That's the reason it exists.
68 *
69 * @since 1.0.0
70 * @access private
71 */
72 final class Get_The_Image {
73
74 /**
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
75 * Array of arguments passed in by the user and merged with the defaults.
76 *
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
77 * @since 1.0.0
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
78 * @access public
79 * @var array
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
80 */
81 public $args = array();
82
83 /**
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
84 * Image arguments array filled by the class. This is used to store data about the image (src,
85 * width, height, etc.). In some scenarios, it may not be set, particularly when getting the
86 * raw image HTML.
87 *
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
88 * @since 1.0.0
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
89 * @access public
90 * @var array
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
91 */
92 public $image_args = array();
93
94 /**
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
95 * The image HTML to output.
96 *
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
97 * @since 1.0.0
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
98 * @access public
99 * @var string
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
100 */
101 public $image = '';
102
103 /**
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
104 * Original image HTML. This is set when splitting an image from the content. By default, this
105 * is only used when 'scan_raw' is set.
106 *
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
107 * @since 1.0.0
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
108 * @access public
109 * @var array
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
110 */
111 public $original_image = '';
112
113 /**
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
114 * Constructor method. This sets up and runs the show.
115 *
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
116 * @since 1.0.0
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
117 * @access public
118 * @param array $args
119 * @return void
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
120 */
121 public function __construct( $args = array() ) {
7769732 @justintadlock Add support for WP embeds with the 'scan' and 'scan_raw' options. Th…
authored
122 global $wp_embed;
123
124 /* Use WP's embed functionality to handle the [embed] shortcode and autoembeds. */
125 add_filter( 'get_the_image_post_content', array( $wp_embed, 'run_shortcode' ) );
126 add_filter( 'get_the_image_post_content', array( $wp_embed, 'autoembed' ) );
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
127
128 /* Set the default arguments. */
129 $defaults = array(
130
131 /* Post the image is associated with. */
132 'post_id' => get_the_ID(),
133
134 /* Method order (see methods below). */
135 'order' => array( 'meta_key', 'featured', 'attachment', 'scan', 'scan_raw', 'callback', 'default' ),
136
137 /* Methods of getting an image (in order). */
138 'meta_key' => array( 'Thumbnail', 'thumbnail' ), // array|string
139 'featured' => true,
140 'attachment' => true,
141 'scan' => false,
142 'scan_raw' => false, // Note: don't use the array format option with this.
143 'callback' => null,
144 'default' => false,
145
146 /* Split image from post content (by default, only used with the 'scan_raw' option). */
147 'split_content' => false,
148
149 /* Attachment-specific arguments. */
42d7413 @justintadlock Use the new has_image_size() introduced in WP 3.9 to check for the 'p…
authored
150 'size' => has_image_size( 'post-thumbnail' ) ? 'post-thumbnail' : 'thumbnail',
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
151
152 /* Format/display of image. */
153 'link_to_post' => true,
154 'image_class' => false,
155 'width' => false,
156 'height' => false,
157 'before' => '',
158 'after' => '',
159
4250072 @justintadlock Add 'min_width' and 'min_height' arguments to allow devs to only use …
authored
160 /* Minimum allowed sizes. */
161 'min_width' => 0,
162 'min_height' => 0,
163
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
164 /* Captions. */
165 'caption' => false, // Default WP [caption] requires a width.
166
167 /* Saving the image. */
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
168 'meta_key_save' => false, // Save as metadata (string).
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
169 'thumbnail_id_save' => false, // Set 'featured image'.
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
170 'cache' => true, // Cache the image.
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
171
172 /* Return/echo image. */
173 'format' => 'img',
174 'echo' => true,
175
176 /* Deprecated arguments. */
177 'custom_key' => null, // @deprecated 0.6.0 Use 'meta_key'.
178 'default_size' => null, // @deprecated 0.5.0 Use 'size'.
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
179 'the_post_thumbnail' => null, // @deprecated 1.0.0 Use 'featured'.
180 'image_scan' => null, // @deprecated 1.0.0 Use 'scan' or 'scan_raw'.
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
181 'default_image' => null, // @deprecated 1.0.0 Use 'default'.
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
182 'order_of_image' => null, // @deprecated 1.0.0 No replacement.
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
183 );
184
185 /* Allow plugins/themes to filter the arguments. */
9516e9c @justintadlock $args should be $this->args. Merge a that bit of code too.
authored
186 $this->args = apply_filters(
187 'get_the_image_args',
8e78e30 @justintadlock Use $args instead of $this->args.
authored
188 wp_parse_args( $args, $defaults )
9516e9c @justintadlock $args should be $this->args. Merge a that bit of code too.
authored
189 );
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
190
191 /* If no post ID, return. */
192 if ( empty( $this->args['post_id'] ) )
193 return false;
194
195 /* === Handle deprecated arguments. === */
196
197 /* If $default_size is given, overwrite $size. */
198 if ( !is_null( $this->args['default_size'] ) )
199 $this->args['size'] = $this->args['default_size'];
200
201 /* If $custom_key is set, overwrite $meta_key. */
202 if ( !is_null( $this->args['custom_key'] ) )
203 $this->args['meta_key'] = $this->args['custom_key'];
204
205 /* If 'the_post_thumbnail' is set, overwrite 'featured'. */
206 if ( !is_null( $this->args['the_post_thumbnail'] ) )
207 $this->args['featured'] = $this->args['the_post_thumbnail'];
208
209 /* If 'image_scan' is set, overwrite 'scan'. */
210 if ( !is_null( $this->args['image_scan'] ) )
211 $this->args['scan'] = $this->args['image_scan'];
212
213 /* If 'default_image' is set, overwrite 'default'. */
214 if ( !is_null( $this->args['default_image'] ) )
215 $this->args['default'] = $this->args['default_image'];
74ab31b @justintadlock Porting changes from version 0.9.0 of the Get the Image script into t…
authored
216
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
217 /* === End deprecated arguments. === */
74ab31b @justintadlock Porting changes from version 0.9.0 of the Get the Image script into t…
authored
218
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
219 /* If $format is set to 'array', don't link to the post. */
220 if ( 'array' == $this->args['format'] )
221 $this->args['link_to_post'] = false;
74ab31b @justintadlock Porting changes from version 0.9.0 of the Get the Image script into t…
authored
222
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
223 /* Find images. */
224 $this->find();
74ab31b @justintadlock Porting changes from version 0.9.0 of the Get the Image script into t…
authored
225
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
226 /* Only used if $original_image is set. */
227 if ( true === $this->args['split_content'] && !empty( $this->original_image ) )
b5d8779 @justintadlock Change priority of 'split_content' filter. The later priority wasn't …
authored
228 add_filter( 'the_content', array( $this, 'split_content' ), 9 );
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
229 }
74ab31b @justintadlock Porting changes from version 0.9.0 of the Get the Image script into t…
authored
230
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
231 /**
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
232 * Returns the image HTML or image array.
233 *
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
234 * @since 1.0.0
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
235 * @access public
236 * @return void
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
237 */
238 public function get_image() {
74ab31b @justintadlock Porting changes from version 0.9.0 of the Get the Image script into t…
authored
239
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
240 /* Allow plugins/theme to override the final output. */
241 $image_html = apply_filters( 'get_the_image', $this->image );
74ab31b @justintadlock Porting changes from version 0.9.0 of the Get the Image script into t…
authored
242
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
243 /* If $format is set to 'array', return an array of image attributes. */
244 if ( 'array' === $this->args['format'] ) {
74ab31b @justintadlock Porting changes from version 0.9.0 of the Get the Image script into t…
authored
245
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
246 /* Set up a default empty array. */
247 $out = array();
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
248
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
249 /* Get the image attributes. */
250 $atts = wp_kses_hair( $image_html, array( 'http', 'https' ) );
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
251
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
252 /* Loop through the image attributes and add them in key/value pairs for the return array. */
253 foreach ( $atts as $att )
254 $out[ $att['name'] ] = $att['value'];
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
255
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
256 /* Return the array of attributes. */
257 return $out;
258 }
74ab31b @justintadlock Porting changes from version 0.9.0 of the Get the Image script into t…
authored
259
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
260 /* Or, if $echo is set to false, return the formatted image. */
261 elseif ( false === $this->args['echo'] ) {
262 return !empty( $image_html ) ? $this->args['before'] . $image_html . $this->args['after'] : $image_html;
263 }
ecc26c2 Deprecated custom_key in favor of meta_key.
greenshady authored
264
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
265 /* If there is a $post_thumbnail_id, do the actions associated with get_the_post_thumbnail(). */
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
266 if ( isset( $this->image_args['post_thumbnail_id'] ) )
6d78739 @justintadlock $this->arg should be $this->args.
authored
267 do_action( 'begin_fetch_post_thumbnail_html', $this->args['post_id'], $this->image_args['post_thumbnail_id'], $this->args['size'] );
ca5ff9c Bringing code up to version 0.5.
greenshady authored
268
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
269 /* Display the image if we get to this point. */
270 echo !empty( $image_html ) ? $this->args['before'] . $image_html . $this->args['after'] : $image_html;
271
272 /* If there is a $post_thumbnail_id, do the actions associated with get_the_post_thumbnail(). */
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
273 if ( isset( $this->image_args['post_thumbnail_id'] ) )
6f01f81 @justintadlock $image should be $this->image_args.
authored
274 do_action( 'end_fetch_post_thumbnail_html', $this->args['post_id'], $this->image_args['post_thumbnail_id'], $this->args['size'] );
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
275 }
ca5ff9c Bringing code up to version 0.5.
greenshady authored
276
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
277 /**
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
278 * Figures out if we have an image related to the post. Runs through the various methods of getting
279 * an image. If there's a cached image, we'll just use that.
280 *
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
281 * @since 1.0.0
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
282 * @access public
283 * @return void
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
284 */
285 public function find() {
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
286
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
287 /* Get cache key based on $this->args. */
288 $key = md5( serialize( compact( array_keys( $this->args ) ) ) );
bd4cc24 Bringing plugin up to version 0.6.2.
greenshady authored
289
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
290 /* Check for a cached image. */
291 $image_cache = wp_cache_get( $this->args['post_id'], 'get_the_image' );
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
292
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
293 if ( !is_array( $image_cache ) )
294 $image_cache = array();
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
295
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
296 /* If there is no cached image, let's see if one exists. */
297 if ( !isset( $image_cache[ $key ] ) || empty( $cache ) ) {
8d6c873 @justintadlock Bringing plugin up to date with version 0.8 of the Get the Image script.
authored
298
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
299 foreach ( $this->args['order'] as $method ) {
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
300
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
301 if ( !empty( $this->image ) || !empty( $this->image_args ) )
302 break;
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
303
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
304 if ( 'meta_key' === $method && !empty( $this->args['meta_key'] ) )
305 $this->get_meta_key_image();
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
306
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
307 elseif ( 'featured' === $method && true === $this->args['featured'] )
308 $this->get_featured_image();
ca5ff9c Bringing code up to version 0.5.
greenshady authored
309
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
310 elseif ( 'attachment' === $method && true === $this->args['attachment'] )
311 $this->get_attachment_image();
ca5ff9c Bringing code up to version 0.5.
greenshady authored
312
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
313 elseif ( 'scan' === $method && true === $this->args['scan'] )
314 $this->get_scan_image();
ecc26c2 Deprecated custom_key in favor of meta_key.
greenshady authored
315
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
316 elseif ( 'scan_raw' === $method && true === $this->args['scan_raw'])
317 $this->get_scan_raw_image();
ca5ff9c Bringing code up to version 0.5.
greenshady authored
318
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
319 elseif ( 'callback' === $method && !is_null( $this->args['callback'] ) )
320 $this->get_callback_image();
bd4cc24 Bringing plugin up to version 0.6.2.
greenshady authored
321
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
322 elseif ( 'default' === $method && !empty( $this->args['default'] ) )
323 $this->get_default_image();
324 }
bd4cc24 Bringing plugin up to version 0.6.2.
greenshady authored
325
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
326 /* Format the image HTML. */
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
327 if ( empty( $this->image ) && !empty( $this->image_args ) )
328 $this->format_image();
329
330 /* If we have image HTML. */
331 if ( !empty( $this->image ) ) {
ca5ff9c Bringing code up to version 0.5.
greenshady authored
332
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
333 /* Save the image as metadata. */
334 if ( !empty( $this->args['meta_key_save'] ) )
335 $this->meta_key_save();
336
337 /* Set the image cache for the specific post. */
338 $image_cache[ $key ] = $this->image;
339 wp_cache_set( $this->args['post_id'], $image_cache, 'get_the_image' );
340 }
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
341 }
342
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
343 /* If an image was already cached for the post and arguments, use it. */
344 else {
345 $this->image = $image_cache[ $key ];
346 }
ca5ff9c Bringing code up to version 0.5.
greenshady authored
347 }
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
348
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
349 /**
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
350 * Gets a image by post meta key.
351 *
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
352 * @since 1.0.0
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
353 * @access public
354 * @return void
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
355 */
356 public function get_meta_key_image() {
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
357
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
358 /* If $meta_key is not an array. */
359 if ( !is_array( $this->args['meta_key'] ) )
360 $this->args['meta_key'] = array( $this->args['meta_key'] );
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
361
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
362 /* Loop through each of the given meta keys. */
363 foreach ( $this->args['meta_key'] as $meta_key ) {
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
364
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
365 /* Get the image URL by the current meta key in the loop. */
366 $image = get_post_meta( $this->args['post_id'], $meta_key, true );
ca5ff9c Bringing code up to version 0.5.
greenshady authored
367
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
368 /* If an image was found, break out of the loop. */
369 if ( !empty( $image ) )
370 break;
371 }
ca5ff9c Bringing code up to version 0.5.
greenshady authored
372
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
373 /* If there's an image and it is numeric, assume it is an attachment ID. */
374 if ( !empty( $image ) && is_numeric( $image ) )
375 $this->_get_image_attachment( absint( $image ) );
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
376
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
377 /* Else, assume the image is a file URL. */
378 elseif ( !empty( $image ) )
379 $this->image_args = array( 'src' => $image );
ca5ff9c Bringing code up to version 0.5.
greenshady authored
380 }
bd4cc24 Bringing plugin up to version 0.6.2.
greenshady authored
381
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
382 /**
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
383 * Gets the featured image (i.e., WP's post thumbnail).
384 *
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
385 * @since 1.0.0
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
386 * @access public
387 * @return void
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
388 */
389 public function get_featured_image() {
390
391 /* Check for a post image ID (set by WP as a custom field). */
392 $post_thumbnail_id = get_post_thumbnail_id( $this->args['post_id'] );
393
394 /* If no post image ID is found, return. */
395 if ( empty( $post_thumbnail_id ) )
396 return;
397
398 /* Apply filters on post_thumbnail_size because this is a default WP filter used with its image feature. */
399 $this->args['size'] = apply_filters( 'post_thumbnail_size', $this->args['size'] );
400
401 /* Set the image args. */
402 $this->_get_image_attachment( $post_thumbnail_id );
403
404 /* Add the post thumbnail ID. */
405 $this->image_args['post_thumbnail_id'] = $post_thumbnail_id;
bd4cc24 Bringing plugin up to version 0.6.2.
greenshady authored
406 }
407
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
408 /**
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
409 * Gets the first image attached to the post. If the post itself is an attachment image, that will
410 * be the image used. This method also works with sub-attachments (images for audio/video attachments
411 * are a good example).
412 *
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
413 * @since 1.0.0
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
414 * @access public
415 * @return void
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
416 */
417 public function get_attachment_image() {
418
419 /* Check if the post itself is an image attachment. */
420 if ( wp_attachment_is_image( $this->args['post_id'] ) ) {
421 $attachment_id = $this->args['post_id'];
422 }
8d6c873 @justintadlock Bringing plugin up to date with version 0.8 of the Get the Image script.
authored
423
6085296 @justintadlock Just grab attachment IDs instead of objects.
authored
424 /* If the post is not an image attachment, check if it has any image attachments. */
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
425 else {
426
427 /* Get attachments for the inputted $post_id. */
428 $attachments = get_children(
429 array(
6085296 @justintadlock Just grab attachment IDs instead of objects.
authored
430 'numberposts' => 1,
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
431 'post_parent' => $this->args['post_id'],
432 'post_status' => 'inherit',
433 'post_type' => 'attachment',
434 'post_mime_type' => 'image',
435 'order' => 'ASC',
436 'orderby' => 'menu_order ID',
6085296 @justintadlock Just grab attachment IDs instead of objects.
authored
437 'fields' => 'ids'
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
438 )
439 );
440
441 /* Check if any attachments were found. */
6085296 @justintadlock Just grab attachment IDs instead of objects.
authored
442 if ( !empty( $attachments ) )
443 $attachment_id = array_shift( $attachments );
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
444 }
8d6c873 @justintadlock Bringing plugin up to date with version 0.8 of the Get the Image script.
authored
445
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
446 if ( !empty( $attachment_id ) )
447 $this->_get_image_attachment( $attachment_id );
448 }
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
449
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
450 /**
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
451 * Scans the post content for an image. It first scans and checks for an image with the
452 * "wp-image-xxx" ID. If that exists, it'll grab the actual image attachment. If not, it looks
453 * for the image source.
454 *
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
455 * @since 1.0.0
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
456 * @access public
457 * @return void
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
458 */
459 public function get_scan_image() {
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
460
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
461 /* Get the post content. */
ea8ba8d @justintadlock Corrections in get_scan_image().
authored
462 $post_content = get_post_field( 'post_content', $this->args['post_id'] );
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
463
7769732 @justintadlock Add support for WP embeds with the 'scan' and 'scan_raw' options. Th…
authored
464 /* Apply filters to content. */
465 $post_content = apply_filters( 'get_the_image_post_content', $post_content );
466
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
467 /* Check the content for `id="wp-image-%d"`. */
ea8ba8d @justintadlock Corrections in get_scan_image().
authored
468 preg_match( '/id=[\'"]wp-image-([\d]*)[\'"]/i', $post_content, $image_ids );
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
469
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
470 /* Loop through any found image IDs. */
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
471 if ( is_array( $image_ids ) ) {
7d49016 Bringing this up to 0.6.1 standards.
greenshady authored
472
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
473 foreach ( $image_ids as $image_id ) {
474 $this->_get_image_attachment( $image_id );
7d49016 Bringing this up to 0.6.1 standards.
greenshady authored
475
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
476 if ( !empty( $this->image_args ) )
ea8ba8d @justintadlock Corrections in get_scan_image().
authored
477 return;
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
478 }
479 }
7d49016 Bringing this up to 0.6.1 standards.
greenshady authored
480
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
481 /* Search the post's content for the <img /> tag and get its URL. */
ea8ba8d @justintadlock Corrections in get_scan_image().
authored
482 preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i', $post_content, $matches );
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
483
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
484 /* If there is a match for the image, set the image args. */
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
485 if ( isset( $matches ) && !empty( $matches[1][0] ) )
486 $this->image_args = array( 'src' => $matches[1][0] );
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
487 }
488
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
489 /**
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
490 * Scans the post content for a complete image. This method will attempt to grab the complete
491 * HTML for an image. If an image is found, pretty much all arguments passed in may be ignored
492 * in favor of getting the actual image used in the post content. It works with both captions
493 * and linked images. However, it can't account for all possible HTML wrappers for images used
494 * in all setups.
495 *
496 * This method was created for use with the WordPress "image" post format where theme authors
497 * might want to pull the whole image from the content as the user added it. It's also meant
498 * to be used (not required) with the `split_content` option.
499 *
500 * Note: This option should not be used if returning the image as an array. If that's desired,
501 * use the `scan` option instead.
502 *
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
503 * @since 1.0.0
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
504 * @access public
505 * @return void
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
506 */
507 public function get_scan_raw_image() {
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
508
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
509 /* Get the post content. */
510 $post_content = get_post_field( 'post_content', $this->args['post_id'] );
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
511
7769732 @justintadlock Add support for WP embeds with the 'scan' and 'scan_raw' options. Th…
authored
512 /* Apply filters to content. */
513 $post_content = apply_filters( 'get_the_image_post_content', $post_content );
514
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
515 /* Finds matches for shortcodes in the content. */
516 preg_match_all( '/' . get_shortcode_regex() . '/s', $post_content, $matches, PREG_SET_ORDER );
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
517
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
518 if ( !empty( $matches ) ) {
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
519
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
520 foreach ( $matches as $shortcode ) {
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
521
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
522 if ( in_array( $shortcode[2], array( 'caption', 'wp_caption' ) ) ) {
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
523
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
524 preg_match( '#id=[\'"]attachment_([\d]*)[\'"]|class=[\'"].*?wp-image-([\d]*).*?[\'"]#i', $shortcode[0], $matches );
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
525
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
526 if ( !empty( $matches ) && isset( $matches[1] ) || isset( $matches[2] ) ) {
74ab31b @justintadlock Porting changes from version 0.9.0 of the Get the Image script into t…
authored
527
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
528 $attachment_id = !empty( $matches[1] ) ? absint( $matches[1] ) : absint( $matches[2] );
ca5ff9c Bringing code up to version 0.5.
greenshady authored
529
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
530 $image_src = wp_get_attachment_image_src( $attachment_id, $this->args['size'] );
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
531
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
532 if ( !empty( $image_src ) ) {
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
533
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
534 /* old-style captions. */
535 if ( preg_match( '#.*?[\s]caption=[\'"](.+?)[\'"]#i', $shortcode[0], $caption_matches ) )
536 $image_caption = trim( $caption_matches[1] );
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
537
61278ef @justintadlock Don't overwrite $this->args. New $caption_args variable fixes that.
authored
538 $caption_args = array(
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
539 'width' => $image_src[1],
540 'align' => 'center'
541 );
8d6c873 @justintadlock Bringing plugin up to date with version 0.8 of the Get the Image script.
authored
542
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
543 if ( !empty( $image_caption ) )
61278ef @justintadlock Don't overwrite $this->args. New $caption_args variable fixes that.
authored
544 $caption_args['caption'] = $image_caption;
8d6c873 @justintadlock Bringing plugin up to date with version 0.8 of the Get the Image script.
authored
545
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
546 /* Set up the patterns for the 'src', 'width', and 'height' attributes. */
547 $patterns = array(
548 '/(src=[\'"]).+?([\'"])/i',
549 '/(width=[\'"]).+?([\'"])/i',
550 '/(height=[\'"]).+?([\'"])/i',
551 );
8d6c873 @justintadlock Bringing plugin up to date with version 0.8 of the Get the Image script.
authored
552
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
553 /* Set up the replacements for the 'src', 'width', and 'height' attributes. */
554 $replacements = array(
555 '${1}' . $image_src[0] . '${2}',
556 '${1}' . $image_src[1] . '${2}',
557 '${1}' . $image_src[2] . '${2}',
558 );
8d6c873 @justintadlock Bringing plugin up to date with version 0.8 of the Get the Image script.
authored
559
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
560 /* Filter the image attributes. */
561 $shortcode_content = preg_replace( $patterns, $replacements, $shortcode[5] );
8d6c873 @justintadlock Bringing plugin up to date with version 0.8 of the Get the Image script.
authored
562
61278ef @justintadlock Don't overwrite $this->args. New $caption_args variable fixes that.
authored
563 $this->image = img_caption_shortcode( $caption_args, $shortcode_content );
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
564 $this->original_image = $shortcode[0];
565 return;
566 }
567 else {
568 $this->image = do_shortcode( $shortcode[0] );
569 $this->original_image = $shortcode[0];
570 return;
571 }
572 }
573 }
8d6c873 @justintadlock Bringing plugin up to date with version 0.8 of the Get the Image script.
authored
574 }
ca5ff9c Bringing code up to version 0.5.
greenshady authored
575 }
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
576
577 /* Pull a raw HTML image + link if it exists. */
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
578 if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)#is', $post_content, $matches ) )
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
579 $this->image = $this->original_image = $matches[0];
580 }
581
582 /**
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
583 * Allows developers to create a custom callback function. If the `callback` argument is set, theme
584 * developers are expected to **always** return an array. Even if nothing is found, return an empty
585 * array.
586 *
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
587 * @since 1.0.0
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
588 * @access public
589 * @return void
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
590 */
591 public function get_callback_image() {
592 $this->image_args = call_user_func( $this->args['callback'], $this->args );
ca5ff9c Bringing code up to version 0.5.
greenshady authored
593 }
594
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
595 /**
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
596 * Sets the default image.
597 *
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
598 * @since 1.0.0
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
599 * @access public
600 * @return void
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
601 */
602 public function get_default_image() {
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
603 $this->image_args = array( 'src' => $this->args['default'] );
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
604 }
605
606 /**
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
607 * Handles an image attachment. Other methods rely on this method for getting the image data since
608 * most images are actually attachments.
609 *
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
610 * @since 1.0.0
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
611 * @access public
612 * @param int $attachment_id
613 * @return void
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
614 */
615 public function _get_image_attachment( $attachment_id ) {
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
616
8d6c873 @justintadlock Bringing plugin up to date with version 0.8 of the Get the Image script.
authored
617 /* Get the attachment image. */
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
618 $image = wp_get_attachment_image_src( $attachment_id, $this->args['size'] );
ecc26c2 Deprecated custom_key in favor of meta_key.
greenshady authored
619
74ab31b @justintadlock Porting changes from version 0.9.0 of the Get the Image script into t…
authored
620 /* Get the attachment alt text. */
621 $alt = trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) );
622
623 /* Get the attachment caption. */
624 $caption = get_post_field( 'post_excerpt', $attachment_id );
8d6c873 @justintadlock Bringing plugin up to date with version 0.8 of the Get the Image script.
authored
625
626 /* Save the attachment as the 'featured image'. */
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
627 if ( true === $this->args['thumbnail_id_save'] )
628 $this->thumbnail_id_save( $attachment_id );
629
630 /* Set the image args. */
631 $this->image_args = array(
632 'src' => $image[0],
633 'width' => $image[1],
634 'height' => $image[2],
635 'alt' => $alt,
636 'caption' => $caption
637 );
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
638 }
639
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
640 /**
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
641 * Formats the image HTML. This method is only called if the `$image` property isn't set. It uses
642 * the `$image_args` property to set up the image.
643 *
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
644 * @since 1.0.0
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
645 * @access public
646 * @return void
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
647 */
648 public function format_image() {
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
649
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
650 /* If there is no image URL, return false. */
651 if ( empty( $this->image_args['src'] ) )
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
652 return;
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
653
4250072 @justintadlock Add 'min_width' and 'min_height' arguments to allow devs to only use …
authored
654 /* Check against min. width. If the image width is too small return. */
655 if ( 0 < $this->args['min_width'] && isset( $this->image_args['width'] ) && $this->image_args['width'] < $this->args['min_width'] )
656 return;
657
658 /* Check against min. height. If the image height is too small return. */
659 if ( 0 < $this->args['min_height'] && isset( $this->image_args['height'] ) && $this->image_args['height'] < $this->args['min_height'] )
660 return;
661
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
662 /* Empty classes array. */
663 $classes = array();
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
664
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
665 /* If there is alt text, set it. Otherwise, default to the post title. */
666 $image_alt = !empty( $this->image_args['alt'] ) ? $this->image_args['alt'] : get_post_field( 'post_title', $this->args['post_id'] );
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
667
9eb1d5e @justintadlock Add image orientation class if we have a width/height.
authored
668 /* If there's a width/height for the image. */
669 if ( isset( $this->image_args['width'] ) && isset( $this->image_args['height'] ) ) {
670
671 /* Set a class based on the orientation. */
672 $classes[] = ( $this->image_args['height'] > $this->image_args['width'] ) ? 'portrait' : 'landscape';
673
674 /* If an explicit width/height is not set, use the info from the image. */
675 if ( empty( $this->args['width'] ) && empty( $this->args['height'] ) ) {
676 $this->args['width'] = $this->image_args['width'];
677 $this->args['height'] = $this->image_args['height'];
678 }
679 }
680
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
681 /* If there is a width or height, set them as HMTL-ready attributes. */
682 $width = $this->args['width'] ? ' width="' . esc_attr( $this->args['width'] ) . '"' : '';
683 $height = $this->args['height'] ? ' height="' . esc_attr( $this->args['height'] ) . '"' : '';
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
684
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
685 /* Add the meta key(s) to the classes array. */
686 if ( !empty( $this->args['meta_key'] ) )
687 $classes = array_merge( $classes, (array)$this->args['meta_key'] );
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
688
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
689 /* Add the $size to the class. */
690 $classes[] = $this->args['size'];
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
691
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
692 /* Get the custom image class. */
693 if ( !empty( $this->args['image_class'] ) ) {
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
694
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
695 if ( !is_array( $this->args['image_class'] ) )
696 $this->args['image_class'] = preg_split( '#\s+#', $this->args['image_class'] );
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
697
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
698 $classes = array_merge( $classes, $this->args['image_class'] );
699 }
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
700
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
701 /* Sanitize all the classes. */
702 $classes = $this->sanitize_class( $classes );
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
703
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
704 /* Join all the classes into a single string and make sure there are no duplicates. */
705 $class = join( ' ', $classes );
ca5ff9c Bringing code up to version 0.5.
greenshady authored
706
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
707 /* Add the image attributes to the <img /> element. */
8dd9f79 @justintadlock Add itemprop="image" to the image output.
authored
708 $html = sprintf( '<img src="%s" alt="%s" class="%s"%s itemprop="image" />', esc_attr( $this->image_args['src'] ), esc_attr( strip_tags( $image_alt ) ), $class, $width . $height );
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
709
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
710 /* If $link_to_post is set to true, link the image to its post. */
711 if ( $this->args['link_to_post'] )
712 $html = '<a href="' . get_permalink( $this->args['post_id'] ) . '" title="' . esc_attr( get_post_field( 'post_title', $this->args['post_id'] ) ) . '">' . $html . '</a>';
713
714 /* If there is a $post_thumbnail_id, apply the WP filters normally associated with get_the_post_thumbnail(). */
715 if ( !empty( $this->image_args['post_thumbnail_id'] ) )
716 $html = apply_filters( 'post_thumbnail_html', $html, $this->args['post_id'], $this->image_args['post_thumbnail_id'], $this->args['size'], '' );
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
717
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
718 /* If we're showing a caption. */
719 if ( true === $this->args['caption'] && !empty( $this->image_args['caption'] ) )
720 $html = img_caption_shortcode( array( 'caption' => $this->image_args['caption'], 'width' => $this->args['width'] ), $html );
74ab31b @justintadlock Porting changes from version 0.9.0 of the Get the Image script into t…
authored
721
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
722 $this->image = $html;
74ab31b @justintadlock Porting changes from version 0.9.0 of the Get the Image script into t…
authored
723 }
724
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
725 /**
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
726 * Saves the image source as metadata. Saving the image as meta is actually quite a bit quicker
727 * if the user doesn't have a persistent caching plugin available. However, it doesn't play as
728 * nicely with custom image sizes used across multiple themes where one might want to resize images.
729 * This option should be reserved for advanced users only. Don't use in publicly-distributed
730 * themes.
731 *
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
732 * @since 1.0.0
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
733 * @access public
734 * @return void
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
735 */
736 public function meta_key_save() {
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
737
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
738 /* If the $meta_key_save argument is empty or there is no image $url given, return. */
739 if ( empty( $this->args['meta_key_save'] ) || empty( $this->image_args['src'] ) )
740 return;
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
741
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
742 /* Get the current value of the meta key. */
743 $meta = get_post_meta( $this->args['post_id'], $this->args['meta_key_save'], true );
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
744
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
745 /* If there is no value for the meta key, set a new value with the image $url. */
746 if ( empty( $meta ) )
747 add_post_meta( $this->args['post_id'], $this->args['meta_key_save'], $this->image_args['src'] );
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
748
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
749 /* If the current value doesn't match the image $url, update it. */
750 elseif ( $meta !== $this->image_args['src'] )
751 update_post_meta( $this->args['post_id'], $this->args['meta_key_save'], $this->image_args['src'], $meta );
752 }
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
753
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
754 /**
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
755 * Saves the image attachment as the WordPress featured image. This is useful for setting the
756 * featured image for the post in the case that the user forgot to (win for client work!). It
757 * should not be used in publicly-distributed themes where you don't know how the user will be
758 * setting up their site.
759 *
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
760 * @since 1.0.0
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
761 * @access public
762 * @return void
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
763 */
764 public function thumbnail_id_save( $attachment_id ) {
74ab31b @justintadlock Porting changes from version 0.9.0 of the Get the Image script into t…
authored
765
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
766 /* Save the attachment as the 'featured image'. */
767 if ( true === $this->args['thumbnail_id_save'] )
768 set_post_thumbnail( $this->args['post_id'], $attachment_id );
769 }
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
770
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
771 /**
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
772 * Sanitizes the image class.
773 *
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
774 * @since 1.0.0
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
775 * @access public
776 * @param array $classes
777 * @return array
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
778 */
779 public function sanitize_class( $classes ) {
ecc26c2 Deprecated custom_key in favor of meta_key.
greenshady authored
780
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
781 $classes = array_map( 'strtolower', $classes );
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
782 $classes = array_map( 'sanitize_html_class', $classes );
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
783
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
784 return array_unique( $classes );
785 }
ecc26c2 Deprecated custom_key in favor of meta_key.
greenshady authored
786
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
787 /**
788 * Splits the original image HTML from the post content.
789 *
790 * @since 1.0.0
791 * @access public
792 * @param string $content
793 * @return string
794 */
795 public function split_content( $content ) {
ecc26c2 Deprecated custom_key in favor of meta_key.
greenshady authored
796
b5d8779 @justintadlock Change priority of 'split_content' filter. The later priority wasn't …
authored
797 remove_filter( 'the_content', array( $this, 'split_content' ), 9 );
ecc26c2 Deprecated custom_key in favor of meta_key.
greenshady authored
798
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
799 return str_replace( $this->original_image, '', $content );
800 }
ecc26c2 Deprecated custom_key in favor of meta_key.
greenshady authored
801 }
802
803 /**
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
804 * Deletes the image cache for the specific post when the 'save_post' hook is fired.
ca5ff9c Bringing code up to version 0.5.
greenshady authored
805 *
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
806 * @since 0.7.0
8d6c873 @justintadlock Bringing plugin up to date with version 0.8 of the Get the Image script.
authored
807 * @access private
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
808 * @param int $post_id The ID of the post to delete the cache for.
8d6c873 @justintadlock Bringing plugin up to date with version 0.8 of the Get the Image script.
authored
809 * @return void
ca5ff9c Bringing code up to version 0.5.
greenshady authored
810 */
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
811 function get_the_image_delete_cache_by_post( $post_id ) {
812 wp_cache_delete( $post_id, 'get_the_image' );
ca5ff9c Bringing code up to version 0.5.
greenshady authored
813 }
814
815 /**
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
816 * Deletes the image cache for a specific post when the 'added_post_meta', 'deleted_post_meta',
817 * or 'updated_post_meta' hooks are called.
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
818 *
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
819 * @since 0.7.0
8d6c873 @justintadlock Bringing plugin up to date with version 0.8 of the Get the Image script.
authored
820 * @access private
1984baf @justintadlock Code cleanup. A few bug fixes. Loads of inline docs.
authored
821 * @param int $meta_id The ID of the metadata being updated.
822 * @param int $post_id The ID of the post to delete the cache for.
8d6c873 @justintadlock Bringing plugin up to date with version 0.8 of the Get the Image script.
authored
823 * @return void
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
824 */
825 function get_the_image_delete_cache_by_meta( $meta_id, $post_id ) {
826 wp_cache_delete( $post_id, 'get_the_image' );
827 }
828
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
829
830 /* === Deprecated functions === */
831
832
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
833 /**
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
834 * @since 0.1.0
7d49016 Bringing this up to 0.6.1 standards.
greenshady authored
835 * @deprecated 0.3.0
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
836 * @access public
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
837 */
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
838 function get_the_image_link() {
839 _deprecated_function( __FUNCTION__, '0.3.0', 'get_the_image' );
840 get_the_image( array( 'link_to_post' => true ) );
4c2ae75 Importing the files and folders for the Get the Image plugin.
greenshady authored
841 }
842
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
843 /**
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
844 * @since 0.3.0
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
845 * @deprecated 0.7.0
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
846 * @access private
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
847 */
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
848 function image_by_custom_field() {}
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
849
850 /**
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
851 * @since 0.4.0
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
852 * @deprecated 0.7.0
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
853 * @access private
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
854 */
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
855 function image_by_the_post_thumbnail() {}
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
856
857 /**
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
858 * @since 0.3.0
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
859 * @deprecated 0.7.0
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
860 * @access private
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
861 */
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
862 function image_by_attachment() {}
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
863
864 /**
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
865 * @since 0.3.0
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
866 * @deprecated 0.7.0
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
867 * @access private
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
868 */
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
869 function image_by_scan() {}
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
870
871 /**
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
872 * @since 0.3.0
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
873 * @deprecated 0.7.0
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
874 * @access private
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
875 */
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
876 function image_by_default() {}
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
877
878 /**
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
879 * @since 0.1.0
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
880 * @deprecated 0.7.0
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
881 * @access private
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
882 */
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
883 function display_the_image() {}
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
884
885 /**
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
886 * @since 0.5.0
887 * @deprecated 0.7.0
888 * @access private
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
889 */
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
890 function get_the_image_delete_cache() {}
9933b0b Updating plugin to use version 0.7 of the script.
greenshady authored
891
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
892 /**
893 * @since 0.7.0
894 * @deprecated 1.0.0
895 * @access private
896 */
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
897 function get_the_image_by_meta_key() {}
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
898
899 /**
900 * @since 0.7.0
901 * @deprecated 1.0.0
902 * @access private
903 */
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
904 function get_the_image_by_post_thumbnail() {}
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
905
906 /**
907 * @since 0.7.0
908 * @deprecated 1.0.0
909 * @access private
910 */
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
911 function get_the_image_by_attachment() {}
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
912
913 /**
914 * @since 0.7.0
915 * @deprecated 1.0.0
916 * @access private
917 */
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
918 function get_the_image_by_scan() {}
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
919
920 /**
921 * @since 0.7.0
922 * @deprecated 1.0.0
923 * @access private
924 */
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
925 function get_the_image_by_default() {}
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
926
927 /**
928 * @since 0.7.0
929 * @deprecated 1.0.0
930 * @access private
931 */
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
932 function get_the_image_format() {}
d692029 @justintadlock Droppin' the bomb on some code. Introducing the new Get_The_Image cl…
authored
933
934 /**
935 * @since 0.6.0
936 * @deprecated 1.0.0
937 * @access private
938 */
fc7e3d2 @justintadlock Clean up deprecated functions.
authored
939 function get_the_image_meta_key_save() {}
Something went wrong with that request. Please try again.