Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks

Gallery::extract_images_from_gallery_content │ private │ WC 1.0

Extract all images from gallery content with their links and captions.

Method of the class: Gallery{}

No Hooks.

Returns

array. Array of sanitized image HTML strings.

Usage

// private - for code of main (parent) class only
$result = $this->extract_images_from_gallery_content( $block_content, $parsed_block, $columns, $layout_width ): array;
$block_content(string) (required)
The rendered gallery block HTML.
$parsed_block(array) (required)
The parsed block data.
$columns(int) (required)
Number of gallery columns.
$layout_width(int) (required)
Available layout width in px.

Gallery::extract_images_from_gallery_content() code WC 11.1.2

private function extract_images_from_gallery_content( string $block_content, array $parsed_block, int $columns, int $layout_width ): array {
	$gallery_images = array();
	$inner_blocks   = $parsed_block['innerBlocks'] ?? array();

	// The gallery can request a crop (aspect ratio) for all of its images. Individual images
	// may override it with their own aspectRatio attribute. Guard against malformed input so a
	// non-string ratio can't throw a TypeError and abort rendering.
	$gallery_attrs        = isset( $parsed_block['attrs'] ) && is_array( $parsed_block['attrs'] ) ? $parsed_block['attrs'] : array();
	$gallery_aspect_ratio = isset( $gallery_attrs['aspectRatio'] ) && is_string( $gallery_attrs['aspectRatio'] ) ? $gallery_attrs['aspectRatio'] : null;

	// Collect the image blocks first so we know how many land in each rendered row and can size
	// each crop to its actual cell (incomplete final rows are wider — see get_cell_width()).
	$image_blocks = array();
	foreach ( $inner_blocks as $block ) {
		if ( 'core/image' === $block['blockName'] && isset( $block['innerHTML'] ) ) {
			$image_blocks[] = $block;
		}
	}
	$image_count = count( $image_blocks );

	foreach ( $image_blocks as $index => $block ) {
		$image_attrs = isset( $block['attrs'] ) && is_array( $block['attrs'] ) ? $block['attrs'] : array();

		// Use the per-image override only when it is a valid ratio; a malformed override falls
		// back to the gallery ratio instead of silently disabling the crop for that image.
		$image_ratio  = isset( $image_attrs['aspectRatio'] ) && is_string( $image_attrs['aspectRatio'] ) ? $image_attrs['aspectRatio'] : null;
		$aspect_ratio = ( null !== $image_ratio && null !== $this->parse_aspect_ratio( $image_ratio ) ) ? $image_ratio : $gallery_aspect_ratio;

		$cell_width      = $this->get_cell_width( $index, $image_count, $columns, $layout_width );
		$extracted_image = $this->extract_image_from_html( $block['innerHTML'], $aspect_ratio, $cell_width, $image_attrs );
		if ( ! empty( $extracted_image ) ) {
			$gallery_images[] = $extracted_image;
		}
	}

	return $gallery_images;
}