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 5.2.0-beta.1 10.0.2 5.2.0-rc.1 10.0.3 5.2.0-rc.2 10.0.4 5.2.1 10.0.5 5.2.2 10.0.6 5.2.3 10.1.0 5.2.4 10.1.0-rc.1 5.2.5 10.1.0-rc.2 5.3.0 10.1.0-rc.3 5.3.0-beta.1 10.1.0-rc.4 5.3.0-rc.1 10.1.1 5.3.0-rc.2 10.1.2 5.3.1 10.1.3 5.3.2 10.1.4 5.3.3 10.2.0 5.4.0 10.2.0-beta.1 5.4.0-beta.1 10.2.0-beta.2 5.4.0-rc.1 10.2.0-rc.1 5.4.1 10.2.1 5.4.2 10.2.2 5.4.3 10.2.3 5.4.4 10.2.4 5.4.5 10.3.0 5.5.0 10.3.0-beta.1 5.5.0-beta.1 10.3.0-beta.2 5.5.0-rc.1 10.3.0-rc.1 5.5.0-rc.2 10.3.0-rc.2 5.5.1 10.3.1 5.5.2 10.3.2 5.5.3 10.3.3 5.5.4 10.3.4 5.5.5 10.3.5 5.6.0 10.3.6 5.6.0-beta.1 10.3.7 5.6.0-rc.1 10.3.8 5.6.0-rc.2 10.4.0 5.6.1 10.4.0-beta.1 5.6.2 10.4.0-beta.2 5.6.3 10.4.0-rc.1 5.7.0 10.4.1 5.7.0-beta.1 10.4.2 5.7.0-rc.1 10.4.3 5.7.1 10.4.4 5.7.2 10.5.0 5.7.3 10.5.0-beta.1 5.8.0 10.5.0-beta.2 5.8.0-beta.1 10.5.0-rc.1 5.8.0-beta.2 10.5.0-rc.2 5.8.0-rc.1 10.5.0-rc.3 5.8.1 10.5.1 5.8.2 10.5.2 5.9.0 10.5.3 5.9.0-beta.1 10.6.0 5.9.0-rc.1 10.6.0-beta.1 5.9.0-rc.2 10.6.0-beta.2 5.9.1 10.6.0-rc.1 5.9.2 10.6.1 6.0.0 10.6.2 6.0.0-beta.1 10.7.0 6.0.0-rc.1 10.7.0-beta.1 6.0.1 10.7.0-beta.2 6.0.2 10.7.0-rc.1 6.1.0 3.0.0 6.1.0-beta.1 3.0.1 6.1.0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7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Internal / ProductGallery / ProductMediaGallery.php
woocommerce / src / Internal / ProductGallery Last commit date
ProductMediaGallery.php 2 weeks ago
ProductMediaGallery.php
765 lines
1 <?php
2 /**
3 * Product media gallery utilities.
4 *
5 * @package Automattic\WooCommerce\Internal\ProductGallery
6 */
7
8 declare( strict_types=1 );
9
10 namespace Automattic\WooCommerce\Internal\ProductGallery;
11
12 use Automattic\WooCommerce\Utilities\FeaturesUtil;
13 use WC_Product;
14
15 defined( 'ABSPATH' ) || exit;
16
17 /**
18 * Shared helpers for product media gallery data.
19 */
20 class ProductMediaGallery {
21
22 /**
23 * Product meta key used for beta product video gallery storage.
24 */
25 private const VIDEO_GALLERY_META_KEY = '_wc_video_gallery';
26
27 /**
28 * The feature id used by `FeaturesController` (Settings -> Advanced -> Features).
29 */
30 public const FEATURE_ID = 'product_gallery_videos';
31
32 /**
33 * Option backing the product gallery videos feature toggle.
34 */
35 public const ENABLE_OPTION_NAME = 'woocommerce_feature_product_gallery_videos_enabled';
36
37 /**
38 * Check if product gallery videos are enabled.
39 *
40 * @return bool
41 */
42 public static function is_feature_enabled(): bool {
43 return FeaturesUtil::feature_is_enabled( self::FEATURE_ID );
44 }
45
46 /**
47 * Get ordered media gallery items for a product.
48 *
49 * @param WC_Product $product Product object.
50 * @param array $args Optional arguments.
51 * @return array
52 */
53 public static function get_product_media_gallery_items( WC_Product $product, array $args = array() ): array {
54 $args = wp_parse_args(
55 $args,
56 array(
57 'context' => 'view',
58 'include_product_image' => true,
59 'include_placeholder' => false,
60 'resolve_video_posters' => true,
61 'deduplicate' => false,
62 )
63 );
64
65 $context = is_string( $args['context'] ) ? $args['context'] : 'view';
66 $include_product_image = (bool) $args['include_product_image'];
67 $media_items = self::get_product_image_media_items( $product, $include_product_image, $context );
68
69 if ( self::is_feature_enabled() ) {
70 $video_gallery = self::normalize_video_gallery_items(
71 self::get_stored_video_gallery_items( $product )
72 );
73
74 if ( ! empty( $video_gallery ) ) {
75 $media_items = self::merge_positioned_video_gallery_items(
76 $media_items,
77 $video_gallery,
78 self::get_video_position_offset( $product, $include_product_image, $context )
79 );
80 }
81 }
82
83 if ( $args['deduplicate'] ) {
84 $media_items = self::deduplicate_media_items( $media_items );
85 }
86
87 if ( $args['resolve_video_posters'] ) {
88 $media_items = self::resolve_video_poster_ids( $media_items );
89 }
90
91 if ( $args['include_placeholder'] && empty( $media_items ) ) {
92 $media_items[] = array(
93 'media_type' => 'image',
94 'source_type' => 'placeholder',
95 'id' => 0,
96 );
97 }
98
99 return array_values( $media_items );
100 }
101
102 /**
103 * Get media gallery items for classic product gallery templates.
104 *
105 * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
106 *
107 * @param WC_Product $product Product object.
108 * @return array
109 *
110 * @since 11.0.0
111 */
112 public static function get_product_media_gallery_items_for_display( WC_Product $product ): array {
113 $media_items = self::get_product_media_gallery_items(
114 $product,
115 array(
116 'include_placeholder' => true,
117 'deduplicate' => true,
118 )
119 );
120
121 self::prime_attachment_caches( $media_items, true );
122
123 return $media_items;
124 }
125
126 /**
127 * Get video gallery items stored for a product.
128 *
129 * @param WC_Product $product Product object.
130 * @return array
131 */
132 public static function get_stored_video_gallery_items( WC_Product $product ): array {
133 $value = $product->get_id()
134 ? get_post_meta( $product->get_id(), self::VIDEO_GALLERY_META_KEY, true )
135 : $product->get_meta( self::VIDEO_GALLERY_META_KEY, true, 'edit' );
136
137 return self::decode_video_gallery_meta( $value );
138 }
139
140 /**
141 * Copy stored video gallery items between products.
142 *
143 * @param WC_Product $source_product Source product.
144 * @param WC_Product $target_product Target product.
145 * @return array Stored video gallery items.
146 */
147 public static function copy_stored_video_gallery_items( WC_Product $source_product, WC_Product $target_product ): array {
148 return self::set_stored_video_gallery_items(
149 $target_product,
150 self::get_stored_video_gallery_items( $source_product )
151 );
152 }
153
154 /**
155 * Normalize and store video gallery items for a product.
156 *
157 * @param WC_Product $product Product object.
158 * @param array $video_gallery Video gallery data.
159 * @return array Stored video gallery items.
160 */
161 public static function set_stored_video_gallery_items(
162 WC_Product $product,
163 array $video_gallery
164 ): array {
165 $video_gallery = self::normalize_video_gallery_items( $video_gallery );
166
167 self::update_video_gallery_meta( $product, $video_gallery );
168
169 return $video_gallery;
170 }
171
172 /**
173 * Normalize media gallery items.
174 *
175 * @param array $media_gallery Media gallery data.
176 * @return array
177 */
178 public static function normalize_media_gallery_items( array $media_gallery ): array {
179 $items = array();
180
181 foreach ( $media_gallery as $index => $item ) {
182 if ( ! is_array( $item ) ) {
183 continue;
184 }
185
186 $media_type = isset( $item['media_type'] ) ? sanitize_key( $item['media_type'] ) : '';
187 $source_type = isset( $item['source_type'] ) ? sanitize_key( $item['source_type'] ) : 'attachment';
188 $attachment_id = isset( $item['id'] ) ? absint( $item['id'] ) : 0;
189
190 if ( 'attachment' !== $source_type || ! $attachment_id ) {
191 continue;
192 }
193
194 if ( 'image' === $media_type ) {
195 if (
196 ! wp_attachment_is_image( $attachment_id ) &&
197 0 !== strpos( (string) get_post_mime_type( $attachment_id ), 'image/' )
198 ) {
199 continue;
200 }
201
202 $items[] = self::get_image_media_item( $attachment_id );
203 continue;
204 }
205
206 if ( 'video' !== $media_type ) {
207 continue;
208 }
209
210 $video_item = self::normalize_video_gallery_item( $item, absint( $index ) );
211
212 if ( ! empty( $video_item ) ) {
213 $items[] = self::get_video_media_item( $video_item );
214 }
215 }
216
217 return $items;
218 }
219
220 /**
221 * Get positioned video gallery items from a mixed media gallery.
222 *
223 * The mixed gallery excludes the featured product image. Positions are
224 * stored as 0-based indexes in that gallery and shifted later only when the
225 * composed media gallery includes the featured image.
226 *
227 * @param array $media_gallery Media gallery items.
228 * @return array
229 */
230 public static function get_positioned_video_gallery_items_from_media_gallery( array $media_gallery ): array {
231 $video_gallery = array();
232
233 foreach ( array_values( $media_gallery ) as $position => $media_item ) {
234 if ( ! is_array( $media_item ) || 'video' !== ( $media_item['media_type'] ?? '' ) ) {
235 continue;
236 }
237
238 $media_item['position'] = $position;
239 $video_gallery[] = $media_item;
240 }
241
242 return $video_gallery;
243 }
244
245 /**
246 * Normalize video gallery items.
247 *
248 * @param array $video_gallery Video gallery data.
249 * @return array
250 */
251 public static function normalize_video_gallery_items( array $video_gallery ): array {
252 $items = array();
253
254 foreach ( $video_gallery as $fallback_position => $item ) {
255 if ( ! is_array( $item ) ) {
256 continue;
257 }
258
259 $item = self::normalize_video_gallery_item( $item, absint( $fallback_position ) );
260
261 if ( ! empty( $item ) ) {
262 $items[] = $item;
263 }
264 }
265
266 return $items;
267 }
268
269 /**
270 * Get image media items from product image props.
271 *
272 * @param WC_Product $product Product object.
273 * @param bool $include_product_image Whether to include the product image.
274 * @param string $context Product read context.
275 * @return array
276 */
277 public static function get_product_image_media_items( WC_Product $product, bool $include_product_image = true, string $context = 'view' ): array {
278 $attachment_ids = array();
279
280 if ( $include_product_image && $product->get_image_id( $context ) ) {
281 $attachment_ids[] = $product->get_image_id( $context );
282 }
283
284 $attachment_ids = array_merge( $attachment_ids, $product->get_gallery_image_ids( $context ) );
285 $media_items = array();
286
287 foreach ( $attachment_ids as $attachment_id ) {
288 $attachment_id = absint( $attachment_id );
289
290 if ( $attachment_id ) {
291 $media_items[] = self::get_image_media_item( $attachment_id );
292 }
293 }
294
295 return $media_items;
296 }
297
298 /**
299 * Get image IDs from media gallery items.
300 *
301 * @param array $media_gallery Media gallery items.
302 * @return array
303 */
304 public static function get_image_ids( array $media_gallery ): array {
305 $image_ids = array();
306
307 foreach ( $media_gallery as $item ) {
308 if (
309 is_array( $item ) &&
310 'image' === ( $item['media_type'] ?? '' ) &&
311 'attachment' === ( $item['source_type'] ?? 'attachment' ) &&
312 ! empty( $item['id'] )
313 ) {
314 $image_ids[] = absint( $item['id'] );
315 }
316 }
317
318 return array_values( array_filter( $image_ids ) );
319 }
320
321 /**
322 * Get the poster attachment ID for a gallery video item.
323 *
324 * @param array $media_item Product media gallery item.
325 * @return int
326 */
327 public static function get_video_poster_id( array $media_item ): int {
328 $poster_id = isset( $media_item['poster_id'] ) ? absint( $media_item['poster_id'] ) : 0;
329
330 if ( $poster_id ) {
331 return $poster_id;
332 }
333
334 $attachment_id = isset( $media_item['id'] ) ? absint( $media_item['id'] ) : 0;
335
336 return $attachment_id ? (int) get_post_thumbnail_id( $attachment_id ) : 0;
337 }
338
339 /**
340 * Get HTML for a classic product gallery video.
341 *
342 * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
343 *
344 * @param array $media_item Product media gallery item.
345 * @param bool $main_video Whether this is the main gallery item.
346 * @return string
347 *
348 * @since 11.0.0
349 */
350 public static function get_gallery_video_html( array $media_item, bool $main_video = false ): string {
351 $attachment_id = isset( $media_item['id'] ) ? absint( $media_item['id'] ) : 0;
352 $video_src = $attachment_id ? wp_get_attachment_url( $attachment_id ) : '';
353
354 if ( ! $video_src ) {
355 return '';
356 }
357
358 /**
359 * Filters whether the single product gallery flexslider is enabled.
360 *
361 * @since 3.0.0
362 *
363 * @param bool $enabled Whether flexslider is enabled.
364 */
365 $flexslider = (bool) apply_filters(
366 'woocommerce_single_product_flexslider_enabled',
367 get_theme_support( 'wc-product-gallery-slider' )
368 );
369 $gallery_thumbnail = wc_get_image_size( 'gallery_thumbnail' );
370 /**
371 * Filters the product gallery thumbnail image size.
372 *
373 * @since 3.3.2
374 *
375 * @param string|array $size Image size.
376 */
377 $thumbnail_size = apply_filters(
378 'woocommerce_gallery_thumbnail_size',
379 array( $gallery_thumbnail['width'], $gallery_thumbnail['height'] )
380 );
381 /**
382 * Filters the product gallery image size.
383 *
384 * @since 3.3.2
385 *
386 * @param string|array $size Image size.
387 */
388 $image_size = apply_filters(
389 'woocommerce_gallery_image_size',
390 $flexslider || $main_video ? 'woocommerce_single' : $thumbnail_size
391 );
392 /**
393 * Filters the product gallery full image size.
394 *
395 * @since 3.3.2
396 *
397 * @param string|array $size Image size.
398 */
399 $full_size = apply_filters(
400 'woocommerce_gallery_full_size',
401 /**
402 * Filters the product thumbnails large image size.
403 *
404 * @since 3.0.0
405 *
406 * @param string|array $size Image size.
407 */
408 apply_filters( 'woocommerce_product_thumbnails_large_size', 'full' )
409 );
410
411 $poster_id = self::get_video_poster_id( $media_item );
412 $thumbnail_src = $poster_id ? wp_get_attachment_image_src( $poster_id, $thumbnail_size ) : false;
413 $thumbnail_srcset = $poster_id ? wp_get_attachment_image_srcset( $poster_id, $thumbnail_size ) : false;
414 $thumbnail_sizes = $poster_id ? wp_get_attachment_image_sizes( $poster_id, $thumbnail_size ) : false;
415 $poster_src = $poster_id ? wp_get_attachment_image_src( $poster_id, $image_size ) : false;
416 $full_src = $poster_id ? wp_get_attachment_image_src( $poster_id, $full_size ) : false;
417 $alt_text = $poster_id
418 ? trim( wp_strip_all_tags( get_post_meta( $poster_id, '_wp_attachment_image_alt', true ) ) )
419 : '';
420
421 if ( empty( $alt_text ) ) {
422 $video_title = get_post_field( 'post_title', $attachment_id );
423 $alt_text = $video_title
424 ? sprintf(
425 /* translators: %s is the video title. */
426 __( 'Video: %s', 'woocommerce' ),
427 $video_title
428 )
429 : '';
430 }
431
432 $full_width = isset( $full_src[1] ) ? absint( $full_src[1] ) : 1000;
433 $full_height = isset( $full_src[2] ) ? absint( $full_src[2] ) : 1000;
434 $full_src_url = isset( $full_src[0] ) ? $full_src[0] : wc_placeholder_img_src( 'woocommerce_single' );
435 $video_attributes = array(
436 'autoplay' => 'autoplay',
437 'class' => $main_video ? 'wp-post-video wp-post-image' : 'wp-post-video',
438 'src' => $video_src,
439 'loop' => 'loop',
440 'muted' => 'muted',
441 'playsinline' => 'playsinline',
442 'preload' => 'metadata',
443 'data-caption' => get_post_field( 'post_excerpt', $attachment_id ),
444 'data-src' => $full_src_url,
445 'data-large_image' => $full_src_url,
446 'data-large_image_width' => $full_width,
447 'data-large_image_height' => $full_height,
448 'aria-label' => $alt_text,
449 );
450
451 if ( false !== $poster_src && isset( $poster_src[0] ) ) {
452 $video_attributes['poster'] = $poster_src[0];
453 }
454
455 $thumbnail = isset( $thumbnail_src[0] )
456 ? $thumbnail_src[0]
457 : wc_placeholder_img_src( 'woocommerce_gallery_thumbnail' );
458 $thumbnail_video_src = isset( $thumbnail_src[0] ) ? '' : $video_src;
459
460 return sprintf(
461 '<div data-thumb="%1$s" data-thumb-alt="%2$s" data-thumb-srcset="%3$s" data-thumb-sizes="%4$s" data-thumb-video-src="%5$s" ' .
462 'class="woocommerce-product-gallery__image woocommerce-product-gallery__video"><a href="%6$s"><video %7$s></video></a></div>',
463 esc_url( $thumbnail ),
464 esc_attr( $alt_text ),
465 esc_attr( $thumbnail_srcset ? $thumbnail_srcset : '' ),
466 esc_attr( $thumbnail_sizes ? $thumbnail_sizes : '' ),
467 esc_url( $thumbnail_video_src ),
468 esc_url( $video_src ),
469 wc_implode_html_attributes(
470 array_filter(
471 $video_attributes,
472 static function ( $value ) {
473 return '' !== $value;
474 }
475 )
476 )
477 );
478 }
479
480 /**
481 * Merge positioned video items into product image media items.
482 *
483 * Video positions are 0-based indexes in the final mixed gallery, excluding
484 * the featured product image. The positions are not anchors to the image-only
485 * gallery; they already describe where videos should land after images and
486 * earlier videos are composed together.
487 *
488 * Example:
489 * - Images: array( 'image A', 'image B', 'image C' )
490 * - Videos: array(
491 * array( 'id' => 10, 'position' => 1 ),
492 * array( 'id' => 11, 'position' => 2 ),
493 * )
494 * - Outcome: array( 'image A', 'video 10', 'video 11', 'image B', 'image C' )
495 *
496 * @param array $media_items Product image media items.
497 * @param array $video_gallery Stored video gallery items.
498 * @param int $position_offset Offset applied when featured image is included.
499 * @return array
500 */
501 private static function merge_positioned_video_gallery_items( array $media_items, array $video_gallery, int $position_offset ): array {
502 foreach ( $video_gallery as $index => $video_item ) {
503 $video_gallery[ $index ]['_sort_order'] = $index;
504 }
505
506 usort(
507 $video_gallery,
508 static function ( $first, $second ) {
509 return array(
510 $first['position'] ?? 0,
511 $first['_sort_order'] ?? 0,
512 ) <=> array(
513 $second['position'] ?? 0,
514 $second['_sort_order'] ?? 0,
515 );
516 }
517 );
518
519 foreach ( $video_gallery as $video_item ) {
520 unset( $video_item['_sort_order'] );
521
522 $position = isset( $video_item['position'] ) ? absint( $video_item['position'] ) : count( $media_items );
523 $position = min( count( $media_items ), $position + $position_offset );
524
525 array_splice( $media_items, $position, 0, array( self::get_video_media_item( $video_item ) ) );
526 }
527
528 return $media_items;
529 }
530
531 /**
532 * Get the positional offset used when composing videos into product media.
533 *
534 * @param WC_Product $product Product object.
535 * @param bool $include_product_image Whether the product image is included.
536 * @param string $context Product read context.
537 * @return int
538 */
539 private static function get_video_position_offset( WC_Product $product, bool $include_product_image, string $context ): int {
540 return $include_product_image && absint( $product->get_image_id( $context ) ) ? 1 : 0;
541 }
542
543 /**
544 * Normalize one video gallery item.
545 *
546 * @param array $item Video gallery item.
547 * @param int $fallback_position Position used when item has no position.
548 * @return array
549 */
550 private static function normalize_video_gallery_item( array $item, int $fallback_position ): array {
551 $media_type = isset( $item['media_type'] ) ? sanitize_key( $item['media_type'] ) : 'video';
552 $source_type = isset( $item['source_type'] ) ? sanitize_key( $item['source_type'] ) : 'attachment';
553 $attachment_id = isset( $item['id'] ) ? absint( $item['id'] ) : 0;
554
555 if ( 'video' !== $media_type || 'attachment' !== $source_type || ! $attachment_id ) {
556 return array();
557 }
558
559 $mime_type = get_post_mime_type( $attachment_id );
560
561 if ( ! is_string( $mime_type ) || 0 !== strpos( $mime_type, 'video/' ) ) {
562 return array();
563 }
564
565 $video_item = array();
566 $poster_id = isset( $item['poster_id'] ) ? absint( $item['poster_id'] ) : 0;
567
568 $video_item['source_type'] = 'attachment';
569 $video_item['id'] = $attachment_id;
570 $video_item['position'] = isset( $item['position'] ) ? absint( $item['position'] ) : $fallback_position;
571
572 if (
573 $poster_id &&
574 (
575 wp_attachment_is_image( $poster_id ) ||
576 0 === strpos( (string) get_post_mime_type( $poster_id ), 'image/' )
577 )
578 ) {
579 $video_item['poster_id'] = $poster_id;
580 }
581
582 return $video_item;
583 }
584
585 /**
586 * Get attachment IDs represented by media gallery items.
587 *
588 * @param array $media_gallery Media gallery items.
589 * @param bool $include_posters Whether to include resolved video poster IDs.
590 * @return array
591 */
592 private static function get_attachment_ids( array $media_gallery, bool $include_posters = false ): array {
593 $attachment_ids = array();
594
595 foreach ( $media_gallery as $item ) {
596 if ( ! is_array( $item ) ) {
597 continue;
598 }
599
600 if ( 'attachment' === ( $item['source_type'] ?? 'attachment' ) && ! empty( $item['id'] ) ) {
601 $attachment_ids[] = absint( $item['id'] );
602 }
603
604 if (
605 $include_posters &&
606 (
607 ! isset( $item['media_type'] ) ||
608 'video' === $item['media_type']
609 )
610 ) {
611 $attachment_ids[] = self::get_video_poster_id( $item );
612 }
613 }
614
615 return array_values( array_unique( array_filter( $attachment_ids ) ) );
616 }
617
618 /**
619 * Prime attachment post caches for media gallery items.
620 *
621 * @param array $media_gallery Media gallery items.
622 * @param bool $include_posters Whether to include resolved video poster IDs.
623 * @return void
624 */
625 public static function prime_attachment_caches( array $media_gallery, bool $include_posters = false ): void {
626 $attachment_ids = self::get_attachment_ids( $media_gallery, $include_posters );
627
628 if ( ! empty( $attachment_ids ) ) {
629 _prime_post_caches( $attachment_ids );
630 }
631 }
632
633 /**
634 * Get a normalized image media item.
635 *
636 * @param int $attachment_id Attachment ID.
637 * @return array
638 */
639 private static function get_image_media_item( int $attachment_id ): array {
640 return array(
641 'media_type' => 'image',
642 'source_type' => 'attachment',
643 'id' => $attachment_id,
644 );
645 }
646
647 /**
648 * Get a normalized video media item.
649 *
650 * @param array $video_item Stored video gallery item.
651 * @return array
652 */
653 private static function get_video_media_item( array $video_item ): array {
654 unset( $video_item['position'] );
655
656 $video_item['media_type'] = 'video';
657 $video_item['source_type'] = 'attachment';
658
659 return $video_item;
660 }
661
662 /**
663 * Resolve poster IDs for video items.
664 *
665 * @param array $media_gallery Media gallery items.
666 * @return array
667 */
668 private static function resolve_video_poster_ids( array $media_gallery ): array {
669 foreach ( $media_gallery as $index => $media_item ) {
670 if ( ! is_array( $media_item ) || 'video' !== ( $media_item['media_type'] ?? '' ) ) {
671 continue;
672 }
673
674 $poster_id = self::get_video_poster_id( $media_item );
675
676 if ( $poster_id ) {
677 $media_gallery[ $index ]['poster_id'] = $poster_id;
678 }
679 }
680
681 return $media_gallery;
682 }
683
684 /**
685 * Remove duplicate media items by attachment ID.
686 *
687 * @param array $media_gallery Media gallery items.
688 * @return array
689 */
690 private static function deduplicate_media_items( array $media_gallery ): array {
691 $seen_ids = array();
692 $items = array();
693
694 foreach ( $media_gallery as $media_item ) {
695 if ( ! is_array( $media_item ) ) {
696 continue;
697 }
698
699 $attachment_id = isset( $media_item['id'] ) ? absint( $media_item['id'] ) : 0;
700
701 if ( $attachment_id && isset( $seen_ids[ $attachment_id ] ) ) {
702 continue;
703 }
704
705 if ( $attachment_id ) {
706 $seen_ids[ $attachment_id ] = true;
707 }
708
709 $items[] = $media_item;
710 }
711
712 return $items;
713 }
714
715 /**
716 * Decode video gallery post meta.
717 *
718 * @param mixed $value Raw video gallery meta value.
719 * @return array
720 */
721 private static function decode_video_gallery_meta( $value ): array {
722 if ( is_array( $value ) ) {
723 return $value;
724 }
725
726 if ( ! is_string( $value ) || '' === $value ) {
727 return array();
728 }
729
730 $decoded = json_decode( $value, true );
731
732 return is_array( $decoded ) ? $decoded : array();
733 }
734
735 /**
736 * Update video gallery post meta.
737 *
738 * @param WC_Product $product Product object.
739 * @param array $video_gallery Video gallery items.
740 * @return void
741 */
742 private static function update_video_gallery_meta( WC_Product $product, array $video_gallery ): void {
743 if ( empty( $video_gallery ) ) {
744 if ( $product->get_id() ) {
745 delete_post_meta( $product->get_id(), self::VIDEO_GALLERY_META_KEY );
746 } else {
747 $product->delete_meta_data( self::VIDEO_GALLERY_META_KEY );
748 }
749 return;
750 }
751
752 $value = wp_json_encode( $video_gallery );
753
754 if ( false === $value ) {
755 return;
756 }
757
758 if ( $product->get_id() ) {
759 update_post_meta( $product->get_id(), self::VIDEO_GALLERY_META_KEY, wp_slash( $value ) );
760 } else {
761 $product->update_meta_data( self::VIDEO_GALLERY_META_KEY, $value );
762 }
763 }
764 }
765