Automattic\WooCommerce\Internal\EmailEditor\WCTransactionalEmails
WCTransactionalEmailPostsGenerator::render_block_template_html
Render the block template HTML for a given email.
Resolves the block template (honouring theme overrides), falls back to the default block content on failure, and applies the woocommerce_email_block_template_html Stateless so both the generator (via {@see self::get_email_template()}) and the divergence detector observe an identical rendering pipeline.
Method of the class: WCTransactionalEmailPostsGenerator{}
Hooks from the method
Returns
String. The rendered template HTML.
Usage
$result = WCTransactionalEmailPostsGenerator::render_block_template_html( $email ): string;
- $email(WC_Email) (required)
- The email object.
Changelog
| Since 10.8.0 | Introduced. |
WCTransactionalEmailPostsGenerator::render_block_template_html() WCTransactionalEmailPostsGenerator::render block template html code WC 10.8.1
public static function render_block_template_html( $email ): string {
$template_name = self::resolve_block_template_name( $email );
try {
$template_html = wc_get_template_html(
$template_name,
array(),
'',
(string) $email->template_base
);
} catch ( \Exception $e ) {
// wc_get_template_html() uses ob_start(), so we need to clean the output buffer if an exception is thrown.
if ( ob_get_level() > 0 ) {
ob_end_clean();
}
$template_html = '';
}
// wc_get_template_html does not throw an error when the template is not found.
// We need to check if the template is not found by checking the template_html content.
$has_template_error =
StringUtil::contains( $template_html, 'No such file or directory', false ) ||
StringUtil::contains( $template_html, 'Failed to open stream', false ) ||
StringUtil::contains( $template_html, 'Warning: include', false );
if ( is_wp_error( $template_html ) || empty( $template_html ) || $has_template_error ) {
$default_template_name = 'emails/block/default-block-content.php';
$template_html = wc_get_template_html(
$default_template_name,
array()
);
}
/**
* Filter the email template HTML.
*
* @param string $template_html The email template HTML.
* @param \WC_Email $email The email object.
* @since 10.7.0
*/
$filtered_template_html = apply_filters( 'woocommerce_email_block_template_html', $template_html, $email );
return is_string( $filtered_template_html ) ? $filtered_template_html : $template_html;
}