Automattic\WooCommerce\Blocks\BlockTypes

ProductGallery::renderprotectedWC 1.0

Include and render the block.

Метод класса: ProductGallery{}

Хуков нет.

Возвращает

Строку. Rendered block type output.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->render( $attributes, $content, $block );
$attributes(массив) (обязательный)
Block attributes.
По умолчанию: empty array
$content(строка) (обязательный)
Block content.
По умолчанию: empty string
$block(WP_Block) (обязательный)
Block instance.

Код ProductGallery::render() WC 9.9.4

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

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

	$image_ids              = ProductGalleryUtils::get_all_image_ids( $product );
	$classname              = StyleAttributesUtils::get_classes_by_attributes( $attributes, array( 'extra_classes' ) );
	$initial_image_id       = count( $image_ids ) > 0 ? $image_ids[0] : -1;
	$classname_single_image = count( $image_ids ) < 2 ? 'is-single-product-gallery-image' : '';
	$product_id             = strval( $product->get_id() );
	$full_image_data        = ProductGalleryUtils::get_image_src_data( $image_ids, 'full' );
	$gallery_with_dialog    = $this->inject_dialog( $content, $this->render_dialog( $full_image_data ) );
	$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(
					'imageData'          => $image_ids,
					'isDialogOpen'       => false,
					'disableLeft'        => true,
					'disableRight'       => false,
					'isDragging'         => false,
					'touchStartX'        => 0,
					'touchCurrentX'      => 0,
					'productId'          => $product_id,
					'selectedImageId'    => $initial_image_id,
					'thumbnailsOverflow' => [
						'top'    => false,
						'bottom' => false,
						'left'   => false,
						'right'  => false,
					],
				),
				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;
}