Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks
Post_Template::prepare_item_content
Turn a rendered <li>'s inner HTML into email-safe cell content.
Each image in the item is rebuilt as a clean, responsive <img> (preserving its link) sitting directly in the cell, so its width resolves against the grid column instead of collapsing inside the fixed-width wrapper tables WordPress renders around it. Any non-image content the card holds (post title, date, excerpt) is kept below the image, so a post grid isn't reduced to bare images.
When the card is image-only (e.g. a featured-image sponsor grid) the leftover is nothing but the now-empty wrapper shells, which are dropped so the output stays a clean logo grid. An item with no image at all is returned unchanged — its text stacks correctly without intervention.
Метод класса: Post_Template{}
Хуков нет.
Возвращает
string. Cell content HTML.
Использование
// private - только в коде основоного (родительского) класса $result = $this->prepare_item_content( $item_html, $cell_width ): string;
- $item_html(строка) (обязательный)
- Inner HTML of a single list item.
- $cell_width(int) (обязательный)
- Cell content width in px.
Код Post_Template::prepare_item_content() Post Template::prepare item content WC 11.2.2
private function prepare_item_content( string $item_html, int $cell_width ): string {
// `stripos` (not `strpos`) so an uppercase `<IMG>` fast-path isn't skipped. In practice the
// item HTML arrives lowercased by DOM serialization, but this keeps the guard correct if a
// caller ever passes raw markup.
if ( false === stripos( $item_html, '<img' ) ) {
return $item_html;
}
$item_dom = new Dom_Document_Helper( $item_html );
$images = array();
$remove_targets = array();
foreach ( $item_dom->find_elements( 'img' ) as $img_element ) {
// Record every image up front so none can linger in the preserved remainder — whether we
// rebuild it below or drop it as unrenderable. Targets are computed now (before any removal)
// so the media counts stay accurate.
$remove_targets[] = $this->find_image_removal_target( $img_element );
$normalized_img = $this->normalize_image_for_email( $item_dom->get_outer_html( $img_element ), $cell_width );
if ( '' === $normalized_img ) {
// The image had no usable src (e.g. an unsafe URL esc_url rejected); drop it rather than
// leak the original unsanitized tag through the remainder.
continue;
}
$href = $this->find_link_href( $img_element );
if ( '' !== $href ) {
$images[] = '<a href="' . esc_url( $href ) . '">' . $normalized_img . '</a>';
} else {
$images[] = $normalized_img;
}
}
// The `<img` match was not a real image element (e.g. it sat inside a comment); leave the
// content untouched.
if ( empty( $remove_targets ) ) {
return $item_html;
}
// Strip every image found — including any we couldn't rebuild — so an unrenderable, unsanitized
// `<img>` (e.g. one carrying `onerror`) can never survive through the remainder, then keep
// whatever real content remains (title/date/excerpt). Empty wrapper shells are dropped.
foreach ( $remove_targets as $target ) {
$item_dom->remove_element( $target );
}
return implode( '', $images ) . $this->extract_remaining_content( $item_dom );
}