Extract Theme related methods from SettingsController as ThemeController

I want to avoid the SettingsController becoming some kind of a god class
so in this step, I extracted themejson-related stuff to an extra class.

In the future, we should also move methods for manipulating CSS to a helper.
[MAILPOET-5741]
This commit is contained in:
Rostislav Wolny
2024-02-22 13:03:24 +01:00
committed by Rostislav Wolný
parent aa658782fb
commit b724ef4ac2
8 changed files with 111 additions and 79 deletions

View File

@@ -38,7 +38,18 @@ class SettingsController {
*/
const FLEX_GAP = '16px';
private $availableStylesheets = '';
private string $availableStylesheets = '';
private ThemeController $themeController;
/**
* @param ThemeController $themeController
*/
public function __construct(
ThemeController $themeController
) {
$this->themeController = $themeController;
}
public function getSettings(): array {
$coreDefaultSettings = get_default_block_editor_settings();
@@ -143,59 +154,14 @@ class SettingsController {
}
public function getTheme(): \WP_Theme_JSON {
$coreThemeData = \WP_Theme_JSON_Resolver::get_core_data();
$themeJson = (string)file_get_contents(dirname(__FILE__) . '/theme.json');
$themeJson = json_decode($themeJson, true);
/** @var array $themeJson */
$coreThemeData->merge(new \WP_Theme_JSON($themeJson, 'default'));
return apply_filters('mailpoet_email_editor_theme_json', $coreThemeData);
}
public function getStylesheetForRendering(): string {
$emailThemeSettings = $this->getTheme()->get_settings();
$cssPresets = '';
// Font family classes
foreach ($emailThemeSettings['typography']['fontFamilies']['default'] as $fontFamily) {
$cssPresets .= ".has-{$fontFamily['slug']}-font-family { font-family: {$fontFamily['fontFamily']}; } \n";
}
// Font size classes
foreach ($emailThemeSettings['typography']['fontSizes']['default'] as $fontSize) {
$cssPresets .= ".has-{$fontSize['slug']}-font-size { font-size: {$fontSize['size']}; } \n";
}
// Color palette classes
foreach ($emailThemeSettings['color']['palette']['default'] as $color) {
$cssPresets .= ".has-{$color['slug']}-color { color: {$color['color']}; } \n";
$cssPresets .= ".has-{$color['slug']}-background-color { background-color: {$color['color']}; } \n";
}
// Block specific styles
$cssBlocks = '';
$blocks = $this->getTheme()->get_styles_block_nodes();
foreach ($blocks as $blockMetadata) {
$cssBlocks .= $this->getTheme()->get_styles_for_block($blockMetadata);
}
return $cssPresets . $cssBlocks;
return $this->themeController->getTheme();
}
public function translateSlugToFontSize(string $fontSize): string {
$settings = $this->getTheme()->get_settings();
foreach ($settings['typography']['fontSizes']['default'] as $fontSizeDefinition) {
if ($fontSizeDefinition['slug'] === $fontSize) {
return $fontSizeDefinition['size'];
}
}
return $fontSize;
return $this->themeController->translateSlugToFontSize($fontSize);
}
public function translateSlugToColor(string $colorSlug): string {
$settings = $this->getTheme()->get_settings();
foreach ($settings['color']['palette']['default'] as $colorDefinition) {
if ($colorDefinition['slug'] === $colorSlug) {
return $colorDefinition['color'];
}
}
return $colorSlug;
return $this->themeController->translateSlugToColor($colorSlug);
}
}