PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.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 / generators / open-graph-image-generator.php

open-graph-image-generator.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.2, at src/generators/open-graph-image-generator.php

233 lines 6.1 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\Generators;
4
5 use Error;
6 use Yoast\WP\SEO\Context\Meta_Tags_Context;
7 use Yoast\WP\SEO\Helpers\Image_Helper;
8 use Yoast\WP\SEO\Helpers\Open_Graph\Image_Helper as Open_Graph_Image_Helper;
9 use Yoast\WP\SEO\Helpers\Options_Helper;
10 use Yoast\WP\SEO\Helpers\Url_Helper;
11 use Yoast\WP\SEO\Models\Indexable;
12 use Yoast\WP\SEO\Values\Open_Graph\Images;
13
14 /**
15 * Represents the generator class for the Open Graph images.
16 */
17 class Open_Graph_Image_Generator implements Generator_Interface {
18
19 /**
20 * The Open Graph image helper.
21 *
22 * @var Open_Graph_Image_Helper
23 */
24 protected $open_graph_image;
25
26 /**
27 * The image helper.
28 *
29 * @var Image_Helper
30 */
31 protected $image;
32
33 /**
34 * The URL helper.
35 *
36 * @var Url_Helper
37 */
38 protected $url;
39
40 /**
41 * The options helper.
42 *
43 * @var Options_Helper
44 */
45 private $options;
46
47 /**
48 * Images constructor.
49 *
50 * @codeCoverageIgnore
51 *
52 * @param Open_Graph_Image_Helper $open_graph_image Image helper for Open Graph.
53 * @param Image_Helper $image The image helper.
54 * @param Options_Helper $options The options helper.
55 * @param Url_Helper $url The url helper.
56 */
57 public function __construct(
58 Open_Graph_Image_Helper $open_graph_image,
59 Image_Helper $image,
60 Options_Helper $options,
61 Url_Helper $url
62 ) {
63 $this->open_graph_image = $open_graph_image;
64 $this->image = $image;
65 $this->options = $options;
66 $this->url = $url;
67 }
68
69 /**
70 * Retrieves the images for an indexable.
71 *
72 * For legacy reasons some plugins might expect we filter a WPSEO_Opengraph_Image object. That might cause
73 * type errors. This is why we try/catch our filters.
74 *
75 * @param Meta_Tags_Context $context The context.
76 *
77 * @return array The images.
78 */
79 public function generate( Meta_Tags_Context $context ) {
80 $image_container = $this->get_image_container();
81 $backup_image_container = $this->get_image_container();
82
83 try {
84 /**
85 * Filter: wpseo_add_opengraph_images - Allow developers to add images to the Open Graph tags.
86 *
87 * @param Yoast\WP\SEO\Values\Open_Graph\Images $image_container The current object.
88 */
89 \apply_filters( 'wpseo_add_opengraph_images', $image_container );
90 } catch ( Error $error ) {
91 $image_container = $backup_image_container;
92 }
93
94 $this->add_from_indexable( $context->indexable, $image_container );
95 $backup_image_container = $image_container;
96
97 try {
98 /**
99 * Filter: wpseo_add_opengraph_additional_images - Allows to add additional images to the Open Graph tags.
100 *
101 * @param Yoast\WP\SEO\Values\Open_Graph\Images $image_container The current object.
102 */
103 \apply_filters( 'wpseo_add_opengraph_additional_images', $image_container );
104 } catch ( Error $error ) {
105 $image_container = $backup_image_container;
106 }
107
108 $this->add_from_templates( $context, $image_container );
109 $this->add_from_default( $image_container );
110
111 return $image_container->get_images();
112 }
113
114 /**
115 * Retrieves the images for an author archive indexable.
116 *
117 * This is a custom method to address the case of Author Archives, since they always have an Open Graph image
118 * set in the indexable (even if it is an empty default Gravatar).
119 *
120 * @param Meta_Tags_Context $context The context.
121 *
122 * @return array The images.
123 */
124 public function generate_for_author_archive( Meta_Tags_Context $context ) {
125 $image_container = $this->get_image_container();
126
127 $this->add_from_templates( $context, $image_container );
128 if ( $image_container->has_images() ) {
129 return $image_container->get_images();
130 }
131
132 return $this->generate( $context );
133 }
134
135 /**
136 * Adds an image based on the given indexable.
137 *
138 * @param Indexable $indexable The indexable.
139 * @param Images $image_container The image container.
140 *
141 * @return void
142 */
143 protected function add_from_indexable( Indexable $indexable, Images $image_container ) {
144 if ( $indexable->open_graph_image_meta ) {
145 $image_container->add_image_by_meta( $indexable->open_graph_image_meta );
146 return;
147 }
148
149 if ( $indexable->open_graph_image_id ) {
150 $image_container->add_image_by_id( $indexable->open_graph_image_id );
151 return;
152 }
153
154 if ( $indexable->open_graph_image ) {
155 $meta_data = [];
156 if ( $indexable->open_graph_image_meta && \is_string( $indexable->open_graph_image_meta ) ) {
157 $meta_data = \json_decode( $indexable->open_graph_image_meta, true );
158 }
159
160 $image_container->add_image(
161 \array_merge(
162 (array) $meta_data,
163 [
164 'url' => $indexable->open_graph_image,
165 ],
166 ),
167 );
168 }
169 }
170
171 /**
172 * Retrieves the default Open Graph image.
173 *
174 * @param Images $image_container The image container.
175 *
176 * @return void
177 */
178 protected function add_from_default( Images $image_container ) {
179 if ( $image_container->has_images() ) {
180 return;
181 }
182
183 $default_image_id = $this->options->get( 'og_default_image_id', '' );
184 if ( $default_image_id ) {
185 $image_container->add_image_by_id( $default_image_id );
186
187 return;
188 }
189
190 $default_image_url = $this->options->get( 'og_default_image', '' );
191 if ( $default_image_url ) {
192 $image_container->add_image_by_url( $default_image_url );
193 }
194 }
195
196 /**
197 * Retrieves the default Open Graph image.
198 *
199 * @param Meta_Tags_Context $context The context.
200 * @param Images $image_container The image container.
201 *
202 * @return void
203 */
204 protected function add_from_templates( Meta_Tags_Context $context, Images $image_container ) {
205 if ( $image_container->has_images() ) {
206 return;
207 }
208
209 if ( $context->presentation->open_graph_image_id ) {
210 $image_container->add_image_by_id( $context->presentation->open_graph_image_id );
211 return;
212 }
213
214 if ( $context->presentation->open_graph_image ) {
215 $image_container->add_image_by_url( $context->presentation->open_graph_image );
216 }
217 }
218
219 /**
220 * Retrieves an instance of the image container.
221 *
222 * @codeCoverageIgnore
223 *
224 * @return Images The image container.
225 */
226 protected function get_image_container() {
227 $image_container = new Images( $this->image, $this->url );
228 $image_container->set_helpers( $this->open_graph_image );
229
230 return $image_container;
231 }
232 }
233