| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Values; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Helpers\Image_Helper; |
| 6 |
use Yoast\WP\SEO\Helpers\Url_Helper; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class Images |
| 10 |
* |
| 11 |
* Value object for the Images. |
| 12 |
*/ |
| 13 |
class Images { |
| 14 |
|
| 15 |
/** |
| 16 |
* The image size. |
| 17 |
* |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
public $image_size = 'full'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Holds the images that have been put out as image. |
| 24 |
* |
| 25 |
* @var array |
| 26 |
*/ |
| 27 |
protected $images = []; |
| 28 |
|
| 29 |
/** |
| 30 |
* The image helper. |
| 31 |
* |
| 32 |
* @var Image_Helper |
| 33 |
*/ |
| 34 |
protected $image; |
| 35 |
|
| 36 |
/** |
| 37 |
* The URL helper. |
| 38 |
* |
| 39 |
* @var Url_Helper |
| 40 |
*/ |
| 41 |
protected $url; |
| 42 |
|
| 43 |
/** |
| 44 |
* Images constructor. |
| 45 |
* |
| 46 |
* @codeCoverageIgnore |
| 47 |
* |
| 48 |
* @param Image_Helper $image The image helper. |
| 49 |
* @param Url_Helper $url The url helper. |
| 50 |
*/ |
| 51 |
public function __construct( Image_Helper $image, Url_Helper $url ) { |
| 52 |
$this->image = $image; |
| 53 |
$this->url = $url; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Adds an image to the list by image ID. |
| 58 |
* |
| 59 |
* @param int $image_id The image ID to add. |
| 60 |
* |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
public function add_image_by_id( $image_id ) { |
| 64 |
$image = $this->image->get_attachment_image_source( $image_id, $this->image_size ); |
| 65 |
if ( $image ) { |
| 66 |
$this->add_image( $image ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Adds an image to the list by image ID. |
| 72 |
* |
| 73 |
* @param string $image_meta JSON encoded image meta. |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public function add_image_by_meta( $image_meta ) { |
| 78 |
$this->add_image( (array) \json_decode( $image_meta ) ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Return the images array. |
| 83 |
* |
| 84 |
* @return array The images. |
| 85 |
*/ |
| 86 |
public function get_images() { |
| 87 |
return $this->images; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Check whether we have images or not. |
| 92 |
* |
| 93 |
* @return bool True if we have images, false if we don't. |
| 94 |
*/ |
| 95 |
public function has_images() { |
| 96 |
return ! empty( $this->images ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Adds an image based on a given URL. |
| 101 |
* |
| 102 |
* @param string $url The given URL. |
| 103 |
* |
| 104 |
* @return number|null Returns the found image ID if it exists. Otherwise -1. |
| 105 |
* If the URL is empty we return null. |
| 106 |
*/ |
| 107 |
public function add_image_by_url( $url ) { |
| 108 |
if ( empty( $url ) ) { |
| 109 |
return null; |
| 110 |
} |
| 111 |
|
| 112 |
$image_id = $this->image->get_attachment_by_url( $url ); |
| 113 |
|
| 114 |
if ( $image_id ) { |
| 115 |
$this->add_image_by_id( $image_id ); |
| 116 |
|
| 117 |
return $image_id; |
| 118 |
} |
| 119 |
|
| 120 |
$this->add_image( $url ); |
| 121 |
|
| 122 |
return -1; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Adds an image to the list of images. |
| 127 |
* |
| 128 |
* @param string|array $image Image array. |
| 129 |
* |
| 130 |
* @return void |
| 131 |
*/ |
| 132 |
public function add_image( $image ) { |
| 133 |
if ( \is_string( $image ) ) { |
| 134 |
$image = [ 'url' => $image ]; |
| 135 |
} |
| 136 |
|
| 137 |
if ( ! \is_array( $image ) || empty( $image['url'] ) || ! \is_string( $image['url'] ) ) { |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
if ( $this->url->is_relative( $image['url'] ) && $image['url'][0] === '/' ) { |
| 142 |
$image['url'] = $this->url->build_absolute_url( $image['url'] ); |
| 143 |
} |
| 144 |
|
| 145 |
if ( \array_key_exists( $image['url'], $this->images ) ) { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
$this->images[ $image['url'] ] = $image; |
| 150 |
} |
| 151 |
} |
| 152 |
|