Render using custom template on preview

This commit is contained in:
Mike Jolley
2024-04-12 11:32:01 +01:00
parent 36ff27d927
commit 76f6e7cdbd
2 changed files with 29 additions and 18 deletions

View File

@@ -51,8 +51,7 @@ class Renderer {
$templateStyles = file_get_contents(dirname(__FILE__) . '/' . self::TEMPLATE_STYLES_FILE);
$templateStyles = apply_filters('mailpoet_email_renderer_styles', $templateStyles, $post);
// @todo use custom template to render.
$templateHtml = $this->contentRenderer->render($post, $this->templates->getBlockTemplateFromFile('email-general.html'));
$templateHtml = $this->contentRenderer->render($post, $this->templates->getBlockTemplate('mailpoet/mailpoet//email-general'));
ob_start();
include self::TEMPLATE_FILE;

View File

@@ -23,23 +23,23 @@ class Templates {
add_filter('get_block_template', [$this, 'addBlockTemplateDetails'], 10, 1);
}
public function getBlockFileTemplate($template, $id, $template_type) {
$template_name_parts = explode('//', $id);
public function getBlockFileTemplate($return, $templateId, $template_type) {
$template_name_parts = explode('//', $templateId);
if (count($template_name_parts) < 2) {
return $template;
return $return;
}
list( $template_id, $template_slug ) = $template_name_parts;
list( $templatePrefix, $templateSlug ) = $template_name_parts;
if ($this->pluginSlug !== $template_id) {
return $template;
if ($this->pluginSlug !== $templatePrefix) {
return $return;
}
$templatePath = $template_slug . '.html';
$templatePath = $templateSlug . '.html';
if (!is_readable($this->templateDirectory . $templatePath)) {
return $template;
return $return;
}
return $this->getBlockTemplateFromFile($templatePath);
@@ -88,21 +88,33 @@ class Templates {
return $block_template;
}
private function getBlockTemplates() {
public function getBlockTemplate($templateId) {
$templates = $this->getBlockTemplates();
return $templates[$templateId] ?? null;
}
/**
* Gets block templates indexed by ID.
*/
public function getBlockTemplates() {
$file_templates = [
$this->getBlockTemplateFromFile('email-general.html'),
];
$custom_templates = $this->getCustomBlockTemplates();
$custom_template_ids = wp_list_pluck($custom_templates, 'id');
return array_merge(
$custom_templates,
array_filter(
$file_templates,
function($blockTemplate) use ($custom_template_ids) {
return !in_array($blockTemplate->id, $custom_template_ids, true);
}
return array_column(
array_merge(
$custom_templates,
array_filter(
$file_templates,
function($blockTemplate) use ($custom_template_ids) {
return !in_array($blockTemplate->id, $custom_template_ids, true);
}
),
),
null,
'id'
);
}