Automattic\WooCommerce\Blocks\Utils

ProductGalleryUtils::get_video_attributes │ public static │ WC 1.0

Get base attributes for product gallery videos.

Method of the class: ProductGalleryUtils{}

No Hooks.

Returns

array. Video attributes.

Usage

$result = ProductGalleryUtils::get_video_attributes( $media, $video_location );
$media(array) (required)
Video media data.
$video_location(string) (required)
Video location in the product gallery.

ProductGalleryUtils::get_video_attributes() code WC 11.1.2

public static function get_video_attributes( $media, $video_location ) {
	if ( empty( $media['video_src'] ) ) {
		return array();
	}

	$video_location = in_array( $video_location, array( 'dialog', 'gallery' ), true ) ? $video_location : 'gallery';
	$attrs          = array(
		'aria-label'      => $media['alt'] ?? '',
		'autoplay'        => 'autoplay',
		'data-image-id'   => $media['id'] ?? '',
		'data-wp-context' => sprintf( '{"videoLocation":"%s"}', $video_location ),
		'data-wp-watch'   => 'callbacks.syncVideoPlayback',
		'loop'            => 'loop',
		'muted'           => 'muted',
		'playsinline'     => 'playsinline',
		'preload'         => 'metadata',
		'src'             => $media['video_src'],
	);

	if ( 'dialog' === $video_location ) {
		unset( $attrs['autoplay'] );
		$attrs['data-wp-init--dialog-video-playback'] = 'callbacks.initDialogVideoPlayback';
	}

	if ( ! empty( $media['poster_id'] ) && ! empty( $media['poster_src'] ) ) {
		$attrs['poster'] = $media['poster_src'];
	}

	return $attrs;
}