Automattic\WooCommerce\Internal\ProductGallery
ProductMediaGallery::get_attachment_ids
Get attachment IDs represented by media gallery items.
Method of the class: ProductMediaGallery{}
No Hooks.
Returns
array.
Usage
$result = ProductMediaGallery::get_attachment_ids( $media_gallery, $include_posters ): array;
- $media_gallery(array) (required)
- Media gallery items.
- $include_posters(true|false)
- Whether to include resolved video poster IDs.
Default:false
ProductMediaGallery::get_attachment_ids() ProductMediaGallery::get attachment ids code WC 11.1.2
private static function get_attachment_ids( array $media_gallery, bool $include_posters = false ): array {
$attachment_ids = array();
foreach ( $media_gallery as $item ) {
if ( ! is_array( $item ) ) {
continue;
}
if ( 'attachment' === ( $item['source_type'] ?? 'attachment' ) && ! empty( $item['id'] ) ) {
$attachment_ids[] = absint( $item['id'] );
}
if (
$include_posters &&
(
! isset( $item['media_type'] ) ||
'video' === $item['media_type']
)
) {
$attachment_ids[] = self::get_video_poster_id( $item );
}
}
return array_values( array_unique( array_filter( $attachment_ids ) ) );
}