PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / inc / class-wpseo-image-utils.php

class-wpseo-image-utils.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.5, at inc/class-wpseo-image-utils.php

486 lines 13.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO
6 */
7
8 /**
9 * WPSEO_Image_Utils.
10 */
11 class WPSEO_Image_Utils {
12
13 /**
14 * Find an attachment ID for a given URL.
15 *
16 * @param string $url The URL to find the attachment for.
17 *
18 * @return int The found attachment ID, or 0 if none was found.
19 */
20 public static function get_attachment_by_url( $url ) {
21 /*
22 * As get_attachment_by_url won't work on resized versions of images,
23 * we strip out the size part of an image URL.
24 */
25 $url = preg_replace( '/(.*)-\d+x\d+\.(jpg|png|gif)$/', '$1.$2', $url );
26
27 static $uploads;
28
29 if ( $uploads === null ) {
30 $uploads = wp_get_upload_dir();
31 }
32
33 // Don't try to do this for external URLs.
34 if ( strpos( $url, $uploads['baseurl'] ) !== 0 ) {
35 return 0;
36 }
37
38 if ( function_exists( 'wpcom_vip_attachment_url_to_postid' ) ) {
39 // @codeCoverageIgnoreStart -- We can't test this properly.
40 return (int) wpcom_vip_attachment_url_to_postid( $url );
41 // @codeCoverageIgnoreEnd -- The rest we _can_ test.
42 }
43
44 return self::attachment_url_to_postid( $url );
45 }
46
47 /**
48 * Implements the attachment_url_to_postid with use of WP Cache.
49 *
50 * @param string $url The attachment URL for which we want to know the Post ID.
51 *
52 * @return int The Post ID belonging to the attachment, 0 if not found.
53 */
54 protected static function attachment_url_to_postid( $url ) {
55 $cache_key = sprintf( 'yoast_attachment_url_post_id_%s', md5( $url ) );
56
57 // Set the ID based on the hashed URL in the cache.
58 $id = wp_cache_get( $cache_key );
59
60 if ( $id === 'not_found' ) {
61 return 0;
62 }
63
64 // ID is found in cache, return.
65 if ( $id !== false ) {
66 return $id;
67 }
68
69 // Note: We use the WP COM version if we can, see above.
70 $id = attachment_url_to_postid( $url );
71
72 if ( empty( $id ) ) {
73 wp_cache_set( $cache_key, 'not_found', '', ( 12 * HOUR_IN_SECONDS + wp_rand( 0, ( 4 * HOUR_IN_SECONDS ) ) ) );
74 return 0;
75 }
76
77 // We have the Post ID, but it's not in the cache yet. We do that here and return.
78 wp_cache_set( $cache_key, $id, '', ( 24 * HOUR_IN_SECONDS + wp_rand( 0, ( 12 * HOUR_IN_SECONDS ) ) ) );
79 return $id;
80 }
81
82 /**
83 * Retrieves the image data.
84 *
85 * @param array $image Image array with URL and metadata.
86 * @param int $attachment_id Attachment ID.
87 *
88 * @return false|array {
89 * Array of image data
90 *
91 * @type string $alt Image's alt text.
92 * @type string $path Path of image.
93 * @type int $width Width of image.
94 * @type int $height Height of image.
95 * @type string $type Image's MIME type.
96 * @type string $size Image's size.
97 * @type string $url Image's URL.
98 * @type int $filesize The file size in bytes, if already set.
99 * }
100 */
101 public static function get_data( $image, $attachment_id ) {
102 if ( ! is_array( $image ) ) {
103 return false;
104 }
105
106 // Deals with non-set keys and values being null or false.
107 if ( empty( $image['width'] ) || empty( $image['height'] ) ) {
108 return false;
109 }
110
111 $image['id'] = $attachment_id;
112 $image['alt'] = self::get_alt_tag( $attachment_id );
113 $image['pixels'] = ( (int) $image['width'] * (int) $image['height'] );
114
115 if ( ! isset( $image['type'] ) ) {
116 $image['type'] = get_post_mime_type( $attachment_id );
117 }
118
119 /**
120 * Filter: 'wpseo_image_data' - Filter image data.
121 *
122 * Elements with keys not listed in the section will be discarded.
123 *
124 * @api array {
125 * Array of image data
126 *
127 * @type int id Image's ID as an attachment.
128 * @type string alt Image's alt text.
129 * @type string path Image's path.
130 * @type int width Width of image.
131 * @type int height Height of image.
132 * @type int pixels Number of pixels in the image.
133 * @type string type Image's MIME type.
134 * @type string size Image's size.
135 * @type string url Image's URL.
136 * @type int filesize The file size in bytes, if already set.
137 * }
138 * @api int Attachment ID.
139 */
140 $image = apply_filters( 'wpseo_image_data', $image, $attachment_id );
141
142 // Keep only the keys we need, and nothing else.
143 return array_intersect_key( $image, array_flip( [ 'id', 'alt', 'path', 'width', 'height', 'pixels', 'type', 'size', 'url', 'filesize' ] ) );
144 }
145
146 /**
147 * Checks a size version of an image to see if it's not too heavy.
148 *
149 * @param array $image Image to check the file size of.
150 *
151 * @return bool True when the image is within limits, false if not.
152 */
153 public static function has_usable_file_size( $image ) {
154 if ( ! is_array( $image ) || $image === [] ) {
155 return false;
156 }
157
158 /**
159 * Filter: 'wpseo_image_image_weight_limit' - Determines what the maximum weight
160 * (in bytes) of an image is allowed to be, default is 2 MB.
161 *
162 * @api int - The maximum weight (in bytes) of an image.
163 */
164 $max_size = apply_filters( 'wpseo_image_image_weight_limit', 2097152 );
165
166 // We cannot check without a path, so assume it's fine.
167 if ( ! isset( $image['path'] ) ) {
168 return true;
169 }
170
171 return ( self::get_file_size( $image ) <= $max_size );
172 }
173
174 /**
175 * Find the right version of an image based on size.
176 *
177 * @param int $attachment_id Attachment ID.
178 * @param string $size Size name.
179 *
180 * @return array|false Returns an array with image data on success, false on failure.
181 */
182 public static function get_image( $attachment_id, $size ) {
183 $image = false;
184 if ( $size === 'full' ) {
185 $image = self::get_full_size_image_data( $attachment_id );
186 }
187
188 if ( ! $image ) {
189 $image = image_get_intermediate_size( $attachment_id, $size );
190 }
191
192 if ( ! $image ) {
193 return false;
194 }
195
196 $image['size'] = $size;
197
198 return self::get_data( $image, $attachment_id );
199 }
200
201 /**
202 * Returns the image data for the full size image.
203 *
204 * @param int $attachment_id Attachment ID.
205 *
206 * @return array|false Array when there is a full size image. False if not.
207 */
208 protected static function get_full_size_image_data( $attachment_id ) {
209 $image = wp_get_attachment_metadata( $attachment_id );
210 if ( ! is_array( $image ) ) {
211 return false;
212 }
213
214 $image['url'] = wp_get_attachment_image_url( $attachment_id, 'full' );
215 $image['path'] = get_attached_file( $attachment_id );
216 $image['size'] = 'full';
217
218 return $image;
219 }
220
221 /**
222 * Finds the full file path for a given image file.
223 *
224 * @param string $path The relative file path.
225 *
226 * @return string The full file path.
227 */
228 public static function get_absolute_path( $path ) {
229 static $uploads;
230
231 if ( $uploads === null ) {
232 $uploads = wp_get_upload_dir();
233 }
234
235 // Add the uploads basedir if the path does not start with it.
236 if ( empty( $uploads['error'] ) && strpos( $path, $uploads['basedir'] ) !== 0 ) {
237 return $uploads['basedir'] . DIRECTORY_SEPARATOR . ltrim( $path, DIRECTORY_SEPARATOR );
238 }
239
240 return $path;
241 }
242
243 /**
244 * Get the relative path of the image.
245 *
246 * @param string $img Image URL.
247 *
248 * @return string The expanded image URL.
249 */
250 public static function get_relative_path( $img ) {
251 if ( $img[0] !== '/' ) {
252 return $img;
253 }
254
255 // If it's a relative URL, it's relative to the domain, not necessarily to the WordPress install, we
256 // want to preserve domain name and URL scheme (http / https) though.
257 $parsed_url = wp_parse_url( home_url() );
258 $img = $parsed_url['scheme'] . '://' . $parsed_url['host'] . $img;
259
260 return $img;
261 }
262
263 /**
264 * Get the image file size.
265 *
266 * @param array $image An image array object.
267 *
268 * @return int The file size in bytes.
269 */
270 public static function get_file_size( $image ) {
271 if ( isset( $image['filesize'] ) ) {
272 return $image['filesize'];
273 }
274
275 // If the file size for the file is over our limit, we're going to go for a smaller version.
276 // @todo Save the filesize to the image metadata.
277 // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- If file size doesn't properly return, we'll not fail.
278 return @filesize( self::get_absolute_path( $image['path'] ) );
279 }
280
281 /**
282 * Returns the different image variations for consideration.
283 *
284 * @param int $attachment_id The attachment to return the variations for.
285 *
286 * @return array The different variations possible for this attachment ID.
287 */
288 public static function get_variations( $attachment_id ) {
289 $variations = [];
290
291 foreach ( self::get_sizes() as $size ) {
292 $variation = self::get_image( $attachment_id, $size );
293
294 // The get_image function returns false if the size doesn't exist for this attachment.
295 if ( $variation ) {
296 $variations[] = $variation;
297 }
298 }
299
300 return $variations;
301 }
302
303 /**
304 * Check original size of image. If original image is too small, return false, else return true.
305 *
306 * Filters a list of variations by a certain set of usable dimensions.
307 *
308 * @param array $usable_dimensions {
309 * The parameters to check against.
310 *
311 * @type int $min_width Minimum width of image.
312 * @type int $max_width Maximum width of image.
313 * @type int $min_height Minimum height of image.
314 * @type int $max_height Maximum height of image.
315 * }
316 * @param array $variations The variations that should be considered.
317 *
318 * @return array Whether a variation is fit for display or not.
319 */
320 public static function filter_usable_dimensions( $usable_dimensions, $variations ) {
321 $filtered = [];
322
323 foreach ( $variations as $variation ) {
324 $dimensions = $variation;
325
326 if ( self::has_usable_dimensions( $dimensions, $usable_dimensions ) ) {
327 $filtered[] = $variation;
328 }
329 }
330
331 return $filtered;
332 }
333
334 /**
335 * Filters a list of variations by (disk) file size.
336 *
337 * @param array $variations The variations to consider.
338 *
339 * @return array The validations that pass the required file size limits.
340 */
341 public static function filter_usable_file_size( $variations ) {
342 foreach ( $variations as $variation ) {
343 // We return early to prevent measuring the file size of all the variations.
344 if ( self::has_usable_file_size( $variation ) ) {
345 return [ $variation ];
346 }
347 }
348
349 return [];
350 }
351
352 /**
353 * Retrieve the internal WP image file sizes.
354 *
355 * @return array An array of image sizes.
356 */
357 public static function get_sizes() {
358 /**
359 * Filter: 'wpseo_image_sizes' - Determines which image sizes we'll loop through to get an appropriate image.
360 *
361 * @api array - The array of image sizes to loop through.
362 */
363 return apply_filters( 'wpseo_image_sizes', [ 'full', 'large', 'medium_large' ] );
364 }
365
366 /**
367 * Grabs an image alt text.
368 *
369 * @param int $attachment_id The attachment ID.
370 *
371 * @return string The image alt text.
372 */
373 public static function get_alt_tag( $attachment_id ) {
374 return (string) get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
375 }
376
377 /**
378 * Checks whether an img sizes up to the parameters.
379 *
380 * @param array $dimensions The image values.
381 * @param array $usable_dimensions The parameters to check against.
382 *
383 * @return bool True if the image has usable measurements, false if not.
384 */
385 private static function has_usable_dimensions( $dimensions, $usable_dimensions ) {
386 foreach ( [ 'width', 'height' ] as $param ) {
387 $minimum = $usable_dimensions[ 'min_' . $param ];
388 $maximum = $usable_dimensions[ 'max_' . $param ];
389
390 $current = $dimensions[ $param ];
391 if ( ( $current < $minimum ) || ( $current > $maximum ) ) {
392 return false;
393 }
394 }
395
396 return true;
397 }
398
399 /**
400 * Gets the post's first usable content image. Null if none is available.
401 *
402 * @param int|null $post_id The post id.
403 *
404 * @return string|null The image URL.
405 */
406 public static function get_first_usable_content_image_for_post( $post_id = null ) {
407 $post = get_post( $post_id );
408
409 // We know get_post() returns the post or null.
410 if ( ! $post ) {
411 return null;
412 }
413
414 $image_finder = new WPSEO_Content_Images();
415 $images = $image_finder->get_images( $post->ID, $post );
416
417 return self::get_first_image( $images );
418 }
419
420 /**
421 * Gets the term's first usable content image. Null if none is available.
422 *
423 * @param int $term_id The term id.
424 *
425 * @return string|null The image URL.
426 */
427 public static function get_first_content_image_for_term( $term_id ) {
428 $term_description = term_description( $term_id );
429
430 // We know term_description() returns a string which may be empty.
431 if ( $term_description === '' ) {
432 return null;
433 }
434
435 $image_finder = new WPSEO_Content_Images();
436 $images = $image_finder->get_images_from_content( $term_description );
437
438 return self::get_first_image( $images );
439 }
440
441 /**
442 * Retrieves an attachment ID for an image uploaded in the settings.
443 *
444 * Due to self::get_attachment_by_url returning 0 instead of false.
445 * 0 is also a possibility when no ID is available.
446 *
447 * @param string $setting The setting the image is stored in.
448 *
449 * @return int|bool The attachment id, or false or 0 if no ID is available.
450 */
451 public static function get_attachment_id_from_settings( $setting ) {
452 $image_id = WPSEO_Options::get( $setting . '_id', false );
453 if ( ! $image_id ) {
454 $image = WPSEO_Options::get( $setting, false );
455 if ( $image ) {
456 // There is not an option to put a URL in an image field in the settings anymore, only to upload it through the media manager.
457 // This means an attachment always exists, so doing this is only needed once.
458 $image_id = self::get_attachment_by_url( $image );
459 WPSEO_Options::set( $setting . '_id', $image_id );
460 }
461 }
462
463 return $image_id;
464 }
465
466 /**
467 * Retrieves the first possible image url from an array of images.
468 *
469 * @param array $images The array to extract image url from.
470 *
471 * @return string|null The extracted image url when found, null when not found.
472 */
473 protected static function get_first_image( $images ) {
474 if ( ! is_array( $images ) ) {
475 return null;
476 }
477
478 $images = array_filter( $images );
479 if ( empty( $images ) ) {
480 return null;
481 }
482
483 return reset( $images );
484 }
485 }
486