Automattic\WooCommerce\Internal\ProductGallery

ProductMediaGallery::normalize_media_gallery_items │ public static │ WC 1.0

Normalize media gallery items.

Method of the class: ProductMediaGallery{}

No Hooks.

Returns

array.

Usage

$result = ProductMediaGallery::normalize_media_gallery_items( $media_gallery ): array;
$media_gallery(array) (required)
Media gallery data.

ProductMediaGallery::normalize_media_gallery_items() code WC 11.1.2

public static function normalize_media_gallery_items( array $media_gallery ): array {
	$items = array();

	foreach ( $media_gallery as $index => $item ) {
		if ( ! is_array( $item ) ) {
			continue;
		}

		$media_type    = isset( $item['media_type'] ) ? sanitize_key( $item['media_type'] ) : '';
		$source_type   = isset( $item['source_type'] ) ? sanitize_key( $item['source_type'] ) : 'attachment';
		$attachment_id = isset( $item['id'] ) ? absint( $item['id'] ) : 0;

		if ( 'attachment' !== $source_type || ! $attachment_id ) {
			continue;
		}

		if ( 'image' === $media_type ) {
			if (
				! wp_attachment_is_image( $attachment_id ) &&
				0 !== strpos( (string) get_post_mime_type( $attachment_id ), 'image/' )
			) {
				continue;
			}

			$items[] = self::get_image_media_item( $attachment_id );
			continue;
		}

		if ( 'video' !== $media_type ) {
			continue;
		}

		$video_item = self::normalize_video_gallery_item( $item, absint( $index ) );

		if ( ! empty( $video_item ) ) {
			$items[] = self::get_video_media_item( $video_item );
		}
	}

	return $items;
}