Automattic\WooCommerce\Internal\ProductGallery
ProductMediaGallery::merge_positioned_video_gallery_items
Merge positioned video items into product image media items.
Video positions are 0-based indexes in the final mixed gallery, excluding the featured product image. The positions are not anchors to the image-only gallery; they already describe where videos should land after images and earlier videos are composed together.
Example:
- Images: array(
'image A','image B','image C') - Videos: array(
array( 'id' => 10, 'position' => 1 ), array( 'id' => 11, 'position' => 2 ),
)
- Outcome: array(
'image A','video 10','video 11','image B','image C')
Method of the class: ProductMediaGallery{}
No Hooks.
Returns
array.
Usage
$result = ProductMediaGallery::merge_positioned_video_gallery_items( $media_items, $video_gallery, $position_offset ): array;
- $media_items(array) (required)
- Product image media items.
- $video_gallery(array) (required)
- Stored video gallery items.
- $position_offset(int) (required)
- Offset applied when featured image is included.
ProductMediaGallery::merge_positioned_video_gallery_items() ProductMediaGallery::merge positioned video gallery items code WC 11.1.2
private static function merge_positioned_video_gallery_items( array $media_items, array $video_gallery, int $position_offset ): array {
foreach ( $video_gallery as $index => $video_item ) {
$video_gallery[ $index ]['_sort_order'] = $index;
}
usort(
$video_gallery,
static function ( $first, $second ) {
return array(
$first['position'] ?? 0,
$first['_sort_order'] ?? 0,
) <=> array(
$second['position'] ?? 0,
$second['_sort_order'] ?? 0,
);
}
);
foreach ( $video_gallery as $video_item ) {
unset( $video_item['_sort_order'] );
$position = isset( $video_item['position'] ) ? absint( $video_item['position'] ) : count( $media_items );
$position = min( count( $media_items ), $position + $position_offset );
array_splice( $media_items, $position, 0, array( self::get_video_media_item( $video_item ) ) );
}
return $media_items;
}