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

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