|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Useful for finding an image to display alongside/in representation of a specific post. |
|
5
|
|
|
* |
|
6
|
|
|
* Includes a few different methods, all of which return a similar-format array containing |
|
7
|
|
|
* details of any images found. Everything can (should) be called statically, it's just a |
|
8
|
|
|
* function-bucket. You can also call Jetpack_PostImages::get_image() to cycle through all of the methods until |
|
9
|
|
|
* one of them finds something useful. |
|
10
|
|
|
* |
|
11
|
|
|
* This file is included verbatim in Jetpack |
|
12
|
|
|
*/ |
|
13
|
|
|
class Jetpack_PostImages { |
|
14
|
|
|
/** |
|
15
|
|
|
* If a slideshow is embedded within a post, then parse out the images involved and return them |
|
16
|
|
|
*/ |
|
17
|
|
|
static function from_slideshow( $post_id, $width = 200, $height = 200 ) { |
|
18
|
|
|
$images = array(); |
|
19
|
|
|
|
|
20
|
|
|
$post = get_post( $post_id ); |
|
21
|
|
|
|
|
22
|
|
|
if ( ! $post ) { |
|
23
|
|
|
return $images; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
if ( ! empty( $post->post_password ) ) { |
|
27
|
|
|
return $images; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
if ( false === has_shortcode( $post->post_content, 'slideshow' ) ) { |
|
31
|
|
|
return $images; // no slideshow - bail |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$permalink = get_permalink( $post->ID ); |
|
35
|
|
|
|
|
36
|
|
|
// Mechanic: Somebody set us up the bomb |
|
37
|
|
|
$old_post = $GLOBALS['post']; |
|
38
|
|
|
$GLOBALS['post'] = $post; |
|
39
|
|
|
$old_shortcodes = $GLOBALS['shortcode_tags']; |
|
40
|
|
|
$GLOBALS['shortcode_tags'] = array( 'slideshow' => $old_shortcodes['slideshow'] ); |
|
41
|
|
|
|
|
42
|
|
|
// Find all the slideshows |
|
43
|
|
|
preg_match_all( '/' . get_shortcode_regex() . '/sx', $post->post_content, $slideshow_matches, PREG_SET_ORDER ); |
|
44
|
|
|
|
|
45
|
|
|
ob_start(); // The slideshow shortcode handler calls wp_print_scripts and wp_print_styles... not too happy about that |
|
46
|
|
|
|
|
47
|
|
|
foreach ( $slideshow_matches as $slideshow_match ) { |
|
|
|
|
|
|
48
|
|
|
$slideshow = do_shortcode_tag( $slideshow_match ); |
|
49
|
|
|
if ( false === $pos = stripos( $slideshow, 'jetpack-slideshow' ) ) // must be something wrong - or we changed the output format in which case none of the following will work |
|
50
|
|
|
continue; |
|
51
|
|
|
$start = strpos( $slideshow, '[', $pos ); |
|
52
|
|
|
$end = strpos( $slideshow, ']', $start ); |
|
53
|
|
|
$post_images = json_decode( wp_specialchars_decode( str_replace( "'", '"', substr( $slideshow, $start, $end - $start + 1 ) ), ENT_QUOTES ) ); // parse via JSON |
|
54
|
|
|
foreach ( $post_images as $post_image ) { |
|
55
|
|
|
if ( !$post_image_id = absint( $post_image->id ) ) |
|
56
|
|
|
continue; |
|
57
|
|
|
|
|
58
|
|
|
$meta = wp_get_attachment_metadata( $post_image_id ); |
|
59
|
|
|
|
|
60
|
|
|
// Must be larger than 200x200 (or user-specified) |
|
61
|
|
|
if ( !isset( $meta['width'] ) || $meta['width'] < $width ) |
|
62
|
|
|
continue; |
|
63
|
|
|
if ( !isset( $meta['height'] ) || $meta['height'] < $height ) |
|
64
|
|
|
continue; |
|
65
|
|
|
|
|
66
|
|
|
$url = wp_get_attachment_url( $post_image_id ); |
|
67
|
|
|
|
|
68
|
|
|
$images[] = array( |
|
69
|
|
|
'type' => 'image', |
|
70
|
|
|
'from' => 'slideshow', |
|
71
|
|
|
'src' => $url, |
|
72
|
|
|
'src_width' => $meta['width'], |
|
73
|
|
|
'src_height' => $meta['height'], |
|
74
|
|
|
'href' => $permalink, |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
ob_end_clean(); |
|
79
|
|
|
|
|
80
|
|
|
// Operator: Main screen turn on |
|
81
|
|
|
$GLOBALS['shortcode_tags'] = $old_shortcodes; |
|
82
|
|
|
$GLOBALS['post'] = $old_post; |
|
83
|
|
|
|
|
84
|
|
|
return $images; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* If a gallery is detected, then get all the images from it. |
|
89
|
|
|
*/ |
|
90
|
|
|
static function from_gallery( $post_id ) { |
|
91
|
|
|
$images = array(); |
|
92
|
|
|
|
|
93
|
|
|
$post = get_post( $post_id ); |
|
94
|
|
|
|
|
95
|
|
|
if ( ! $post ) { |
|
96
|
|
|
return $images; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if ( ! empty( $post->post_password ) ) { |
|
100
|
|
|
return $images; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$permalink = get_permalink( $post->ID ); |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Juggle global post object because the gallery shortcode uses the |
|
107
|
|
|
* global object. |
|
108
|
|
|
* |
|
109
|
|
|
* See core ticket: |
|
110
|
|
|
* https://core.trac.wordpress.org/ticket/39304 |
|
111
|
|
|
*/ |
|
112
|
|
|
if ( isset( $GLOBALS['post'] ) ) { |
|
113
|
|
|
$juggle_post = $GLOBALS['post']; |
|
114
|
|
|
$GLOBALS['post'] = $post; |
|
115
|
|
|
$galleries = get_post_galleries( $post->ID, false ); |
|
116
|
|
|
$GLOBALS['post'] = $juggle_post; |
|
117
|
|
|
} else { |
|
118
|
|
|
$GLOBALS['post'] = $post; |
|
119
|
|
|
$galleries = get_post_galleries( $post->ID, false ); |
|
120
|
|
|
unset( $GLOBALS['post'] ); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
foreach ( $galleries as $gallery ) { |
|
124
|
|
|
if ( isset( $gallery['type'] ) && 'slideshow' === $gallery['type'] && ! empty( $gallery['ids'] ) ) { |
|
125
|
|
|
$image_ids = explode( ',', $gallery['ids'] ); |
|
126
|
|
|
$image_size = isset( $gallery['size'] ) ? $gallery['size'] : 'thumbnail'; |
|
127
|
|
|
foreach ( $image_ids as $image_id ) { |
|
128
|
|
|
$image = wp_get_attachment_image_src( $image_id, $image_size ); |
|
129
|
|
View Code Duplication |
if ( ! empty( $image[0] ) ) { |
|
130
|
|
|
list( $raw_src ) = explode( '?', $image[0] ); // pull off any Query string (?w=250) |
|
131
|
|
|
$raw_src = wp_specialchars_decode( $raw_src ); // rawify it |
|
132
|
|
|
$raw_src = esc_url_raw( $raw_src ); // clean it |
|
133
|
|
|
$images[] = array( |
|
134
|
|
|
'type' => 'image', |
|
135
|
|
|
'from' => 'gallery', |
|
136
|
|
|
'src' => $raw_src, |
|
137
|
|
|
'href' => $permalink, |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
View Code Duplication |
} elseif ( ! empty( $gallery['src'] ) ) { |
|
142
|
|
|
foreach ( $gallery['src'] as $src ) { |
|
143
|
|
|
list( $raw_src ) = explode( '?', $src ); // pull off any Query string (?w=250) |
|
144
|
|
|
$raw_src = wp_specialchars_decode( $raw_src ); // rawify it |
|
145
|
|
|
$raw_src = esc_url_raw( $raw_src ); // clean it |
|
146
|
|
|
$images[] = array( |
|
147
|
|
|
'type' => 'image', |
|
148
|
|
|
'from' => 'gallery', |
|
149
|
|
|
'src' => $raw_src, |
|
150
|
|
|
'href' => $permalink, |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return $images; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Get attachment images for a specified post and return them. Also make sure |
|
161
|
|
|
* their dimensions are at or above a required minimum. |
|
162
|
|
|
*/ |
|
163
|
|
|
static function from_attachment( $post_id, $width = 200, $height = 200 ) { |
|
164
|
|
|
$images = array(); |
|
165
|
|
|
|
|
166
|
|
|
$post = get_post( $post_id ); |
|
167
|
|
|
|
|
168
|
|
|
if ( ! empty( $post->post_password ) ) { |
|
169
|
|
|
return $images; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
$post_images = get_posts( array( |
|
173
|
|
|
'post_parent' => $post_id, // Must be children of post |
|
174
|
|
|
'numberposts' => 5, // No more than 5 |
|
175
|
|
|
'post_type' => 'attachment', // Must be attachments |
|
176
|
|
|
'post_mime_type' => 'image', // Must be images |
|
177
|
|
|
'suppress_filters' => false, |
|
178
|
|
|
) ); |
|
179
|
|
|
|
|
180
|
|
|
if ( ! $post_images ) { |
|
181
|
|
|
return $images; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
$permalink = get_permalink( $post_id ); |
|
185
|
|
|
|
|
186
|
|
|
foreach ( $post_images as $post_image ) { |
|
187
|
|
|
$meta = wp_get_attachment_metadata( $post_image->ID ); |
|
188
|
|
|
// Must be larger than 200x200 |
|
189
|
|
|
if ( !isset( $meta['width'] ) || $meta['width'] < $width ) |
|
190
|
|
|
continue; |
|
191
|
|
|
if ( !isset( $meta['height'] ) || $meta['height'] < $height ) |
|
192
|
|
|
continue; |
|
193
|
|
|
|
|
194
|
|
|
$url = wp_get_attachment_url( $post_image->ID ); |
|
195
|
|
|
|
|
196
|
|
|
$images[] = array( |
|
197
|
|
|
'type' => 'image', |
|
198
|
|
|
'from' => 'attachment', |
|
199
|
|
|
'src' => $url, |
|
200
|
|
|
'src_width' => $meta['width'], |
|
201
|
|
|
'src_height' => $meta['height'], |
|
202
|
|
|
'href' => $permalink, |
|
203
|
|
|
); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/* |
|
207
|
|
|
* We only want to pass back attached images that were actually inserted. |
|
208
|
|
|
* We can load up all the images found in the HTML source and then |
|
209
|
|
|
* compare URLs to see if an image is attached AND inserted. |
|
210
|
|
|
*/ |
|
211
|
|
|
$html_images = self::from_html( $post_id ); |
|
212
|
|
|
$inserted_images = array(); |
|
213
|
|
|
|
|
214
|
|
|
foreach( $html_images as $html_image ) { |
|
215
|
|
|
$src = parse_url( $html_image['src'] ); |
|
216
|
|
|
// strip off any query strings from src |
|
217
|
|
|
if( ! empty( $src['scheme'] ) && ! empty( $src['host'] ) ) { |
|
218
|
|
|
$inserted_images[] = $src['scheme'] . '://' . $src['host'] . $src['path']; |
|
219
|
|
|
} elseif( ! empty( $src['host'] ) ) { |
|
220
|
|
|
$inserted_images[] = set_url_scheme( 'http://' . $src['host'] . $src['path'] ); |
|
221
|
|
|
} else { |
|
222
|
|
|
$inserted_images[] = site_url( '/' ) . $src['path']; |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
foreach( $images as $i => $image ) { |
|
226
|
|
|
if ( !in_array( $image['src'], $inserted_images ) ) |
|
227
|
|
|
unset( $images[$i] ); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
return $images; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Check if a Featured Image is set for this post, and return it in a similar |
|
235
|
|
|
* format to the other images?_from_*() methods. |
|
236
|
|
|
* @param int $post_id The post ID to check |
|
237
|
|
|
* @return Array containing details of the Featured Image, or empty array if none. |
|
238
|
|
|
*/ |
|
239
|
|
|
static function from_thumbnail( $post_id, $width = 200, $height = 200 ) { |
|
240
|
|
|
$images = array(); |
|
241
|
|
|
|
|
242
|
|
|
$post = get_post( $post_id ); |
|
243
|
|
|
|
|
244
|
|
|
if ( ! empty( $post->post_password ) ) { |
|
245
|
|
|
return $images; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
if ( ! function_exists( 'get_post_thumbnail_id' ) ) { |
|
249
|
|
|
return $images; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
$thumb = get_post_thumbnail_id( $post_id ); |
|
253
|
|
|
|
|
254
|
|
|
if ( $thumb ) { |
|
255
|
|
|
$meta = wp_get_attachment_metadata( $thumb ); |
|
256
|
|
|
// Must be larger than requested minimums |
|
257
|
|
|
if ( !isset( $meta['width'] ) || $meta['width'] < $width ) |
|
258
|
|
|
return $images; |
|
259
|
|
|
if ( !isset( $meta['height'] ) || $meta['height'] < $height ) |
|
260
|
|
|
return $images; |
|
261
|
|
|
|
|
262
|
|
|
$too_big = ( ( ! empty( $meta['width'] ) && $meta['width'] > 1200 ) || ( ! empty( $meta['height'] ) && $meta['height'] > 1200 ) ); |
|
263
|
|
|
|
|
264
|
|
|
if ( |
|
265
|
|
|
$too_big && |
|
266
|
|
|
( |
|
267
|
|
|
( method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'photon' ) ) || |
|
268
|
|
|
( defined( 'IS_WPCOM' ) && IS_WPCOM ) |
|
269
|
|
|
) |
|
270
|
|
|
) { |
|
271
|
|
|
$img_src = wp_get_attachment_image_src( $thumb, array( 1200, 1200 ) ); |
|
272
|
|
|
} else { |
|
273
|
|
|
$img_src = wp_get_attachment_image_src( $thumb, 'full' ); |
|
274
|
|
|
} |
|
275
|
|
|
if ( ! is_array( $img_src ) ) { |
|
276
|
|
|
// If wp_get_attachment_image_src returns false but we know that there should be an image that could be used. |
|
277
|
|
|
// we try a bit harder and user the data that we have. |
|
278
|
|
|
$thumb_post_data = get_post( $thumb ); |
|
279
|
|
|
$img_src = array( $thumb_post_data->guid, $meta['width'], $meta['height'] ); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
$url = $img_src[0]; |
|
283
|
|
|
$images = array( array( // Other methods below all return an array of arrays |
|
284
|
|
|
'type' => 'image', |
|
285
|
|
|
'from' => 'thumbnail', |
|
286
|
|
|
'src' => $url, |
|
287
|
|
|
'src_width' => $img_src[1], |
|
288
|
|
|
'src_height' => $img_src[2], |
|
289
|
|
|
'href' => get_permalink( $thumb ), |
|
290
|
|
|
) ); |
|
291
|
|
|
|
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
if ( empty( $images ) && ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { |
|
295
|
|
|
$meta_thumbnail = get_post_meta( $post_id, '_jetpack_post_thumbnail', true ); |
|
296
|
|
|
if ( ! empty( $meta_thumbnail ) ) { |
|
297
|
|
|
if ( ! isset( $meta_thumbnail['width'] ) || $meta_thumbnail['width'] < $width ) { |
|
298
|
|
|
return $images; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
if ( ! isset( $meta_thumbnail['height'] ) || $meta_thumbnail['height'] < $height ) { |
|
302
|
|
|
return $images; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
$images = array( array( // Other methods below all return an array of arrays |
|
306
|
|
|
'type' => 'image', |
|
307
|
|
|
'from' => 'thumbnail', |
|
308
|
|
|
'src' => $meta_thumbnail['URL'], |
|
309
|
|
|
'src_width' => $meta_thumbnail['width'], |
|
310
|
|
|
'src_height' => $meta_thumbnail['height'], |
|
311
|
|
|
'href' => $meta_thumbnail['URL'], |
|
312
|
|
|
) ); |
|
313
|
|
|
} |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
return $images; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* Very raw -- just parse the HTML and pull out any/all img tags and return their src |
|
321
|
|
|
* |
|
322
|
|
|
* @param mixed $html_or_id The HTML string to parse for images, or a post id. |
|
323
|
|
|
* @param int $width Minimum Image width. |
|
324
|
|
|
* @param int $height Minimum Image height. |
|
325
|
|
|
* |
|
326
|
|
|
* @uses DOMDocument |
|
327
|
|
|
* |
|
328
|
|
|
* @return Array containing images |
|
329
|
|
|
*/ |
|
330
|
|
|
static function from_html( $html_or_id, $width = 200, $height = 200 ) { |
|
331
|
|
|
$images = array(); |
|
332
|
|
|
|
|
333
|
|
|
if ( is_numeric( $html_or_id ) ) { |
|
334
|
|
|
$post = get_post( $html_or_id ); |
|
335
|
|
|
|
|
336
|
|
|
if ( empty( $post ) || ! empty( $post->post_password ) ) { |
|
337
|
|
|
return $images; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
$html = $post->post_content; // DO NOT apply the_content filters here, it will cause loops. |
|
341
|
|
|
} else { |
|
342
|
|
|
$html = $html_or_id; |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
if ( ! $html ) { |
|
346
|
|
|
return $images; |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
// Do not go any further if DOMDocument is disabled on the server. |
|
350
|
|
|
if ( ! class_exists( 'DOMDocument' ) ) { |
|
351
|
|
|
return $images; |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
// Let's grab all image tags from the HTML. |
|
355
|
|
|
$dom_doc = new DOMDocument; |
|
356
|
|
|
|
|
357
|
|
|
// The @ is not enough to suppress errors when dealing with libxml, |
|
358
|
|
|
// we have to tell it directly how we want to handle errors. |
|
359
|
|
|
libxml_use_internal_errors( true ); |
|
360
|
|
|
@$dom_doc->loadHTML( $html ); |
|
|
|
|
|
|
361
|
|
|
libxml_use_internal_errors( false ); |
|
362
|
|
|
|
|
363
|
|
|
$image_tags = $dom_doc->getElementsByTagName( 'img' ); |
|
364
|
|
|
|
|
365
|
|
|
// For each image Tag, make sure it can be added to the $images array, and add it. |
|
366
|
|
|
foreach ( $image_tags as $image_tag ) { |
|
367
|
|
|
$img_src = $image_tag->getAttribute( 'src' ); |
|
368
|
|
|
|
|
369
|
|
|
if ( empty( $img_src ) ) { |
|
370
|
|
|
continue; |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
// Do not grab smiley images that were automatically created by WP when entering text smilies. |
|
374
|
|
|
if ( stripos( $img_src, '/smilies/' ) ) { |
|
375
|
|
|
continue; |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
$meta = array( |
|
379
|
|
|
'width' => (int) $image_tag->getAttribute( 'width' ), |
|
380
|
|
|
'height' => (int) $image_tag->getAttribute( 'height' ), |
|
381
|
|
|
); |
|
382
|
|
|
|
|
383
|
|
|
// Must be larger than 200x200 (or user-specified). |
|
384
|
|
|
if ( empty( $meta['width'] ) || $meta['width'] < $width ) { |
|
385
|
|
|
continue; |
|
386
|
|
|
} |
|
387
|
|
|
if ( empty( $meta['height'] ) || $meta['height'] < $height ) { |
|
388
|
|
|
continue; |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
$images[] = array( |
|
392
|
|
|
'type' => 'image', |
|
393
|
|
|
'from' => 'html', |
|
394
|
|
|
'src' => $img_src, |
|
395
|
|
|
'src_width' => $meta['width'], |
|
396
|
|
|
'src_height' => $meta['height'], |
|
397
|
|
|
'href' => '', // No link to apply to these. Might potentially parse for that as well, but not for now. |
|
398
|
|
|
); |
|
399
|
|
|
} |
|
400
|
|
|
return $images; |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
/** |
|
404
|
|
|
* @param int $post_id The post ID to check |
|
405
|
|
|
* @param int $size |
|
406
|
|
|
* @return Array containing details of the image, or empty array if none. |
|
407
|
|
|
*/ |
|
408
|
|
|
static function from_blavatar( $post_id, $size = 96 ) { |
|
409
|
|
|
|
|
410
|
|
|
$permalink = get_permalink( $post_id ); |
|
411
|
|
|
|
|
412
|
|
|
if ( function_exists( 'blavatar_domain' ) && function_exists( 'blavatar_exists' ) && function_exists( 'blavatar_url' ) ) { |
|
413
|
|
|
$domain = blavatar_domain( $permalink ); |
|
414
|
|
|
|
|
415
|
|
|
if ( ! blavatar_exists( $domain ) ) { |
|
416
|
|
|
return array(); |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
$url = blavatar_url( $domain, 'img', $size ); |
|
420
|
|
|
} elseif ( function_exists( 'has_site_icon' ) && has_site_icon() ) { |
|
421
|
|
|
$url = get_site_icon_url( $size ); |
|
422
|
|
|
} else { |
|
423
|
|
|
return array(); |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
return array( array( |
|
427
|
|
|
'type' => 'image', |
|
428
|
|
|
'from' => 'blavatar', |
|
429
|
|
|
'src' => $url, |
|
430
|
|
|
'src_width' => $size, |
|
431
|
|
|
'src_height' => $size, |
|
432
|
|
|
'href' => $permalink, |
|
433
|
|
|
) ); |
|
434
|
|
|
} |
|
435
|
|
|
|
|
436
|
|
|
/** |
|
437
|
|
|
* Gets a post image from the author avatar. |
|
438
|
|
|
* |
|
439
|
|
|
* @param int $post_id The post ID to check. |
|
440
|
|
|
* @param int $size The size of the avatar to get. |
|
441
|
|
|
* @param string $default The default image to use. |
|
|
|
|
|
|
442
|
|
|
* @return Array containing details of the image, or empty array if none. |
|
443
|
|
|
*/ |
|
444
|
|
|
static function from_gravatar( $post_id, $size = 96, $default = false ) { |
|
445
|
|
|
$post = get_post( $post_id ); |
|
446
|
|
|
$permalink = get_permalink( $post_id ); |
|
447
|
|
|
|
|
448
|
|
|
if ( function_exists( 'wpcom_get_avatar_url' ) ) { |
|
449
|
|
|
$url = wpcom_get_avatar_url( $post->post_author, $size, $default, true ); |
|
450
|
|
|
if ( $url && is_array( $url ) ) { |
|
451
|
|
|
$url = $url[0]; |
|
452
|
|
|
} |
|
453
|
|
|
} else { |
|
454
|
|
|
$url = get_avatar_url( $post->post_author, array( |
|
455
|
|
|
'size' => $size, |
|
456
|
|
|
'default' => $default, |
|
457
|
|
|
) ); |
|
458
|
|
|
} |
|
459
|
|
|
|
|
460
|
|
|
return array( |
|
461
|
|
|
array( |
|
462
|
|
|
'type' => 'image', |
|
463
|
|
|
'from' => 'gravatar', |
|
464
|
|
|
'src' => $url, |
|
465
|
|
|
'src_width' => $size, |
|
466
|
|
|
'src_height' => $size, |
|
467
|
|
|
'href' => $permalink, |
|
468
|
|
|
), |
|
469
|
|
|
); |
|
470
|
|
|
} |
|
471
|
|
|
|
|
472
|
|
|
/** |
|
473
|
|
|
* Run through the different methods that we have available to try to find a single good |
|
474
|
|
|
* display image for this post. |
|
475
|
|
|
* @param int $post_id |
|
476
|
|
|
* @param array $args Other arguments (currently width and height required for images where possible to determine) |
|
477
|
|
|
* @return Array containing details of the best image to be used |
|
478
|
|
|
*/ |
|
479
|
|
|
static function get_image( $post_id, $args = array() ) { |
|
480
|
|
|
$image = ''; |
|
481
|
|
|
|
|
482
|
|
|
/** |
|
483
|
|
|
* Fires before we find a single good image for a specific post. |
|
484
|
|
|
* |
|
485
|
|
|
* @since 2.2.0 |
|
486
|
|
|
* |
|
487
|
|
|
* @param int $post_id Post ID. |
|
488
|
|
|
*/ |
|
489
|
|
|
do_action( 'jetpack_postimages_pre_get_image', $post_id ); |
|
490
|
|
|
$media = self::get_images( $post_id, $args ); |
|
491
|
|
|
|
|
492
|
|
|
|
|
493
|
|
|
if ( is_array( $media ) ) { |
|
494
|
|
|
foreach ( $media as $item ) { |
|
495
|
|
|
if ( 'image' == $item['type'] ) { |
|
496
|
|
|
$image = $item; |
|
497
|
|
|
break; |
|
498
|
|
|
} |
|
499
|
|
|
} |
|
500
|
|
|
} |
|
501
|
|
|
|
|
502
|
|
|
/** |
|
503
|
|
|
* Fires after we find a single good image for a specific post. |
|
504
|
|
|
* |
|
505
|
|
|
* @since 2.2.0 |
|
506
|
|
|
* |
|
507
|
|
|
* @param int $post_id Post ID. |
|
508
|
|
|
*/ |
|
509
|
|
|
do_action( 'jetpack_postimages_post_get_image', $post_id ); |
|
510
|
|
|
|
|
511
|
|
|
return $image; |
|
512
|
|
|
} |
|
513
|
|
|
|
|
514
|
|
|
/** |
|
515
|
|
|
* Get an array containing a collection of possible images for this post, stopping once we hit a method |
|
516
|
|
|
* that returns something useful. |
|
517
|
|
|
* @param int $post_id |
|
518
|
|
|
* @param array $args Optional args, see defaults list for details |
|
519
|
|
|
* @return Array containing images that would be good for representing this post |
|
520
|
|
|
*/ |
|
521
|
|
|
static function get_images( $post_id, $args = array() ) { |
|
522
|
|
|
// Figure out which image to attach to this post. |
|
523
|
|
|
$media = false; |
|
524
|
|
|
|
|
525
|
|
|
/** |
|
526
|
|
|
* Filters the array of images that would be good for a specific post. |
|
527
|
|
|
* This filter is applied before options ($args) filter the original array. |
|
528
|
|
|
* |
|
529
|
|
|
* @since 2.0.0 |
|
530
|
|
|
* |
|
531
|
|
|
* @param array $media Array of images that would be good for a specific post. |
|
532
|
|
|
* @param int $post_id Post ID. |
|
533
|
|
|
* @param array $args Array of options to get images. |
|
534
|
|
|
*/ |
|
535
|
|
|
$media = apply_filters( 'jetpack_images_pre_get_images', $media, $post_id, $args ); |
|
536
|
|
|
if ( $media ) |
|
537
|
|
|
return $media; |
|
538
|
|
|
|
|
539
|
|
|
$defaults = array( |
|
540
|
|
|
'width' => 200, // Required minimum width (if possible to determine) |
|
541
|
|
|
'height' => 200, // Required minimum height (if possible to determine) |
|
542
|
|
|
|
|
543
|
|
|
'fallback_to_avatars' => false, // Optionally include Blavatar and Gravatar (in that order) in the image stack |
|
544
|
|
|
'avatar_size' => 96, // Used for both Grav and Blav |
|
545
|
|
|
'gravatar_default' => false, // Default image to use if we end up with no Gravatar |
|
546
|
|
|
|
|
547
|
|
|
'from_thumbnail' => true, // Use these flags to specify which methods to use to find an image |
|
548
|
|
|
'from_slideshow' => true, |
|
549
|
|
|
'from_gallery' => true, |
|
550
|
|
|
'from_attachment' => true, |
|
551
|
|
|
'from_html' => true, |
|
552
|
|
|
|
|
553
|
|
|
'html_content' => '' // HTML string to pass to from_html() |
|
554
|
|
|
); |
|
555
|
|
|
$args = wp_parse_args( $args, $defaults ); |
|
556
|
|
|
|
|
557
|
|
|
$media = false; |
|
558
|
|
|
if ( $args['from_thumbnail'] ) |
|
559
|
|
|
$media = self::from_thumbnail( $post_id, $args['width'], $args['height'] ); |
|
560
|
|
View Code Duplication |
if ( !$media && $args['from_slideshow'] ) |
|
561
|
|
|
$media = self::from_slideshow( $post_id, $args['width'], $args['height'] ); |
|
562
|
|
|
if ( !$media && $args['from_gallery'] ) |
|
563
|
|
|
$media = self::from_gallery( $post_id ); |
|
564
|
|
View Code Duplication |
if ( !$media && $args['from_attachment'] ) |
|
565
|
|
|
$media = self::from_attachment( $post_id, $args['width'], $args['height'] ); |
|
566
|
|
|
if ( !$media && $args['from_html'] ) { |
|
567
|
|
View Code Duplication |
if ( empty( $args['html_content'] ) ) |
|
568
|
|
|
$media = self::from_html( $post_id, $args['width'], $args['height'] ); // Use the post_id, which will load the content |
|
569
|
|
|
else |
|
570
|
|
|
$media = self::from_html( $args['html_content'], $args['width'], $args['height'] ); // If html_content is provided, use that |
|
571
|
|
|
} |
|
572
|
|
|
|
|
573
|
|
|
if ( !$media && $args['fallback_to_avatars'] ) { |
|
574
|
|
|
$media = self::from_blavatar( $post_id, $args['avatar_size'] ); |
|
575
|
|
|
if ( !$media ) |
|
|
|
|
|
|
576
|
|
|
$media = self::from_gravatar( $post_id, $args['avatar_size'], $args['gravatar_default'] ); |
|
577
|
|
|
} |
|
578
|
|
|
|
|
579
|
|
|
/** |
|
580
|
|
|
* Filters the array of images that would be good for a specific post. |
|
581
|
|
|
* This filter is applied after options ($args) filter the original array. |
|
582
|
|
|
* |
|
583
|
|
|
* @since 2.0.0 |
|
584
|
|
|
* |
|
585
|
|
|
* @param array $media Array of images that would be good for a specific post. |
|
586
|
|
|
* @param int $post_id Post ID. |
|
587
|
|
|
* @param array $args Array of options to get images. |
|
588
|
|
|
*/ |
|
589
|
|
|
return apply_filters( 'jetpack_images_get_images', $media, $post_id, $args ); |
|
590
|
|
|
} |
|
591
|
|
|
|
|
592
|
|
|
/** |
|
593
|
|
|
* Takes an image URL and pixel dimensions then returns a URL for the |
|
594
|
|
|
* resized and cropped image. |
|
595
|
|
|
* |
|
596
|
|
|
* @param string $src |
|
597
|
|
|
* @param int $dimension |
|
|
|
|
|
|
598
|
|
|
* @return string Transformed image URL |
|
599
|
|
|
*/ |
|
600
|
|
|
static function fit_image_url( $src, $width, $height ) { |
|
601
|
|
|
$width = (int) $width; |
|
602
|
|
|
$height = (int) $height; |
|
603
|
|
|
|
|
604
|
|
|
if ( $width < 1 || $height < 1 ) { |
|
605
|
|
|
return $src; |
|
606
|
|
|
} |
|
607
|
|
|
|
|
608
|
|
|
// See if we should bypass WordPress.com SaaS resizing |
|
609
|
|
|
if ( has_filter( 'jetpack_images_fit_image_url_override' ) ) { |
|
610
|
|
|
/** |
|
611
|
|
|
* Filters the image URL used after dimensions are set by Photon. |
|
612
|
|
|
* |
|
613
|
|
|
* @since 3.3.0 |
|
614
|
|
|
* |
|
615
|
|
|
* @param string $src Image URL. |
|
616
|
|
|
* @param int $width Image width. |
|
617
|
|
|
* @param int $width Image height. |
|
618
|
|
|
*/ |
|
619
|
|
|
return apply_filters( 'jetpack_images_fit_image_url_override', $src, $width, $height ); |
|
620
|
|
|
} |
|
621
|
|
|
|
|
622
|
|
|
// If WPCOM hosted image use native transformations |
|
623
|
|
|
$img_host = parse_url( $src, PHP_URL_HOST ); |
|
624
|
|
|
if ( '.files.wordpress.com' == substr( $img_host, -20 ) ) { |
|
625
|
|
|
return add_query_arg( array( 'w' => $width, 'h' => $height, 'crop' => 1 ), set_url_scheme( $src ) ); |
|
626
|
|
|
} |
|
627
|
|
|
|
|
628
|
|
|
// Use Photon magic |
|
629
|
|
|
if( function_exists( 'jetpack_photon_url' ) ) { |
|
630
|
|
|
return jetpack_photon_url( $src, array( 'resize' => "$width,$height" ) ); |
|
631
|
|
|
} |
|
632
|
|
|
|
|
633
|
|
|
// Arg... no way to resize image using WordPress.com infrastructure! |
|
634
|
|
|
return $src; |
|
635
|
|
|
} |
|
636
|
|
|
} |
|
637
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.