Migrate last email editor files from engine to WP Coding Standard
[MAILPOET-6240]
This commit is contained in:
@@ -1,5 +1,11 @@
|
|||||||
<?php declare(strict_types = 1);
|
<?php
|
||||||
|
/**
|
||||||
|
* This file is part of the MailPoet plugin.
|
||||||
|
*
|
||||||
|
* @package MailPoet\EmailEditor
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types = 1);
|
||||||
namespace MailPoet\EmailEditor\Engine\Renderer;
|
namespace MailPoet\EmailEditor\Engine\Renderer;
|
||||||
|
|
||||||
use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Content_Renderer;
|
use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Content_Renderer;
|
||||||
@@ -10,95 +16,146 @@ use MailPoetVendor\Pelago\Emogrifier\CssInliner;
|
|||||||
use WP_Style_Engine;
|
use WP_Style_Engine;
|
||||||
use WP_Theme_JSON;
|
use WP_Theme_JSON;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Renderer
|
||||||
|
*/
|
||||||
class Renderer {
|
class Renderer {
|
||||||
private Theme_Controller $themeController;
|
/**
|
||||||
private Content_Renderer $contentRenderer;
|
* Theme controller
|
||||||
|
*
|
||||||
|
* @var Theme_Controller
|
||||||
|
*/
|
||||||
|
private Theme_Controller $theme_controller;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Content renderer
|
||||||
|
*
|
||||||
|
* @var Content_Renderer
|
||||||
|
*/
|
||||||
|
private Content_Renderer $content_renderer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Templates
|
||||||
|
*
|
||||||
|
* @var Templates
|
||||||
|
*/
|
||||||
private Templates $templates;
|
private Templates $templates;
|
||||||
/** @var WP_Theme_JSON|null */
|
|
||||||
|
/**
|
||||||
|
* Theme data for the template being rendered.
|
||||||
|
*
|
||||||
|
* @var WP_Theme_JSON|null
|
||||||
|
*/
|
||||||
private static $theme = null;
|
private static $theme = null;
|
||||||
|
|
||||||
const TEMPLATE_FILE = 'template-canvas.php';
|
const TEMPLATE_FILE = 'template-canvas.php';
|
||||||
const TEMPLATE_STYLES_FILE = 'template-canvas.css';
|
const TEMPLATE_STYLES_FILE = 'template-canvas.css';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renderer constructor.
|
||||||
|
*
|
||||||
|
* @param Content_Renderer $content_renderer Content renderer.
|
||||||
|
* @param Templates $templates Templates.
|
||||||
|
* @param Theme_Controller $theme_controller Theme controller.
|
||||||
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
Content_Renderer $contentRenderer,
|
Content_Renderer $content_renderer,
|
||||||
Templates $templates,
|
Templates $templates,
|
||||||
Theme_Controller $themeController
|
Theme_Controller $theme_controller
|
||||||
) {
|
) {
|
||||||
$this->contentRenderer = $contentRenderer;
|
$this->content_renderer = $content_renderer;
|
||||||
$this->templates = $templates;
|
$this->templates = $templates;
|
||||||
$this->themeController = $themeController;
|
$this->theme_controller = $theme_controller;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* During rendering, this stores the theme data for the template being rendered.
|
* During rendering, this stores the theme data for the template being rendered.
|
||||||
*/
|
*/
|
||||||
public static function getTheme() {
|
public static function get_theme() {
|
||||||
return self::$theme;
|
return self::$theme;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function render( \WP_Post $post, string $subject, string $preHeader, string $language, $metaRobots = '' ): array {
|
/**
|
||||||
$templateId = 'mailpoet/mailpoet//' . ( get_page_template_slug( $post ) ?: 'email-general' );
|
* Renders the email template
|
||||||
$template = $this->templates->get_block_template( $templateId );
|
*
|
||||||
$theme = $this->templates->get_block_template_theme( $templateId, $template->wp_id ); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
|
* @param \WP_Post $post Post object.
|
||||||
|
* @param string $subject Email subject.
|
||||||
|
* @param string $pre_header Email preheader.
|
||||||
|
* @param string $language Email language.
|
||||||
|
* @param string $meta_robots Email meta robots.
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function render( \WP_Post $post, string $subject, string $pre_header, string $language, $meta_robots = '' ): array { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
|
||||||
|
$template_id = 'mailpoet/mailpoet//' . ( get_page_template_slug( $post ) ? get_page_template_slug( $post ) : 'email-general' );
|
||||||
|
$template = $this->templates->get_block_template( $template_id );
|
||||||
|
$theme = $this->templates->get_block_template_theme( $template_id, $template->wp_id );
|
||||||
|
|
||||||
// Set the theme for the template. This is merged with base theme.json and core json before rendering.
|
// Set the theme for the template. This is merged with base theme.json and core json before rendering.
|
||||||
self::$theme = new WP_Theme_JSON( $theme, 'default' );
|
self::$theme = new WP_Theme_JSON( $theme, 'default' );
|
||||||
|
|
||||||
$emailStyles = $this->themeController->get_styles( $post, $template );
|
$email_styles = $this->theme_controller->get_styles( $post, $template );
|
||||||
$templateHtml = $this->contentRenderer->render( $post, $template );
|
$template_html = $this->content_renderer->render( $post, $template );
|
||||||
$layout = $this->themeController->get_layout_settings();
|
$layout = $this->theme_controller->get_layout_settings();
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
include self::TEMPLATE_FILE;
|
include self::TEMPLATE_FILE;
|
||||||
$renderedTemplate = (string) ob_get_clean();
|
$rendered_template = (string) ob_get_clean();
|
||||||
|
|
||||||
$templateStyles =
|
$template_styles =
|
||||||
WP_Style_Engine::compile_css(
|
WP_Style_Engine::compile_css(
|
||||||
array(
|
array(
|
||||||
'background-color' => $emailStyles['color']['background'] ?? 'inherit',
|
'background-color' => $email_styles['color']['background'] ?? 'inherit',
|
||||||
'color' => $emailStyles['color']['text'] ?? 'inherit',
|
'color' => $email_styles['color']['text'] ?? 'inherit',
|
||||||
'padding-top' => $emailStyles['spacing']['padding']['top'] ?? '0px',
|
'padding-top' => $email_styles['spacing']['padding']['top'] ?? '0px',
|
||||||
'padding-bottom' => $emailStyles['spacing']['padding']['bottom'] ?? '0px',
|
'padding-bottom' => $email_styles['spacing']['padding']['bottom'] ?? '0px',
|
||||||
'padding-left' => $emailStyles['spacing']['padding']['left'] ?? '0px',
|
'padding-left' => $email_styles['spacing']['padding']['left'] ?? '0px',
|
||||||
'padding-right' => $emailStyles['spacing']['padding']['right'] ?? '0px',
|
'padding-right' => $email_styles['spacing']['padding']['right'] ?? '0px',
|
||||||
'font-family' => $emailStyles['typography']['fontFamily'] ?? 'inherit',
|
'font-family' => $email_styles['typography']['fontFamily'] ?? 'inherit',
|
||||||
'line-height' => $emailStyles['typography']['lineHeight'] ?? '1.5',
|
'line-height' => $email_styles['typography']['lineHeight'] ?? '1.5',
|
||||||
'font-size' => $emailStyles['typography']['fontSize'] ?? 'inherit',
|
'font-size' => $email_styles['typography']['fontSize'] ?? 'inherit',
|
||||||
),
|
),
|
||||||
'body, .email_layout_wrapper'
|
'body, .email_layout_wrapper'
|
||||||
);
|
);
|
||||||
$templateStyles .= '.email_layout_wrapper { box-sizing: border-box;}';
|
$template_styles .= '.email_layout_wrapper { box-sizing: border-box;}';
|
||||||
$templateStyles .= file_get_contents( __DIR__ . '/' . self::TEMPLATE_STYLES_FILE );
|
$template_styles .= file_get_contents( __DIR__ . '/' . self::TEMPLATE_STYLES_FILE );
|
||||||
$templateStyles = '<style>' . wp_strip_all_tags( (string) apply_filters( 'mailpoet_email_renderer_styles', $templateStyles, $post ) ) . '</style>';
|
$template_styles = '<style>' . wp_strip_all_tags( (string) apply_filters( 'mailpoet_email_renderer_styles', $template_styles, $post ) ) . '</style>';
|
||||||
$renderedTemplate = $this->inlineCSSStyles( $templateStyles . $renderedTemplate );
|
$rendered_template = $this->inline_css_styles( $template_styles . $rendered_template );
|
||||||
|
|
||||||
// This is a workaround to support link :hover in some clients. Ideally we would remove the ability to set :hover
|
// This is a workaround to support link :hover in some clients. Ideally we would remove the ability to set :hover
|
||||||
// however this is not possible using the color panel from Gutenberg.
|
// however this is not possible using the color panel from Gutenberg.
|
||||||
if ( isset( $emailStyles['elements']['link'][':hover']['color']['text'] ) ) {
|
if ( isset( $email_styles['elements']['link'][':hover']['color']['text'] ) ) {
|
||||||
$renderedTemplate = str_replace( '<!-- Forced Styles -->', '<style>a:hover { color: ' . esc_attr( $emailStyles['elements']['link'][':hover']['color']['text'] ) . ' !important; }</style>', $renderedTemplate );
|
$rendered_template = str_replace( '<!-- Forced Styles -->', '<style>a:hover { color: ' . esc_attr( $email_styles['elements']['link'][':hover']['color']['text'] ) . ' !important; }</style>', $rendered_template );
|
||||||
}
|
}
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
'html' => $renderedTemplate,
|
'html' => $rendered_template,
|
||||||
'text' => $this->renderTextVersion( $renderedTemplate ),
|
'text' => $this->render_text_version( $rendered_template ),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $template
|
* Inlines CSS styles into the HTML
|
||||||
|
*
|
||||||
|
* @param string $template HTML template.
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function inlineCSSStyles( $template ) {
|
private function inline_css_styles( $template ) {
|
||||||
return CssInliner::fromHtml( $template )->inlineCss()->render();
|
return CssInliner::fromHtml( $template )->inlineCss()->render();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $template
|
* Renders the text version of the email template
|
||||||
|
*
|
||||||
|
* @param string $template HTML template.
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function renderTextVersion( $template ) {
|
private function render_text_version( $template ) {
|
||||||
$template = ( mb_detect_encoding( $template, 'UTF-8', true ) ) ? $template : mb_convert_encoding( $template, 'UTF-8', mb_list_encodings() );
|
$template = ( mb_detect_encoding( $template, 'UTF-8', true ) ) ? $template : mb_convert_encoding( $template, 'UTF-8', mb_list_encodings() );
|
||||||
return @Html2Text::convert( $template );
|
$result = Html2Text::convert( $template );
|
||||||
|
if ( false === $result ) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,11 @@
|
|||||||
<?php declare(strict_types = 1);
|
<?php
|
||||||
|
/**
|
||||||
|
* This file is part of the MailPoet plugin.
|
||||||
|
*
|
||||||
|
* @package MailPoet\EmailEditor
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types = 1);
|
||||||
|
|
||||||
// phpcs:disable Generic.Files.InlineHTML.Found
|
// phpcs:disable Generic.Files.InlineHTML.Found
|
||||||
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
|
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||||
@@ -8,9 +15,9 @@
|
|||||||
* Variables passed to this template:
|
* Variables passed to this template:
|
||||||
*
|
*
|
||||||
* @var $subject string
|
* @var $subject string
|
||||||
* @var $preHeader string
|
* @var $pre_header string
|
||||||
* @var $templateHtml string
|
* @var $template_html string
|
||||||
* @var $metaRobots string
|
* @var $meta_robots string
|
||||||
* @var $layout array{contentSize: string}
|
* @var $layout array{contentSize: string}
|
||||||
*/
|
*/
|
||||||
?><!DOCTYPE html>
|
?><!DOCTYPE html>
|
||||||
@@ -22,7 +29,7 @@
|
|||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||||
<meta name="format-detection" content="telephone=no" />
|
<meta name="format-detection" content="telephone=no" />
|
||||||
<?php echo $metaRobots; // HTML defined by MailPoet--do not escape ?>
|
<?php echo $meta_robots; // HTML defined by MailPoet--do not escape. ?>
|
||||||
<!-- Forced Styles -->
|
<!-- Forced Styles -->
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@@ -32,12 +39,12 @@
|
|||||||
<tbody>
|
<tbody>
|
||||||
<tr>
|
<tr>
|
||||||
<td class="email_preheader" height="1">
|
<td class="email_preheader" height="1">
|
||||||
<?php echo esc_html( wp_strip_all_tags( $preHeader ) ); ?>
|
<?php echo esc_html( wp_strip_all_tags( $pre_header ) ); ?>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td class="email_content_wrapper">
|
<td class="email_content_wrapper">
|
||||||
<?php echo $templateHtml; ?>
|
<?php echo $template_html; ?>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
|
@@ -52,8 +52,8 @@ class Theme_Controller {
|
|||||||
$theme->merge( $this->core_theme );
|
$theme->merge( $this->core_theme );
|
||||||
$theme->merge( $this->base_theme );
|
$theme->merge( $this->base_theme );
|
||||||
|
|
||||||
if ( Renderer::getTheme() !== null ) {
|
if ( Renderer::get_theme() !== null ) {
|
||||||
$theme->merge( Renderer::getTheme() );
|
$theme->merge( Renderer::get_theme() );
|
||||||
}
|
}
|
||||||
|
|
||||||
return apply_filters( 'mailpoet_email_editor_theme_json', $theme );
|
return apply_filters( 'mailpoet_email_editor_theme_json', $theme );
|
||||||
|
Reference in New Issue
Block a user