Automattic\WooCommerce\Internal\EmailEditor\WCTransactionalEmails
WCEmailTemplateDivergenceDetector::classify_post
Classify a single generated woo_email post.
Pure function: given the inputs, always returns the same outcome.
Classification hinges on two independent questions:
- Has core moved since we stamped the post? (
currentCoreHash !== storedSourceHash) - Has the merchant edited the post since we stamped it? (
currentPostHash !== storedSourceHash)
Note that "uncustomized" here means the merchant has not edited the post, which is detected by currentPostHash === storedSourceHash — NOT by comparing against the new core hash. The latter would only hold after an auto-apply step which this code path does not perform.
Returns null when the stored baseline is ambiguous — i.e. core has not moved but the post has drifted from the stamp. In that case the existing status is preserved rather than overwritten with a new guess.
Method of the class: WCEmailTemplateDivergenceDetector{}
No Hooks.
Returns
String|null. One of the STATUS_* constants, or null when the status should not be updated.
Usage
$result = WCEmailTemplateDivergenceDetector::classify_post( $post_id, $email, $stamps ): ?string;
- $post_id(int) (required)
- The post ID (kept in the signature for context in tests and logs).
- $email(WC_Email) (required)
- The registered email instance.
- $stamps(array) (required)
- Map with keys post_content persisted content) and
stored_source_hash(value of_wc_email_template_source_hash).
Changelog
| Since 10.8.0 | Introduced. |
WCEmailTemplateDivergenceDetector::classify_post() WCEmailTemplateDivergenceDetector::classify post code WC 10.8.1
public static function classify_post( int $post_id, \WC_Email $email, array $stamps ): ?string {
// $post_id is surfaced in the signature for future instrumentation and log context; no current use.
unset( $post_id );
$current_core_hash = sha1( WCTransactionalEmailPostsGenerator::compute_canonical_post_content( $email ) );
$current_post_hash = sha1( (string) ( $stamps['post_content'] ?? '' ) );
$stored_source_hash = (string) ( $stamps['stored_source_hash'] ?? '' );
if ( ! self::is_sha1_hash( $stored_source_hash ) ) {
return null;
}
// Core has not moved since stamping. If the post also matches the stamp we're in sync;
// otherwise the merchant drifted without a core update — ambiguous, leave prior status.
if ( $current_core_hash === $stored_source_hash ) {
return $current_post_hash === $stored_source_hash ? self::STATUS_IN_SYNC : null;
}
// Core has moved. Did the merchant edit the post since we stamped it?
return $current_post_hash === $stored_source_hash
? self::STATUS_CORE_UPDATED_UNCUSTOMIZED
: self::STATUS_CORE_UPDATED_CUSTOMIZED;
}