| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers\Schema; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Helpers\Image_Helper as Main_Image_Helper; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Image_Helper. |
| 9 |
*/ |
| 10 |
class Image_Helper { |
| 11 |
|
| 12 |
/** |
| 13 |
* The HTML helper. |
| 14 |
* |
| 15 |
* @var HTML_Helper |
| 16 |
*/ |
| 17 |
private $html; |
| 18 |
|
| 19 |
/** |
| 20 |
* The language helper. |
| 21 |
* |
| 22 |
* @var Language_Helper |
| 23 |
*/ |
| 24 |
private $language; |
| 25 |
|
| 26 |
/** |
| 27 |
* Represents the main image helper. |
| 28 |
* |
| 29 |
* @var Main_Image_Helper |
| 30 |
*/ |
| 31 |
private $image; |
| 32 |
|
| 33 |
/** |
| 34 |
* Image_Helper constructor. |
| 35 |
* |
| 36 |
* @codeCoverageIgnore It handles dependencies. |
| 37 |
* |
| 38 |
* @param HTML_Helper $html The HTML helper. |
| 39 |
* @param Language_Helper $language The language helper. |
| 40 |
* @param Main_Image_Helper $image The 'main' image helper. |
| 41 |
*/ |
| 42 |
public function __construct( HTML_Helper $html, Language_Helper $language, Main_Image_Helper $image ) { |
| 43 |
$this->html = $html; |
| 44 |
$this->language = $language; |
| 45 |
$this->image = $image; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Find an image based on its URL and generate a Schema object for it. |
| 50 |
* |
| 51 |
* @param string $schema_id The `@id` to use for the returned image. |
| 52 |
* @param string $url The image URL to base our object on. |
| 53 |
* @param string $caption An optional caption. |
| 54 |
* @param bool $add_hash Whether a hash will be added as a suffix in the @id. |
| 55 |
* @param bool $use_link_table Whether the SEO Links table will be used to retrieve the id. |
| 56 |
* |
| 57 |
* @return array Schema ImageObject array. |
| 58 |
*/ |
| 59 |
public function generate_from_url( $schema_id, $url, $caption = '', $add_hash = false, $use_link_table = true ) { |
| 60 |
$attachment_id = $this->image->get_attachment_by_url( $url, $use_link_table ); |
| 61 |
if ( $attachment_id > 0 ) { |
| 62 |
return $this->generate_from_attachment_id( $schema_id, $attachment_id, $caption, $add_hash ); |
| 63 |
} |
| 64 |
|
| 65 |
return $this->simple_image_object( $schema_id, $url, $caption, $add_hash ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Retrieve data about an image from the database and use it to generate a Schema object. |
| 70 |
* |
| 71 |
* @param string $schema_id The `@id` to use for the returned image. |
| 72 |
* @param int $attachment_id The attachment to retrieve data from. |
| 73 |
* @param string $caption The caption string, if there is one. |
| 74 |
* @param bool $add_hash Whether a hash will be added as a suffix in the @id. |
| 75 |
* |
| 76 |
* @return array Schema ImageObject array. |
| 77 |
*/ |
| 78 |
public function generate_from_attachment_id( $schema_id, $attachment_id, $caption = '', $add_hash = false ) { |
| 79 |
$data = $this->generate_object(); |
| 80 |
$url = $this->image->get_attachment_image_url( $attachment_id, 'full' ); |
| 81 |
|
| 82 |
$id_suffix = ( $add_hash ) ? \md5( $url ) : ''; |
| 83 |
|
| 84 |
$data['@id'] = $schema_id . $id_suffix; |
| 85 |
$data['url'] = $url; |
| 86 |
$data['contentUrl'] = $url; |
| 87 |
$data = $this->add_image_size( $data, $attachment_id ); |
| 88 |
$data = $this->add_caption( $data, $attachment_id, $caption ); |
| 89 |
|
| 90 |
return $data; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Retrieve data about an image from the database and use it to generate a Schema object. |
| 95 |
* |
| 96 |
* @param string $schema_id The `@id` to use for the returned image. |
| 97 |
* @param array $attachment_meta The attachment metadata. |
| 98 |
* @param string $caption The caption string, if there is one. |
| 99 |
* @param bool $add_hash Whether a hash will be added as a suffix in the @id. |
| 100 |
* |
| 101 |
* @return array Schema ImageObject array. |
| 102 |
*/ |
| 103 |
public function generate_from_attachment_meta( $schema_id, $attachment_meta, $caption = '', $add_hash = false ) { |
| 104 |
$data = $this->generate_object(); |
| 105 |
|
| 106 |
$id_suffix = ( $add_hash ) ? \md5( $attachment_meta['url'] ) : ''; |
| 107 |
|
| 108 |
$data['@id'] = $schema_id . $id_suffix; |
| 109 |
$data['url'] = $attachment_meta['url']; |
| 110 |
$data['contentUrl'] = $data['url']; |
| 111 |
$data['width'] = $attachment_meta['width']; |
| 112 |
$data['height'] = $attachment_meta['height']; |
| 113 |
if ( ! empty( $caption ) ) { |
| 114 |
$data['caption'] = $this->html->smart_strip_tags( $caption ); |
| 115 |
} |
| 116 |
|
| 117 |
return $data; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* If we can't find $url in our database, we output a simple ImageObject. |
| 122 |
* |
| 123 |
* @param string $schema_id The `@id` to use for the returned image. |
| 124 |
* @param string $url The image URL. |
| 125 |
* @param string $caption A caption, if set. |
| 126 |
* @param bool $add_hash Whether a hash will be added as a suffix in the @id. |
| 127 |
* |
| 128 |
* @return array Schema ImageObject array. |
| 129 |
*/ |
| 130 |
public function simple_image_object( $schema_id, $url, $caption = '', $add_hash = false ) { |
| 131 |
$data = $this->generate_object(); |
| 132 |
|
| 133 |
$id_suffix = ( $add_hash ) ? \md5( $url ) : ''; |
| 134 |
|
| 135 |
$data['@id'] = $schema_id . $id_suffix; |
| 136 |
$data['url'] = $url; |
| 137 |
$data['contentUrl'] = $url; |
| 138 |
|
| 139 |
if ( ! empty( $caption ) ) { |
| 140 |
$data['caption'] = $this->html->smart_strip_tags( $caption ); |
| 141 |
} |
| 142 |
|
| 143 |
return $data; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Retrieves an image's caption if set, or uses the alt tag if that's set. |
| 148 |
* |
| 149 |
* @param array $data An ImageObject Schema array. |
| 150 |
* @param int $attachment_id Attachment ID. |
| 151 |
* @param string $caption The caption string, if there is one. |
| 152 |
* |
| 153 |
* @return array An imageObject with width and height set if available. |
| 154 |
*/ |
| 155 |
private function add_caption( $data, $attachment_id, $caption = '' ) { |
| 156 |
if ( $caption !== '' ) { |
| 157 |
$data['caption'] = $caption; |
| 158 |
|
| 159 |
return $data; |
| 160 |
} |
| 161 |
|
| 162 |
$caption = $this->image->get_caption( $attachment_id ); |
| 163 |
if ( ! empty( $caption ) ) { |
| 164 |
$data['caption'] = $this->html->smart_strip_tags( $caption ); |
| 165 |
|
| 166 |
return $data; |
| 167 |
} |
| 168 |
|
| 169 |
return $data; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Generates our bare bone ImageObject. |
| 174 |
* |
| 175 |
* @return array an empty ImageObject |
| 176 |
*/ |
| 177 |
private function generate_object() { |
| 178 |
$data = [ |
| 179 |
'@type' => 'ImageObject', |
| 180 |
]; |
| 181 |
|
| 182 |
$data = $this->language->add_piece_language( $data ); |
| 183 |
|
| 184 |
return $data; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Adds image's width and height. |
| 189 |
* |
| 190 |
* @param array $data An ImageObject Schema array. |
| 191 |
* @param int $attachment_id Attachment ID. |
| 192 |
* |
| 193 |
* @return array An imageObject with width and height set if available. |
| 194 |
*/ |
| 195 |
private function add_image_size( $data, $attachment_id ) { |
| 196 |
$image_meta = $this->image->get_metadata( $attachment_id ); |
| 197 |
if ( empty( $image_meta['width'] ) || empty( $image_meta['height'] ) ) { |
| 198 |
return $data; |
| 199 |
} |
| 200 |
$data['width'] = $image_meta['width']; |
| 201 |
$data['height'] = $image_meta['height']; |
| 202 |
|
| 203 |
return $data; |
| 204 |
} |
| 205 |
} |
| 206 |
|