Automattic\WooCommerce\Blocks\Utils
ProductGalleryUtils::get_video_data
Get source data for one video.
Method of the class: ProductGalleryUtils{}
No Hooks.
Returns
array. Video source data.
Usage
$result = ProductGalleryUtils::get_video_data( $media_item, $size, $product_title, $index );
- $media_item(array) (required)
- Product gallery media item.
- $size(string) (required)
- The size of the poster image to retrieve.
- $product_title(string) (required)
- The title of the product used for alt fallback.
- $index(int) (required)
- The media index.
ProductGalleryUtils::get_video_data() ProductGalleryUtils::get video data code WC 11.1.2
private static function get_video_data( $media_item, $size, $product_title, $index ) {
$video_id = isset( $media_item['id'] ) ? (int) $media_item['id'] : 0;
$poster_id = ProductMediaGallery::get_video_poster_id( $media_item );
$poster = self::get_image_data( $poster_id, $size, $product_title, $index );
$video_src = $video_id ? wp_get_attachment_url( $video_id ) : ( $media_item['url'] ?? '' );
$mime_type = $video_id ? get_post_mime_type( $video_id ) : '';
$settings = isset( $media_item['settings'] ) && is_array( $media_item['settings'] )
? $media_item['settings']
: array();
$alt = $poster_id ? get_post_meta( $poster_id, '_wp_attachment_image_alt', true ) : '';
if ( empty( $alt ) && $video_id ) {
$video_title = get_post_field( 'post_title', $video_id );
$alt = $video_title
? sprintf(
/* translators: %s is the video title. */
__( 'Video: %s', 'woocommerce' ),
$video_title
)
: '';
}
if ( empty( $alt ) && $product_title ) {
$alt = sprintf(
/* translators: %s is the product title. */
__( 'Product video: %s', 'woocommerce' ),
$product_title
);
}
return array_merge(
$poster,
array(
'alt' => $alt,
'id' => $video_id,
'media_type' => 'video',
'mime_type' => $mime_type ? $mime_type : '',
'poster_id' => $poster_id,
'poster_src' => $poster['src'],
'settings' => $settings,
'video_src' => $video_src ? $video_src : '',
)
);
}