Automattic\WooCommerce\Internal\EmailEditor\WCTransactionalEmails
WCEmailScratchpadRefresher::refresh_content
Refresh the scratchpad's content and title to the current file template.
The title is system-owned, so it moves with the content. Keeps the sync meta baseline in step with the new content so a later was_never_edited() check still recognizes the post as untouched.
Method of the class: WCEmailScratchpadRefresher{}
No Hooks.
Returns
null. Nothing (null).
Usage
// private - for code of main (parent) class only $result = $this->refresh_content( $scratchpad, $email ): void;
- $scratchpad(WP_Post) (required)
- The unpublished scratchpad post.
- $email(WC_Email) (required)
- The email instance.
WCEmailScratchpadRefresher::refresh_content() WCEmailScratchpadRefresher::refresh content code WC 11.2.1
private function refresh_content( \WP_Post $scratchpad, \WC_Email $email ): void {
$post_data = WCTransactionalEmailPostsGenerator::build_filtered_post_data( (string) $email->id, $email );
$canonical = (string) ( $post_data['post_content'] ?? '' );
$canonical_title = (string) ( $post_data['post_title'] ?? '' );
if ( '' === $canonical || ( $canonical === $scratchpad->post_content && $canonical_title === $scratchpad->post_title ) ) {
return;
}
$updated = wp_update_post(
array(
'ID' => $scratchpad->ID,
'post_content' => $canonical,
'post_title' => $canonical_title,
// The explicit empty value makes core skip template handling
// entirely, leaving the stored `_wp_page_template` meta as is.
// With the key omitted, wp_update_post() would re-inject the
// stored template (WP_Post::to_array()) and fail with "Invalid
// page template." whenever the email template is not
// registered — it only is while the editor package is
// bootstrapped.
'page_template' => '',
),
true
);
if ( is_wp_error( $updated ) ) {
return;
}
$saved_post = get_post( $scratchpad->ID );
$saved_body = $saved_post instanceof \WP_Post ? (string) $saved_post->post_content : $canonical;
// Restamped for every email, not only sync-registry ones: the update
// above bumped `post_modified`, so without a matching hash the
// timestamp fallback in `was_never_edited()` would treat this
// scratchpad as edited forever and this refresh would never run again.
update_post_meta( $scratchpad->ID, WCEmailTemplateDivergenceDetector::SOURCE_HASH_META_KEY, sha1( $saved_body ) );
$sync_config = WCEmailTemplateSyncRegistry::get_email_sync_config( (string) $email->id );
if ( null !== $sync_config ) {
update_post_meta( $scratchpad->ID, WCEmailTemplateDivergenceDetector::VERSION_META_KEY, (string) $sync_config['version'] );
update_post_meta( $scratchpad->ID, WCEmailTemplateDivergenceDetector::LAST_SYNCED_AT_META_KEY, gmdate( 'Y-m-d H:i:s' ) );
update_post_meta( $scratchpad->ID, WCEmailTemplateDivergenceDetector::LAST_CORE_RENDER_META_KEY, $canonical );
}
$scratchpad->post_content = $saved_body;
$scratchpad->post_title = $canonical_title;
}