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