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

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