Automattic\WooCommerce\Internal\EmailEditor\WCTransactionalEmails
WCTransactionalEmailPostsGenerator::generate_single_template
Generate a single email template.
This function generates a single email template post and sets its postmeta association.
Method of the class: WCTransactionalEmailPostsGenerator{}
No Hooks.
Returns
Int. The post ID of the generated template.
Usage
// private - for code of main (parent) class only $result = $this->generate_single_template( $email_type, $email_data );
- $email_type(string) (required)
- The email type.
- $email_data(WC_Email) (required)
- The transactional email data.
WCTransactionalEmailPostsGenerator::generate_single_template() WCTransactionalEmailPostsGenerator::generate single template code WC 10.8.1
private function generate_single_template( $email_type, $email_data ) {
$post_data = self::build_filtered_post_data( (string) $email_type, $email_data );
// Sync meta stamp for emails participating in template update propagation.
$sync_config = WCEmailTemplateSyncRegistry::get_email_sync_config( (string) $email_data->id );
if ( null !== $sync_config ) {
if ( ! isset( $post_data['meta_input'] ) || ! is_array( $post_data['meta_input'] ) ) {
$post_data['meta_input'] = array();
}
$post_data['meta_input'][ WCEmailTemplateDivergenceDetector::VERSION_META_KEY ] = (string) $sync_config['version'];
$post_data['meta_input'][ WCEmailTemplateDivergenceDetector::SOURCE_HASH_META_KEY ] = sha1( (string) ( $post_data['post_content'] ?? '' ) );
$post_data['meta_input'][ WCEmailTemplateDivergenceDetector::LAST_SYNCED_AT_META_KEY ] = gmdate( 'Y-m-d H:i:s' );
$post_data['meta_input'][ WCEmailTemplateDivergenceDetector::LAST_CORE_RENDER_META_KEY ] = (string) ( $post_data['post_content'] ?? '' );
}
$post_id = wp_insert_post( $post_data, true );
if ( is_wp_error( $post_id ) ) {
throw new \Exception( esc_html( $post_id->get_error_message() ) );
}
$this->template_manager->save_email_template_post_id( $email_type, $post_id );
return $post_id;
}