Automattic\WooCommerce\Internal\EmailEditor\WCTransactionalEmails
WCEmailTemplateDivergenceDetector::run_sweep
Run the post-upgrade divergence sweep.
Intended to be hooked on woocommerce_updated, which fires once per WC upgrade inside \WC_Install::check_version() under a distributed install lock — that guarantees the sweep runs at most once per upgrade without any additional fence option or cache lock on our side.
Early-returns when the RSM-149 backfill has not yet flagged completion, so we never classify a half-populated set of posts.
Method of the class: WCEmailTemplateDivergenceDetector{}
No Hooks.
Returns
null. Nothing (null).
Usage
$result = WCEmailTemplateDivergenceDetector::run_sweep(): void;
Changelog
| Since 10.8.0 | Introduced. |
WCEmailTemplateDivergenceDetector::run_sweep() WCEmailTemplateDivergenceDetector::run sweep code WC 10.8.1
public static function run_sweep(): void {
if ( 'yes' !== get_option( self::BACKFILL_COMPLETE_OPTION ) ) {
return;
}
$registry = WCEmailTemplateSyncRegistry::get_sync_enabled_emails();
if ( empty( $registry ) ) {
return;
}
$posts_manager = WCTransactionalEmailPostsManager::get_instance();
$canonical_emails = $posts_manager->get_emails_by_id();
foreach ( $registry as $email_id => $_config ) {
try {
$email = $canonical_emails[ $email_id ] ?? null;
if ( ! $email instanceof \WC_Email ) {
// Extension providing the email was deactivated; nothing to classify.
continue;
}
$post = $posts_manager->get_email_post( (string) $email_id );
if ( ! $post instanceof \WP_Post ) {
continue;
}
$stored_source_hash = (string) get_post_meta( (int) $post->ID, self::SOURCE_HASH_META_KEY, true );
if ( '' === $stored_source_hash ) {
// This should not normally occur post-backfill: the generator always stamps
// this meta and RSM-149 is supposed to have backfilled pre-existing posts.
// Surface at warning so it's visible in the default WC log UI without
// requiring operators to lower the email-editor logging threshold.
self::get_logger()->warning(
sprintf(
'Email template divergence sweep skipped post %d for email "%s": no stored source hash.',
(int) $post->ID,
(string) $email_id
),
array(
'email_id' => (string) $email_id,
'post_id' => (int) $post->ID,
'context' => 'email_template_divergence_detector',
)
);
continue;
}
$status = self::classify_post(
(int) $post->ID,
$email,
array(
'post_content' => (string) $post->post_content,
'stored_source_hash' => $stored_source_hash,
)
);
if ( null === $status ) {
continue;
}
$existing_status = (string) get_post_meta( (int) $post->ID, self::STATUS_META_KEY, true );
if ( $existing_status === $status ) {
continue;
}
update_post_meta( (int) $post->ID, self::STATUS_META_KEY, $status );
} catch ( \Throwable $e ) {
self::get_logger()->error(
sprintf(
'Email template divergence sweep failed for email "%s": %s',
(string) $email_id,
$e->getMessage()
),
array(
'email_id' => (string) $email_id,
'context' => 'email_template_divergence_detector',
)
);
continue;
}//end try
}//end foreach
}