Automattic\WooCommerce\Internal\EmailEditor\WCTransactionalEmails

WCEmailTemplateChangeSummary::attrs_hash │ private static │ WC 1.0

Hash a block's attributes so two attribute sets can be compared as strings. Keys are sorted at every level, so key order never reads as an edit.

The block editor rewrites some attributes on save, on blocks nobody edited. Each rewrite we know of is folded into its saved form first, so the PHP render of a template and the editor's save of it hash the same. The list is deliberately short and explicit: an unknown rewrite makes a block read as merchant-edited, which asks the merchant instead of overwriting them.

Method of the class: WCEmailTemplateChangeSummary{}

No Hooks.

Returns

null. Nothing (null).

Usage

$result = WCEmailTemplateChangeSummary::attrs_hash( $block_name, $attrs ): string;
$block_name(string) (required)
Normalized block name.
$attrs(array<string, mixed>) (required)
Block attributes from parse_blocks().

WCEmailTemplateChangeSummary::attrs_hash() code WC 11.3.0

private static function attrs_hash( string $block_name, array $attrs ): string {
	// Since WordPress 7.1 text alignment is saved as `style.typography.textAlign`:
	// the `textAlign` attribute on any block, and `align` on paragraphs. Elsewhere
	// `align` is layout alignment (wide, full, float) and is left alone.
	$legacy_key = isset( $attrs['textAlign'] ) ? 'textAlign' : ( 'core/paragraph' === $block_name && isset( $attrs['align'] ) ? 'align' : null );
	// Hand-authored block JSON can carry a non-array `style`; leave it alone.
	if (
		null !== $legacy_key
		&& is_array( $attrs['style'] ?? array() )
		&& is_array( $attrs['style']['typography'] ?? array() )
		&& ! isset( $attrs['style']['typography']['textAlign'] )
	) {
		$attrs['style']['typography']['textAlign'] = $attrs[ $legacy_key ];
		unset( $attrs[ $legacy_key ] );
	}

	self::ksort_recursive( $attrs );
	$encoded = wp_json_encode( $attrs );

	// An encode failure (invalid UTF-8, or nesting past the depth limit)
	// would hash every such block alike and read as "unchanged". Fall back
	// to serialize(), which handles both.
	return sha1( false === $encoded ? serialize( $attrs ) : $encoded ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- Hash input only; never unserialized.
}