Automattic\WooCommerce\Internal\EmailEditor\WCTransactionalEmails

WCEmailTemplateSelectiveApplier::merge │ private static │ WC 1.0

Compute the merged block tree, starting from the merchant's post and layering on core's changes per the v1 algorithm.

With $base_content (the caller's last_core_render meta was set), core and post blocks are paired through the base with WCEmailTemplateChangeSummary::align_to_base(), the same pairing the summary shown to the merchant uses, and each pair is classified with WCEmailTemplateChangeSummary::merchant_changed(), the same rule:

  • Only a block core reworded is on offer, as in the drawer. A styling-only core change is not applied, and neither is anything on a block that holds other blocks.
  • A reworded block the merchant never touched defaults to use_core — the drawer lists it as auto-resolved and states it will apply.
  • A reworded block the merchant also changed defaults to keep_yours.
  • Whichever way use_core is reached, the block takes core's attrs along with its content, so it is core's version whole.
  • Core blocks the merchant deleted are not re-inserted.

Without $base_content (legacy two-way fallback), core and post are paired with a direct LCS, every matched pair with differing inner_text is eligible for use_core, and the default stays keep_yours.

Method of the class: WCEmailTemplateSelectiveApplier{}

No Hooks.

Returns

array{content:string, structural_skipped:bool, aliases_migrated:string[]}.

Usage

$result = WCEmailTemplateSelectiveApplier::merge( $post_content, $core_content, $choices, $base_content ): array;
$post_content(string) (required)
Merchant's current post_content.
$core_content(string) (required)
Canonical core render.
$choices(array) (required)
.
$base_content(string)
The last_core_render, or '' for the legacy two-way merge.
Default: ''

WCEmailTemplateSelectiveApplier::merge() code WC 11.3.0

private static function merge( string $post_content, string $core_content, array $choices, string $base_content = '' ): array {
	$post_blocks = parse_blocks( $post_content );
	$core_blocks = parse_blocks( $core_content );

	if ( empty( $post_blocks ) || empty( $core_blocks ) ) {
		return array(
			'content'            => $post_content,
			'structural_skipped' => false,
			'aliases_migrated'   => array(),
		);
	}

	$post_records = WCEmailTemplateChangeSummary::flatten_blocks( $post_blocks );
	$core_records = WCEmailTemplateChangeSummary::flatten_blocks( $core_blocks );

	$three_way                 = '' !== $base_content;
	$merchant_removed_core_set = array();
	$base_of_post              = array();
	if ( $three_way ) {
		$base_records = WCEmailTemplateChangeSummary::flatten_blocks( parse_blocks( $base_content ) );
		$matches      = array();
		foreach ( WCEmailTemplateChangeSummary::align_to_base( $core_records, $base_records, $post_records ) as $base_idx => $sides ) {
			if ( null === $sides['core'] ) {
				continue;
			}
			if ( null === $sides['post'] ) {
				$merchant_removed_core_set[ $sides['core'] ] = true;
				continue;
			}
			$matches[]                      = array( $sides['core'], $sides['post'] );
			$base_of_post[ $sides['post'] ] = $base_records[ $base_idx ];
		}
	} else {
		$matches = WCEmailTemplateChangeSummary::lcs_matches( $core_records, $post_records );
	}

	$choice_map = array();
	foreach ( $choices as $choice ) {
		if ( ! is_array( $choice ) || ! isset( $choice['path'] ) || ! is_array( $choice['path'] ) ) {
			continue;
		}
		$decision = (string) ( $choice['decision'] ?? 'keep_yours' );
		if ( 'use_core' !== $decision && 'keep_yours' !== $decision ) {
			continue;
		}
		$choice_map[ self::path_key( $choice['path'] ) ] = $decision;
	}

	// Pass 1: matched pairs. Apply use_core decisions on copy changes;
	// detect parent-name diffs (structural punted, but we still flag
	// `structural_skipped` so the caller can surface it).
	$structural_skipped = false;
	$matched_core_set   = array();
	$matched_post_set   = array();
	foreach ( $matches as $pair ) {
		$core_rec = $core_records[ $pair[0] ];
		$post_rec = $post_records[ $pair[1] ];
		$post_key = self::path_key( $post_rec['path'] );

		$matched_core_set[ $pair[0] ] = true;
		$matched_post_set[ $pair[1] ] = true;

		if ( $core_rec['parent_name'] !== $post_rec['parent_name'] ) {
			$structural_skipped = true;
		}

		$core_block = self::block_at_path( $core_blocks, $core_rec['path'] );
		$post_block = self::block_at_path( $post_blocks, $post_rec['path'] );
		if ( null === $core_block || null === $post_block ) {
			continue;
		}

		// A block holding other blocks is left alone. Taking core's content
		// would replace `innerBlocks` wholesale and discard merchant edits this
		// same pass is preserving, since the children are paired separately and
		// each gets its own turn. The quote in the customer-note template is one.
		if ( ! empty( $core_block['innerBlocks'] ) || ! empty( $post_block['innerBlocks'] ) ) {
			continue;
		}

		$auto_resolvable = false;
		if ( $three_way ) {
			$base_rec = $base_of_post[ $pair[1] ];

			// Only a block core reworded is on offer, which is what the drawer
			// lists. A styling-only core change has no wording to show there,
			// so apply leaves it out too rather than change a block unannounced.
			if ( $base_rec['inner_text'] === $core_rec['inner_text'] ) {
				continue;
			}

			// The merchant already has core's markup, having typed it themselves.
			if ( $core_rec['inner_html_hash'] === $post_rec['inner_html_hash'] ) {
				continue;
			}

			$auto_resolvable = ! WCEmailTemplateChangeSummary::merchant_changed( $base_rec, $post_rec );
		} elseif ( $core_rec['inner_text'] === $post_rec['inner_text'] ) {
			continue;
		}

		// A block only core changed is listed in the drawer as auto-resolved,
		// stating the update will apply, and no choice is sent for it — by the
		// drawer or by a bulk or CLI caller — so the default has to carry it.
		// A choice, when one is sent, still wins.
		$decision = $choice_map[ $post_key ] ?? ( $auto_resolvable ? 'use_core' : 'keep_yours' );
		if ( 'use_core' !== $decision ) {
			continue;
		}

		// Core's `attrs` come along with its content: taking the text while
		// keeping the merchant's styling leaves the block describing markup it
		// no longer has, and "Use core" says the merchant's edit is discarded.
		// The legacy two-way path has no base to judge against, so it keeps
		// its old attrs-preserving behaviour.
		$post_blocks = self::replace_block_content_at_path( $post_blocks, $post_rec['path'], $core_block, $three_way );
	}//end foreach

	// Pass 2: unmatched core records. Insert non-structural blocks at
	// the equivalent path; flag structural wrappers as skipped.
	$insertions = array();
	foreach ( $core_records as $i => $rec ) {
		if ( isset( $matched_core_set[ $i ] ) ) {
			continue;
		}
		if ( self::is_structural_block( $rec['name'] ) ) {
			$structural_skipped = true;
			continue;
		}
		if ( isset( $merchant_removed_core_set[ $i ] ) ) {
			continue;
		}
		$core_block = self::block_at_path( $core_blocks, $rec['path'] );
		if ( null === $core_block ) {
			continue;
		}
		$insertions[] = array(
			'path'  => $rec['path'],
			'block' => $core_block,
		);
	}

	// Insert in order of decreasing path-depth+index so each insert's
	// target index isn't shifted by a prior insert at the same level.
	usort(
		$insertions,
		static function ( array $a, array $b ): int {
			$path_a    = $a['path'];
			$path_b    = $b['path'];
			$depth_cmp = count( $path_b ) - count( $path_a );
			if ( 0 !== $depth_cmp ) {
				return $depth_cmp;
			}
			$last_a = end( $path_a );
			$last_b = end( $path_b );
			return ( (int) $last_b ) - ( (int) $last_a );
		}
	);
	foreach ( $insertions as $insertion ) {
		$post_blocks = self::insert_block_at_path( $post_blocks, $insertion['path'], $insertion['block'] );
	}

	// Pass 3: unmatched post records (`removed_blocks`). Auto-resolved
	// as Keep yours — no change. Detect structural wrappers solely so
	// we can flag `structural_skipped` honestly.
	foreach ( $post_records as $i => $rec ) {
		if ( isset( $matched_post_set[ $i ] ) ) {
			continue;
		}
		if ( self::is_structural_block( $rec['name'] ) ) {
			$structural_skipped = true;
		}
	}

	// Final pass: explicit deprecated-namespace migration. Whenever a
	// `wp:woo/email-content` block is found in the merged tree, rewrite
	// it to the canonical `wp:woocommerce/email-content` form, including
	// the `wp-block-{old}` CSS class on the inner div so the comment and
	// class stay consistent. The block's `attrs` and inner content are
	// preserved — only the namespace label changes. This is unconditional
	// (independent of `choices`) because `woo/email-content` is a known
	// alias of the canonical core block, not a customisation worth
	// preserving.
	$aliases_migrated = array();
	$post_blocks      = self::migrate_woo_email_content_namespace( $post_blocks, $aliases_migrated );

	return array(
		// $post_blocks originates from parse_blocks() and our mutations only
		// rewrite well-typed fields; serialize_blocks accepts the same shape.
		// PHPStan can't follow the mutation chain, so the explicit ignore.
		// @phpstan-ignore-next-line argument.type
		'content'            => serialize_blocks( $post_blocks ),
		'structural_skipped' => $structural_skipped,
		'aliases_migrated'   => array_values( array_unique( $aliases_migrated ) ),
	);
}