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

Post_Template::find_image_removal_target │ private │ WC 1.0

Choose which element to strip when hoisting an image, so the preserved remainder is clean.

Climbs from the image through every ancestor that wraps nothing but that single image — no text, no other media — and returns the outermost such wrapper. This removes the whole empty <figure>/<a>/layout-table shell WordPress renders around a featured image in one go, rather than leaving hollow, padded cells behind. Climbing stops as soon as an ancestor holds real content, so a sibling title/date (outside the image's wrapper) and a <figcaption> (whose text lives on the figure) are both preserved.

Method of the class: Post_Template{}

No Hooks.

Returns

\DOMElement. The element to remove from the item.

Usage

// private - for code of main (parent) class only
$result = $this->find_image_removal_target( $img_element ): \DOMElement;
$img_element(DOMElement) (required)
The image element being hoisted.

Post_Template::find_image_removal_target() code WC 11.1.2

private function find_image_removal_target( \DOMElement $img_element ): \DOMElement {
	$target = $img_element;
	$parent = $img_element->parentNode; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
	while ( $parent instanceof \DOMElement ) {
		// Stop once the ancestor carries text (e.g. a caption or a sibling title) or wraps more
		// than just this one image — removing it would take real content with it.
		if ( '' !== trim( $parent->textContent ) || 1 !== $this->count_media_descendants( $parent ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
			break;
		}
		$target = $parent;
		$parent = $parent->parentNode; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
	}
	return $target;
}