| 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 |
* @deprecated 22.4 |
| 44 |
* @codeCoverageIgnore |
| 45 |
* |
| 46 |
* @param array<array<string, string|int>> $image The image array. |
| 47 |
* |
| 48 |
* @return bool Whether or not the URL is a valid image. |
| 49 |
*/ |
| 50 |
public function is_image_url_valid( array $image ) { |
| 51 |
\_deprecated_function( __METHOD__, 'Yoast SEO 22.4' ); |
| 52 |
|
| 53 |
if ( empty( $image['url'] ) || ! \is_string( $image['url'] ) ) { |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
$image_extension = $this->url->get_extension_from_url( $image['url'] ); |
| 58 |
$is_valid = $this->image->is_extension_valid( $image_extension ); |
| 59 |
|
| 60 |
/** |
| 61 |
* Filter: 'wpseo_opengraph_is_valid_image_url' - Allows extra validation for an image url. |
| 62 |
* |
| 63 |
* @param bool $is_valid Current validation result. |
| 64 |
* @param string $url The image url to validate. |
| 65 |
*/ |
| 66 |
return (bool) \apply_filters( 'wpseo_opengraph_is_valid_image_url', $is_valid, $image['url'] ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Retrieves the overridden image size value. |
| 71 |
* |
| 72 |
* @return string|null The image size when overriden by filter or null when not. |
| 73 |
*/ |
| 74 |
public function get_override_image_size() { |
| 75 |
/** |
| 76 |
* Filter: 'wpseo_opengraph_image_size' - Allow overriding the image size used |
| 77 |
* for Open Graph sharing. If this filter is used, the defined size will always be |
| 78 |
* used for the og:image. The image will still be rejected if it is too small. |
| 79 |
* |
| 80 |
* Only use this filter if you manually want to determine the best image size |
| 81 |
* for the `og:image` tag. |
| 82 |
* |
| 83 |
* Use the `wpseo_image_sizes` filter if you want to use our logic. That filter |
| 84 |
* can be used to add an image size that needs to be taken into consideration |
| 85 |
* within our own logic. |
| 86 |
* |
| 87 |
* @param string|false $size Size string. |
| 88 |
*/ |
| 89 |
return \apply_filters( 'wpseo_opengraph_image_size', null ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Retrieves the image data by a given attachment id. |
| 94 |
* |
| 95 |
* @param int $attachment_id The attachment id. |
| 96 |
* |
| 97 |
* @return array<string, string|int>|false The image data when found, `false` when not. |
| 98 |
*/ |
| 99 |
public function get_image_by_id( $attachment_id ) { |
| 100 |
if ( ! $this->image->is_valid_attachment( $attachment_id ) ) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
$override_image_size = $this->get_override_image_size(); |
| 105 |
if ( $override_image_size ) { |
| 106 |
return $this->image->get_image( $attachment_id, $override_image_size ); |
| 107 |
} |
| 108 |
|
| 109 |
return $this->image->get_best_attachment_variation( $attachment_id ); |
| 110 |
} |
| 111 |
} |
| 112 |
|