Automattic\WooCommerce\Blocks\BlockTypes
MiniCart::process_template_contents
Process template contents to remove unwanted div wrappers.
The old Mini Cart template had extra divs nested within the block tags that are no longer necessary since we don't render the Mini Cart with React anymore. To maintain compatibility with user saved templates that have these wrapper divs, we must remove them.
Method of the class: MiniCart{}
No Hooks.
Returns
String. The processed template contents.
Usage
// protected - for code of main (parent) or child class $result = $this->process_template_contents( $template_contents );
- $template_contents(string) (required)
- The template contents to process.
MiniCart::process_template_contents() MiniCart::process template contents code WC 10.3.6
protected function process_template_contents( $template_contents ) {
$p = new \WP_HTML_Tag_Processor( $template_contents );
$is_old_template = $p->next_tag(
array(
'tag_name' => 'div',
'class_name' => 'wp-block-woocommerce-mini-cart-contents',
)
);
if ( ! $is_old_template ) {
return $template_contents;
}
$output = '';
$was_at = 0;
$is_mini_cart_block_stack = array( false );
foreach ( Block_Delimiter::scan_delimiters( $template_contents ) as $where => $delimiter ) {
list( $at, $length ) = $where;
$block_type = $delimiter->allocate_and_return_block_type();
$delimiter_type = $delimiter->get_delimiter_type();
if ( ! $is_mini_cart_block_stack[ array_key_last( $is_mini_cart_block_stack ) ] ) {
// Copy content up to and including this block delimiter.
$output .= substr( $template_contents, $was_at, $at + $length - $was_at );
} else {
// Just copy the block delimiter, skipping the wrapper div that existed before.
$output .= substr( $template_contents, $at, $length );
}
// Update the position to the end of the block delimiter.
$was_at = $at + $length;
if ( Block_Delimiter::OPENER === $delimiter_type ) {
// Add the Mini Cart block info to a stack.
$is_mini_cart_block_stack[] = in_array( $block_type, self::MINI_CART_TEMPLATE_BLOCKS, true );
} elseif ( Block_Delimiter::CLOSER === $delimiter_type ) {
// Pop the last Mini Cart block info from the stack.
array_pop( $is_mini_cart_block_stack );
}
}
// Add any remaining content.
$output .= substr( $template_contents, $was_at );
return $output;
}