PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.0
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 / open-graph / image-helper.php

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

108 lines 2.8 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\Open_Graph;
4
5 use Yoast\WP\SEO\Helpers\Image_Helper as Base_Image_Helper;
6 use Yoast\WP\SEO\Helpers\Url_Helper;
7
8 /**
9 * A helper object for Open Graph images.
10 */
11 class Image_Helper {
12
13 /**
14 * The URL helper.
15 *
16 * @var Url_Helper
17 */
18 private $url;
19
20 /**
21 * The base image helper.
22 *
23 * @var Base_Image_Helper
24 */
25 private $image;
26
27 /**
28 * Image_Helper constructor.
29 *
30 * @codeCoverageIgnore
31 *
32 * @param Url_Helper $url The url helper.
33 * @param Base_Image_Helper $image The image helper.
34 */
35 public function __construct( Url_Helper $url, Base_Image_Helper $image ) {
36 $this->url = $url;
37 $this->image = $image;
38 }
39
40 /**
41 * Determines whether the passed URL is considered valid.
42 *
43 * @param array $image The image array.
44 *
45 * @return bool Whether or not the URL is a valid image.
46 */
47 public function is_image_url_valid( array $image ) {
48 if ( empty( $image['url'] ) || ! \is_string( $image['url'] ) ) {
49 return false;
50 }
51
52 $image_extension = $this->url->get_extension_from_url( $image['url'] );
53 $is_valid = $this->image->is_extension_valid( $image_extension );
54
55 /**
56 * Filter: 'wpseo_opengraph_is_valid_image_url' - Allows extra validation for an image url.
57 *
58 * @api bool - Current validation result.
59 *
60 * @param string $url The image url to validate.
61 */
62 return (bool) \apply_filters( 'wpseo_opengraph_is_valid_image_url', $is_valid, $image['url'] );
63 }
64
65 /**
66 * Retrieves the overridden image size value.
67 *
68 * @return string|null The image size when overriden by filter or null when not.
69 */
70 public function get_override_image_size() {
71 /**
72 * Filter: 'wpseo_opengraph_image_size' - Allow overriding the image size used
73 * for Open Graph sharing. If this filter is used, the defined size will always be
74 * used for the og:image. The image will still be rejected if it is too small.
75 *
76 * Only use this filter if you manually want to determine the best image size
77 * for the `og:image` tag.
78 *
79 * Use the `wpseo_image_sizes` filter if you want to use our logic. That filter
80 * can be used to add an image size that needs to be taken into consideration
81 * within our own logic.
82 *
83 * @api string|false $size Size string.
84 */
85 return \apply_filters( 'wpseo_opengraph_image_size', null );
86 }
87
88 /**
89 * Retrieves the image data by a given attachment id.
90 *
91 * @param int $attachment_id The attachment id.
92 *
93 * @return array|false The image data when found, `false` when not.
94 */
95 public function get_image_by_id( $attachment_id ) {
96 if ( ! $this->image->is_valid_attachment( $attachment_id ) ) {
97 return false;
98 }
99
100 $override_image_size = $this->get_override_image_size();
101 if ( $override_image_size ) {
102 return $this->image->get_image( $attachment_id, $override_image_size );
103 }
104
105 return $this->image->get_best_attachment_variation( $attachment_id );
106 }
107 }
108