Migrate email editor template-preview class to WP Coding Standard

[MAILPOET-6240]
This commit is contained in:
Jan Lysý
2024-10-17 12:12:59 +02:00
committed by Jan Lysý
parent 0a105dbe81
commit a31857ffee

View File

@@ -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\Templates;
use MailPoet\EmailEditor\Engine\Settings_Controller;
@@ -7,27 +13,55 @@ use MailPoet\EmailEditor\Engine\Theme_Controller;
use MailPoet\EmailEditor\Validator\Builder;
use WP_Theme_JSON;
/**
* Template_Preview class.
*/
class Template_Preview {
private Theme_Controller $themeController;
private Settings_Controller $settingsController;
/**
* Provides the theme controller.
*
* @var Theme_Controller
*/
private Theme_Controller $theme_controller;
/**
* Provides the settings controller.
*
* @var Settings_Controller
*/
private Settings_Controller $settings_controller;
/**
* Provides the templates.
*
* @var Templates
*/
private Templates $templates;
/**
* Template_Preview constructor.
*
* @param Theme_Controller $theme_controller Theme controller.
* @param Settings_Controller $settings_controller Theme controller.
* @param Templates $templates Templates.
*/
public function __construct(
Theme_Controller $themeController,
Settings_Controller $settingsController,
Theme_Controller $theme_controller,
Settings_Controller $settings_controller,
Templates $templates
) {
$this->themeController = $themeController;
$this->settingsController = $settingsController;
$this->templates = $templates;
$this->theme_controller = $theme_controller;
$this->settings_controller = $settings_controller;
$this->templates = $templates;
}
/**
* Initializes the class.
*/
public function initialize(): void {
register_rest_field(
'wp_template',
'email_theme_css',
array(
'get_callback' => array( $this, 'getEmailThemePreviewCss' ),
'get_callback' => array( $this, 'get_email_theme_preview_css' ),
'update_callback' => null,
'schema' => Builder::string()->toArray(),
)
@@ -37,21 +71,23 @@ class Template_Preview {
/**
* Generates CSS for preview of email theme
* They are applied in the preview BLockPreview in template selection
*
* @param array $template Template data.
*/
public function getEmailThemePreviewCss( $template ): string {
$editorTheme = clone $this->themeController->get_theme();
$templateTheme = $this->templates->getBlockTemplateTheme( $template['id'], $template['wp_id'] );
if ( is_array( $templateTheme ) ) {
$editorTheme->merge( new WP_Theme_JSON( $templateTheme, 'custom' ) );
public function get_email_theme_preview_css( $template ): string {
$editor_theme = clone $this->theme_controller->get_theme();
$template_theme = $this->templates->getBlockTemplateTheme( $template['id'], $template['wp_id'] );
if ( is_array( $template_theme ) ) {
$editor_theme->merge( new WP_Theme_JSON( $template_theme, 'custom' ) );
}
$editorSettings = $this->settingsController->get_settings();
$additionalCSS = '';
foreach ( $editorSettings['styles'] as $style ) {
$additionalCSS .= $style['css'];
$editor_settings = $this->settings_controller->get_settings();
$additional_css = '';
foreach ( $editor_settings['styles'] as $style ) {
$additional_css .= $style['css'];
}
// Set proper content width for previews
$layoutSettings = $this->themeController->get_layout_settings();
$additionalCSS .= ".is-root-container { width: {$layoutSettings['contentSize']}; margin: 0 auto; }";
return $editorTheme->get_stylesheet() . $additionalCSS;
// Set proper content width for previews.
$layout_settings = $this->theme_controller->get_layout_settings();
$additional_css .= ".is-root-container { width: {$layout_settings['contentSize']}; margin: 0 auto; }";
return $editor_theme->get_stylesheet() . $additional_css;
}
}