Automattic\WooCommerce\Blocks\BlockTypes

ProductGallery::render()protectedWC 1.0

Include and render the block.

Method of the class: ProductGallery{}

No Hooks.

Return

String. Rendered block type output.

Usage

// protected - for code of main (parent) or child class
$result = $this->render( $attributes, $content, $block );
$attributes(array) (required)
Block attributes.
Default: empty array
$content(string) (required)
Block content.
Default: empty string
$block(WP_Block) (required)
Block instance.

ProductGallery::render() code WC 9.8.1

protected function render( $attributes, $content, $block ) {
	$post_id = $block->context['postId'] ?? '';
	$product = wc_get_product( $post_id );

	if ( ! $product instanceof \WC_Product ) {
		return '';
	}

	wp_enqueue_script_module( $this->get_full_block_name() );

	$product_gallery_thumbnail_images = ProductGalleryUtils::get_product_gallery_images( $post_id, 'thumbnail', array() );
	$product_gallery_full_images      = ProductGalleryUtils::get_product_gallery_images( $post_id, 'full', array() );
	$classname_single_image           = '';

	if ( count( $product_gallery_thumbnail_images ) < 2 ) {
		// The gallery consists of a single image.
		$classname_single_image = 'is-single-product-gallery-image';
	}

	$classname           = StyleAttributesUtils::get_classes_by_attributes( $attributes, array( 'extra_classes' ) );
	$product_id          = strval( $product->get_id() );
	$gallery_with_dialog = $this->inject_dialog( $content, $this->render_dialog( $product_gallery_full_images ) );
	$p                   = new \WP_HTML_Tag_Processor( $gallery_with_dialog );

	if ( $p->next_tag() ) {
		$p->set_attribute( 'data-wp-interactive', $this->get_full_block_name() );
		$p->set_attribute(
			'data-wp-context',
			wp_json_encode(
				array(
					'selectedImageNumber' => 1,
					'isDialogOpen'        => false,
					'disableLeft'         => true,
					'disableRight'        => false,
					'isDragging'          => false,
					'touchStartX'         => 0,
					'touchCurrentX'       => 0,
					'productId'           => $product_id,
					'imageIds'            => ProductGalleryUtils::get_product_gallery_image_ids( $product, null, false ),
            'styles'                 => array(
						'transform'        => 'scale(1.0)',
						'transform-origin' => '',
					),
				),
				JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP
			)
		);

		if ( $product->is_type( ProductType::VARIABLE ) ) {
			$p->set_attribute( 'data-wp-init--watch-changes-on-add-to-cart-form', 'callbacks.watchForChangesOnAddToCartForm' );
		}

		$p->add_class( $classname );
		$p->add_class( $classname_single_image );
		$html = $p->get_updated_html();
	}

	return $html;
}