PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 19.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v19.0
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 19.0, at src/generators/open-graph-image-generator.php

227 lines 6.0 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 * @api Yoast\WP\SEO\Values\Open_Graph\Images 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 * @api Yoast\WP\SEO\Values\Open_Graph\Images 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 protected function add_from_indexable( Indexable $indexable, Images $image_container ) {
142 if ( $indexable->open_graph_image_meta ) {
143 $image_container->add_image_by_meta( $indexable->open_graph_image_meta );
144 return;
145 }
146
147 if ( $indexable->open_graph_image_id ) {
148 $image_container->add_image_by_id( $indexable->open_graph_image_id );
149 return;
150 }
151
152 if ( $indexable->open_graph_image ) {
153 $meta_data = [];
154 if ( $indexable->open_graph_image_meta && \is_string( $indexable->open_graph_image_meta ) ) {
155 $meta_data = \json_decode( $indexable->open_graph_image_meta, true );
156 }
157
158 $image_container->add_image(
159 \array_merge(
160 (array) $meta_data,
161 [
162 'url' => $indexable->open_graph_image,
163 ]
164 )
165 );
166 }
167 }
168
169 /**
170 * Retrieves the default Open Graph image.
171 *
172 * @param Images $image_container The image container.
173 */
174 protected function add_from_default( Images $image_container ) {
175 if ( $image_container->has_images() ) {
176 return;
177 }
178
179 $default_image_id = $this->options->get( 'og_default_image_id', '' );
180 if ( $default_image_id ) {
181 $image_container->add_image_by_id( $default_image_id );
182
183 return;
184 }
185
186 $default_image_url = $this->options->get( 'og_default_image', '' );
187 if ( $default_image_url ) {
188 $image_container->add_image_by_url( $default_image_url );
189 }
190 }
191
192 /**
193 * Retrieves the default Open Graph image.
194 *
195 * @param Meta_Tags_Context $context The context.
196 * @param Images $image_container The image container.
197 */
198 protected function add_from_templates( Meta_Tags_Context $context, Images $image_container ) {
199 if ( $image_container->has_images() ) {
200 return;
201 }
202
203 if ( $context->presentation->open_graph_image_id ) {
204 $image_container->add_image_by_id( $context->presentation->open_graph_image_id );
205 return;
206 }
207
208 if ( $context->presentation->open_graph_image ) {
209 $image_container->add_image_by_url( $context->presentation->open_graph_image );
210 }
211 }
212
213 /**
214 * Retrieves an instance of the image container.
215 *
216 * @codeCoverageIgnore
217 *
218 * @return Images The image container.
219 */
220 protected function get_image_container() {
221 $image_container = new Images( $this->image, $this->url );
222 $image_container->set_helpers( $this->open_graph_image );
223
224 return $image_container;
225 }
226 }
227