Completed
Push — update/readme-stable-381 ( 9db1ac )
by
unknown
10:07
created

class.jetpack-post-images.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
		if ( !empty( $post->post_password ) )
22
			return $images;
23
24
		if ( false === has_shortcode( $post->post_content, 'slideshow' ) ) {
25
			return false; // no slideshow - bail
26
		}
27
28
		$permalink = get_permalink( $post->ID );
29
30
		// Mechanic: Somebody set us up the bomb
31
		$old_post = $GLOBALS['post'];
32
		$GLOBALS['post'] = $post;
33
		$old_shortcodes = $GLOBALS['shortcode_tags'];
34
		$GLOBALS['shortcode_tags'] = array( 'slideshow' => $old_shortcodes['slideshow'] );
35
36
		// Find all the slideshows
37
		preg_match_all( '/' . get_shortcode_regex() . '/sx', $post->post_content, $slideshow_matches, PREG_SET_ORDER );
38
39
		ob_start(); // The slideshow shortcode handler calls wp_print_scripts and wp_print_styles... not too happy about that
40
41
		foreach ( $slideshow_matches as $slideshow_match ) {
0 ignored issues
show
The expression $slideshow_matches of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. 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:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
42
			$slideshow = do_shortcode_tag( $slideshow_match );
43
			if ( false === $pos = stripos( $slideshow, 'slideShow.images' ) ) // must be something wrong - or we changed the output format in which case none of the following will work
44
				continue;
45
			$start = strpos( $slideshow, '[', $pos );
46
			$end = strpos( $slideshow, ']', $start );
47
			$post_images = json_decode( str_replace( "'", '"', substr( $slideshow, $start, $end - $start + 1 ) ) ); // parse via JSON
48
			foreach ( $post_images as $post_image ) {
49
				if ( !$post_image_id = absint( $post_image->id ) )
50
					continue;
51
52
				$meta = wp_get_attachment_metadata( $post_image_id );
53
54
				// Must be larger than 200x200 (or user-specified)
55
				if ( !isset( $meta['width'] ) || $meta['width'] < $width )
56
					continue;
57
				if ( !isset( $meta['height'] ) || $meta['height'] < $height )
58
					continue;
59
60
				$url = wp_get_attachment_url( $post_image_id );
61
62
				$images[] = array(
63
					'type'       => 'image',
64
					'from'       => 'slideshow',
65
					'src'        => $url,
66
					'src_width'  => $meta['width'],
67
					'src_height' => $meta['height'],
68
					'href'       => $permalink,
69
				);
70
			}
71
		}
72
		ob_end_clean();
73
74
		// Operator: Main screen turn on
75
		$GLOBALS['shortcode_tags'] = $old_shortcodes;
76
		$GLOBALS['post'] = $old_post;
77
78
		return $images;
79
	}
80
81
	/**
82
	 * If a gallery is detected, then get all the images from it.
83
	 */
84
	static function from_gallery( $post_id ) {
85
		$images = array();
86
87
		$post = get_post( $post_id );
88
		if ( ! empty( $post->post_password ) ) {
89
			return $images;
90
		}
91
92
		$permalink = get_permalink( $post->ID );
93
94
		$gallery_images = get_post_galleries_images( $post->ID, false );
95
96
		foreach ( $gallery_images as $galleries ) {
97
			foreach ( $galleries as $src ) {
98
				list( $raw_src ) = explode( '?', $src ); // pull off any Query string (?w=250)
99
				$raw_src = wp_specialchars_decode( $raw_src ); // rawify it
100
				$raw_src = esc_url_raw( $raw_src ); // clean it
101
				$images[] = array(
102
					'type'  => 'image',
103
					'from'  => 'gallery',
104
					'src'   => $raw_src,
105
					'href'  => $permalink,
106
				);
107
			}
108
		}
109
110
		return $images;
111
	}
112
113
	/**
114
	 * Get attachment images for a specified post and return them. Also make sure
115
	 * their dimensions are at or above a required minimum.
116
	 */
117
	static function from_attachment( $post_id, $width = 200, $height = 200 ) {
118
		$images = array();
119
120
		$post = get_post( $post_id );
121
		if ( !empty( $post->post_password ) )
122
			return $images;
123
124
		$post_images = get_posts( array(
125
			'post_parent' => $post_id,   // Must be children of post
126
			'numberposts' => 5,          // No more than 5
127
			'post_type' => 'attachment', // Must be attachments
128
			'post_mime_type' => 'image', // Must be images
129
		) );
130
131
		if ( !$post_images )
132
			return false;
133
134
		$permalink = get_permalink( $post_id );
135
136
		foreach ( $post_images as $post_image ) {
137
			$meta = wp_get_attachment_metadata( $post_image->ID );
138
			// Must be larger than 200x200
139
			if ( !isset( $meta['width'] ) || $meta['width'] < $width )
140
				continue;
141
			if ( !isset( $meta['height'] ) || $meta['height'] < $height )
142
				continue;
143
144
			$url = wp_get_attachment_url( $post_image->ID );
145
146
			$images[] = array(
147
				'type'       => 'image',
148
				'from'       => 'attachment',
149
				'src'        => $url,
150
				'src_width'  => $meta['width'],
151
				'src_height' => $meta['height'],
152
				'href'       => $permalink,
153
			);
154
		}
155
156
		/*
157
		* We only want to pass back attached images that were actually inserted.
158
		* We can load up all the images found in the HTML source and then
159
		* compare URLs to see if an image is attached AND inserted.
160
		*/
161
		$html_images = self::from_html( $post_id );
162
		$inserted_images = array();
163
164
		foreach( $html_images as $html_image ) {
165
			$src = parse_url( $html_image['src'] );
166
			// strip off any query strings from src
167
			if( ! empty( $src['scheme'] ) && ! empty( $src['host'] ) ) {
168
				$inserted_images[] = $src['scheme'] . '://' . $src['host'] . $src['path'];
169
			} elseif( ! empty( $src['host'] ) ) {
170
				$inserted_images[] = set_url_scheme( 'http://' . $src['host'] . $src['path'] );
171
			} else {
172
				$inserted_images[] = site_url( '/' ) . $src['path'];
173
			}
174
		}
175
		foreach( $images as $i => $image ) {
176
			if ( !in_array( $image['src'], $inserted_images ) )
177
				unset( $images[$i] );
178
		}
179
180
		return $images;
181
	}
182
183
	/**
184
	 * Check if a Featured Image is set for this post, and return it in a similar
185
	 * format to the other images?_from_*() methods.
186
	 * @param  int $post_id The post ID to check
187
	 * @return Array containing details of the Featured Image, or empty array if none.
188
	 */
189
	static function from_thumbnail( $post_id, $width = 200, $height = 200 ) {
190
		$images = array();
191
192
		$post = get_post( $post_id );
193
		if ( !empty( $post->post_password ) )
194
			return $images;
195
196
		if ( !function_exists( 'get_post_thumbnail_id' ) )
197
			return $images;
198
199
		$thumb = get_post_thumbnail_id( $post_id );
200
201
		if ( $thumb ) {
202
			$meta = wp_get_attachment_metadata( $thumb );
203
204
			// Must be larger than requested minimums
205
			if ( !isset( $meta['width'] ) || $meta['width'] < $width )
206
				return $images;
207
			if ( !isset( $meta['height'] ) || $meta['height'] < $height )
208
				return $images;
209
210
			$too_big = ( ( ! empty( $meta['width'] ) && $meta['width'] > 1200 ) || ( ! empty( $meta['height'] ) && $meta['height'] > 1200 ) );
211
212
			if ( $too_big ) {
213
				$img_src = wp_get_attachment_image_src( $thumb, array( 1200, 1200 ) );
214
			} else {
215
				$img_src = wp_get_attachment_image_src( $thumb, 'full' );
216
			}
217
218
			$url = $img_src[0];
219
220
			$images = array( array( // Other methods below all return an array of arrays
221
				'type'       => 'image',
222
				'from'       => 'thumbnail',
223
				'src'        => $url,
224
				'src_width'  => $img_src[1],
225
				'src_height' => $img_src[2],
226
				'href'       => get_permalink( $thumb ),
227
			) );
228
		}
229
		return $images;
230
	}
231
232
	/**
233
	 * Very raw -- just parse the HTML and pull out any/all img tags and return their src
234
	 * @param  mixed $html_or_id The HTML string to parse for images, or a post id
235
	 * @return Array containing images
236
	 */
237
	static function from_html( $html_or_id ) {
238
		$images = array();
239
240
		if ( is_numeric( $html_or_id ) ) {
241
			$post = get_post( $html_or_id );
242
			if ( empty( $post ) || !empty( $post->post_password ) )
243
				return $images;
244
245
			$html = $post->post_content; // DO NOT apply the_content filters here, it will cause loops
246
		} else {
247
			$html = $html_or_id;
248
		}
249
250
		if ( !$html )
251
			return $images;
252
253
		preg_match_all( '!<img.*src=[\'"]([^"]+)[\'"].*/?>!iUs', $html, $matches );
254
		if ( !empty( $matches[1] ) ) {
255
			foreach ( $matches[1] as $match ) {
256
				if ( stristr( $match, '/smilies/' ) )
257
					continue;
258
259
				$images[] = array(
260
					'type'  => 'image',
261
					'from'  => 'html',
262
					'src'   => html_entity_decode( $match ),
263
					'href'  => '', // No link to apply to these. Might potentially parse for that as well, but not for now
264
				);
265
			}
266
		}
267
268
		return $images;
269
	}
270
271
	/**
272
	 * @param    int $post_id The post ID to check
273
	 * @param    int $size
274
	 * @return Array containing details of the image, or empty array if none.
275
	 */
276
	static function from_blavatar( $post_id, $size = 96 ) {
277
278
		$permalink = get_permalink( $post_id );
279
280
		if ( function_exists( 'blavatar_domain' ) && function_exists( 'blavatar_exists' ) && function_exists( 'blavatar_url' ) ) {
281
			$domain = blavatar_domain( $permalink );
282
283
			if ( ! blavatar_exists( $domain ) ) {
284
				return array();
285
			}
286
287
			$url = blavatar_url( $domain, 'img', $size );
288
		} elseif ( function_exists( 'jetpack_has_site_icon' ) && jetpack_has_site_icon() ) {
289
			$url = jetpack_site_icon_url( null, $size, $default = false );
290
		} else {
291
			return array();
292
		}
293
294
		return array( array(
295
			'type'       => 'image',
296
			'from'       => 'blavatar',
297
			'src'        => $url,
298
			'src_width'  => $size,
299
			'src_height' => $size,
300
			'href'       => $permalink,
301
		) );
302
	}
303
304
	/**
305
	 * @param    int $post_id The post ID to check
306
	 * @param    int $size
307
	 * @param string $default The default image to use.
0 ignored issues
show
Should the type for parameter $default not be false|string?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
308
	 * @return Array containing details of the image, or empty array if none.
309
	 */
310
	static function from_gravatar( $post_id, $size = 96, $default = false ) {
311
		$post = get_post( $post_id );
312
		$permalink = get_permalink( $post_id );
313
314
		if ( function_exists( 'wpcom_get_avatar_url' ) ) {
315
			$url = wpcom_get_avatar_url( $post->post_author, $size, $default, true );
316
			if ( $url && is_array( $url ) ) {
317
				$url = $url[0];
318
			}
319
		} else {
320
			$has_filter = has_filter( 'pre_option_show_avatars', '__return_true' );
321
			if ( !$has_filter ) {
322
				add_filter( 'pre_option_show_avatars', '__return_true' );
323
			}
324
			$avatar = get_avatar( $post->post_author, $size, $default );
325
			if ( !$has_filter ) {
326
				remove_filter( 'pre_option_show_avatars', '__return_true' );
327
			}
328
329
			if ( !$avatar ) {
330
				return array();
331
			}
332
333
			if ( !preg_match( '/src=["\']([^"\']+)["\']/', $avatar, $matches ) ) {
334
				return array();
335
			}
336
337
			$url = wp_specialchars_decode( $matches[1], ENT_QUOTES );
338
		}
339
340
		return array( array(
341
			'type'       => 'image',
342
			'from'       => 'gravatar',
343
			'src'        => $url,
344
			'src_width'  => $size,
345
			'src_height' => $size,
346
			'href'       => $permalink,
347
		) );
348
	}
349
350
	/**
351
	 * Run through the different methods that we have available to try to find a single good
352
	 * display image for this post.
353
	 * @param  int $post_id
354
	 * @param array $args Other arguments (currently width and height required for images where possible to determine)
355
	 * @return Array containing details of the best image to be used
356
	 */
357
	static function get_image( $post_id, $args = array() ) {
358
		$image = '';
359
360
		/**
361
		 * Fires before we find a single good image for a specific post.
362
		 *
363
		 * @since 2.2.0
364
		 *
365
		 * @param int $post_id Post ID.
366
		 */
367
		do_action( 'jetpack_postimages_pre_get_image', $post_id );
368
		$media = self::get_images( $post_id, $args );
369
370
371
		if ( is_array( $media ) ) {
372
			foreach ( $media as $item ) {
373
				if ( 'image' == $item['type'] ) {
374
					$image = $item;
375
					break;
376
				}
377
			}
378
		}
379
380
		/**
381
		 * Fires after we find a single good image for a specific post.
382
		 *
383
		 * @since 2.2.0
384
		 *
385
		 * @param int $post_id Post ID.
386
		 */
387
		do_action( 'jetpack_postimages_post_get_image', $post_id );
388
389
		return $image;
390
	}
391
392
	/**
393
	 * Get an array containing a collection of possible images for this post, stopping once we hit a method
394
	 * that returns something useful.
395
	 * @param  int $post_id
396
	 * @param  array  $args Optional args, see defaults list for details
397
	 * @return Array containing images that would be good for representing this post
398
	 */
399
	static function get_images( $post_id, $args = array() ) {
400
		// Figure out which image to attach to this post.
401
		$media = false;
402
403
		/**
404
		 * Filters the array of images that would be good for a specific post.
405
		 * This filter is applied before options ($args) filter the original array.
406
		 *
407
		 * @since 2.0.0
408
		 *
409
		 * @param array $media Array of images that would be good for a specific post.
410
		 * @param int $post_id Post ID.
411
		 * @param array $args Array of options to get images.
412
		 */
413
		$media = apply_filters( 'jetpack_images_pre_get_images', $media, $post_id, $args );
414
		if ( $media )
415
			return $media;
416
417
		$defaults = array(
418
			'width'               => 200, // Required minimum width (if possible to determine)
419
			'height'              => 200, // Required minimum height (if possible to determine)
420
421
			'fallback_to_avatars' => false, // Optionally include Blavatar and Gravatar (in that order) in the image stack
422
			'avatar_size'         => 96, // Used for both Grav and Blav
423
			'gravatar_default'    => false, // Default image to use if we end up with no Gravatar
424
425
			'from_thumbnail'      => true, // Use these flags to specify which methods to use to find an image
426
			'from_slideshow'      => true,
427
			'from_gallery'        => true,
428
			'from_attachment'     => true,
429
			'from_html'           => true,
430
431
			'html_content'        => '' // HTML string to pass to from_html()
432
		);
433
		$args = wp_parse_args( $args, $defaults );
434
435
		$media = false;
436
		if ( $args['from_thumbnail'] )
437
			$media = self::from_thumbnail( $post_id, $args['width'], $args['height'] );
438 View Code Duplication
		if ( !$media && $args['from_slideshow'] )
439
			$media = self::from_slideshow( $post_id, $args['width'], $args['height'] );
440
		if ( !$media && $args['from_gallery'] )
441
			$media = self::from_gallery( $post_id );
442 View Code Duplication
		if ( !$media && $args['from_attachment'] )
443
			$media = self::from_attachment( $post_id, $args['width'], $args['height'] );
444
		if ( !$media && $args['from_html'] ) {
445
			if ( empty( $args['html_content'] ) )
446
				$media = self::from_html( $post_id ); // Use the post_id, which will load the content
447
			else
448
				$media = self::from_html( $args['html_content'] ); // If html_content is provided, use that
449
		}
450
451
		if ( !$media && $args['fallback_to_avatars'] ) {
452
			$media = self::from_blavatar( $post_id, $args['avatar_size'] );
453
			if ( !$media )
0 ignored issues
show
Bug Best Practice introduced by
The expression $media of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
454
				$media = self::from_gravatar( $post_id, $args['avatar_size'], $args['gravatar_default'] );
455
		}
456
457
		/**
458
		 * Filters the array of images that would be good for a specific post.
459
		 * This filter is applied after options ($args) filter the original array.
460
		 *
461
		 * @since 2.0.0
462
		 *
463
		 * @param array $media Array of images that would be good for a specific post.
464
		 * @param int $post_id Post ID.
465
		 * @param array $args Array of options to get images.
466
		 */
467
		return apply_filters( 'jetpack_images_get_images', $media, $post_id, $args );
468
	}
469
470
	/**
471
	 * Takes an image URL and pixel dimensions then returns a URL for the
472
	 * resized and croped image.
473
	 *
474
	 * @param  string $src
475
	 * @param  int    $dimension
0 ignored issues
show
There is no parameter named $dimension. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
476
	 * @return string            Transformed image URL
477
	 */
478
	static function fit_image_url( $src, $width, $height ) {
479
		$width = (int) $width;
480
		$height = (int) $height;
481
482
		// Umm...
483
		if ( $width < 1 || $height < 1 ) {
484
			return $src;
485
		}
486
487
		// See if we should bypass WordPress.com SaaS resizing
488
		if ( has_filter( 'jetpack_images_fit_image_url_override' ) ) {
489
			/**
490
			 * Filters the image URL used after dimensions are set by Photon.
491
			 *
492
			 * @since 3.3.0
493
			 *
494
			 * @param string $src Image URL.
495
			 * @param int $width Image width.
496
			 * @param int $width Image height.
497
			 */
498
			return apply_filters( 'jetpack_images_fit_image_url_override', $src, $width, $height );
499
		}
500
501
		// If WPCOM hosted image use native transformations
502
		$img_host = parse_url( $src, PHP_URL_HOST );
503
		if ( '.files.wordpress.com' == substr( $img_host, -20 ) ) {
504
			return add_query_arg( array( 'w' => $width, 'h' => $height, 'crop' => 1 ), $src );
505
		}
506
507
		// Use Photon magic
508
		if( function_exists( 'jetpack_photon_url' ) ) {
509
			return jetpack_photon_url( $src, array( 'resize' => "$width,$height" ) );
510
		}
511
512
		// Arg... no way to resize image using WordPress.com infrastructure!
513
		return $src;
514
	}
515
}
516