Completed
Push — add/feature-checking-selectors ( 605db7...fb0d87 )
by
unknown
08:20 queued 01:39
created

class.jetpack-post-images.php (5 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
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 ) {
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...
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
			// If the JSON didn't decode don't try and act on it.
55
			if ( is_array( $post_images ) ) {
56
				foreach ( $post_images as $post_image ) {
57
					if ( !$post_image_id = absint( $post_image->id ) )
58
						continue;
59
60
					$meta = wp_get_attachment_metadata( $post_image_id );
61
62
					// Must be larger than 200x200 (or user-specified)
63
					if ( !isset( $meta['width'] ) || $meta['width'] < $width )
64
						continue;
65
					if ( !isset( $meta['height'] ) || $meta['height'] < $height )
66
						continue;
67
68
					$url = wp_get_attachment_url( $post_image_id );
69
70
					$images[] = array(
71
						'type'       => 'image',
72
						'from'       => 'slideshow',
73
						'src'        => $url,
74
						'src_width'  => $meta['width'],
75
						'src_height' => $meta['height'],
76
						'href'       => $permalink,
77
					);
78
				}
79
			}
80
		}
81
		ob_end_clean();
82
83
		// Operator: Main screen turn on
84
		$GLOBALS['shortcode_tags'] = $old_shortcodes;
85
		$GLOBALS['post'] = $old_post;
86
87
		return $images;
88
	}
89
90
	/**
91
	 * If a gallery is detected, then get all the images from it.
92
	 */
93
	static function from_gallery( $post_id ) {
94
		$images = array();
95
96
		$post = get_post( $post_id );
97
98
		if ( ! $post ) {
99
			return $images;
100
		}
101
102
		if ( ! empty( $post->post_password ) ) {
103
			return $images;
104
		}
105
106
		$permalink = get_permalink( $post->ID );
107
108
		/**
109
		 *  Juggle global post object because the gallery shortcode uses the
110
		 *  global object.
111
		 *
112
		 *  See core ticket:
113
		 *  https://core.trac.wordpress.org/ticket/39304
114
		 */
115
		if ( isset( $GLOBALS['post'] ) ) {
116
			$juggle_post = $GLOBALS['post'];
117
			$GLOBALS['post'] = $post;
118
			$galleries = get_post_galleries( $post->ID, false );
119
			$GLOBALS['post'] = $juggle_post;
120
		} else {
121
			$GLOBALS['post'] = $post;
122
			$galleries = get_post_galleries( $post->ID, false );
123
			unset( $GLOBALS['post'] );
124
		}
125
126
		foreach ( $galleries as $gallery ) {
127
			if ( isset( $gallery['type'] ) && 'slideshow' === $gallery['type'] && ! empty( $gallery['ids'] ) ) {
128
				$image_ids = explode( ',', $gallery['ids'] );
129
				$image_size = isset( $gallery['size'] ) ? $gallery['size'] : 'thumbnail';
130
				foreach ( $image_ids as $image_id ) {
131
					$image = wp_get_attachment_image_src( $image_id, $image_size );
132 View Code Duplication
					if ( ! empty( $image[0] ) ) {
133
						list( $raw_src ) = explode( '?', $image[0] ); // pull off any Query string (?w=250)
134
						$raw_src = wp_specialchars_decode( $raw_src ); // rawify it
135
						$raw_src = esc_url_raw( $raw_src ); // clean it
136
						$images[] = array(
137
							'type'  => 'image',
138
							'from'  => 'gallery',
139
							'src'   => $raw_src,
140
							'href'  => $permalink,
141
						);
142
					}
143
				}
144 View Code Duplication
			} elseif ( ! empty( $gallery['src'] ) ) {
145
				foreach ( $gallery['src'] as $src ) {
146
					list( $raw_src ) = explode( '?', $src ); // pull off any Query string (?w=250)
147
					$raw_src = wp_specialchars_decode( $raw_src ); // rawify it
148
					$raw_src = esc_url_raw( $raw_src ); // clean it
149
					$images[] = array(
150
						'type'  => 'image',
151
						'from'  => 'gallery',
152
						'src'   => $raw_src,
153
						'href'  => $permalink,
154
					);
155
				}
156
			}
157
		}
158
159
		return $images;
160
	}
161
162
	/**
163
	 * Get attachment images for a specified post and return them. Also make sure
164
	 * their dimensions are at or above a required minimum.
165
	 */
166
	static function from_attachment( $post_id, $width = 200, $height = 200 ) {
167
		$images = array();
168
169
		$post = get_post( $post_id );
170
171
		if ( ! empty( $post->post_password ) ) {
172
			return $images;
173
		}
174
175
		$post_images = get_posts( array(
176
			'post_parent' => $post_id,   // Must be children of post
177
			'numberposts' => 5,          // No more than 5
178
			'post_type' => 'attachment', // Must be attachments
179
			'post_mime_type' => 'image', // Must be images
180
			'suppress_filters' => false,
181
		) );
182
183
		if ( ! $post_images ) {
184
			return $images;
185
		}
186
187
		$permalink = get_permalink( $post_id );
188
189
		foreach ( $post_images as $post_image ) {
190
			$current_image = self::get_attachment_data( $post_image->ID, $permalink, $width, $height );
191
			if ( false !== $current_image ) {
192
				$images[] = $current_image;
193
			}
194
		}
195
196
		/*
197
		* We only want to pass back attached images that were actually inserted.
198
		* We can load up all the images found in the HTML source and then
199
		* compare URLs to see if an image is attached AND inserted.
200
		*/
201
		$html_images = self::from_html( $post_id );
202
		$inserted_images = array();
203
204
		foreach( $html_images as $html_image ) {
205
			$src = parse_url( $html_image['src'] );
206
			// strip off any query strings from src
207
			if( ! empty( $src['scheme'] ) && ! empty( $src['host'] ) ) {
208
				$inserted_images[] = $src['scheme'] . '://' . $src['host'] . $src['path'];
209
			} elseif( ! empty( $src['host'] ) ) {
210
				$inserted_images[] = set_url_scheme( 'http://' . $src['host'] . $src['path'] );
211
			} else {
212
				$inserted_images[] = site_url( '/' ) . $src['path'];
213
			}
214
		}
215
		foreach( $images as $i => $image ) {
216
			if ( !in_array( $image['src'], $inserted_images ) )
217
				unset( $images[$i] );
218
		}
219
220
		return $images;
221
	}
222
223
	/**
224
	 * Check if a Featured Image is set for this post, and return it in a similar
225
	 * format to the other images?_from_*() methods.
226
	 * @param  int $post_id The post ID to check
227
	 * @return Array containing details of the Featured Image, or empty array if none.
228
	 */
229
	static function from_thumbnail( $post_id, $width = 200, $height = 200 ) {
230
		$images = array();
231
232
		$post = get_post( $post_id );
233
234
		if ( ! empty( $post->post_password ) ) {
235
			return $images;
236
		}
237
238
		if ( ! function_exists( 'get_post_thumbnail_id' ) ) {
239
			return $images;
240
		}
241
242
		if ( 'attachment' === get_post_type( $post ) && wp_attachment_is_image( $post ) ) {
243
			$thumb = $post_id;
244
		} else {
245
			$thumb = get_post_thumbnail_id( $post );
246
		}
247
248
		if ( $thumb ) {
249
			$meta = wp_get_attachment_metadata( $thumb );
250
			// Must be larger than requested minimums
251
			if ( !isset( $meta['width'] ) || $meta['width'] < $width )
252
				return $images;
253
			if ( !isset( $meta['height'] ) || $meta['height'] < $height )
254
				return $images;
255
256
			$too_big = ( ( ! empty( $meta['width'] ) && $meta['width'] > 1200 ) || ( ! empty( $meta['height'] ) && $meta['height'] > 1200 ) );
257
258
			if (
259
				$too_big &&
260
				(
261
					( method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'photon' ) ) ||
262
					( defined( 'IS_WPCOM' ) && IS_WPCOM )
263
				)
264
			) {
265
				$img_src = wp_get_attachment_image_src( $thumb, array( 1200, 1200 ) );
266
			} else {
267
				$img_src = wp_get_attachment_image_src( $thumb, 'full' );
268
			}
269
			if ( ! is_array( $img_src ) ) {
270
				// If wp_get_attachment_image_src returns false but we know that there should be an image that could be used.
271
				// we try a bit harder and user the data that we have.
272
				$thumb_post_data = get_post( $thumb );
273
				$img_src = array( $thumb_post_data->guid, $meta['width'], $meta['height'] );
274
			}
275
276
			$url    = $img_src[0];
277
			$images = array( array( // Other methods below all return an array of arrays
278
				'type'       => 'image',
279
				'from'       => 'thumbnail',
280
				'src'        => $url,
281
				'src_width'  => $img_src[1],
282
				'src_height' => $img_src[2],
283
				'href'       => get_permalink( $thumb ),
284
				'alt_text'   => self::get_alt_text( $thumb ),
285
			) );
286
287
		}
288
289
		if ( empty( $images ) && ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
290
			$meta_thumbnail = get_post_meta( $post_id, '_jetpack_post_thumbnail', true );
291
			if ( ! empty( $meta_thumbnail ) ) {
292
				if ( ! isset( $meta_thumbnail['width'] ) || $meta_thumbnail['width'] < $width ) {
293
					return $images;
294
				}
295
296
				if ( ! isset( $meta_thumbnail['height'] ) || $meta_thumbnail['height'] < $height ) {
297
					return $images;
298
				}
299
300
				$images = array( array( // Other methods below all return an array of arrays
301
					'type'       => 'image',
302
					'from'       => 'thumbnail',
303
					'src'        => $meta_thumbnail['URL'],
304
					'src_width'  => $meta_thumbnail['width'],
305
					'src_height' => $meta_thumbnail['height'],
306
					'href'       => $meta_thumbnail['URL'],
307
					'alt_text'   => self::get_alt_text( $thumb ),
308
				) );
309
			}
310
		}
311
312
		return $images;
313
	}
314
315
	/**
316
	 * Get images from Gutenberg Image blocks.
317
	 *
318
	 * @since 6.9.0
319
	 *
320
	 * @param mixed $html_or_id The HTML string to parse for images, or a post id.
321
	 * @param int   $width      Minimum Image width.
322
	 * @param int   $height     Minimum Image height.
323
	 */
324
	public static function from_blocks( $html_or_id, $width = 200, $height = 200 ) {
325
		$images = array();
326
327
		// Bail early if the site does not support the block editor.
328
		if ( ! function_exists( 'parse_blocks' ) ) {
329
			return $images;
330
		}
331
332
		$html_info = self::get_post_html( $html_or_id );
333
334
		if ( empty( $html_info['html'] ) ) {
335
			return $images;
336
		}
337
338
		// Look for block information in the HTML.
339
		$blocks = parse_blocks( $html_info['html'] );
340
		if ( empty( $blocks ) ) {
341
			return $images;
342
		}
343
344
		foreach ( $blocks as $block ) {
345
			/**
346
			 * Parse content from Core Image blocks.
347
			 * If it is an image block for an image hosted on our site, it will have an ID.
348
			 * If it does not have an ID, let `from_html` parse that content later,
349
			 * and extract an image if it has size parameters.
350
			 */
351
			if (
352
				'core/image' === $block['blockName']
353
				&& ! empty( $block['attrs']['id'] )
354
			) {
355
				$images[] = self::get_attachment_data( $block['attrs']['id'], $html_info['post_url'], $width, $height );
356
			}
357
358
			/**
359
			 * Parse content from Core Gallery blocks and Jetpack's Tiled Gallery blocks.
360
			 * Gallery blocks include the ID of each one of the images in the gallery.
361
			 */
362
			if (
363
				( 'core/gallery' === $block['blockName'] || 'jetpack/tiled-gallery' === $block['blockName'] )
364
				&& ! empty( $block['attrs']['ids'] )
365
			) {
366
				foreach ( $block['attrs']['ids'] as $img_id ) {
367
					$images[] = self::get_attachment_data( $img_id, $html_info['post_url'], $width, $height );
368
				}
369
			}
370
		}
371
372
		/**
373
		 * Returning a filtered array because get_attachment_data returns false
374
		 * for unsuccessful attempts.
375
		 */
376
		return array_filter( $images );
377
	}
378
379
	/**
380
	 * Very raw -- just parse the HTML and pull out any/all img tags and return their src
381
	 *
382
	 * @param mixed $html_or_id The HTML string to parse for images, or a post id.
383
	 * @param int   $width      Minimum Image width.
384
	 * @param int   $height     Minimum Image height.
385
	 *
386
	 * @uses DOMDocument
387
	 *
388
	 * @return Array containing images
389
	 */
390
	static function from_html( $html_or_id, $width = 200, $height = 200 ) {
391
		$images = array();
392
393
		$html_info = self::get_post_html( $html_or_id );
394
395
		if ( empty( $html_info['html'] ) ) {
396
			return $images;
397
		}
398
399
		// Do not go any further if DOMDocument is disabled on the server.
400
		if ( ! class_exists( 'DOMDocument' ) ) {
401
			return $images;
402
		}
403
404
		// Let's grab all image tags from the HTML.
405
		$dom_doc = new DOMDocument;
406
407
		// The @ is not enough to suppress errors when dealing with libxml,
408
		// we have to tell it directly how we want to handle errors.
409
		libxml_use_internal_errors( true );
410
		@$dom_doc->loadHTML( $html_info['html'] );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
411
		libxml_use_internal_errors( false );
412
413
		$image_tags = $dom_doc->getElementsByTagName( 'img' );
414
415
		// For each image Tag, make sure it can be added to the $images array, and add it.
416
		foreach ( $image_tags as $image_tag ) {
417
			$img_src = $image_tag->getAttribute( 'src' );
418
419
			if ( empty( $img_src ) ) {
420
				continue;
421
			}
422
423
			// Do not grab smiley images that were automatically created by WP when entering text smilies.
424
			if ( stripos( $img_src, '/smilies/' ) ) {
425
				continue;
426
			}
427
428
			$meta = array(
429
				'width'    => (int) $image_tag->getAttribute( 'width' ),
430
				'height'   => (int) $image_tag->getAttribute( 'height' ),
431
				'alt_text' => $image_tag->getAttribute( 'alt' ),
432
			);
433
434
			/**
435
			 * Filters the switch to ignore minimum image size requirements. Can be used
436
			 * to add custom logic to image dimensions, like only enforcing one of the dimensions,
437
			 * or disabling it entirely.
438
			 *
439
			 * @since 6.4.0
440
			 *
441
			 * @param bool $ignore Should the image dimensions be ignored?
442
			 * @param array $meta Array containing image dimensions parsed from the markup.
443
			 */
444
			$ignore_dimensions = apply_filters( 'jetpack_postimages_ignore_minimum_dimensions', false, $meta );
445
446
			// Must be larger than 200x200 (or user-specified).
447
			if (
448
				! $ignore_dimensions
449
				&& (
450
					empty( $meta['width'] )
451
					|| empty( $meta['height'] )
452
					|| $meta['width'] < $width
453
					|| $meta['height'] < $height
454
				)
455
			) {
456
				continue;
457
			}
458
459
			$images[] = array(
460
				'type'       => 'image',
461
				'from'       => 'html',
462
				'src'        => $img_src,
463
				'src_width'  => $meta['width'],
464
				'src_height' => $meta['height'],
465
				'href'       => $html_info['post_url'],
466
				'alt_text'   => $meta['alt_text'],
467
			);
468
		}
469
		return $images;
470
	}
471
472
	/**
473
	 * @param    int $post_id The post ID to check
474
	 * @param    int $size
475
	 * @return Array containing details of the image, or empty array if none.
476
	 */
477
	static function from_blavatar( $post_id, $size = 96 ) {
478
479
		$permalink = get_permalink( $post_id );
480
481
		if ( function_exists( 'blavatar_domain' ) && function_exists( 'blavatar_exists' ) && function_exists( 'blavatar_url' ) ) {
482
			$domain = blavatar_domain( $permalink );
483
484
			if ( ! blavatar_exists( $domain ) ) {
485
				return array();
486
			}
487
488
			$url = blavatar_url( $domain, 'img', $size );
489
		} elseif ( function_exists( 'has_site_icon' ) && has_site_icon() ) {
490
			$url = get_site_icon_url( $size );
491
		} else {
492
			return array();
493
		}
494
495
		return array( array(
496
			'type'       => 'image',
497
			'from'       => 'blavatar',
498
			'src'        => $url,
499
			'src_width'  => $size,
500
			'src_height' => $size,
501
			'href'       => $permalink,
502
			'alt_text'   => '',
503
		) );
504
	}
505
506
	/**
507
	 * Gets a post image from the author avatar.
508
	 *
509
	 * @param int    $post_id The post ID to check.
510
	 * @param int    $size The size of the avatar to get.
511
	 * @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...
512
	 * @return Array containing details of the image, or empty array if none.
513
	 */
514
	static function from_gravatar( $post_id, $size = 96, $default = false ) {
515
		$post = get_post( $post_id );
516
		$permalink = get_permalink( $post_id );
517
518
		if ( function_exists( 'wpcom_get_avatar_url' ) ) {
519
			$url = wpcom_get_avatar_url( $post->post_author, $size, $default, true );
520
			if ( $url && is_array( $url ) ) {
521
				$url = $url[0];
522
			}
523
		} else {
524
			$url = get_avatar_url( $post->post_author, array(
525
				'size' => $size,
526
				'default' => $default,
527
			) );
528
		}
529
530
		return array(
531
			array(
532
				'type'       => 'image',
533
				'from'       => 'gravatar',
534
				'src'        => $url,
535
				'src_width'  => $size,
536
				'src_height' => $size,
537
				'href'       => $permalink,
538
				'alt_text'   => '',
539
			),
540
		);
541
	}
542
543
	/**
544
	 * Run through the different methods that we have available to try to find a single good
545
	 * display image for this post.
546
	 * @param  int $post_id
547
	 * @param array $args Other arguments (currently width and height required for images where possible to determine)
548
	 * @return Array containing details of the best image to be used
549
	 */
550
	static function get_image( $post_id, $args = array() ) {
551
		$image = '';
552
553
		/**
554
		 * Fires before we find a single good image for a specific post.
555
		 *
556
		 * @since 2.2.0
557
		 *
558
		 * @param int $post_id Post ID.
559
		 */
560
		do_action( 'jetpack_postimages_pre_get_image', $post_id );
561
		$media = self::get_images( $post_id, $args );
562
563
564
		if ( is_array( $media ) ) {
565
			foreach ( $media as $item ) {
566
				if ( 'image' == $item['type'] ) {
567
					$image = $item;
568
					break;
569
				}
570
			}
571
		}
572
573
		/**
574
		 * Fires after we find a single good image for a specific post.
575
		 *
576
		 * @since 2.2.0
577
		 *
578
		 * @param int $post_id Post ID.
579
		 */
580
		do_action( 'jetpack_postimages_post_get_image', $post_id );
581
582
		return $image;
583
	}
584
585
	/**
586
	 * Get an array containing a collection of possible images for this post, stopping once we hit a method
587
	 * that returns something useful.
588
	 * @param  int $post_id
589
	 * @param  array  $args Optional args, see defaults list for details
590
	 * @return Array containing images that would be good for representing this post
591
	 */
592
	static function get_images( $post_id, $args = array() ) {
593
		// Figure out which image to attach to this post.
594
		$media = false;
595
596
		/**
597
		 * Filters the array of images that would be good for a specific post.
598
		 * This filter is applied before options ($args) filter the original array.
599
		 *
600
		 * @since 2.0.0
601
		 *
602
		 * @param array $media Array of images that would be good for a specific post.
603
		 * @param int $post_id Post ID.
604
		 * @param array $args Array of options to get images.
605
		 */
606
		$media = apply_filters( 'jetpack_images_pre_get_images', $media, $post_id, $args );
607
		if ( $media )
608
			return $media;
609
610
		$defaults = array(
611
			'width'               => 200, // Required minimum width (if possible to determine)
612
			'height'              => 200, // Required minimum height (if possible to determine)
613
614
			'fallback_to_avatars' => false, // Optionally include Blavatar and Gravatar (in that order) in the image stack
615
			'avatar_size'         => 96, // Used for both Grav and Blav
616
			'gravatar_default'    => false, // Default image to use if we end up with no Gravatar
617
618
			'from_thumbnail'      => true, // Use these flags to specify which methods to use to find an image
619
			'from_slideshow'      => true,
620
			'from_gallery'        => true,
621
			'from_attachment'     => true,
622
			'from_blocks'         => true,
623
			'from_html'           => true,
624
625
			'html_content'        => '' // HTML string to pass to from_html()
626
		);
627
		$args = wp_parse_args( $args, $defaults );
628
629
		$media = false;
630
		if ( $args['from_thumbnail'] )
631
			$media = self::from_thumbnail( $post_id, $args['width'], $args['height'] );
632 View Code Duplication
		if ( !$media && $args['from_slideshow'] )
633
			$media = self::from_slideshow( $post_id, $args['width'], $args['height'] );
634
		if ( !$media && $args['from_gallery'] )
635
			$media = self::from_gallery( $post_id );
636 View Code Duplication
		if ( !$media && $args['from_attachment'] )
637
			$media = self::from_attachment( $post_id, $args['width'], $args['height'] );
638 View Code Duplication
		if ( ! $media && $args['from_blocks'] ) {
639
			if ( empty( $args['html_content'] ) )
640
				$media = self::from_blocks( $post_id, $args['width'], $args['height'] ); // Use the post_id, which will load the content
641
			else
642
				$media = self::from_blocks( $args['html_content'], $args['width'], $args['height'] ); // If html_content is provided, use that
643
		}
644 View Code Duplication
		if ( !$media && $args['from_html'] ) {
645
			if ( empty( $args['html_content'] ) )
646
				$media = self::from_html( $post_id, $args['width'], $args['height'] ); // Use the post_id, which will load the content
647
			else
648
				$media = self::from_html( $args['html_content'], $args['width'], $args['height'] ); // If html_content is provided, use that
649
		}
650
651
		if ( !$media && $args['fallback_to_avatars'] ) {
652
			$media = self::from_blavatar( $post_id, $args['avatar_size'] );
653
			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...
654
				$media = self::from_gravatar( $post_id, $args['avatar_size'], $args['gravatar_default'] );
655
		}
656
657
		/**
658
		 * Filters the array of images that would be good for a specific post.
659
		 * This filter is applied after options ($args) filter the original array.
660
		 *
661
		 * @since 2.0.0
662
		 *
663
		 * @param array $media Array of images that would be good for a specific post.
664
		 * @param int $post_id Post ID.
665
		 * @param array $args Array of options to get images.
666
		 */
667
		return apply_filters( 'jetpack_images_get_images', $media, $post_id, $args );
668
	}
669
670
	/**
671
	 * Takes an image URL and pixel dimensions then returns a URL for the
672
	 * resized and cropped image.
673
	 *
674
	 * @param  string $src
675
	 * @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...
676
	 * @return string            Transformed image URL
677
	 */
678
	static function fit_image_url( $src, $width, $height ) {
679
		$width = (int) $width;
680
		$height = (int) $height;
681
682
		if ( $width < 1 || $height < 1 ) {
683
			return $src;
684
		}
685
686
		// See if we should bypass WordPress.com SaaS resizing
687
		if ( has_filter( 'jetpack_images_fit_image_url_override' ) ) {
688
			/**
689
			 * Filters the image URL used after dimensions are set by Photon.
690
			 *
691
			 * @since 3.3.0
692
			 *
693
			 * @param string $src Image URL.
694
			 * @param int $width Image width.
695
			 * @param int $width Image height.
696
			 */
697
			return apply_filters( 'jetpack_images_fit_image_url_override', $src, $width, $height );
698
		}
699
700
		// If WPCOM hosted image use native transformations
701
		$img_host = parse_url( $src, PHP_URL_HOST );
702
		if ( '.files.wordpress.com' == substr( $img_host, -20 ) ) {
703
			return add_query_arg( array( 'w' => $width, 'h' => $height, 'crop' => 1 ), set_url_scheme( $src ) );
704
		}
705
706
		// Use Photon magic
707
		if( function_exists( 'jetpack_photon_url' ) ) {
708
			return jetpack_photon_url( $src, array( 'resize' => "$width,$height" ) );
709
		}
710
711
		// Arg... no way to resize image using WordPress.com infrastructure!
712
		return $src;
713
	}
714
715
	/**
716
	 * Get HTML from given post content.
717
	 *
718
	 * @since 6.9.0
719
	 *
720
	 * @param mixed $html_or_id The HTML string to parse for images, or a post id.
721
	 *
722
	 * @return array $html_info {
723
	 * @type string $html     Post content.
724
	 * @type string $post_url Post URL.
725
	 * }
726
	 */
727
	static function get_post_html( $html_or_id ) {
728
		if ( is_numeric( $html_or_id ) ) {
729
			$post = get_post( $html_or_id );
730
731
			if ( empty( $post ) || ! empty( $post->post_password ) ) {
732
				return '';
733
			}
734
735
			$html_info = array(
736
				'html'     => $post->post_content, // DO NOT apply the_content filters here, it will cause loops.
737
				'post_url' => get_permalink( $post->ID ),
738
			);
739
		} else {
740
			$html_info = array(
741
				'html'     => $html_or_id,
742
				'post_url' => '',
743
			);
744
		}
745
		return $html_info;
746
	}
747
748
	/**
749
	 * Get info about a WordPress attachment.
750
	 *
751
	 * @since 6.9.0
752
	 *
753
	 * @param int    $attachment_id Attachment ID.
754
	 * @param string $post_url      URL of the post, if we have one.
755
	 * @param int    $width         Minimum Image width.
756
	 * @param int    $height        Minimum Image height.
757
	 * @return array|bool           Image data or false if unavailable.
758
	 */
759
	public static function get_attachment_data( $attachment_id, $post_url = '', $width, $height ) {
760
		if ( empty( $attachment_id ) ) {
761
			return false;
762
		}
763
764
		$meta = wp_get_attachment_metadata( $attachment_id );
765
766
		// The image must be larger than 200x200.
767
		if ( ! isset( $meta['width'] ) || $meta['width'] < $width ) {
768
			return false;
769
		}
770
		if ( ! isset( $meta['height'] ) || $meta['height'] < $height ) {
771
			return false;
772
		}
773
774
		$url = wp_get_attachment_url( $attachment_id );
775
776
		return array(
777
			'type'       => 'image',
778
			'from'       => 'attachment',
779
			'src'        => $url,
780
			'src_width'  => $meta['width'],
781
			'src_height' => $meta['height'],
782
			'href'       => $post_url,
783
			'alt_text'   => self::get_alt_text( $attachment_id ),
784
		);
785
	}
786
787
	/**
788
	 * Get the alt text for an image or other media from the Media Library.
789
	 *
790
	 * @since 7.1
791
	 *
792
	 * @param int $attachment_id The Post ID of the media.
793
	 * @return string The alt text value or an emptry string.
794
	 */
795
	public static function get_alt_text( $attachment_id ) {
796
		return get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
797
	}
798
}
799