Automattic\WooCommerce\Internal\EmailEditor\WCTransactionalEmails

WCTransactionalEmailPostsGenerator::generate_single_templateprivateWC 1.0

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() code WC 10.3.6

private function generate_single_template( $email_type, $email_data ) {
	$email_enabled = $email_data->is_enabled() || $email_data->is_manual();
	$post_data     = array(
		'post_type'    => Integration::EMAIL_POST_TYPE,
		'post_status'  => $email_enabled ? 'publish' : 'draft',
		'post_name'    => $email_type,
		'post_title'   => $email_data->get_title(),
		'post_excerpt' => $email_data->get_description(),
		'post_content' => $this->get_email_template( $email_data ),
		'meta_input'   => array(
			'_wp_page_template' => ( new WooEmailTemplate() )->get_slug(),
		),
	);

	$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;
}