Make sure all compressed vars are decompressed when getting theme styles
The previous implementation was keeping compressed vars (e.g. var:preset|spacing|10) in styles settings and value replacing was able to process only those prefixed var|preset|color. This refactor adds extraction of all compressed values to valid css variables, which can be replaced immediately or later by variables postprocessor. [MAILPOET-6249]
This commit is contained in:
committed by
Jan Lysý
parent
84dab903d7
commit
cec9175c5d
@@ -36,37 +36,32 @@ class ThemeController {
|
||||
return apply_filters('mailpoet_email_editor_theme_json', $theme);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert compressed format presets to valid CSS values.
|
||||
*
|
||||
* @param string $value Value to convert.
|
||||
* @param array $presets List of variable presets from theme.json
|
||||
* @return mixed Converted or original value.
|
||||
*/
|
||||
private function maybeConvertPreset($value, $presets) {
|
||||
if (!is_string($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
if (strstr($value, 'var:preset|color|')) {
|
||||
$value = str_replace('var:preset|color|', '', $value);
|
||||
$value = sprintf('var(--wp--preset--color--%s)', $value);
|
||||
}
|
||||
|
||||
return preg_replace(array_keys($presets), array_values($presets), $value);
|
||||
}
|
||||
|
||||
private function recursiveReplacePresets($values, $presets) {
|
||||
foreach ($values as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
$values[$key] = $this->recursiveReplacePresets($value, $presets);
|
||||
} elseif (is_string($value)) {
|
||||
$values[$key] = preg_replace(array_keys($presets), array_values($presets), $value);
|
||||
} else {
|
||||
$values[$key] = self::maybeConvertPreset($value, $presets);
|
||||
$values[$key] = $value;
|
||||
}
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
|
||||
private function recursiveExtractPresetVariables($styles) {
|
||||
foreach ($styles as $key => $styleValue) {
|
||||
if (is_array($styleValue)) {
|
||||
$styles[$key] = $this->recursiveExtractPresetVariables($styleValue);
|
||||
} elseif (strpos($styleValue, 'var:preset|') === 0) {
|
||||
$styles[$key] = 'var(--wp--' . str_replace('|', '--', str_replace('var:', '', $styleValue)) . ')';
|
||||
} else {
|
||||
$styles[$key] = $styleValue;
|
||||
}
|
||||
}
|
||||
return $styles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get styles for the e-mail.
|
||||
*
|
||||
@@ -96,6 +91,9 @@ class ThemeController {
|
||||
$themeStyles = array_replace_recursive($themeStyles, $templateStyles);
|
||||
}
|
||||
|
||||
// Extract preset variables
|
||||
$themeStyles = $this->recursiveExtractPresetVariables($themeStyles);
|
||||
|
||||
// Replace preset values.
|
||||
if ($convertPresets) {
|
||||
$variables = $this->getVariablesValuesMap();
|
||||
|
Reference in New Issue
Block a user