Automattic\WooCommerce\Blocks\BlockTypes

ProductGalleryLargeImage::replace_product_image_with_video │ private │ WC 1.0

Replace Product Image markup with product gallery video markup.

Method of the class: ProductGalleryLargeImage{}

No Hooks.

Returns

string.

Usage

// private - for code of main (parent) class only
$result = $this->replace_product_image_with_video( $image_html, $media, $context );
$image_html(string) (required)
Product Image HTML.
$media(array) (required)
Video media data.
$context(array) (required)
The block context.

ProductGalleryLargeImage::replace_product_image_with_video() code WC 11.1.2

private function replace_product_image_with_video( $image_html, $media, $context ) {
	$p = $this->get_product_image_processor( $image_html );

	if ( ! $p->next_tag( array( 'tag_name' => 'img' ) ) ) {
		return $image_html;
	}

	$attrs = ProductGalleryUtils::get_video_attributes( $media, 'gallery' );

	if ( empty( $attrs ) ) {
		return $image_html;
	}

	$image_style   = $p->get_attribute( 'style' );
	$video_classes = 'wc-block-woocommerce-product-gallery-large-image__image ' .
		'wc-block-woocommerce-product-gallery-large-image__video';

	if ( ! empty( $context['fullScreenOnClick'] ) ) {
		$video_classes             .= ' wc-block-woocommerce-product-gallery-large-image__image--full-screen-on-click';
		$attrs['data-wp-on--click'] = 'actions.openDialog';
	}

	$attrs = array_merge(
		$attrs,
		array(
			'class'                  => $video_classes,
			'data-wp-on--touchend'   => 'actions.onTouchEnd',
			'data-wp-on--touchmove'  => 'actions.onTouchMove',
			'data-wp-on--touchstart' => 'actions.onTouchStart',
			'draggable'              => 'false',
			'tabindex'               => '-1',
		)
	);

	if ( is_string( $image_style ) && '' !== $image_style ) {
		$attrs['style'] = $image_style;
	}

	$placeholder_attribute = 'data-wc-product-gallery-video-placeholder';

	if ( ! $p->set_attribute( $placeholder_attribute, '1' ) ) {
		return $image_html;
	}

	$updated_html      = $p->get_updated_html();
	$video_html        = ProductGalleryUtils::get_video_html( $attrs );
	$pattern           = sprintf(
		'/<img\b(?=[^>]*\s%s(?:\s*=\s*(?:"1"|\'1\'|1))?)[^>]*>/i',
		preg_quote( $placeholder_attribute, '/' )
	);
	$replacement_count = 0;
	$video_markup      = preg_replace_callback(
		$pattern,
		static function () use ( $video_html ) {
			return $video_html;
		},
		$updated_html,
		1,
		$replacement_count
	);

	return is_string( $video_markup ) && $replacement_count
		? $video_markup
		: $image_html;
}