PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.2
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 / schema / image-helper.php

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

194 lines 5.3 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\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 *
55 * @return array Schema ImageObject array.
56 */
57 public function generate_from_url( $schema_id, $url, $caption = '' ) {
58 $attachment_id = $this->image->get_attachment_by_url( $url );
59 if ( $attachment_id > 0 ) {
60 return $this->generate_from_attachment_id( $schema_id, $attachment_id, $caption );
61 }
62
63 return $this->simple_image_object( $schema_id, $url, $caption );
64 }
65
66 /**
67 * Retrieve data about an image from the database and use it to generate a Schema object.
68 *
69 * @param string $schema_id The `@id` to use for the returned image.
70 * @param int $attachment_id The attachment to retrieve data from.
71 * @param string $caption The caption string, if there is one.
72 *
73 * @return array Schema ImageObject array.
74 */
75 public function generate_from_attachment_id( $schema_id, $attachment_id, $caption = '' ) {
76 $data = $this->generate_object( $schema_id );
77
78 $data['url'] = $this->image->get_attachment_image_url( $attachment_id, 'full' );
79 $data['contentUrl'] = $data['url'];
80 $data = $this->add_image_size( $data, $attachment_id );
81 $data = $this->add_caption( $data, $attachment_id, $caption );
82
83 return $data;
84 }
85
86 /**
87 * Retrieve data about an image from the database and use it to generate a Schema object.
88 *
89 * @param string $schema_id The `@id` to use for the returned image.
90 * @param array $attachment_meta The attachment metadata.
91 * @param string $caption The caption string, if there is one.
92 *
93 * @return array Schema ImageObject array.
94 */
95 public function generate_from_attachment_meta( $schema_id, $attachment_meta, $caption = '' ) {
96 $data = $this->generate_object( $schema_id );
97
98 $data['url'] = $attachment_meta['url'];
99 $data['contentUrl'] = $data['url'];
100 $data['width'] = $attachment_meta['width'];
101 $data['height'] = $attachment_meta['height'];
102 if ( ! empty( $caption ) ) {
103 $data['caption'] = $this->html->smart_strip_tags( $caption );
104 }
105
106 return $data;
107 }
108
109 /**
110 * If we can't find $url in our database, we output a simple ImageObject.
111 *
112 * @param string $schema_id The `@id` to use for the returned image.
113 * @param string $url The image URL.
114 * @param string $caption A caption, if set.
115 *
116 * @return array Schema ImageObject array.
117 */
118 public function simple_image_object( $schema_id, $url, $caption = '' ) {
119 $data = $this->generate_object( $schema_id );
120
121 $data['url'] = $url;
122 $data['contentUrl'] = $url;
123
124 if ( ! empty( $caption ) ) {
125 $data['caption'] = $this->html->smart_strip_tags( $caption );
126 }
127
128 return $data;
129 }
130
131 /**
132 * Retrieves an image's caption if set, or uses the alt tag if that's set.
133 *
134 * @param array $data An ImageObject Schema array.
135 * @param int $attachment_id Attachment ID.
136 * @param string $caption The caption string, if there is one.
137 *
138 * @return array An imageObject with width and height set if available.
139 */
140 private function add_caption( $data, $attachment_id, $caption = '' ) {
141 if ( $caption !== '' ) {
142 $data['caption'] = $caption;
143
144 return $data;
145 }
146
147 $caption = $this->image->get_caption( $attachment_id );
148 if ( ! empty( $caption ) ) {
149 $data['caption'] = $this->html->smart_strip_tags( $caption );
150
151 return $data;
152 }
153
154 return $data;
155 }
156
157 /**
158 * Generates our bare bone ImageObject.
159 *
160 * @param string $schema_id The `@id` to use for the returned image.
161 *
162 * @return array an empty ImageObject
163 */
164 private function generate_object( $schema_id ) {
165 $data = [
166 '@type' => 'ImageObject',
167 '@id' => $schema_id,
168 ];
169
170 $data = $this->language->add_piece_language( $data );
171
172 return $data;
173 }
174
175 /**
176 * Adds image's width and height.
177 *
178 * @param array $data An ImageObject Schema array.
179 * @param int $attachment_id Attachment ID.
180 *
181 * @return array An imageObject with width and height set if available.
182 */
183 private function add_image_size( $data, $attachment_id ) {
184 $image_meta = $this->image->get_metadata( $attachment_id );
185 if ( empty( $image_meta['width'] ) || empty( $image_meta['height'] ) ) {
186 return $data;
187 }
188 $data['width'] = $image_meta['width'];
189 $data['height'] = $image_meta['height'];
190
191 return $data;
192 }
193 }
194