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