ThirdParty.php
7.47 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
325
326
327
<?php
namespace AIOSEO\Plugin\Common\Sitemap\Image;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Holds all code to extract images from third-party content.
*
* @since 4.2.2
*/
class ThirdParty {
/**
* The post object.
*
* @since 4.2.2
*
* @var WP_Post
*/
private $post;
/**
* The parsed post content.
* The post object holds the unparsed content as we need that for Divi.
*
* @since 4.2.5
*
* @var string
*/
private $parsedPostContent;
/**
* The image URLs and IDs.
*
* @since 4.2.2
*
* @var array[mixed]
*/
private $images = [];
/**
* Divi shortcodes.
*
* @since 4.2.3
*
* @var string[]
*/
private $shortcodes = [
'et_pb_section',
'et_pb_column',
'et_pb_row',
'et_pb_image',
'et_pb_gallery',
'et_pb_accordion',
'et_pb_accordion_item',
'et_pb_counters',
'et_pb_blurb',
'et_pb_cta',
'et_pb_code',
'et_pb_contact_form',
'et_pb_divider',
'et_pb_filterable_portfolio',
'et_pb_map',
'et_pb_number_counter',
'et_pb_post_slider',
'et_pb_pricing_tables',
'et_pb_pricing_table',
'et_pb_shop',
'et_pb_slider',
'et_pb_slide',
'et_pb_tabs',
'et_pb_tab',
'et_pb_text',
'et_pb_video',
'et_pb_audio',
'et_pb_blog',
'et_pb_circle_counter',
'et_pb_comments',
'et_pb_countdown_timer',
'et_pb_signup',
'et_pb_login',
'et_pb_menu',
'et_pb_team_member',
'et_pb_post_nav',
'et_pb_post_title',
'et_pb_search',
'et_pb_sidebar',
'et_pb_social_media_follow',
'et_pb_social_media_follow_network',
'et_pb_testimonial',
'et_pb_toggle',
'et_pb_video_slider',
'et_pb_video_slider_item',
];
/**
* Class constructor.
*
* @since 4.2.2
*
* @param WP_Post $post The post object.
* @param string $parsedPostContent The parsed post content.
*/
public function __construct( $post, $parsedPostContent ) {
$this->post = $post;
$this->parsedPostContent = $parsedPostContent;
}
/**
* Extracts the images from third-party content.
*
* @since 4.2.2
*
* @return array[mixed] The image URLs and IDs.
*/
public function extract() {
$integrations = [
'acf',
'divi',
'nextGen',
'wooCommerce'
];
foreach ( $integrations as $integration ) {
$this->{$integration}();
}
return $this->images;
}
/**
* Extracts image URLs from ACF fields.
*
* @since 4.2.2
*
* @return void
*/
private function acf() {
if ( ! class_exists( 'ACF' ) || ! function_exists( 'get_fields' ) ) {
return;
}
$fields = get_fields( $this->post->ID );
if ( ! $fields ) {
return;
}
$images = $this->acfHelper( $fields );
$this->images = array_merge( $this->images, $images );
}
/**
* Helper function for acf().
*
* @since 4.2.2
*
* @param array $fields The ACF fields.
* @return array[string] The image URLs or IDs.
*/
private function acfHelper( $fields ) {
$images = [];
foreach ( $fields as $value ) {
if ( is_array( $value ) ) {
// Recursively loop over grouped fields.
// We continue on since arrays aren't necessarily groups and might also simply aLready contain the value we're looking for.
$images = array_merge( $images, $this->acfHelper( $value ) );
if ( isset( $value['type'] ) && 'image' !== strtolower( $value['type'] ) ) {
$images[] = $value['url'];
}
continue;
}
// Capture the value if it's an image URL, but not the default thumbnail from ACF.
if ( is_string( $value ) && preg_match( aioseo()->sitemap->image->getImageExtensionRegexPattern(), $value ) && ! preg_match( '/media\/default\.png$/i', $value ) ) {
$images[] = $value;
continue;
}
// Capture the value if it's a numeric image ID, but make sure it's not an array of random field object properties.
if (
is_numeric( $value ) &&
! isset( $fields['ID'] ) &&
! isset( $fields['thumbnail'] )
) {
$images[] = $value;
}
}
return $images;
}
/**
* Extracts images from Divi shortcodes.
*
* @since 4.1.8
*
* @return void
*/
private function divi() {
if ( ! defined( 'ET_BUILDER_VERSION' ) ) {
return;
}
$urls = [];
$regex = implode( '|', array_map( 'preg_quote', $this->shortcodes ) );
preg_match_all(
"/\[($regex)(?![\w-])([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)\[\/\2\])?)(\]?)/i",
$this->post->post_content,
$matches,
PREG_SET_ORDER
);
foreach ( $matches as $shortcode ) {
$attributes = shortcode_parse_atts( $shortcode[0] );
if ( ! empty( $attributes['src'] ) ) {
$urls[] = $attributes['src'];
}
if ( ! empty( $attributes['image_src'] ) ) {
$urls[] = $attributes['image_src'];
}
if ( ! empty( $attributes['image_url'] ) ) {
$urls[] = $attributes['image_url'];
}
if ( ! empty( $attributes['portrait_url'] ) ) {
$urls[] = $attributes['portrait_url'];
}
if ( ! empty( $attributes['image'] ) ) {
$urls[] = $attributes['image'];
}
if ( ! empty( $attributes['background_image'] ) ) {
$urls[] = $attributes['background_image'];
}
if ( ! empty( $attributes['logo'] ) ) {
$urls[] = $attributes['logo'];
}
if ( ! empty( $attributes['gallery_ids'] ) ) {
$attachmentIds = explode( ',', $attributes['gallery_ids'] );
foreach ( $attachmentIds as $attachmentId ) {
$urls[] = wp_get_attachment_url( $attachmentId );
}
}
}
$this->images = array_merge( $this->images, $urls );
}
/**
* Extracts the image IDs of more advanced NextGen Pro gallerlies like the Mosaic and Thumbnail Grid.
*
* @since 4.2.5
*
* @return void
*/
private function nextGen() {
if ( ! defined( 'NGG_PLUGIN_BASENAME' ) && ! defined( 'NGG_PRO_PLUGIN_BASENAME' ) ) {
return;
}
preg_match_all( '/data-image-id=\"([0-9]*)\"/i', $this->parsedPostContent, $imageIds );
if ( ! empty( $imageIds[1] ) ) {
$this->images = array_merge( $this->images, $imageIds[1] );
}
// For this specific check, we only want to parse blocks and do not want to run shortcodes because some NextGen blocks (e.g. Mosaic) are parsed into shortcodes.
// And after parsing the shortcodes, the attributes we're looking for are gone.
$contentWithBlocksParsed = function_exists( 'do_blocks' ) ? do_blocks( $this->post->post_content ) : $this->post->post_content; // phpcs:disable AIOSEO.WpFunctionUse.NewFunctions
$imageIds = [];
preg_match_all( '/\[ngg.*src="galleries" ids="(.*?)".*\]/i', $contentWithBlocksParsed, $shortcodes );
if ( empty( $shortcodes[1] ) ) {
return;
}
foreach ( $shortcodes[1] as $shortcode ) {
$galleryIds = explode( ',', $shortcode[0] );
foreach ( $galleryIds as $galleryId ) {
global $nggdb;
$galleryImageIds = $nggdb->get_ids_from_gallery( $galleryId );
if ( empty( $galleryImageIds ) ) {
continue;
}
foreach ( $galleryImageIds as $galleryImageId ) {
$image = $nggdb->find_image( $galleryImageId );
if ( ! empty( $image ) ) {
$imageIds[] = $image->get_permalink();
}
}
}
}
$this->images = array_merge( $this->images, $imageIds );
}
/**
* Extracts the image IDs of WooCommerce product galleries.
*
* @since 4.1.2
*
* @return void
*/
private function wooCommerce() {
if ( ! aioseo()->helpers->isWooCommerceActive() || 'product' !== $this->post->post_type ) {
return;
}
$productImageIds = get_post_meta( $this->post->ID, '_product_image_gallery', true );
if ( ! $productImageIds ) {
return;
}
$productImageIds = explode( ',', $productImageIds );
$this->images = array_merge( $this->images, $productImageIds );
}
}