Image.php
7.55 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
namespace AIOSEO\Plugin\Common\Social;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\Plugin\Common\Models;
/**
* Handles the Open Graph and Twitter Image.
*
* @since 4.0.0
*/
class Image {
/**
* The type of image ("facebook" or "twitter").
*
* @since 4.1.6.2
*
* @var string
*/
protected $type;
/**
* The post object.
*
* @since 4.1.6.2
*
* @var WP_Post
*/
private $post;
/**
* The default thumbnail size.
*
* @since 4.0.0
*
* @var string
*/
protected $thumbnailSize;
/**
* Whether or not to use the cached images.
*
* @since 4.1.6
*
* @var boolean
*/
public $useCache = true;
/**
* Returns the Facebook or Twitter image.
*
* @since 4.0.0
*
* @param string $type The type ("Facebook" or "Twitter").
* @param string $imageSource The image source.
* @param WP_Post $post The post object.
* @return string|array The image data.
*/
public function getImage( $type, $imageSource, $post ) {
$this->type = $type;
$this->post = $post;
$this->thumbnailSize = apply_filters( 'aioseo_thumbnail_size', 'fullsize' );
static $images = [];
if ( isset( $images[ $this->type ] ) ) {
return $images[ $this->type ];
}
if ( 'auto' === $imageSource && aioseo()->helpers->getPostPageBuilderName( $post->ID ) ) {
$imageSource = 'default';
}
if ( is_a( $this->post, 'WP_Post' ) ) {
switch ( $imageSource ) {
case 'featured':
$image = $this->getFeaturedImage();
break;
case 'attach':
$image = $this->getFirstAttachedImage();
break;
case 'content':
$image = $this->getFirstImageInContent();
break;
case 'author':
$image = $this->getAuthorAvatar();
break;
case 'auto':
$image = $this->getFirstAvailableImage();
break;
case 'custom':
$image = $this->getCustomFieldImage();
break;
case 'custom_image':
$metaData = aioseo()->meta->metaData->getMetaData();
if ( empty( $metaData ) ) {
break;
}
$image = 'facebook' === strtolower( $this->type )
? $metaData->og_image_custom_url
: $metaData->twitter_image_custom_url;
break;
case 'default':
default:
$image = aioseo()->options->social->{$this->type}->general->defaultImagePosts;
}
}
if ( empty( $image ) ) {
$image = aioseo()->options->social->{$this->type}->general->defaultImagePosts;
}
if ( is_array( $image ) ) {
$images[ $this->type ] = $image;
return $images[ $this->type ];
}
$imageWithoutDimensions = aioseo()->helpers->removeImageDimensions( $image );
$attachmentId = aioseo()->helpers->attachmentUrlToPostId( $imageWithoutDimensions );
$images[ $this->type ] = $attachmentId ? wp_get_attachment_image_src( $attachmentId, $this->thumbnailSize ) : $image;
return $images[ $this->type ];
}
/**
* Returns the Featured Image for the post.
*
* @since 4.0.0
*
* @return array The image data.
*/
private function getFeaturedImage() {
$cachedImage = $this->getCachedImage();
if ( $cachedImage ) {
return $cachedImage;
}
$imageId = get_post_thumbnail_id( $this->post->ID );
return $imageId ? wp_get_attachment_image_src( $imageId, $this->thumbnailSize ) : '';
}
/**
* Returns the first attached image.
*
* @since 4.0.0
*
* @return string The image data.
*/
private function getFirstAttachedImage() {
$cachedImage = $this->getCachedImage();
if ( $cachedImage ) {
return $cachedImage;
}
if ( 'attachment' === get_post_type( $this->post->ID ) ) {
return wp_get_attachment_image_src( $this->post->ID, $this->thumbnailSize );
}
$attachments = get_children(
[
'post_parent' => $this->post->ID,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
]
);
return $attachments && count( $attachments ) ? wp_get_attachment_image_src( array_values( $attachments )[0]->ID, $this->thumbnailSize ) : '';
}
/**
* Returns the first image found in the post content.
*
* @since 4.0.0
*
* @return string The image URL.
*/
private function getFirstImageInContent() {
$cachedImage = $this->getCachedImage();
if ( $cachedImage ) {
return $cachedImage;
}
$postContent = aioseo()->helpers->getPostContent( $this->post );
preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i', $postContent, $matches );
// Ignore cover block background image - WP >= 5.7.
if ( ! empty( $matches[0] ) && apply_filters( 'aioseo_social_image_ignore_cover_block', true, $this->post, $matches ) ) {
foreach ( $matches[0] as $key => $match ) {
if ( false !== stripos( $match, 'wp-block-cover__image-background' ) ) {
unset( $matches[1][ $key ] );
}
}
}
return ! empty( $matches[1] ) ? current( $matches[1] ) : '';
}
/**
* Returns the author avatar.
*
* @since 4.0.0
*
* @return string The image URL.
*/
private function getAuthorAvatar() {
$avatar = get_avatar( $this->post->post_author, 300 );
preg_match( "/src='(.*?)'/i", $avatar, $matches );
return ! empty( $matches[1] ) ? $matches[1] : '';
}
/**
* Returns the first available image.
*
* @since 4.0.0
*
* @return string The image URL.
*/
private function getFirstAvailableImage() {
// Disable the cache.
$this->useCache = false;
$image = $this->getCustomFieldImage();
if ( ! $image ) {
$image = $this->getFeaturedImage();
}
if ( ! $image ) {
$image = $this->getFirstAttachedImage();
}
if ( ! $image ) {
$image = $this->getFirstImageInContent();
}
if ( ! $image && 'twitter' === strtolower( $this->type ) ) {
$image = aioseo()->options->social->twitter->homePage->image;
}
// Enable the cache.
$this->useCache = true;
return $image ? $image : aioseo()->options->social->facebook->homePage->image;
}
/**
* Returns the image from a custom field.
*
* @since 4.0.0
*
* @return string The image URL.
*/
private function getCustomFieldImage() {
$cachedImage = $this->getCachedImage();
if ( $cachedImage ) {
return $cachedImage;
}
$prefix = 'facebook' === strtolower( $this->type ) ? 'og_' : 'twitter_';
$aioseoPost = Models\Post::getPost( $this->post->ID );
$customFields = ! empty( $aioseoPost->{ $prefix . 'image_custom_fields' } )
? $aioseoPost->{ $prefix . 'image_custom_fields' }
: aioseo()->options->social->{$this->type}->general->customFieldImagePosts;
if ( ! $customFields ) {
return '';
}
$customFields = explode( ',', $customFields );
foreach ( $customFields as $customField ) {
$image = get_post_meta( $this->post->ID, $customField, true );
if ( ! empty( $image ) ) {
return is_numeric( $image )
? wp_get_attachment_image_src( $image, $this->thumbnailSize )
: $image;
}
}
return '';
}
/**
* Returns the cached image if there is one.
*
* @since 4.1.6.2
*
* @param WP_term The object for which we need to get the cached image.
* @return string|array The image URL or data.
*/
protected function getCachedImage( $object = null ) {
if ( null === $object ) {
// This isn't null if we call it from the Pro class.
$object = $this->post;
}
$metaData = aioseo()->meta->metaData->getMetaData( $object );
switch ( $this->type ) {
case 'facebook':
if ( ! empty( $metaData->og_image_url ) && $this->useCache ) {
return aioseo()->meta->metaData->getCachedOgImage( $metaData );
}
break;
case 'twitter':
if ( ! empty( $metaData->twitter_image_url ) && $this->useCache ) {
return $metaData->twitter_image_url;
}
break;
default:
break;
}
return '';
}
}