PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.6
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.6
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 / src / helpers / image-helper.php

image-helper.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.6, at src/helpers/image-helper.php

384 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Helpers;
4
5 use WPSEO_Image_Utils;
6 use Yoast\WP\SEO\Models\SEO_Links;
7 use Yoast\WP\SEO\Repositories\Indexable_Repository;
8
9 /**
10 * A helper object for images.
11 */
12 class Image_Helper {
13
14 /**
15 * Image types that are supported by Open Graph.
16 *
17 * @var array
18 */
19 protected static $valid_image_types = [ 'image/jpeg', 'image/gif', 'image/png' ];
20
21 /**
22 * Image extensions that are supported by Open Graph.
23 *
24 * @var array
25 */
26 protected static $valid_image_extensions = [ 'jpeg', 'jpg', 'gif', 'png' ];
27
28 /**
29 * Represents the indexables repository.
30 *
31 * @var Indexable_Repository
32 */
33 protected $indexable_repository;
34
35 /**
36 * The options helper.
37 *
38 * @var Options_Helper
39 */
40 private $options_helper;
41
42 /**
43 * The URL helper.
44 *
45 * @var Url_Helper
46 */
47 private $url_helper;
48
49 /**
50 * Image_Helper constructor.
51 *
52 * @param Indexable_Repository $indexable_repository The indexable repository.
53 * @param Options_Helper $options The options helper.
54 * @param Url_Helper $url_helper The URL helper.
55 */
56 public function __construct(
57 Indexable_Repository $indexable_repository,
58 Options_Helper $options,
59 Url_Helper $url_helper
60 ) {
61 $this->indexable_repository = $indexable_repository;
62 $this->options_helper = $options;
63 $this->url_helper = $url_helper;
64 }
65
66 /**
67 * Determines whether or not the wanted attachment is considered valid.
68 *
69 * @param int $attachment_id The attachment ID to get the attachment by.
70 *
71 * @return bool Whether or not the attachment is valid.
72 */
73 public function is_valid_attachment( $attachment_id ) {
74 if ( ! \wp_attachment_is_image( $attachment_id ) ) {
75 return false;
76 }
77
78 $post_mime_type = \get_post_mime_type( $attachment_id );
79 if ( $post_mime_type === false ) {
80 return false;
81 }
82
83 return $this->is_valid_image_type( $post_mime_type );
84 }
85
86 /**
87 * Checks if the given extension is a valid extension
88 *
89 * @param string $image_extension The image extension.
90 *
91 * @return bool True when valid.
92 */
93 public function is_extension_valid( $image_extension ) {
94 return \in_array( $image_extension, static::$valid_image_extensions, true );
95 }
96
97 /**
98 * Determines whether the passed mime type is a valid image type.
99 *
100 * @param string $mime_type The detected mime type.
101 *
102 * @return bool Whether or not the attachment is a valid image type.
103 */
104 public function is_valid_image_type( $mime_type ) {
105 return \in_array( $mime_type, static::$valid_image_types, true );
106 }
107
108 /**
109 * Retrieves the image source for an attachment.
110 *
111 * @param int $attachment_id The attachment.
112 * @param string $image_size The image size to retrieve.
113 *
114 * @return string The image url or an empty string when not found.
115 */
116 public function get_attachment_image_source( $attachment_id, $image_size = 'full' ) {
117 $attachment = \wp_get_attachment_image_src( $attachment_id, $image_size );
118
119 if ( ! $attachment ) {
120 return '';
121 }
122
123 return $attachment[0];
124 }
125
126 /**
127 * Retrieves the ID of the featured image.
128 *
129 * @param int $post_id The post id to get featured image id for.
130 *
131 * @return int|bool ID when found, false when not.
132 */
133 public function get_featured_image_id( $post_id ) {
134 if ( ! \has_post_thumbnail( $post_id ) ) {
135 return false;
136 }
137
138 return \get_post_thumbnail_id( $post_id );
139 }
140
141 /**
142 * Gets the image url from the content.
143 *
144 * @param int $post_id The post id to extract the images from.
145 *
146 * @return string The image url or an empty string when not found.
147 */
148 public function get_post_content_image( $post_id ) {
149 $image_url = $this->get_first_usable_content_image_for_post( $post_id );
150
151 if ( $image_url === null ) {
152 return '';
153 }
154
155 return $image_url;
156 }
157
158 /**
159 * Gets the first image url of a gallery.
160 *
161 * @param int $post_id Post ID to use.
162 *
163 * @return string The image url or an empty string when not found.
164 */
165 public function get_gallery_image( $post_id ) {
166 $post = \get_post( $post_id );
167 if ( \strpos( $post->post_content, '[gallery' ) === false ) {
168 return '';
169 }
170
171 $images = \get_post_gallery_images( $post );
172 if ( empty( $images ) ) {
173 return '';
174 }
175
176 return \reset( $images );
177 }
178
179 /**
180 * Gets the image url from the term content.
181 *
182 * @param int $term_id The term id to extract the images from.
183 *
184 * @return string The image url or an empty string when not found.
185 */
186 public function get_term_content_image( $term_id ) {
187 $image_url = $this->get_first_content_image_for_term( $term_id );
188
189 if ( $image_url === null ) {
190 return '';
191 }
192
193 return $image_url;
194 }
195
196 /**
197 * Retrieves the caption for an attachment.
198 *
199 * @param int $attachment_id Attachment ID.
200 *
201 * @return string The caption when found, empty string when no caption is found.
202 */
203 public function get_caption( $attachment_id ) {
204 $caption = \wp_get_attachment_caption( $attachment_id );
205 if ( ! empty( $caption ) ) {
206 return $caption;
207 }
208
209 $caption = \get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
210 if ( ! empty( $caption ) ) {
211 return $caption;
212 }
213
214 return '';
215 }
216
217 /**
218 * Retrieves the attachment metadata.
219 *
220 * @param int $attachment_id Attachment ID.
221 *
222 * @return array The metadata, empty array when no metadata is found.
223 */
224 public function get_metadata( $attachment_id ) {
225 $metadata = \wp_get_attachment_metadata( $attachment_id );
226 if ( ! $metadata || ! \is_array( $metadata ) ) {
227 return [];
228 }
229
230 return $metadata;
231 }
232
233 /**
234 * Retrieves the attachment image url.
235 *
236 * @param int $attachment_id Attachment ID.
237 * @param string $size The size to get.
238 *
239 * @return string The url when found, empty string otherwise.
240 */
241 public function get_attachment_image_url( $attachment_id, $size ) {
242 $url = \wp_get_attachment_image_url( $attachment_id, $size );
243 if ( ! $url ) {
244 return '';
245 }
246
247 return $url;
248 }
249
250 /**
251 * Find the right version of an image based on size.
252 *
253 * @codeCoverageIgnore - We have to write test when this method contains own code.
254 *
255 * @param int $attachment_id Attachment ID.
256 * @param string $size Size name.
257 *
258 * @return array|false Returns an array with image data on success, false on failure.
259 */
260 public function get_image( $attachment_id, $size ) {
261 return WPSEO_Image_Utils::get_image( $attachment_id, $size );
262 }
263
264 /**
265 * Retrieves the best attachment variation for the given attachment.
266 *
267 * @codeCoverageIgnore - We have to write test when this method contains own code.
268 *
269 * @param int $attachment_id The attachment id.
270 *
271 * @return bool|string The attachment url or false when no variations found.
272 */
273 public function get_best_attachment_variation( $attachment_id ) {
274 $variations = WPSEO_Image_Utils::get_variations( $attachment_id );
275 $variations = WPSEO_Image_Utils::filter_usable_file_size( $variations );
276
277 // If we are left without variations, there is no valid variation for this attachment.
278 if ( empty( $variations ) ) {
279 return false;
280 }
281
282 // The variations are ordered so the first variations is by definition the best one.
283 return \reset( $variations );
284 }
285
286 /**
287 * Find an attachment ID for a given URL.
288 *
289 * @param string $url The URL to find the attachment for.
290 *
291 * @return int The found attachment ID, or 0 if none was found.
292 */
293 public function get_attachment_by_url( $url ) {
294 // Strip out the size part of an image URL.
295 $url = \preg_replace( '/(.*)-\d+x\d+\.(jpeg|jpg|png|gif)$/', '$1.$2', $url );
296
297 // Don't try to do this for external URLs.
298 if ( $this->url_helper->get_link_type( $url ) === SEO_Links::TYPE_EXTERNAL ) {
299 return 0;
300 }
301
302 $indexable = $this->indexable_repository->find_by_permalink( $url );
303
304 if ( $indexable && $indexable->object_type === 'post' && $indexable->object_sub_type === 'attachment' ) {
305 return $indexable->object_id;
306 }
307
308 $post_id = WPSEO_Image_Utils::get_attachment_by_url( $url );
309
310 if ( $post_id !== 0 ) {
311 // Find the indexable, this triggers creating it so it can be found next time.
312 $this->indexable_repository->find_by_id_and_type( $post_id, 'post' );
313 }
314
315 return $post_id;
316 }
317
318 /**
319 * Retrieves an attachment ID for an image uploaded in the settings.
320 *
321 * Due to self::get_attachment_by_url returning 0 instead of false.
322 * 0 is also a possibility when no ID is available.
323 *
324 * @codeCoverageIgnore - We have to write test when this method contains own code.
325 *
326 * @param string $setting The setting the image is stored in.
327 *
328 * @return int|bool The attachment id, or false or 0 if no ID is available.
329 */
330 public function get_attachment_id_from_settings( $setting ) {
331 return WPSEO_Image_Utils::get_attachment_id_from_settings( $setting );
332 }
333
334 /**
335 * Based on and image ID return array with the best variation of that image. If it's not saved to the DB, save it to an option.
336 *
337 * @param string $setting The setting name. Should be company or person.
338 *
339 * @return array|bool Array with image details when the image is found, boolean when it's not found.
340 */
341 public function get_attachment_meta_from_settings( $setting ) {
342 $image_meta = $this->options_helper->get( $setting . '_meta', false );
343 if ( ! $image_meta ) {
344 $image_id = $this->options_helper->get( $setting . '_id', false );
345 if ( $image_id ) {
346 // 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.
347 // This means an attachment always exists, so doing this is only needed once.
348 $image_meta = $this->get_best_attachment_variation( $image_id );
349 if ( $image_meta ) {
350 $this->options_helper->set( $setting . '_meta', $image_meta );
351 }
352 }
353 }
354
355 return $image_meta;
356 }
357
358 /**
359 * Retrieves the first usable content image for a post.
360 *
361 * @codeCoverageIgnore - We have to write test when this method contains own code.
362 *
363 * @param int $post_id The post id to extract the images from.
364 *
365 * @return string|null
366 */
367 protected function get_first_usable_content_image_for_post( $post_id ) {
368 return WPSEO_Image_Utils::get_first_usable_content_image_for_post( $post_id );
369 }
370
371 /**
372 * Gets the term's first usable content image. Null if none is available.
373 *
374 * @codeCoverageIgnore - We have to write test when this method contains own code.
375 *
376 * @param int $term_id The term id.
377 *
378 * @return string|null The image URL.
379 */
380 protected function get_first_content_image_for_term( $term_id ) {
381 return WPSEO_Image_Utils::get_first_content_image_for_term( $term_id );
382 }
383 }
384