Refactor email content styles from string to array

[MAILPOET-5591]
This commit is contained in:
Jan Lysý
2023-10-18 19:45:26 +02:00
committed by Jan Lysý
parent 1250d81670
commit 5946884cb2
2 changed files with 40 additions and 15 deletions

View File

@@ -42,17 +42,16 @@ class Renderer {
$parser = new \WP_Block_Parser(); $parser = new \WP_Block_Parser();
$parsedBlocks = $parser->parse($post->post_content); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps $parsedBlocks = $parser->parse($post->post_content); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$parsedBlocks = $this->preprocessManager->preprocess($parsedBlocks, $this->stylesController->getEmailLayoutStyles()); $layoutStyles = $this->stylesController->getEmailLayoutStyles();
$parsedBlocks = $this->preprocessManager->preprocess($parsedBlocks, $layoutStyles);
$renderedBody = $this->blocksRenderer->render($parsedBlocks); $renderedBody = $this->blocksRenderer->render($parsedBlocks);
$styles = (string)file_get_contents(dirname(__FILE__) . '/' . self::TEMPLATE_STYLES_FILE); $styles = (string)file_get_contents(dirname(__FILE__) . '/' . self::TEMPLATE_STYLES_FILE);
$styles .= $this->stylesController->getEmailContentStyles();
$styles = apply_filters('mailpoet_email_renderer_styles', $styles, $post); $styles = apply_filters('mailpoet_email_renderer_styles', $styles, $post);
$template = (string)file_get_contents(dirname(__FILE__) . '/' . self::TEMPLATE_FILE); $template = (string)file_get_contents(dirname(__FILE__) . '/' . self::TEMPLATE_FILE);
// Apply layout styles // Apply layout styles
$layoutStyles = $this->stylesController->getEmailLayoutStyles();
$template = str_replace( $template = str_replace(
['{{width}}', '{{background}}', '{{padding_top}}', '{{padding_right}}', '{{padding_bottom}}', '{{padding_left}}'], ['{{width}}', '{{background}}', '{{padding_top}}', '{{padding_right}}', '{{padding_bottom}}', '{{padding_left}}'],
[$layoutStyles['width'], $layoutStyles['background'], $layoutStyles['padding']['top'], $layoutStyles['padding']['right'], $layoutStyles['padding']['bottom'], $layoutStyles['padding']['left']], [$layoutStyles['width'], $layoutStyles['background'], $layoutStyles['padding']['top'], $layoutStyles['padding']['right'], $layoutStyles['padding']['bottom'], $layoutStyles['padding']['left']],

View File

@@ -25,20 +25,46 @@ class StylesController {
* Default styles applied to the email. These are going to be replaced by style settings. * Default styles applied to the email. These are going to be replaced by style settings.
* This is currently more af a proof of concept that we can apply styles to the email. * This is currently more af a proof of concept that we can apply styles to the email.
* We will gradually replace these hardcoded values with styles saved as global styles or styles saved with the email. * We will gradually replace these hardcoded values with styles saved as global styles or styles saved with the email.
* @var string * @var array
*/ */
const DEFAULT_EMAIL_CONTENT_STYLES = " const DEFAULT_EMAIL_CONTENT_STYLES = [
body { font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; } 'typography' => [
p { font-size: 16px; } 'fontFamily' => "Arial, 'Helvetica Neue', Helvetica, sans-serif",
h1 { font-size: 32px; } 'fontSize' => '16px',
h2 { font-size: 24px; } ],
h3 { font-size: 18px; } 'h1' => [
h4 { font-size: 16px; } 'typography' => [
h5 { font-size: 14px; } 'fontSize' => '32px',
h6 { font-size: 12px; } ],
"; ],
'h2' => [
'typography' => [
'fontSize' => '24px',
],
],
'h3' => [
'typography' => [
'fontSize' => '18px',
],
],
'h4' => [
'typography' => [
'fontSize' => '16px',
],
],
'h5' => [
'typography' => [
'fontSize' => '14px',
],
],
'h6' => [
'typography' => [
'fontSize' => '12px',
],
],
];
public function getEmailContentStyles(): string { public function getEmailContentStyles(): array {
return self::DEFAULT_EMAIL_CONTENT_STYLES; return self::DEFAULT_EMAIL_CONTENT_STYLES;
} }